From 5cffe693b0b4412a6a3652d0864a38944fc054ec Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 21 Jan 2013 10:49:42 +0100 Subject: input/file,mms: convert to C++ --- src/input/FileInputPlugin.cxx | 166 ++++++++++++++++++++++++++++++++++++++++++ src/input/FileInputPlugin.hxx | 25 +++++++ src/input/MmsInputPlugin.cxx | 148 +++++++++++++++++++++++++++++++++++++ src/input/MmsInputPlugin.hxx | 25 +++++++ src/input/file_input_plugin.c | 155 --------------------------------------- src/input/file_input_plugin.h | 25 ------- src/input/mms_input_plugin.c | 140 ----------------------------------- src/input/mms_input_plugin.h | 25 ------- 8 files changed, 364 insertions(+), 345 deletions(-) create mode 100644 src/input/FileInputPlugin.cxx create mode 100644 src/input/FileInputPlugin.hxx create mode 100644 src/input/MmsInputPlugin.cxx create mode 100644 src/input/MmsInputPlugin.hxx delete mode 100644 src/input/file_input_plugin.c delete mode 100644 src/input/file_input_plugin.h delete mode 100644 src/input/mms_input_plugin.c delete mode 100644 src/input/mms_input_plugin.h (limited to 'src/input') diff --git a/src/input/FileInputPlugin.cxx b/src/input/FileInputPlugin.cxx new file mode 100644 index 000000000..edf96e500 --- /dev/null +++ b/src/input/FileInputPlugin.cxx @@ -0,0 +1,166 @@ +/* + * 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" /* must be first for large file support */ +#include "FileInputPlugin.hxx" +#include "input_internal.h" +#include "input_plugin.h" +#include "fd_util.h" +#include "open.h" +#include "io_error.h" + +#include +#include +#include +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "input_file" + +struct FileInputStream { + struct input_stream base; + + int fd; + + FileInputStream(const char *path, int _fd, off_t size, + GMutex *mutex, GCond *cond) + :fd(_fd) { + input_stream_init(&base, &input_plugin_file, path, + mutex, cond); + + base.size = size; + base.seekable = true; + base.ready = true; + } + + ~FileInputStream() { + close(fd); + input_stream_deinit(&base); + } +}; + +static struct input_stream * +input_file_open(const char *filename, + GMutex *mutex, GCond *cond, + GError **error_r) +{ + int fd, ret; + struct stat st; + + if (!g_path_is_absolute(filename)) + return nullptr; + + fd = open_cloexec(filename, O_RDONLY|O_BINARY, 0); + if (fd < 0) { + if (errno != ENOENT && errno != ENOTDIR) + g_set_error(error_r, errno_quark(), errno, + "Failed to open \"%s\": %s", + filename, g_strerror(errno)); + return nullptr; + } + + ret = fstat(fd, &st); + if (ret < 0) { + g_set_error(error_r, errno_quark(), errno, + "Failed to stat \"%s\": %s", + filename, g_strerror(errno)); + close(fd); + return nullptr; + } + + if (!S_ISREG(st.st_mode)) { + g_set_error(error_r, errno_quark(), 0, + "Not a regular file: %s", filename); + close(fd); + return nullptr; + } + +#ifdef POSIX_FADV_SEQUENTIAL + posix_fadvise(fd, (off_t)0, st.st_size, POSIX_FADV_SEQUENTIAL); +#endif + + FileInputStream *fis = new FileInputStream(filename, fd, st.st_size, + mutex, cond); + return &fis->base; +} + +static bool +input_file_seek(struct input_stream *is, goffset offset, int whence, + GError **error_r) +{ + FileInputStream *fis = (FileInputStream *)is; + + offset = (goffset)lseek(fis->fd, (off_t)offset, whence); + if (offset < 0) { + g_set_error(error_r, errno_quark(), errno, + "Failed to seek: %s", g_strerror(errno)); + return false; + } + + is->offset = offset; + return true; +} + +static size_t +input_file_read(struct input_stream *is, void *ptr, size_t size, + GError **error_r) +{ + FileInputStream *fis = (FileInputStream *)is; + ssize_t nbytes; + + nbytes = read(fis->fd, ptr, size); + if (nbytes < 0) { + g_set_error(error_r, errno_quark(), errno, + "Failed to read: %s", g_strerror(errno)); + return 0; + } + + is->offset += nbytes; + return (size_t)nbytes; +} + +static void +input_file_close(struct input_stream *is) +{ + FileInputStream *fis = (FileInputStream *)is; + + delete fis; +} + +static bool +input_file_eof(struct input_stream *is) +{ + return is->offset >= is->size; +} + +const struct input_plugin input_plugin_file = { + "file", + nullptr, + nullptr, + input_file_open, + input_file_close, + nullptr, + nullptr, + nullptr, + nullptr, + input_file_read, + input_file_eof, + input_file_seek, +}; diff --git a/src/input/FileInputPlugin.hxx b/src/input/FileInputPlugin.hxx new file mode 100644 index 000000000..aacfd0b5d --- /dev/null +++ b/src/input/FileInputPlugin.hxx @@ -0,0 +1,25 @@ +/* + * 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. + */ + +#ifndef MPD_INPUT_FILE_HXX +#define MPD_INPUT_FILE_HXX + +extern const struct input_plugin input_plugin_file; + +#endif diff --git a/src/input/MmsInputPlugin.cxx b/src/input/MmsInputPlugin.cxx new file mode 100644 index 000000000..45cdd072c --- /dev/null +++ b/src/input/MmsInputPlugin.cxx @@ -0,0 +1,148 @@ +/* + * 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 "MmsInputPlugin.hxx" +#include "input_internal.h" +#include "input_plugin.h" + +#include +#include + +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "input_mms" + +struct MmsInputStream { + struct input_stream base; + + mmsx_t *mms; + + bool eof; + + MmsInputStream(const char *uri, + GMutex *mutex, GCond *cond, + mmsx_t *_mms) + :mms(_mms), eof(false) { + input_stream_init(&base, &input_plugin_mms, uri, mutex, cond); + + /* XX is this correct? at least this selects the ffmpeg + decoder, which seems to work fine*/ + base.mime = g_strdup("audio/x-ms-wma"); + + base.ready = true; + } + + ~MmsInputStream() { + mmsx_close(mms); + input_stream_deinit(&base); + } +}; + +static inline GQuark +mms_quark(void) +{ + return g_quark_from_static_string("mms"); +} + +static struct input_stream * +input_mms_open(const char *url, + GMutex *mutex, GCond *cond, + GError **error_r) +{ + if (!g_str_has_prefix(url, "mms://") && + !g_str_has_prefix(url, "mmsh://") && + !g_str_has_prefix(url, "mmst://") && + !g_str_has_prefix(url, "mmsu://")) + return nullptr; + + const auto mms = mmsx_connect(nullptr, nullptr, url, 128 * 1024); + if (mms == nullptr) { + g_set_error(error_r, mms_quark(), 0, "mmsx_connect() failed"); + return nullptr; + } + + auto m = new MmsInputStream(url, mutex, cond, mms); + return &m->base; +} + +static size_t +input_mms_read(struct input_stream *is, void *ptr, size_t size, + GError **error_r) +{ + MmsInputStream *m = (MmsInputStream *)is; + int ret; + + ret = mmsx_read(nullptr, m->mms, (char *)ptr, size); + if (ret <= 0) { + if (ret < 0) { + g_set_error(error_r, mms_quark(), errno, + "mmsx_read() failed: %s", + g_strerror(errno)); + } + + m->eof = true; + return false; + } + + is->offset += ret; + + return (size_t)ret; +} + +static void +input_mms_close(struct input_stream *is) +{ + MmsInputStream *m = (MmsInputStream *)is; + + delete m; +} + +static bool +input_mms_eof(struct input_stream *is) +{ + MmsInputStream *m = (MmsInputStream *)is; + + return m->eof; +} + +static bool +input_mms_seek(G_GNUC_UNUSED struct input_stream *is, + G_GNUC_UNUSED goffset offset, G_GNUC_UNUSED int whence, + G_GNUC_UNUSED GError **error_r) +{ + return false; +} + +const struct input_plugin input_plugin_mms = { + "mms", + nullptr, + nullptr, + input_mms_open, + input_mms_close, + nullptr, + nullptr, + nullptr, + nullptr, + input_mms_read, + input_mms_eof, + input_mms_seek, +}; diff --git a/src/input/MmsInputPlugin.hxx b/src/input/MmsInputPlugin.hxx new file mode 100644 index 000000000..d6aa593f2 --- /dev/null +++ b/src/input/MmsInputPlugin.hxx @@ -0,0 +1,25 @@ +/* + * 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. + */ + +#ifndef INPUT_MMS_H +#define INPUT_MMS_H + +extern const struct input_plugin input_plugin_mms; + +#endif diff --git a/src/input/file_input_plugin.c b/src/input/file_input_plugin.c deleted file mode 100644 index e130230a7..000000000 --- a/src/input/file_input_plugin.c +++ /dev/null @@ -1,155 +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" /* must be first for large file support */ -#include "input/file_input_plugin.h" -#include "input_internal.h" -#include "input_plugin.h" -#include "fd_util.h" -#include "open.h" -#include "io_error.h" - -#include -#include -#include -#include -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "input_file" - -struct file_input_stream { - struct input_stream base; - - int fd; -}; - -static struct input_stream * -input_file_open(const char *filename, - GMutex *mutex, GCond *cond, - GError **error_r) -{ - int fd, ret; - struct stat st; - struct file_input_stream *fis; - - if (!g_path_is_absolute(filename)) - return NULL; - - fd = open_cloexec(filename, O_RDONLY|O_BINARY, 0); - if (fd < 0) { - if (errno != ENOENT && errno != ENOTDIR) - g_set_error(error_r, errno_quark(), errno, - "Failed to open \"%s\": %s", - filename, g_strerror(errno)); - return NULL; - } - - ret = fstat(fd, &st); - if (ret < 0) { - g_set_error(error_r, errno_quark(), errno, - "Failed to stat \"%s\": %s", - filename, g_strerror(errno)); - close(fd); - return NULL; - } - - if (!S_ISREG(st.st_mode)) { - g_set_error(error_r, errno_quark(), 0, - "Not a regular file: %s", filename); - close(fd); - return NULL; - } - -#ifdef POSIX_FADV_SEQUENTIAL - posix_fadvise(fd, (off_t)0, st.st_size, POSIX_FADV_SEQUENTIAL); -#endif - - fis = g_new(struct file_input_stream, 1); - input_stream_init(&fis->base, &input_plugin_file, filename, - mutex, cond); - - fis->base.size = st.st_size; - fis->base.seekable = true; - fis->base.ready = true; - - fis->fd = fd; - - return &fis->base; -} - -static bool -input_file_seek(struct input_stream *is, goffset offset, int whence, - GError **error_r) -{ - struct file_input_stream *fis = (struct file_input_stream *)is; - - offset = (goffset)lseek(fis->fd, (off_t)offset, whence); - if (offset < 0) { - g_set_error(error_r, errno_quark(), errno, - "Failed to seek: %s", g_strerror(errno)); - return false; - } - - is->offset = offset; - return true; -} - -static size_t -input_file_read(struct input_stream *is, void *ptr, size_t size, - GError **error_r) -{ - struct file_input_stream *fis = (struct file_input_stream *)is; - ssize_t nbytes; - - nbytes = read(fis->fd, ptr, size); - if (nbytes < 0) { - g_set_error(error_r, errno_quark(), errno, - "Failed to read: %s", g_strerror(errno)); - return 0; - } - - is->offset += nbytes; - return (size_t)nbytes; -} - -static void -input_file_close(struct input_stream *is) -{ - struct file_input_stream *fis = (struct file_input_stream *)is; - - close(fis->fd); - input_stream_deinit(&fis->base); - g_free(fis); -} - -static bool -input_file_eof(struct input_stream *is) -{ - return is->offset >= is->size; -} - -const struct input_plugin input_plugin_file = { - .name = "file", - .open = input_file_open, - .close = input_file_close, - .read = input_file_read, - .eof = input_file_eof, - .seek = input_file_seek, -}; diff --git a/src/input/file_input_plugin.h b/src/input/file_input_plugin.h deleted file mode 100644 index f24769d57..000000000 --- a/src/input/file_input_plugin.h +++ /dev/null @@ -1,25 +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. - */ - -#ifndef MPD_INPUT_FILE_H -#define MPD_INPUT_FILE_H - -extern const struct input_plugin input_plugin_file; - -#endif diff --git a/src/input/mms_input_plugin.c b/src/input/mms_input_plugin.c deleted file mode 100644 index cff15125b..000000000 --- a/src/input/mms_input_plugin.c +++ /dev/null @@ -1,140 +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/mms_input_plugin.h" -#include "input_internal.h" -#include "input_plugin.h" - -#include -#include - -#include -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "input_mms" - -struct input_mms { - struct input_stream base; - - mmsx_t *mms; - - bool eof; -}; - -static inline GQuark -mms_quark(void) -{ - return g_quark_from_static_string("mms"); -} - -static struct input_stream * -input_mms_open(const char *url, - GMutex *mutex, GCond *cond, - GError **error_r) -{ - struct input_mms *m; - - if (!g_str_has_prefix(url, "mms://") && - !g_str_has_prefix(url, "mmsh://") && - !g_str_has_prefix(url, "mmst://") && - !g_str_has_prefix(url, "mmsu://")) - return NULL; - - m = g_new(struct input_mms, 1); - input_stream_init(&m->base, &input_plugin_mms, url, - mutex, cond); - - m->mms = mmsx_connect(NULL, NULL, url, 128 * 1024); - if (m->mms == NULL) { - g_free(m); - g_set_error(error_r, mms_quark(), 0, "mmsx_connect() failed"); - return NULL; - } - - m->eof = false; - - /* XX is this correct? at least this selects the ffmpeg - decoder, which seems to work fine*/ - m->base.mime = g_strdup("audio/x-ms-wma"); - - m->base.ready = true; - - return &m->base; -} - -static size_t -input_mms_read(struct input_stream *is, void *ptr, size_t size, - GError **error_r) -{ - struct input_mms *m = (struct input_mms *)is; - int ret; - - ret = mmsx_read(NULL, m->mms, ptr, size); - if (ret <= 0) { - if (ret < 0) { - g_set_error(error_r, mms_quark(), errno, - "mmsx_read() failed: %s", - g_strerror(errno)); - } - - m->eof = true; - return false; - } - - is->offset += ret; - - return (size_t)ret; -} - -static void -input_mms_close(struct input_stream *is) -{ - struct input_mms *m = (struct input_mms *)is; - - mmsx_close(m->mms); - input_stream_deinit(&m->base); - g_free(m); -} - -static bool -input_mms_eof(struct input_stream *is) -{ - struct input_mms *m = (struct input_mms *)is; - - return m->eof; -} - -static bool -input_mms_seek(G_GNUC_UNUSED struct input_stream *is, - G_GNUC_UNUSED goffset offset, G_GNUC_UNUSED int whence, - G_GNUC_UNUSED GError **error_r) -{ - return false; -} - -const struct input_plugin input_plugin_mms = { - .name = "mms", - .open = input_mms_open, - .close = input_mms_close, - .read = input_mms_read, - .eof = input_mms_eof, - .seek = input_mms_seek, -}; diff --git a/src/input/mms_input_plugin.h b/src/input/mms_input_plugin.h deleted file mode 100644 index d6aa593f2..000000000 --- a/src/input/mms_input_plugin.h +++ /dev/null @@ -1,25 +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. - */ - -#ifndef INPUT_MMS_H -#define INPUT_MMS_H - -extern const struct input_plugin input_plugin_mms; - -#endif -- cgit v1.2.3