From 9c870e4276157a292673fb1a21f5a9e8a60cb57f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 21 Jan 2013 17:36:19 +0100 Subject: input/rewind: convert to C++ --- Makefile.am | 3 +- src/InputStream.cxx | 2 +- src/input/RewindInputPlugin.cxx | 260 ++++++++++++++++++++++++++++++++++++++++ src/input/RewindInputPlugin.hxx | 37 ++++++ src/input/rewind_input_plugin.c | 256 --------------------------------------- src/input/rewind_input_plugin.h | 37 ------ 6 files changed, 299 insertions(+), 296 deletions(-) create mode 100644 src/input/RewindInputPlugin.cxx create mode 100644 src/input/RewindInputPlugin.hxx delete mode 100644 src/input/rewind_input_plugin.c delete mode 100644 src/input/rewind_input_plugin.h diff --git a/Makefile.am b/Makefile.am index 6896206e8..131a5ef77 100644 --- a/Makefile.am +++ b/Makefile.am @@ -80,7 +80,6 @@ mpd_headers = \ src/input_plugin.h \ src/input_stream.h \ src/input/ffmpeg_input_plugin.h \ - src/input/rewind_input_plugin.h \ src/input/despotify_input_plugin.h \ src/input/cdio_paranoia_input_plugin.h \ src/despotify_utils.h \ @@ -711,7 +710,7 @@ libinput_a_SOURCES = \ src/InputRegistry.cxx src/InputRegistry.hxx \ src/InputStream.cxx \ src/input_internal.c src/input_internal.h \ - src/input/rewind_input_plugin.c \ + src/input/RewindInputPlugin.cxx src/input/RewindInputPlugin.hxx \ src/input/FileInputPlugin.cxx src/input/FileInputPlugin.hxx libinput_a_CPPFLAGS = $(AM_CPPFLAGS) \ diff --git a/src/InputStream.cxx b/src/InputStream.cxx index 64f347555..c81181b6e 100644 --- a/src/InputStream.cxx +++ b/src/InputStream.cxx @@ -21,9 +21,9 @@ #include "input_stream.h" #include "InputRegistry.hxx" #include "input_plugin.h" +#include "input/RewindInputPlugin.hxx" extern "C" { -#include "input/rewind_input_plugin.h" #include "uri.h" } diff --git a/src/input/RewindInputPlugin.cxx b/src/input/RewindInputPlugin.cxx new file mode 100644 index 000000000..6890a8eed --- /dev/null +++ b/src/input/RewindInputPlugin.cxx @@ -0,0 +1,260 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "RewindInputPlugin.hxx" +#include "input_internal.h" +#include "input_plugin.h" +#include "tag.h" + +#include + +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "input_rewind" + +extern const struct input_plugin rewind_input_plugin; + +struct RewindInputStream { + struct input_stream base; + + struct input_stream *input; + + /** + * The read position within the buffer. Undefined as long as + * ReadingFromBuffer() returns false. + */ + size_t head; + + /** + * The write/append position within the buffer. + */ + size_t tail; + + /** + * The size of this buffer is the maximum number of bytes + * which can be rewinded cheaply without passing the "seek" + * call to CURL. + * + * The origin of this buffer is always the beginning of the + * stream (offset 0). + */ + char buffer[64 * 1024]; + + RewindInputStream(input_stream *_input) + :input(_input), tail(0) { + input_stream_init(&base, &rewind_input_plugin, input->uri, + input->mutex, input->cond); + } + + ~RewindInputStream() { + input_stream_close(input); + + input_stream_deinit(&base); + } + + /** + * Are we currently reading from the buffer, and does the + * buffer contain more data for the next read operation? + */ + bool ReadingFromBuffer() const { + return tail > 0 && base.offset < input->offset; + } + + /** + * Copy public attributes from the underlying input stream to the + * "rewind" input stream. This function is called when a method of + * the underlying stream has returned, which may have modified these + * attributes. + */ + void CopyAttributes() { + struct input_stream *dest = &base; + const struct input_stream *src = input; + + assert(dest != src); + assert(src->mime == NULL || dest->mime != src->mime); + + bool dest_ready = dest->ready; + + dest->ready = src->ready; + dest->seekable = src->seekable; + dest->size = src->size; + dest->offset = src->offset; + + if (!dest_ready && src->ready) { + g_free(dest->mime); + dest->mime = g_strdup(src->mime); + } + } +}; + +static void +input_rewind_close(struct input_stream *is) +{ + RewindInputStream *r = (RewindInputStream *)is; + + delete r; +} + +static bool +input_rewind_check(struct input_stream *is, GError **error_r) +{ + RewindInputStream *r = (RewindInputStream *)is; + + return input_stream_check(r->input, error_r); +} + +static void +input_rewind_update(struct input_stream *is) +{ + RewindInputStream *r = (RewindInputStream *)is; + + if (!r->ReadingFromBuffer()) + r->CopyAttributes(); +} + +static struct tag * +input_rewind_tag(struct input_stream *is) +{ + RewindInputStream *r = (RewindInputStream *)is; + + return input_stream_tag(r->input); +} + +static bool +input_rewind_available(struct input_stream *is) +{ + RewindInputStream *r = (RewindInputStream *)is; + + return input_stream_available(r->input); +} + +static size_t +input_rewind_read(struct input_stream *is, void *ptr, size_t size, + GError **error_r) +{ + RewindInputStream *r = (RewindInputStream *)is; + + if (r->ReadingFromBuffer()) { + /* buffered read */ + + assert(r->head == (size_t)is->offset); + assert(r->tail == (size_t)r->input->offset); + + if (size > r->tail - r->head) + size = r->tail - r->head; + + memcpy(ptr, r->buffer + r->head, size); + r->head += size; + is->offset += size; + + return size; + } else { + /* pass method call to underlying stream */ + + size_t nbytes = input_stream_read(r->input, ptr, size, error_r); + + if (r->input->offset > (goffset)sizeof(r->buffer)) + /* disable buffering */ + r->tail = 0; + else if (r->tail == (size_t)is->offset) { + /* append to buffer */ + + memcpy(r->buffer + r->tail, ptr, nbytes); + r->tail += nbytes; + + assert(r->tail == (size_t)r->input->offset); + } + + r->CopyAttributes(); + + return nbytes; + } +} + +static bool +input_rewind_eof(struct input_stream *is) +{ + RewindInputStream *r = (RewindInputStream *)is; + + return !r->ReadingFromBuffer() && input_stream_eof(r->input); +} + +static bool +input_rewind_seek(struct input_stream *is, goffset offset, int whence, + GError **error_r) +{ + RewindInputStream *r = (RewindInputStream *)is; + + assert(is->ready); + + if (whence == SEEK_SET && r->tail > 0 && offset <= (goffset)r->tail) { + /* buffered seek */ + + assert(!r->ReadingFromBuffer() || + r->head == (size_t)is->offset); + assert(r->tail == (size_t)r->input->offset); + + r->head = (size_t)offset; + is->offset = offset; + + return true; + } else { + bool success = input_stream_seek(r->input, offset, whence, + error_r); + r->CopyAttributes(); + + /* disable the buffer, because r->input has left the + buffered range now */ + r->tail = 0; + + return success; + } +} + +const struct input_plugin rewind_input_plugin = { + nullptr, + nullptr, + nullptr, + nullptr, + input_rewind_close, + input_rewind_check, + input_rewind_update, + input_rewind_tag, + input_rewind_available, + input_rewind_read, + input_rewind_eof, + input_rewind_seek, +}; + +struct input_stream * +input_rewind_open(struct input_stream *is) +{ + assert(is != NULL); + assert(is->offset == 0); + + if (is->seekable) + /* seekable resources don't need this plugin */ + return is; + + RewindInputStream *c = new RewindInputStream(is); + return &c->base; +} diff --git a/src/input/RewindInputPlugin.hxx b/src/input/RewindInputPlugin.hxx new file mode 100644 index 000000000..cf21e92f1 --- /dev/null +++ b/src/input/RewindInputPlugin.hxx @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/** \file + * + * A wrapper for an input_stream object which allows cheap buffered + * rewinding. This is useful while detecting the stream codec (let + * each decoder plugin peek a portion from the stream). + */ + +#ifndef MPD_INPUT_REWIND_HXX +#define MPD_INPUT_REWIND_HXX + +#include "check.h" + +struct input_stream; + +struct input_stream * +input_rewind_open(struct input_stream *is); + +#endif diff --git a/src/input/rewind_input_plugin.c b/src/input/rewind_input_plugin.c deleted file mode 100644 index cf06fc57b..000000000 --- a/src/input/rewind_input_plugin.c +++ /dev/null @@ -1,256 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * http://www.musicpd.org - * - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "input/rewind_input_plugin.h" -#include "input_internal.h" -#include "input_plugin.h" -#include "tag.h" - -#include - -#include -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "input_rewind" - -struct input_rewind { - struct input_stream base; - - struct input_stream *input; - - /** - * The read position within the buffer. Undefined as long as - * reading_from_buffer() returns false. - */ - size_t head; - - /** - * The write/append position within the buffer. - */ - size_t tail; - - /** - * The size of this buffer is the maximum number of bytes - * which can be rewinded cheaply without passing the "seek" - * call to CURL. - * - * The origin of this buffer is always the beginning of the - * stream (offset 0). - */ - char buffer[64 * 1024]; -}; - -/** - * Are we currently reading from the buffer, and does the buffer - * contain more data for the next read operation? - */ -static bool -reading_from_buffer(const struct input_rewind *r) -{ - return r->tail > 0 && r->base.offset < r->input->offset; -} - -/** - * Copy public attributes from the underlying input stream to the - * "rewind" input stream. This function is called when a method of - * the underlying stream has returned, which may have modified these - * attributes. - */ -static void -copy_attributes(struct input_rewind *r) -{ - struct input_stream *dest = &r->base; - const struct input_stream *src = r->input; - - assert(dest != src); - assert(src->mime == NULL || dest->mime != src->mime); - - bool dest_ready = dest->ready; - - dest->ready = src->ready; - dest->seekable = src->seekable; - dest->size = src->size; - dest->offset = src->offset; - - if (!dest_ready && src->ready) { - g_free(dest->mime); - dest->mime = g_strdup(src->mime); - } -} - -static void -input_rewind_close(struct input_stream *is) -{ - struct input_rewind *r = (struct input_rewind *)is; - - input_stream_close(r->input); - - input_stream_deinit(&r->base); - g_free(r); -} - -static bool -input_rewind_check(struct input_stream *is, GError **error_r) -{ - struct input_rewind *r = (struct input_rewind *)is; - - return input_stream_check(r->input, error_r); -} - -static void -input_rewind_update(struct input_stream *is) -{ - struct input_rewind *r = (struct input_rewind *)is; - - if (!reading_from_buffer(r)) - copy_attributes(r); -} - -static struct tag * -input_rewind_tag(struct input_stream *is) -{ - struct input_rewind *r = (struct input_rewind *)is; - - return input_stream_tag(r->input); -} - -static bool -input_rewind_available(struct input_stream *is) -{ - struct input_rewind *r = (struct input_rewind *)is; - - return input_stream_available(r->input); -} - -static size_t -input_rewind_read(struct input_stream *is, void *ptr, size_t size, - GError **error_r) -{ - struct input_rewind *r = (struct input_rewind *)is; - - if (reading_from_buffer(r)) { - /* buffered read */ - - assert(r->head == (size_t)is->offset); - assert(r->tail == (size_t)r->input->offset); - - if (size > r->tail - r->head) - size = r->tail - r->head; - - memcpy(ptr, r->buffer + r->head, size); - r->head += size; - is->offset += size; - - return size; - } else { - /* pass method call to underlying stream */ - - size_t nbytes = input_stream_read(r->input, ptr, size, error_r); - - if (r->input->offset > (goffset)sizeof(r->buffer)) - /* disable buffering */ - r->tail = 0; - else if (r->tail == (size_t)is->offset) { - /* append to buffer */ - - memcpy(r->buffer + r->tail, ptr, nbytes); - r->tail += nbytes; - - assert(r->tail == (size_t)r->input->offset); - } - - copy_attributes(r); - - return nbytes; - } -} - -static bool -input_rewind_eof(struct input_stream *is) -{ - struct input_rewind *r = (struct input_rewind *)is; - - return !reading_from_buffer(r) && input_stream_eof(r->input); -} - -static bool -input_rewind_seek(struct input_stream *is, goffset offset, int whence, - GError **error_r) -{ - struct input_rewind *r = (struct input_rewind *)is; - - assert(is->ready); - - if (whence == SEEK_SET && r->tail > 0 && offset <= (goffset)r->tail) { - /* buffered seek */ - - assert(!reading_from_buffer(r) || - r->head == (size_t)is->offset); - assert(r->tail == (size_t)r->input->offset); - - r->head = (size_t)offset; - is->offset = offset; - - return true; - } else { - bool success = input_stream_seek(r->input, offset, whence, - error_r); - copy_attributes(r); - - /* disable the buffer, because r->input has left the - buffered range now */ - r->tail = 0; - - return success; - } -} - -static const struct input_plugin rewind_input_plugin = { - .close = input_rewind_close, - .check = input_rewind_check, - .update = input_rewind_update, - .tag = input_rewind_tag, - .available = input_rewind_available, - .read = input_rewind_read, - .eof = input_rewind_eof, - .seek = input_rewind_seek, -}; - -struct input_stream * -input_rewind_open(struct input_stream *is) -{ - struct input_rewind *c; - - assert(is != NULL); - assert(is->offset == 0); - - if (is->seekable) - /* seekable resources don't need this plugin */ - return is; - - c = g_new(struct input_rewind, 1); - input_stream_init(&c->base, &rewind_input_plugin, is->uri, - is->mutex, is->cond); - c->tail = 0; - c->input = is; - - return &c->base; -} diff --git a/src/input/rewind_input_plugin.h b/src/input/rewind_input_plugin.h deleted file mode 100644 index 83abe257a..000000000 --- a/src/input/rewind_input_plugin.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * http://www.musicpd.org - * - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/** \file - * - * A wrapper for an input_stream object which allows cheap buffered - * rewinding. This is useful while detecting the stream codec (let - * each decoder plugin peek a portion from the stream). - */ - -#ifndef MPD_INPUT_REWIND_H -#define MPD_INPUT_REWIND_H - -#include "check.h" - -struct input_stream; - -struct input_stream * -input_rewind_open(struct input_stream *is); - -#endif -- cgit v1.2.3