2016-12-26 13:30:29 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Espen Jurgensen
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
|
|
|
#include "transcode.h"
|
|
|
|
#include "http.h"
|
2017-01-27 05:02:10 -05:00
|
|
|
#include "misc.h"
|
2019-01-11 13:34:36 -05:00
|
|
|
#include "logger.h"
|
2016-12-26 13:30:29 -05:00
|
|
|
#include "input.h"
|
|
|
|
|
|
|
|
static int
|
2019-02-16 13:34:36 -05:00
|
|
|
setup(struct input_source *source)
|
2016-12-26 13:30:29 -05:00
|
|
|
{
|
2019-02-16 13:34:36 -05:00
|
|
|
struct transcode_ctx *ctx;
|
|
|
|
|
|
|
|
ctx = transcode_setup(XCODE_PCM_NATIVE, source->data_kind, source->path, source->len_ms, NULL);
|
|
|
|
if (!ctx)
|
2016-12-26 13:30:29 -05:00
|
|
|
return -1;
|
|
|
|
|
2019-02-16 13:34:36 -05:00
|
|
|
CHECK_NULL(L_PLAYER, source->evbuf = evbuffer_new());
|
|
|
|
|
|
|
|
source->quality.sample_rate = transcode_encode_query(ctx->encode_ctx, "sample_rate");
|
|
|
|
source->quality.bits_per_sample = transcode_encode_query(ctx->encode_ctx, "bits_per_sample");
|
|
|
|
source->quality.channels = transcode_encode_query(ctx->encode_ctx, "channels");
|
|
|
|
|
|
|
|
source->input_ctx = ctx;
|
2016-12-26 13:30:29 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-02-16 13:34:36 -05:00
|
|
|
setup_http(struct input_source *source)
|
2016-12-26 13:30:29 -05:00
|
|
|
{
|
|
|
|
char *url;
|
|
|
|
|
2019-02-16 13:34:36 -05:00
|
|
|
if (http_stream_setup(&url, source->path) < 0)
|
2016-12-26 13:30:29 -05:00
|
|
|
return -1;
|
|
|
|
|
2019-02-16 13:34:36 -05:00
|
|
|
free(source->path);
|
|
|
|
source->path = url;
|
2016-12-26 13:30:29 -05:00
|
|
|
|
2019-02-16 13:34:36 -05:00
|
|
|
return setup(source);
|
2016-12-26 13:30:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-02-16 13:34:36 -05:00
|
|
|
stop(struct input_source *source)
|
2016-12-26 13:30:29 -05:00
|
|
|
{
|
2019-02-16 13:34:36 -05:00
|
|
|
struct transcode_ctx *ctx = source->input_ctx;
|
2016-12-26 13:30:29 -05:00
|
|
|
|
2019-02-16 13:34:36 -05:00
|
|
|
transcode_cleanup(&ctx);
|
|
|
|
evbuffer_free(source->evbuf);
|
2016-12-26 13:30:29 -05:00
|
|
|
|
2019-02-16 13:34:36 -05:00
|
|
|
source->input_ctx = NULL;
|
2016-12-26 13:30:29 -05:00
|
|
|
|
2019-02-16 13:34:36 -05:00
|
|
|
return 0;
|
2016-12-26 13:30:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-02-16 13:34:36 -05:00
|
|
|
play(struct input_source *source)
|
2016-12-26 13:30:29 -05:00
|
|
|
{
|
2019-02-16 13:34:36 -05:00
|
|
|
struct transcode_ctx *ctx = source->input_ctx;
|
|
|
|
int icy_timer;
|
|
|
|
int ret;
|
|
|
|
short flags;
|
2017-02-26 17:41:30 -05:00
|
|
|
|
2019-02-16 13:34:36 -05:00
|
|
|
ret = input_wait();
|
|
|
|
if (ret < 0)
|
|
|
|
return 0; // Loop, input_buffer is not ready for writing
|
|
|
|
|
|
|
|
// We set "wanted" to 1 because the read size doesn't matter to us
|
|
|
|
// TODO optimize?
|
|
|
|
ret = transcode(source->evbuf, &icy_timer, ctx, 1);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
input_write(source->evbuf, &source->quality, INPUT_FLAG_EOF);
|
|
|
|
stop(source);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
input_write(NULL, NULL, INPUT_FLAG_ERROR);
|
|
|
|
stop(source);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = (icy_timer ? INPUT_FLAG_METADATA : 0);
|
2016-12-26 13:30:29 -05:00
|
|
|
|
2019-02-16 13:34:36 -05:00
|
|
|
input_write(source->evbuf, &source->quality, flags);
|
2016-12-26 13:30:29 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-02-16 13:34:36 -05:00
|
|
|
seek(struct input_source *source, int seek_ms)
|
2016-12-26 13:30:29 -05:00
|
|
|
{
|
2019-02-16 13:34:36 -05:00
|
|
|
return transcode_seek(source->input_ctx, seek_ms);
|
2016-12-26 13:30:29 -05:00
|
|
|
}
|
|
|
|
|
2017-01-22 17:23:18 -05:00
|
|
|
static int
|
2019-02-16 13:34:36 -05:00
|
|
|
metadata_get_http(struct input_metadata *metadata, struct input_source *source)
|
2017-01-22 17:23:18 -05:00
|
|
|
{
|
|
|
|
struct http_icy_metadata *m;
|
|
|
|
int changed;
|
|
|
|
|
2019-02-16 13:34:36 -05:00
|
|
|
m = transcode_metadata(source->input_ctx, &changed);
|
2017-01-22 17:23:18 -05:00
|
|
|
if (!m)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!changed)
|
|
|
|
{
|
|
|
|
http_icy_metadata_free(m, 0);
|
|
|
|
return -1; // TODO Perhaps a problem since this prohibits the player updating metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->artist)
|
2017-01-27 05:02:10 -05:00
|
|
|
swap_pointers(&metadata->artist, &m->artist);
|
2017-01-22 17:23:18 -05:00
|
|
|
// Note we map title to album, because clients should show stream name as titel
|
|
|
|
if (m->title)
|
2017-01-27 05:02:10 -05:00
|
|
|
swap_pointers(&metadata->album, &m->title);
|
2017-01-22 17:23:18 -05:00
|
|
|
if (m->artwork_url)
|
2017-01-27 05:02:10 -05:00
|
|
|
swap_pointers(&metadata->artwork_url, &m->artwork_url);
|
2017-01-22 17:23:18 -05:00
|
|
|
|
|
|
|
http_icy_metadata_free(m, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-26 13:30:29 -05:00
|
|
|
struct input_definition input_file =
|
|
|
|
{
|
|
|
|
.name = "file",
|
|
|
|
.type = INPUT_TYPE_FILE,
|
|
|
|
.disabled = 0,
|
|
|
|
.setup = setup,
|
2019-02-16 13:34:36 -05:00
|
|
|
.play = play,
|
2016-12-26 13:30:29 -05:00
|
|
|
.stop = stop,
|
|
|
|
.seek = seek,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct input_definition input_http =
|
|
|
|
{
|
|
|
|
.name = "http",
|
|
|
|
.type = INPUT_TYPE_HTTP,
|
|
|
|
.disabled = 0,
|
2017-01-22 17:23:18 -05:00
|
|
|
.setup = setup_http,
|
2019-02-16 13:34:36 -05:00
|
|
|
.play = play,
|
2016-12-26 13:30:29 -05:00
|
|
|
.stop = stop,
|
2017-01-22 17:23:18 -05:00
|
|
|
.metadata_get = metadata_get_http,
|
2016-12-26 13:30:29 -05:00
|
|
|
};
|