diff --git a/src/Makefile.am b/src/Makefile.am index a65d2372..157fe5bc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -130,7 +130,7 @@ forked_daapd_SOURCES = main.c \ worker.c worker.h \ settings.c settings.h \ input.h input.c \ - inputs/file_http.c inputs/pipe.c inputs/timer.c \ + inputs/file.c inputs/http.c inputs/pipe.c inputs/timer.c \ outputs.h outputs.c \ outputs/rtp_common.h outputs/rtp_common.c \ outputs/raop.c $(RAOP_VERIFICATION_SRC) \ diff --git a/src/inputs/file.c b/src/inputs/file.c new file mode 100644 index 00000000..5a5ee3d0 --- /dev/null +++ b/src/inputs/file.c @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2017-2020 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 +#include +#include +#include + +#include + +#include "transcode.h" +#include "misc.h" +#include "logger.h" +#include "input.h" + +/*---------------------------- Input implementation --------------------------*/ + +// Important! If you change any of the below then consider if the change also +// should be made in http.c + +static int +setup(struct input_source *source) +{ + struct transcode_ctx *ctx; + + ctx = transcode_setup(XCODE_PCM_NATIVE, NULL, source->data_kind, source->path, source->len_ms, NULL); + if (!ctx) + return -1; + + 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; + + return 0; +} + +static int +stop(struct input_source *source) +{ + struct transcode_ctx *ctx = source->input_ctx; + + transcode_cleanup(&ctx); + + if (source->evbuf) + evbuffer_free(source->evbuf); + + source->input_ctx = NULL; + source->evbuf = NULL; + + return 0; +} + +static int +play(struct input_source *source) +{ + struct transcode_ctx *ctx = source->input_ctx; + int ret; + + // We set "wanted" to 1 because the read size doesn't matter to us + // TODO optimize? + ret = transcode(source->evbuf, NULL, 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; + } + + input_write(source->evbuf, &source->quality, 0); + + return 0; +} + +static int +seek(struct input_source *source, int seek_ms) +{ + return transcode_seek(source->input_ctx, seek_ms); +} + +struct input_definition input_file = +{ + .name = "file", + .type = INPUT_TYPE_FILE, + .disabled = 0, + .setup = setup, + .play = play, + .stop = stop, + .seek = seek, +}; diff --git a/src/inputs/file_http.c b/src/inputs/http.c similarity index 94% rename from src/inputs/file_http.c rename to src/inputs/http.c index 5801e5cf..7b75a928 100644 --- a/src/inputs/file_http.c +++ b/src/inputs/http.c @@ -289,10 +289,20 @@ metadata_prepare(struct input_source *source) /*---------------------------- Input implementation --------------------------*/ +// Important! If you change any of the below then consider if the change also +// should be made in file.c + static int setup(struct input_source *source) { struct transcode_ctx *ctx; + char *url; + + if (http_stream_setup(&url, source->path) < 0) + return -1; + + free(source->path); + source->path = url; ctx = transcode_setup(XCODE_PCM_NATIVE, NULL, source->data_kind, source->path, source->len_ms, NULL); if (!ctx) @@ -309,20 +319,6 @@ setup(struct input_source *source) return 0; } -static int -setup_http(struct input_source *source) -{ - char *url; - - if (http_stream_setup(&url, source->path) < 0) - return -1; - - free(source->path); - source->path = url; - - return setup(source); -} - static int stop(struct input_source *source) { @@ -372,12 +368,6 @@ play(struct input_source *source) static int seek(struct input_source *source, int seek_ms) -{ - return transcode_seek(source->input_ctx, seek_ms); -} - -static int -seek_http(struct input_source *source, int seek_ms) { // Stream is live/unknown length so can't seek. We return 0 anyway, because // it is valid for the input to request a seek, since the input is not @@ -389,7 +379,7 @@ seek_http(struct input_source *source, int seek_ms) } static int -metadata_get_http(struct input_metadata *metadata, struct input_source *source) +metadata_get(struct input_metadata *metadata, struct input_source *source) { pthread_mutex_lock(&prepared_metadata.lock); @@ -404,39 +394,28 @@ metadata_get_http(struct input_metadata *metadata, struct input_source *source) } static int -init_http(void) +init(void) { CHECK_ERR(L_PLAYER, mutex_init(&prepared_metadata.lock)); return 0; } static void -deinit_http(void) +deinit(void) { CHECK_ERR(L_PLAYER, pthread_mutex_destroy(&prepared_metadata.lock)); } -struct input_definition input_file = -{ - .name = "file", - .type = INPUT_TYPE_FILE, - .disabled = 0, - .setup = setup, - .play = play, - .stop = stop, - .seek = seek, -}; - struct input_definition input_http = { .name = "http", .type = INPUT_TYPE_HTTP, .disabled = 0, - .setup = setup_http, + .setup = setup, .play = play, .stop = stop, - .metadata_get = metadata_get_http, - .seek = seek_http, - .init = init_http, - .deinit = deinit_http, + .metadata_get = metadata_get, + .seek = seek, + .init = init, + .deinit = deinit, };