Commit 9c870e42 authored by Max Kellermann's avatar Max Kellermann

input/rewind: convert to C++

parent 5cffe693
...@@ -80,7 +80,6 @@ mpd_headers = \ ...@@ -80,7 +80,6 @@ mpd_headers = \
src/input_plugin.h \ src/input_plugin.h \
src/input_stream.h \ src/input_stream.h \
src/input/ffmpeg_input_plugin.h \ src/input/ffmpeg_input_plugin.h \
src/input/rewind_input_plugin.h \
src/input/despotify_input_plugin.h \ src/input/despotify_input_plugin.h \
src/input/cdio_paranoia_input_plugin.h \ src/input/cdio_paranoia_input_plugin.h \
src/despotify_utils.h \ src/despotify_utils.h \
...@@ -711,7 +710,7 @@ libinput_a_SOURCES = \ ...@@ -711,7 +710,7 @@ libinput_a_SOURCES = \
src/InputRegistry.cxx src/InputRegistry.hxx \ src/InputRegistry.cxx src/InputRegistry.hxx \
src/InputStream.cxx \ src/InputStream.cxx \
src/input_internal.c src/input_internal.h \ 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 src/input/FileInputPlugin.cxx src/input/FileInputPlugin.hxx
libinput_a_CPPFLAGS = $(AM_CPPFLAGS) \ libinput_a_CPPFLAGS = $(AM_CPPFLAGS) \
......
...@@ -21,9 +21,9 @@ ...@@ -21,9 +21,9 @@
#include "input_stream.h" #include "input_stream.h"
#include "InputRegistry.hxx" #include "InputRegistry.hxx"
#include "input_plugin.h" #include "input_plugin.h"
#include "input/RewindInputPlugin.hxx"
extern "C" { extern "C" {
#include "input/rewind_input_plugin.h"
#include "uri.h" #include "uri.h"
} }
......
/* /*
* Copyright (C) 2003-2011 The Music Player Daemon Project * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
*/ */
#include "config.h" #include "config.h"
#include "input/rewind_input_plugin.h" #include "RewindInputPlugin.hxx"
#include "input_internal.h" #include "input_internal.h"
#include "input_plugin.h" #include "input_plugin.h"
#include "tag.h" #include "tag.h"
...@@ -31,14 +31,16 @@ ...@@ -31,14 +31,16 @@
#undef G_LOG_DOMAIN #undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "input_rewind" #define G_LOG_DOMAIN "input_rewind"
struct input_rewind { extern const struct input_plugin rewind_input_plugin;
struct RewindInputStream {
struct input_stream base; struct input_stream base;
struct input_stream *input; struct input_stream *input;
/** /**
* The read position within the buffer. Undefined as long as * The read position within the buffer. Undefined as long as
* reading_from_buffer() returns false. * ReadingFromBuffer() returns false.
*/ */
size_t head; size_t head;
...@@ -56,61 +58,66 @@ struct input_rewind { ...@@ -56,61 +58,66 @@ struct input_rewind {
* stream (offset 0). * stream (offset 0).
*/ */
char buffer[64 * 1024]; char buffer[64 * 1024];
};
/** RewindInputStream(input_stream *_input)
* Are we currently reading from the buffer, and does the buffer :input(_input), tail(0) {
* contain more data for the next read operation? input_stream_init(&base, &rewind_input_plugin, input->uri,
*/ input->mutex, input->cond);
static bool }
reading_from_buffer(const struct input_rewind *r)
{
return r->tail > 0 && r->base.offset < r->input->offset;
}
/** ~RewindInputStream() {
* Copy public attributes from the underlying input stream to the input_stream_close(input);
* "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); input_stream_deinit(&base);
assert(src->mime == NULL || dest->mime != src->mime); }
bool dest_ready = dest->ready; /**
* 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->ready = src->ready;
dest->seekable = src->seekable; dest->seekable = src->seekable;
dest->size = src->size; dest->size = src->size;
dest->offset = src->offset; dest->offset = src->offset;
if (!dest_ready && src->ready) { if (!dest_ready && src->ready) {
g_free(dest->mime); g_free(dest->mime);
dest->mime = g_strdup(src->mime); dest->mime = g_strdup(src->mime);
}
} }
} };
static void static void
input_rewind_close(struct input_stream *is) input_rewind_close(struct input_stream *is)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
input_stream_close(r->input); delete r;
input_stream_deinit(&r->base);
g_free(r);
} }
static bool static bool
input_rewind_check(struct input_stream *is, GError **error_r) input_rewind_check(struct input_stream *is, GError **error_r)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
return input_stream_check(r->input, error_r); return input_stream_check(r->input, error_r);
} }
...@@ -118,16 +125,16 @@ input_rewind_check(struct input_stream *is, GError **error_r) ...@@ -118,16 +125,16 @@ input_rewind_check(struct input_stream *is, GError **error_r)
static void static void
input_rewind_update(struct input_stream *is) input_rewind_update(struct input_stream *is)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
if (!reading_from_buffer(r)) if (!r->ReadingFromBuffer())
copy_attributes(r); r->CopyAttributes();
} }
static struct tag * static struct tag *
input_rewind_tag(struct input_stream *is) input_rewind_tag(struct input_stream *is)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
return input_stream_tag(r->input); return input_stream_tag(r->input);
} }
...@@ -135,7 +142,7 @@ input_rewind_tag(struct input_stream *is) ...@@ -135,7 +142,7 @@ input_rewind_tag(struct input_stream *is)
static bool static bool
input_rewind_available(struct input_stream *is) input_rewind_available(struct input_stream *is)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
return input_stream_available(r->input); return input_stream_available(r->input);
} }
...@@ -144,9 +151,9 @@ static size_t ...@@ -144,9 +151,9 @@ static size_t
input_rewind_read(struct input_stream *is, void *ptr, size_t size, input_rewind_read(struct input_stream *is, void *ptr, size_t size,
GError **error_r) GError **error_r)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
if (reading_from_buffer(r)) { if (r->ReadingFromBuffer()) {
/* buffered read */ /* buffered read */
assert(r->head == (size_t)is->offset); assert(r->head == (size_t)is->offset);
...@@ -177,7 +184,7 @@ input_rewind_read(struct input_stream *is, void *ptr, size_t size, ...@@ -177,7 +184,7 @@ input_rewind_read(struct input_stream *is, void *ptr, size_t size,
assert(r->tail == (size_t)r->input->offset); assert(r->tail == (size_t)r->input->offset);
} }
copy_attributes(r); r->CopyAttributes();
return nbytes; return nbytes;
} }
...@@ -186,23 +193,23 @@ input_rewind_read(struct input_stream *is, void *ptr, size_t size, ...@@ -186,23 +193,23 @@ input_rewind_read(struct input_stream *is, void *ptr, size_t size,
static bool static bool
input_rewind_eof(struct input_stream *is) input_rewind_eof(struct input_stream *is)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
return !reading_from_buffer(r) && input_stream_eof(r->input); return !r->ReadingFromBuffer() && input_stream_eof(r->input);
} }
static bool static bool
input_rewind_seek(struct input_stream *is, goffset offset, int whence, input_rewind_seek(struct input_stream *is, goffset offset, int whence,
GError **error_r) GError **error_r)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
assert(is->ready); assert(is->ready);
if (whence == SEEK_SET && r->tail > 0 && offset <= (goffset)r->tail) { if (whence == SEEK_SET && r->tail > 0 && offset <= (goffset)r->tail) {
/* buffered seek */ /* buffered seek */
assert(!reading_from_buffer(r) || assert(!r->ReadingFromBuffer() ||
r->head == (size_t)is->offset); r->head == (size_t)is->offset);
assert(r->tail == (size_t)r->input->offset); assert(r->tail == (size_t)r->input->offset);
...@@ -213,7 +220,7 @@ input_rewind_seek(struct input_stream *is, goffset offset, int whence, ...@@ -213,7 +220,7 @@ input_rewind_seek(struct input_stream *is, goffset offset, int whence,
} else { } else {
bool success = input_stream_seek(r->input, offset, whence, bool success = input_stream_seek(r->input, offset, whence,
error_r); error_r);
copy_attributes(r); r->CopyAttributes();
/* disable the buffer, because r->input has left the /* disable the buffer, because r->input has left the
buffered range now */ buffered range now */
...@@ -223,22 +230,24 @@ input_rewind_seek(struct input_stream *is, goffset offset, int whence, ...@@ -223,22 +230,24 @@ input_rewind_seek(struct input_stream *is, goffset offset, int whence,
} }
} }
static const struct input_plugin rewind_input_plugin = { const struct input_plugin rewind_input_plugin = {
.close = input_rewind_close, nullptr,
.check = input_rewind_check, nullptr,
.update = input_rewind_update, nullptr,
.tag = input_rewind_tag, nullptr,
.available = input_rewind_available, input_rewind_close,
.read = input_rewind_read, input_rewind_check,
.eof = input_rewind_eof, input_rewind_update,
.seek = input_rewind_seek, input_rewind_tag,
input_rewind_available,
input_rewind_read,
input_rewind_eof,
input_rewind_seek,
}; };
struct input_stream * struct input_stream *
input_rewind_open(struct input_stream *is) input_rewind_open(struct input_stream *is)
{ {
struct input_rewind *c;
assert(is != NULL); assert(is != NULL);
assert(is->offset == 0); assert(is->offset == 0);
...@@ -246,11 +255,6 @@ input_rewind_open(struct input_stream *is) ...@@ -246,11 +255,6 @@ input_rewind_open(struct input_stream *is)
/* seekable resources don't need this plugin */ /* seekable resources don't need this plugin */
return is; return is;
c = g_new(struct input_rewind, 1); RewindInputStream *c = new RewindInputStream(is);
input_stream_init(&c->base, &rewind_input_plugin, is->uri,
is->mutex, is->cond);
c->tail = 0;
c->input = is;
return &c->base; return &c->base;
} }
/* /*
* Copyright (C) 2003-2011 The Music Player Daemon Project * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
...@@ -24,8 +24,8 @@ ...@@ -24,8 +24,8 @@
* each decoder plugin peek a portion from the stream). * each decoder plugin peek a portion from the stream).
*/ */
#ifndef MPD_INPUT_REWIND_H #ifndef MPD_INPUT_REWIND_HXX
#define MPD_INPUT_REWIND_H #define MPD_INPUT_REWIND_HXX
#include "check.h" #include "check.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment