diff options
366 files changed, 8291 insertions, 5229 deletions
diff --git a/Makefile.am b/Makefile.am index 7240cb3f1..0ba0fc11b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -82,6 +82,7 @@ src_mpd_SOURCES = \ src/command/CommandError.cxx src/command/CommandError.hxx \ src/command/AllCommands.cxx src/command/AllCommands.hxx \ src/command/QueueCommands.cxx src/command/QueueCommands.hxx \ + src/command/TagCommands.cxx src/command/TagCommands.hxx \ src/command/PlayerCommands.cxx src/command/PlayerCommands.hxx \ src/command/PlaylistCommands.cxx src/command/PlaylistCommands.hxx \ src/command/DatabaseCommands.cxx src/command/DatabaseCommands.hxx \ @@ -146,7 +147,9 @@ src_mpd_SOURCES = \ src/ClientFile.cxx src/ClientFile.hxx \ src/Listen.cxx src/Listen.hxx \ src/LogInit.cxx src/LogInit.hxx \ + src/LogBackend.cxx src/LogBackend.hxx \ src/Log.cxx src/Log.hxx src/LogV.hxx \ + src/LogLevel.hxx \ src/ls.cxx src/ls.hxx \ src/IOThread.cxx src/IOThread.hxx \ src/Main.cxx src/Main.hxx \ @@ -170,6 +173,7 @@ src_mpd_SOURCES = \ src/PlaylistGlobal.cxx src/PlaylistGlobal.hxx \ src/PlaylistControl.cxx \ src/PlaylistEdit.cxx \ + src/PlaylistTag.cxx \ src/PlaylistPrint.cxx src/PlaylistPrint.hxx \ src/PlaylistSave.cxx src/PlaylistSave.hxx \ src/PlaylistMapper.cxx src/PlaylistMapper.hxx \ @@ -198,7 +202,6 @@ src_mpd_SOURCES = \ src/TagPrint.cxx src/TagPrint.hxx \ src/TagSave.cxx src/TagSave.hxx \ src/TagFile.cxx src/TagFile.hxx \ - src/TextFile.cxx src/TextFile.hxx \ src/TextInputStream.cxx \ src/Volume.cxx src/Volume.hxx \ src/SongFilter.cxx src/SongFilter.hxx \ @@ -245,6 +248,7 @@ endif libutil_a_SOURCES = \ src/util/Macros.hxx \ + src/util/Clamp.hxx \ src/util/Error.cxx src/util/Error.hxx \ src/util/Domain.hxx \ src/util/ReusableArray.hxx \ @@ -252,19 +256,22 @@ libutil_a_SOURCES = \ src/util/CharUtil.hxx \ src/util/NumberParser.hxx \ src/util/StringUtil.cxx src/util/StringUtil.hxx \ + src/util/SplitString.cxx src/util/SplitString.hxx \ src/util/FormatString.cxx src/util/FormatString.hxx \ src/util/Tokenizer.cxx src/util/Tokenizer.hxx \ src/util/UriUtil.cxx src/util/UriUtil.hxx \ src/util/Manual.hxx \ src/util/RefCount.hxx \ - src/util/fifo_buffer.c src/util/fifo_buffer.h \ src/util/FifoBuffer.hxx \ + src/util/DynamicFifoBuffer.hxx \ + src/util/ConstBuffer.hxx \ src/util/WritableBuffer.hxx \ - src/util/growing_fifo.c src/util/growing_fifo.h \ src/util/LazyRandomEngine.cxx src/util/LazyRandomEngine.hxx \ src/util/SliceBuffer.hxx \ src/util/HugeAllocator.cxx src/util/HugeAllocator.hxx \ src/util/PeakBuffer.cxx src/util/PeakBuffer.hxx \ + src/util/OptionParser.cxx OptionParser.hxx \ + src/util/OptionDef.hxx \ src/util/list.h \ src/util/list_sort.c src/util/list_sort.h \ src/util/ByteReverse.cxx src/util/ByteReverse.hxx \ @@ -297,12 +304,18 @@ libsystem_a_SOURCES = \ src/system/EventFD.cxx src/system/EventFD.hxx \ src/system/SignalFD.cxx src/system/SignalFD.hxx \ src/system/EPollFD.cxx src/system/EPollFD.hxx \ + src/system/PeriodClock.hxx \ src/system/Clock.cxx src/system/Clock.hxx # Event loop library libevent_a_SOURCES = \ src/event/WakeFD.hxx \ + src/event/PollGroup.hxx \ + src/event/PollGroupEPoll.hxx \ + src/event/PollGroupPoll.hxx src/event/PollGroupPoll.cxx \ + src/event/PollGroupWinSelect.hxx src/event/PollGroupWinSelect.cxx \ + src/event/PollResultGeneric.hxx \ src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx \ src/event/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx \ src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx \ @@ -318,32 +331,45 @@ libevent_a_SOURCES = \ # PCM library libpcm_a_SOURCES = \ + src/pcm/Domain.cxx src/pcm/Domain.hxx \ + src/pcm/Traits.hxx \ src/pcm/PcmBuffer.cxx src/pcm/PcmBuffer.hxx \ src/pcm/PcmExport.cxx src/pcm/PcmExport.hxx \ src/pcm/PcmConvert.cxx src/pcm/PcmConvert.hxx \ src/pcm/dsd2pcm/dsd2pcm.c src/pcm/dsd2pcm/dsd2pcm.h \ src/pcm/PcmDsd.cxx src/pcm/PcmDsd.hxx \ src/pcm/PcmDsdUsb.cxx src/pcm/PcmDsdUsb.hxx \ - src/pcm/PcmVolume.cxx src/pcm/PcmVolume.hxx \ + src/pcm/Volume.cxx src/pcm/Volume.hxx \ src/pcm/PcmMix.cxx src/pcm/PcmMix.hxx \ src/pcm/PcmChannels.cxx src/pcm/PcmChannels.hxx \ src/pcm/PcmPack.cxx src/pcm/PcmPack.hxx \ src/pcm/PcmFormat.cxx src/pcm/PcmFormat.hxx \ - src/pcm/PcmResample.cxx src/pcm/PcmResample.hxx \ - src/pcm/PcmResampleFallback.cxx \ - src/pcm/PcmResampleInternal.hxx \ + src/pcm/FormatConverter.cxx src/pcm/FormatConverter.hxx \ + src/pcm/ChannelsConverter.cxx src/pcm/ChannelsConverter.hxx \ + src/pcm/Resampler.hxx \ + src/pcm/GlueResampler.cxx src/pcm/GlueResampler.hxx \ + src/pcm/FallbackResampler.cxx src/pcm/FallbackResampler.hxx \ + src/pcm/ConfiguredResampler.cxx src/pcm/ConfiguredResampler.hxx \ src/pcm/PcmDither.cxx src/pcm/PcmDither.hxx \ src/pcm/PcmPrng.hxx \ src/pcm/PcmUtils.hxx libpcm_a_CPPFLAGS = $(AM_CPPFLAGS) \ + $(SOXR_CFLAGS) \ $(SAMPLERATE_CFLAGS) PCM_LIBS = \ libpcm.a \ + $(SOXR_LIBS) \ $(SAMPLERATE_LIBS) if HAVE_LIBSAMPLERATE -libpcm_a_SOURCES += src/pcm/PcmResampleLibsamplerate.cxx +libpcm_a_SOURCES += \ + src/pcm/LibsamplerateResampler.cxx src/pcm/LibsamplerateResampler.hxx +endif + +if HAVE_SOXR +libpcm_a_SOURCES += \ + src/pcm/SoxrResampler.cxx src/pcm/SoxrResampler.hxx endif # File system library @@ -356,7 +382,9 @@ libfs_a_SOURCES = \ src/fs/Charset.cxx src/fs/Charset.hxx \ src/fs/Path.cxx src/fs/Path.hxx \ src/fs/AllocatedPath.cxx src/fs/AllocatedPath.hxx \ + src/fs/TextFile.cxx src/fs/TextFile.hxx \ src/fs/FileSystem.cxx src/fs/FileSystem.hxx \ + src/fs/StandardDirectory.cxx src/fs/StandardDirectory.hxx \ src/fs/DirectoryReader.hxx # database plugins @@ -763,6 +791,7 @@ libinput_a_SOURCES = \ libinput_a_CPPFLAGS = $(AM_CPPFLAGS) \ $(CURL_CFLAGS) \ + $(SMBCLIENT_CFLAGS) \ $(CDIO_PARANOIA_CFLAGS) \ $(FFMPEG_CFLAGS) \ $(DESPOTIFY_CFLAGS) \ @@ -771,17 +800,31 @@ libinput_a_CPPFLAGS = $(AM_CPPFLAGS) \ INPUT_LIBS = \ libinput.a \ $(CURL_LIBS) \ + $(SMBCLIENT_LIBS) \ $(CDIO_PARANOIA_LIBS) \ $(FFMPEG_LIBS) \ $(DESPOTIFY_LIBS) \ $(MMS_LIBS) +if HAVE_ALSA +libinput_a_SOURCES += \ + src/input/AlsaInputPlugin.cxx \ + src/input/AlsaInputPlugin.hxx +INPUT_LIBS += $(ALSA_LIBS) +endif + + if ENABLE_CURL libinput_a_SOURCES += \ src/input/CurlInputPlugin.cxx src/input/CurlInputPlugin.hxx \ src/IcyMetaDataParser.cxx src/IcyMetaDataParser.hxx endif +if ENABLE_SMBCLIENT +libinput_a_SOURCES += \ + src/input/SmbclientInputPlugin.cxx src/input/SmbclientInputPlugin.hxx +endif + if ENABLE_CDIO_PARANOIA libinput_a_SOURCES += \ src/input/CdioParanoiaInputPlugin.cxx \ @@ -963,14 +1006,6 @@ libplaylist_plugins_a_SOURCES = \ src/playlist/ExtM3uPlaylistPlugin.hxx \ src/playlist/M3uPlaylistPlugin.cxx \ src/playlist/M3uPlaylistPlugin.hxx \ - src/playlist/PlsPlaylistPlugin.cxx \ - src/playlist/PlsPlaylistPlugin.hxx \ - src/playlist/XspfPlaylistPlugin.cxx \ - src/playlist/XspfPlaylistPlugin.hxx \ - src/playlist/AsxPlaylistPlugin.cxx \ - src/playlist/AsxPlaylistPlugin.hxx \ - src/playlist/RssPlaylistPlugin.cxx \ - src/playlist/RssPlaylistPlugin.hxx \ src/playlist/CuePlaylistPlugin.cxx \ src/playlist/CuePlaylistPlugin.hxx \ src/playlist/EmbeddedCuePlaylistPlugin.cxx \ @@ -997,6 +1032,18 @@ libplaylist_plugins_a_SOURCES += \ PLAYLIST_LIBS += $(YAJL_LIBS) endif +if HAVE_GLIB +libplaylist_plugins_a_SOURCES += \ + src/playlist/PlsPlaylistPlugin.cxx \ + src/playlist/PlsPlaylistPlugin.hxx \ + src/playlist/XspfPlaylistPlugin.cxx \ + src/playlist/XspfPlaylistPlugin.hxx \ + src/playlist/AsxPlaylistPlugin.cxx \ + src/playlist/AsxPlaylistPlugin.hxx \ + src/playlist/RssPlaylistPlugin.cxx \ + src/playlist/RssPlaylistPlugin.hxx +endif + # # Filter plugins # @@ -1110,7 +1157,7 @@ test_read_conf_LDADD = \ libfs.a \ $(GLIB_LIBS) test_read_conf_SOURCES = \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ test/read_conf.cxx test_run_resolver_LDADD = \ @@ -1118,7 +1165,7 @@ test_run_resolver_LDADD = \ libutil.a \ $(GLIB_LIBS) test_run_resolver_SOURCES = \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ test/run_resolver.cxx test_DumpDatabase_LDADD = \ @@ -1131,7 +1178,7 @@ test_DumpDatabase_LDADD = \ $(GLIB_LIBS) test_DumpDatabase_SOURCES = test/DumpDatabase.cxx \ src/protocol/Ack.cxx \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/DatabaseError.cxx \ src/DatabaseRegistry.cxx \ src/DatabaseSelection.cxx \ @@ -1140,8 +1187,7 @@ test_DumpDatabase_SOURCES = test/DumpDatabase.cxx \ src/DatabaseLock.cxx src/DatabaseSave.cxx \ src/Song.cxx src/SongSave.cxx src/SongSort.cxx \ src/TagSave.cxx \ - src/SongFilter.cxx \ - src/TextFile.cxx + src/SongFilter.cxx test_run_input_LDADD = \ $(INPUT_LIBS) \ @@ -1156,7 +1202,7 @@ test_run_input_LDADD = \ $(GLIB_LIBS) test_run_input_SOURCES = test/run_input.cxx \ test/stdbin.h \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/IOThread.cxx \ src/TagSave.cxx @@ -1174,7 +1220,7 @@ test_visit_archive_LDADD = \ libfs.a \ $(GLIB_LIBS) test_visit_archive_SOURCES = test/visit_archive.cxx \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/IOThread.cxx \ src/InputStream.cxx @@ -1197,7 +1243,7 @@ test_dump_text_file_LDADD = \ $(GLIB_LIBS) test_dump_text_file_SOURCES = test/dump_text_file.cxx \ test/stdbin.h \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/IOThread.cxx \ src/TextInputStream.cxx @@ -1217,8 +1263,9 @@ test_dump_playlist_LDADD = \ libpcm.a \ $(GLIB_LIBS) test_dump_playlist_SOURCES = test/dump_playlist.cxx \ + test/FakeDecoderAPI.cxx \ $(DECODER_SRC) \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/IOThread.cxx \ src/Song.cxx src/TagSave.cxx \ src/TagFile.cxx \ @@ -1247,7 +1294,7 @@ test_run_decoder_LDADD = \ $(GLIB_LIBS) test_run_decoder_SOURCES = test/run_decoder.cxx \ test/stdbin.h \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/IOThread.cxx \ src/ReplayGainInfo.cxx \ src/AudioFormat.cxx src/CheckAudioFormat.cxx \ @@ -1270,7 +1317,8 @@ test_read_tags_LDADD = \ libutil.a \ $(GLIB_LIBS) test_read_tags_SOURCES = test/read_tags.cxx \ - src/Log.cxx \ + test/FakeDecoderAPI.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/IOThread.cxx \ src/ReplayGainInfo.cxx \ src/CheckAudioFormat.cxx \ @@ -1282,7 +1330,7 @@ test_dump_rva2_LDADD = \ libutil.a \ $(GLIB_LIBS) test_dump_rva2_SOURCES = \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ test/dump_rva2.cxx endif @@ -1296,7 +1344,7 @@ test_run_filter_LDADD = \ test_run_filter_SOURCES = test/run_filter.cxx \ test/FakeReplayGainConfig.cxx \ test/stdbin.h \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/FilterPlugin.cxx src/FilterRegistry.cxx \ src/CheckAudioFormat.cxx \ src/AudioFormat.cxx \ @@ -1316,7 +1364,7 @@ if ENABLE_ENCODER noinst_PROGRAMS += test/run_encoder test_run_encoder_SOURCES = test/run_encoder.cxx \ test/stdbin.h \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/CheckAudioFormat.cxx \ src/AudioFormat.cxx \ src/AudioParser.cxx @@ -1336,7 +1384,7 @@ if ENABLE_VORBIS_ENCODER noinst_PROGRAMS += test/test_vorbis_encoder test_test_vorbis_encoder_SOURCES = test/test_vorbis_encoder.cxx \ test/stdbin.h \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/CheckAudioFormat.cxx \ src/AudioFormat.cxx \ src/AudioParser.cxx \ @@ -1356,7 +1404,8 @@ endif test_software_volume_SOURCES = test/software_volume.cxx \ test/stdbin.h \ - src/CheckAudioFormat.cxx \ + src/Log.cxx src/LogBackend.cxx \ + src/AudioFormat.cxx src/CheckAudioFormat.cxx \ src/AudioParser.cxx test_software_volume_LDADD = \ $(PCM_LIBS) \ @@ -1364,7 +1413,7 @@ test_software_volume_LDADD = \ $(GLIB_LIBS) test_run_avahi_SOURCES = \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/ZeroconfAvahi.cxx src/AvahiPoll.cxx \ test/ShutdownHandler.cxx test/ShutdownHandler.hxx \ test/run_avahi.cxx @@ -1386,7 +1435,7 @@ test_run_normalize_LDADD = \ $(GLIB_LIBS) test_run_convert_SOURCES = test/run_convert.cxx \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/AudioFormat.cxx \ src/CheckAudioFormat.cxx \ src/AudioParser.cxx @@ -1412,7 +1461,7 @@ test_run_output_LDADD = $(MPD_LIBS) \ test_run_output_SOURCES = test/run_output.cxx \ test/FakeReplayGainConfig.cxx \ test/stdbin.h \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/IOThread.cxx \ src/CheckAudioFormat.cxx \ src/AudioFormat.cxx \ @@ -1440,9 +1489,10 @@ test_read_mixer_LDADD = \ libfs.a \ $(GLIB_LIBS) test_read_mixer_SOURCES = test/read_mixer.cxx \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/MixerControl.cxx \ src/FilterPlugin.cxx \ + src/AudioFormat.cxx \ src/filter/VolumeFilterPlugin.cxx if ENABLE_BZIP2_TEST @@ -1461,7 +1511,7 @@ if ENABLE_INOTIFY noinst_PROGRAMS += test/run_inotify test_run_inotify_SOURCES = test/run_inotify.cxx \ test/ShutdownHandler.cxx test/ShutdownHandler.hxx \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ src/InotifyDomain.cxx \ src/InotifySource.cxx test_run_inotify_LDADD = \ @@ -1488,7 +1538,7 @@ test_test_byte_reverse_LDADD = \ $(CPPUNIT_LIBS) test_test_mixramp_SOURCES = \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ test/test_mixramp.cxx test_test_mixramp_CPPFLAGS = $(AM_CPPFLAGS) $(CPPUNIT_CFLAGS) -DCPPUNIT_HAVE_RTTI=0 test_test_mixramp_CXXFLAGS = $(AM_CXXFLAGS) -Wno-error=deprecated-declarations @@ -1497,6 +1547,7 @@ test_test_mixramp_LDADD = \ $(CPPUNIT_LIBS) test_test_pcm_SOURCES = \ + src/AudioFormat.cxx \ test/test_pcm_util.hxx \ test/test_pcm_dither.cxx \ test/test_pcm_pack.cxx \ @@ -1515,7 +1566,7 @@ test_test_pcm_LDADD = \ $(GLIB_LIBS) test_test_archive_SOURCES = \ - src/Log.cxx \ + src/Log.cxx src/LogBackend.cxx \ test/test_archive.cxx test_test_archive_CPPFLAGS = $(AM_CPPFLAGS) $(CPPUNIT_CFLAGS) -DCPPUNIT_HAVE_RTTI=0 test_test_archive_CXXFLAGS = $(AM_CXXFLAGS) -Wno-error=deprecated-declarations @@ -1,3 +1,13 @@ +ver 0.19 (not yet released) +* protocol + - new commands "addtagid", "cleartagid" +* input + - alsa: new input plugin + - smbclient: new input plugin +* filter + - volume: improved software volume dithering +* new resampler option using libsoxr + ver 0.18.7 (not yet released) * playlist - soundcloud: fix build failure with libyajl 2.0.1 diff --git a/configure.ac b/configure.ac index b2e952808..eb63735ce 100644 --- a/configure.ac +++ b/configure.ac @@ -1,9 +1,9 @@ AC_PREREQ(2.60) -AC_INIT(mpd, 0.18.7, musicpd-dev-team@lists.sourceforge.net) +AC_INIT(mpd, 0.19~git, musicpd-dev-team@lists.sourceforge.net) VERSION_MAJOR=0 -VERSION_MINOR=18 +VERSION_MINOR=19 VERSION_REVISION=0 VERSION_EXTRA=0 @@ -13,7 +13,7 @@ AM_SILENT_RULES AC_CONFIG_HEADERS(config.h) AC_CONFIG_MACRO_DIR([m4]) -AC_DEFINE(PROTOCOL_VERSION, "0.18.0", [The MPD protocol version]) +AC_DEFINE(PROTOCOL_VERSION, "0.19.0", [The MPD protocol version]) dnl --------------------------------------------------------------------------- @@ -65,9 +65,20 @@ dnl OS Specific Defaults dnl --------------------------------------------------------------------------- AC_CANONICAL_HOST +host_is_unix=yes +host_is_linux=no host_is_darwin=no +host_is_solaris=no +host_is_windows=no + +linux_auto=no case "$host_os" in +linux*) + host_is_linux=yes + linux_auto=auto + ;; + mingw32* | windows*) AC_CONFIG_FILES([ src/win/mpd_win32_rc.rc @@ -76,14 +87,19 @@ mingw32* | windows*) AM_CPPFLAGS="$AM_CPPFLAGS -DWIN32_LEAN_AND_MEAN" AM_CPPFLAGS="$AM_CPPFLAGS -DWINVER=0x0600 -D_WIN32_WINNT=0x0600" LIBS="$LIBS -lws2_32" - HAVE_WINDOWS=1 + host_is_windows=yes + host_is_unix=no ;; darwin*) host_is_darwin=yes ;; + +solaris*) + host_is_solaris=yes + ;; esac -AM_CONDITIONAL([HAVE_WINDOWS], [test x$HAVE_WINDOWS = x1]) +AM_CONDITIONAL([HAVE_WINDOWS], [test x$host_is_windows = xyes]) if test -z "$prefix" || test "x$prefix" = xNONE; then local_lib= @@ -145,10 +161,16 @@ AC_SEARCH_LIBS([syslog], [bsd socket inet], AC_SEARCH_LIBS([socket], [socket]) AC_SEARCH_LIBS([gethostbyname], [nsl]) -AC_CHECK_FUNCS(pipe2 accept4) -MPD_OPTIONAL_FUNC(eventfd, eventfd, USE_EVENTFD) -MPD_OPTIONAL_FUNC(signalfd, signalfd, USE_SIGNALFD) -MPD_OPTIONAL_FUNC(epoll, epoll_create1, USE_EPOLL) +if test x$host_is_linux = xyes; then + AC_CHECK_FUNCS(pipe2 accept4) +fi + +AC_CHECK_FUNCS(getpwnam_r getpwuid_r) + +if test x$host_is_linux = xyes; then + MPD_OPTIONAL_FUNC(eventfd, eventfd, USE_EVENTFD) + MPD_OPTIONAL_FUNC(signalfd, signalfd, USE_SIGNALFD) +fi AC_SEARCH_LIBS([exp], [m],, [AC_MSG_ERROR([exp() not found])]) @@ -157,6 +179,80 @@ AC_CHECK_HEADERS(locale.h) AC_CHECK_HEADERS(valgrind/memcheck.h) dnl --------------------------------------------------------------------------- +dnl Event loop selection +dnl --------------------------------------------------------------------------- + +MPD_OPTIONAL_FUNC_NODEF(poll, poll) + +if test x$host_is_linux = xyes; then + MPD_OPTIONAL_FUNC_NODEF(epoll, epoll_create1) +fi + +AC_ARG_WITH(eventloop, + AS_HELP_STRING( + [--with-eventloop=@<:@glib|internal|auto@:>@], + [specify event loop implementation (default=auto)]),, + [with_eventloop=auto]) + +AC_ARG_WITH(pollmethod, + AS_HELP_STRING( + [--with-pollmethod=@<:@epoll|poll|winselect|auto@:>@], + [specify poll method for internal event loop (default=auto)]),, + [with_pollmethod=auto]) + +if test "x$with_eventloop" = xauto; then + if + test "x$enable_epoll" = xyes || + test "x$host_is_windows" = xyes; then + with_eventloop=internal + else + with_eventloop=glib + fi +fi + +case "$with_eventloop" in +glib) + AC_DEFINE(USE_GLIB_EVENTLOOP, 1, + [Define to use GLib event loop]) + ;; +internal) + AC_DEFINE(USE_INTERNAL_EVENTLOOP, 1, + [Define to use internal event loop]) + ;; +*) + AC_MSG_ERROR([unknown eventloop option: $with_eventloop]) + ;; +esac + +if test "x$with_eventloop" = xinternal; then + if test "x$with_pollmethod" = xauto; then + if test "x$enable_epoll" = xyes; then + with_pollmethod=epoll + elif test "x$enable_poll" = xyes; then + with_pollmethod=poll + elif test "x$host_is_windows" = xyes; then + with_pollmethod=winselect + else + AC_MSG_ERROR([no poll method is available for your platform]) + fi + fi + case "$with_pollmethod" in + epoll) + AC_DEFINE(USE_EPOLL, 1, [Define to poll sockets with epoll]) + ;; + poll) + AC_DEFINE(USE_POLL, 1, [Define to poll sockets with poll]) + ;; + winselect) + AC_DEFINE(USE_WINSELECT, 1, + [Define to poll sockets with Windows select]) + ;; + *) + AC_MSG_ERROR([unknown pollmethod option: $with_pollmethod]) + esac +fi + +dnl --------------------------------------------------------------------------- dnl Allow tools to be specifically built dnl --------------------------------------------------------------------------- @@ -172,7 +268,7 @@ AC_ARG_ENABLE(adplug, AC_ARG_ENABLE(alsa, AS_HELP_STRING([--enable-alsa], [enable ALSA support]),, - [enable_alsa=auto]) + [enable_alsa=$linux_auto]) AC_ARG_ENABLE(roar, AS_HELP_STRING([--enable-roar], @@ -204,6 +300,11 @@ AC_ARG_ENABLE(curl, [enable support for libcurl HTTP streaming (default: auto)]),, [enable_curl=auto]) +AC_ARG_ENABLE(smbclient, + AS_HELP_STRING([--enable-smbclient], + [enable support for libsmbclient (default: auto)]),, + [enable_smbclient=auto]) + AC_ARG_ENABLE(debug, AS_HELP_STRING([--enable-debug], [enable debugging (default: disabled)]),, @@ -295,6 +396,11 @@ AC_ARG_ENABLE(lsr, [enable libsamplerate support]),, enable_lsr=auto) +AC_ARG_ENABLE(soxr, + AS_HELP_STRING([--enable-soxr], + [enable the libsoxr resampler]),, + enable_soxr=auto) + AC_ARG_ENABLE(mad, AS_HELP_STRING([--enable-mad], [enable libmad mp3 decoder plugin]),, @@ -379,7 +485,7 @@ AC_ARG_ENABLE(sndfile, AC_ARG_ENABLE(solaris_output, AS_HELP_STRING([--enable-solaris-output], [enables the Solaris /dev/audio output]),, - [enable_solaris_output=auto]) + [enable_solaris_output=$host_is_solaris]) AC_ARG_ENABLE(sqlite, AS_HELP_STRING([--enable-sqlite], @@ -389,7 +495,7 @@ AC_ARG_ENABLE(sqlite, AC_ARG_ENABLE(systemd-daemon, AS_HELP_STRING([--enable-systemd-daemon], [use the systemd daemon library (default=auto)]),, - [enable_systemd_daemon=auto]) + [enable_systemd_daemon=$linux_auto]) AC_ARG_ENABLE(tcp, AS_HELP_STRING([--disable-tcp], @@ -414,7 +520,7 @@ AC_ARG_ENABLE(twolame-encoder, AC_ARG_ENABLE(un, AS_HELP_STRING([--disable-un], [disable support for clients connecting via unix domain sockets (default: enable)]),, - [enable_un=yes]) + [enable_un=$host_is_unix]) AC_ARG_ENABLE(vorbis, AS_HELP_STRING([--enable-vorbis], @@ -470,13 +576,24 @@ AC_ARG_WITH(tremor-includes, dnl --------------------------------------------------------------------------- dnl Mandatory Libraries dnl --------------------------------------------------------------------------- -PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.28 gthread-2.0],, + +AC_ARG_ENABLE(glib, + AS_HELP_STRING([--enable-glib], + [enable GLib usage (default: enabled)]),, + enable_glib=yes) + +if test x$enable_glib = xyes; then + PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.28 gthread-2.0],, [AC_MSG_ERROR([GLib 2.28 is required])]) -if test x$GCC = xyes; then - # suppress warnings in the GLib headers - GLIB_CFLAGS=`echo $GLIB_CFLAGS |sed -e 's,-I/,-isystem /,g'` + if test x$GCC = xyes; then + # suppress warnings in the GLib headers + GLIB_CFLAGS=`echo $GLIB_CFLAGS |sed -e 's,-I/,-isystem /,g'` + fi + + AC_DEFINE(HAVE_GLIB, 1, [Define if GLib is used]) fi +AM_CONDITIONAL(HAVE_GLIB, test x$enable_glib = xyes) dnl --------------------------------------------------------------------------- dnl Protocol Options @@ -514,12 +631,6 @@ if test x$enable_tcp = xyes; then AC_DEFINE(HAVE_TCP, 1, [Define if TCP socket support is enabled]) fi -case "$host_os" in -mingw* | windows* | cygwin*) - enable_un=no - ;; -esac - if test x$enable_un = xyes; then AC_DEFINE(HAVE_UN, 1, [Define if unix domain socket support is enabled]) STRUCT_UCRED @@ -692,21 +803,22 @@ dnl Converter Plugins dnl --------------------------------------------------------------------------- dnl ------------------------------ libsamplerate ------------------------------ -MPD_AUTO_PKG(lsr, SAMPLERATE, [samplerate >= 0.0.15], +MPD_AUTO_PKG(lsr, SAMPLERATE, [samplerate >= 0.1.3], [libsamplerate resampling], [libsamplerate not found]) if test x$enable_lsr = xyes; then AC_DEFINE([HAVE_LIBSAMPLERATE], 1, [Define to enable libsamplerate]) fi +AM_CONDITIONAL(HAVE_LIBSAMPLERATE, test x$enable_lsr = xyes) -if test x$enable_lsr = xyes; then - PKG_CHECK_MODULES([SAMPLERATE_013], - [samplerate >= 0.1.3],, - [AC_DEFINE([HAVE_LIBSAMPLERATE_NOINT], 1, - [libsamplerate doesn't provide src_int_to_float_array() (<0.1.3)])]) +dnl ------------------------------ libsoxr ------------------------------------ +MPD_AUTO_PKG(soxr, SOXR, [soxr], + [libsoxr resampler], [libsoxr not found]) +if test x$enable_soxr = xyes; then + AC_DEFINE([HAVE_SOXR], 1, [Define to enable libsoxr]) fi -AM_CONDITIONAL(HAVE_LIBSAMPLERATE, test x$enable_lsr = xyes) +AM_CONDITIONAL(HAVE_SOXR, test x$enable_soxr = xyes) dnl --------------------------------------------------------------------------- dnl Input Plugins @@ -720,6 +832,14 @@ if test x$enable_curl = xyes; then fi AM_CONDITIONAL(ENABLE_CURL, test x$enable_curl = xyes) +dnl ----------------------------------- smbclient ----------------------------- +MPD_AUTO_PKG(smbclient, SMBCLIENT, [smbclient >= 0.2], + [smbclient input plugin], [libsmbclient not found]) +if test x$enable_smbclient = xyes; then + AC_DEFINE(ENABLE_SMBCLIENT, 1, [Define when libsmbclient is used]) +fi +AM_CONDITIONAL(ENABLE_SMBCLIENT, test x$enable_smbclient = xyes) + dnl --------------------------------- Despotify --------------------------------- MPD_AUTO_PKG(despotify, DESPOTIFY, [despotify], [Despotify support], [despotify not found]) @@ -1376,18 +1496,6 @@ AM_CONDITIONAL(HAVE_SHOUT, test x$enable_shout = xyes) dnl --------------------------------- Solaris --------------------------------- -if test x$enable_solaris_output = xauto; then - case "$host_os" in - solaris*) - enable_solaris_output=yes - ;; - - *) - enable_solaris_output=no - ;; - esac -fi - if test x$enable_solaris_output = xyes; then AC_DEFINE(ENABLE_SOLARIS_OUTPUT, 1, [Define to enable Solaris /dev/audio support]) fi @@ -1396,17 +1504,13 @@ AM_CONDITIONAL(ENABLE_SOLARIS_OUTPUT, test x$enable_solaris_output = xyes) dnl --------------------------------- WinMM --------------------------------- -case "$host_os" in - mingw32* | windows*) - AC_DEFINE(ENABLE_WINMM_OUTPUT, 1, [Define to enable WinMM support]) - enable_winmm_output=yes - LIBS="$LIBS -lwinmm" - ;; - - *) - enable_winmm_output=no - ;; -esac +if test "x$host_is_windows" = xyes; then + AC_DEFINE(ENABLE_WINMM_OUTPUT, 1, [Define to enable WinMM support]) + enable_winmm_output=yes + LIBS="$LIBS -lwinmm" +else + enable_winmm_output=no +fi AM_CONDITIONAL(ENABLE_WINMM_OUTPUT, test x$enable_winmm_output = xyes) @@ -1574,6 +1678,7 @@ results(wildmidi, [WildMidi]) printf '\nOther features:\n\t' results(lsr, [libsamplerate]) +results(soxr, [libsoxr]) results(libmpdclient, [libmpdclient]) results(inotify, [inotify]) results(sqlite, [SQLite]) @@ -1616,11 +1721,22 @@ fi printf '\nStreaming support:\n\t' results(cdio_paranoia, [CDIO_PARANOIA]) results(curl,[CURL]) +results(smbclient,[SMBCLIENT]) results(despotify,[Despotify]) results(soundcloud,[Soundcloud]) printf '\n\t' results(mms,[MMS]) +printf '\nEvent loop:\n\t' +case $with_eventloop in +glib) + printf 'GLib' + ;; +internal) + printf 'Internal (%s)' $with_pollmethod + ;; +esac + printf '\n\n##########################################\n\n' echo 'Generating files needed for compilation' diff --git a/doc/mpd.conf.5 b/doc/mpd.conf.5 index 6431613d1..6e3bae7b2 100644 --- a/doc/mpd.conf.5 +++ b/doc/mpd.conf.5 @@ -136,53 +136,6 @@ for the format of this parameter. Multiple audio_output sections may be specified. If no audio_output section is specified, then MPD will scan for a usable audio output. .TP -.B audio_output_format <sample_rate:bits:channels> -This specifies the sample rate, bits per sample, and number of channels of -audio that is sent to each audio output. Note that audio outputs may specify -their own audio format which will be used for actual output to the audio -device. An example is "44100:16:2" for 44100Hz, 16 bits, and 2 channels. The -default is to use the audio format of the input file. -Any of the three attributes may be an asterisk to specify that this -attribute should not be enforced -.TP -.B samplerate_converter <integer or prefix> -This specifies the libsamplerate converter to use. The supplied value should -either be an integer or a prefix of the name of a converter. The default is -"Fastest Sinc Interpolator". - -At the time of this writing, the following converters are available: -.RS -.TP -Best Sinc Interpolator (0) - -Band limited sinc interpolation, best quality, 97dB SNR, 96% BW. -.TP -Medium Sinc Interpolator (1) - -Band limited sinc interpolation, medium quality, 97dB SNR, 90% BW. -.TP -Fastest Sinc Interpolator (2) - -Band limited sinc interpolation, fastest, 97dB SNR, 80% BW. -.TP -ZOH Interpolator (3) - -Zero order hold interpolator, very fast, very poor quality with audible -distortions. -.TP -Linear Interpolator (4) - -Linear interpolator, very fast, poor quality. -.TP -internal - -Poor quality, no floating point operations. This is the default (and -only choice) if MPD was compiled without libsamplerate. -.RE -.IP -For an up-to-date list of available converters, please see the libsamplerate -documentation (available online at <\fBhttp://www.mega\-nerd.com/SRC/\fP>). -.TP .B replaygain <off or album or track or auto> If specified, mpd will adjust the volume of songs played using ReplayGain tags (see <\fBhttp://www.replaygain.org/\fP>). Setting this to "album" will adjust diff --git a/doc/protocol.xml b/doc/protocol.xml index abc74e4e6..a0211bffd 100644 --- a/doc/protocol.xml +++ b/doc/protocol.xml @@ -1258,6 +1258,44 @@ OK </para> </listitem> </varlistentry> + + <varlistentry id="command_addtagid"> + <term> + <cmdsynopsis> + <command>addtagid</command> + <arg choice="req"><replaceable>SONGID</replaceable></arg> + <arg choice="req"><replaceable>TAG</replaceable></arg> + <arg choice="req"><replaceable>VALUE</replaceable></arg> + </cmdsynopsis> + </term> + <listitem> + <para> + Adds a tag to the specified song. Editing song tags is + only possible for remote songs. This change is + volatile: it may be overwritten by tags received from + the server, and the data is gone when the song gets + removed from the queue. + </para> + </listitem> + </varlistentry> + + <varlistentry id="command_cleartagid"> + <term> + <cmdsynopsis> + <command>cleartagid</command> + <arg choice="req"><replaceable>SONGID</replaceable></arg> + <arg choice="opt"><replaceable>TAG</replaceable></arg> + </cmdsynopsis> + </term> + <listitem> + <para> + Removes tags from the specified song. If + <varname>TAG</varname> is not specified, then all tag + values will be removed. Editing song tags is only + possible for remote songs. + </para> + </listitem> + </varlistentry> </variablelist> </section> @@ -1952,6 +1990,32 @@ OK <para> Shows information about all outputs. </para> + <screen> +outputid: 0 +outputname: My ALSA Device +outputenabled: 0 +OK + </screen> + <para> + Return information: + </para> + <itemizedlist> + <listitem> + <para> + <varname>outputid</varname>: ID of the output. May change between executions + </para> + </listitem> + <listitem> + <para> + <varname>outputname</varname>: Name of the output. It can be any. + </para> + </listitem> + <listitem> + <para> + <varname>outputenabled</varname>: Status of the output. 0 if disabled, 1 if enabled. + </para> + </listitem> + </itemizedlist> </listitem> </varlistentry> </variablelist> diff --git a/doc/user.xml b/doc/user.xml index 99278a69b..733b1bd46 100644 --- a/doc/user.xml +++ b/doc/user.xml @@ -393,7 +393,7 @@ systemctl start mpd.socket</programlisting> a name registered in the PULSE server. </entry> </row> - <row> + <row id="ao_format"> <entry> <varname>format</varname> </entry> @@ -417,8 +417,6 @@ systemctl start mpd.socket</programlisting> (signed 8 bit integer samples), <varname>16</varname>, <varname>24</varname> (signed 24 bit integer samples padded to 32 bit), - <varname>24_3</varname> (signed 24 bit integer - samples, no padding, 3 bytes per sample), <varname>32</varname> (signed 32 bit integer samples), <varname>f</varname> (32 bit floating point, -1.0 to 1.0). @@ -609,6 +607,165 @@ systemctl start mpd.socket</programlisting> </tgroup> </informaltable> </section> + + <section> + <title>Audio Format Settings</title> + + <section> + <title>Global Audio Format</title> + + <para> + The setting <varname>audio_output_format</varname> forces + MPD to use one audio format for all outputs. Doing that is + usually not a good idea. The values are the same as in + <link linkend="ao_format"><varname>format</varname> in the + <varname>audio_output</varname> section</link>. + </para> + </section> + + <section> + <title>Resampler</title> + + <para> + Sometimes, music needs to be resampled before it can be + played; for example, CDs use a sample rate of 44,100 Hz + while many cheap audio chips can only handle 48,000 Hz. + Resampling reduces the quality and consumes a lot of CPU. + There are different options, some of them optimized for high + quality and others for low CPU usage, but you can't have + both at the same time. Often, the resampler is the + component that is responsible for most of MPD's CPU usage. + Since MPD comes with high quality defaults, it may appear + that MPD consumes more CPU than other software. + </para> + + <para> + The following resamplers are available (if enabled at + compile time): + </para> + + <itemizedlist> + <listitem> + <para> + <ulink + url="http://www.mega-nerd.com/SRC/">libsamplerate</ulink> + a.k.a. Secret Rabbit Code (SRC). + </para> + </listitem> + + <listitem> + <para> + <ulink + url="http://sourceforge.net/projects/soxr/">libsoxr</ulink>, + the SoX Resampler library + </para> + </listitem> + + <listitem> + <para> + internal: low CPU usage, but very poor quality. This is + the fallback if MPD was compiled without an external + resampler. + </para> + </listitem> + </itemizedlist> + + <para> + The setting <varname>samplerate_converter</varname> controls + how MPD shall resample music. Possible values: + </para> + + <informaltable> + <tgroup cols="2"> + <thead> + <row> + <entry> + Value + </entry> + <entry> + Description + </entry> + </row> + </thead> + <tbody> + <row> + <entry> + "<parameter>internal</parameter>" + </entry> + <entry> + The internal resampler. Low CPU usage, but very + poor quality. + </entry> + </row> + + <row> + <entry> + "<parameter>soxr</parameter>" + </entry> + <entry> + Use libsoxr. + </entry> + </row> + + <row> + <entry> + "<parameter>Best Sinc Interpolator</parameter>" or + "<parameter>0</parameter>" + </entry> + <entry> + libsamplerate: Band limited sinc interpolation, best + quality, 97dB SNR, 96% BW. + </entry> + </row> + + <row> + <entry> + "<parameter>Medium Sinc Interpolator</parameter>" or + "<parameter>1</parameter>" + </entry> + <entry> + libsamplerate: Band limited sinc interpolation, + medium quality, 97dB SNR, 90% BW. + </entry> + </row> + + <row> + <entry> + "<parameter>Fastest Sinc Interpolator</parameter>" or + "<parameter>2</parameter>" + </entry> + <entry> + libsamplerate: Band limited sinc interpolation, + fastest, 97dB SNR, 80% BW. + </entry> + </row> + + <row> + <entry> + "<parameter>ZOH Sinc Interpolator</parameter>" or + "<parameter>3</parameter>" + </entry> + <entry> + libsamplerate: Zero order hold interpolator, very + fast, very poor quality with audible distortions. + </entry> + </row> + + <row> + <entry> + "<parameter>Linear Interpolator</parameter>" or + "<parameter>4</parameter>" + </entry> + <entry> + libsamplerate: Linear interpolator, very fast, poor + quality. + </entry> + </row> + </tbody> + </tgroup> + </informaltable> + </section> + </section> </chapter> <chapter> @@ -902,6 +1059,20 @@ systemctl start mpd.socket</programlisting> </tgroup> </informaltable> </section> + + <section> + <title><varname>smbclient</varname></title> + + <para> + Allows MPD to access files on SMB/CIFS servers (e.g. Samba + or Microsoft Windows). All URIs with the + <filename>smb://</filename> scheme are used. Example: + </para> + + <para> + <filename>mpc add smb://servername/sharename/filename.ogg</filename> + </para> + </section> </section> <section> diff --git a/m4/mpd_func.m4 b/m4/mpd_func.m4 index d12d27062..5f2bf8f3d 100644 --- a/m4/mpd_func.m4 +++ b/m4/mpd_func.m4 @@ -10,3 +10,16 @@ AC_DEFUN([MPD_OPTIONAL_FUNC], [ [AC_CHECK_FUNC([$2], [AC_DEFINE([$3], 1, [Define to use $1])],)]) ]) + +dnl MPD_OPTIONAL_FUNC_NODEF(name, func) +dnl +dnl Allow the user to enable or disable the use of a function. +dnl Works similar to MPD_OPTIONAL_FUNC, however MPD_OPTIONAL_FUNC_NODEF +dnl does not invoke AC_DEFINE when function is enabled. Shell variable +dnl enable_$name is set to "yes" instead. +AC_DEFUN([MPD_OPTIONAL_FUNC_NODEF], [ + AC_ARG_ENABLE([$1], + AS_HELP_STRING([--enable-$1], + [use the function "$1" (default: auto)]),, + [AC_CHECK_FUNC([$2], [enable_$1=yes],)]) +]) diff --git a/src/ArchiveFile.hxx b/src/ArchiveFile.hxx index 4bdba62ab..154b4b297 100644 --- a/src/ArchiveFile.hxx +++ b/src/ArchiveFile.hxx @@ -23,6 +23,8 @@ class Mutex; class Cond; class Error; +class ArchiveVisitor; +struct InputStream; class ArchiveFile { public: diff --git a/src/ArchiveLookup.cxx b/src/ArchiveLookup.cxx index 7a93c136a..65925a6e3 100644 --- a/src/ArchiveLookup.cxx +++ b/src/ArchiveLookup.cxx @@ -24,7 +24,6 @@ #include <string.h> #include <sys/stat.h> -#include <unistd.h> #include <errno.h> gcc_pure diff --git a/src/ArchivePlugin.hxx b/src/ArchivePlugin.hxx index 6439c7242..c23826540 100644 --- a/src/ArchivePlugin.hxx +++ b/src/ArchivePlugin.hxx @@ -20,9 +20,7 @@ #ifndef MPD_ARCHIVE_PLUGIN_HXX #define MPD_ARCHIVE_PLUGIN_HXX -struct InputStream; class ArchiveFile; -class ArchiveVisitor; class Error; struct archive_plugin { diff --git a/src/AvahiPoll.cxx b/src/AvahiPoll.cxx index 0d5a43dad..d6f8b9f79 100644 --- a/src/AvahiPoll.cxx +++ b/src/AvahiPoll.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "AvahiPoll.hxx" -#include "event/Loop.hxx" #include "event/SocketMonitor.hxx" #include "event/TimeoutMonitor.hxx" diff --git a/src/Client.hxx b/src/Client.hxx index f0bc6b0f7..c794bf0ee 100644 --- a/src/Client.hxx +++ b/src/Client.hxx @@ -165,7 +165,7 @@ void client_manager_init(void); void client_new(EventLoop &loop, Partition &partition, - int fd, const struct sockaddr *sa, size_t sa_length, int uid); + int fd, const sockaddr *sa, size_t sa_length, int uid); /** * Write a C string to the client. diff --git a/src/ClientFile.cxx b/src/ClientFile.cxx index 382b76083..d1b00ebbc 100644 --- a/src/ClientFile.cxx +++ b/src/ClientFile.cxx @@ -24,11 +24,8 @@ #include "fs/Path.hxx" #include "fs/FileSystem.hxx" #include "util/Error.hxx" -#include "util/Domain.hxx" #include <sys/stat.h> -#include <sys/types.h> -#include <errno.h> #include <unistd.h> bool diff --git a/src/ClientGlobal.cxx b/src/ClientGlobal.cxx index e79f3430b..8fb1f8e49 100644 --- a/src/ClientGlobal.cxx +++ b/src/ClientGlobal.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "ClientInternal.hxx" -#include "ClientList.hxx" #include "ConfigGlobal.hxx" #define CLIENT_TIMEOUT_DEFAULT (60) diff --git a/src/ClientNew.cxx b/src/ClientNew.cxx index e84887072..5618e9ece 100644 --- a/src/ClientNew.cxx +++ b/src/ClientNew.cxx @@ -28,16 +28,12 @@ #include "util/Error.hxx" #include "Log.hxx" -#include <glib.h> - #include <assert.h> -#include <sys/types.h> #ifdef WIN32 #include <winsock2.h> #else #include <sys/socket.h> #endif -#include <unistd.h> #ifdef HAVE_LIBWRAP #include <tcpd.h> @@ -65,15 +61,14 @@ client_new(EventLoop &loop, Partition &partition, int fd, const struct sockaddr *sa, size_t sa_length, int uid) { static unsigned int next_client_num; - char *remote; + const auto remote = sockaddr_to_string(sa, sa_length); assert(fd >= 0); #ifdef HAVE_LIBWRAP if (sa->sa_family != AF_UNIX) { - char *hostaddr = sockaddr_to_string(sa, sa_length, - IgnoreError()); - const char *progname = g_get_prgname(); + // TODO: shall we obtain the program name from argv[0]? + const char *progname = "mpd"; struct request_info req; request_init(&req, RQ_FILE, fd, RQ_DAEMON, progname, 0); @@ -84,14 +79,11 @@ client_new(EventLoop &loop, Partition &partition, /* tcp wrappers says no */ FormatWarning(client_domain, "libwrap refused connection (libwrap=%s) from %s", - progname, hostaddr); + progname, remote.c_str()); - g_free(hostaddr); close_socket(fd); return; } - - g_free(hostaddr); } #endif /* HAVE_WRAP */ @@ -109,9 +101,8 @@ client_new(EventLoop &loop, Partition &partition, client_list.Add(*client); - remote = sockaddr_to_string(sa, sa_length, IgnoreError()); - FormatInfo(client_domain, "[%u] opened from %s", client->num, remote); - g_free(remote); + FormatInfo(client_domain, "[%u] opened from %s", + client->num, remote.c_str()); } void diff --git a/src/ClientRead.cxx b/src/ClientRead.cxx index 22edefe60..205884815 100644 --- a/src/ClientRead.cxx +++ b/src/ClientRead.cxx @@ -23,7 +23,6 @@ #include "event/Loop.hxx" #include "util/CharUtil.hxx" -#include <assert.h> #include <string.h> BufferedSocket::InputResult diff --git a/src/ClientSubscribe.cxx b/src/ClientSubscribe.cxx index 3a9f1b19c..f4d9ad316 100644 --- a/src/ClientSubscribe.cxx +++ b/src/ClientSubscribe.cxx @@ -22,11 +22,6 @@ #include "Idle.hxx" #include <assert.h> -#include <string.h> - - bool Unsubscribe(const char *channel); - void UnsubscribeAll(); - bool PushMessage(const ClientMessage &msg); Client::SubscribeResult Client::Subscribe(const char *channel) diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index 05f0a358c..58d07ddb7 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -34,9 +34,11 @@ #include "fs/AllocatedPath.hxx" #include "fs/Traits.hxx" #include "fs/FileSystem.hxx" +#include "fs/StandardDirectory.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" -#include "system/FatalError.hxx" +#include "util/OptionDef.hxx" +#include "util/OptionParser.hxx" #ifdef ENABLE_ENCODER #include "EncoderList.hxx" @@ -48,19 +50,37 @@ #include "ArchivePlugin.hxx" #endif -#include <glib.h> - #include <stdio.h> #include <stdlib.h> #ifdef WIN32 -#define CONFIG_FILE_LOCATION "\\mpd\\mpd.conf" +#define CONFIG_FILE_LOCATION "mpd\\mpd.conf" +#define APP_CONFIG_FILE_LOCATION "conf\\mpd.conf" #else #define USER_CONFIG_FILE_LOCATION1 ".mpdconf" #define USER_CONFIG_FILE_LOCATION2 ".mpd/mpd.conf" #define USER_CONFIG_FILE_LOCATION_XDG "mpd/mpd.conf" #endif +static const OptionDef opt_kill( + "kill", "kill the currently running mpd session"); +static const OptionDef opt_no_config( + "no-config", "don't read from config"); +static const OptionDef opt_no_daemon( + "no-daemon", "don't detach from console"); +static const OptionDef opt_stdout( + "stdout", nullptr); // hidden, compatibility with old versions +static const OptionDef opt_stderr( + "stderr", "print messages to stderr"); +static const OptionDef opt_verbose( + "verbose", 'v', "verbose logging"); +static const OptionDef opt_version( + "version", 'V', "print version number"); +static const OptionDef opt_help( + "help", 'h', "show help options"); +static const OptionDef opt_help_alt( + nullptr, '?', nullptr); // hidden, standard alias for --help + static constexpr Domain cmdline_domain("cmdline"); gcc_noreturn @@ -132,122 +152,165 @@ static void version(void) exit(EXIT_SUCCESS); } -static const char *summary = - "Music Player Daemon - a daemon for playing music."; +static void PrintOption(const OptionDef &opt) +{ + if (opt.HasShortOption()) + printf(" -%c, --%-12s%s\n", + opt.GetShortOption(), + opt.GetLongOption(), + opt.GetDescription()); + else + printf(" --%-16s%s\n", + opt.GetLongOption(), + opt.GetDescription()); +} -gcc_pure -static AllocatedPath -PathBuildChecked(const AllocatedPath &a, PathTraits::const_pointer b) +gcc_noreturn +static void help(void) { - if (a.IsNull()) - return AllocatedPath::Null(); + puts("Usage:\n" + " mpd [OPTION...] [path/to/mpd.conf]\n" + "\n" + "Music Player Daemon - a daemon for playing music.\n" + "\n" + "Options:"); - return AllocatedPath::Build(a, b); + PrintOption(opt_help); + PrintOption(opt_kill); + PrintOption(opt_no_config); + PrintOption(opt_no_daemon); + PrintOption(opt_stderr); + PrintOption(opt_verbose); + PrintOption(opt_version); + + exit(EXIT_SUCCESS); +} + +class ConfigLoader +{ + Error &error; + bool result; +public: + ConfigLoader(Error &_error) : error(_error), result(false) { } + + bool GetResult() const { return result; } + + bool TryFile(const Path path); + bool TryFile(const AllocatedPath &base_path, + PathTraitsFS::const_pointer path); +}; + +bool ConfigLoader::TryFile(Path path) +{ + if (FileExists(path)) { + result = ReadConfigFile(path, error); + return true; + } + return false; +} + +bool ConfigLoader::TryFile(const AllocatedPath &base_path, + PathTraitsFS::const_pointer path) +{ + if (base_path.IsNull()) + return false; + auto full_path = AllocatedPath::Build(base_path, path); + return TryFile(full_path); } bool parse_cmdline(int argc, char **argv, struct options *options, Error &error) { - GOptionContext *context; - bool ret; - static gboolean option_version, - option_no_daemon, - option_no_config; - const GOptionEntry entries[] = { - { "kill", 0, 0, G_OPTION_ARG_NONE, &options->kill, - "kill the currently running mpd session", nullptr }, - { "no-config", 0, 0, G_OPTION_ARG_NONE, &option_no_config, - "don't read from config", nullptr }, - { "no-daemon", 0, 0, G_OPTION_ARG_NONE, &option_no_daemon, - "don't detach from console", nullptr }, - { "stdout", 0, 0, G_OPTION_ARG_NONE, &options->log_stderr, - nullptr, nullptr }, - { "stderr", 0, 0, G_OPTION_ARG_NONE, &options->log_stderr, - "print messages to stderr", nullptr }, - { "verbose", 'v', 0, G_OPTION_ARG_NONE, &options->verbose, - "verbose logging", nullptr }, - { "version", 'V', 0, G_OPTION_ARG_NONE, &option_version, - "print version number", nullptr }, - { nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr } - }; - + bool use_config_file = true; options->kill = false; options->daemon = true; options->log_stderr = false; options->verbose = false; - context = g_option_context_new("[path/to/mpd.conf]"); - g_option_context_add_main_entries(context, entries, nullptr); - - g_option_context_set_summary(context, summary); - - GError *gerror = nullptr; - ret = g_option_context_parse(context, &argc, &argv, &gerror); - g_option_context_free(context); - - if (!ret) - FatalError("option parsing failed", gerror); + // First pass: handle command line options + OptionParser parser(argc, argv); + while (parser.HasEntries()) { + if (!parser.ParseNext()) + continue; + if (parser.CheckOption(opt_kill)) { + options->kill = true; + continue; + } + if (parser.CheckOption(opt_no_config)) { + use_config_file = false; + continue; + } + if (parser.CheckOption(opt_no_daemon)) { + options->daemon = false; + continue; + } + if (parser.CheckOption(opt_stderr, opt_stdout)) { + options->log_stderr = true; + continue; + } + if (parser.CheckOption(opt_verbose)) { + options->verbose = true; + continue; + } + if (parser.CheckOption(opt_version)) + version(); + if (parser.CheckOption(opt_help, opt_help_alt)) + help(); - if (option_version) - version(); + error.Format(cmdline_domain, "invalid option: %s", + parser.GetOption()); + return false; + } /* initialize the logging library, so the configuration file parser can use it already */ log_early_init(options->verbose); - options->daemon = !option_no_daemon; - - if (option_no_config) { + if (!use_config_file) { LogDebug(cmdline_domain, "Ignoring config, using daemon defaults"); return true; - } else if (argc <= 1) { - /* default configuration file path */ + } -#ifdef WIN32 - AllocatedPath path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_user_config_dir()), - CONFIG_FILE_LOCATION); - if (!path.IsNull() && FileExists(path)) - return ReadConfigFile(path, error); - - const char *const*system_config_dirs = - g_get_system_config_dirs(); - - for (unsigned i = 0; system_config_dirs[i] != nullptr; ++i) { - path = PathBuildChecked(AllocatedPath::FromUTF8(system_config_dirs[i]), - CONFIG_FILE_LOCATION); - if (!path.IsNull() && FileExists(path)) - return ReadConfigFile(path, error); + // Second pass: find non-option parameters (i.e. config file) + const char *config_file = nullptr; + for (int i = 1; i < argc; ++i) { + if (OptionParser::IsOption(argv[i])) + continue; + if (config_file == nullptr) { + config_file = argv[i]; + continue; } + error.Set(cmdline_domain, "too many arguments"); + return false; + } + + if (config_file != nullptr) { + /* use specified configuration file */ + return ReadConfigFile(Path::FromFS(config_file), error); + } + + /* use default configuration file path */ + + ConfigLoader loader(error); + + bool found = +#ifdef WIN32 + loader.TryFile(GetUserConfigDir(), CONFIG_FILE_LOCATION) || + loader.TryFile(GetSystemConfigDir(), CONFIG_FILE_LOCATION) || + loader.TryFile(GetAppBaseDir(), APP_CONFIG_FILE_LOCATION); #else - AllocatedPath path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_user_config_dir()), - USER_CONFIG_FILE_LOCATION_XDG); - if (!path.IsNull() && FileExists(path)) - return ReadConfigFile(path, error); - - path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_home_dir()), - USER_CONFIG_FILE_LOCATION1); - if (!path.IsNull() && FileExists(path)) - return ReadConfigFile(path, error); - - path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_home_dir()), - USER_CONFIG_FILE_LOCATION2); - if (!path.IsNull() && FileExists(path)) - return ReadConfigFile(path, error); - - path = AllocatedPath::FromUTF8(SYSTEM_CONFIG_FILE_LOCATION); - if (!path.IsNull() && FileExists(path)) - return ReadConfigFile(path, error); + loader.TryFile(GetUserConfigDir(), + USER_CONFIG_FILE_LOCATION_XDG) || + loader.TryFile(GetHomeDir(), USER_CONFIG_FILE_LOCATION1) || + loader.TryFile(GetHomeDir(), USER_CONFIG_FILE_LOCATION2) || + loader.TryFile(Path::FromFS(SYSTEM_CONFIG_FILE_LOCATION)); #endif - + if (!found) { error.Set(cmdline_domain, "No configuration file found"); return false; - } else if (argc == 2) { - /* specified configuration file */ - return ReadConfigFile(Path::FromFS(argv[1]), error); - } else { - error.Set(cmdline_domain, "too many arguments"); - return false; } + + return loader.GetResult(); } diff --git a/src/CommandLine.hxx b/src/CommandLine.hxx index 214150eae..3b24a0d77 100644 --- a/src/CommandLine.hxx +++ b/src/CommandLine.hxx @@ -20,15 +20,13 @@ #ifndef MPD_COMMAND_LINE_HXX #define MPD_COMMAND_LINE_HXX -#include <glib.h> - class Error; struct options { - gboolean kill; - gboolean daemon; - gboolean log_stderr; - gboolean verbose; + bool kill; + bool daemon; + bool log_stderr; + bool verbose; }; bool diff --git a/src/ConfigData.cxx b/src/ConfigData.cxx index c6cabee6e..cd4e34a94 100644 --- a/src/ConfigData.cxx +++ b/src/ConfigData.cxx @@ -26,7 +26,6 @@ #include "system/FatalError.hxx" #include <assert.h> -#include <string.h> #include <stdlib.h> int @@ -64,7 +63,7 @@ block_param::GetBoolValue() const } config_param::config_param(const char *_value, int _line) - :next(nullptr), value(_value), line(_line) {} + :next(nullptr), value(_value), line(_line), used(false) {} config_param::~config_param() { diff --git a/src/ConfigFile.cxx b/src/ConfigFile.cxx index 90859a89a..16b23bc0d 100644 --- a/src/ConfigFile.cxx +++ b/src/ConfigFile.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "ConfigFile.hxx" -#include "ConfigError.hxx" #include "ConfigData.hxx" #include "ConfigTemplates.hxx" #include "util/Tokenizer.hxx" @@ -32,9 +31,7 @@ #include "Log.hxx" #include <assert.h> -#include <string.h> #include <stdio.h> -#include <errno.h> #define MAX_STRING_SIZE MPD_PATH_MAX+80 diff --git a/src/ConfigGlobal.cxx b/src/ConfigGlobal.cxx index 49b9c08fb..93929ccb4 100644 --- a/src/ConfigGlobal.cxx +++ b/src/ConfigGlobal.cxx @@ -30,8 +30,6 @@ #include "system/FatalError.hxx" #include "Log.hxx" -#include <assert.h> -#include <string.h> #include <stdlib.h> static ConfigData config_data; diff --git a/src/ConfigPath.cxx b/src/ConfigPath.cxx index b88de3934..a2d7c2bb4 100644 --- a/src/ConfigPath.cxx +++ b/src/ConfigPath.cxx @@ -22,11 +22,10 @@ #include "fs/AllocatedPath.hxx" #include "fs/Traits.hxx" #include "fs/Domain.hxx" +#include "fs/StandardDirectory.hxx" #include "util/Error.hxx" #include "ConfigGlobal.hxx" -#include <glib.h> - #include <assert.h> #include <string.h> @@ -39,14 +38,14 @@ static AllocatedPath GetHome(const char *user, Error &error) { - passwd *pw = getpwnam(user); - if (pw == nullptr) { + AllocatedPath result = GetHomeDir(user); + if (result.IsNull()) { error.Format(path_domain, "no such user: %s", user); return AllocatedPath::Null(); } - return AllocatedPath::FromFS(pw->pw_dir); + return result; } /** @@ -55,14 +54,14 @@ GetHome(const char *user, Error &error) static AllocatedPath GetHome(Error &error) { - const char *home = g_get_home_dir(); - if (home == nullptr) { + AllocatedPath result = GetHomeDir(); + if (result.IsNull()) { error.Set(path_domain, "problems getting home for current user"); return AllocatedPath::Null(); } - return AllocatedPath::FromUTF8(home, error); + return result; } /** @@ -119,7 +118,7 @@ ParsePath(const char *path, Error &error) return AllocatedPath::Null(); return AllocatedPath::Build(home, path2); - } else if (!PathTraits::IsAbsoluteUTF8(path)) { + } else if (!PathTraitsUTF8::IsAbsolute(path)) { error.Format(path_domain, "not an absolute path: %s", path); return AllocatedPath::Null(); diff --git a/src/ConfigTemplates.hxx b/src/ConfigTemplates.hxx index 4f5460460..1fbe3b737 100644 --- a/src/ConfigTemplates.hxx +++ b/src/ConfigTemplates.hxx @@ -20,8 +20,6 @@ #ifndef MPD_CONFIG_TEMPLATES_HXX #define MPD_CONFIG_TEMPLATES_HXX -#include "ConfigOption.hxx" - struct ConfigTemplate { const char *const name; const bool repeatable; diff --git a/src/CrossFade.cxx b/src/CrossFade.cxx index 601d74dc2..c1dafd951 100644 --- a/src/CrossFade.cxx +++ b/src/CrossFade.cxx @@ -26,8 +26,6 @@ #include "Log.hxx" #include <assert.h> -#include <string.h> -#include <stdlib.h> static constexpr Domain cross_fade_domain("cross_fade"); diff --git a/src/Daemon.cxx b/src/Daemon.cxx index 557c47777..0a4eea841 100644 --- a/src/Daemon.cxx +++ b/src/Daemon.cxx @@ -25,14 +25,9 @@ #include "util/Domain.hxx" #include "Log.hxx" -#include <glib.h> - #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#include <string.h> -#include <sys/types.h> -#include <sys/stat.h> #include <fcntl.h> #ifndef WIN32 @@ -215,10 +210,10 @@ daemonize_init(const char *user, const char *group, AllocatedPath &&_pidfile) user_uid = pwd->pw_uid; user_gid = pwd->pw_gid; - user_name = g_strdup(user); + user_name = strdup(user); /* this is needed by libs such as arts */ - g_setenv("HOME", pwd->pw_dir, true); + setenv("HOME", pwd->pw_dir, true); } if (group) { @@ -241,7 +236,7 @@ daemonize_finish(void) pidfile = AllocatedPath::Null(); } - g_free(user_name); + free(user_name); } #endif diff --git a/src/DatabaseGlue.cxx b/src/DatabaseGlue.cxx index 013a3e329..bc99baa4f 100644 --- a/src/DatabaseGlue.cxx +++ b/src/DatabaseGlue.cxx @@ -21,7 +21,6 @@ #include "DatabaseGlue.hxx" #include "DatabaseSimple.hxx" #include "DatabaseRegistry.hxx" -#include "DatabaseSave.hxx" #include "DatabaseError.hxx" #include "Directory.hxx" #include "util/Error.hxx" @@ -30,13 +29,8 @@ #include "DatabasePlugin.hxx" #include "db/SimpleDatabasePlugin.hxx" -#include <sys/types.h> -#include <sys/stat.h> -#include <unistd.h> #include <assert.h> #include <string.h> -#include <errno.h> - static Database *db; static bool db_is_open; diff --git a/src/DatabaseGlue.hxx b/src/DatabaseGlue.hxx index bc05942e0..725aa302a 100644 --- a/src/DatabaseGlue.hxx +++ b/src/DatabaseGlue.hxx @@ -44,7 +44,7 @@ DatabaseGlobalOpen(Error &error); * Returns the global #Database instance. May return nullptr if this MPD * configuration has no database (no music_directory was configured). */ -gcc_pure +gcc_const const Database * GetDatabase(); diff --git a/src/DatabaseHelpers.hxx b/src/DatabaseHelpers.hxx index d8806bc69..9dc82bcda 100644 --- a/src/DatabaseHelpers.hxx +++ b/src/DatabaseHelpers.hxx @@ -22,7 +22,6 @@ #include "DatabaseVisitor.hxx" #include "tag/TagType.h" -#include "Compiler.h" class Error; class Database; diff --git a/src/DatabaseLock.cxx b/src/DatabaseLock.cxx index d85f72d3b..660515d4e 100644 --- a/src/DatabaseLock.cxx +++ b/src/DatabaseLock.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "DatabaseLock.hxx" -#include "Compiler.h" Mutex db_mutex; diff --git a/src/DatabasePrint.cxx b/src/DatabasePrint.cxx index 3732e98f3..e500ee0c8 100644 --- a/src/DatabasePrint.cxx +++ b/src/DatabasePrint.cxx @@ -21,7 +21,6 @@ #include "DatabasePrint.hxx" #include "DatabaseSelection.hxx" #include "SongFilter.hxx" -#include "PlaylistVector.hxx" #include "SongPrint.hxx" #include "TimePrint.hxx" #include "Directory.hxx" diff --git a/src/DatabasePrint.hxx b/src/DatabasePrint.hxx index 36a68d87b..0ed7ff1e7 100644 --- a/src/DatabasePrint.hxx +++ b/src/DatabasePrint.hxx @@ -24,7 +24,6 @@ class SongFilter; struct DatabaseSelection; -struct db_visitor; class Client; class Error; diff --git a/src/DatabaseQueue.cxx b/src/DatabaseQueue.cxx index 98f80c5b6..b093ce073 100644 --- a/src/DatabaseQueue.cxx +++ b/src/DatabaseQueue.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "DatabaseQueue.hxx" -#include "DatabaseSelection.hxx" #include "DatabaseGlue.hxx" #include "DatabasePlugin.hxx" #include "Partition.hxx" diff --git a/src/DatabaseSave.cxx b/src/DatabaseSave.cxx index abfd4a34f..f41edefda 100644 --- a/src/DatabaseSave.cxx +++ b/src/DatabaseSave.cxx @@ -23,17 +23,14 @@ #include "DatabaseError.hxx" #include "Directory.hxx" #include "DirectorySave.hxx" -#include "Song.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "tag/Tag.hxx" #include "tag/TagSettings.h" #include "fs/Charset.hxx" +#include "util/StringUtil.hxx" #include "util/Error.hxx" #include "Log.hxx" -#include <glib.h> - -#include <assert.h> #include <string.h> #include <stdlib.h> @@ -83,16 +80,16 @@ db_load_internal(TextFile &file, Directory &music_root, Error &error) while ((line = file.ReadLine()) != nullptr && strcmp(line, DIRECTORY_INFO_END) != 0) { - if (g_str_has_prefix(line, DB_FORMAT_PREFIX)) { + if (StringStartsWith(line, DB_FORMAT_PREFIX)) { format = atoi(line + sizeof(DB_FORMAT_PREFIX) - 1); - } else if (g_str_has_prefix(line, DIRECTORY_MPD_VERSION)) { + } else if (StringStartsWith(line, DIRECTORY_MPD_VERSION)) { if (found_version) { error.Set(db_domain, "Duplicate version line"); return false; } found_version = true; - } else if (g_str_has_prefix(line, DIRECTORY_FS_CHARSET)) { + } else if (StringStartsWith(line, DIRECTORY_FS_CHARSET)) { const char *new_charset; if (found_charset) { @@ -113,7 +110,7 @@ db_load_internal(TextFile &file, Directory &music_root, Error &error) new_charset, old_charset); return false; } - } else if (g_str_has_prefix(line, DB_TAG_PREFIX)) { + } else if (StringStartsWith(line, DB_TAG_PREFIX)) { const char *name = line + sizeof(DB_TAG_PREFIX) - 1; TagType tag = tag_name_parse(name); if (tag == TAG_NUM_OF_ITEM_TYPES) { diff --git a/src/DecoderAPI.cxx b/src/DecoderAPI.cxx index 4fea02bef..a3f2fc3e0 100644 --- a/src/DecoderAPI.cxx +++ b/src/DecoderAPI.cxx @@ -20,6 +20,7 @@ #include "config.h" #include "DecoderAPI.hxx" #include "DecoderError.hxx" +#include "pcm/PcmConvert.hxx" #include "AudioConfig.hxx" #include "ReplayGainConfig.hxx" #include "MusicChunk.hxx" @@ -33,7 +34,6 @@ #include "Log.hxx" #include <assert.h> -#include <stdlib.h> #include <string.h> #include <math.h> @@ -47,6 +47,7 @@ decoder_initialized(Decoder &decoder, assert(dc.state == DecoderState::START); assert(dc.pipe != nullptr); + assert(decoder.convert == nullptr); assert(decoder.stream_tag == nullptr); assert(decoder.decoder_tag == nullptr); assert(!decoder.seeking); @@ -59,19 +60,28 @@ decoder_initialized(Decoder &decoder, dc.seekable = seekable; dc.total_time = total_time; - dc.Lock(); - dc.state = DecoderState::DECODE; - dc.client_cond.signal(); - dc.Unlock(); - FormatDebug(decoder_domain, "audio_format=%s, seekable=%s", audio_format_to_string(dc.in_audio_format, &af_string), seekable ? "true" : "false"); - if (dc.in_audio_format != dc.out_audio_format) + if (dc.in_audio_format != dc.out_audio_format) { FormatDebug(decoder_domain, "converting to %s", audio_format_to_string(dc.out_audio_format, &af_string)); + + decoder.convert = new PcmConvert(); + + Error error; + if (!decoder.convert->Open(dc.in_audio_format, + dc.out_audio_format, + error)) + decoder.error = std::move(error); + } + + dc.Lock(); + dc.state = DecoderState::DECODE; + dc.client_cond.signal(); + dc.Unlock(); } /** @@ -129,6 +139,10 @@ gcc_pure static DecoderCommand decoder_get_virtual_command(Decoder &decoder) { + if (decoder.error.IsDefined()) + /* an error has occurred: stop the decoder plugin */ + return DecoderCommand::STOP; + const DecoderControl &dc = decoder.dc; assert(dc.pipe != nullptr); @@ -292,6 +306,40 @@ decoder_read(Decoder *decoder, return nbytes; } +bool +decoder_read_full(Decoder *decoder, InputStream &is, + void *_buffer, size_t size) +{ + uint8_t *buffer = (uint8_t *)_buffer; + + while (size > 0) { + size_t nbytes = decoder_read(decoder, is, buffer, size); + if (nbytes == 0) + return false; + + buffer += nbytes; + size -= nbytes; + } + + return true; +} + +bool +decoder_skip(Decoder *decoder, InputStream &is, size_t size) +{ + while (size > 0) { + char buffer[1024]; + size_t nbytes = decoder_read(decoder, is, buffer, + std::min(sizeof(buffer), size)); + if (nbytes == 0) + return false; + + size -= nbytes; + } + + return true; +} + void decoder_timestamp(Decoder &decoder, double t) { @@ -312,12 +360,12 @@ do_send_tag(Decoder &decoder, const Tag &tag) if (decoder.chunk != nullptr) { /* there is a partial chunk - flush it, we want the tag in a new chunk */ - decoder_flush_chunk(decoder); + decoder.FlushChunk(); } assert(decoder.chunk == nullptr); - chunk = decoder_get_chunk(decoder); + chunk = decoder.GetChunk(); if (chunk == nullptr) { assert(decoder.dc.command != DecoderCommand::NONE); return decoder.dc.command; @@ -388,13 +436,13 @@ decoder_data(Decoder &decoder, return cmd; } - if (dc.in_audio_format != dc.out_audio_format) { + if (decoder.convert != nullptr) { + assert(dc.in_audio_format != dc.out_audio_format); + Error error; - data = decoder.conv_state.Convert(dc.in_audio_format, - data, length, - dc.out_audio_format, - &length, - error); + data = decoder.convert->Convert(data, length, + &length, + error); if (data == nullptr) { /* the PCM conversion has failed - stop playback, since we have no better way to @@ -402,13 +450,15 @@ decoder_data(Decoder &decoder, LogError(error); return DecoderCommand::STOP; } + } else { + assert(dc.in_audio_format == dc.out_audio_format); } while (length > 0) { struct music_chunk *chunk; bool full; - chunk = decoder_get_chunk(decoder); + chunk = decoder.GetChunk(); if (chunk == nullptr) { assert(dc.command != DecoderCommand::NONE); return dc.command; @@ -421,7 +471,7 @@ decoder_data(Decoder &decoder, kbit_rate); if (dest.IsNull()) { /* the chunk is full, flush it */ - decoder_flush_chunk(decoder); + decoder.FlushChunk(); continue; } @@ -440,7 +490,7 @@ decoder_data(Decoder &decoder, full = chunk->Expand(dc.out_audio_format, nbytes); if (full) { /* the chunk is full, flush it */ - decoder_flush_chunk(decoder); + decoder.FlushChunk(); } data = (const uint8_t *)data + nbytes; @@ -532,7 +582,7 @@ decoder_replay_gain(Decoder &decoder, /* flush the current chunk because the new replay gain values affect the following samples */ - decoder_flush_chunk(decoder); + decoder.FlushChunk(); } } else decoder.replay_gain_serial = 0; diff --git a/src/DecoderAPI.hxx b/src/DecoderAPI.hxx index 2ee42483c..bb693dbc4 100644 --- a/src/DecoderAPI.hxx +++ b/src/DecoderAPI.hxx @@ -27,6 +27,8 @@ #ifndef MPD_DECODER_API_HXX #define MPD_DECODER_API_HXX +// IWYU pragma: begin_exports + #include "check.h" #include "DecoderCommand.hxx" #include "DecoderPlugin.hxx" @@ -36,6 +38,8 @@ #include "MixRampInfo.hxx" #include "ConfigData.hxx" +// IWYU pragma: end_exports + /** * Notify the player thread that it has finished initialization and * that it has read the song's meta data. @@ -113,6 +117,25 @@ decoder_read(Decoder &decoder, InputStream &is, } /** + * Blocking read from the input stream. Attempts to fill the buffer + * completely; there is no partial result. + * + * @return true on success, false on error or command or not enough + * data + */ +bool +decoder_read_full(Decoder *decoder, InputStream &is, + void *buffer, size_t size); + +/** + * Skip data on the #InputStream. + * + * @return true on success, false on error or command + */ +bool +decoder_skip(Decoder *decoder, InputStream &is, size_t size); + +/** * Sets the time stamp for the next data chunk [seconds]. The MPD * core automatically counts it up, and a decoder plugin only needs to * use this function if it thinks that adding to the time stamp based diff --git a/src/DecoderControl.cxx b/src/DecoderControl.cxx index ab460ced0..b63dba3a0 100644 --- a/src/DecoderControl.cxx +++ b/src/DecoderControl.cxx @@ -22,8 +22,6 @@ #include "MusicPipe.hxx" #include "Song.hxx" -#include <glib.h> - #include <assert.h> DecoderControl::DecoderControl(Mutex &_mutex, Cond &_client_cond) diff --git a/src/DecoderInternal.cxx b/src/DecoderInternal.cxx index d5f40ad48..38e54a36c 100644 --- a/src/DecoderInternal.cxx +++ b/src/DecoderInternal.cxx @@ -20,6 +20,7 @@ #include "config.h" #include "DecoderInternal.hxx" #include "DecoderControl.hxx" +#include "pcm/PcmConvert.hxx" #include "MusicPipe.hxx" #include "MusicBuffer.hxx" #include "MusicChunk.hxx" @@ -32,6 +33,11 @@ Decoder::~Decoder() /* caller must flush the chunk */ assert(chunk == nullptr); + if (convert != nullptr) { + convert->Close(); + delete convert; + } + delete song_tag; delete stream_tag; delete decoder_tag; @@ -51,24 +57,21 @@ need_chunks(DecoderControl &dc) } struct music_chunk * -decoder_get_chunk(Decoder &decoder) +Decoder::GetChunk() { - DecoderControl &dc = decoder.dc; DecoderCommand cmd; - if (decoder.chunk != nullptr) - return decoder.chunk; + if (chunk != nullptr) + return chunk; do { - decoder.chunk = dc.buffer->Allocate(); - if (decoder.chunk != nullptr) { - decoder.chunk->replay_gain_serial = - decoder.replay_gain_serial; - if (decoder.replay_gain_serial != 0) - decoder.chunk->replay_gain_info = - decoder.replay_gain_info; - - return decoder.chunk; + chunk = dc.buffer->Allocate(); + if (chunk != nullptr) { + chunk->replay_gain_serial = replay_gain_serial; + if (replay_gain_serial != 0) + chunk->replay_gain_info = replay_gain_info; + + return chunk; } dc.Lock(); @@ -80,18 +83,16 @@ decoder_get_chunk(Decoder &decoder) } void -decoder_flush_chunk(Decoder &decoder) +Decoder::FlushChunk() { - DecoderControl &dc = decoder.dc; - - assert(decoder.chunk != nullptr); + assert(chunk != nullptr); - if (decoder.chunk->IsEmpty()) - dc.buffer->Return(decoder.chunk); + if (chunk->IsEmpty()) + dc.buffer->Return(chunk); else - dc.pipe->Push(decoder.chunk); + dc.pipe->Push(chunk); - decoder.chunk = nullptr; + chunk = nullptr; dc.Lock(); if (dc.client_is_waiting) diff --git a/src/DecoderInternal.hxx b/src/DecoderInternal.hxx index 46069a561..fbd613a36 100644 --- a/src/DecoderInternal.hxx +++ b/src/DecoderInternal.hxx @@ -20,18 +20,21 @@ #ifndef MPD_DECODER_INTERNAL_HXX #define MPD_DECODER_INTERNAL_HXX -#include "DecoderCommand.hxx" -#include "pcm/PcmConvert.hxx" #include "ReplayGainInfo.hxx" +#include "util/Error.hxx" +class PcmConvert; struct DecoderControl; -struct InputStream; struct Tag; struct Decoder { DecoderControl &dc; - PcmConvert conv_state; + /** + * For converting input data to the configured audio format. + * nullptr means no conversion necessary. + */ + PcmConvert *convert; /** * The time stamp of the next data chunk, in seconds. @@ -83,8 +86,15 @@ struct Decoder { */ unsigned replay_gain_serial; + /** + * An error has occurred (in DecoderAPI.cxx), and the plugin + * will be asked to stop. + */ + Error error; + Decoder(DecoderControl &_dc, bool _initial_seek_pending, Tag *_tag) :dc(_dc), + convert(nullptr), timestamp(0), initial_seek_pending(_initial_seek_pending), initial_seek_running(false), @@ -95,23 +105,21 @@ struct Decoder { } ~Decoder(); -}; -/** - * Returns the current chunk the decoder writes to, or allocates a new - * chunk if there is none. - * - * @return the chunk, or NULL if we have received a decoder command - */ -struct music_chunk * -decoder_get_chunk(Decoder &decoder); + /** + * Returns the current chunk the decoder writes to, or allocates a new + * chunk if there is none. + * + * @return the chunk, or NULL if we have received a decoder command + */ + music_chunk *GetChunk(); -/** - * Flushes the current chunk. - * - * Caller must not lock the #DecoderControl object. - */ -void -decoder_flush_chunk(Decoder &decoder); + /** + * Flushes the current chunk. + * + * Caller must not lock the #DecoderControl object. + */ + void FlushChunk(); +}; #endif diff --git a/src/DecoderPlugin.hxx b/src/DecoderPlugin.hxx index 6b0340123..41e009bec 100644 --- a/src/DecoderPlugin.hxx +++ b/src/DecoderPlugin.hxx @@ -24,7 +24,6 @@ struct config_param; struct InputStream; -struct Tag; struct tag_handler; /** diff --git a/src/DecoderThread.cxx b/src/DecoderThread.cxx index 72fc3cfb4..3ccbd1082 100644 --- a/src/DecoderThread.cxx +++ b/src/DecoderThread.cxx @@ -29,7 +29,6 @@ #include "fs/Traits.hxx" #include "fs/AllocatedPath.hxx" #include "DecoderAPI.hxx" -#include "tag/Tag.hxx" #include "InputStream.hxx" #include "DecoderList.hxx" #include "util/UriUtil.hxx" @@ -146,7 +145,7 @@ decoder_file_decode(const DecoderPlugin &plugin, assert(decoder.stream_tag == nullptr); assert(decoder.decoder_tag == nullptr); assert(path != nullptr); - assert(PathTraits::IsAbsoluteFS(path)); + assert(PathTraitsFS::IsAbsolute(path)); assert(decoder.dc.state == DecoderState::START); FormatDebug(decoder_thread_domain, "probing plugin %s", plugin.name); @@ -356,11 +355,16 @@ decoder_run_song(DecoderControl &dc, /* flush the last chunk */ if (decoder.chunk != nullptr) - decoder_flush_chunk(decoder); + decoder.FlushChunk(); dc.Lock(); - if (ret) + if (decoder.error.IsDefined()) { + /* copy the Error from sruct Decoder to + DecoderControl */ + dc.state = DecoderState::ERROR; + dc.error = std::move(decoder.error); + } else if (ret) dc.state = DecoderState::STOP; else { dc.state = DecoderState::ERROR; diff --git a/src/DespotifyUtils.cxx b/src/DespotifyUtils.cxx index e91587a7f..5bfcfd6a4 100644 --- a/src/DespotifyUtils.cxx +++ b/src/DespotifyUtils.cxx @@ -19,6 +19,7 @@ #include "DespotifyUtils.hxx" #include "tag/Tag.hxx" +#include "tag/TagBuilder.hxx" #include "ConfigGlobal.hxx" #include "ConfigOption.hxx" #include "util/Domain.hxx" @@ -90,25 +91,24 @@ mpd_despotify_tag_from_track(struct ds_track *track) char comment[80]; char date[20]; - Tag *tag = new Tag(); - if (!track->has_meta_data) - return tag; + return new Tag(); + TagBuilder tag; snprintf(tracknum, sizeof(tracknum), "%d", track->tracknumber); snprintf(date, sizeof(date), "%d", track->year); snprintf(comment, sizeof(comment), "Bitrate %d Kbps, %sgeo restricted", track->file_bitrate / 1000, track->geo_restricted ? "" : "not "); - tag->AddItem(TAG_TITLE, track->title); - tag->AddItem(TAG_ARTIST, track->artist->name); - tag->AddItem(TAG_TRACK, tracknum); - tag->AddItem(TAG_ALBUM, track->album); - tag->AddItem(TAG_DATE, date); - tag->AddItem(TAG_COMMENT, comment); - tag->time = track->length / 1000; - - return tag; + tag.AddItem(TAG_TITLE, track->title); + tag.AddItem(TAG_ARTIST, track->artist->name); + tag.AddItem(TAG_TRACK, tracknum); + tag.AddItem(TAG_ALBUM, track->album); + tag.AddItem(TAG_DATE, date); + tag.AddItem(TAG_COMMENT, comment); + tag.SetTime(track->length / 1000); + + return tag.Commit(); } struct despotify_session *mpd_despotify_get_session(void) diff --git a/src/Directory.cxx b/src/Directory.cxx index b2942588e..a64b4d84c 100644 --- a/src/Directory.cxx +++ b/src/Directory.cxx @@ -114,7 +114,7 @@ Directory::GetName() const { assert(!IsRoot()); - return PathTraits::GetBaseUTF8(path); + return PathTraitsUTF8::GetBase(path); } Directory * diff --git a/src/DirectorySave.cxx b/src/DirectorySave.cxx index fa330d126..709184289 100644 --- a/src/DirectorySave.cxx +++ b/src/DirectorySave.cxx @@ -23,15 +23,13 @@ #include "Song.hxx" #include "SongSave.hxx" #include "PlaylistDatabase.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" +#include "util/StringUtil.hxx" #include "util/NumberParser.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" -#include <glib.h> - -#include <assert.h> -#include <string.h> +#include <stddef.h> #define DIRECTORY_DIR "directory: " #define DIRECTORY_MTIME "mtime: " @@ -91,7 +89,7 @@ directory_load_subdir(TextFile &file, Directory &parent, const char *name, return nullptr; } - if (g_str_has_prefix(line, DIRECTORY_MTIME)) { + if (StringStartsWith(line, DIRECTORY_MTIME)) { directory->mtime = ParseUint64(line + sizeof(DIRECTORY_MTIME) - 1); @@ -103,7 +101,7 @@ directory_load_subdir(TextFile &file, Directory &parent, const char *name, } } - if (!g_str_has_prefix(line, DIRECTORY_BEGIN)) { + if (!StringStartsWith(line, DIRECTORY_BEGIN)) { error.Format(directory_domain, "Malformed line: %s", line); directory->Delete(); return nullptr; @@ -124,15 +122,15 @@ directory_load(TextFile &file, Directory &directory, Error &error) const char *line; while ((line = file.ReadLine()) != nullptr && - !g_str_has_prefix(line, DIRECTORY_END)) { - if (g_str_has_prefix(line, DIRECTORY_DIR)) { + !StringStartsWith(line, DIRECTORY_END)) { + if (StringStartsWith(line, DIRECTORY_DIR)) { Directory *subdir = directory_load_subdir(file, directory, line + sizeof(DIRECTORY_DIR) - 1, error); if (subdir == nullptr) return false; - } else if (g_str_has_prefix(line, SONG_BEGIN)) { + } else if (StringStartsWith(line, SONG_BEGIN)) { const char *name = line + sizeof(SONG_BEGIN) - 1; Song *song; @@ -147,19 +145,11 @@ directory_load(TextFile &file, Directory &directory, Error &error) return false; directory.AddSong(song); - } else if (g_str_has_prefix(line, PLAYLIST_META_BEGIN)) { - /* duplicate the name, because - playlist_metadata_load() will overwrite the - buffer */ - char *name = g_strdup(line + sizeof(PLAYLIST_META_BEGIN) - 1); - + } else if (StringStartsWith(line, PLAYLIST_META_BEGIN)) { + const char *name = line + sizeof(PLAYLIST_META_BEGIN) - 1; if (!playlist_metadata_load(file, directory.playlists, - name, error)) { - g_free(name); + name, error)) return false; - } - - g_free(name); } else { error.Format(directory_domain, "Malformed line: %s", line); diff --git a/src/EncoderAPI.hxx b/src/EncoderAPI.hxx index b3397f25c..3ae1879cb 100644 --- a/src/EncoderAPI.hxx +++ b/src/EncoderAPI.hxx @@ -25,9 +25,13 @@ #ifndef MPD_ENCODER_API_HXX #define MPD_ENCODER_API_HXX +// IWYU pragma: begin_exports + #include "EncoderPlugin.hxx" #include "AudioFormat.hxx" #include "tag/Tag.hxx" #include "ConfigData.hxx" +// IWYU pragma: end_exports + #endif diff --git a/src/FilterConfig.cxx b/src/FilterConfig.cxx index cfac1c756..423b84086 100644 --- a/src/FilterConfig.cxx +++ b/src/FilterConfig.cxx @@ -21,8 +21,6 @@ #include "FilterConfig.hxx" #include "filter/ChainFilterPlugin.hxx" #include "FilterPlugin.hxx" -#include "FilterInternal.hxx" -#include "FilterRegistry.hxx" #include "ConfigData.hxx" #include "ConfigOption.hxx" #include "ConfigGlobal.hxx" diff --git a/src/FilterPlugin.cxx b/src/FilterPlugin.cxx index 608542f92..d5e3b664a 100644 --- a/src/FilterPlugin.cxx +++ b/src/FilterPlugin.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "FilterPlugin.hxx" -#include "FilterInternal.hxx" #include "FilterRegistry.hxx" #include "ConfigData.hxx" #include "ConfigError.hxx" diff --git a/src/FilterRegistry.cxx b/src/FilterRegistry.cxx index b3b08505e..427f4e875 100644 --- a/src/FilterRegistry.cxx +++ b/src/FilterRegistry.cxx @@ -21,7 +21,6 @@ #include "FilterRegistry.hxx" #include "FilterPlugin.hxx" -#include <stddef.h> #include <string.h> const struct filter_plugin *const filter_plugins[] = { diff --git a/src/GlobalEvents.cxx b/src/GlobalEvents.cxx index 86bfb3e2a..b7ba85286 100644 --- a/src/GlobalEvents.cxx +++ b/src/GlobalEvents.cxx @@ -21,7 +21,6 @@ #include "GlobalEvents.hxx" #include "util/Manual.hxx" #include "event/DeferredMonitor.hxx" -#include "Compiler.h" #include <atomic> diff --git a/src/IcyMetaDataParser.cxx b/src/IcyMetaDataParser.cxx index 8861efb2e..5444db8d9 100644 --- a/src/IcyMetaDataParser.cxx +++ b/src/IcyMetaDataParser.cxx @@ -20,6 +20,7 @@ #include "config.h" #include "IcyMetaDataParser.hxx" #include "tag/Tag.hxx" +#include "tag/TagBuilder.hxx" #include "util/Domain.hxx" #include "Log.hxx" @@ -37,7 +38,7 @@ IcyMetaDataParser::Reset() return; if (data_rest == 0 && meta_size > 0) - g_free(meta_data); + delete[] meta_data; delete tag; @@ -66,7 +67,7 @@ IcyMetaDataParser::Data(size_t length) } static void -icy_add_item(Tag &tag, TagType type, const char *value) +icy_add_item(TagBuilder &tag, TagType type, const char *value) { size_t length = strlen(value); @@ -81,7 +82,7 @@ icy_add_item(Tag &tag, TagType type, const char *value) } static void -icy_parse_tag_item(Tag &tag, const char *item) +icy_parse_tag_item(TagBuilder &tag, const char *item) { gchar **p = g_strsplit(item, "=", 0); @@ -99,15 +100,16 @@ icy_parse_tag_item(Tag &tag, const char *item) static Tag * icy_parse_tag(const char *p) { - Tag *tag = new Tag(); + TagBuilder tag; + gchar **items = g_strsplit(p, ";", 0); for (unsigned i = 0; items[i] != nullptr; ++i) - icy_parse_tag_item(*tag, items[i]); + icy_parse_tag_item(tag, items[i]); g_strfreev(items); - return tag; + return tag.Commit(); } size_t @@ -136,7 +138,7 @@ IcyMetaDataParser::Meta(const void *data, size_t length) /* initialize metadata reader, allocate enough memory (+1 for the null terminator) */ meta_position = 0; - meta_data = (char *)g_malloc(meta_size + 1); + meta_data = new char[meta_size + 1]; } assert(meta_position < meta_size); @@ -161,7 +163,7 @@ IcyMetaDataParser::Meta(const void *data, size_t length) delete tag; tag = icy_parse_tag(meta_data); - g_free(meta_data); + delete[] meta_data; /* change back to normal data mode */ diff --git a/src/IcyMetaDataServer.cxx b/src/IcyMetaDataServer.cxx index f5e981c2f..eebe8d0d4 100644 --- a/src/IcyMetaDataServer.cxx +++ b/src/IcyMetaDataServer.cxx @@ -25,7 +25,6 @@ #include <glib.h> -#include <assert.h> #include <string.h> char* diff --git a/src/InotifyQueue.cxx b/src/InotifyQueue.cxx index c24816241..7a652ef01 100644 --- a/src/InotifyQueue.cxx +++ b/src/InotifyQueue.cxx @@ -21,7 +21,6 @@ #include "InotifyQueue.hxx" #include "InotifyDomain.hxx" #include "UpdateGlue.hxx" -#include "event/Loop.hxx" #include "Log.hxx" #include <string.h> diff --git a/src/InotifySource.hxx b/src/InotifySource.hxx index f6ddea966..026b156e6 100644 --- a/src/InotifySource.hxx +++ b/src/InotifySource.hxx @@ -22,7 +22,6 @@ #include "event/SocketMonitor.hxx" #include "util/FifoBuffer.hxx" -#include "Compiler.h" class Error; diff --git a/src/InputRegistry.cxx b/src/InputRegistry.cxx index aa6c06ed1..6e46d3cd7 100644 --- a/src/InputRegistry.cxx +++ b/src/InputRegistry.cxx @@ -22,6 +22,10 @@ #include "util/Macros.hxx" #include "input/FileInputPlugin.hxx" +#ifdef HAVE_ALSA +#include "input/AlsaInputPlugin.hxx" +#endif + #ifdef ENABLE_ARCHIVE #include "input/ArchiveInputPlugin.hxx" #endif @@ -34,6 +38,10 @@ #include "input/FfmpegInputPlugin.hxx" #endif +#ifdef ENABLE_SMBCLIENT +#include "input/SmbclientInputPlugin.hxx" +#endif + #ifdef ENABLE_MMS #include "input/MmsInputPlugin.hxx" #endif @@ -48,6 +56,9 @@ const InputPlugin *const input_plugins[] = { &input_plugin_file, +#ifdef HAVE_ALSA + &input_plugin_alsa, +#endif #ifdef ENABLE_ARCHIVE &input_plugin_archive, #endif @@ -57,6 +68,9 @@ const InputPlugin *const input_plugins[] = { #ifdef HAVE_FFMPEG &input_plugin_ffmpeg, #endif +#ifdef ENABLE_SMBCLIENT + &input_plugin_smbclient, +#endif #ifdef ENABLE_MMS &input_plugin_mms, #endif diff --git a/src/Log.cxx b/src/Log.cxx index a46c0ced8..c58790841 100644 --- a/src/Log.cxx +++ b/src/Log.cxx @@ -19,62 +19,19 @@ #include "config.h" #include "LogV.hxx" -#include "ConfigData.hxx" -#include "ConfigGlobal.hxx" -#include "ConfigOption.hxx" -#include "system/fd_util.h" -#include "system/FatalError.hxx" -#include "fs/Path.hxx" -#include "fs/FileSystem.hxx" #include "util/Error.hxx" -#include "util/Domain.hxx" -#include "system/FatalError.hxx" - -#include <glib.h> #include <assert.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <string.h> -#include <fcntl.h> #include <stdio.h> -#include <stdlib.h> -#include <time.h> -#include <unistd.h> +#include <string.h> #include <errno.h> -static GLogLevelFlags -ToGLib(LogLevel level) -{ - switch (level) { - case LogLevel::DEBUG: - return G_LOG_LEVEL_DEBUG; - - case LogLevel::INFO: - return G_LOG_LEVEL_INFO; - - case LogLevel::DEFAULT: - return G_LOG_LEVEL_MESSAGE; - - case LogLevel::WARNING: - case LogLevel::ERROR: - return G_LOG_LEVEL_WARNING; - } - - assert(false); - gcc_unreachable(); -} - -void -Log(const Domain &domain, LogLevel level, const char *msg) -{ - g_log(domain.GetName(), ToGLib(level), "%s", msg); -} - void LogFormatV(const Domain &domain, LogLevel level, const char *fmt, va_list ap) { - g_logv(domain.GetName(), ToGLib(level), fmt, ap); + char msg[1024]; + vsnprintf(msg, sizeof(msg), fmt, ap); + Log(domain, level, msg); } void @@ -159,7 +116,7 @@ FormatError(const Error &error, const char *fmt, ...) void LogErrno(const Domain &domain, int e, const char *msg) { - LogFormat(domain, LogLevel::ERROR, "%s: %s", msg, g_strerror(e)); + LogFormat(domain, LogLevel::ERROR, "%s: %s", msg, strerror(e)); } void diff --git a/src/Log.hxx b/src/Log.hxx index 920a8d8a3..c8e166056 100644 --- a/src/Log.hxx +++ b/src/Log.hxx @@ -20,47 +20,12 @@ #ifndef MPD_LOG_HXX #define MPD_LOG_HXX +#include "LogLevel.hxx" #include "Compiler.h" -#ifdef WIN32 -#include <windows.h> -/* damn you, windows.h! */ -#ifdef ERROR -#undef ERROR -#endif -#endif - class Error; class Domain; -enum class LogLevel { - /** - * Debug message for developers. - */ - DEBUG, - - /** - * Unimportant informational message. - */ - INFO, - - /** - * Interesting informational message. - */ - DEFAULT, - - /** - * Warning: something may be wrong. - */ - WARNING, - - /** - * An error has occurred, an operation could not finish - * successfully. - */ - ERROR, -}; - void Log(const Domain &domain, LogLevel level, const char *msg); diff --git a/src/LogBackend.cxx b/src/LogBackend.cxx new file mode 100644 index 000000000..bddf8db16 --- /dev/null +++ b/src/LogBackend.cxx @@ -0,0 +1,193 @@ +/* + * 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 "LogBackend.hxx" +#include "Log.hxx" +#include "util/Domain.hxx" +#include "util/CharUtil.hxx" + +#ifdef HAVE_GLIB +#include <glib.h> +#endif + +#include <assert.h> +#include <stdio.h> +#include <string.h> +#include <time.h> + +#ifdef HAVE_SYSLOG +#include <syslog.h> +#endif + +static LogLevel log_threshold = LogLevel::INFO; + +#ifdef HAVE_GLIB +static const char *log_charset; +#endif + +static bool enable_timestamp; + +#ifdef HAVE_SYSLOG +static bool enable_syslog; +#endif + +void +SetLogThreshold(LogLevel _threshold) +{ + log_threshold = _threshold; +} + +#ifdef HAVE_GLIB + +void +SetLogCharset(const char *_charset) +{ + log_charset = _charset; +} + +#endif + +void +EnableLogTimestamp() +{ +#ifdef HAVE_SYSLOG + assert(!enable_syslog); +#endif + assert(!enable_timestamp); + + enable_timestamp = true; +} + +static const char *log_date(void) +{ + static constexpr size_t LOG_DATE_BUF_SIZE = 16; + static char buf[LOG_DATE_BUF_SIZE]; + time_t t = time(nullptr); + strftime(buf, LOG_DATE_BUF_SIZE, "%b %d %H:%M : ", localtime(&t)); + return buf; +} + +/** + * Determines the length of the string excluding trailing whitespace + * characters. + */ +static int +chomp_length(const char *p) +{ + size_t length = strlen(p); + + while (length > 0 && IsWhitespaceOrNull(p[length - 1])) + --length; + + return (int)length; +} + +#ifdef HAVE_SYSLOG + +static int +ToSysLogLevel(LogLevel log_level) +{ + switch (log_level) { + case LogLevel::DEBUG: + return LOG_DEBUG; + + case LogLevel::INFO: + return LOG_INFO; + + case LogLevel::DEFAULT: + return LOG_NOTICE; + + case LogLevel::WARNING: + return LOG_WARNING; + + case LogLevel::ERROR: + return LOG_ERR; + } + + assert(false); + gcc_unreachable(); +} + +static void +SysLog(const Domain &domain, LogLevel log_level, const char *message) +{ + syslog(ToSysLogLevel(log_level), "%s: %.*s", + domain.GetName(), + chomp_length(message), message); +} + +void +LogInitSysLog() +{ + openlog(PACKAGE, 0, LOG_DAEMON); + enable_syslog = true; +} + +void +LogFinishSysLog() +{ + if (enable_syslog) + closelog(); +} + +#endif + +static void +FileLog(const Domain &domain, const char *message) +{ +#ifdef HAVE_GLIB + char *converted; + + if (log_charset != nullptr) { + converted = g_convert_with_fallback(message, -1, + log_charset, "utf-8", + nullptr, nullptr, + nullptr, nullptr); + if (converted != nullptr) + message = converted; + } else + converted = nullptr; +#endif + + fprintf(stderr, "%s%s: %.*s\n", + enable_timestamp ? log_date() : "", + domain.GetName(), + chomp_length(message), message); + +#ifdef HAVE_GLIB + g_free(converted); +#endif +} + +void +Log(const Domain &domain, LogLevel level, const char *msg) +{ + if (level < log_threshold) + return; + +#ifdef HAVE_SYSLOG + if (enable_syslog) { + SysLog(domain, level, msg); + return; + } +#endif + + FileLog(domain, msg); +} diff --git a/src/LogBackend.hxx b/src/LogBackend.hxx new file mode 100644 index 000000000..0d4808e48 --- /dev/null +++ b/src/LogBackend.hxx @@ -0,0 +1,40 @@ +/* + * 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_LOG_BACKEND_HXX +#define MPD_LOG_BACKEND_HXX + +#include "LogLevel.hxx" + +void +SetLogThreshold(LogLevel _threshold); + +void +SetLogCharset(const char *_charset); + +void +EnableLogTimestamp(); + +void +LogInitSysLog(); + +void +LogFinishSysLog(); + +#endif /* LOG_H */ diff --git a/src/LogInit.cxx b/src/LogInit.cxx index 41d13a5e8..bd858a32c 100644 --- a/src/LogInit.cxx +++ b/src/LogInit.cxx @@ -19,48 +19,36 @@ #include "config.h" #include "LogInit.hxx" +#include "LogBackend.hxx" #include "Log.hxx" #include "ConfigData.hxx" #include "ConfigGlobal.hxx" #include "ConfigOption.hxx" -#include "system/fd_util.h" #include "system/FatalError.hxx" #include "fs/AllocatedPath.hxx" #include "fs/FileSystem.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" -#include "util/CharUtil.hxx" #include "system/FatalError.hxx" +#ifdef HAVE_GLIB +#include <glib.h> +#endif + #include <assert.h> -#include <sys/types.h> -#include <sys/stat.h> #include <string.h> -#include <stdarg.h> #include <fcntl.h> #include <stdio.h> -#include <stdlib.h> #include <time.h> #include <unistd.h> -#include <errno.h> -#include <glib.h> - -#ifdef HAVE_SYSLOG -#include <syslog.h> -#endif -#define LOG_LEVEL_SECURE G_LOG_LEVEL_INFO +#define LOG_LEVEL_SECURE LogLevel::INFO #define LOG_DATE_BUF_SIZE 16 #define LOG_DATE_LEN (LOG_DATE_BUF_SIZE - 1) static constexpr Domain log_domain("log"); -static GLogLevelFlags log_threshold = G_LOG_LEVEL_MESSAGE; - -static const char *log_charset; - -static bool stdout_mode = true; static int out_fd; static AllocatedPath out_path = AllocatedPath::Null(); @@ -73,66 +61,6 @@ static void redirect_logs(int fd) FatalSystemError("Failed to dup2 stderr"); } -static const char *log_date(void) -{ - static char buf[LOG_DATE_BUF_SIZE]; - time_t t = time(nullptr); - strftime(buf, LOG_DATE_BUF_SIZE, "%b %d %H:%M : ", localtime(&t)); - return buf; -} - -/** - * Determines the length of the string excluding trailing whitespace - * characters. - */ -static int -chomp_length(const char *p) -{ - size_t length = strlen(p); - - while (length > 0 && IsWhitespaceOrNull(p[length - 1])) - --length; - - return (int)length; -} - -static void -file_log_func(const gchar *domain, - GLogLevelFlags log_level, - const gchar *message, gcc_unused gpointer user_data) -{ - char *converted; - - if (log_level > log_threshold) - return; - - if (log_charset != nullptr) { - converted = g_convert_with_fallback(message, -1, - log_charset, "utf-8", - nullptr, nullptr, - nullptr, nullptr); - if (converted != nullptr) - message = converted; - } else - converted = nullptr; - - if (domain == nullptr) - domain = ""; - - fprintf(stderr, "%s%s%s%.*s\n", - stdout_mode ? "" : log_date(), - domain, *domain == 0 ? "" : ": ", - chomp_length(message), message); - - g_free(converted); -} - -static void -log_init_stdout(void) -{ - g_log_set_default_handler(file_log_func, nullptr); -} - static int open_log_file(void) { @@ -154,85 +82,22 @@ log_init_file(unsigned line, Error &error) return false; } - g_log_set_default_handler(file_log_func, nullptr); + EnableLogTimestamp(); return true; } -#ifdef HAVE_SYSLOG - -static int -glib_to_syslog_level(GLogLevelFlags log_level) -{ - switch (log_level & G_LOG_LEVEL_MASK) { - case G_LOG_LEVEL_ERROR: - case G_LOG_LEVEL_CRITICAL: - return LOG_ERR; - - case G_LOG_LEVEL_WARNING: - return LOG_WARNING; - - case G_LOG_LEVEL_MESSAGE: - return LOG_NOTICE; - - case G_LOG_LEVEL_INFO: - return LOG_INFO; - - case G_LOG_LEVEL_DEBUG: - return LOG_DEBUG; - - default: - return LOG_NOTICE; - } -} - -static void -syslog_log_func(const gchar *domain, - GLogLevelFlags log_level, const gchar *message, - gcc_unused gpointer user_data) -{ - if (stdout_mode) { - /* fall back to the file log function during - startup */ - file_log_func(domain, log_level, - message, user_data); - return; - } - - if (log_level > log_threshold) - return; - - if (domain == nullptr) - domain = ""; - - syslog(glib_to_syslog_level(log_level), "%s%s%.*s", - domain, *domain == 0 ? "" : ": ", - chomp_length(message), message); -} - -static void -log_init_syslog(void) -{ - assert(out_path.IsNull()); - - openlog(PACKAGE, 0, LOG_DAEMON); - g_log_set_default_handler(syslog_log_func, nullptr); -} - -#endif - -static inline GLogLevelFlags +static inline LogLevel parse_log_level(const char *value, unsigned line) { if (0 == strcmp(value, "default")) - return G_LOG_LEVEL_MESSAGE; + return LogLevel::DEFAULT; if (0 == strcmp(value, "secure")) return LOG_LEVEL_SECURE; else if (0 == strcmp(value, "verbose")) - return G_LOG_LEVEL_DEBUG; + return LogLevel::DEBUG; else { FormatFatalError("unknown log level \"%s\" at line %u", value, line); - return G_LOG_LEVEL_MESSAGE; } } @@ -240,9 +105,7 @@ void log_early_init(bool verbose) { if (verbose) - log_threshold = G_LOG_LEVEL_DEBUG; - - log_init_stdout(); + SetLogThreshold(LogLevel::DEBUG); } bool @@ -250,16 +113,19 @@ log_init(bool verbose, bool use_stdout, Error &error) { const struct config_param *param; - g_get_charset(&log_charset); +#ifdef HAVE_GLIB + const char *charset; + g_get_charset(&charset); + SetLogCharset(charset); +#endif if (verbose) - log_threshold = G_LOG_LEVEL_DEBUG; + SetLogThreshold(LogLevel::DEBUG); else if ((param = config_get_param(CONF_LOG_LEVEL)) != nullptr) - log_threshold = parse_log_level(param->value.c_str(), - param->line); + SetLogThreshold(parse_log_level(param->value.c_str(), + param->line)); if (use_stdout) { - log_init_stdout(); return true; } else { param = config_get_param(CONF_LOG_FILE); @@ -267,7 +133,7 @@ log_init(bool verbose, bool use_stdout, Error &error) #ifdef HAVE_SYSLOG /* no configuration: default to syslog (if available) */ - log_init_syslog(); + LogInitSysLog(); return true; #else error.Set(log_domain, @@ -276,7 +142,7 @@ log_init(bool verbose, bool use_stdout, Error &error) #endif #ifdef HAVE_SYSLOG } else if (strcmp(param->value.c_str(), "syslog") == 0) { - log_init_syslog(); + LogInitSysLog(); return true; #endif } else { @@ -290,12 +156,8 @@ log_init(bool verbose, bool use_stdout, Error &error) static void close_log_files(void) { - if (stdout_mode) - return; - #ifdef HAVE_SYSLOG - if (out_path.IsNull()) - closelog(); + LogFinishSysLog(); #endif } @@ -309,32 +171,35 @@ log_deinit(void) void setup_log_output(bool use_stdout) { + if (use_stdout) + return; + fflush(nullptr); - if (!use_stdout) { -#ifndef WIN32 - if (out_path.IsNull()) - out_fd = open("/dev/null", O_WRONLY); + + if (out_fd < 0) { +#ifdef WIN32 + return; +#else + out_fd = open("/dev/null", O_WRONLY); + if (out_fd < 0) + return; #endif + } - if (out_fd >= 0) { - redirect_logs(out_fd); - close(out_fd); - } + redirect_logs(out_fd); + close(out_fd); + out_fd = -1; - stdout_mode = false; - log_charset = nullptr; - } + SetLogCharset(nullptr); } int cycle_log_files(void) { int fd; - if (stdout_mode || out_path.IsNull()) + if (out_path.IsNull()) return 0; - assert(!out_path.IsNull()); - FormatDebug(log_domain, "Cycling log files"); close_log_files(); diff --git a/src/LogLevel.hxx b/src/LogLevel.hxx new file mode 100644 index 000000000..dd24cc384 --- /dev/null +++ b/src/LogLevel.hxx @@ -0,0 +1,59 @@ +/* + * 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_LOG_LEVEL_HXX +#define MPD_LOG_LEVEL_HXX + +#ifdef WIN32 +#include <windows.h> +/* damn you, windows.h! */ +#ifdef ERROR +#undef ERROR +#endif +#endif + +enum class LogLevel { + /** + * Debug message for developers. + */ + DEBUG, + + /** + * Unimportant informational message. + */ + INFO, + + /** + * Interesting informational message. + */ + DEFAULT, + + /** + * Warning: something may be wrong. + */ + WARNING, + + /** + * An error has occurred, an operation could not finish + * successfully. + */ + ERROR, +}; + +#endif diff --git a/src/LogV.hxx b/src/LogV.hxx index 4bd4f801d..9f3687ba0 100644 --- a/src/LogV.hxx +++ b/src/LogV.hxx @@ -20,7 +20,7 @@ #ifndef MPD_LOGV_HXX #define MPD_LOGV_HXX -#include "Log.hxx" +#include "Log.hxx" // IWYU pragma: export #include <stdarg.h> diff --git a/src/Main.cxx b/src/Main.cxx index b45e2c3ae..da8458340 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -50,11 +50,12 @@ #include "IOThread.hxx" #include "fs/AllocatedPath.hxx" #include "fs/Config.hxx" +#include "fs/StandardDirectory.hxx" #include "PlaylistRegistry.hxx" #include "ZeroconfGlue.hxx" #include "DecoderList.hxx" #include "AudioConfig.hxx" -#include "pcm/PcmResample.hxx" +#include "pcm/PcmConvert.hxx" #include "Daemon.hxx" #include "system/FatalError.hxx" #include "util/Error.hxx" @@ -78,12 +79,11 @@ #include "ArchiveList.hxx" #endif +#ifdef HAVE_GLIB #include <glib.h> +#endif -#include <unistd.h> #include <stdlib.h> -#include <errno.h> -#include <string.h> #ifdef HAVE_LOCALE_H #include <locale.h> @@ -94,6 +94,8 @@ #include <ws2tcpip.h> #endif +#include <limits.h> + static constexpr unsigned DEFAULT_BUFFER_SIZE = 4096; static constexpr unsigned DEFAULT_BUFFER_BEFORE_PLAY = 10; @@ -135,13 +137,9 @@ glue_mapper_init(Error &error) return false; if (music_dir.IsNull()) { - const char *path = - g_get_user_special_dir(G_USER_DIRECTORY_MUSIC); - if (path != nullptr) { - music_dir = AllocatedPath::FromUTF8(path, error); - if (music_dir.IsNull()) - return false; - } + music_dir = GetUserMusicDir(); + if (music_dir.IsNull()) + return false; } mapper_init(std::move(music_dir), std::move(playlist_dir)); @@ -365,15 +363,17 @@ int mpd_main(int argc, char *argv[]) setlocale(LC_CTYPE,""); #endif +#ifdef HAVE_GLIB g_set_application_name("Music Player Daemon"); #if !GLIB_CHECK_VERSION(2,32,0) /* enable GLib's thread safety code */ g_thread_init(nullptr); #endif +#endif - io_thread_init(); winsock_init(); + io_thread_init(); config_global_init(); success = parse_cmdline(argc, argv, &options, error); @@ -431,7 +431,7 @@ int mpd_main(int argc, char *argv[]) archive_plugin_init_all(); #endif - if (!pcm_resample_global_init(error)) { + if (!pcm_convert_global_init(error)) { LogError(error); return EXIT_FAILURE; } @@ -479,7 +479,7 @@ int mpd_main(int argc, char *argv[]) } if (!glue_state_file_init(error)) { - g_printerr("%s\n", error.GetMessage()); + LogError(error); return EXIT_FAILURE; } @@ -544,7 +544,6 @@ int mpd_main(int argc, char *argv[]) playlist_list_global_finish(); input_stream_global_finish(); audio_output_all_finish(); - volume_finish(); mapper_finish(); delete instance->partition; command_finish(); @@ -554,7 +553,6 @@ int mpd_main(int argc, char *argv[]) archive_plugin_deinit_all(); #endif config_global_finish(); - stats_global_finish(); io_thread_deinit(); SignalHandlersFinish(); delete instance; diff --git a/src/Mapper.cxx b/src/Mapper.cxx index cbe45daa0..fbe1e8c34 100644 --- a/src/Mapper.cxx +++ b/src/Mapper.cxx @@ -36,9 +36,7 @@ #include <assert.h> #include <string.h> #include <sys/stat.h> -#include <unistd.h> #include <errno.h> -#include <dirent.h> static constexpr Domain mapper_domain("mapper"); @@ -150,7 +148,7 @@ map_to_relative_path(const char *path_utf8) return !music_dir_utf8.empty() && memcmp(path_utf8, music_dir_utf8.c_str(), music_dir_utf8_length) == 0 && - PathTraits::IsSeparatorUTF8(path_utf8[music_dir_utf8_length]) + PathTraitsUTF8::IsSeparator(path_utf8[music_dir_utf8_length]) ? path_utf8 + music_dir_utf8_length + 1 : path_utf8; } @@ -234,7 +232,7 @@ map_song_fs(const Song &song) std::string map_fs_to_utf8(const char *path_fs) { - if (PathTraits::IsSeparatorFS(path_fs[0])) { + if (PathTraitsFS::IsSeparator(path_fs[0])) { path_fs = music_dir_fs.RelativeFS(path_fs); if (path_fs == nullptr || *path_fs == 0) return std::string(); diff --git a/src/Mapper.hxx b/src/Mapper.hxx index 947fd2822..c757c421e 100644 --- a/src/Mapper.hxx +++ b/src/Mapper.hxx @@ -30,7 +30,6 @@ #define PLAYLIST_FILE_SUFFIX ".m3u" -class Path; class AllocatedPath; struct Directory; struct Song; @@ -138,8 +137,7 @@ map_spl_path(void); /** * Maps a playlist name (without the ".m3u" suffix) to a file system - * path. The return value is allocated on the heap and must be freed - * with g_free(). + * path. * * @return the path in file system encoding, or nullptr if mapping failed */ diff --git a/src/MixerAll.cxx b/src/MixerAll.cxx index 37225fd25..2eb54c232 100644 --- a/src/MixerAll.cxx +++ b/src/MixerAll.cxx @@ -23,7 +23,7 @@ #include "MixerInternal.hxx" #include "MixerList.hxx" #include "OutputAll.hxx" -#include "pcm/PcmVolume.hxx" +#include "pcm/Volume.hxx" #include "OutputInternal.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" diff --git a/src/MixerControl.cxx b/src/MixerControl.cxx index dd4f03543..08973a698 100644 --- a/src/MixerControl.cxx +++ b/src/MixerControl.cxx @@ -23,7 +23,6 @@ #include "util/Error.hxx" #include <assert.h> -#include <stddef.h> Mixer * mixer_new(const struct mixer_plugin *plugin, void *ao, diff --git a/src/MixerControl.hxx b/src/MixerControl.hxx index c1ee01eec..e7e825e7c 100644 --- a/src/MixerControl.hxx +++ b/src/MixerControl.hxx @@ -31,7 +31,7 @@ struct mixer_plugin; struct config_param; Mixer * -mixer_new(const struct mixer_plugin *plugin, void *ao, +mixer_new(const mixer_plugin *plugin, void *ao, const config_param ¶m, Error &error); diff --git a/src/OutputAPI.hxx b/src/OutputAPI.hxx index 73cbaf183..437141c5c 100644 --- a/src/OutputAPI.hxx +++ b/src/OutputAPI.hxx @@ -20,10 +20,14 @@ #ifndef MPD_OUTPUT_API_HXX #define MPD_OUTPUT_API_HxX +// IWYU pragma: begin_exports + #include "OutputPlugin.hxx" #include "OutputInternal.hxx" #include "AudioFormat.hxx" #include "tag/Tag.hxx" #include "ConfigData.hxx" +// IWYU pragma: end_exports + #endif diff --git a/src/OutputAll.cxx b/src/OutputAll.cxx index 36d41184a..8cbfb6e23 100644 --- a/src/OutputAll.cxx +++ b/src/OutputAll.cxx @@ -33,8 +33,6 @@ #include "ConfigOption.hxx" #include "notify.hxx" -#include <glib.h> - #include <assert.h> #include <string.h> @@ -110,7 +108,7 @@ audio_output_all_init(PlayerControl &pc) Error error; num_audio_outputs = audio_output_config_count(); - audio_outputs = g_new(struct audio_output *, num_audio_outputs); + audio_outputs = new audio_output *[num_audio_outputs]; const config_param empty; @@ -160,7 +158,7 @@ audio_output_all_finish(void) audio_output_finish(audio_outputs[i]); } - g_free(audio_outputs); + delete[] audio_outputs; audio_outputs = nullptr; num_audio_outputs = 0; } @@ -225,10 +223,7 @@ audio_output_reset_reopen(struct audio_output *ao) { const ScopeLock protect(ao->mutex); - if (!ao->open && ao->fail_timer != nullptr) { - g_timer_destroy(ao->fail_timer); - ao->fail_timer = nullptr; - } + ao->fail_timer.Reset(); } /** diff --git a/src/OutputAll.hxx b/src/OutputAll.hxx index 98061c345..2d9b1477f 100644 --- a/src/OutputAll.hxx +++ b/src/OutputAll.hxx @@ -115,7 +115,7 @@ audio_output_all_set_replay_gain_mode(ReplayGainMode mode); * (all closed then) */ bool -audio_output_all_play(struct music_chunk *chunk, Error &error); +audio_output_all_play(music_chunk *chunk, Error &error); /** * Checks if the output devices have drained their music pipe, and diff --git a/src/OutputCommand.cxx b/src/OutputCommand.cxx index 10b5bb322..efc8081e0 100644 --- a/src/OutputCommand.cxx +++ b/src/OutputCommand.cxx @@ -28,7 +28,6 @@ #include "OutputCommand.hxx" #include "OutputAll.hxx" #include "OutputInternal.hxx" -#include "OutputPlugin.hxx" #include "PlayerControl.hxx" #include "MixerControl.hxx" #include "Idle.hxx" diff --git a/src/OutputControl.cxx b/src/OutputControl.cxx index 27f280231..bcfd6a8c1 100644 --- a/src/OutputControl.cxx +++ b/src/OutputControl.cxx @@ -24,18 +24,13 @@ #include "OutputInternal.hxx" #include "OutputPlugin.hxx" #include "OutputError.hxx" -#include "MixerPlugin.hxx" #include "MixerControl.hxx" #include "notify.hxx" #include "filter/ReplayGainFilterPlugin.hxx" -#include "FilterPlugin.hxx" #include "util/Error.hxx" #include "Log.hxx" -#include <glib.h> - #include <assert.h> -#include <stdlib.h> /** after a failure, wait this number of seconds before automatically reopening the device */ @@ -154,10 +149,7 @@ audio_output_open(struct audio_output *ao, assert(ao->allow_play); assert(audio_format.IsValid()); - if (ao->fail_timer != nullptr) { - g_timer_destroy(ao->fail_timer); - ao->fail_timer = nullptr; - } + ao->fail_timer.Reset(); if (ao->open && audio_format == ao->in_audio_format) { assert(ao->pipe == &mp || @@ -215,14 +207,12 @@ audio_output_close_locked(struct audio_output *ao) if (ao->mixer != nullptr) mixer_auto_close(ao->mixer); - assert(!ao->open || ao->fail_timer == nullptr); + assert(!ao->open || !ao->fail_timer.IsDefined()); if (ao->open) ao_command(ao, AO_COMMAND_CLOSE); - else if (ao->fail_timer != nullptr) { - g_timer_destroy(ao->fail_timer); - ao->fail_timer = nullptr; - } + else + ao->fail_timer.Reset(); } bool @@ -233,8 +223,7 @@ audio_output_update(struct audio_output *ao, const ScopeLock protect(ao->mutex); if (ao->enabled && ao->really_enabled) { - if (ao->fail_timer == nullptr || - g_timer_elapsed(ao->fail_timer, nullptr) > REOPEN_AFTER) { + if (ao->fail_timer.Check(REOPEN_AFTER * 1000)) { return audio_output_open(ao, audio_format, mp); } } else if (audio_output_is_open(ao)) @@ -314,7 +303,7 @@ audio_output_release(struct audio_output *ao) void audio_output_close(struct audio_output *ao) { assert(ao != nullptr); - assert(!ao->open || ao->fail_timer == nullptr); + assert(!ao->open || !ao->fail_timer.IsDefined()); const ScopeLock protect(ao->mutex); audio_output_close_locked(ao); @@ -324,7 +313,7 @@ void audio_output_finish(struct audio_output *ao) { audio_output_close(ao); - assert(ao->fail_timer == nullptr); + assert(!ao->fail_timer.IsDefined()); if (ao->thread.IsDefined()) { assert(ao->allow_play); diff --git a/src/OutputControl.hxx b/src/OutputControl.hxx index d8f0b432d..c63383293 100644 --- a/src/OutputControl.hxx +++ b/src/OutputControl.hxx @@ -30,20 +30,20 @@ struct config_param; class MusicPipe; void -audio_output_set_replay_gain_mode(struct audio_output *ao, +audio_output_set_replay_gain_mode(audio_output *ao, ReplayGainMode mode); /** * Enables the device. */ void -audio_output_enable(struct audio_output *ao); +audio_output_enable(audio_output *ao); /** * Disables the device. */ void -audio_output_disable(struct audio_output *ao); +audio_output_disable(audio_output *ao); /** * Opens or closes the device, depending on the "enabled" flag. @@ -51,40 +51,44 @@ audio_output_disable(struct audio_output *ao); * @return true if the device is open */ bool -audio_output_update(struct audio_output *ao, +audio_output_update(audio_output *ao, AudioFormat audio_format, const MusicPipe &mp); void -audio_output_play(struct audio_output *ao); +audio_output_play(audio_output *ao); -void audio_output_pause(struct audio_output *ao); +void +audio_output_pause(audio_output *ao); void -audio_output_drain_async(struct audio_output *ao); +audio_output_drain_async(audio_output *ao); /** * Clear the "allow_play" flag and send the "CANCEL" command * asynchronously. To finish the operation, the caller has to call * audio_output_allow_play(). */ -void audio_output_cancel(struct audio_output *ao); +void +audio_output_cancel(audio_output *ao); /** * Set the "allow_play" and signal the thread. */ void -audio_output_allow_play(struct audio_output *ao); +audio_output_allow_play(audio_output *ao); -void audio_output_close(struct audio_output *ao); +void +audio_output_close(audio_output *ao); /** * Closes the audio output, but if the "always_on" flag is set, put it * into pause mode instead. */ void -audio_output_release(struct audio_output *ao); +audio_output_release(audio_output *ao); -void audio_output_finish(struct audio_output *ao); +void +audio_output_finish(audio_output *ao); #endif diff --git a/src/OutputFinish.cxx b/src/OutputFinish.cxx index db6599b53..b19f4c402 100644 --- a/src/OutputFinish.cxx +++ b/src/OutputFinish.cxx @@ -29,7 +29,7 @@ void ao_base_finish(struct audio_output *ao) { assert(!ao->open); - assert(ao->fail_timer == nullptr); + assert(!ao->fail_timer.IsDefined()); assert(!ao->thread.IsDefined()); if (ao->mixer != nullptr) @@ -44,7 +44,7 @@ void audio_output_free(struct audio_output *ao) { assert(!ao->open); - assert(ao->fail_timer == nullptr); + assert(!ao->fail_timer.IsDefined()); assert(!ao->thread.IsDefined()); ao_plugin_finish(ao); diff --git a/src/OutputInit.cxx b/src/OutputInit.cxx index 28eba1ab2..c9e03e0f2 100644 --- a/src/OutputInit.cxx +++ b/src/OutputInit.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "OutputInternal.hxx" -#include "OutputControl.hxx" #include "OutputList.hxx" #include "OutputError.hxx" #include "OutputAPI.hxx" @@ -173,7 +172,6 @@ ao_base_init(struct audio_output *ao, ao->allow_play = true; ao->in_playback_loop = false; ao->woken_for_play = false; - ao->fail_timer = nullptr; /* set up the filter chain */ diff --git a/src/OutputInternal.hxx b/src/OutputInternal.hxx index c07cdf856..ab1c1242e 100644 --- a/src/OutputInternal.hxx +++ b/src/OutputInternal.hxx @@ -22,18 +22,17 @@ #include "AudioFormat.hxx" #include "pcm/PcmBuffer.hxx" +#include "pcm/PcmDither.hxx" #include "thread/Mutex.hxx" #include "thread/Cond.hxx" #include "thread/Thread.hxx" - -#include <time.h> +#include "system/PeriodClock.hxx" class Error; class Filter; class MusicPipe; struct config_param; struct PlayerControl; -typedef struct _GTimer GTimer; enum audio_output_command { AO_COMMAND_NONE = 0, @@ -147,7 +146,7 @@ struct audio_output { * to estimate how long it should stay disabled (unless * explicitly reopened with "play"). */ - GTimer *fail_timer; + PeriodClock fail_timer; /** * The configured audio format. @@ -174,6 +173,11 @@ struct audio_output { PcmBuffer cross_fade_buffer; /** + * The dithering state for cross-fading two streams. + */ + PcmDither cross_fade_dither; + + /** * The filter object of this audio output. This is an * instance of chain_filter_plugin. */ diff --git a/src/OutputState.cxx b/src/OutputState.cxx index a3650413c..0124e1e33 100644 --- a/src/OutputState.cxx +++ b/src/OutputState.cxx @@ -28,12 +28,10 @@ #include "OutputInternal.hxx" #include "OutputError.hxx" #include "Log.hxx" - -#include <glib.h> +#include "util/StringUtil.hxx" #include <assert.h> #include <stdlib.h> -#include <string.h> #define AUDIO_DEVICE_STATE "audio_device_state:" @@ -62,7 +60,7 @@ audio_output_state_read(const char *line) const char *name; struct audio_output *ao; - if (!g_str_has_prefix(line, AUDIO_DEVICE_STATE)) + if (!StringStartsWith(line, AUDIO_DEVICE_STATE)) return false; line += sizeof(AUDIO_DEVICE_STATE) - 1; diff --git a/src/OutputThread.cxx b/src/OutputThread.cxx index 30d3ba30f..62833e360 100644 --- a/src/OutputThread.cxx +++ b/src/OutputThread.cxx @@ -35,8 +35,6 @@ #include "Log.hxx" #include "Compiler.h" -#include <glib.h> - #include <assert.h> #include <string.h> @@ -98,10 +96,16 @@ ao_filter_open(struct audio_output *ao, AudioFormat &format, assert(format.IsValid()); /* the replay_gain filter cannot fail here */ - if (ao->replay_gain_filter != nullptr) - ao->replay_gain_filter->Open(format, error_r); - if (ao->other_replay_gain_filter != nullptr) - ao->other_replay_gain_filter->Open(format, error_r); + if (ao->replay_gain_filter != nullptr && + !ao->replay_gain_filter->Open(format, error_r).IsDefined()) + return AudioFormat::Undefined(); + + if (ao->other_replay_gain_filter != nullptr && + !ao->other_replay_gain_filter->Open(format, error_r).IsDefined()) { + if (ao->replay_gain_filter != nullptr) + ao->replay_gain_filter->Close(); + return AudioFormat::Undefined(); + } const AudioFormat af = ao->filter->Open(format, error_r); if (!af.IsDefined()) { @@ -137,14 +141,7 @@ ao_open(struct audio_output *ao) assert(ao->chunk == nullptr); assert(ao->in_audio_format.IsValid()); - if (ao->fail_timer != nullptr) { - /* this can only happen when this - output thread fails while - audio_output_open() is run in the - player thread */ - g_timer_destroy(ao->fail_timer); - ao->fail_timer = nullptr; - } + ao->fail_timer.Reset(); /* enable the device (just in case the last enable has failed) */ @@ -160,7 +157,7 @@ ao_open(struct audio_output *ao) FormatError(error, "Failed to open filter for \"%s\" [%s]", ao->name, ao->plugin->name); - ao->fail_timer = g_timer_new(); + ao->fail_timer.Update(); return; } @@ -180,11 +177,19 @@ ao_open(struct audio_output *ao) ao->name, ao->plugin->name); ao_filter_close(ao); - ao->fail_timer = g_timer_new(); + ao->fail_timer.Update(); return; } - convert_filter_set(ao->convert_filter, ao->out_audio_format); + if (!convert_filter_set(ao->convert_filter, ao->out_audio_format, + error)) { + FormatError(error, "Failed to convert for \"%s\" [%s]", + ao->name, ao->plugin->name); + + ao_filter_close(ao); + ao->fail_timer.Update(); + return; + } ao->open = true; @@ -233,7 +238,9 @@ ao_reopen_filter(struct audio_output *ao) ao_filter_close(ao); const AudioFormat filter_audio_format = ao_filter_open(ao, ao->in_audio_format, error); - if (!filter_audio_format.IsDefined()) { + if (!filter_audio_format.IsDefined() || + !convert_filter_set(ao->convert_filter, ao->out_audio_format, + error)) { FormatError(error, "Failed to open filter for \"%s\" [%s]", ao->name, ao->plugin->name); @@ -246,7 +253,7 @@ ao_reopen_filter(struct audio_output *ao) ao->chunk = nullptr; ao->open = false; - ao->fail_timer = g_timer_new(); + ao->fail_timer.Update(); ao->mutex.unlock(); ao_plugin_close(ao); @@ -254,8 +261,6 @@ ao_reopen_filter(struct audio_output *ao) return; } - - convert_filter_set(ao->convert_filter, ao->out_audio_format); } static void @@ -387,7 +392,7 @@ ao_filter_chunk(struct audio_output *ao, const struct music_chunk *chunk, void *dest = ao->cross_fade_buffer.Get(other_length); memcpy(dest, other_data, other_length); - if (!pcm_mix(dest, data, length, + if (!pcm_mix(ao->cross_fade_dither, dest, data, length, ao->in_audio_format.format, 1.0 - chunk->mix_ratio)) { FormatError(output_domain, @@ -437,7 +442,7 @@ ao_play_chunk(struct audio_output *ao, const struct music_chunk *chunk) /* don't automatically reopen this device for 10 seconds */ - ao->fail_timer = g_timer_new(); + ao->fail_timer.Update(); return false; } @@ -461,8 +466,8 @@ ao_play_chunk(struct audio_output *ao, const struct music_chunk *chunk) /* don't automatically reopen this device for 10 seconds */ - assert(ao->fail_timer == nullptr); - ao->fail_timer = g_timer_new(); + assert(!ao->fail_timer.IsDefined()); + ao->fail_timer.Update(); return false; } diff --git a/src/OutputThread.hxx b/src/OutputThread.hxx index 1a7932162..980d203fe 100644 --- a/src/OutputThread.hxx +++ b/src/OutputThread.hxx @@ -22,6 +22,7 @@ struct audio_output; -void audio_output_thread_start(struct audio_output *ao); +void +audio_output_thread_start(audio_output *ao); #endif diff --git a/src/Page.hxx b/src/Page.hxx index 27c6092cc..4e1302ba5 100644 --- a/src/Page.hxx +++ b/src/Page.hxx @@ -27,8 +27,6 @@ #include "util/RefCount.hxx" -#include <algorithm> - #include <stddef.h> /** diff --git a/src/PlayerControl.cxx b/src/PlayerControl.cxx index f180874b0..74b4673bc 100644 --- a/src/PlayerControl.cxx +++ b/src/PlayerControl.cxx @@ -21,9 +21,8 @@ #include "PlayerControl.hxx" #include "Idle.hxx" #include "Song.hxx" -#include "DecoderControl.hxx" -#include <cmath> +#include <algorithm> #include <assert.h> diff --git a/src/PlayerThread.cxx b/src/PlayerThread.cxx index 356559e37..aa42eed49 100644 --- a/src/PlayerThread.cxx +++ b/src/PlayerThread.cxx @@ -25,7 +25,6 @@ #include "MusicBuffer.hxx" #include "MusicChunk.hxx" #include "Song.hxx" -#include "Main.hxx" #include "system/FatalError.hxx" #include "CrossFade.hxx" #include "PlayerControl.hxx" diff --git a/src/Playlist.cxx b/src/Playlist.cxx index 8d9ab24a3..f566527ac 100644 --- a/src/Playlist.cxx +++ b/src/Playlist.cxx @@ -22,7 +22,6 @@ #include "PlaylistError.hxx" #include "PlayerControl.hxx" #include "Song.hxx" -#include "tag/Tag.hxx" #include "Idle.hxx" #include "Log.hxx" diff --git a/src/Playlist.hxx b/src/Playlist.hxx index 5875ff4d8..2116abe7c 100644 --- a/src/Playlist.hxx +++ b/src/Playlist.hxx @@ -23,8 +23,10 @@ #include "Queue.hxx" #include "PlaylistError.hxx" +enum TagType : uint8_t; struct PlayerControl; struct Song; +class Error; struct playlist { /** @@ -205,6 +207,10 @@ public: PlaylistResult SetPriorityId(PlayerControl &pc, unsigned song_id, uint8_t priority); + bool AddSongIdTag(unsigned id, TagType tag_type, const char *value, + Error &error); + bool ClearSongIdTag(unsigned id, TagType tag_type, Error &error); + void Stop(PlayerControl &pc); PlaylistResult PlayPosition(PlayerControl &pc, int position); diff --git a/src/PlaylistAny.cxx b/src/PlaylistAny.cxx index 52304700f..dfe3bfdff 100644 --- a/src/PlaylistAny.cxx +++ b/src/PlaylistAny.cxx @@ -21,7 +21,6 @@ #include "PlaylistAny.hxx" #include "PlaylistMapper.hxx" #include "PlaylistRegistry.hxx" -#include "PlaylistError.hxx" #include "util/UriUtil.hxx" #include "util/Error.hxx" #include "InputStream.hxx" diff --git a/src/PlaylistDatabase.cxx b/src/PlaylistDatabase.cxx index a6d15e755..e6cdb922b 100644 --- a/src/PlaylistDatabase.cxx +++ b/src/PlaylistDatabase.cxx @@ -20,7 +20,7 @@ #include "config.h" #include "PlaylistDatabase.hxx" #include "PlaylistVector.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "util/StringUtil.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index e7dae6258..3f9845166 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -26,7 +26,7 @@ #include "DatabaseGlue.hxx" #include "Song.hxx" #include "Mapper.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "ConfigGlobal.hxx" #include "ConfigOption.hxx" #include "ConfigDefaults.hxx" @@ -43,10 +43,7 @@ #include <glib.h> #include <assert.h> -#include <sys/types.h> #include <sys/stat.h> -#include <unistd.h> -#include <dirent.h> #include <string.h> #include <errno.h> @@ -159,10 +156,9 @@ LoadPlaylistFileInfo(PlaylistInfo &info, if (!StatFile(path_fs, st) || !S_ISREG(st.st_mode)) return false; - char *name = g_strndup(name_fs_str, - name_length + 1 - sizeof(PLAYLIST_FILE_SUFFIX)); - std::string name_utf8 = PathToUTF8(name); - g_free(name); + std::string name(name_fs_str, + name_length + 1 - sizeof(PLAYLIST_FILE_SUFFIX)); + std::string name_utf8 = PathToUTF8(name.c_str()); if (name_utf8.empty()) return false; @@ -250,7 +246,7 @@ LoadPlaylistFile(const char *utf8path, Error &error) if (!uri_has_scheme(s)) { uri_utf8 = map_fs_to_utf8(s); if (uri_utf8.empty()) { - if (PathTraits::IsAbsoluteFS(s)) { + if (PathTraitsFS::IsAbsolute(s)) { uri_utf8 = PathToUTF8(s); if (uri_utf8.empty()) continue; diff --git a/src/PlaylistFile.hxx b/src/PlaylistFile.hxx index f04530bcc..92030b1eb 100644 --- a/src/PlaylistFile.hxx +++ b/src/PlaylistFile.hxx @@ -24,7 +24,6 @@ #include <string> struct Song; -struct PlaylistInfo; class PlaylistVector; class Error; diff --git a/src/PlaylistGlobal.cxx b/src/PlaylistGlobal.cxx index 97902275b..0e8493687 100644 --- a/src/PlaylistGlobal.cxx +++ b/src/PlaylistGlobal.cxx @@ -24,7 +24,6 @@ #include "config.h" #include "PlaylistGlobal.hxx" -#include "Playlist.hxx" #include "Main.hxx" #include "Instance.hxx" #include "GlobalEvents.hxx" diff --git a/src/PlaylistPrint.cxx b/src/PlaylistPrint.cxx index 8e3beaa47..e92d6e18a 100644 --- a/src/PlaylistPrint.cxx +++ b/src/PlaylistPrint.cxx @@ -23,8 +23,6 @@ #include "PlaylistAny.hxx" #include "PlaylistSong.hxx" #include "Playlist.hxx" -#include "PlaylistRegistry.hxx" -#include "PlaylistPlugin.hxx" #include "QueuePrint.hxx" #include "SongEnumerator.hxx" #include "SongPrint.hxx" @@ -150,7 +148,7 @@ playlist_provider_print(Client &client, const char *uri, SongEnumerator &e, bool detail) { const std::string base_uri = uri != nullptr - ? PathTraits::GetParentUTF8(uri) + ? PathTraitsUTF8::GetParent(uri) : std::string("."); Song *song; diff --git a/src/PlaylistQueue.cxx b/src/PlaylistQueue.cxx index a8359d427..3a7660134 100644 --- a/src/PlaylistQueue.cxx +++ b/src/PlaylistQueue.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "PlaylistQueue.hxx" -#include "PlaylistPlugin.hxx" #include "PlaylistAny.hxx" #include "PlaylistSong.hxx" #include "Playlist.hxx" @@ -36,7 +35,7 @@ playlist_load_into_queue(const char *uri, SongEnumerator &e, bool secure) { const std::string base_uri = uri != nullptr - ? PathTraits::GetParentUTF8(uri) + ? PathTraitsUTF8::GetParent(uri) : std::string("."); Song *song; diff --git a/src/PlaylistRegistry.cxx b/src/PlaylistRegistry.cxx index 9afbe349d..65f55e6ff 100644 --- a/src/PlaylistRegistry.cxx +++ b/src/PlaylistRegistry.cxx @@ -40,8 +40,6 @@ #include "system/FatalError.hxx" #include "Log.hxx" -#include <glib.h> - #include <assert.h> #include <string.h> @@ -130,13 +128,12 @@ static SongEnumerator * playlist_list_open_uri_scheme(const char *uri, Mutex &mutex, Cond &cond, bool *tried) { - char *scheme; SongEnumerator *playlist = nullptr; assert(uri != nullptr); - scheme = g_uri_parse_scheme(uri); - if (scheme == nullptr) + const auto scheme = uri_get_scheme(uri); + if (scheme.empty()) return nullptr; for (unsigned i = 0; playlist_plugins[i] != nullptr; ++i) { @@ -146,7 +143,7 @@ playlist_list_open_uri_scheme(const char *uri, Mutex &mutex, Cond &cond, if (playlist_plugins_enabled[i] && plugin->open_uri != nullptr && plugin->schemes != nullptr && - string_array_contains(plugin->schemes, scheme)) { + string_array_contains(plugin->schemes, scheme.c_str())) { playlist = playlist_plugin_open_uri(plugin, uri, mutex, cond); if (playlist != nullptr) @@ -156,7 +153,6 @@ playlist_list_open_uri_scheme(const char *uri, Mutex &mutex, Cond &cond, } } - g_free(scheme); return playlist; } diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx index 481d9bf75..1006fdcc7 100644 --- a/src/PlaylistSave.cxx +++ b/src/PlaylistSave.cxx @@ -56,7 +56,7 @@ void playlist_print_uri(FILE *file, const char *uri) { auto path = playlist_saveAbsolutePaths && !uri_has_scheme(uri) && - !PathTraits::IsAbsoluteUTF8(uri) + !PathTraitsUTF8::IsAbsolute(uri) ? map_uri_fs(uri) : AllocatedPath::FromUTF8(uri); diff --git a/src/PlaylistSong.cxx b/src/PlaylistSong.cxx index 60774dc36..f016e46b6 100644 --- a/src/PlaylistSong.cxx +++ b/src/PlaylistSong.cxx @@ -30,8 +30,6 @@ #include "util/Error.hxx" #include "Song.hxx" -#include <glib.h> - #include <assert.h> #include <string.h> @@ -99,7 +97,7 @@ playlist_check_load_song(const Song *song, const char *uri, bool secure) if (uri_has_scheme(uri)) { dest = Song::NewRemote(uri); - } else if (PathTraits::IsAbsoluteUTF8(uri) && secure) { + } else if (PathTraitsUTF8::IsAbsolute(uri) && secure) { dest = Song::LoadFile(uri, nullptr); if (dest == nullptr) return nullptr; @@ -142,13 +140,13 @@ playlist_check_translate_song(Song *song, const char *base_uri, } if (base_uri != nullptr && strcmp(base_uri, ".") == 0) - /* PathTraits::GetParentUTF8() returns "." when there + /* PathTraitsUTF8::GetParent() returns "." when there is no directory name in the given path; clear that now, because it would break the database lookup functions */ base_uri = nullptr; - if (PathTraits::IsAbsoluteUTF8(uri)) { + if (PathTraitsUTF8::IsAbsolute(uri)) { /* XXX fs_charset vs utf8? */ const char *suffix = map_to_relative_path(uri); assert(suffix != nullptr); @@ -165,12 +163,14 @@ playlist_check_translate_song(Song *song, const char *base_uri, base_uri = nullptr; } - char *allocated = nullptr; - if (base_uri != nullptr) - uri = allocated = g_build_filename(base_uri, uri, nullptr); + std::string full_uri; + if (base_uri != nullptr) { + full_uri = PathTraitsUTF8::Build(base_uri, uri); + uri = full_uri.c_str(); + } Song *dest = playlist_check_load_song(song, uri, secure); song->Free(); - g_free(allocated); + return dest; } diff --git a/src/PlaylistState.cxx b/src/PlaylistState.cxx index ac2deebbf..bb776dce7 100644 --- a/src/PlaylistState.cxx +++ b/src/PlaylistState.cxx @@ -27,16 +27,15 @@ #include "PlaylistError.hxx" #include "Playlist.hxx" #include "QueueSave.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "PlayerControl.hxx" #include "ConfigGlobal.hxx" #include "ConfigOption.hxx" #include "fs/Limits.hxx" #include "util/CharUtil.hxx" +#include "util/StringUtil.hxx" #include "Log.hxx" -#include <glib.h> - #include <string.h> #include <stdlib.h> @@ -112,7 +111,7 @@ playlist_state_load(TextFile &file, struct playlist &playlist) return; } - while (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_PLAYLIST_END)) { + while (!StringStartsWith(line, PLAYLIST_STATE_FILE_PLAYLIST_END)) { queue_load_song(file, line, playlist.queue); line = file.ReadLine(); @@ -135,7 +134,7 @@ playlist_state_restore(const char *line, TextFile &file, int seek_time = 0; bool random_mode = false; - if (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_STATE)) + if (!StringStartsWith(line, PLAYLIST_STATE_FILE_STATE)) return false; line += sizeof(PLAYLIST_STATE_FILE_STATE) - 1; @@ -149,40 +148,40 @@ playlist_state_restore(const char *line, TextFile &file, state = PlayerState::STOP; while ((line = file.ReadLine()) != nullptr) { - if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_TIME)) { + if (StringStartsWith(line, PLAYLIST_STATE_FILE_TIME)) { seek_time = atoi(&(line[strlen(PLAYLIST_STATE_FILE_TIME)])); - } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_REPEAT)) { + } else if (StringStartsWith(line, PLAYLIST_STATE_FILE_REPEAT)) { playlist.SetRepeat(pc, strcmp(&(line[strlen(PLAYLIST_STATE_FILE_REPEAT)]), "1") == 0); - } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_SINGLE)) { + } else if (StringStartsWith(line, PLAYLIST_STATE_FILE_SINGLE)) { playlist.SetSingle(pc, strcmp(&(line[strlen(PLAYLIST_STATE_FILE_SINGLE)]), "1") == 0); - } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CONSUME)) { + } else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CONSUME)) { playlist.SetConsume(strcmp(&(line[strlen(PLAYLIST_STATE_FILE_CONSUME)]), "1") == 0); - } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CROSSFADE)) { + } else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CROSSFADE)) { pc.SetCrossFade(atoi(line + strlen(PLAYLIST_STATE_FILE_CROSSFADE))); - } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_MIXRAMPDB)) { + } else if (StringStartsWith(line, PLAYLIST_STATE_FILE_MIXRAMPDB)) { pc.SetMixRampDb(atof(line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDB))); - } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) { + } else if (StringStartsWith(line, PLAYLIST_STATE_FILE_MIXRAMPDELAY)) { const char *p = line + strlen(PLAYLIST_STATE_FILE_MIXRAMPDELAY); /* this check discards "nan" which was used prior to MPD 0.18 */ if (IsDigitASCII(*p)) pc.SetMixRampDelay(atof(p)); - } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_RANDOM)) { + } else if (StringStartsWith(line, PLAYLIST_STATE_FILE_RANDOM)) { random_mode = strcmp(line + strlen(PLAYLIST_STATE_FILE_RANDOM), "1") == 0; - } else if (g_str_has_prefix(line, PLAYLIST_STATE_FILE_CURRENT)) { + } else if (StringStartsWith(line, PLAYLIST_STATE_FILE_CURRENT)) { current = atoi(&(line [strlen (PLAYLIST_STATE_FILE_CURRENT)])); - } else if (g_str_has_prefix(line, + } else if (StringStartsWith(line, PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)) { playlist_state_load(file, playlist); } diff --git a/src/PlaylistState.hxx b/src/PlaylistState.hxx index 01cc94d03..408113e49 100644 --- a/src/PlaylistState.hxx +++ b/src/PlaylistState.hxx @@ -32,12 +32,12 @@ struct PlayerControl; class TextFile; void -playlist_state_save(FILE *fp, const struct playlist &playlist, +playlist_state_save(FILE *fp, const playlist &playlist, PlayerControl &pc); bool playlist_state_restore(const char *line, TextFile &file, - struct playlist &playlist, PlayerControl &pc); + playlist &playlist, PlayerControl &pc); /** * Generates a hash number for the current state of the playlist and @@ -46,7 +46,7 @@ playlist_state_restore(const char *line, TextFile &file, * be saved. */ unsigned -playlist_state_get_hash(const struct playlist &playlist, +playlist_state_get_hash(const playlist &playlist, PlayerControl &c); #endif diff --git a/src/PlaylistTag.cxx b/src/PlaylistTag.cxx new file mode 100644 index 000000000..e158018f5 --- /dev/null +++ b/src/PlaylistTag.cxx @@ -0,0 +1,99 @@ +/* + * 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. + */ + +/* + * Functions for editing the playlist (adding, removing, reordering + * songs in the queue). + * + */ + +#include "config.h" +#include "Playlist.hxx" +#include "PlaylistError.hxx" +#include "Song.hxx" +#include "tag/Tag.hxx" +#include "tag/TagBuilder.hxx" +#include "util/Error.hxx" + +bool +playlist::AddSongIdTag(unsigned id, TagType tag_type, const char *value, + Error &error) +{ + const int position = queue.IdToPosition(id); + if (position < 0) { + error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_SONG), + "No such song"); + return false; + } + + Song &song = queue.Get(position); + if (song.IsFile()) { + error.Set(playlist_domain, int(PlaylistResult::DENIED), + "Cannot edit tags of local file"); + return false; + } + + TagBuilder tag; + if (song.tag != nullptr) { + tag = std::move(*song.tag); + delete song.tag; + } + + tag.AddItem(tag_type, value); + song.tag = tag.Commit(); + + queue.ModifyAtPosition(position); + OnModified(); + return true; +} + +bool +playlist::ClearSongIdTag(unsigned id, TagType tag_type, + Error &error) +{ + const int position = queue.IdToPosition(id); + if (position < 0) { + error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_SONG), + "No such song"); + return false; + } + + Song &song = queue.Get(position); + if (song.IsFile()) { + error.Set(playlist_domain, int(PlaylistResult::DENIED), + "Cannot edit tags of local file"); + return false; + } + + if (song.tag == nullptr) + return true; + + TagBuilder tag(std::move(*song.tag)); + delete song.tag; + + if (tag_type == TAG_NUM_OF_ITEM_TYPES) + tag.RemoveAll(); + else + tag.RemoveType(tag_type); + song.tag = tag.Commit(); + + queue.ModifyAtPosition(position); + OnModified(); + return true; +} diff --git a/src/PlaylistVector.cxx b/src/PlaylistVector.cxx index 5bb0cdf64..701edd896 100644 --- a/src/PlaylistVector.cxx +++ b/src/PlaylistVector.cxx @@ -24,7 +24,6 @@ #include <algorithm> #include <assert.h> -#include <string.h> PlaylistVector::iterator PlaylistVector::find(const char *name) diff --git a/src/Queue.cxx b/src/Queue.cxx index 451609438..92f92f77c 100644 --- a/src/Queue.cxx +++ b/src/Queue.cxx @@ -21,8 +21,6 @@ #include "Queue.hxx" #include "Song.hxx" -#include <stdlib.h> - queue::queue(unsigned _max_length) :max_length(_max_length), length(0), version(1), diff --git a/src/QueuePrint.cxx b/src/QueuePrint.cxx index d5651cde0..fec4a36fb 100644 --- a/src/QueuePrint.cxx +++ b/src/QueuePrint.cxx @@ -22,13 +22,8 @@ #include "Queue.hxx" #include "SongFilter.hxx" #include "SongPrint.hxx" -#include "Mapper.hxx" #include "Client.hxx" -extern "C" { -#include "Song.hxx" -} - /** * Send detailed information about a range of songs in the queue to a * client. diff --git a/src/QueueSave.cxx b/src/QueueSave.cxx index 6a1a51992..f3fc7c6d1 100644 --- a/src/QueueSave.cxx +++ b/src/QueueSave.cxx @@ -25,14 +25,13 @@ #include "SongSave.hxx" #include "DatabasePlugin.hxx" #include "DatabaseGlue.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" +#include "util/StringUtil.hxx" #include "util/UriUtil.hxx" #include "util/Error.hxx" #include "fs/Traits.hxx" #include "Log.hxx" -#include <glib.h> - #include <stdlib.h> #define PRIO_LABEL "Prio: " @@ -78,7 +77,7 @@ queue_load_song(TextFile &file, const char *line, queue &queue) return; uint8_t priority = 0; - if (g_str_has_prefix(line, PRIO_LABEL)) { + if (StringStartsWith(line, PRIO_LABEL)) { priority = strtoul(line + sizeof(PRIO_LABEL) - 1, nullptr, 10); line = file.ReadLine(); @@ -89,9 +88,9 @@ queue_load_song(TextFile &file, const char *line, queue &queue) const Database *db = nullptr; Song *song; - if (g_str_has_prefix(line, SONG_BEGIN)) { + if (StringStartsWith(line, SONG_BEGIN)) { const char *uri = line + sizeof(SONG_BEGIN) - 1; - if (!uri_has_scheme(uri) && !PathTraits::IsAbsoluteUTF8(uri)) + if (!uri_has_scheme(uri) && !PathTraitsUTF8::IsAbsolute(uri)) return; Error error; diff --git a/src/ReplayGainConfig.cxx b/src/ReplayGainConfig.cxx index 8f9b0d3f7..00ae5787f 100644 --- a/src/ReplayGainConfig.cxx +++ b/src/ReplayGainConfig.cxx @@ -22,12 +22,12 @@ #include "Idle.hxx" #include "ConfigData.hxx" #include "ConfigGlobal.hxx" -#include "Playlist.hxx" #include "system/FatalError.hxx" #include <assert.h> #include <stdlib.h> #include <string.h> +#include <math.h> ReplayGainMode replay_gain_mode = REPLAY_GAIN_OFF; diff --git a/src/Song.hxx b/src/Song.hxx index b74690e77..21e158560 100644 --- a/src/Song.hxx +++ b/src/Song.hxx @@ -26,7 +26,7 @@ #include <string> #include <assert.h> -#include <sys/time.h> +#include <time.h> #define SONG_FILE "file: " #define SONG_TIME "Time: " diff --git a/src/SongSave.cxx b/src/SongSave.cxx index 63e279a16..9682bf650 100644 --- a/src/SongSave.cxx +++ b/src/SongSave.cxx @@ -21,8 +21,7 @@ #include "SongSave.hxx" #include "Song.hxx" #include "TagSave.hxx" -#include "Directory.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "tag/Tag.hxx" #include "tag/TagBuilder.hxx" #include "util/StringUtil.hxx" diff --git a/src/SongSort.cxx b/src/SongSort.cxx index 4d422657a..1c2d01080 100644 --- a/src/SongSort.cxx +++ b/src/SongSort.cxx @@ -20,7 +20,6 @@ #include "config.h" #include "SongSort.hxx" #include "Song.hxx" -#include "util/list.h" #include "tag/Tag.hxx" extern "C" { @@ -29,7 +28,6 @@ extern "C" { #include <glib.h> -#include <assert.h> #include <stdlib.h> static const char * diff --git a/src/SongSort.hxx b/src/SongSort.hxx index b3b67b0c0..45ab3dae9 100644 --- a/src/SongSort.hxx +++ b/src/SongSort.hxx @@ -23,6 +23,6 @@ struct list_head; void -song_list_sort(struct list_head *songs); +song_list_sort(list_head *songs); #endif diff --git a/src/SongSticker.hxx b/src/SongSticker.hxx index 0923f0c3a..7a63a64b4 100644 --- a/src/SongSticker.hxx +++ b/src/SongSticker.hxx @@ -63,7 +63,7 @@ sticker_song_delete_value(const Song *song, const char *name); * @param song the song object * @return a sticker object, or NULL on error or if there is no sticker */ -struct sticker * +sticker * sticker_song_get(const Song *song); /** diff --git a/src/SongUpdate.cxx b/src/SongUpdate.cxx index 1a873fedc..6e5e37a2b 100644 --- a/src/SongUpdate.cxx +++ b/src/SongUpdate.cxx @@ -20,14 +20,11 @@ #include "config.h" /* must be first for large file support */ #include "Song.hxx" #include "util/UriUtil.hxx" -#include "util/Error.hxx" #include "Directory.hxx" #include "Mapper.hxx" #include "fs/AllocatedPath.hxx" #include "fs/Traits.hxx" #include "fs/FileSystem.hxx" -#include "InputStream.hxx" -#include "DecoderPlugin.hxx" #include "DecoderList.hxx" #include "tag/Tag.hxx" #include "tag/TagBuilder.hxx" @@ -35,13 +32,10 @@ #include "tag/TagId3.hxx" #include "tag/ApeTag.hxx" #include "TagFile.hxx" -#include "thread/Cond.hxx" #include <assert.h> #include <string.h> -#include <sys/types.h> #include <sys/stat.h> -#include <stdio.h> Song * Song::LoadFile(const char *path_utf8, Directory *parent) @@ -49,7 +43,7 @@ Song::LoadFile(const char *path_utf8, Directory *parent) Song *song; bool ret; - assert((parent == nullptr) == PathTraits::IsAbsoluteUTF8(path_utf8)); + assert((parent == nullptr) == PathTraitsUTF8::IsAbsolute(path_utf8)); assert(!uri_has_scheme(path_utf8)); assert(strchr(path_utf8, '\n') == nullptr); diff --git a/src/StateFile.cxx b/src/StateFile.cxx index 75cef2c99..7ca8b8946 100644 --- a/src/StateFile.cxx +++ b/src/StateFile.cxx @@ -21,10 +21,10 @@ #include "StateFile.hxx" #include "OutputState.hxx" #include "PlaylistState.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "Partition.hxx" #include "Volume.hxx" -#include "event/Loop.hxx" + #include "fs/FileSystem.hxx" #include "util/Domain.hxx" #include "Log.hxx" diff --git a/src/Stats.cxx b/src/Stats.cxx index f224bdf49..e22f1a995 100644 --- a/src/Stats.cxx +++ b/src/Stats.cxx @@ -26,21 +26,24 @@ #include "DatabasePlugin.hxx" #include "DatabaseSimple.hxx" #include "util/Error.hxx" +#include "system/Clock.hxx" #include "Log.hxx" -#include <glib.h> +#ifndef WIN32 +/** + * The monotonic time stamp when MPD was started. It is used to + * calculate the uptime. + */ +static unsigned start_time; +#endif -static GTimer *uptime; static DatabaseStats stats; void stats_global_init(void) { - uptime = g_timer_new(); -} - -void stats_global_finish(void) -{ - g_timer_destroy(uptime); +#ifndef WIN32 + start_time = MonotonicClockS(); +#endif } void stats_update(void) @@ -94,9 +97,13 @@ void stats_print(Client &client) { client_printf(client, - "uptime: %lu\n" + "uptime: %u\n" "playtime: %lu\n", - (unsigned long)g_timer_elapsed(uptime, NULL), +#ifdef WIN32 + GetProcessUptimeS(), +#else + MonotonicClockS() - start_time, +#endif (unsigned long)(client.player_control.GetTotalPlayTime() + 0.5)); if (GetDatabase() != nullptr) diff --git a/src/Stats.hxx b/src/Stats.hxx index dd131ce19..63b96c4d6 100644 --- a/src/Stats.hxx +++ b/src/Stats.hxx @@ -24,8 +24,6 @@ class Client; void stats_global_init(void); -void stats_global_finish(void); - void stats_update(void); void diff --git a/src/StickerDatabase.hxx b/src/StickerDatabase.hxx index 42522b7b4..75f0fc77b 100644 --- a/src/StickerDatabase.hxx +++ b/src/StickerDatabase.hxx @@ -106,7 +106,7 @@ sticker_delete_value(const char *type, const char *uri, const char *name); * @param sticker the sticker object to be freed */ void -sticker_free(struct sticker *sticker); +sticker_free(sticker *sticker); /** * Determines a single value in a sticker. @@ -117,7 +117,7 @@ sticker_free(struct sticker *sticker); */ gcc_pure const char * -sticker_get_value(const struct sticker &sticker, const char *name); +sticker_get_value(const sticker &sticker, const char *name); /** * Iterates over all sticker items in a sticker. @@ -127,7 +127,7 @@ sticker_get_value(const struct sticker &sticker, const char *name); * @param user_data an opaque pointer for the callback function */ void -sticker_foreach(const struct sticker &sticker, +sticker_foreach(const sticker &sticker, void (*func)(const char *name, const char *value, void *user_data), void *user_data); @@ -139,7 +139,7 @@ sticker_foreach(const struct sticker &sticker, * @param uri the URI of the resource, e.g. the song path * @return a sticker object, or nullptr on error or if there is no sticker */ -struct sticker * +sticker * sticker_load(const char *type, const char *uri); /** diff --git a/src/TagFile.hxx b/src/TagFile.hxx index 078abebd9..d1dbb3e46 100644 --- a/src/TagFile.hxx +++ b/src/TagFile.hxx @@ -34,6 +34,6 @@ struct tag_handler; */ bool tag_file_scan(Path path, - const struct tag_handler *handler, void *handler_ctx); + const tag_handler *handler, void *handler_ctx); #endif diff --git a/src/TextInputStream.cxx b/src/TextInputStream.cxx index 36a726aa6..036da052f 100644 --- a/src/TextInputStream.cxx +++ b/src/TextInputStream.cxx @@ -21,7 +21,6 @@ #include "TextInputStream.hxx" #include "InputStream.hxx" #include "util/CharUtil.hxx" -#include "util/fifo_buffer.h" #include "util/Error.hxx" #include "Log.hxx" diff --git a/src/TextInputStream.hxx b/src/TextInputStream.hxx index a6c15f670..350871488 100644 --- a/src/TextInputStream.hxx +++ b/src/TextInputStream.hxx @@ -25,7 +25,6 @@ #include <string> struct InputStream; -struct fifo_buffer; class TextInputStream { InputStream &is; diff --git a/src/Timer.cxx b/src/Timer.cxx index 661aa29ee..a132977f1 100644 --- a/src/Timer.cxx +++ b/src/Timer.cxx @@ -27,8 +27,6 @@ #include <limits> #include <assert.h> -#include <limits.h> -#include <stddef.h> Timer::Timer(const AudioFormat af) : time(0), diff --git a/src/UpdateArchive.hxx b/src/UpdateArchive.hxx index 8f52ca0b6..35601a844 100644 --- a/src/UpdateArchive.hxx +++ b/src/UpdateArchive.hxx @@ -26,7 +26,6 @@ #include <sys/stat.h> struct Directory; -struct archive_plugin; #ifdef ENABLE_ARCHIVE diff --git a/src/UpdateRemove.cxx b/src/UpdateRemove.cxx index f4043b2f3..555b340d3 100644 --- a/src/UpdateRemove.cxx +++ b/src/UpdateRemove.cxx @@ -20,7 +20,6 @@ #include "config.h" /* must be first for large file support */ #include "UpdateRemove.hxx" #include "UpdateDomain.hxx" -#include "Playlist.hxx" #include "GlobalEvents.hxx" #include "thread/Mutex.hxx" #include "thread/Cond.hxx" diff --git a/src/UpdateSong.cxx b/src/UpdateSong.cxx index bfab5c4a0..bbfde7f4d 100644 --- a/src/UpdateSong.cxx +++ b/src/UpdateSong.cxx @@ -27,7 +27,6 @@ #include "DatabaseLock.hxx" #include "Directory.hxx" #include "Song.hxx" -#include "DecoderPlugin.hxx" #include "DecoderList.hxx" #include "Log.hxx" diff --git a/src/UpdateWalk.cxx b/src/UpdateWalk.cxx index d4586456b..811978d52 100644 --- a/src/UpdateWalk.cxx +++ b/src/UpdateWalk.cxx @@ -44,12 +44,8 @@ #include <glib.h> #include <assert.h> -#include <sys/types.h> #include <sys/stat.h> -#include <unistd.h> -#include <dirent.h> #include <string.h> -#include <stdlib.h> #include <errno.h> bool walk_discard; @@ -294,7 +290,7 @@ skip_symlink(const Directory *directory, const char *utf8_name) const char *target_str = target.c_str(); - if (PathTraits::IsAbsoluteFS(target_str)) { + if (PathTraitsFS::IsAbsolute(target_str)) { /* if the symlink points to an absolute path, see if that path is inside the music directory */ const char *relative = map_to_relative_path(target_str); @@ -305,7 +301,7 @@ skip_symlink(const Directory *directory, const char *utf8_name) const char *p = target_str; while (*p == '.') { - if (p[1] == '.' && PathTraits::IsSeparatorFS(p[2])) { + if (p[1] == '.' && PathTraitsFS::IsSeparator(p[2])) { /* "../" moves to parent directory */ directory = directory->parent; if (directory == nullptr) { @@ -315,7 +311,7 @@ skip_symlink(const Directory *directory, const char *utf8_name) return !follow_outside_symlinks; } p += 3; - } else if (PathTraits::IsSeparatorFS(p[1])) + } else if (PathTraitsFS::IsSeparator(p[1])) /* eliminate "./" */ p += 2; else @@ -458,7 +454,7 @@ update_uri(const char *uri) if (parent == nullptr) return; - const char *name = PathTraits::GetBaseUTF8(uri); + const char *name = PathTraitsUTF8::GetBase(uri); struct stat st; if (!skip_symlink(parent, name) && diff --git a/src/Volume.cxx b/src/Volume.cxx index 6c5f8dc4d..a4112a4cc 100644 --- a/src/Volume.cxx +++ b/src/Volume.cxx @@ -22,11 +22,11 @@ #include "MixerAll.hxx" #include "Idle.hxx" #include "GlobalEvents.hxx" +#include "util/StringUtil.hxx" #include "util/Domain.hxx" +#include "system/PeriodClock.hxx" #include "Log.hxx" -#include <glib.h> - #include <assert.h> #include <stdlib.h> @@ -39,7 +39,7 @@ static unsigned volume_software_set = 100; /** the cached hardware mixer value; invalid if negative */ static int last_hardware_volume = -1; /** the age of #last_hardware_volume */ -static GTimer *hardware_volume_timer; +static PeriodClock hardware_volume_clock; /** * Handler for #GlobalEvents::MIXER. @@ -54,29 +54,19 @@ mixer_event_callback(void) idle_add(IDLE_MIXER); } -void volume_finish(void) -{ - g_timer_destroy(hardware_volume_timer); -} - void volume_init(void) { - hardware_volume_timer = g_timer_new(); - GlobalEvents::Register(GlobalEvents::MIXER, mixer_event_callback); } int volume_level_get(void) { - assert(hardware_volume_timer != nullptr); - if (last_hardware_volume >= 0 && - g_timer_elapsed(hardware_volume_timer, nullptr) < 1.0) + !hardware_volume_clock.CheckUpdate(1000)) /* throttle access to hardware mixers */ return last_hardware_volume; last_hardware_volume = mixer_all_get_volume(); - g_timer_start(hardware_volume_timer); return last_hardware_volume; } @@ -115,7 +105,7 @@ read_sw_volume_state(const char *line) char *end = nullptr; long int sv; - if (!g_str_has_prefix(line, SW_VOLUME_STATE)) + if (!StringStartsWith(line, SW_VOLUME_STATE)) return false; line += sizeof(SW_VOLUME_STATE) - 1; diff --git a/src/Volume.hxx b/src/Volume.hxx index 6b937aca3..d448cc4e4 100644 --- a/src/Volume.hxx +++ b/src/Volume.hxx @@ -26,8 +26,6 @@ void volume_init(void); -void volume_finish(void); - gcc_pure int volume_level_get(void); diff --git a/src/ZeroconfAvahi.cxx b/src/ZeroconfAvahi.cxx index 083647b42..f486a2e31 100644 --- a/src/ZeroconfAvahi.cxx +++ b/src/ZeroconfAvahi.cxx @@ -29,14 +29,11 @@ #include <avahi-client/client.h> #include <avahi-client/publish.h> -#include <avahi-common/watch.h> #include <avahi-common/alternative.h> #include <avahi-common/domain.h> #include <avahi-common/malloc.h> #include <avahi-common/error.h> -#include <stddef.h> - static constexpr Domain avahi_domain("avahi"); static char *avahiName; diff --git a/src/archive/Bzip2ArchivePlugin.cxx b/src/archive/Bzip2ArchivePlugin.cxx index d1e6b51af..c2d312cd1 100644 --- a/src/archive/Bzip2ArchivePlugin.cxx +++ b/src/archive/Bzip2ArchivePlugin.cxx @@ -35,9 +35,7 @@ #include <bzlib.h> -#include <stdint.h> #include <stddef.h> -#include <string.h> #ifdef HAVE_OLDER_BZIP2 #define BZ2_bzDecompressInit bzDecompressInit @@ -53,7 +51,7 @@ public: Bzip2ArchiveFile(const char *path, InputStream *_is) :ArchiveFile(bz2_archive_plugin), - name(PathTraits::GetBaseUTF8(path)), + name(PathTraitsUTF8::GetBase(path)), istream(_is) { // remove .bz2 suffix const size_t len = name.length(); diff --git a/src/archive/Iso9660ArchivePlugin.cxx b/src/archive/Iso9660ArchivePlugin.cxx index ad21d4a3d..c83e38b0d 100644 --- a/src/archive/Iso9660ArchivePlugin.cxx +++ b/src/archive/Iso9660ArchivePlugin.cxx @@ -32,7 +32,6 @@ #include "util/Error.hxx" #include "util/Domain.hxx" -#include <cdio/cdio.h> #include <cdio/iso9660.h> #include <stdlib.h> @@ -41,11 +40,11 @@ #define CEILING(x, y) ((x+(y-1))/y) class Iso9660ArchiveFile final : public ArchiveFile { -public: RefCount ref; iso9660_t *iso; +public: Iso9660ArchiveFile(iso9660_t *_iso) :ArchiveFile(iso9660_archive_plugin), iso(_iso) {} @@ -53,11 +52,19 @@ public: iso9660_close(iso); } + void Ref() { + ref.Increment(); + } + void Unref() { if (ref.Decrement()) delete this; } + long SeekRead(void *ptr, lsn_t start, long int i_size) const { + return iso9660_iso_seek_read(iso, ptr, start, i_size); + } + void Visit(const char *path, ArchiveVisitor &visitor); virtual void Close() override { @@ -131,31 +138,35 @@ Iso9660ArchiveFile::Visit(ArchiveVisitor &visitor) /* single archive handling */ -struct Iso9660InputStream { +class Iso9660InputStream { InputStream base; - Iso9660ArchiveFile *archive; + Iso9660ArchiveFile &archive; iso9660_stat_t *statbuf; - size_t max_blocks; +public: Iso9660InputStream(Iso9660ArchiveFile &_archive, const char *uri, Mutex &mutex, Cond &cond, iso9660_stat_t *_statbuf) :base(iso9660_input_plugin, uri, mutex, cond), - archive(&_archive), statbuf(_statbuf), - max_blocks(CEILING(statbuf->size, ISO_BLOCKSIZE)) { - + archive(_archive), statbuf(_statbuf) { base.ready = true; base.size = statbuf->size; - archive->ref.Increment(); + archive.Ref(); } ~Iso9660InputStream() { free(statbuf); - archive->Unref(); + archive.Unref(); } + + InputStream *Get() { + return &base; + } + + size_t Read(void *ptr, size_t size, Error &error); }; InputStream * @@ -173,7 +184,7 @@ Iso9660ArchiveFile::OpenStream(const char *pathname, Iso9660InputStream *iis = new Iso9660InputStream(*this, pathname, mutex, cond, statbuf); - return &iis->base; + return iis->Get(); } static void @@ -184,45 +195,49 @@ iso9660_input_close(InputStream *is) delete iis; } - -static size_t -iso9660_input_read(InputStream *is, void *ptr, size_t size, - Error &error) +inline size_t +Iso9660InputStream::Read(void *ptr, size_t size, Error &error) { - Iso9660InputStream *iis = (Iso9660InputStream *)is; int readed = 0; int no_blocks, cur_block; - size_t left_bytes = iis->statbuf->size - is->offset; - - size = (size * ISO_BLOCKSIZE) / ISO_BLOCKSIZE; + size_t left_bytes = statbuf->size - base.offset; if (left_bytes < size) { no_blocks = CEILING(left_bytes,ISO_BLOCKSIZE); } else { no_blocks = size / ISO_BLOCKSIZE; } - if (no_blocks > 0) { - cur_block = is->offset / ISO_BLOCKSIZE; + if (no_blocks == 0) + return 0; - readed = iso9660_iso_seek_read (iis->archive->iso, ptr, - iis->statbuf->lsn + cur_block, no_blocks); + cur_block = base.offset / ISO_BLOCKSIZE; - if (readed != no_blocks * ISO_BLOCKSIZE) { - error.Format(iso9660_domain, - "error reading ISO file at lsn %lu", - (unsigned long)cur_block); - return 0; - } - if (left_bytes < size) { - readed = left_bytes; - } + readed = archive.SeekRead(ptr, statbuf->lsn + cur_block, + no_blocks); - is->offset += readed; + if (readed != no_blocks * ISO_BLOCKSIZE) { + error.Format(iso9660_domain, + "error reading ISO file at lsn %lu", + (unsigned long)cur_block); + return 0; + } + if (left_bytes < size) { + readed = left_bytes; } + + base.offset += readed; return readed; } +static size_t +iso9660_input_read(InputStream *is, void *ptr, size_t size, + Error &error) +{ + Iso9660InputStream *iis = (Iso9660InputStream *)is; + return iis->Read(ptr, size, error); +} + static bool iso9660_input_eof(InputStream *is) { diff --git a/src/archive/ZzipArchivePlugin.cxx b/src/archive/ZzipArchivePlugin.cxx index 973fe91dc..2ad4ea72e 100644 --- a/src/archive/ZzipArchivePlugin.cxx +++ b/src/archive/ZzipArchivePlugin.cxx @@ -34,8 +34,6 @@ #include <zzip/zzip.h> -#include <string.h> - class ZzipArchiveFile final : public ArchiveFile { public: RefCount ref; diff --git a/src/command/AllCommands.cxx b/src/command/AllCommands.cxx index 74802ced4..23641ddec 100644 --- a/src/command/AllCommands.cxx +++ b/src/command/AllCommands.cxx @@ -20,6 +20,7 @@ #include "config.h" #include "AllCommands.hxx" #include "QueueCommands.hxx" +#include "TagCommands.hxx" #include "PlayerCommands.hxx" #include "PlaylistCommands.hxx" #include "DatabaseCommands.hxx" @@ -74,9 +75,11 @@ handle_not_commands(Client &client, int argc, char *argv[]); static const struct command commands[] = { { "add", PERMISSION_ADD, 1, 1, handle_add }, { "addid", PERMISSION_ADD, 1, 2, handle_addid }, + { "addtagid", PERMISSION_ADD, 3, 3, handle_addtagid }, { "channels", PERMISSION_READ, 0, 0, handle_channels }, { "clear", PERMISSION_CONTROL, 0, 0, handle_clear }, { "clearerror", PERMISSION_CONTROL, 0, 0, handle_clearerror }, + { "cleartagid", PERMISSION_ADD, 1, 2, handle_cleartagid }, { "close", PERMISSION_NONE, -1, -1, handle_close }, { "commands", PERMISSION_NONE, 0, 0, handle_commands }, { "config", PERMISSION_ADMIN, 0, 0, handle_config }, diff --git a/src/command/CommandError.cxx b/src/command/CommandError.cxx index fc14d4a5d..c1ef09904 100644 --- a/src/command/CommandError.cxx +++ b/src/command/CommandError.cxx @@ -24,9 +24,8 @@ #include "util/Error.hxx" #include "Log.hxx" -#include <glib.h> - #include <assert.h> +#include <string.h> #include <errno.h> CommandResult @@ -38,7 +37,7 @@ print_playlist_result(Client &client, PlaylistResult result) case PlaylistResult::ERRNO: command_error(client, ACK_ERROR_SYSTEM, "%s", - g_strerror(errno)); + strerror(errno)); return CommandResult::ERROR; case PlaylistResult::DENIED: @@ -115,7 +114,7 @@ print_error(Client &client, const Error &error) } } else if (error.IsDomain(errno_domain)) { command_error(client, ACK_ERROR_SYSTEM, "%s", - g_strerror(error.GetCode())); + strerror(error.GetCode())); return CommandResult::ERROR; } diff --git a/src/command/DatabaseCommands.cxx b/src/command/DatabaseCommands.cxx index b86cbdae7..55405c1ef 100644 --- a/src/command/DatabaseCommands.cxx +++ b/src/command/DatabaseCommands.cxx @@ -26,14 +26,10 @@ #include "CommandError.hxx" #include "Client.hxx" #include "tag/Tag.hxx" -#include "util/UriUtil.hxx" #include "util/Error.hxx" #include "SongFilter.hxx" #include "protocol/Result.hxx" -#include <assert.h> -#include <string.h> - CommandResult handle_lsinfo2(Client &client, int argc, char *argv[]) { diff --git a/src/command/MessageCommands.cxx b/src/command/MessageCommands.cxx index 7d9893e70..0c2263d59 100644 --- a/src/command/MessageCommands.cxx +++ b/src/command/MessageCommands.cxx @@ -24,7 +24,6 @@ #include "Instance.hxx" #include "Main.hxx" #include "protocol/Result.hxx" -#include "protocol/ArgParser.hxx" #include <set> #include <string> diff --git a/src/command/OtherCommands.cxx b/src/command/OtherCommands.cxx index 7b2cb1331..0c1de8792 100644 --- a/src/command/OtherCommands.cxx +++ b/src/command/OtherCommands.cxx @@ -44,10 +44,6 @@ #include "Client.hxx" #include "Idle.hxx" -#ifdef ENABLE_SQLITE -#include "StickerDatabase.hxx" -#endif - #include <assert.h> #include <string.h> diff --git a/src/command/OutputCommands.cxx b/src/command/OutputCommands.cxx index e949448af..d4d699180 100644 --- a/src/command/OutputCommands.cxx +++ b/src/command/OutputCommands.cxx @@ -24,8 +24,6 @@ #include "protocol/Result.hxx" #include "protocol/ArgParser.hxx" -#include <string.h> - CommandResult handle_enableoutput(Client &client, gcc_unused int argc, char *argv[]) { diff --git a/src/command/PlaylistCommands.cxx b/src/command/PlaylistCommands.cxx index d178fa097..20c5a2595 100644 --- a/src/command/PlaylistCommands.cxx +++ b/src/command/PlaylistCommands.cxx @@ -35,9 +35,6 @@ #include "util/UriUtil.hxx" #include "util/Error.hxx" -#include <assert.h> -#include <stdlib.h> - static void print_spl_list(Client &client, const PlaylistVector &list) { diff --git a/src/command/StickerCommands.cxx b/src/command/StickerCommands.cxx index b65e6f607..d353a5541 100644 --- a/src/command/StickerCommands.cxx +++ b/src/command/StickerCommands.cxx @@ -31,8 +31,6 @@ #include "protocol/Result.hxx" #include "util/Error.hxx" -#include <glib.h> - #include <string.h> struct sticker_song_find_data { diff --git a/src/command/TagCommands.cxx b/src/command/TagCommands.cxx new file mode 100644 index 000000000..829d34b81 --- /dev/null +++ b/src/command/TagCommands.cxx @@ -0,0 +1,78 @@ +/* + * 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 "TagCommands.hxx" +#include "CommandError.hxx" +#include "Client.hxx" +#include "protocol/ArgParser.hxx" +#include "protocol/Result.hxx" +#include "tag/Tag.hxx" +#include "Partition.hxx" + +CommandResult +handle_addtagid(Client &client, gcc_unused int argc, char *argv[]) +{ + unsigned song_id; + if (!check_unsigned(client, &song_id, argv[1])) + return CommandResult::ERROR; + + const char *const tag_name = argv[2]; + const TagType tag_type = tag_name_parse_i(tag_name); + if (tag_type == TAG_NUM_OF_ITEM_TYPES) { + command_error(client, ACK_ERROR_ARG, + "Unknown tag type: %s", tag_name); + return CommandResult::ERROR; + } + + const char *const value = argv[3]; + + Error error; + if (!client.partition.playlist.AddSongIdTag(song_id, tag_type, value, + error)) + return print_error(client, error); + + return CommandResult::OK; +} + +CommandResult +handle_cleartagid(Client &client, int argc, char *argv[]) +{ + unsigned song_id; + if (!check_unsigned(client, &song_id, argv[1])) + return CommandResult::ERROR; + + TagType tag_type = TAG_NUM_OF_ITEM_TYPES; + if (argc >= 3) { + const char *const tag_name = argv[2]; + tag_type = tag_name_parse_i(tag_name); + if (tag_type == TAG_NUM_OF_ITEM_TYPES) { + command_error(client, ACK_ERROR_ARG, + "Unknown tag type: %s", tag_name); + return CommandResult::ERROR; + } + } + + Error error; + if (!client.partition.playlist.ClearSongIdTag(song_id, tag_type, + error)) + return print_error(client, error); + + return CommandResult::OK; +} diff --git a/src/command/TagCommands.hxx b/src/command/TagCommands.hxx new file mode 100644 index 000000000..8ccafa76a --- /dev/null +++ b/src/command/TagCommands.hxx @@ -0,0 +1,33 @@ +/* + * 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_TAG_COMMANDS_HXX +#define MPD_TAG_COMMANDS_HXX + +#include "CommandResult.hxx" + +class Client; + +CommandResult +handle_addtagid(Client &client, int argc, char *argv[]); + +CommandResult +handle_cleartagid(Client &client, int argc, char *argv[]); + +#endif diff --git a/src/cue/CueParser.cxx b/src/cue/CueParser.cxx index 60b33b6b4..7a226c849 100644 --- a/src/cue/CueParser.cxx +++ b/src/cue/CueParser.cxx @@ -31,7 +31,7 @@ #include <stdlib.h> CueParser::CueParser() - :state(HEADER), tag(new Tag()), + :state(HEADER), current(nullptr), previous(nullptr), finished(nullptr), @@ -39,8 +39,6 @@ CueParser::CueParser() CueParser::~CueParser() { - delete tag; - if (current != nullptr) current->Free(); @@ -109,7 +107,7 @@ cue_next_value(char **pp) } static void -cue_add_tag(Tag &tag, TagType type, char *p) +cue_add_tag(TagBuilder &tag, TagType type, char *p) { const char *value = cue_next_value(&p); if (value != nullptr) @@ -118,7 +116,7 @@ cue_add_tag(Tag &tag, TagType type, char *p) } static void -cue_parse_rem(char *p, Tag &tag) +cue_parse_rem(char *p, TagBuilder &tag) { const char *type = cue_next_token(&p); if (type == nullptr) @@ -129,13 +127,13 @@ cue_parse_rem(char *p, Tag &tag) cue_add_tag(tag, type2, p); } -Tag * +TagBuilder * CueParser::GetCurrentTag() { if (state == HEADER) - return tag; + return &header_tag; else if (state == TRACK) - return current->tag; + return &song_tag; else return nullptr; } @@ -172,6 +170,9 @@ CueParser::Commit() if (current == nullptr) return; + assert(current->tag == nullptr); + current->tag = song_tag.Commit(); + finished = previous; previous = current; current = nullptr; @@ -188,9 +189,9 @@ CueParser::Feed2(char *p) return; if (strcmp(command, "REM") == 0) { - Tag *current_tag = GetCurrentTag(); - if (current_tag != nullptr) - cue_parse_rem(p, *current_tag); + TagBuilder *tag = GetCurrentTag(); + if (tag != nullptr) + cue_parse_rem(p, *tag); } else if (strcmp(command, "PERFORMER") == 0) { /* MPD knows a "performer" tag, but it is not a good match for this CUE tag; from the Hydrogenaudio @@ -202,14 +203,14 @@ CueParser::Feed2(char *p) ? TAG_ARTIST : TAG_ALBUM_ARTIST; - Tag *current_tag = GetCurrentTag(); - if (current_tag != nullptr) - cue_add_tag(*current_tag, type, p); + TagBuilder *tag = GetCurrentTag(); + if (tag != nullptr) + cue_add_tag(*tag, type, p); } else if (strcmp(command, "TITLE") == 0) { if (state == HEADER) - cue_add_tag(*tag, TAG_ALBUM, p); + cue_add_tag(header_tag, TAG_ALBUM, p); else if (state == TRACK) - cue_add_tag(*current->tag, TAG_TITLE, p); + cue_add_tag(song_tag, TAG_TITLE, p); } else if (strcmp(command, "FILE") == 0) { Commit(); @@ -251,8 +252,10 @@ CueParser::Feed2(char *p) state = TRACK; current = Song::NewRemote(filename.c_str()); assert(current->tag == nullptr); - current->tag = new Tag(*tag); - current->tag->AddItem(TAG_TRACK, nr); + + song_tag = header_tag; + song_tag.AddItem(TAG_TRACK, nr); + last_updated = false; } else if (state == IGNORE_TRACK) { return; diff --git a/src/cue/CueParser.hxx b/src/cue/CueParser.hxx index abcceaa2e..bcc759c37 100644 --- a/src/cue/CueParser.hxx +++ b/src/cue/CueParser.hxx @@ -21,6 +21,7 @@ #define MPD_CUE_PARSER_HXX #include "check.h" +#include "tag/TagBuilder.hxx" #include "Compiler.h" #include <string> @@ -56,7 +57,17 @@ class CueParser { IGNORE_TRACK, } state; - Tag *tag; + /** + * Tags read from the CUE header. + */ + TagBuilder header_tag; + + /** + * Tags read for the current song (attribute #current). When + * #current gets moved to #previous, TagBuilder::Commit() will + * be called. + */ + TagBuilder song_tag; std::string filename; @@ -118,7 +129,7 @@ public: private: gcc_pure - Tag *GetCurrentTag(); + TagBuilder *GetCurrentTag(); /** * Commit the current song. It will be moved to "previous", diff --git a/src/db/ProxyDatabasePlugin.cxx b/src/db/ProxyDatabasePlugin.cxx index 00b5d445f..a4fa88edd 100644 --- a/src/db/ProxyDatabasePlugin.cxx +++ b/src/db/ProxyDatabasePlugin.cxx @@ -22,7 +22,6 @@ #include "DatabasePlugin.hxx" #include "DatabaseSelection.hxx" #include "DatabaseError.hxx" -#include "PlaylistVector.hxx" #include "Directory.hxx" #include "Song.hxx" #include "SongFilter.hxx" diff --git a/src/db/SimpleDatabasePlugin.cxx b/src/db/SimpleDatabasePlugin.cxx index e7ea7a62d..7fc66ed67 100644 --- a/src/db/SimpleDatabasePlugin.cxx +++ b/src/db/SimpleDatabasePlugin.cxx @@ -26,14 +26,13 @@ #include "DatabaseSave.hxx" #include "DatabaseLock.hxx" #include "DatabaseError.hxx" -#include "TextFile.hxx" +#include "fs/TextFile.hxx" #include "ConfigData.hxx" #include "fs/FileSystem.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" #include "Log.hxx" -#include <sys/types.h> #include <errno.h> static constexpr Domain simple_db_domain("simple_db"); @@ -72,7 +71,7 @@ SimpleDatabase::Check(Error &error) const assert(!path.IsNull()); /* Check if the file exists */ - if (!CheckAccess(path, F_OK)) { + if (!CheckAccess(path)) { /* If the file doesn't exist, we can't check if we can write * it, so we are going to try to get the directory path, and * see if we can write a file in that */ @@ -95,6 +94,7 @@ SimpleDatabase::Check(Error &error) const return false; } +#ifndef WIN32 /* Check if we can write to the directory */ if (!CheckAccess(dirPath, X_OK | W_OK)) { const int e = errno; @@ -103,7 +103,7 @@ SimpleDatabase::Check(Error &error) const dirPath_utf8.c_str()); return false; } - +#endif return true; } @@ -122,12 +122,14 @@ SimpleDatabase::Check(Error &error) const return false; } +#ifndef WIN32 /* And check that we can write to it */ if (!CheckAccess(path, R_OK | W_OK)) { error.FormatErrno("Can't open db file \"%s\" for reading/writing", path_utf8.c_str()); return false; } +#endif return true; } diff --git a/src/decoder/DsdLib.cxx b/src/decoder/DsdLib.cxx index 67cc7e945..b0ba73126 100644 --- a/src/decoder/DsdLib.cxx +++ b/src/decoder/DsdLib.cxx @@ -27,8 +27,6 @@ #include "DsdLib.hxx" #include "DecoderAPI.hxx" #include "InputStream.hxx" -#include "util/bit_reverse.h" -#include "tag/TagHandler.hxx" #include "tag/TagId3.hxx" #include "util/Error.hxx" diff --git a/src/decoder/DsdiffDecoderPlugin.cxx b/src/decoder/DsdiffDecoderPlugin.cxx index a3c0149b9..bc9fc2353 100644 --- a/src/decoder/DsdiffDecoderPlugin.cxx +++ b/src/decoder/DsdiffDecoderPlugin.cxx @@ -38,9 +38,6 @@ #include "DsdLib.hxx" #include "Log.hxx" -#include <unistd.h> -#include <stdio.h> /* for SEEK_SET, SEEK_CUR */ - struct DsdiffHeader { DsdId id; DffDsdUint64 size; diff --git a/src/decoder/DsfDecoderPlugin.cxx b/src/decoder/DsfDecoderPlugin.cxx index 5ef94e647..ab96e8ce6 100644 --- a/src/decoder/DsfDecoderPlugin.cxx +++ b/src/decoder/DsfDecoderPlugin.cxx @@ -39,9 +39,6 @@ #include "tag/TagHandler.hxx" #include "Log.hxx" -#include <unistd.h> -#include <stdio.h> /* for SEEK_SET, SEEK_CUR */ - struct DsfMetaData { unsigned sample_rate, channels; bool bitreverse; diff --git a/src/decoder/FfmpegDecoderPlugin.cxx b/src/decoder/FfmpegDecoderPlugin.cxx index 47e1a3384..01b551bb1 100644 --- a/src/decoder/FfmpegDecoderPlugin.cxx +++ b/src/decoder/FfmpegDecoderPlugin.cxx @@ -38,7 +38,6 @@ extern "C" { #include <libavutil/avutil.h> #include <libavutil/log.h> #include <libavutil/mathematics.h> -#include <libavutil/dict.h> } #include <assert.h> diff --git a/src/decoder/FfmpegMetaData.hxx b/src/decoder/FfmpegMetaData.hxx index 0fd73df04..998cdf5a8 100644 --- a/src/decoder/FfmpegMetaData.hxx +++ b/src/decoder/FfmpegMetaData.hxx @@ -21,8 +21,6 @@ #define MPD_FFMPEG_METADATA_HXX extern "C" { -#include <libavformat/avformat.h> -#include <libavutil/avutil.h> #include <libavutil/dict.h> } @@ -35,6 +33,6 @@ struct tag_handler; void ffmpeg_scan_dictionary(AVDictionary *dict, - const struct tag_handler *handler, void *handler_ctx); + const tag_handler *handler, void *handler_ctx); #endif diff --git a/src/decoder/FlacCommon.cxx b/src/decoder/FlacCommon.cxx index e4b906c12..e355f285a 100644 --- a/src/decoder/FlacCommon.cxx +++ b/src/decoder/FlacCommon.cxx @@ -25,14 +25,10 @@ #include "FlacCommon.hxx" #include "FlacMetadata.hxx" #include "FlacPcm.hxx" -#include "MixRampInfo.hxx" #include "CheckAudioFormat.hxx" #include "util/Error.hxx" -#include "util/Domain.hxx" #include "Log.hxx" -#include <assert.h> - flac_data::flac_data(Decoder &_decoder, InputStream &_input_stream) :FlacInput(_input_stream, &_decoder), diff --git a/src/decoder/FlacCommon.hxx b/src/decoder/FlacCommon.hxx index de000dfa1..0f6b09973 100644 --- a/src/decoder/FlacCommon.hxx +++ b/src/decoder/FlacCommon.hxx @@ -29,7 +29,6 @@ #include "pcm/PcmBuffer.hxx" #include <FLAC/stream_decoder.h> -#include <FLAC/metadata.h> struct flac_data : public FlacInput { PcmBuffer buffer; diff --git a/src/decoder/FlacDecoderPlugin.cxx b/src/decoder/FlacDecoderPlugin.cxx index 1b5734434..2c811a8e0 100644 --- a/src/decoder/FlacDecoderPlugin.cxx +++ b/src/decoder/FlacDecoderPlugin.cxx @@ -26,10 +26,6 @@ #include "util/Error.hxx" #include "Log.hxx" -#include <glib.h> - -#include <assert.h> - #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7 #error libFLAC is too old #endif diff --git a/src/decoder/FlacMetadata.cxx b/src/decoder/FlacMetadata.cxx index 17cc4cd8d..f5aef7309 100644 --- a/src/decoder/FlacMetadata.cxx +++ b/src/decoder/FlacMetadata.cxx @@ -21,43 +21,44 @@ #include "FlacMetadata.hxx" #include "XiphTags.hxx" #include "MixRampInfo.hxx" -#include "tag/Tag.hxx" #include "tag/TagHandler.hxx" #include "tag/TagTable.hxx" #include "tag/TagBuilder.hxx" #include "ReplayGainInfo.hxx" #include "util/ASCII.hxx" +#include "util/SplitString.hxx" -#include <glib.h> - -#include <assert.h> #include <string.h> +static const char * +vorbis_comment_value(const FLAC__StreamMetadata *block, + const char *name) +{ + int offset = + FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0, + name); + if (offset < 0) + return nullptr; + + size_t name_length = strlen(name); + + const FLAC__StreamMetadata_VorbisComment_Entry &vc = + block->data.vorbis_comment.comments[offset]; + const char *comment = (const char *)vc.entry; + + /* 1 is for '=' */ + return comment + name_length + 1; +} + static bool flac_find_float_comment(const FLAC__StreamMetadata *block, const char *cmnt, float *fl) { - int offset; - size_t pos; - int len; - unsigned char tmp, *p; - - offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0, - cmnt); - if (offset < 0) - return false; - - pos = strlen(cmnt) + 1; /* 1 is for '=' */ - len = block->data.vorbis_comment.comments[offset].length - pos; - if (len <= 0) + const char *value = vorbis_comment_value(block, cmnt); + if (value == nullptr) return false; - p = &block->data.vorbis_comment.comments[offset].entry[pos]; - tmp = p[len]; - p[len] = '\0'; - *fl = (float)atof((char *)p); - p[len] = tmp; - + *fl = (float)atof(value); return true; } @@ -88,23 +89,11 @@ gcc_pure static std::string flac_find_string_comment(const FLAC__StreamMetadata *block, const char *cmnt) { - int offset; - size_t pos; - int len; - const unsigned char *p; - - offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0, - cmnt); - if (offset < 0) + const char *value = vorbis_comment_value(block, cmnt); + if (value == nullptr) return std::string(); - pos = strlen(cmnt) + 1; /* 1 is for '=' */ - len = block->data.vorbis_comment.comments[offset].length - pos; - if (len <= 0) - return std::string(); - - p = &block->data.vorbis_comment.comments[offset].entry[pos]; - return std::string((const char *)p, len); + return std::string(value); } MixRampInfo @@ -118,21 +107,19 @@ flac_parse_mixramp(const FLAC__StreamMetadata *block) /** * Checks if the specified name matches the entry's name, and if yes, - * returns the comment value (not null-temrinated). + * returns the comment value; */ static const char * flac_comment_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry, - const char *name, size_t *length_r) + const char *name) { size_t name_length = strlen(name); const char *comment = (const char*)entry->entry; - if (entry->length <= name_length || - !StringEqualsCaseASCII(comment, name, name_length)) + if (!StringEqualsCaseASCII(comment, name, name_length)) return nullptr; if (comment[name_length] == '=') { - *length_r = entry->length - name_length - 1; return comment + name_length + 1; } @@ -148,14 +135,9 @@ flac_copy_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *name, TagType tag_type, const struct tag_handler *handler, void *handler_ctx) { - const char *value; - size_t value_length; - - value = flac_comment_value(entry, name, &value_length); + const char *value = flac_comment_value(entry, name); if (value != nullptr) { - char *p = g_strndup(value, value_length); - tag_handler_invoke_tag(handler, handler_ctx, tag_type, p); - g_free(p); + tag_handler_invoke_tag(handler, handler_ctx, tag_type, value); return true; } @@ -167,16 +149,12 @@ flac_scan_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry, const struct tag_handler *handler, void *handler_ctx) { if (handler->pair != nullptr) { - char *name = g_strdup((const char*)entry->entry); - char *value = strchr(name, '='); - - if (value != nullptr && value > name) { - *value++ = 0; + const char *comment = (const char *)entry->entry; + const SplitString split(comment, '='); + if (split.IsDefined() && !split.IsEmpty()) tag_handler_invoke_pair(handler, handler_ctx, - name, value); - } - - g_free(name); + split.GetFirst(), + split.GetSecond()); } for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i) diff --git a/src/decoder/FlacMetadata.hxx b/src/decoder/FlacMetadata.hxx index 96c61b8e6..137101a65 100644 --- a/src/decoder/FlacMetadata.hxx +++ b/src/decoder/FlacMetadata.hxx @@ -27,6 +27,7 @@ #include <assert.h> +struct tag_handler; class MixRampInfo; class FlacMetadataChain { @@ -81,7 +82,7 @@ public: return FLAC__Metadata_ChainStatusString[GetStatus()]; } - void Scan(const struct tag_handler *handler, void *handler_ctx); + void Scan(const tag_handler *handler, void *handler_ctx); }; class FLACMetadataIterator { @@ -110,7 +111,6 @@ public: } }; -struct tag_handler; struct Tag; struct ReplayGainInfo; @@ -136,6 +136,6 @@ flac_vorbis_comments_to_tag(Tag &tag, void flac_scan_metadata(const FLAC__StreamMetadata *block, - const struct tag_handler *handler, void *handler_ctx); + const tag_handler *handler, void *handler_ctx); #endif diff --git a/src/decoder/MadDecoderPlugin.cxx b/src/decoder/MadDecoderPlugin.cxx index 9dd86c55f..76eead96d 100644 --- a/src/decoder/MadDecoderPlugin.cxx +++ b/src/decoder/MadDecoderPlugin.cxx @@ -26,23 +26,23 @@ #include "tag/TagRva2.hxx" #include "tag/TagHandler.hxx" #include "CheckAudioFormat.hxx" +#include "util/StringUtil.hxx" #include "util/ASCII.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" #include "Log.hxx" -#include <assert.h> -#include <unistd.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <glib.h> #include <mad.h> #ifdef HAVE_ID3TAG #include <id3tag.h> #endif +#include <assert.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + #define FRAMES_CUSHION 2000 #define READ_BUFFER_SIZE 40960 @@ -349,24 +349,14 @@ MadDecoder::ParseId3(size_t tagsize, Tag **mpd_tag) id3_data = stream.this_frame; mad_stream_skip(&(stream), tagsize); } else { - allocated = (id3_byte_t *)g_malloc(tagsize); + allocated = new id3_byte_t[tagsize]; memcpy(allocated, stream.this_frame, count); mad_stream_skip(&(stream), count); - while (count < tagsize) { - size_t len; - - len = decoder_read(decoder, input_stream, - allocated + count, tagsize - count); - if (len == 0) - break; - else - count += len; - } - - if (count != tagsize) { + if (!decoder_read_full(decoder, input_stream, + allocated + count, tagsize - count)) { LogDebug(mad_domain, "error parsing ID3 tag"); - g_free(allocated); + delete[] allocated; return; } @@ -375,7 +365,7 @@ MadDecoder::ParseId3(size_t tagsize, Tag **mpd_tag) id3_tag = id3_tag_parse(id3_data, tagsize); if (id3_tag == nullptr) { - g_free(allocated); + delete[] allocated; return; } @@ -400,7 +390,7 @@ MadDecoder::ParseId3(size_t tagsize, Tag **mpd_tag) id3_tag_delete(id3_tag); - g_free(allocated); + delete[] allocated; #else /* !HAVE_ID3TAG */ (void)mpd_tag; @@ -413,20 +403,7 @@ MadDecoder::ParseId3(size_t tagsize, Tag **mpd_tag) mad_stream_skip(&stream, tagsize); } else { mad_stream_skip(&stream, count); - - while (count < tagsize) { - size_t len = tagsize - count; - char ignored[1024]; - if (len > sizeof(ignored)) - len = sizeof(ignored); - - len = decoder_read(decoder, input_stream, - ignored, len); - if (len == 0) - break; - else - count += len; - } + decoder_skip(decoder, input_stream, tagsize - count); } #endif } @@ -690,7 +667,7 @@ parse_lame(struct lame *lame, struct mad_bitptr *ptr, int *bitlen) /* This is technically incorrect, since the encoder might not be lame. * But there's no other way to determine if this is a lame tag, and we * wouldn't want to go reading a tag that's not there. */ - if (!g_str_has_prefix(lame->encoder, "LAME")) + if (!StringStartsWith(lame->encoder, "LAME")) return false; if (sscanf(lame->encoder+4, "%u.%u", diff --git a/src/decoder/MikmodDecoderPlugin.cxx b/src/decoder/MikmodDecoderPlugin.cxx index 34381aafa..93a3cc280 100644 --- a/src/decoder/MikmodDecoderPlugin.cxx +++ b/src/decoder/MikmodDecoderPlugin.cxx @@ -25,8 +25,8 @@ #include "util/Domain.hxx" #include "Log.hxx" -#include <glib.h> #include <mikmod.h> + #include <assert.h> static constexpr Domain mikmod_domain("mikmod"); diff --git a/src/decoder/MpcdecDecoderPlugin.cxx b/src/decoder/MpcdecDecoderPlugin.cxx index dc258623c..620bfad30 100644 --- a/src/decoder/MpcdecDecoderPlugin.cxx +++ b/src/decoder/MpcdecDecoderPlugin.cxx @@ -30,8 +30,6 @@ #include <mpc/mpcdec.h> -#include <assert.h> -#include <unistd.h> #include <math.h> struct mpc_decoder_data { diff --git a/src/decoder/Mpg123DecoderPlugin.cxx b/src/decoder/Mpg123DecoderPlugin.cxx index df23f7847..895c8b865 100644 --- a/src/decoder/Mpg123DecoderPlugin.cxx +++ b/src/decoder/Mpg123DecoderPlugin.cxx @@ -26,9 +26,8 @@ #include "util/Domain.hxx" #include "Log.hxx" -#include <glib.h> - #include <mpg123.h> + #include <stdio.h> static constexpr Domain mpg123_domain("mpg123"); diff --git a/src/decoder/OggCodec.cxx b/src/decoder/OggCodec.cxx index 565dbafcf..cc6cd20c0 100644 --- a/src/decoder/OggCodec.cxx +++ b/src/decoder/OggCodec.cxx @@ -23,6 +23,7 @@ #include "config.h" #include "OggCodec.hxx" +#include "DecoderAPI.hxx" #include <string.h> diff --git a/src/decoder/OggCodec.hxx b/src/decoder/OggCodec.hxx index 857871607..13eab2cdb 100644 --- a/src/decoder/OggCodec.hxx +++ b/src/decoder/OggCodec.hxx @@ -24,7 +24,8 @@ #ifndef MPD_OGG_CODEC_HXX #define MPD_OGG_CODEC_HXX -#include "DecoderAPI.hxx" +struct Decoder; +struct InputStream; enum ogg_codec { OGG_CODEC_UNKNOWN, diff --git a/src/decoder/OggFind.hxx b/src/decoder/OggFind.hxx index ad51ccdf3..a329e42a4 100644 --- a/src/decoder/OggFind.hxx +++ b/src/decoder/OggFind.hxx @@ -25,7 +25,6 @@ #include <ogg/ogg.h> -struct InputStream; class OggSyncState; /** diff --git a/src/decoder/OpusDecoderPlugin.cxx b/src/decoder/OpusDecoderPlugin.cxx index f3d7b342e..201d61d96 100644 --- a/src/decoder/OpusDecoderPlugin.cxx +++ b/src/decoder/OpusDecoderPlugin.cxx @@ -22,12 +22,10 @@ #include "OpusDomain.hxx" #include "OpusHead.hxx" #include "OpusTags.hxx" -#include "OggUtil.hxx" #include "OggFind.hxx" #include "OggSyncState.hxx" #include "DecoderAPI.hxx" #include "OggCodec.hxx" -#include "CheckAudioFormat.hxx" #include "tag/TagHandler.hxx" #include "tag/TagBuilder.hxx" #include "InputStream.hxx" diff --git a/src/decoder/OpusHead.cxx b/src/decoder/OpusHead.cxx index 0417d3905..dd2b125b0 100644 --- a/src/decoder/OpusHead.cxx +++ b/src/decoder/OpusHead.cxx @@ -21,7 +21,6 @@ #include "OpusHead.hxx" #include <stdint.h> -#include <string.h> struct OpusHead { char signature[8]; diff --git a/src/decoder/PcmDecoderPlugin.cxx b/src/decoder/PcmDecoderPlugin.cxx index dbc38fb76..0bef8dc69 100644 --- a/src/decoder/PcmDecoderPlugin.cxx +++ b/src/decoder/PcmDecoderPlugin.cxx @@ -25,8 +25,6 @@ #include "util/ByteReverse.hxx" #include "Log.hxx" -#include <glib.h> -#include <unistd.h> #include <string.h> #include <stdio.h> /* for SEEK_SET */ diff --git a/src/decoder/VorbisComments.cxx b/src/decoder/VorbisComments.cxx index d4f019b58..2adc498d5 100644 --- a/src/decoder/VorbisComments.cxx +++ b/src/decoder/VorbisComments.cxx @@ -20,16 +20,13 @@ #include "config.h" #include "VorbisComments.hxx" #include "XiphTags.hxx" -#include "tag/Tag.hxx" #include "tag/TagTable.hxx" #include "tag/TagHandler.hxx" #include "tag/TagBuilder.hxx" #include "ReplayGainInfo.hxx" #include "util/ASCII.hxx" +#include "util/SplitString.hxx" -#include <glib.h> - -#include <assert.h> #include <stddef.h> #include <string.h> #include <stdlib.h> @@ -104,16 +101,11 @@ vorbis_scan_comment(const char *comment, const struct tag_handler *handler, void *handler_ctx) { if (handler->pair != nullptr) { - char *name = g_strdup((const char*)comment); - char *value = strchr(name, '='); - - if (value != nullptr && value > name) { - *value++ = 0; + const SplitString split(comment, '='); + if (split.IsDefined() && !split.IsEmpty()) tag_handler_invoke_pair(handler, handler_ctx, - name, value); - } - - g_free(name); + split.GetFirst(), + split.GetSecond()); } for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i) diff --git a/src/decoder/VorbisComments.hxx b/src/decoder/VorbisComments.hxx index e5a48ef6b..f537bf30f 100644 --- a/src/decoder/VorbisComments.hxx +++ b/src/decoder/VorbisComments.hxx @@ -31,7 +31,7 @@ vorbis_comments_to_replay_gain(ReplayGainInfo &rgi, char **comments); void vorbis_comments_scan(char **comments, - const struct tag_handler *handler, void *handler_ctx); + const tag_handler *handler, void *handler_ctx); Tag * vorbis_comments_to_tag(char **comments); diff --git a/src/decoder/VorbisDecoderPlugin.cxx b/src/decoder/VorbisDecoderPlugin.cxx index 4d3e48528..a637241b1 100644 --- a/src/decoder/VorbisDecoderPlugin.cxx +++ b/src/decoder/VorbisDecoderPlugin.cxx @@ -25,9 +25,7 @@ #include "InputStream.hxx" #include "OggCodec.hxx" #include "util/Error.hxx" -#include "util/UriUtil.hxx" #include "util/Macros.hxx" -#include "system/ByteOrder.hxx" #include "CheckAudioFormat.hxx" #include "tag/TagHandler.hxx" #include "Log.hxx" @@ -48,7 +46,6 @@ #define ov_time_seek_page(VF, S) (ov_time_seek_page(VF, (S)*1000)) #endif /* HAVE_TREMOR */ -#include <assert.h> #include <errno.h> struct vorbis_input_stream { diff --git a/src/decoder/VorbisDomain.hxx b/src/decoder/VorbisDomain.hxx index a35edd041..7946bf52a 100644 --- a/src/decoder/VorbisDomain.hxx +++ b/src/decoder/VorbisDomain.hxx @@ -22,6 +22,8 @@ #include "check.h" -extern const class Domain vorbis_domain; +class Domain; + +extern const Domain vorbis_domain; #endif diff --git a/src/encoder/FlacEncoderPlugin.cxx b/src/encoder/FlacEncoderPlugin.cxx index fa7ed992d..91c9382ad 100644 --- a/src/encoder/FlacEncoderPlugin.cxx +++ b/src/encoder/FlacEncoderPlugin.cxx @@ -23,16 +23,10 @@ #include "AudioFormat.hxx" #include "pcm/PcmBuffer.hxx" #include "ConfigError.hxx" +#include "util/Manual.hxx" +#include "util/DynamicFifoBuffer.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" -#include "util/fifo_buffer.h" - -extern "C" { -#include "util/growing_fifo.h" -} - -#include <assert.h> -#include <string.h> #include <FLAC/stream_encoder.h> @@ -54,7 +48,7 @@ struct flac_encoder { * This buffer will hold encoded data from libFLAC until it is * picked up with flac_encoder_read(). */ - struct fifo_buffer *output_buffer; + Manual<DynamicFifoBuffer<uint8_t>> output_buffer; flac_encoder():encoder(flac_encoder_plugin) {} }; @@ -141,7 +135,7 @@ flac_write_callback(gcc_unused const FLAC__StreamEncoder *fse, struct flac_encoder *encoder = (struct flac_encoder *) client_data; //transfer data to buffer - growing_fifo_append(&encoder->output_buffer, data, bytes); + encoder->output_buffer->Append((const uint8_t *)data, bytes); return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; } @@ -154,7 +148,7 @@ flac_encoder_close(Encoder *_encoder) FLAC__stream_encoder_delete(encoder->fse); encoder->expand_buffer.Clear(); - fifo_buffer_free(encoder->output_buffer); + encoder->output_buffer.Destruct(); } static bool @@ -196,7 +190,7 @@ flac_encoder_open(Encoder *_encoder, AudioFormat &audio_format, Error &error) return false; } - encoder->output_buffer = growing_fifo_new(); + encoder->output_buffer.Construct(8192); /* this immediately outputs data through callback */ @@ -305,18 +299,7 @@ flac_encoder_read(Encoder *_encoder, void *dest, size_t length) { struct flac_encoder *encoder = (struct flac_encoder *)_encoder; - size_t max_length; - const char *src = (const char *) - fifo_buffer_read(encoder->output_buffer, &max_length); - if (src == nullptr) - return 0; - - if (length > max_length) - length = max_length; - - memcpy(dest, src, length); - fifo_buffer_consume(encoder->output_buffer, length); - return length; + return encoder->output_buffer->Read((uint8_t *)dest, length); } static const char * diff --git a/src/encoder/NullEncoderPlugin.cxx b/src/encoder/NullEncoderPlugin.cxx index 3b1aae5e2..d35d880d4 100644 --- a/src/encoder/NullEncoderPlugin.cxx +++ b/src/encoder/NullEncoderPlugin.cxx @@ -20,21 +20,19 @@ #include "config.h" #include "NullEncoderPlugin.hxx" #include "EncoderAPI.hxx" -#include "util/fifo_buffer.h" -extern "C" { -#include "util/growing_fifo.h" -} +#include "util/Manual.hxx" +#include "util/DynamicFifoBuffer.hxx" #include "Compiler.h" #include <assert.h> -#include <string.h> struct NullEncoder final { Encoder encoder; - struct fifo_buffer *buffer; + Manual<DynamicFifoBuffer<uint8_t>> buffer; - NullEncoder():encoder(null_encoder_plugin) {} + NullEncoder() + :encoder(null_encoder_plugin) {} }; static Encoder * @@ -58,7 +56,7 @@ null_encoder_close(Encoder *_encoder) { NullEncoder *encoder = (NullEncoder *)_encoder; - fifo_buffer_free(encoder->buffer); + encoder->buffer.Destruct(); } @@ -68,7 +66,7 @@ null_encoder_open(Encoder *_encoder, gcc_unused Error &error) { NullEncoder *encoder = (NullEncoder *)_encoder; - encoder->buffer = growing_fifo_new(); + encoder->buffer.Construct(8192); return true; } @@ -79,7 +77,7 @@ null_encoder_write(Encoder *_encoder, { NullEncoder *encoder = (NullEncoder *)_encoder; - growing_fifo_append(&encoder->buffer, data, length); + encoder->buffer->Append((const uint8_t *)data, length); return length; } @@ -88,17 +86,7 @@ null_encoder_read(Encoder *_encoder, void *dest, size_t length) { NullEncoder *encoder = (NullEncoder *)_encoder; - size_t max_length; - const void *src = fifo_buffer_read(encoder->buffer, &max_length); - if (src == nullptr) - return 0; - - if (length > max_length) - length = max_length; - - memcpy(dest, src, length); - fifo_buffer_consume(encoder->buffer, length); - return length; + return encoder->buffer->Read((uint8_t *)dest, length); } const EncoderPlugin null_encoder_plugin = { diff --git a/src/encoder/VorbisEncoderPlugin.cxx b/src/encoder/VorbisEncoderPlugin.cxx index 5b40aaea1..830910288 100644 --- a/src/encoder/VorbisEncoderPlugin.cxx +++ b/src/encoder/VorbisEncoderPlugin.cxx @@ -33,8 +33,6 @@ #include <glib.h> -#include <assert.h> - struct vorbis_encoder { /** the base class */ Encoder encoder; diff --git a/src/encoder/WaveEncoderPlugin.cxx b/src/encoder/WaveEncoderPlugin.cxx index acae0be9e..083d25f95 100644 --- a/src/encoder/WaveEncoderPlugin.cxx +++ b/src/encoder/WaveEncoderPlugin.cxx @@ -21,10 +21,8 @@ #include "WaveEncoderPlugin.hxx" #include "EncoderAPI.hxx" #include "system/ByteOrder.hxx" -#include "util/fifo_buffer.h" -extern "C" { -#include "util/growing_fifo.h" -} +#include "util/Manual.hxx" +#include "util/DynamicFifoBuffer.hxx" #include <assert.h> #include <string.h> @@ -33,7 +31,7 @@ struct WaveEncoder { Encoder encoder; unsigned bits; - struct fifo_buffer *buffer; + Manual<DynamicFifoBuffer<uint8_t>> buffer; WaveEncoder():encoder(wave_encoder_plugin) {} }; @@ -128,9 +126,11 @@ wave_encoder_open(Encoder *_encoder, break; } - encoder->buffer = growing_fifo_new(); - wave_header *header = (wave_header *) - growing_fifo_write(&encoder->buffer, sizeof(*header)); + encoder->buffer.Construct(8192); + + auto range = encoder->buffer->Write(); + assert(range.size >= sizeof(wave_header)); + wave_header *header = (wave_header *)range.data; /* create PCM wave header in initial buffer */ fill_wave_header(header, @@ -138,7 +138,8 @@ wave_encoder_open(Encoder *_encoder, encoder->bits, audio_format.sample_rate, (encoder->bits / 8) * audio_format.channels); - fifo_buffer_append(encoder->buffer, sizeof(*header)); + + encoder->buffer->Append(sizeof(*header)); return true; } @@ -148,7 +149,7 @@ wave_encoder_close(Encoder *_encoder) { WaveEncoder *encoder = (WaveEncoder *)_encoder; - fifo_buffer_free(encoder->buffer); + encoder->buffer.Destruct(); } static size_t @@ -198,7 +199,7 @@ wave_encoder_write(Encoder *_encoder, { WaveEncoder *encoder = (WaveEncoder *)_encoder; - uint8_t *dst = (uint8_t *)growing_fifo_write(&encoder->buffer, length); + uint8_t *dst = encoder->buffer->Write(length); if (IsLittleEndian()) { switch (encoder->bits) { @@ -230,7 +231,7 @@ wave_encoder_write(Encoder *_encoder, } } - fifo_buffer_append(encoder->buffer, length); + encoder->buffer->Append(length); return true; } @@ -239,17 +240,7 @@ wave_encoder_read(Encoder *_encoder, void *dest, size_t length) { WaveEncoder *encoder = (WaveEncoder *)_encoder; - size_t max_length; - const void *src = fifo_buffer_read(encoder->buffer, &max_length); - if (src == NULL) - return 0; - - if (length > max_length) - length = max_length; - - memcpy(dest, src, length); - fifo_buffer_consume(encoder->buffer, length); - return length; + return encoder->buffer->Read((uint8_t *)dest, length); } static const char * diff --git a/src/event/BufferedSocket.cxx b/src/event/BufferedSocket.cxx index 92e350e85..0f71bd941 100644 --- a/src/event/BufferedSocket.cxx +++ b/src/event/BufferedSocket.cxx @@ -22,6 +22,9 @@ #include "system/SocketError.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" +#include "Compiler.h" + +#include <algorithm> BufferedSocket::ssize_t BufferedSocket::DirectRead(void *data, size_t length) diff --git a/src/event/BufferedSocket.hxx b/src/event/BufferedSocket.hxx index db920f981..c31d7953e 100644 --- a/src/event/BufferedSocket.hxx +++ b/src/event/BufferedSocket.hxx @@ -23,13 +23,12 @@ #include "check.h" #include "SocketMonitor.hxx" #include "util/FifoBuffer.hxx" -#include "Compiler.h" #include <assert.h> #include <stdint.h> -struct fifo_buffer; class Error; +class EventLoop; /** * A #SocketMonitor specialization that adds an input buffer. diff --git a/src/event/Call.cxx b/src/event/Call.cxx index ab1d5ffbd..7767824f9 100644 --- a/src/event/Call.cxx +++ b/src/event/Call.cxx @@ -28,7 +28,7 @@ #include <assert.h> class BlockingCallMonitor final -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP : DeferredMonitor #endif { @@ -40,20 +40,22 @@ class BlockingCallMonitor final bool done; public: -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP BlockingCallMonitor(EventLoop &loop, std::function<void()> &&_f) :f(std::move(_f)), done(false) { loop.AddCall([this](){ this->DoRun(); }); } -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP BlockingCallMonitor(EventLoop &_loop, std::function<void()> &&_f) :DeferredMonitor(_loop), f(std::move(_f)), done(false) {} #endif void Run() { -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP assert(!done); Schedule(); @@ -65,13 +67,14 @@ public: mutex.unlock(); } -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP private: virtual void RunDeferred() override { DoRun(); } +#endif -#else +#ifdef USE_INTERNAL_EVENTLOOP public: #endif void DoRun() { diff --git a/src/event/DeferredMonitor.cxx b/src/event/DeferredMonitor.cxx index 4ffffaa89..40b4b0b62 100644 --- a/src/event/DeferredMonitor.cxx +++ b/src/event/DeferredMonitor.cxx @@ -24,9 +24,10 @@ void DeferredMonitor::Cancel() { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP pending = false; -#else +#endif +#ifdef USE_GLIB_EVENTLOOP const auto id = source_id.exchange(0); if (id != 0) g_source_remove(id); @@ -36,10 +37,11 @@ DeferredMonitor::Cancel() void DeferredMonitor::Schedule() { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP if (!pending.exchange(true)) fd.Write(); -#else +#endif +#ifdef USE_GLIB_EVENTLOOP const unsigned id = loop.AddIdle(Callback, this); const auto old_id = source_id.exchange(id); if (old_id != 0) @@ -47,7 +49,7 @@ DeferredMonitor::Schedule() #endif } -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP bool DeferredMonitor::OnSocketReady(unsigned) @@ -60,7 +62,9 @@ DeferredMonitor::OnSocketReady(unsigned) return true; } -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP void DeferredMonitor::Run() diff --git a/src/event/DeferredMonitor.hxx b/src/event/DeferredMonitor.hxx index 2380fb66f..d4c812e44 100644 --- a/src/event/DeferredMonitor.hxx +++ b/src/event/DeferredMonitor.hxx @@ -23,10 +23,12 @@ #include "check.h" #include "Compiler.h" -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP #include "SocketMonitor.hxx" #include "WakeFD.hxx" -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP #include <glib.h> #endif @@ -38,44 +40,51 @@ class EventLoop; * Defer execution of an event into an #EventLoop. */ class DeferredMonitor -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP : private SocketMonitor #endif { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP std::atomic_bool pending; WakeFD fd; -#else - EventLoop &loop; +#endif +#ifdef USE_GLIB_EVENTLOOP + EventLoop &loop; std::atomic<guint> source_id; #endif public: -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP DeferredMonitor(EventLoop &_loop) :SocketMonitor(_loop), pending(false) { SocketMonitor::Open(fd.Get()); SocketMonitor::Schedule(SocketMonitor::READ); } -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP DeferredMonitor(EventLoop &_loop) :loop(_loop), source_id(0) {} #endif ~DeferredMonitor() { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP /* avoid closing the WakeFD twice */ SocketMonitor::Steal(); -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP Cancel(); #endif } EventLoop &GetEventLoop() { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP return SocketMonitor::GetEventLoop(); -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP return loop; #endif } @@ -87,9 +96,11 @@ protected: virtual void RunDeferred() = 0; private: -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP virtual bool OnSocketReady(unsigned flags) override final; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP void Run(); static gboolean Callback(gpointer data); #endif diff --git a/src/event/FullyBufferedSocket.cxx b/src/event/FullyBufferedSocket.cxx index 8b57b1308..375132e34 100644 --- a/src/event/FullyBufferedSocket.cxx +++ b/src/event/FullyBufferedSocket.cxx @@ -20,9 +20,9 @@ #include "config.h" #include "FullyBufferedSocket.hxx" #include "system/SocketError.hxx" -#include "util/fifo_buffer.h" #include "util/Error.hxx" #include "util/Domain.hxx" +#include "Compiler.h" #include <assert.h> #include <stdint.h> @@ -59,15 +59,14 @@ FullyBufferedSocket::Flush() { assert(IsDefined()); - size_t length; - const void *data = output.Read(&length); - if (data == nullptr) { + const auto data = output.Read(); + if (data.IsEmpty()) { IdleMonitor::Cancel(); CancelWrite(); return true; } - auto nbytes = DirectWrite(data, length); + auto nbytes = DirectWrite(data.data, data.size); if (gcc_unlikely(nbytes <= 0)) return nbytes == 0; diff --git a/src/event/FullyBufferedSocket.hxx b/src/event/FullyBufferedSocket.hxx index c50bb5f61..083fab15e 100644 --- a/src/event/FullyBufferedSocket.hxx +++ b/src/event/FullyBufferedSocket.hxx @@ -24,7 +24,6 @@ #include "BufferedSocket.hxx" #include "IdleMonitor.hxx" #include "util/PeakBuffer.hxx" -#include "Compiler.h" /** * A #BufferedSocket specialization that adds an output buffer. diff --git a/src/event/IdleMonitor.cxx b/src/event/IdleMonitor.cxx index c99c66b26..45f533e3f 100644 --- a/src/event/IdleMonitor.cxx +++ b/src/event/IdleMonitor.cxx @@ -21,6 +21,8 @@ #include "IdleMonitor.hxx" #include "Loop.hxx" +#include <assert.h> + void IdleMonitor::Cancel() { @@ -29,10 +31,11 @@ IdleMonitor::Cancel() if (!IsActive()) return; -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP active = false; loop.RemoveIdle(*this); -#else +#endif +#ifdef USE_GLIB_EVENTLOOP g_source_remove(source_id); source_id = 0; #endif @@ -47,10 +50,11 @@ IdleMonitor::Schedule() /* already scheduled */ return; -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP active = true; loop.AddIdle(*this); -#else +#endif +#ifdef USE_GLIB_EVENTLOOP source_id = loop.AddIdle(Callback, this); #endif } @@ -60,10 +64,11 @@ IdleMonitor::Run() { assert(loop.IsInside()); -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP assert(active); active = false; -#else +#endif +#ifdef USE_GLIB_EVENTLOOP assert(source_id != 0); source_id = 0; #endif @@ -71,7 +76,7 @@ IdleMonitor::Run() OnIdle(); } -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP gboolean IdleMonitor::Callback(gpointer data) diff --git a/src/event/IdleMonitor.hxx b/src/event/IdleMonitor.hxx index c8e79eb1d..b040915e7 100644 --- a/src/event/IdleMonitor.hxx +++ b/src/event/IdleMonitor.hxx @@ -22,7 +22,7 @@ #include "check.h" -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP #include <glib.h> #endif @@ -34,23 +34,27 @@ class EventLoop; * methods must be run from EventLoop's thread. */ class IdleMonitor { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP friend class EventLoop; #endif EventLoop &loop; -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP bool active; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP guint source_id; #endif public: -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP IdleMonitor(EventLoop &_loop) :loop(_loop), active(false) {} -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP IdleMonitor(EventLoop &_loop) :loop(_loop), source_id(0) {} #endif @@ -64,9 +68,11 @@ public: } bool IsActive() const { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP return active; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP return source_id != 0; #endif } @@ -79,7 +85,7 @@ protected: private: void Run(); -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP static gboolean Callback(gpointer data); #endif }; diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index 5aa24aea2..f7b3df022 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -20,7 +20,7 @@ #include "config.h" #include "Loop.hxx" -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP #include "system/Clock.hxx" #include "TimeoutMonitor.hxx" @@ -33,7 +33,6 @@ EventLoop::EventLoop(Default) :SocketMonitor(*this), now_ms(::MonotonicClockMS()), quit(false), - n_events(0), thread(ThreadId::Null()) { SocketMonitor::Open(wake_fd.Get()); @@ -58,19 +57,18 @@ EventLoop::Break() AddCall([this]() { Break(); }); } -void -EventLoop::Abandon(SocketMonitor &m) +bool +EventLoop::Abandon(int _fd, SocketMonitor &m) { - for (unsigned i = 0, n = n_events; i < n; ++i) - if (events[i].data.ptr == &m) - events[i].events = 0; + poll_result.Clear(&m); + return poll_group.Abandon(_fd); } bool EventLoop::RemoveFD(int _fd, SocketMonitor &m) { - Abandon(m); - return epoll.Remove(_fd); + poll_result.Clear(&m); + return poll_group.Remove(_fd); } void @@ -115,7 +113,7 @@ EventLoop::Run() assert(thread.IsNull()); thread = ThreadId::GetCurrent(); -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP assert(!quit); do { @@ -164,37 +162,37 @@ EventLoop::Run() /* wait for new event */ - const int n = epoll.Wait(events, MAX_EVENTS, timeout_ms); - n_events = std::max(n, 0); + poll_group.ReadEvents(poll_result, timeout_ms); now_ms = ::MonotonicClockMS(); assert(!quit); /* invoke sockets */ - - for (int i = 0; i < n; ++i) { - const auto &e = events[i]; - - if (e.events != 0) { - SocketMonitor &m = *(SocketMonitor *)e.data.ptr; - m.Dispatch(e.events); + for (int i = 0; i < poll_result.GetSize(); ++i) { + auto events = poll_result.GetEvents(i); + if (events != 0) { + auto m = (SocketMonitor *)poll_result.GetObject(i); + m->Dispatch(events); if (quit) break; } } - n_events = 0; + poll_result.Reset(); + } while (!quit); -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP g_main_loop_run(loop); #endif assert(thread.IsInside()); } -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP void EventLoop::AddCall(std::function<void()> &&f) @@ -229,7 +227,9 @@ EventLoop::OnSocketReady(gcc_unused unsigned flags) return true; } -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP guint EventLoop::AddIdle(GSourceFunc function, gpointer data) diff --git a/src/event/Loop.hxx b/src/event/Loop.hxx index 62e733747..029d01245 100644 --- a/src/event/Loop.hxx +++ b/src/event/Loop.hxx @@ -24,8 +24,8 @@ #include "thread/Id.hxx" #include "Compiler.h" -#ifdef USE_EPOLL -#include "system/EPollFD.hxx" +#ifdef USE_INTERNAL_EVENTLOOP +#include "PollGroup.hxx" #include "thread/Mutex.hxx" #include "WakeFD.hxx" #include "SocketMonitor.hxx" @@ -33,11 +33,13 @@ #include <functional> #include <list> #include <set> -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP #include <glib.h> #endif -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP class TimeoutMonitor; class IdleMonitor; class SocketMonitor; @@ -45,12 +47,21 @@ class SocketMonitor; #include <assert.h> +/** + * An event loop that polls for events on file/socket descriptors. + * + * This class is not thread-safe, all methods must be called from the + * thread that runs it, except where explicitly documented as + * thread-safe. + * + * @see SocketMonitor, MultiSocketMonitor, TimeoutMonitor, IdleMonitor + */ class EventLoop final -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP : private SocketMonitor #endif { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP struct TimerRecord { /** * Projected monotonic_clock_ms() value when this @@ -73,8 +84,6 @@ class EventLoop final } }; - EPollFD epoll; - WakeFD wake_fd; std::multiset<TimerRecord> timers; @@ -87,10 +96,11 @@ class EventLoop final bool quit; - static constexpr unsigned MAX_EVENTS = 16; - unsigned n_events; - epoll_event events[MAX_EVENTS]; -#else + PollGroup poll_group; + PollResult poll_result; +#endif + +#ifdef USE_GLIB_EVENTLOOP GMainContext *context; GMainLoop *loop; #endif @@ -101,24 +111,32 @@ class EventLoop final ThreadId thread; public: -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP struct Default {}; EventLoop(Default dummy=Default()); ~EventLoop(); + /** + * A caching wrapper for MonotonicClockMS(). + */ unsigned GetTimeMS() const { return now_ms; } + /** + * Stop execution of this #EventLoop at the next chance. This + * method is thread-safe and non-blocking: after returning, it + * is not guaranteed that the EventLoop has really stopped. + */ void Break(); bool AddFD(int _fd, unsigned flags, SocketMonitor &m) { - return epoll.Add(_fd, flags, &m); + return poll_group.Add(_fd, flags, &m); } bool ModifyFD(int _fd, unsigned flags, SocketMonitor &m) { - return epoll.Modify(_fd, flags, &m); + return poll_group.Modify(_fd, flags, &m); } /** @@ -126,7 +144,7 @@ public: * has been closed. This is like RemoveFD(), but does not * attempt to use #EPOLL_CTL_DEL. */ - void Abandon(SocketMonitor &m); + bool Abandon(int fd, SocketMonitor &m); bool RemoveFD(int fd, SocketMonitor &m); @@ -138,13 +156,19 @@ public: void AddCall(std::function<void()> &&f); + /** + * The main function of this class. It will loop until + * Break() gets called. Can be called only once. + */ void Run(); private: virtual bool OnSocketReady(unsigned flags) override; public: -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP EventLoop() :context(g_main_context_new()), loop(g_main_loop_new(context, false)), diff --git a/src/event/MultiSocketMonitor.cxx b/src/event/MultiSocketMonitor.cxx index bd1aa6fef..f26ab6c28 100644 --- a/src/event/MultiSocketMonitor.cxx +++ b/src/event/MultiSocketMonitor.cxx @@ -25,7 +25,7 @@ #include <assert.h> -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP MultiSocketMonitor::MultiSocketMonitor(EventLoop &_loop) :IdleMonitor(_loop), TimeoutMonitor(_loop), ready(false) { @@ -65,7 +65,9 @@ MultiSocketMonitor::OnIdle() } } -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP /** * The vtable for our GSource implementation. Unfortunately, we diff --git a/src/event/MultiSocketMonitor.hxx b/src/event/MultiSocketMonitor.hxx index 8ee81a508..680930037 100644 --- a/src/event/MultiSocketMonitor.hxx +++ b/src/event/MultiSocketMonitor.hxx @@ -23,22 +23,25 @@ #include "check.h" #include "Compiler.h" -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP #include "IdleMonitor.hxx" #include "TimeoutMonitor.hxx" #include "SocketMonitor.hxx" -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP #include <glib.h> #endif #include <forward_list> +#include <iterator> #include <assert.h> #include <stdint.h> #ifdef WIN32 -/* ERRORis a WIN32 macro that poisons our namespace; this is a - kludge to allow us to use it anyway */ +/* ERROR is a WIN32 macro that poisons our namespace; this is a kludge + to allow us to use it anyway */ #ifdef ERROR #undef ERROR #endif @@ -47,14 +50,17 @@ class EventLoop; /** - * Monitor multiple sockets. + * Similar to #SocketMonitor, but monitors multiple sockets. To use + * it, implement the methods PrepareSockets() and DispatchSockets(). + * In PrepareSockets(), use UpdateSocketList() and AddSocket(). + * DispatchSockets() will be called if at least one socket is ready. */ class MultiSocketMonitor -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP : private IdleMonitor, private TimeoutMonitor #endif { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP class SingleFD final : public SocketMonitor { MultiSocketMonitor &multi; @@ -99,7 +105,9 @@ class MultiSocketMonitor friend class SingleFD; bool ready, refresh; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP struct Source { GSource base; @@ -138,12 +146,14 @@ class MultiSocketMonitor std::forward_list<SingleFD> fds; public: -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP static constexpr unsigned READ = SocketMonitor::READ; static constexpr unsigned WRITE = SocketMonitor::WRITE; static constexpr unsigned ERROR = SocketMonitor::ERROR; static constexpr unsigned HANGUP = SocketMonitor::HANGUP; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP static constexpr unsigned READ = G_IO_IN; static constexpr unsigned WRITE = G_IO_OUT; static constexpr unsigned ERROR = G_IO_ERR; @@ -153,16 +163,18 @@ public: MultiSocketMonitor(EventLoop &_loop); ~MultiSocketMonitor(); -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP using IdleMonitor::GetEventLoop; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP EventLoop &GetEventLoop() { return loop; } #endif public: -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP gcc_pure uint64_t GetTime() const { return g_source_get_time(&source->base); @@ -170,10 +182,12 @@ public: #endif void InvalidateSockets() { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP refresh = true; IdleMonitor::Schedule(); -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP /* no-op because GLib always calls the GSource's "prepare" method before each poll() anyway */ #endif @@ -181,7 +195,7 @@ public: void AddSocket(int fd, unsigned events) { fds.emplace_front(*this, fd, events); -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP g_source_add_poll(&source->base, &fds.front().pfd); #endif } @@ -198,9 +212,11 @@ public: i->SetEvents(events); prev = i; } else { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP i->Steal(); -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP g_source_remove_poll(&source->base, &i->pfd); #endif fds.erase_after(prev); @@ -215,7 +231,7 @@ protected: virtual int PrepareSockets() = 0; virtual void DispatchSockets() = 0; -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP private: void SetReady() { ready = true; @@ -231,7 +247,9 @@ private: virtual void OnIdle() final; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP public: /* GSource callbacks */ static gboolean Prepare(GSource *source, gint *timeout_r); diff --git a/src/event/PollGroup.hxx b/src/event/PollGroup.hxx new file mode 100644 index 000000000..038ffc13e --- /dev/null +++ b/src/event/PollGroup.hxx @@ -0,0 +1,41 @@ +/* + * 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_EVENT_POLLGROUP_HXX +#define MPD_EVENT_POLLGROUP_HXX + +#ifdef USE_EPOLL +#include "PollGroupEPoll.hxx" +typedef PollResultEPoll PollResult; +typedef PollGroupEPoll PollGroup; +#endif + +#ifdef USE_WINSELECT +#include "PollGroupWinSelect.hxx" +typedef PollResultGeneric PollResult; +typedef PollGroupWinSelect PollGroup; +#endif + +#ifdef USE_POLL +#include "PollGroupPoll.hxx" +typedef PollResultGeneric PollResult; +typedef PollGroupPoll PollGroup; +#endif + +#endif diff --git a/src/event/PollGroupEPoll.hxx b/src/event/PollGroupEPoll.hxx new file mode 100644 index 000000000..21bb3a322 --- /dev/null +++ b/src/event/PollGroupEPoll.hxx @@ -0,0 +1,91 @@ +/* + * 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_EVENT_POLLGROUP_EPOLL_HXX +#define MPD_EVENT_POLLGROUP_EPOLL_HXX + +#include "check.h" + +#include "Compiler.h" +#include "system/EPollFD.hxx" + +#include <array> +#include <algorithm> + +class PollResultEPoll +{ + friend class PollGroupEPoll; + + std::array<epoll_event, 16> events; + int n_events; +public: + PollResultEPoll() : n_events(0) { } + + int GetSize() const { return n_events; } + unsigned GetEvents(int i) const { return events[i].events; } + void *GetObject(int i) const { return events[i].data.ptr; } + void Reset() { n_events = 0; } + + void Clear(void *obj) { + for (int i = 0; i < n_events; ++i) + if (events[i].data.ptr == obj) + events[i].events = 0; + } +}; + +class PollGroupEPoll +{ + EPollFD epoll; + + PollGroupEPoll(PollGroupEPoll &) = delete; + PollGroupEPoll &operator=(PollGroupEPoll &) = delete; +public: + static constexpr unsigned READ = EPOLLIN; + static constexpr unsigned WRITE = EPOLLOUT; + static constexpr unsigned ERROR = EPOLLERR; + static constexpr unsigned HANGUP = EPOLLHUP; + + PollGroupEPoll() = default; + + void ReadEvents(PollResultEPoll &result, int timeout_ms) { + int ret = epoll.Wait(result.events.data(), result.events.size(), + timeout_ms); + result.n_events = std::max(0, ret); + } + + bool Add(int fd, unsigned events, void *obj) { + return epoll.Add(fd, events, obj); + } + + bool Modify(int fd, unsigned events, void *obj) { + return epoll.Modify(fd, events, obj); + } + + bool Remove(int fd) { + return epoll.Remove(fd); + } + + bool Abandon(gcc_unused int fd) { + // Nothing to do in this implementation. + // Closed descriptors are automatically unregistered. + return true; + } +}; + +#endif diff --git a/src/event/PollGroupPoll.cxx b/src/event/PollGroupPoll.cxx new file mode 100644 index 000000000..a2b4d1d88 --- /dev/null +++ b/src/event/PollGroupPoll.cxx @@ -0,0 +1,88 @@ +/* + * 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" + +#ifdef USE_POLL + +#include "PollGroupPoll.hxx" + +#include <assert.h> + +PollGroupPoll::PollGroupPoll() { } +PollGroupPoll::~PollGroupPoll() { } + +bool PollGroupPoll::Add(int fd, unsigned events, void *obj) +{ + assert(items.find(fd) == items.end()); + poll_events.resize(poll_events.size() + 1); + size_t index = poll_events.size() - 1; + auto &e = poll_events[index]; + e.fd = fd; + e.events = events; + e.revents = 0; + auto &item = items[fd]; + item.index = index; + item.obj = obj; + return true; +} + +bool PollGroupPoll::Modify(int fd, unsigned events, void *obj) +{ + auto item_iter = items.find(fd); + assert(item_iter != items.end()); + auto &item = item_iter->second; + item.obj = obj; + auto &e = poll_events[item.index]; + e.events = events; + e.revents &= events; + return true; +} + +bool PollGroupPoll::Remove(int fd) +{ + auto item_iter = items.find(fd); + assert(item_iter != items.end()); + auto &item = item_iter->second; + size_t index = item.index; + size_t last_index = poll_events.size() - 1; + if (index != last_index) { + std::swap(poll_events[index], poll_events[last_index]); + items[poll_events[index].fd].index = index; + } + poll_events.pop_back(); + items.erase(item_iter); + return true; +} + +void PollGroupPoll::ReadEvents(PollResultGeneric &result, int timeout_ms) +{ + int n = poll(poll_events.empty() ? nullptr : &poll_events[0], + poll_events.size(), timeout_ms); + + for (size_t i = 0; n > 0 && i < poll_events.size(); ++i) { + const auto &e = poll_events[i]; + if (e.revents != 0) { + result.Add(e.revents, items[e.fd].obj); + --n; + } + } +} + +#endif diff --git a/src/event/PollGroupPoll.hxx b/src/event/PollGroupPoll.hxx new file mode 100644 index 000000000..6c69787ac --- /dev/null +++ b/src/event/PollGroupPoll.hxx @@ -0,0 +1,63 @@ +/* + * 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_EVENT_POLLGROUP_POLL_HXX +#define MPD_EVENT_POLLGROUP_POLL_HXX + +#include "check.h" +#include "PollResultGeneric.hxx" + +#include <vector> +#include <unordered_map> + +#include <stddef.h> +#include <sys/poll.h> + +class PollGroupPoll +{ + struct Item + { + size_t index; + void *obj; + }; + + std::vector<pollfd> poll_events; + std::unordered_map<int, Item> items; + + PollGroupPoll(PollGroupPoll &) = delete; + PollGroupPoll &operator=(PollGroupPoll &) = delete; +public: + static constexpr unsigned READ = POLLIN; + static constexpr unsigned WRITE = POLLOUT; + static constexpr unsigned ERROR = POLLERR; + static constexpr unsigned HANGUP = POLLHUP; + + PollGroupPoll(); + ~PollGroupPoll(); + + void ReadEvents(PollResultGeneric &result, int timeout_ms); + bool Add(int fd, unsigned events, void *obj); + bool Modify(int fd, unsigned events, void *obj); + bool Remove(int fd); + bool Abandon(int fd) { + return Remove(fd); + } +}; + +#endif diff --git a/src/event/PollGroupWinSelect.cxx b/src/event/PollGroupWinSelect.cxx new file mode 100644 index 000000000..b184ff2b2 --- /dev/null +++ b/src/event/PollGroupWinSelect.cxx @@ -0,0 +1,161 @@ +/* + * 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" + +#ifdef USE_WINSELECT + +#include "PollGroupWinSelect.hxx" + +constexpr int EVENT_READ = 0; +constexpr int EVENT_WRITE = 1; + +static inline bool HasEvent(unsigned events, int event_id) +{ + return (events & (1 << event_id)) != 0; +} + +PollGroupWinSelect::PollGroupWinSelect() { } +PollGroupWinSelect::~PollGroupWinSelect() { } + +bool PollGroupWinSelect::CanModify(PollGroupWinSelect::Item &item, + unsigned events, int event_id) +{ + if (item.index[event_id] < 0 && HasEvent(events, event_id)) + return !event_set[event_id].IsFull(); + return true; +} + +void PollGroupWinSelect::Modify(PollGroupWinSelect::Item &item, int fd, + unsigned events, int event_id) +{ + int index = item.index[event_id]; + auto &set = event_set[event_id]; + + if (index < 0 && HasEvent(events, event_id)) + item.index[event_id] = set.Add(fd); + else if (index >= 0 && !HasEvent(events, event_id)) { + if (index != set.Size() - 1) { + set.MoveToEnd(index); + items[set[index]].index[event_id] = index; + } + set.RemoveLast(); + item.index[event_id] = -1; + } +} + +bool PollGroupWinSelect::Add(int fd, unsigned events, void *obj) +{ + assert(items.find(fd) == items.end()); + auto &item = items[fd]; + + item.index[EVENT_READ] = -1; + item.index[EVENT_WRITE] = -1; + item.obj = obj; + item.events = 0; + + if (!CanModify(item, events, EVENT_READ)) { + items.erase(fd); + return false; + } + if (!CanModify(item, events, EVENT_WRITE)) { + items.erase(fd); + return false; + } + + Modify(item, fd, events, EVENT_READ); + Modify(item, fd, events, EVENT_WRITE); + return true; +} + +bool PollGroupWinSelect::Modify(int fd, unsigned events, void *obj) +{ + auto item_iter = items.find(fd); + assert(item_iter != items.end()); + auto &item = item_iter->second; + + if (!CanModify(item, events, EVENT_READ)) + return false; + if (!CanModify(item, events, EVENT_WRITE)) + return false; + + item.obj = obj; + Modify(item, fd, events, EVENT_READ); + Modify(item, fd, events, EVENT_WRITE); + return true; +} + +bool PollGroupWinSelect::Remove(int fd) +{ + auto item_iter = items.find(fd); + assert(item_iter != items.end()); + auto &item = item_iter->second; + + Modify(item, fd, 0, EVENT_READ); + Modify(item, fd, 0, EVENT_WRITE); + items.erase(item_iter); + return true; +} + +void PollGroupWinSelect::ReadEvents(PollResultGeneric &result, int timeout_ms) +{ + bool use_sleep = event_set[EVENT_READ].IsEmpty() && + event_set[EVENT_WRITE].IsEmpty(); + + if (use_sleep) { + Sleep(timeout_ms < 0 ? INFINITE : (DWORD) timeout_ms); + return; + } + + SocketSet read_set(event_set[EVENT_READ]); + SocketSet write_set(event_set[EVENT_WRITE]); + SocketSet except_set(event_set[EVENT_WRITE]); + + timeval tv; + if (timeout_ms >= 0) { + tv.tv_sec = timeout_ms / 1000; + tv.tv_usec = (timeout_ms % 1000) * 1000; + } + + int ret = select(0, + read_set.IsEmpty() ? nullptr : read_set.GetPtr(), + write_set.IsEmpty() ? nullptr : write_set.GetPtr(), + except_set.IsEmpty() ? nullptr : except_set.GetPtr(), + timeout_ms < 0 ? nullptr : &tv); + + if (ret == 0 || ret == SOCKET_ERROR) + return; + + for (int i = 0; i < read_set.Size(); ++i) + items[read_set[i]].events |= READ; + + for (int i = 0; i < write_set.Size(); ++i) + items[write_set[i]].events |= WRITE; + + for (int i = 0; i < except_set.Size(); ++i) + items[except_set[i]].events |= WRITE; + + for (auto i = items.begin(); i != items.end(); ++i) + if (i->second.events != 0) { + result.Add(i->second.events, i->second.obj); + i->second.events = 0; + } +} + +#endif diff --git a/src/event/PollGroupWinSelect.hxx b/src/event/PollGroupWinSelect.hxx new file mode 100644 index 000000000..7a6d12d8a --- /dev/null +++ b/src/event/PollGroupWinSelect.hxx @@ -0,0 +1,111 @@ +/* + * 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_EVENT_POLLGROUP_WINSELECT_HXX +#define MPD_EVENT_POLLGROUP_WINSELECT_HXX + +#include "check.h" + +#include "PollResultGeneric.hxx" + +#include <assert.h> +#include <string.h> + +#include <unordered_map> + +#include <windows.h> +#include <winsock2.h> + +#ifdef ERROR +#undef ERROR +#endif + +class SocketSet +{ + fd_set set; +public: + SocketSet() { set.fd_count = 0; } + SocketSet(SocketSet &other) { + set.fd_count = other.set.fd_count; + memcpy(set.fd_array, + other.set.fd_array, + sizeof (SOCKET) * set.fd_count); + } + + fd_set *GetPtr() { return &set; } + int Size() { return set.fd_count; } + bool IsEmpty() { return set.fd_count == 0; } + bool IsFull() { return set.fd_count == FD_SETSIZE; } + + int operator[](int index) { + assert(index >= 0 && (u_int)index < set.fd_count); + return set.fd_array[index]; + } + + int Add(int fd) { + assert(!IsFull()); + set.fd_array[set.fd_count] = fd; + return set.fd_count++; + } + + void MoveToEnd(int index) { + assert(index >= 0 && (u_int)index < set.fd_count); + std::swap(set.fd_array[index], set.fd_array[set.fd_count - 1]); + } + + void RemoveLast() { + assert(!IsEmpty()); + --set.fd_count; + } +}; + +class PollGroupWinSelect +{ + struct Item + { + int index[2]; + void *obj; + unsigned events; + }; + + SocketSet event_set[2]; + std::unordered_map<int, Item> items; + + bool CanModify(Item &item, unsigned events, int event_id); + void Modify(Item &item, int fd, unsigned events, int event_id); + + PollGroupWinSelect(PollGroupWinSelect &) = delete; + PollGroupWinSelect &operator=(PollGroupWinSelect &) = delete; +public: + static constexpr unsigned READ = 1; + static constexpr unsigned WRITE = 2; + static constexpr unsigned ERROR = 0; + static constexpr unsigned HANGUP = 0; + + PollGroupWinSelect(); + ~PollGroupWinSelect(); + + void ReadEvents(PollResultGeneric &result, int timeout_ms); + bool Add(int fd, unsigned events, void *obj); + bool Modify(int fd, unsigned events, void *obj); + bool Remove(int fd); + bool Abandon(int fd) { return Remove(fd); } +}; + +#endif diff --git a/src/event/PollResultGeneric.hxx b/src/event/PollResultGeneric.hxx new file mode 100644 index 000000000..1c2c0d00b --- /dev/null +++ b/src/event/PollResultGeneric.hxx @@ -0,0 +1,57 @@ +/* + * 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_EVENT_POLLRESULT_GENERIC_HXX +#define MPD_EVENT_POLLRESULT_GENERIC_HXX + +#include "check.h" + +#include <vector> + +class PollResultGeneric +{ + struct Item + { + unsigned events; + void *obj; + + Item() = default; + Item(unsigned _events, void *_obj) + : events(_events), obj(_obj) { } + }; + + std::vector<Item> items; +public: + int GetSize() const { return items.size(); } + unsigned GetEvents(int i) const { return items[i].events; } + void *GetObject(int i) const { return items[i].obj; } + void Reset() { items.clear(); } + + void Clear(void *obj) { + for (auto i = items.begin(); i != items.end(); ++i) + if (i->obj == obj) + i->events = 0; + } + + void Add(unsigned events, void *obj) { + items.emplace_back(events, obj); + } +}; + +#endif diff --git a/src/event/ServerSocket.cxx b/src/event/ServerSocket.cxx index 781d29181..a05d7bde2 100644 --- a/src/event/ServerSocket.cxx +++ b/src/event/ServerSocket.cxx @@ -38,6 +38,7 @@ #include <glib.h> #include <string> +#include <algorithm> #include <sys/types.h> #include <sys/stat.h> @@ -106,7 +107,10 @@ public: using SocketMonitor::IsDefined; using SocketMonitor::Close; - char *ToString() const; + gcc_pure + std::string ToString() const { + return sockaddr_to_string(address, address_length); + } void SetFD(int _fd) { SocketMonitor::Open(_fd); @@ -121,18 +125,6 @@ private: static constexpr Domain server_socket_domain("server_socket"); -/** - * Wraper for sockaddr_to_string() which never fails. - */ -char * -OneServerSocket::ToString() const -{ - char *p = sockaddr_to_string(address, address_length, IgnoreError()); - if (p == nullptr) - p = g_strdup("[unknown]"); - return p; -} - static int get_remote_uid(int fd) { @@ -242,23 +234,21 @@ ServerSocket::Open(Error &error) Error error2; if (!i.Open(error2)) { if (good != nullptr && good->GetSerial() == i.GetSerial()) { - char *address_string = i.ToString(); - char *good_string = good->ToString(); + const auto address_string = i.ToString(); + const auto good_string = good->ToString(); FormatWarning(server_socket_domain, "bind to '%s' failed: %s " "(continuing anyway, because " "binding to '%s' succeeded)", - address_string, error2.GetMessage(), - good_string); - g_free(address_string); - g_free(good_string); + address_string.c_str(), + error2.GetMessage(), + good_string.c_str()); } else if (bad == nullptr) { bad = &i; - char *address_string = i.ToString(); + const auto address_string = i.ToString(); error2.FormatPrefix("Failed to bind to '%s': ", - address_string); - g_free(address_string); + address_string.c_str()); last_error = std::move(error2); } diff --git a/src/event/ServerSocket.hxx b/src/event/ServerSocket.hxx index facb10371..05e13ff63 100644 --- a/src/event/ServerSocket.hxx +++ b/src/event/ServerSocket.hxx @@ -36,6 +36,9 @@ typedef void (*server_socket_callback_t)(int fd, class OneServerSocket; +/** + * A socket that accepts incoming stream connections (e.g. TCP). + */ class ServerSocket { friend class OneServerSocket; diff --git a/src/event/SignalMonitor.cxx b/src/event/SignalMonitor.cxx index 8c8527a77..bb75f49a3 100644 --- a/src/event/SignalMonitor.cxx +++ b/src/event/SignalMonitor.cxx @@ -39,6 +39,9 @@ #include <algorithm> +#include <assert.h> +#include <signal.h> + class SignalMonitor final : private SocketMonitor { #ifdef USE_SIGNALFD SignalFD fd; diff --git a/src/event/SocketMonitor.cxx b/src/event/SocketMonitor.cxx index 2b97059f7..769efba82 100644 --- a/src/event/SocketMonitor.cxx +++ b/src/event/SocketMonitor.cxx @@ -28,11 +28,10 @@ #ifdef WIN32 #include <winsock2.h> #else -#include <sys/types.h> #include <sys/socket.h> #endif -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP void SocketMonitor::Dispatch(unsigned flags) @@ -43,7 +42,9 @@ SocketMonitor::Dispatch(unsigned flags) Cancel(); } -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP /* * GSource methods @@ -113,14 +114,14 @@ void SocketMonitor::Open(int _fd) { assert(fd < 0); -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP assert(source == nullptr); #endif assert(_fd >= 0); fd = _fd; -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP poll = {fd, 0, 0}; source = (Source *)g_source_new(&socket_monitor_source_funcs, @@ -142,7 +143,7 @@ SocketMonitor::Steal() int result = fd; fd = -1; -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP g_source_destroy(&source->base); g_source_unref(&source->base); source = nullptr; @@ -156,9 +157,10 @@ SocketMonitor::Abandon() { assert(IsDefined()); -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP + int old_fd = fd; fd = -1; - loop.Abandon(*this); + loop.Abandon(old_fd, *this); #else Steal(); #endif @@ -178,7 +180,7 @@ SocketMonitor::Schedule(unsigned flags) if (flags == GetScheduledFlags()) return; -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP if (scheduled_flags == 0) loop.AddFD(fd, flags, *this); else if (flags == 0) @@ -187,7 +189,9 @@ SocketMonitor::Schedule(unsigned flags) loop.ModifyFD(fd, flags, *this); scheduled_flags = flags; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP poll.events = flags; poll.revents &= flags; diff --git a/src/event/SocketMonitor.hxx b/src/event/SocketMonitor.hxx index 5369ddb8a..f6aac2bd6 100644 --- a/src/event/SocketMonitor.hxx +++ b/src/event/SocketMonitor.hxx @@ -22,9 +22,11 @@ #include "check.h" -#ifdef USE_EPOLL -#include <sys/epoll.h> -#else +#ifdef USE_INTERNAL_EVENTLOOP +#include "PollGroup.hxx" +#endif + +#ifdef USE_GLIB_EVENTLOOP #include <glib.h> #endif @@ -34,8 +36,8 @@ #include <stddef.h> #ifdef WIN32 -/* ERRORis a WIN32 macro that poisons our namespace; this is a - kludge to allow us to use it anyway */ +/* ERROR is a WIN32 macro that poisons our namespace; this is a kludge + to allow us to use it anyway */ #ifdef ERROR #undef ERROR #endif @@ -43,9 +45,14 @@ class EventLoop; +/** + * Monitor events on a socket. Call Schedule() to announce events + * you're interested in, or Cancel() to cancel your subscription. The + * #EventLoop will invoke virtual method OnSocketReady() as soon as + * any of the subscribed events are ready. + */ class SocketMonitor { -#ifdef USE_EPOLL -#else +#ifdef USE_GLIB_EVENTLOOP struct Source { GSource base; @@ -56,38 +63,44 @@ class SocketMonitor { int fd; EventLoop &loop; -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP /** * A bit mask of events that is currently registered in the EventLoop. */ unsigned scheduled_flags; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP Source *source; GPollFD poll; #endif public: -#ifdef USE_EPOLL - static constexpr unsigned READ = EPOLLIN; - static constexpr unsigned WRITE = EPOLLOUT; - static constexpr unsigned ERROR = EPOLLERR; - static constexpr unsigned HANGUP = EPOLLHUP; -#else +#ifdef USE_INTERNAL_EVENTLOOP + static constexpr unsigned READ = PollGroup::READ; + static constexpr unsigned WRITE = PollGroup::WRITE; + static constexpr unsigned ERROR = PollGroup::ERROR; + static constexpr unsigned HANGUP = PollGroup::HANGUP; +#endif + +#ifdef USE_GLIB_EVENTLOOP static constexpr unsigned READ = G_IO_IN; static constexpr unsigned WRITE = G_IO_OUT; static constexpr unsigned ERROR = G_IO_ERR; static constexpr unsigned HANGUP = G_IO_HUP; -#endif +#endif typedef std::make_signed<size_t>::type ssize_t; -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP SocketMonitor(EventLoop &_loop) :fd(-1), loop(_loop), scheduled_flags(0) {} SocketMonitor(int _fd, EventLoop &_loop) :fd(_fd), loop(_loop), scheduled_flags(0) {} -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP SocketMonitor(EventLoop &_loop) :fd(-1), loop(_loop), source(nullptr) {} @@ -128,9 +141,11 @@ public: unsigned GetScheduledFlags() const { assert(IsDefined()); -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP return scheduled_flags; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP return poll.events; #endif } @@ -167,9 +182,11 @@ protected: virtual bool OnSocketReady(unsigned flags) = 0; public: -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP void Dispatch(unsigned flags); -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP /* GSource callbacks */ static gboolean Prepare(GSource *source, gint *timeout_r); static gboolean Check(GSource *source); diff --git a/src/event/TimeoutMonitor.cxx b/src/event/TimeoutMonitor.cxx index cffad6b92..3d7d46324 100644 --- a/src/event/TimeoutMonitor.cxx +++ b/src/event/TimeoutMonitor.cxx @@ -25,10 +25,12 @@ void TimeoutMonitor::Cancel() { if (IsActive()) { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP active = false; loop.CancelTimer(*this); -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP g_source_destroy(source); g_source_unref(source); source = nullptr; @@ -41,10 +43,12 @@ TimeoutMonitor::Schedule(unsigned ms) { Cancel(); -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP active = true; loop.AddTimer(*this, ms); -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP source = loop.AddTimeout(ms, Callback, this); #endif } @@ -54,9 +58,11 @@ TimeoutMonitor::ScheduleSeconds(unsigned s) { Cancel(); -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP Schedule(s * 1000u); -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP source = loop.AddTimeoutSeconds(s, Callback, this); #endif } @@ -64,14 +70,14 @@ TimeoutMonitor::ScheduleSeconds(unsigned s) void TimeoutMonitor::Run() { -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP Cancel(); #endif OnTimeout(); } -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP gboolean TimeoutMonitor::Callback(gpointer data) diff --git a/src/event/TimeoutMonitor.hxx b/src/event/TimeoutMonitor.hxx index 98e4e5564..568aa27ef 100644 --- a/src/event/TimeoutMonitor.hxx +++ b/src/event/TimeoutMonitor.hxx @@ -22,31 +22,39 @@ #include "check.h" -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP #include <glib.h> #endif class EventLoop; +/** + * This class monitors a timeout. Use Schedule() to begin the timeout + * or Cancel() to cancel it. + */ class TimeoutMonitor { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP friend class EventLoop; #endif EventLoop &loop; -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP bool active; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP GSource *source; #endif public: -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP TimeoutMonitor(EventLoop &_loop) :loop(_loop), active(false) { } -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP TimeoutMonitor(EventLoop &_loop) :loop(_loop), source(nullptr) {} #endif @@ -60,9 +68,11 @@ public: } bool IsActive() const { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP return active; -#else +#endif + +#ifdef USE_GLIB_EVENTLOOP return source != nullptr; #endif } @@ -77,7 +87,7 @@ protected: private: void Run(); -#ifndef USE_EPOLL +#ifdef USE_GLIB_EVENTLOOP static gboolean Callback(gpointer data); #endif }; diff --git a/src/filter/AutoConvertFilterPlugin.cxx b/src/filter/AutoConvertFilterPlugin.cxx index 918a16e53..44adc8a66 100644 --- a/src/filter/AutoConvertFilterPlugin.cxx +++ b/src/filter/AutoConvertFilterPlugin.cxx @@ -88,7 +88,11 @@ AutoConvertFilter::Open(AudioFormat &in_audio_format, Error &error) assert(audio_format2 == in_audio_format); - convert_filter_set(convert, child_audio_format); + if (!convert_filter_set(convert, child_audio_format, error)) { + delete convert; + filter->Close(); + return AudioFormat::Undefined(); + } } else /* no */ convert = nullptr; diff --git a/src/filter/ConvertFilterPlugin.cxx b/src/filter/ConvertFilterPlugin.cxx index 040f8426f..e718844e7 100644 --- a/src/filter/ConvertFilterPlugin.cxx +++ b/src/filter/ConvertFilterPlugin.cxx @@ -28,7 +28,6 @@ #include "poison.h" #include <assert.h> -#include <string.h> class ConvertFilter final : public Filter { /** @@ -39,21 +38,18 @@ class ConvertFilter final : public Filter { /** * The output audio format; the consumer of this plugin - * expects PCM data in this format. This defaults to - * #in_audio_format, and can be set with convert_filter_set(). + * expects PCM data in this format. + * + * If this is AudioFormat::Undefined(), then the #PcmConvert + * attribute is not open. This can mean that Set() has failed + * or that no conversion is necessary. */ AudioFormat out_audio_format; Manual<PcmConvert> state; public: - void Set(const AudioFormat &_out_audio_format) { - assert(in_audio_format.IsValid()); - assert(out_audio_format.IsValid()); - assert(_out_audio_format.IsValid()); - - out_audio_format = _out_audio_format; - } + bool Set(const AudioFormat &_out_audio_format, Error &error); virtual AudioFormat Open(AudioFormat &af, Error &error) override; virtual void Close() override; @@ -69,12 +65,40 @@ convert_filter_init(gcc_unused const config_param ¶m, return new ConvertFilter(); } +bool +ConvertFilter::Set(const AudioFormat &_out_audio_format, Error &error) +{ + assert(in_audio_format.IsValid()); + assert(_out_audio_format.IsValid()); + + if (_out_audio_format == out_audio_format) + /* no change */ + return true; + + if (out_audio_format.IsValid()) { + out_audio_format.Clear(); + state->Close(); + } + + if (_out_audio_format == in_audio_format) + /* optimized special case: no-op */ + return true; + + if (!state->Open(in_audio_format, _out_audio_format, error)) + return false; + + out_audio_format = _out_audio_format; + return true; +} + AudioFormat ConvertFilter::Open(AudioFormat &audio_format, gcc_unused Error &error) { assert(audio_format.IsValid()); - in_audio_format = out_audio_format = audio_format; + in_audio_format = audio_format; + out_audio_format.Clear(); + state.Construct(); return in_audio_format; @@ -83,6 +107,11 @@ ConvertFilter::Open(AudioFormat &audio_format, gcc_unused Error &error) void ConvertFilter::Close() { + assert(in_audio_format.IsValid()); + + if (out_audio_format.IsValid()) + state->Close(); + state.Destruct(); poison_undefined(&in_audio_format, sizeof(in_audio_format)); @@ -93,15 +122,15 @@ const void * ConvertFilter::FilterPCM(const void *src, size_t src_size, size_t *dest_size_r, Error &error) { - if (in_audio_format == out_audio_format) { + assert(in_audio_format.IsValid()); + + if (!out_audio_format.IsValid()) { /* optimized special case: no-op */ *dest_size_r = src_size; return src; } - return state->Convert(in_audio_format, - src, src_size, - out_audio_format, dest_size_r, + return state->Convert(src, src_size, dest_size_r, error); } @@ -110,10 +139,11 @@ const struct filter_plugin convert_filter_plugin = { convert_filter_init, }; -void -convert_filter_set(Filter *_filter, const AudioFormat out_audio_format) +bool +convert_filter_set(Filter *_filter, AudioFormat out_audio_format, + Error &error) { ConvertFilter *filter = (ConvertFilter *)_filter; - filter->Set(out_audio_format); + return filter->Set(out_audio_format, error); } diff --git a/src/filter/ConvertFilterPlugin.hxx b/src/filter/ConvertFilterPlugin.hxx index c814aaf49..f414e59bf 100644 --- a/src/filter/ConvertFilterPlugin.hxx +++ b/src/filter/ConvertFilterPlugin.hxx @@ -21,6 +21,7 @@ #define MPD_CONVERT_FILTER_PLUGIN_HXX class Filter; +class Error; struct AudioFormat; /** @@ -29,7 +30,8 @@ struct AudioFormat; * format switch is a violation of the filter API, this filter must be * the last in a chain. */ -void -convert_filter_set(Filter *filter, AudioFormat out_audio_format); +bool +convert_filter_set(Filter *filter, AudioFormat out_audio_format, + Error &error); #endif diff --git a/src/filter/NormalizeFilterPlugin.cxx b/src/filter/NormalizeFilterPlugin.cxx index 6c4f6b0e5..93cca9fef 100644 --- a/src/filter/NormalizeFilterPlugin.cxx +++ b/src/filter/NormalizeFilterPlugin.cxx @@ -25,7 +25,6 @@ #include "AudioFormat.hxx" #include "AudioCompress/compress.h" -#include <assert.h> #include <string.h> class NormalizeFilter final : public Filter { diff --git a/src/filter/ReplayGainFilterPlugin.cxx b/src/filter/ReplayGainFilterPlugin.cxx index b2dcde4cc..998fda6f7 100644 --- a/src/filter/ReplayGainFilterPlugin.cxx +++ b/src/filter/ReplayGainFilterPlugin.cxx @@ -26,8 +26,9 @@ #include "ReplayGainInfo.hxx" #include "ReplayGainConfig.hxx" #include "MixerControl.hxx" -#include "pcm/PcmVolume.hxx" +#include "pcm/Volume.hxx" #include "pcm/PcmBuffer.hxx" +#include "util/ConstBuffer.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" #include "Log.hxx" @@ -55,8 +56,8 @@ class ReplayGainFilter final : public Filter { ReplayGainInfo info; /** - * The current volume, between 0 and a value that may or may not exceed - * #PCM_VOLUME_1. + * About the current volume: it is between 0 and a value that + * may or may not exceed #PCM_VOLUME_1. * * If the default value of true is used for replaygain_limit, the * application of the volume to the signal will never cause clipping. @@ -66,16 +67,11 @@ class ReplayGainFilter final : public Filter { * maintain a consistent audio level. Whether clipping will actually * occur depends on what value the user is using for replaygain_preamp. */ - unsigned volume; - - AudioFormat format; - - PcmBuffer buffer; + PcmVolume pv; public: ReplayGainFilter() - :mixer(nullptr), mode(REPLAY_GAIN_OFF), - volume(PCM_VOLUME_1) { + :mixer(nullptr), mode(REPLAY_GAIN_OFF) { info.Clear(); } @@ -125,6 +121,7 @@ public: void ReplayGainFilter::Update() { + unsigned volume = PCM_VOLUME_1; if (mode != REPLAY_GAIN_OFF) { const auto &tuple = info.tuples[mode]; float scale = tuple.CalculateScale(replay_gain_preamp, @@ -134,8 +131,9 @@ ReplayGainFilter::Update() "scale=%f\n", (double)scale); volume = pcm_float_to_volume(scale); - } else - volume = PCM_VOLUME_1; + } + + pv.SetVolume(volume); if (mixer != nullptr) { /* update the hardware mixer volume */ @@ -160,48 +158,25 @@ replay_gain_filter_init(gcc_unused const config_param ¶m, AudioFormat ReplayGainFilter::Open(AudioFormat &af, gcc_unused Error &error) { - format = af; + if (!pv.Open(af.format, error)) + return AudioFormat::Undefined(); - return format; + return af; } void ReplayGainFilter::Close() { - buffer.Clear(); + pv.Close(); } const void * ReplayGainFilter::FilterPCM(const void *src, size_t src_size, - size_t *dest_size_r, Error &error) + size_t *dest_size_r, gcc_unused Error &error) { - - *dest_size_r = src_size; - - if (volume == PCM_VOLUME_1) - /* optimized special case: 100% volume = no-op */ - return src; - - void *dest = buffer.Get(src_size); - if (volume <= 0) { - /* optimized special case: 0% volume = memset(0) */ - /* XXX is this valid for all sample formats? What - about floating point? */ - memset(dest, 0, src_size); - return dest; - } - - memcpy(dest, src, src_size); - - bool success = pcm_volume(dest, src_size, - format.format, - volume); - if (!success) { - error.Set(replay_gain_domain, "pcm_volume() has failed"); - return nullptr; - } - - return dest; + const auto dest = pv.Apply({src, src_size}); + *dest_size_r = dest.size; + return dest.data; } const struct filter_plugin replay_gain_filter_plugin = { diff --git a/src/filter/RouteFilterPlugin.cxx b/src/filter/RouteFilterPlugin.cxx index d9042c21f..d2dc2d563 100644 --- a/src/filter/RouteFilterPlugin.cxx +++ b/src/filter/RouteFilterPlugin.cxx @@ -43,7 +43,6 @@ #include "ConfigError.hxx" #include "ConfigData.hxx" #include "AudioFormat.hxx" -#include "CheckAudioFormat.hxx" #include "FilterPlugin.hxx" #include "FilterInternal.hxx" #include "FilterRegistry.hxx" diff --git a/src/filter/VolumeFilterPlugin.cxx b/src/filter/VolumeFilterPlugin.cxx index 1b663f6eb..8b9c6f8e9 100644 --- a/src/filter/VolumeFilterPlugin.cxx +++ b/src/filter/VolumeFilterPlugin.cxx @@ -22,9 +22,9 @@ #include "FilterPlugin.hxx" #include "FilterInternal.hxx" #include "FilterRegistry.hxx" -#include "pcm/PcmVolume.hxx" -#include "pcm/PcmBuffer.hxx" +#include "pcm/Volume.hxx" #include "AudioFormat.hxx" +#include "util/ConstBuffer.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" @@ -32,29 +32,15 @@ #include <string.h> class VolumeFilter final : public Filter { - /** - * The current volume, from 0 to #PCM_VOLUME_1. - */ - unsigned volume; - - AudioFormat format; - - PcmBuffer buffer; + PcmVolume pv; public: - VolumeFilter() - :volume(PCM_VOLUME_1) {} - unsigned GetVolume() const { - assert(volume <= PCM_VOLUME_1); - - return volume; + return pv.GetVolume(); } void SetVolume(unsigned _volume) { - assert(_volume <= PCM_VOLUME_1); - - volume = _volume; + pv.SetVolume(_volume); } virtual AudioFormat Open(AudioFormat &af, Error &error) override; @@ -73,50 +59,27 @@ volume_filter_init(gcc_unused const config_param ¶m, } AudioFormat -VolumeFilter::Open(AudioFormat &audio_format, gcc_unused Error &error) +VolumeFilter::Open(AudioFormat &audio_format, Error &error) { - format = audio_format; + if (!pv.Open(audio_format.format, error)) + return AudioFormat::Undefined(); - return format; + return audio_format; } void VolumeFilter::Close() { - buffer.Clear(); + pv.Close(); } const void * VolumeFilter::FilterPCM(const void *src, size_t src_size, - size_t *dest_size_r, Error &error) + size_t *dest_size_r, gcc_unused Error &error) { - *dest_size_r = src_size; - - if (volume >= PCM_VOLUME_1) - /* optimized special case: 100% volume = no-op */ - return src; - - void *dest = buffer.Get(src_size); - - if (volume <= 0) { - /* optimized special case: 0% volume = memset(0) */ - /* XXX is this valid for all sample formats? What - about floating point? */ - memset(dest, 0, src_size); - return dest; - } - - memcpy(dest, src, src_size); - - bool success = pcm_volume(dest, src_size, - format.format, - volume); - if (!success) { - error.Set(volume_domain, "pcm_volume() has failed"); - return NULL; - } - - return dest; + const auto dest = pv.Apply({src, src_size}); + *dest_size_r = dest.size; + return dest.data; } const struct filter_plugin volume_filter_plugin = { diff --git a/src/fs/AllocatedPath.cxx b/src/fs/AllocatedPath.cxx index 37b79a685..0d0aaacbe 100644 --- a/src/fs/AllocatedPath.cxx +++ b/src/fs/AllocatedPath.cxx @@ -26,7 +26,6 @@ #include <glib.h> -#include <assert.h> #include <string.h> inline AllocatedPath::AllocatedPath(Donate, pointer _value) @@ -38,12 +37,6 @@ inline AllocatedPath::AllocatedPath(Donate, pointer _value) AllocatedPath::~AllocatedPath() {} AllocatedPath -AllocatedPath::Build(const_pointer a, const_pointer b) -{ - return AllocatedPath(Donate(), g_build_filename(a, b, nullptr)); -} - -AllocatedPath AllocatedPath::FromUTF8(const char *path_utf8) { return AllocatedPath(Donate(), ::PathFromUTF8(path_utf8)); @@ -64,7 +57,7 @@ AllocatedPath::FromUTF8(const char *path_utf8, Error &error) AllocatedPath AllocatedPath::GetDirectoryName() const { - return AllocatedPath(Donate(), g_path_get_dirname(c_str())); + return FromFS(PathTraitsFS::GetParent(c_str())); } std::string @@ -82,14 +75,14 @@ AllocatedPath::RelativeFS(const char *other_fs) const other_fs += l; if (*other_fs != 0) { - if (!PathTraits::IsSeparatorFS(*other_fs)) + if (!PathTraitsFS::IsSeparator(*other_fs)) /* mismatch */ return nullptr; /* skip remaining path separators */ do { ++other_fs; - } while (PathTraits::IsSeparatorFS(*other_fs)); + } while (PathTraitsFS::IsSeparator(*other_fs)); } return other_fs; @@ -101,7 +94,7 @@ AllocatedPath::ChopSeparators() size_t l = length(); const char *p = data(); - while (l >= 2 && PathTraits::IsSeparatorFS(p[l - 1])) { + while (l >= 2 && PathTraitsFS::IsSeparator(p[l - 1])) { --l; #if GCC_CHECK_VERSION(4,7) && !defined(__clang__) diff --git a/src/fs/AllocatedPath.hxx b/src/fs/AllocatedPath.hxx index 36d8a1598..d44953cd6 100644 --- a/src/fs/AllocatedPath.hxx +++ b/src/fs/AllocatedPath.hxx @@ -28,8 +28,6 @@ #include <utility> #include <string> -#include <assert.h> - class Error; /** @@ -39,11 +37,10 @@ class Error; * stored. */ class AllocatedPath { - typedef std::string string; - - typedef PathTraits::value_type value_type; - typedef PathTraits::pointer pointer; - typedef PathTraits::const_pointer const_pointer; + typedef PathTraitsFS::string string; + typedef PathTraitsFS::value_type value_type; + typedef PathTraitsFS::pointer pointer; + typedef PathTraitsFS::const_pointer const_pointer; string value; @@ -56,6 +53,12 @@ class AllocatedPath { AllocatedPath(const_pointer _value):value(_value) {} + AllocatedPath(string &&_value):value(std::move(_value)) {} + + static AllocatedPath Build(const_pointer a, size_t a_size, + const_pointer b, size_t b_size) { + return AllocatedPath(PathTraitsFS::Build(a, a_size, b, b_size)); + } public: /** * Copy a #AllocatedPath object. @@ -89,22 +92,28 @@ public: * Join two path components with the path separator. */ gcc_pure gcc_nonnull_all - static AllocatedPath Build(const_pointer a, const_pointer b); + static AllocatedPath Build(const_pointer a, const_pointer b) { + return Build(a, PathTraitsFS::GetLength(a), + b, PathTraitsFS::GetLength(b)); + } gcc_pure gcc_nonnull_all static AllocatedPath Build(const_pointer a, const AllocatedPath &b) { - return Build(a, b.c_str()); + return Build(a, PathTraitsFS::GetLength(a), + b.value.c_str(), b.value.size()); } gcc_pure gcc_nonnull_all static AllocatedPath Build(const AllocatedPath &a, const_pointer b) { - return Build(a.c_str(), b); + return Build(a.value.c_str(), a.value.size(), + b, PathTraitsFS::GetLength(b)); } gcc_pure static AllocatedPath Build(const AllocatedPath &a, const AllocatedPath &b) { - return Build(a.c_str(), b.c_str()); + return Build(a.value.c_str(), a.value.size(), + b.value.c_str(), b.value.size()); } /** @@ -117,6 +126,15 @@ public: } /** + * Convert a C++ string that is already in the filesystem + * character set to a #Path instance. + */ + gcc_pure + static AllocatedPath FromFS(string &&fs) { + return AllocatedPath(std::move(fs)); + } + + /** * Convert a UTF-8 C string to a #AllocatedPath instance. * Returns return a "nulled" instance on error. */ @@ -215,7 +233,7 @@ public: gcc_pure bool IsAbsolute() { - return PathTraits::IsAbsoluteFS(c_str()); + return PathTraitsFS::IsAbsolute(c_str()); } }; diff --git a/src/fs/Charset.cxx b/src/fs/Charset.cxx index dad5779f9..dcd291a2d 100644 --- a/src/fs/Charset.cxx +++ b/src/fs/Charset.cxx @@ -22,12 +22,13 @@ #include "Domain.hxx" #include "Limits.hxx" #include "system/FatalError.hxx" -#include "util/Error.hxx" -#include "util/Domain.hxx" #include "Log.hxx" +#include "Traits.hxx" #include <glib.h> +#include <algorithm> + #include <assert.h> #include <string.h> @@ -76,13 +77,29 @@ GetFSCharset() return fs_charset.empty() ? "utf-8" : fs_charset.c_str(); } +static inline void FixSeparators(std::string &s) +{ +#ifdef WIN32 + // For whatever reason GCC can't convert constexpr to value reference. + // This leads to link errors when passing separators directly. + auto from = PathTraitsFS::SEPARATOR; + auto to = PathTraitsUTF8::SEPARATOR; + std::replace(s.begin(), s.end(), from, to); +#else + (void)s; +#endif +} + std::string PathToUTF8(const char *path_fs) { assert(path_fs != nullptr); - if (fs_charset.empty()) - return std::string(path_fs); + if (fs_charset.empty()) { + auto result = std::string(path_fs); + FixSeparators(result); + return result; + } GIConv conv = g_iconv_open("utf-8", fs_charset.c_str()); if (conv == reinterpret_cast<GIConv>(-1)) @@ -103,7 +120,9 @@ PathToUTF8(const char *path_fs) if (ret == static_cast<size_t>(-1) || in_left > 0) return std::string(); - return std::string(path_utf8, sizeof(path_utf8) - out_left); + auto result_path = std::string(path_utf8, sizeof(path_utf8) - out_left); + FixSeparators(result_path); + return result_path; } char * diff --git a/src/fs/Config.cxx b/src/fs/Config.cxx index 63e64ef99..87e77512d 100644 --- a/src/fs/Config.cxx +++ b/src/fs/Config.cxx @@ -20,16 +20,10 @@ #include "config.h" #include "Config.hxx" #include "Charset.hxx" -#include "Domain.hxx" #include "ConfigGlobal.hxx" -#include "Log.hxx" -#include "Compiler.h" #include <glib.h> -#include <assert.h> -#include <string.h> - #ifdef WIN32 #include <windows.h> // for GetACP() #include <stdio.h> // for sprintf() diff --git a/src/fs/FileSystem.hxx b/src/fs/FileSystem.hxx index cb2f82d22..e9751c73b 100644 --- a/src/fs/FileSystem.hxx +++ b/src/fs/FileSystem.hxx @@ -28,7 +28,6 @@ #include <sys/stat.h> #include <unistd.h> -#include <assert.h> #include <stdio.h> class AllocatedPath; @@ -37,39 +36,39 @@ namespace FOpenMode { /** * Open mode for reading text files. */ - constexpr PathTraits::const_pointer ReadText = "r"; + constexpr PathTraitsFS::const_pointer ReadText = "r"; /** * Open mode for reading binary files. */ - constexpr PathTraits::const_pointer ReadBinary = "rb"; + constexpr PathTraitsFS::const_pointer ReadBinary = "rb"; /** * Open mode for writing text files. */ - constexpr PathTraits::const_pointer WriteText = "w"; + constexpr PathTraitsFS::const_pointer WriteText = "w"; /** * Open mode for writing binary files. */ - constexpr PathTraits::const_pointer WriteBinary = "wb"; + constexpr PathTraitsFS::const_pointer WriteBinary = "wb"; /** * Open mode for appending text files. */ - constexpr PathTraits::const_pointer AppendText = "a"; + constexpr PathTraitsFS::const_pointer AppendText = "a"; /** * Open mode for appending binary files. */ - constexpr PathTraits::const_pointer AppendBinary = "ab"; + constexpr PathTraitsFS::const_pointer AppendBinary = "ab"; } /** * Wrapper for fopen() that uses #Path names. */ static inline FILE * -FOpen(Path file, PathTraits::const_pointer mode) +FOpen(Path file, PathTraitsFS::const_pointer mode) { return fopen(file.c_str(), mode); } @@ -132,20 +131,28 @@ MakeFifo(Path path, mode_t mode) return mkfifo(path.c_str(), mode) == 0; } -#endif - /** * Wrapper for access() that uses #Path names. */ static inline bool CheckAccess(Path path, int mode) { + return access(path.c_str(), mode) == 0; +} + +#endif + +/** + * Checks is specified path exists and accessible. + */ +static inline bool +CheckAccess(Path path) +{ #ifdef WIN32 - (void)path; - (void)mode; - return true; + struct stat buf; + return StatFile(path, buf); #else - return access(path.c_str(), mode) == 0; + return CheckAccess(path, F_OK); #endif } diff --git a/src/fs/Path.cxx b/src/fs/Path.cxx index 0ff0591fb..4b7fb9319 100644 --- a/src/fs/Path.cxx +++ b/src/fs/Path.cxx @@ -36,14 +36,14 @@ Path::RelativeFS(const char *other_fs) const other_fs += l; if (*other_fs != 0) { - if (!PathTraits::IsSeparatorFS(*other_fs)) + if (!PathTraitsFS::IsSeparator(*other_fs)) /* mismatch */ return nullptr; /* skip remaining path separators */ do { ++other_fs; - } while (PathTraits::IsSeparatorFS(*other_fs)); + } while (PathTraitsFS::IsSeparator(*other_fs)); } return other_fs; diff --git a/src/fs/Path.hxx b/src/fs/Path.hxx index 6ea954577..807677f38 100644 --- a/src/fs/Path.hxx +++ b/src/fs/Path.hxx @@ -29,8 +29,6 @@ #include <assert.h> #include <string.h> -class Error; - /** * A path name in the native file system character set. * @@ -38,9 +36,9 @@ class Error; * instance lives, the string must not be invalidated. */ class Path { - typedef PathTraits::value_type value_type; - typedef PathTraits::pointer pointer; - typedef PathTraits::const_pointer const_pointer; + typedef PathTraitsFS::value_type value_type; + typedef PathTraitsFS::pointer pointer; + typedef PathTraitsFS::const_pointer const_pointer; const char *value; @@ -141,7 +139,7 @@ public: gcc_pure bool IsAbsolute() { - return PathTraits::IsAbsoluteFS(c_str()); + return PathTraitsFS::IsAbsolute(c_str()); } }; diff --git a/src/fs/StandardDirectory.cxx b/src/fs/StandardDirectory.cxx new file mode 100644 index 000000000..889f6b3ea --- /dev/null +++ b/src/fs/StandardDirectory.cxx @@ -0,0 +1,294 @@ +/* + * 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" + +// Use X Desktop guidelines where applicable +#if !defined(__APPLE__) && !defined(WIN32) +#define USE_XDG +#endif + +#include "StandardDirectory.hxx" +#include "FileSystem.hxx" + +#include <array> + +#ifdef WIN32 +#include <windows.h> +#include <shlobj.h> +#else +#include <stdlib.h> +#include <sys/types.h> +#include <unistd.h> +#include <pwd.h> +#endif + +#ifdef USE_XDG +#include "util/CharUtil.hxx" +#include "util/StringUtil.hxx" +#include "TextFile.hxx" +#include <string.h> +#include <utility> +#endif + +#ifndef WIN32 +class PasswdEntry +{ +#if defined(HAVE_GETPWNAM_R) || defined(HAVE_GETPWUID_R) + std::array<char, 16 * 1024> buf; + passwd pw; +#endif + + passwd *result; +public: + PasswdEntry() : result(nullptr) { } + + bool ReadByName(const char *name) { +#ifdef HAVE_GETPWNAM_R + getpwnam_r(name, &pw, buf.data(), buf.size(), &result); +#else + result = getpwnam(name); +#endif + return result != nullptr; + } + + bool ReadByUid(uid_t uid) { +#ifdef HAVE_GETPWUID_R + getpwuid_r(uid, &pw, buf.data(), buf.size(), &result); +#else + result = getpwuid(uid); +#endif + return result != nullptr; + } + + const passwd *operator->() { + assert(result != nullptr); + return result; + } +}; +#endif + +static inline bool IsValidPathString(PathTraitsFS::const_pointer path) +{ + return path != nullptr && *path != '\0'; +} + +static inline bool IsValidDir(PathTraitsFS::const_pointer dir) +{ + return PathTraitsFS::IsAbsolute(dir) && + DirectoryExists(Path::FromFS(dir)); +} + +static inline AllocatedPath SafePathFromFS(PathTraitsFS::const_pointer dir) +{ + if (IsValidPathString(dir) && IsValidDir(dir)) + return AllocatedPath::FromFS(dir); + return AllocatedPath::Null(); +} + +#ifdef WIN32 +static AllocatedPath GetStandardDir(int folder_id) +{ + std::array<char, MAX_PATH> dir; + auto ret = SHGetFolderPath(nullptr, folder_id | CSIDL_FLAG_DONT_VERIFY, + nullptr, SHGFP_TYPE_CURRENT, dir.data()); + if (FAILED(ret)) + return AllocatedPath::Null(); + return SafePathFromFS(dir.data()); +} +#endif + +#ifdef USE_XDG + +static const char home_prefix[] = "$HOME/"; + +static bool ParseConfigLine(const char *line, const char *dir_name, + AllocatedPath &result_dir) +{ + // strip leading white space + line = strchug_fast(line); + + // check for end-of-line or comment + if (*line == '\0' || *line == '#') + return false; + + // check if current setting is for requested dir + if (!StringStartsWith(line, dir_name)) + return false; + line += strlen(dir_name); + + // strip equals sign and spaces around it + line = strchug_fast(line); + if (*line != '=') + return false; + ++line; + line = strchug_fast(line); + + // check if path is quoted + bool quoted = false; + if (*line == '"') { + ++line; + quoted = true; + } + + // check if path is relative to $HOME + bool home_relative = false; + if (StringStartsWith(line, home_prefix)) { + line += strlen(home_prefix); + home_relative = true; + } + + + const char *line_end; + // find end of the string + if (quoted) { + line_end = strrchr(line, '"'); + if (line_end == nullptr) + return true; + } else { + line_end = line + strlen(line); + while (line < line_end && IsWhitespaceNotNull(line_end[-1])) + --line_end; + } + + // check for empty result + if (line == line_end) + return true; + + // build the result path + std::string path(line, line_end); + + auto result = AllocatedPath::Null(); + if (home_relative) { + auto home = GetHomeDir(); + if (home.IsNull()) + return true; + result = AllocatedPath::Build(home, path.c_str()); + } else { + result = AllocatedPath::FromFS(std::move(path)); + } + + if (IsValidDir(result.c_str())) { + result_dir = std::move(result); + return true; + } + return true; +} + +static AllocatedPath GetUserDir(const char *name) +{ + auto result = AllocatedPath::Null(); + auto config_dir = GetUserConfigDir(); + if (config_dir.IsNull()) + return result; + auto dirs_file = AllocatedPath::Build(config_dir, "user-dirs.dirs"); + TextFile input(dirs_file); + if (input.HasFailed()) + return result; + const char *line; + while ((line = input.ReadLine()) != nullptr) + if (ParseConfigLine(line, name, result)) + return result; + return result; +} + +#endif + +AllocatedPath GetUserConfigDir() +{ +#if defined(WIN32) + return GetStandardDir(CSIDL_LOCAL_APPDATA); +#elif defined(USE_XDG) + // Check for $XDG_CONFIG_HOME + auto config_home = getenv("XDG_CONFIG_HOME"); + if (IsValidPathString(config_home) && IsValidDir(config_home)) + return AllocatedPath::FromFS(config_home); + + // Check for $HOME/.config + auto home = GetHomeDir(); + if (!home.IsNull()) { + AllocatedPath fallback = AllocatedPath::Build(home, ".config"); + if (IsValidDir(fallback.c_str())) + return fallback; + } + + return AllocatedPath::Null(); +#else + return AllocatedPath::Null(); +#endif +} + +AllocatedPath GetUserMusicDir() +{ +#if defined(WIN32) + return GetStandardDir(CSIDL_MYMUSIC); +#elif defined(USE_XDG) + return GetUserDir("XDG_MUSIC_DIR"); +#else + return AllocatedPath::Null(); +#endif +} + +#ifdef WIN32 + +AllocatedPath GetSystemConfigDir() +{ + return GetStandardDir(CSIDL_COMMON_APPDATA); +} + +AllocatedPath GetAppBaseDir() +{ + std::array<char, MAX_PATH> app; + auto ret = GetModuleFileName(nullptr, app.data(), app.size()); + + // Check for error + if (ret == 0) + return AllocatedPath::Null(); + + // Check for truncation + if (ret == app.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) + return AllocatedPath::Null(); + + auto app_path = AllocatedPath::FromFS(app.data()); + return app_path.GetDirectoryName().GetDirectoryName(); +} + +#else + +AllocatedPath GetHomeDir() +{ + auto home = getenv("HOME"); + if (IsValidPathString(home) && IsValidDir(home)) + return AllocatedPath::FromFS(home); + PasswdEntry pw; + if (pw.ReadByUid(getuid())) + return SafePathFromFS(pw->pw_dir); + return AllocatedPath::Null(); +} + +AllocatedPath GetHomeDir(const char *user_name) +{ + assert(user_name != nullptr); + PasswdEntry pw; + if (pw.ReadByName(user_name)) + return SafePathFromFS(pw->pw_dir); + return AllocatedPath::Null(); +} + +#endif diff --git a/src/fs/StandardDirectory.hxx b/src/fs/StandardDirectory.hxx new file mode 100644 index 000000000..cc5c5ec2a --- /dev/null +++ b/src/fs/StandardDirectory.hxx @@ -0,0 +1,64 @@ +/* + * 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_FS_STANDARD_DIRECTORY_HXX +#define MPD_FS_STANDARD_DIRECTORY_HXX + +#include "check.h" +#include "AllocatedPath.hxx" + +/** + * Obtains configuration directory for the current user. + */ +AllocatedPath GetUserConfigDir(); + +/** + * Obtains music directory for the current user. + */ +AllocatedPath GetUserMusicDir(); + +#ifdef WIN32 + +/** + * Obtains system configuration directory. + */ +AllocatedPath GetSystemConfigDir(); + +/** + * Obtains application application base directory. + * Application base directory is a directory that contains 'bin' folder + * for current executable. + */ +AllocatedPath GetAppBaseDir(); + +#else + +/** + * Obtains home directory for the current user. + */ +AllocatedPath GetHomeDir(); + +/** + * Obtains home directory for the specified user. + */ +AllocatedPath GetHomeDir(const char *user_name); + +#endif + +#endif diff --git a/src/TextFile.cxx b/src/fs/TextFile.cxx index 4a64ee963..4a64ee963 100644 --- a/src/TextFile.cxx +++ b/src/fs/TextFile.cxx diff --git a/src/TextFile.hxx b/src/fs/TextFile.hxx index 9d8608711..9d8608711 100644 --- a/src/TextFile.hxx +++ b/src/fs/TextFile.hxx diff --git a/src/fs/Traits.cxx b/src/fs/Traits.cxx index 2c3ce075b..3e874a224 100644 --- a/src/fs/Traits.cxx +++ b/src/fs/Traits.cxx @@ -22,24 +22,96 @@ #include <string.h> -const char * -PathTraits::GetBaseUTF8(const char *p) +template<typename Traits> +typename Traits::string +BuildPathImpl(typename Traits::const_pointer a, size_t a_size, + typename Traits::const_pointer b, size_t b_size) +{ + assert(a != nullptr); + assert(b != nullptr); + + if (a_size == 0) + return typename Traits::string(b, b_size); + if (b_size == 0) + return typename Traits::string(a, a_size); + + typename Traits::string result(a, a_size); + + if (!Traits::IsSeparator(a[a_size - 1])) + result.push_back(Traits::SEPARATOR); + + if (Traits::IsSeparator(b[0])) + result.append(b + 1, b_size - 1); + else + result.append(b, b_size); + + return result; +} + +template<typename Traits> +typename Traits::const_pointer +GetBasePathImpl(typename Traits::const_pointer p) { assert(p != nullptr); - const char *slash = strrchr(p, SEPARATOR_UTF8); - return slash != nullptr - ? slash + 1 + typename Traits::const_pointer sep = Traits::FindLastSeparator(p); + return sep != nullptr + ? sep + 1 : p; } -std::string -PathTraits::GetParentUTF8(const char *p) +template<typename Traits> +typename Traits::string +GetParentPathImpl(typename Traits::const_pointer p) { assert(p != nullptr); - const char *slash = strrchr(p, SEPARATOR_UTF8); - return slash != nullptr - ? std::string(p, slash) - : std::string("."); + typename Traits::const_pointer sep = Traits::FindLastSeparator(p); + if (sep == nullptr) + return typename Traits::string("."); + if (sep == p) + return typename Traits::string(p, p + 1); +#ifdef WIN32 + if (Traits::IsDrive(p) && sep == p + 2) + return typename Traits::string(p, p + 3); +#endif + return typename Traits::string(p, sep); +} + +PathTraitsFS::string +PathTraitsFS::Build(PathTraitsFS::const_pointer a, size_t a_size, + PathTraitsFS::const_pointer b, size_t b_size) +{ + return BuildPathImpl<PathTraitsFS>(a, a_size, b, b_size); +} + +PathTraitsFS::const_pointer +PathTraitsFS::GetBase(PathTraitsFS::const_pointer p) +{ + return GetBasePathImpl<PathTraitsFS>(p); +} + +PathTraitsFS::string +PathTraitsFS::GetParent(PathTraitsFS::const_pointer p) +{ + return GetParentPathImpl<PathTraitsFS>(p); +} + +PathTraitsUTF8::string +PathTraitsUTF8::Build(PathTraitsUTF8::const_pointer a, size_t a_size, + PathTraitsUTF8::const_pointer b, size_t b_size) +{ + return BuildPathImpl<PathTraitsUTF8>(a, a_size, b, b_size); +} + +PathTraitsUTF8::const_pointer +PathTraitsUTF8::GetBase(PathTraitsUTF8::const_pointer p) +{ + return GetBasePathImpl<PathTraitsUTF8>(p); +} + +PathTraitsUTF8::string +PathTraitsUTF8::GetParent(PathTraitsUTF8::const_pointer p) +{ + return GetParentPathImpl<PathTraitsUTF8>(p); } diff --git a/src/fs/Traits.hxx b/src/fs/Traits.hxx index 244ab8b5c..927496e53 100644 --- a/src/fs/Traits.hxx +++ b/src/fs/Traits.hxx @@ -24,67 +24,144 @@ #include "Compiler.h" #ifdef WIN32 -#include <glib.h> +#include "util/CharUtil.hxx" #endif #include <string> +#include <string.h> #include <assert.h> -class Error; - /** - * This class describes the nature of a filesystem path. + * This class describes the nature of a native filesystem path. */ -struct PathTraits { +struct PathTraitsFS { + typedef std::string string; typedef char value_type; typedef char *pointer; typedef const char *const_pointer; #ifdef WIN32 - static constexpr value_type SEPARATOR_FS = '\\'; - static constexpr char SEPARATOR_UTF8 = '/'; + static constexpr value_type SEPARATOR = '\\'; #else - static constexpr value_type SEPARATOR_FS = '/'; - static constexpr char SEPARATOR_UTF8 = '/'; + static constexpr value_type SEPARATOR = '/'; #endif - static constexpr bool IsSeparatorFS(value_type ch) { + static constexpr bool IsSeparator(value_type ch) { return #ifdef WIN32 ch == '/' || #endif - ch == SEPARATOR_FS; + ch == SEPARATOR; } - static constexpr bool IsSeparatorUTF8(char ch) { - return + gcc_pure gcc_nonnull_all + static const_pointer FindLastSeparator(const_pointer p) { + assert(p != nullptr); #ifdef WIN32 - ch == '/' || + const_pointer pos = p + GetLength(p); + while (p != pos && !IsSeparator(*pos)) + --pos; + return IsSeparator(*pos) ? pos : nullptr; +#else + return strrchr(p, SEPARATOR); #endif - ch == SEPARATOR_UTF8; } - gcc_pure - static bool IsAbsoluteFS(const_pointer p) { - assert(p != nullptr); +#ifdef WIN32 + gcc_pure gcc_nonnull_all + static constexpr bool IsDrive(const_pointer p) { + return IsAlphaASCII(p[0]) && p[1] == ':'; + } +#endif + gcc_pure gcc_nonnull_all + static bool IsAbsolute(const_pointer p) { + assert(p != nullptr); #ifdef WIN32 - return g_path_is_absolute(p); -#else - return IsSeparatorFS(*p); + if (IsDrive(p) && IsSeparator(p[2])) + return true; #endif + return IsSeparator(*p); + } + + gcc_pure gcc_nonnull_all + static size_t GetLength(const_pointer p) { + return strlen(p); } - gcc_pure - static bool IsAbsoluteUTF8(const char *p) { + /** + * Determine the "base" file name of the given native path. + * The return value points inside the given string. + */ + gcc_pure gcc_nonnull_all + static const_pointer GetBase(const_pointer p); + + /** + * Determine the "parent" file name of the given native path. + * As a special case, returns the string "." if there is no + * separator in the given input string. + */ + gcc_pure gcc_nonnull_all + static string GetParent(const_pointer p); + + /** + * Constructs the path from the given components. + * If either of the components is empty string, + * remaining component is returned unchanged. + * If both components are empty strings, empty string is returned. + */ + gcc_pure gcc_nonnull_all + static string Build(const_pointer a, size_t a_size, + const_pointer b, size_t b_size); + + gcc_pure gcc_nonnull_all + static string Build(const_pointer a, const_pointer b) { + return Build(a, GetLength(a), b, GetLength(b)); + } +}; + +/** + * This class describes the nature of a MPD internal filesystem path. + */ +struct PathTraitsUTF8 { + typedef std::string string; + typedef char value_type; + typedef char *pointer; + typedef const char *const_pointer; + + static constexpr value_type SEPARATOR = '/'; + + static constexpr bool IsSeparator(value_type ch) { + return ch == SEPARATOR; + } + + gcc_pure gcc_nonnull_all + static const_pointer FindLastSeparator(const_pointer p) { assert(p != nullptr); + return strrchr(p, SEPARATOR); + } #ifdef WIN32 - return g_path_is_absolute(p); -#else - return IsSeparatorUTF8(*p); + gcc_pure gcc_nonnull_all + static constexpr bool IsDrive(const_pointer p) { + return IsAlphaASCII(p[0]) && p[1] == ':'; + } +#endif + + gcc_pure gcc_nonnull_all + static bool IsAbsolute(const_pointer p) { + assert(p != nullptr); +#ifdef WIN32 + if (IsDrive(p) && IsSeparator(p[2])) + return true; #endif + return IsSeparator(*p); + } + + gcc_pure gcc_nonnull_all + static size_t GetLength(const_pointer p) { + return strlen(p); } /** @@ -92,7 +169,7 @@ struct PathTraits { * The return value points inside the given string. */ gcc_pure gcc_nonnull_all - static const char *GetBaseUTF8(const char *p); + static const_pointer GetBase(const_pointer p); /** * Determine the "parent" file name of the given UTF-8 path. @@ -100,7 +177,22 @@ struct PathTraits { * separator in the given input string. */ gcc_pure gcc_nonnull_all - static std::string GetParentUTF8(const char *p); + static string GetParent(const_pointer p); + + /** + * Constructs the path from the given components. + * If either of the components is empty string, + * remaining component is returned unchanged. + * If both components are empty strings, empty string is returned. + */ + gcc_pure gcc_nonnull_all + static string Build(const_pointer a, size_t a_size, + const_pointer b, size_t b_size); + + gcc_pure gcc_nonnull_all + static string Build(const_pointer a, const_pointer b) { + return Build(a, GetLength(a), b, GetLength(b)); + } }; #endif diff --git a/src/input/AlsaInputPlugin.cxx b/src/input/AlsaInputPlugin.cxx new file mode 100644 index 000000000..c910ae340 --- /dev/null +++ b/src/input/AlsaInputPlugin.cxx @@ -0,0 +1,206 @@ +/* + * 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. + */ + +/* + * ALSA code based on an example by Paul Davis released under GPL here: + * http://equalarea.com/paul/alsa-audio.html + * and one by Matthias Nagorni, also GPL, here: + * http://alsamodular.sourceforge.net/alsa_programming_howto.html + */ + +#include "config.h" +#include "AlsaInputPlugin.hxx" +#include "InputPlugin.hxx" +#include "InputStream.hxx" +#include "util/Domain.hxx" +#include "util/Error.hxx" +#include "util/StringUtil.hxx" +#include "Log.hxx" + +#include <alsa/asoundlib.h> + +static constexpr Domain alsa_input_domain("alsa"); + +static constexpr const char *default_device = "hw:0,0"; + +// this value chosen to balance between limiting latency and avoiding stutter +static constexpr int max_frames_to_buffer = 64; + +// the following defaults are because the PcmDecoderPlugin forces CD format +static constexpr snd_pcm_format_t default_format = SND_PCM_FORMAT_S16; +static constexpr int default_channels = 2; // stereo +static constexpr unsigned int default_rate = 44100; // cd quality + +struct AlsaInputStream { + InputStream base; + snd_pcm_t *capture_handle; + size_t frame_size; + size_t max_bytes_to_read; + + AlsaInputStream(const char *uri, Mutex &mutex, Cond &cond, + snd_pcm_t *handle) + :base(input_plugin_alsa, uri, mutex, cond), + capture_handle(handle) { + frame_size = snd_pcm_format_width(default_format) / 8 * default_channels; + max_bytes_to_read = max_frames_to_buffer * frame_size; + base.mime = strdup("audio/x-mpd-cdda-pcm"); + base.seekable = false; + base.size = -1; + base.ready = true; + } + + ~AlsaInputStream() { + snd_pcm_close(capture_handle); + } +}; + +static InputStream * +alsa_input_open(const char *uri, Mutex &mutex, Cond &cond, Error &error) +{ + int err; + + // check uri is appropriate for alsa input + if (!StringStartsWith(uri, "alsa://")) + return nullptr; + + const char *device = uri + 7; + if (device[0] == '\0') + device = default_device; + + snd_pcm_t *capture_handle; + if ((err = snd_pcm_open(&capture_handle, device, + SND_PCM_STREAM_CAPTURE, 0)) < 0) { + error.Format(alsa_input_domain, "Failed to open device: %s (%s)", device, snd_strerror(err)); + return nullptr; + } + + snd_pcm_hw_params_t *hw_params; + if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) { + error.Format(alsa_input_domain, "Cannot allocate hardware parameter structure (%s)", snd_strerror(err)); + snd_pcm_close(capture_handle); + return nullptr; + } + + if ((err = snd_pcm_hw_params_any(capture_handle, hw_params)) < 0) { + error.Format(alsa_input_domain, "Cannot initialize hardware parameter structure (%s)", snd_strerror(err)); + snd_pcm_hw_params_free(hw_params); + snd_pcm_close(capture_handle); + return nullptr; + } + + if ((err = snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { + error.Format(alsa_input_domain, "Cannot set access type (%s)", snd_strerror (err)); + snd_pcm_hw_params_free (hw_params); + snd_pcm_close(capture_handle); + return nullptr; + } + + if ((err = snd_pcm_hw_params_set_format(capture_handle, hw_params, default_format)) < 0) { + snd_pcm_hw_params_free(hw_params); + snd_pcm_close(capture_handle); + error.Format(alsa_input_domain, "Cannot set sample format (%s)", snd_strerror (err)); + return nullptr; + } + + if ((err = snd_pcm_hw_params_set_channels(capture_handle, hw_params, default_channels)) < 0) { + snd_pcm_hw_params_free(hw_params); + snd_pcm_close(capture_handle); + error.Format(alsa_input_domain, "Cannot set channels (%s)", snd_strerror (err)); + return nullptr; + } + + if ((err = snd_pcm_hw_params_set_rate(capture_handle, hw_params, default_rate, 0)) < 0) { + snd_pcm_hw_params_free(hw_params); + snd_pcm_close(capture_handle); + error.Format(alsa_input_domain, "Cannot set sample rate (%s)", snd_strerror (err)); + return nullptr; + } + + if ((err = snd_pcm_hw_params(capture_handle, hw_params)) < 0) { + error.Format(alsa_input_domain, "Cannot set parameters (%s)", snd_strerror (err)); + snd_pcm_hw_params_free(hw_params); + snd_pcm_close(capture_handle); + return nullptr; + } + + snd_pcm_hw_params_free (hw_params); + + // clear any data already in the PCM buffer + if ((err = snd_pcm_drop(capture_handle)) < 0) { + error.Format(alsa_input_domain, "Cannot clear PCM buffer (%s)", snd_strerror (err)); + snd_pcm_hw_params_free(hw_params); + snd_pcm_close(capture_handle); + return nullptr; + } + + AlsaInputStream *ais = new AlsaInputStream(uri, mutex, cond, capture_handle); + return &ais->base; +} + +static void +alsa_input_close(InputStream *is) +{ + AlsaInputStream *ais = (AlsaInputStream*) is; + delete ais; +} + +static size_t +alsa_input_read(InputStream *is, void *ptr, size_t size, + gcc_unused Error &error) +{ + AlsaInputStream *ais = (AlsaInputStream*) is; + int num_frames = max_frames_to_buffer; + if (size < ais->max_bytes_to_read) + // calculate number of whole frames that will fit in size bytes + num_frames = size / ais->frame_size; + + int ret; + while ((ret = snd_pcm_readi(ais->capture_handle, ptr, + num_frames)) < 0) { + snd_pcm_prepare(ais->capture_handle); + LogDebug(alsa_input_domain, "Buffer Overrun"); + } + + size_t nbytes = ret == max_frames_to_buffer + ? ais->max_bytes_to_read + : ret * ais->frame_size; + is->offset += nbytes; + return nbytes; +} + +static bool +alsa_input_eof(gcc_unused InputStream *is) +{ + return false; +}; + +const struct InputPlugin input_plugin_alsa = { + "alsa", + nullptr, + nullptr, + alsa_input_open, + alsa_input_close, + nullptr, + nullptr, + nullptr, + nullptr, + alsa_input_read, + alsa_input_eof, + nullptr, +}; diff --git a/src/input/AlsaInputPlugin.hxx b/src/input/AlsaInputPlugin.hxx new file mode 100644 index 000000000..ac9519588 --- /dev/null +++ b/src/input/AlsaInputPlugin.hxx @@ -0,0 +1,28 @@ +/* + * 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_ALSA_INPUT_PLUGIN_HXX +#define MPD_ALSA_INPUT_PLUGIN_HXX + +#include "InputPlugin.hxx" + +extern const struct InputPlugin input_plugin_alsa; + + +#endif diff --git a/src/input/ArchiveInputPlugin.cxx b/src/input/ArchiveInputPlugin.cxx index 5288f2b3b..5797caced 100644 --- a/src/input/ArchiveInputPlugin.cxx +++ b/src/input/ArchiveInputPlugin.cxx @@ -25,7 +25,6 @@ #include "ArchivePlugin.hxx" #include "ArchiveFile.hxx" #include "InputPlugin.hxx" -#include "util/Error.hxx" #include "fs/Traits.hxx" #include "Log.hxx" @@ -47,7 +46,7 @@ input_archive_open(const char *pathname, const struct archive_plugin *arplug; InputStream *is; - if (!PathTraits::IsAbsoluteFS(pathname)) + if (!PathTraitsFS::IsAbsolute(pathname)) return nullptr; char *pname = g_strdup(pathname); diff --git a/src/input/CdioParanoiaInputPlugin.cxx b/src/input/CdioParanoiaInputPlugin.cxx index b3ac57413..bf1c3c908 100644 --- a/src/input/CdioParanoiaInputPlugin.cxx +++ b/src/input/CdioParanoiaInputPlugin.cxx @@ -25,6 +25,7 @@ #include "CdioParanoiaInputPlugin.hxx" #include "InputStream.hxx" #include "InputPlugin.hxx" +#include "util/StringUtil.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" #include "system/ByteOrder.hxx" @@ -122,7 +123,7 @@ struct cdio_uri { static bool parse_cdio_uri(struct cdio_uri *dest, const char *src, Error &error) { - if (!g_str_has_prefix(src, "cdda://")) + if (!StringStartsWith(src, "cdda://")) return false; src += 7; diff --git a/src/input/CurlInputPlugin.cxx b/src/input/CurlInputPlugin.cxx index b78545951..fa8f9affc 100644 --- a/src/input/CurlInputPlugin.cxx +++ b/src/input/CurlInputPlugin.cxx @@ -24,6 +24,7 @@ #include "ConfigGlobal.hxx" #include "ConfigData.hxx" #include "tag/Tag.hxx" +#include "tag/TagBuilder.hxx" #include "IcyMetaDataParser.hxx" #include "event/SocketMonitor.hxx" #include "event/TimeoutMonitor.hxx" @@ -780,8 +781,11 @@ copy_icy_tag(struct input_curl *c) delete c->tag; - if (!c->meta_name.empty() && !tag->HasType(TAG_NAME)) - tag->AddItem(TAG_NAME, c->meta_name.c_str()); + if (!c->meta_name.empty() && !tag->HasType(TAG_NAME)) { + TagBuilder tag_builder(std::move(*tag)); + tag_builder.AddItem(TAG_NAME, c->meta_name.c_str()); + tag_builder.Commit(*tag); + } c->tag = tag; } @@ -910,8 +914,10 @@ input_curl_headerfunction(void *ptr, size_t size, size_t nmemb, void *stream) delete c->tag; - c->tag = new Tag(); - c->tag->AddItem(TAG_NAME, c->meta_name.c_str()); + TagBuilder tag_builder; + tag_builder.AddItem(TAG_NAME, c->meta_name.c_str()); + + c->tag = tag_builder.Commit(); } else if (StringEqualsCaseASCII(name, "icy-metaint")) { char buffer[64]; size_t icy_metaint; diff --git a/src/input/DespotifyInputPlugin.cxx b/src/input/DespotifyInputPlugin.cxx index b08299516..38c2aac75 100644 --- a/src/input/DespotifyInputPlugin.cxx +++ b/src/input/DespotifyInputPlugin.cxx @@ -23,21 +23,20 @@ #include "InputStream.hxx" #include "InputPlugin.hxx" #include "tag/Tag.hxx" +#include "util/StringUtil.hxx" #include "Log.hxx" extern "C" { #include <despotify.h> } -#include <glib.h> - #include <unistd.h> #include <string.h> #include <errno.h> #include <stdio.h> -struct DespotifyInputStream { +class DespotifyInputStream { InputStream base; struct despotify_session *session; @@ -63,30 +62,52 @@ struct DespotifyInputStream { base.ready = true; } +public: ~DespotifyInputStream() { delete tag; despotify_free_track(track); } + + static InputStream *Open(const char *url, Mutex &mutex, Cond &cond, + Error &error); + + bool IsEOF() const { + return eof; + } + + size_t Read(void *ptr, size_t size, Error &error); + + Tag *ReadTag() { + Tag *result = tag; + tag = nullptr; + return result; + } + + void Callback(int sig); + +private: + void FillBuffer(); }; -static void -refill_buffer(DespotifyInputStream *ctx) +inline void +DespotifyInputStream::FillBuffer() { /* Wait until there is data */ while (1) { - int rc = despotify_get_pcm(ctx->session, &ctx->pcm); + int rc = despotify_get_pcm(session, &pcm); - if (rc == 0 && ctx->pcm.len) { - ctx->len_available = ctx->pcm.len; + if (rc == 0 && pcm.len) { + len_available = pcm.len; break; } - if (ctx->eof == true) + + if (eof == true) break; if (rc < 0) { LogDebug(despotify_domain, "despotify_get_pcm error"); - ctx->eof = true; + eof = true; break; } @@ -95,11 +116,9 @@ refill_buffer(DespotifyInputStream *ctx) } } -static void callback(gcc_unused struct despotify_session* ds, - int sig, gcc_unused void* data, void* callback_data) +inline void +DespotifyInputStream::Callback(int sig) { - DespotifyInputStream *ctx = (DespotifyInputStream *)callback_data; - switch (sig) { case DESPOTIFY_NEW_TRACK: break; @@ -109,35 +128,38 @@ static void callback(gcc_unused struct despotify_session* ds, case DESPOTIFY_TRACK_PLAY_ERROR: LogWarning(despotify_domain, "Track play error"); - ctx->eof = true; - ctx->len_available = 0; + eof = true; + len_available = 0; break; case DESPOTIFY_END_OF_PLAYLIST: - ctx->eof = true; - FormatDebug(despotify_domain, "End of playlist: %d", ctx->eof); + eof = true; + LogDebug(despotify_domain, "End of playlist"); break; } } - -static InputStream * -input_despotify_open(const char *url, - Mutex &mutex, Cond &cond, - gcc_unused Error &error) +static void callback(gcc_unused struct despotify_session* ds, + int sig, gcc_unused void* data, void* callback_data) { - struct despotify_session *session; - struct ds_link *ds_link; - struct ds_track *track; + DespotifyInputStream *ctx = (DespotifyInputStream *)callback_data; - if (!g_str_has_prefix(url, "spt://")) + ctx->Callback(sig); +} + +inline InputStream * +DespotifyInputStream::Open(const char *url, + Mutex &mutex, Cond &cond, + gcc_unused Error &error) +{ + if (!StringStartsWith(url, "spt://")) return nullptr; - session = mpd_despotify_get_session(); - if (!session) + despotify_session *session = mpd_despotify_get_session(); + if (session == nullptr) return nullptr; - ds_link = despotify_link_from_uri(url + 6); + ds_link *ds_link = despotify_link_from_uri(url + 6); if (!ds_link) { FormatDebug(despotify_domain, "Can't find %s", url); return nullptr; @@ -147,7 +169,7 @@ input_despotify_open(const char *url, return nullptr; } - track = despotify_link_get_track(session, ds_link); + ds_track *track = despotify_link_get_track(session, ds_link); despotify_free_link(ds_link); if (!track) return nullptr; @@ -170,26 +192,34 @@ input_despotify_open(const char *url, return &ctx->base; } -static size_t -input_despotify_read(InputStream *is, void *ptr, size_t size, - gcc_unused Error &error) +static InputStream * +input_despotify_open(const char *url, Mutex &mutex, Cond &cond, Error &error) { - DespotifyInputStream *ctx = (DespotifyInputStream *)is; - size_t to_cpy = size; + return DespotifyInputStream::Open(url, mutex, cond, error); +} - if (ctx->len_available == 0) - refill_buffer(ctx); +inline size_t +DespotifyInputStream::Read(void *ptr, size_t size, gcc_unused Error &error) +{ + if (len_available == 0) + FillBuffer(); - if (ctx->len_available < size) - to_cpy = ctx->len_available; - memcpy(ptr, ctx->pcm.buf, to_cpy); - ctx->len_available -= to_cpy; + size_t to_cpy = std::min(size, len_available); + memcpy(ptr, pcm.buf, to_cpy); + len_available -= to_cpy; - is->offset += to_cpy; + base.offset += to_cpy; return to_cpy; } +static size_t +input_despotify_read(InputStream *is, void *ptr, size_t size, Error &error) +{ + DespotifyInputStream *ctx = (DespotifyInputStream *)is; + return ctx->Read(ptr, size, error); +} + static void input_despotify_close(InputStream *is) { @@ -204,18 +234,15 @@ input_despotify_eof(InputStream *is) { DespotifyInputStream *ctx = (DespotifyInputStream *)is; - return ctx->eof; + return ctx->IsEOF(); } static Tag * input_despotify_tag(InputStream *is) { DespotifyInputStream *ctx = (DespotifyInputStream *)is; - Tag *tag = ctx->tag; - - ctx->tag = nullptr; - return tag; + return ctx->ReadTag(); } const InputPlugin input_plugin_despotify = { diff --git a/src/input/FfmpegInputPlugin.cxx b/src/input/FfmpegInputPlugin.cxx index 8f9cd0b86..7d041677b 100644 --- a/src/input/FfmpegInputPlugin.cxx +++ b/src/input/FfmpegInputPlugin.cxx @@ -24,17 +24,15 @@ #include "FfmpegInputPlugin.hxx" #include "InputStream.hxx" #include "InputPlugin.hxx" +#include "util/StringUtil.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" extern "C" { -#include <libavutil/avutil.h> #include <libavformat/avio.h> #include <libavformat/avformat.h> } -#include <glib.h> - struct FfmpegInputStream { InputStream base; @@ -91,12 +89,12 @@ input_ffmpeg_open(const char *uri, Mutex &mutex, Cond &cond, Error &error) { - if (!g_str_has_prefix(uri, "gopher://") && - !g_str_has_prefix(uri, "rtp://") && - !g_str_has_prefix(uri, "rtsp://") && - !g_str_has_prefix(uri, "rtmp://") && - !g_str_has_prefix(uri, "rtmpt://") && - !g_str_has_prefix(uri, "rtmps://")) + if (!StringStartsWith(uri, "gopher://") && + !StringStartsWith(uri, "rtp://") && + !StringStartsWith(uri, "rtsp://") && + !StringStartsWith(uri, "rtmp://") && + !StringStartsWith(uri, "rtmpt://") && + !StringStartsWith(uri, "rtmps://")) return nullptr; AVIOContext *h; diff --git a/src/input/FileInputPlugin.cxx b/src/input/FileInputPlugin.cxx index 26e40d609..5a63a469c 100644 --- a/src/input/FileInputPlugin.cxx +++ b/src/input/FileInputPlugin.cxx @@ -30,8 +30,6 @@ #include <sys/stat.h> #include <unistd.h> #include <errno.h> -#include <string.h> -#include <glib.h> static constexpr Domain file_domain("file"); @@ -62,7 +60,7 @@ input_file_open(const char *filename, int fd, ret; struct stat st; - if (!PathTraits::IsAbsoluteFS(filename)) + if (!PathTraitsFS::IsAbsolute(filename)) return nullptr; fd = open_cloexec(filename, O_RDONLY|O_BINARY, 0); diff --git a/src/input/MmsInputPlugin.cxx b/src/input/MmsInputPlugin.cxx index e97c1eb3f..2c7f6d166 100644 --- a/src/input/MmsInputPlugin.cxx +++ b/src/input/MmsInputPlugin.cxx @@ -21,15 +21,12 @@ #include "MmsInputPlugin.hxx" #include "InputStream.hxx" #include "InputPlugin.hxx" +#include "util/StringUtil.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" -#include <glib.h> #include <libmms/mmsx.h> -#include <string.h> -#include <errno.h> - struct MmsInputStream { InputStream base; @@ -61,10 +58,10 @@ input_mms_open(const char *url, Mutex &mutex, Cond &cond, Error &error) { - 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://")) + if (!StringStartsWith(url, "mms://") && + !StringStartsWith(url, "mmsh://") && + !StringStartsWith(url, "mmst://") && + !StringStartsWith(url, "mmsu://")) return nullptr; const auto mms = mmsx_connect(nullptr, nullptr, url, 128 * 1024); diff --git a/src/input/RewindInputPlugin.cxx b/src/input/RewindInputPlugin.cxx index e11f56631..78ab75660 100644 --- a/src/input/RewindInputPlugin.cxx +++ b/src/input/RewindInputPlugin.cxx @@ -21,7 +21,6 @@ #include "RewindInputPlugin.hxx" #include "InputStream.hxx" #include "InputPlugin.hxx" -#include "tag/Tag.hxx" #include <assert.h> #include <string.h> diff --git a/src/input/SmbclientInputPlugin.cxx b/src/input/SmbclientInputPlugin.cxx new file mode 100644 index 000000000..f97aff7d5 --- /dev/null +++ b/src/input/SmbclientInputPlugin.cxx @@ -0,0 +1,203 @@ +/* + * 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 "SmbclientInputPlugin.hxx" +#include "InputStream.hxx" +#include "InputPlugin.hxx" +#include "util/StringUtil.hxx" +#include "util/Error.hxx" + +#include <libsmbclient.h> + +#include <string.h> + +class SmbclientInputStream { + InputStream base; + + SMBCCTX *ctx; + int fd; + +public: + SmbclientInputStream(const char *uri, + Mutex &mutex, Cond &cond, + SMBCCTX *_ctx, int _fd, const struct stat &st) + :base(input_plugin_smbclient, uri, mutex, cond), + ctx(_ctx), fd(_fd) { + base.ready = true; + base.seekable = true; + base.size = st.st_size; + } + + ~SmbclientInputStream() { + smbc_close(fd); + smbc_free_context(ctx, 1); + } + + InputStream *GetBase() { + return &base; + } + + bool IsEOF() const { + return base.offset >= base.size; + } + + size_t Read(void *ptr, size_t size, Error &error) { + ssize_t nbytes = smbc_read(fd, ptr, size); + if (nbytes < 0) { + error.SetErrno("smbc_read() failed"); + nbytes = 0; + } + + return nbytes; + } + + bool Seek(InputStream::offset_type offset, int whence, Error &error) { + off_t result = smbc_lseek(fd, offset, whence); + if (result < 0) { + error.SetErrno("smbc_lseek() failed"); + return false; + } + + base.offset = result; + return true; + } +}; + +static void +mpd_smbc_get_auth_data(gcc_unused const char *srv, + gcc_unused const char *shr, + char *wg, gcc_unused int wglen, + char *un, gcc_unused int unlen, + char *pw, gcc_unused int pwlen) +{ + // TODO: implement + strcpy(wg, "WORKGROUP"); + strcpy(un, "foo"); + strcpy(pw, "bar"); +} + +/* + * InputPlugin methods + * + */ + +static bool +input_smbclient_init(gcc_unused const config_param ¶m, Error &error) +{ + constexpr int debug = 0; + if (smbc_init(mpd_smbc_get_auth_data, debug) < 0) { + error.SetErrno("smbc_init() failed"); + return false; + } + + // TODO: create one global SMBCCTX here? + + // TODO: evaluate config_param, call smbc_setOption*() + + return true; +} + +static InputStream * +input_smbclient_open(const char *uri, + Mutex &mutex, Cond &cond, + Error &error) +{ + if (!StringStartsWith(uri, "smb://")) + return nullptr; + + SMBCCTX *ctx = smbc_new_context(); + if (ctx == nullptr) { + error.SetErrno("smbc_new_context() failed"); + return nullptr; + } + + SMBCCTX *ctx2 = smbc_init_context(ctx); + if (ctx2 == nullptr) { + error.SetErrno("smbc_init_context() failed"); + smbc_free_context(ctx, 1); + return nullptr; + } + + ctx = ctx2; + + int fd = smbc_open(uri, O_RDONLY, 0); + if (fd < 0) { + error.SetErrno("smbc_open() failed"); + smbc_free_context(ctx, 1); + return nullptr; + } + + struct stat st; + if (smbc_fstat(fd, &st) < 0) { + error.SetErrno("smbc_fstat() failed"); + smbc_close(fd); + smbc_free_context(ctx, 1); + return nullptr; + } + + auto s = new SmbclientInputStream(uri, mutex, cond, ctx, fd, st); + return s->GetBase(); +} + +static size_t +input_smbclient_read(InputStream *is, void *ptr, size_t size, + Error &error) +{ + SmbclientInputStream &s = *(SmbclientInputStream *)is; + return s.Read(ptr, size, error); +} + +static void +input_smbclient_close(InputStream *is) +{ + SmbclientInputStream *s = (SmbclientInputStream *)is; + delete s; +} + +static bool +input_smbclient_eof(InputStream *is) +{ + SmbclientInputStream &s = *(SmbclientInputStream *)is; + return s.IsEOF(); +} + +static bool +input_smbclient_seek(InputStream *is, + InputPlugin::offset_type offset, int whence, + Error &error) +{ + SmbclientInputStream &s = *(SmbclientInputStream *)is; + return s.Seek(offset, whence, error); +} + +const InputPlugin input_plugin_smbclient = { + "smbclient", + input_smbclient_init, + nullptr, + input_smbclient_open, + input_smbclient_close, + nullptr, + nullptr, + nullptr, + nullptr, + input_smbclient_read, + input_smbclient_eof, + input_smbclient_seek, +}; diff --git a/src/input/SmbclientInputPlugin.hxx b/src/input/SmbclientInputPlugin.hxx new file mode 100644 index 000000000..7203a01b8 --- /dev/null +++ b/src/input/SmbclientInputPlugin.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_SMBCLIENT_H +#define MPD_INPUT_SMBCLIENT_H + +extern const struct InputPlugin input_plugin_smbclient; + +#endif diff --git a/src/ls.cxx b/src/ls.cxx index b1a636416..1de515a1f 100644 --- a/src/ls.cxx +++ b/src/ls.cxx @@ -19,14 +19,11 @@ #include "config.h" #include "ls.hxx" +#include "util/StringUtil.hxx" #include "util/UriUtil.hxx" #include "Client.hxx" -#include <glib.h> - #include <assert.h> -#include <string.h> - /** * file:// is not included in remoteUrlPrefixes, the connection method @@ -52,12 +49,18 @@ static const char *remoteUrlPrefixes[] = { "rtmpt://", "rtmps://", #endif +#ifdef ENABLE_SMBCLIENT + "smb://", +#endif #ifdef ENABLE_CDIO_PARANOIA "cdda://", #endif #ifdef ENABLE_DESPOTIFY "spt://", #endif +#ifdef HAVE_ALSA + "alsa://", +#endif NULL }; @@ -92,7 +95,7 @@ bool uri_supported_scheme(const char *uri) assert(uri_has_scheme(uri)); while (*urlPrefixes) { - if (g_str_has_prefix(uri, *urlPrefixes)) + if (StringStartsWith(uri, *urlPrefixes)) return true; urlPrefixes++; } diff --git a/src/ls.hxx b/src/ls.hxx index 3879563ee..6d5f989bc 100644 --- a/src/ls.hxx +++ b/src/ls.hxx @@ -20,6 +20,8 @@ #ifndef MPD_LS_HXX #define MPD_LS_HXX +#include "Compiler.h" + #include <stdio.h> class Client; @@ -29,6 +31,7 @@ class Client; * It is not allowed to pass an URI without a scheme, check with * uri_has_scheme() first. */ +gcc_pure bool uri_supported_scheme(const char *url); /** diff --git a/src/mixer/AlsaMixerPlugin.cxx b/src/mixer/AlsaMixerPlugin.cxx index 4a4ca433c..75ba8825c 100644 --- a/src/mixer/AlsaMixerPlugin.cxx +++ b/src/mixer/AlsaMixerPlugin.cxx @@ -27,6 +27,7 @@ #include "event/Call.hxx" #include "util/ASCII.hxx" #include "util/ReusableArray.hxx" +#include "util/Clamp.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" #include "Log.hxx" @@ -47,7 +48,7 @@ class AlsaMixerMonitor final : private MultiSocketMonitor { public: AlsaMixerMonitor(EventLoop &_loop, snd_mixer_t *_mixer) :MultiSocketMonitor(_loop), mixer(_mixer) { -#ifdef USE_EPOLL +#ifdef USE_INTERNAL_EVENTLOOP _loop.AddCall([this](){ InvalidateSockets(); }); #else _loop.AddIdle(InitAlsaMixerMonitor, this); @@ -55,7 +56,7 @@ public: } private: -#ifndef USE_EPOLL +#ifndef USE_INTERNAL_EVENTLOOP static gboolean InitAlsaMixerMonitor(gpointer data) { AlsaMixerMonitor &amm = *(AlsaMixerMonitor *)data; amm.InvalidateSockets(); @@ -372,8 +373,7 @@ AlsaMixer::SetVolume(unsigned volume, Error &error) level = (long)(((vol / 100.0) * (volume_max - volume_min) + volume_min) + 0.5); - level = level > volume_max ? volume_max : level; - level = level < volume_min ? volume_min : level; + level = Clamp(level, volume_min, volume_max); err = snd_mixer_selem_set_playback_volume_all(elem, level); if (err < 0) { diff --git a/src/mixer/OssMixerPlugin.cxx b/src/mixer/OssMixerPlugin.cxx index 0a459bc97..7a91cfcb1 100644 --- a/src/mixer/OssMixerPlugin.cxx +++ b/src/mixer/OssMixerPlugin.cxx @@ -19,7 +19,7 @@ #include "config.h" #include "MixerInternal.hxx" -#include "OutputAPI.hxx" +#include "ConfigData.hxx" #include "system/fd_util.h" #include "util/ASCII.hxx" #include "util/Error.hxx" @@ -28,10 +28,8 @@ #include <assert.h> #include <string.h> -#include <sys/stat.h> #include <sys/ioctl.h> #include <fcntl.h> -#include <errno.h> #include <stdlib.h> #include <unistd.h> diff --git a/src/mixer/PulseMixerPlugin.cxx b/src/mixer/PulseMixerPlugin.cxx index ff10256cb..84d42c392 100644 --- a/src/mixer/PulseMixerPlugin.cxx +++ b/src/mixer/PulseMixerPlugin.cxx @@ -26,7 +26,6 @@ #include "util/Domain.hxx" #include "Log.hxx" -#include <pulse/thread-mainloop.h> #include <pulse/context.h> #include <pulse/introspect.h> #include <pulse/stream.h> @@ -34,7 +33,6 @@ #include <pulse/error.h> #include <assert.h> -#include <string.h> struct PulseMixer final : public Mixer { PulseOutput *output; diff --git a/src/mixer/PulseMixerPlugin.hxx b/src/mixer/PulseMixerPlugin.hxx index fa73e0f5e..46bd06a01 100644 --- a/src/mixer/PulseMixerPlugin.hxx +++ b/src/mixer/PulseMixerPlugin.hxx @@ -20,20 +20,17 @@ #ifndef MPD_PULSE_MIXER_PLUGIN_HXX #define MPD_PULSE_MIXER_PLUGIN_HXX -#include <pulse/def.h> - struct PulseMixer; struct pa_context; struct pa_stream; void -pulse_mixer_on_connect(PulseMixer *pm, struct pa_context *context); +pulse_mixer_on_connect(PulseMixer *pm, pa_context *context); void pulse_mixer_on_disconnect(PulseMixer *pm); void -pulse_mixer_on_change(PulseMixer *pm, - struct pa_context *context, struct pa_stream *stream); +pulse_mixer_on_change(PulseMixer *pm, pa_context *context, pa_stream *stream); #endif diff --git a/src/mixer/RoarMixerPlugin.cxx b/src/mixer/RoarMixerPlugin.cxx index 6bd700551..75147329e 100644 --- a/src/mixer/RoarMixerPlugin.cxx +++ b/src/mixer/RoarMixerPlugin.cxx @@ -21,8 +21,8 @@ #include "config.h" #include "MixerInternal.hxx" -#include "OutputAPI.hxx" #include "output/RoarOutputPlugin.hxx" +#include "Compiler.h" struct RoarMixer final : public Mixer { /** the base mixer class */ diff --git a/src/mixer/SoftwareMixerPlugin.cxx b/src/mixer/SoftwareMixerPlugin.cxx index 193e68f23..e5f4b1659 100644 --- a/src/mixer/SoftwareMixerPlugin.cxx +++ b/src/mixer/SoftwareMixerPlugin.cxx @@ -24,7 +24,7 @@ #include "FilterRegistry.hxx" #include "FilterInternal.hxx" #include "filter/VolumeFilterPlugin.hxx" -#include "pcm/PcmVolume.hxx" +#include "pcm/Volume.hxx" #include "ConfigData.hxx" #include "util/Error.hxx" diff --git a/src/output/AlsaOutputPlugin.cxx b/src/output/AlsaOutputPlugin.cxx index 4877d3a46..b5ca511b2 100644 --- a/src/output/AlsaOutputPlugin.cxx +++ b/src/output/AlsaOutputPlugin.cxx @@ -27,7 +27,6 @@ #include "util/Domain.hxx" #include "Log.hxx" -#include <glib.h> #include <alsa/asoundlib.h> #include <string> @@ -118,7 +117,7 @@ struct AlsaOutput { * It contains silence samples, enough to fill one period (see * #period_frames). */ - void *silence; + uint8_t *silence; AlsaOutput():mode(0), writei(snd_pcm_writei) { } @@ -593,8 +592,8 @@ configure_hw: ad->period_frames = alsa_period_size; ad->period_position = 0; - ad->silence = g_malloc(snd_pcm_frames_to_bytes(ad->pcm, - alsa_period_size)); + ad->silence = new uint8_t[snd_pcm_frames_to_bytes(ad->pcm, + alsa_period_size)]; snd_pcm_format_set_silence(format, ad->silence, alsa_period_size * channels); @@ -641,7 +640,7 @@ alsa_setup_dsd(AlsaOutput *ad, const AudioFormat audio_format, error.Format(alsa_output_domain, "Failed to configure DSD-over-USB on ALSA device \"%s\"", alsa_device(ad)); - g_free(ad->silence); + delete[] ad->silence; return false; } @@ -811,7 +810,7 @@ alsa_close(struct audio_output *ao) AlsaOutput *ad = (AlsaOutput *)ao; snd_pcm_close(ad->pcm); - g_free(ad->silence); + delete[] ad->silence; } static size_t diff --git a/src/output/FifoOutputPlugin.cxx b/src/output/FifoOutputPlugin.cxx index aeb9a6a87..7963d8c82 100644 --- a/src/output/FifoOutputPlugin.cxx +++ b/src/output/FifoOutputPlugin.cxx @@ -22,7 +22,6 @@ #include "ConfigError.hxx" #include "OutputAPI.hxx" #include "Timer.hxx" -#include "system/fd_util.h" #include "fs/AllocatedPath.hxx" #include "fs/FileSystem.hxx" #include "util/Error.hxx" @@ -30,10 +29,8 @@ #include "Log.hxx" #include "open.h" -#include <sys/types.h> #include <sys/stat.h> #include <errno.h> -#include <string.h> #include <unistd.h> #define FIFO_BUFFER_SIZE 65536 /* pipe capacity on Linux >= 2.6.11 */ diff --git a/src/output/HttpdOutputPlugin.cxx b/src/output/HttpdOutputPlugin.cxx index 369c06937..8cbc150ad 100644 --- a/src/output/HttpdOutputPlugin.cxx +++ b/src/output/HttpdOutputPlugin.cxx @@ -33,8 +33,6 @@ #include "util/Domain.hxx" #include "Log.hxx" -#include <glib.h> - #include <assert.h> #include <sys/types.h> @@ -199,9 +197,10 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address, #ifdef HAVE_LIBWRAP if (address.sa_family != AF_UNIX) { - char *hostaddr = sockaddr_to_string(&address, address_length, - IgnoreError()); - const char *progname = g_get_prgname(); + const auto hostaddr = sockaddr_to_string(&address, + address_length); + // TODO: shall we obtain the program name from argv[0]? + const char *progname = "mpd"; struct request_info req; request_init(&req, RQ_FILE, fd, RQ_DAEMON, progname, 0); @@ -212,13 +211,10 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address, /* tcp wrappers says no */ FormatWarning(httpd_output_domain, "libwrap refused connection (libwrap=%s) from %s", - progname, hostaddr); - g_free(hostaddr); + progname, hostaddr.c_str()); close_socket(fd); return; } - - g_free(hostaddr); } #else (void)address; diff --git a/src/output/JackOutputPlugin.cxx b/src/output/JackOutputPlugin.cxx index 7ed672f95..ef06b5abf 100644 --- a/src/output/JackOutputPlugin.cxx +++ b/src/output/JackOutputPlugin.cxx @@ -34,10 +34,6 @@ #include <stdlib.h> #include <string.h> -#include <stdio.h> -#include <sys/types.h> -#include <unistd.h> -#include <errno.h> enum { MAX_PORTS = 16, diff --git a/src/output/NullOutputPlugin.cxx b/src/output/NullOutputPlugin.cxx index e2eec9dbc..a901b7d2a 100644 --- a/src/output/NullOutputPlugin.cxx +++ b/src/output/NullOutputPlugin.cxx @@ -22,8 +22,6 @@ #include "OutputAPI.hxx" #include "Timer.hxx" -#include <assert.h> - struct NullOutput { struct audio_output base; diff --git a/src/output/OSXOutputPlugin.cxx b/src/output/OSXOutputPlugin.cxx index 97ebae056..907cb0346 100644 --- a/src/output/OSXOutputPlugin.cxx +++ b/src/output/OSXOutputPlugin.cxx @@ -20,7 +20,7 @@ #include "config.h" #include "OSXOutputPlugin.hxx" #include "OutputAPI.hxx" -#include "util/fifo_buffer.h" +#include "util/DynamicFifoBuffer.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" #include "thread/Mutex.hxx" @@ -44,7 +44,7 @@ struct OSXOutput { Mutex mutex; Cond condition; - struct fifo_buffer *buffer; + DynamicFifoBuffer<uint8_t> *buffer; }; static constexpr Domain osx_output_domain("osx_output"); @@ -207,22 +207,19 @@ osx_render(void *vdata, od->mutex.lock(); - size_t nbytes; - const void *src = fifo_buffer_read(od->buffer, &nbytes); + auto src = od->buffer->Read(); + if (!src.IsEmpty()) { + if (src.size > buffer_size) + src.size = buffer_size; - if (src != NULL) { - if (nbytes > buffer_size) - nbytes = buffer_size; - - memcpy(buffer->mData, src, nbytes); - fifo_buffer_consume(od->buffer, nbytes); - } else - nbytes = 0; + memcpy(buffer->mData, src.data, src.size); + od->buffer->Consume(src.size); + } od->condition.signal(); od->mutex.unlock(); - buffer->mDataByteSize = nbytes; + buffer->mDataByteSize = src.size; unsigned i; for (i = 1; i < buffer_list->mNumberBuffers; ++i) { @@ -298,7 +295,7 @@ osx_output_cancel(struct audio_output *ao) OSXOutput *od = (OSXOutput *)ao; const ScopeLock protect(od->mutex); - fifo_buffer_clear(od->buffer); + od->buffer->Clear(); } static void @@ -309,7 +306,7 @@ osx_output_close(struct audio_output *ao) AudioOutputUnitStop(od->au); AudioUnitUninitialize(od->au); - fifo_buffer_free(od->buffer); + delete od->buffer; } static bool @@ -370,8 +367,8 @@ osx_output_open(struct audio_output *ao, AudioFormat &audio_format, } /* create a buffer of 1s */ - od->buffer = fifo_buffer_new(audio_format.sample_rate * - audio_format.GetFrameSize()); + od->buffer = new DynamicFifoBuffer<uint8_t>(audio_format.sample_rate * + audio_format.GetFrameSize()); status = AudioOutputUnitStart(od->au); if (status != 0) { @@ -393,23 +390,21 @@ osx_output_play(struct audio_output *ao, const void *chunk, size_t size, const ScopeLock protect(od->mutex); - void *dest; - size_t max_length; - + DynamicFifoBuffer<uint8_t>::Range dest; while (true) { - dest = fifo_buffer_write(od->buffer, &max_length); - if (dest != NULL) + dest = od->buffer->Write(); + if (!dest.IsEmpty()) break; /* wait for some free space in the buffer */ od->condition.wait(od->mutex); } - if (size > max_length) - size = max_length; + if (size > dest.size) + size = dest.size; - memcpy(dest, chunk, size); - fifo_buffer_append(od->buffer, size); + memcpy(dest.data, chunk, size); + od->buffer->Append(size); return size; } diff --git a/src/output/PulseOutputPlugin.hxx b/src/output/PulseOutputPlugin.hxx index 0ed8404bc..69d6c5f99 100644 --- a/src/output/PulseOutputPlugin.hxx +++ b/src/output/PulseOutputPlugin.hxx @@ -41,6 +41,6 @@ pulse_output_clear_mixer(PulseOutput *po, PulseMixer *pm); bool pulse_output_set_volume(PulseOutput *po, - const struct pa_cvolume *volume, Error &error); + const pa_cvolume *volume, Error &error); #endif diff --git a/src/pcm/ChannelsConverter.cxx b/src/pcm/ChannelsConverter.cxx new file mode 100644 index 000000000..8ffcbfe41 --- /dev/null +++ b/src/pcm/ChannelsConverter.cxx @@ -0,0 +1,97 @@ +/* + * 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 "ChannelsConverter.hxx" +#include "PcmChannels.hxx" +#include "Domain.hxx" +#include "util/ConstBuffer.hxx" +#include "util/Error.hxx" + +#include <assert.h> + +bool +PcmChannelsConverter::Open(SampleFormat _format, + unsigned _src_channels, unsigned _dest_channels, + gcc_unused Error &error) +{ + assert(_format != SampleFormat::UNDEFINED); + + switch (_format) { + case SampleFormat::S16: + case SampleFormat::S24_P32: + case SampleFormat::S32: + case SampleFormat::FLOAT: + break; + + default: + error.Format(pcm_domain, + "PCM channel conversion for %s is not implemented", + sample_format_to_string(format)); + return false; + } + + format = _format; + src_channels = _src_channels; + dest_channels = _dest_channels; + return true; +} + +void +PcmChannelsConverter::Close() +{ +#ifndef NDEBUG + format = SampleFormat::UNDEFINED; +#endif +} + +ConstBuffer<void> +PcmChannelsConverter::Convert(ConstBuffer<void> src, gcc_unused Error &error) +{ + switch (format) { + case SampleFormat::UNDEFINED: + case SampleFormat::S8: + case SampleFormat::DSD: + assert(false); + gcc_unreachable(); + + case SampleFormat::S16: + return pcm_convert_channels_16(buffer, dest_channels, + src_channels, + ConstBuffer<int16_t>::FromVoid(src)).ToVoid(); + + case SampleFormat::S24_P32: + return pcm_convert_channels_24(buffer, dest_channels, + src_channels, + ConstBuffer<int32_t>::FromVoid(src)).ToVoid(); + + case SampleFormat::S32: + return pcm_convert_channels_32(buffer, dest_channels, + src_channels, + ConstBuffer<int32_t>::FromVoid(src)).ToVoid(); + + case SampleFormat::FLOAT: + return pcm_convert_channels_float(buffer, dest_channels, + src_channels, + ConstBuffer<float>::FromVoid(src)).ToVoid(); + } + + assert(false); + gcc_unreachable(); +} diff --git a/src/pcm/ChannelsConverter.hxx b/src/pcm/ChannelsConverter.hxx new file mode 100644 index 000000000..4311b9671 --- /dev/null +++ b/src/pcm/ChannelsConverter.hxx @@ -0,0 +1,83 @@ +/* + * 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_PCM_CHANNELS_CONVERTER_HXX +#define MPD_PCM_CHANNELS_CONVERTER_HXX + +#include "check.h" +#include "AudioFormat.hxx" +#include "PcmBuffer.hxx" + +#ifndef NDEBUG +#include <assert.h> +#endif + +class Error; +template<typename T> struct ConstBuffer; + +/** + * A class that converts samples from one format to another. + */ +class PcmChannelsConverter { + SampleFormat format; + unsigned src_channels, dest_channels; + + PcmBuffer buffer; + +public: +#ifndef NDEBUG + PcmChannelsConverter() + :format(SampleFormat::UNDEFINED) {} + + ~PcmChannelsConverter() { + assert(format == SampleFormat::UNDEFINED); + } +#endif + + /** + * Opens the object, prepare for Convert(). + * + * @param format the sample format + * @param src_channels the number of source channels + * @param dest_channels the number of destination channels + * @param error location to store the error + * @return true on success + */ + bool Open(SampleFormat format, + unsigned src_channels, unsigned dest_channels, + Error &error); + + /** + * Closes the object. After that, you may call Open() again. + */ + void Close(); + + /** + * Convert a block of PCM data. + * + * @param src the input buffer + * @param error location to store the error + * @return the destination buffer on success, + * ConstBuffer::Null() on error + */ + gcc_pure + ConstBuffer<void> Convert(ConstBuffer<void> src, Error &error); +}; + +#endif diff --git a/src/pcm/ConfiguredResampler.cxx b/src/pcm/ConfiguredResampler.cxx new file mode 100644 index 000000000..845fa2332 --- /dev/null +++ b/src/pcm/ConfiguredResampler.cxx @@ -0,0 +1,101 @@ +/* + * 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 "ConfiguredResampler.hxx" +#include "FallbackResampler.hxx" +#include "ConfigGlobal.hxx" +#include "ConfigOption.hxx" +#include "ConfigError.hxx" +#include "util/Error.hxx" + +#ifdef HAVE_LIBSAMPLERATE +#include "LibsamplerateResampler.hxx" +#endif + +#ifdef HAVE_SOXR +#include "SoxrResampler.hxx" +#endif + +#include <string.h> + +enum class SelectedResampler { + FALLBACK, + +#ifdef HAVE_LIBSAMPLERATE + LIBSAMPLERATE, +#endif + +#ifdef HAVE_SOXR + SOXR, +#endif +}; + +static SelectedResampler selected_resampler = SelectedResampler::FALLBACK; + +bool +pcm_resampler_global_init(Error &error) +{ + const char *converter = + config_get_string(CONF_SAMPLERATE_CONVERTER, ""); + + if (strcmp(converter, "internal") == 0) + return true; + +#ifdef HAVE_SOXR + if (strcmp(converter, "soxr") == 0) { + selected_resampler = SelectedResampler::SOXR; + return true; + } +#endif + +#ifdef HAVE_LIBSAMPLERATE + selected_resampler = SelectedResampler::LIBSAMPLERATE; + return pcm_resample_lsr_global_init(converter, error); +#endif + + if (*converter == 0) + return true; + + error.Format(config_domain, + "The samplerate_converter '%s' is not available", + converter); + return false; +} + +PcmResampler * +pcm_resampler_create() +{ + switch (selected_resampler) { + case SelectedResampler::FALLBACK: + return new FallbackPcmResampler(); + +#ifdef HAVE_LIBSAMPLERATE + case SelectedResampler::LIBSAMPLERATE: + return new LibsampleratePcmResampler(); +#endif + +#ifdef HAVE_SOXR + case SelectedResampler::SOXR: + return new SoxrPcmResampler(); +#endif + } + + gcc_unreachable(); +} diff --git a/src/pcm/ConfiguredResampler.hxx b/src/pcm/ConfiguredResampler.hxx new file mode 100644 index 000000000..6d12ee9c6 --- /dev/null +++ b/src/pcm/ConfiguredResampler.hxx @@ -0,0 +1,38 @@ +/* + * 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_CONFIGURED_RESAMPLER_HXX +#define MPD_CONFIGURED_RESAMPLER_HXX + +#include "check.h" + +class Error; +class PcmResampler; + +bool +pcm_resampler_global_init(Error &error); + +/** + * Create a #PcmResampler instance from the implementation class + * configured in mpd.conf. + */ +PcmResampler * +pcm_resampler_create(); + +#endif diff --git a/src/pcm/Domain.cxx b/src/pcm/Domain.cxx new file mode 100644 index 000000000..9f07d80dd --- /dev/null +++ b/src/pcm/Domain.cxx @@ -0,0 +1,23 @@ +/* + * 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 "Domain.hxx" +#include "util/Domain.hxx" + +const Domain pcm_domain("pcm"); diff --git a/src/pcm/Domain.hxx b/src/pcm/Domain.hxx new file mode 100644 index 000000000..170d7406d --- /dev/null +++ b/src/pcm/Domain.hxx @@ -0,0 +1,27 @@ +/* + * 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 PCM_DOMAIN_HXX +#define PCM_DOMAIN_HXX + +class Domain; + +extern const Domain pcm_domain; + +#endif diff --git a/src/pcm/FallbackResampler.cxx b/src/pcm/FallbackResampler.cxx new file mode 100644 index 000000000..a3b6b78ee --- /dev/null +++ b/src/pcm/FallbackResampler.cxx @@ -0,0 +1,147 @@ +/* + * 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 "FallbackResampler.hxx" + +#include <assert.h> + +AudioFormat +FallbackPcmResampler::Open(AudioFormat &af, unsigned new_sample_rate, + gcc_unused Error &error) +{ + assert(af.IsValid()); + assert(audio_valid_sample_rate(new_sample_rate)); + + switch (af.format) { + case SampleFormat::UNDEFINED: + assert(false); + gcc_unreachable(); + + case SampleFormat::S8: + af.format = SampleFormat::S16; + break; + + case SampleFormat::S16: + case SampleFormat::FLOAT: + case SampleFormat::S24_P32: + case SampleFormat::S32: + break; + + case SampleFormat::DSD: + af.format = SampleFormat::FLOAT; + break; + } + + format = af; + out_rate = new_sample_rate; + + AudioFormat result = af; + result.sample_rate = new_sample_rate; + return result; +} + +void +FallbackPcmResampler::Close() +{ +} + +template<typename T> +static ConstBuffer<T> +pcm_resample_fallback(PcmBuffer &buffer, + unsigned channels, + unsigned src_rate, + ConstBuffer<T> src, + unsigned dest_rate) +{ + unsigned dest_pos = 0; + unsigned src_frames = src.size / channels; + unsigned dest_frames = + (src_frames * dest_rate + src_rate - 1) / src_rate; + unsigned dest_samples = dest_frames * channels; + size_t dest_size = dest_samples * sizeof(*src.data); + T *dest_buffer = (T *)buffer.Get(dest_size); + + assert((src.size % channels) == 0); + + switch (channels) { + case 1: + while (dest_pos < dest_samples) { + unsigned src_pos = dest_pos * src_rate / dest_rate; + + dest_buffer[dest_pos++] = src.data[src_pos]; + } + break; + case 2: + while (dest_pos < dest_samples) { + unsigned src_pos = dest_pos * src_rate / dest_rate; + src_pos &= ~1; + + dest_buffer[dest_pos++] = src.data[src_pos]; + dest_buffer[dest_pos++] = src.data[src_pos + 1]; + } + break; + } + + return { dest_buffer, dest_samples }; +} + +template<typename T> +static ConstBuffer<void> +pcm_resample_fallback_void(PcmBuffer &buffer, + unsigned channels, + unsigned src_rate, + ConstBuffer<void> src, + unsigned dest_rate) +{ + const auto typed_src = ConstBuffer<T>::FromVoid(src); + return pcm_resample_fallback(buffer, channels, src_rate, typed_src, + dest_rate); +} + +ConstBuffer<void> +FallbackPcmResampler::Resample(ConstBuffer<void> src, gcc_unused Error &error) +{ + switch (format.format) { + case SampleFormat::UNDEFINED: + case SampleFormat::S8: + case SampleFormat::DSD: + assert(false); + gcc_unreachable(); + + case SampleFormat::S16: + return pcm_resample_fallback_void<int16_t>(buffer, + format.channels, + format.sample_rate, + src, + out_rate); + + case SampleFormat::FLOAT: + case SampleFormat::S24_P32: + case SampleFormat::S32: + return pcm_resample_fallback_void<int32_t>(buffer, + format.channels, + format.sample_rate, + src, + out_rate); + } + + assert(false); + gcc_unreachable(); +} diff --git a/src/pcm/FallbackResampler.hxx b/src/pcm/FallbackResampler.hxx new file mode 100644 index 000000000..1b8d0377d --- /dev/null +++ b/src/pcm/FallbackResampler.hxx @@ -0,0 +1,45 @@ +/* + * 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_PCM_FALLBACK_RESAMPLER_HXX +#define MPD_PCM_FALLBACK_RESAMPLER_HXX + +#include "Resampler.hxx" +#include "PcmBuffer.hxx" +#include "AudioFormat.hxx" + +/** + * A naive resampler that is used when no external library was found + * (or when the user explicitly asks for bad quality). + */ +class FallbackPcmResampler final : public PcmResampler { + AudioFormat format; + unsigned out_rate; + + PcmBuffer buffer; + +public: + virtual AudioFormat Open(AudioFormat &af, unsigned new_sample_rate, + Error &error) override; + virtual void Close() override; + virtual ConstBuffer<void> Resample(ConstBuffer<void> src, + Error &error) override; +}; + +#endif diff --git a/src/pcm/FormatConverter.cxx b/src/pcm/FormatConverter.cxx new file mode 100644 index 000000000..8886a8ab0 --- /dev/null +++ b/src/pcm/FormatConverter.cxx @@ -0,0 +1,104 @@ +/* + * 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 "FormatConverter.hxx" +#include "PcmFormat.hxx" +#include "Domain.hxx" +#include "util/ConstBuffer.hxx" +#include "util/Error.hxx" + +#include <assert.h> + +bool +PcmFormatConverter::Open(SampleFormat _src_format, SampleFormat _dest_format, + gcc_unused Error &error) +{ + assert(_src_format != SampleFormat::UNDEFINED); + assert(_dest_format != SampleFormat::UNDEFINED); + + src_format = _src_format; + dest_format = _dest_format; + return true; +} + +void +PcmFormatConverter::Close() +{ +#ifndef NDEBUG + src_format = SampleFormat::UNDEFINED; + dest_format = SampleFormat::UNDEFINED; +#endif +} + +ConstBuffer<void> +PcmFormatConverter::Convert(ConstBuffer<void> src, Error &error) +{ + const void *result = nullptr; + size_t result_size = 0; + + switch (dest_format) { + case SampleFormat::UNDEFINED: + assert(false); + gcc_unreachable(); + + case SampleFormat::S8: + case SampleFormat::DSD: + result = nullptr; + break; + + case SampleFormat::S16: + result = pcm_convert_to_16(buffer, dither, + src_format, + src.data, src.size, + &result_size); + break; + + case SampleFormat::S24_P32: + result = pcm_convert_to_24(buffer, + src_format, + src.data, src.size, + &result_size); + break; + + case SampleFormat::S32: + result = pcm_convert_to_32(buffer, + src_format, + src.data, src.size, + &result_size); + break; + + case SampleFormat::FLOAT: + result = pcm_convert_to_float(buffer, + src_format, + src.data, src.size, + &result_size); + break; + } + + if (result == nullptr) { + error.Format(pcm_domain, + "PCM conversion from %s to %s is not implemented", + sample_format_to_string(src_format), + sample_format_to_string(dest_format)); + return nullptr; + } + + return { result, result_size }; +} diff --git a/src/pcm/FormatConverter.hxx b/src/pcm/FormatConverter.hxx new file mode 100644 index 000000000..f5b13a0b0 --- /dev/null +++ b/src/pcm/FormatConverter.hxx @@ -0,0 +1,84 @@ +/* + * 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_PCM_FORMAT_CONVERTER_HXX +#define MPD_PCM_FORMAT_CONVERTER_HXX + +#include "check.h" +#include "AudioFormat.hxx" +#include "PcmBuffer.hxx" +#include "PcmDither.hxx" + +#ifndef NDEBUG +#include <assert.h> +#endif + +class Error; +template<typename T> struct ConstBuffer; + +/** + * A class that converts samples from one format to another. + */ +class PcmFormatConverter { + SampleFormat src_format, dest_format; + + PcmBuffer buffer; + PcmDither dither; + +public: +#ifndef NDEBUG + PcmFormatConverter() + :src_format(SampleFormat::UNDEFINED), + dest_format(SampleFormat::UNDEFINED) {} + + ~PcmFormatConverter() { + assert(src_format == SampleFormat::UNDEFINED); + assert(dest_format == SampleFormat::UNDEFINED); + } +#endif + + /** + * Opens the object, prepare for Convert(). + * + * @param src_format the sample format of incoming data + * @param dest_format the sample format of outgoing data + * @param error location to store the error + * @return true on success + */ + bool Open(SampleFormat src_format, SampleFormat dest_format, + Error &error); + + /** + * Closes the object. After that, you may call Open() again. + */ + void Close(); + + /** + * Convert a block of PCM data. + * + * @param src the input buffer + * @param error location to store the error + * @return the destination buffer on success, + * ConstBuffer::Null() on error + */ + gcc_pure + ConstBuffer<void> Convert(ConstBuffer<void> src, Error &error); +}; + +#endif diff --git a/src/pcm/GlueResampler.cxx b/src/pcm/GlueResampler.cxx new file mode 100644 index 000000000..ef80e08a5 --- /dev/null +++ b/src/pcm/GlueResampler.cxx @@ -0,0 +1,85 @@ +/* + * 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 "GlueResampler.hxx" +#include "ConfiguredResampler.hxx" +#include "Resampler.hxx" + +#include <assert.h> + +GluePcmResampler::GluePcmResampler() + :resampler(pcm_resampler_create()) {} + +GluePcmResampler::~GluePcmResampler() +{ + delete resampler; +} + +bool +GluePcmResampler::Open(AudioFormat src_format, unsigned new_sample_rate, + Error &error) +{ + assert(src_format.IsValid()); + assert(audio_valid_sample_rate(new_sample_rate)); + + AudioFormat requested_format = src_format; + AudioFormat dest_format = resampler->Open(requested_format, + new_sample_rate, + error); + if (!dest_format.IsValid()) + return false; + + assert(requested_format.channels == src_format.channels); + assert(dest_format.channels == src_format.channels); + assert(dest_format.sample_rate == new_sample_rate); + + if (requested_format.format != src_format.format && + !format_converter.Open(src_format.format, requested_format.format, + error)) + return false; + + src_sample_format = src_format.format; + requested_sample_format = requested_format.format; + output_sample_format = dest_format.format; + return true; +} + +void +GluePcmResampler::Close() +{ + if (requested_sample_format != src_sample_format) + format_converter.Close(); + + resampler->Close(); +} + +ConstBuffer<void> +GluePcmResampler::Resample(ConstBuffer<void> src, Error &error) +{ + assert(!src.IsNull()); + + if (requested_sample_format != src_sample_format) { + src = format_converter.Convert(src, error); + if (src.IsNull()) + return nullptr; + } + + return resampler->Resample(src, error); +} diff --git a/src/pcm/GlueResampler.hxx b/src/pcm/GlueResampler.hxx new file mode 100644 index 000000000..7bd923bab --- /dev/null +++ b/src/pcm/GlueResampler.hxx @@ -0,0 +1,63 @@ +/* + * 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_GLUE_RESAMPLER_HXX +#define MPD_GLUE_RESAMPLER_HXX + +#include "check.h" +#include "AudioFormat.hxx" +#include "FormatConverter.hxx" + +class Error; +class PcmResampler; +template<typename T> struct ConstBuffer; + +/** + * A glue class that integrates a #PcmResampler and automatically + * converts source data to the sample format required by the + * #PcmResampler instance. + */ +class GluePcmResampler { + PcmResampler *const resampler; + + SampleFormat src_sample_format, requested_sample_format; + SampleFormat output_sample_format; + + /** + * This object converts input data to the sample format + * requested by the #PcmResampler. + */ + PcmFormatConverter format_converter; + +public: + GluePcmResampler(); + ~GluePcmResampler(); + + bool Open(AudioFormat src_format, unsigned new_sample_rate, + Error &error); + void Close(); + + SampleFormat GetOutputSampleFormat() const { + return output_sample_format; + } + + ConstBuffer<void> Resample(ConstBuffer<void> src, Error &error); +}; + +#endif diff --git a/src/pcm/LibsamplerateResampler.cxx b/src/pcm/LibsamplerateResampler.cxx new file mode 100644 index 000000000..586391e67 --- /dev/null +++ b/src/pcm/LibsamplerateResampler.cxx @@ -0,0 +1,163 @@ +/* + * 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 "LibsamplerateResampler.hxx" +#include "util/ASCII.hxx" +#include "util/Error.hxx" +#include "util/Domain.hxx" +#include "Log.hxx" + +#include <assert.h> +#include <stdlib.h> +#include <string.h> + +static constexpr Domain libsamplerate_domain("libsamplerate"); + +static int lsr_converter = SRC_SINC_FASTEST; + +static bool +lsr_parse_converter(const char *s) +{ + assert(s != nullptr); + + if (*s == 0) + return true; + + char *endptr; + long l = strtol(s, &endptr, 10); + if (*endptr == 0 && src_get_name(l) != nullptr) { + lsr_converter = l; + return true; + } + + size_t length = strlen(s); + for (int i = 0;; ++i) { + const char *name = src_get_name(i); + if (name == nullptr) + break; + + if (StringEqualsCaseASCII(s, name, length)) { + lsr_converter = i; + return true; + } + } + + return false; +} + +bool +pcm_resample_lsr_global_init(const char *converter, Error &error) +{ + if (!lsr_parse_converter(converter)) { + error.Format(libsamplerate_domain, + "unknown samplerate converter '%s'", converter); + return false; + } + + FormatDebug(libsamplerate_domain, + "libsamplerate converter '%s'", + src_get_name(lsr_converter)); + + return true; +} + +AudioFormat +LibsampleratePcmResampler::Open(AudioFormat &af, unsigned new_sample_rate, + Error &error) +{ + assert(af.IsValid()); + assert(audio_valid_sample_rate(new_sample_rate)); + + src_rate = af.sample_rate; + dest_rate = new_sample_rate; + channels = af.channels; + + /* libsamplerate works with floating point samples */ + af.format = SampleFormat::FLOAT; + + int src_error; + state = src_new(lsr_converter, channels, &src_error); + if (!state) { + error.Format(libsamplerate_domain, src_error, + "libsamplerate initialization has failed: %s", + src_strerror(src_error)); + return AudioFormat::Undefined(); + } + + memset(&data, 0, sizeof(data)); + + data.src_ratio = double(new_sample_rate) / double(af.sample_rate); + FormatDebug(libsamplerate_domain, + "setting samplerate conversion ratio to %.2lf", + data.src_ratio); + src_set_ratio(state, data.src_ratio); + + AudioFormat result = af; + result.sample_rate = new_sample_rate; + return result; +} + +void +LibsampleratePcmResampler::Close() +{ + state = src_delete(state); +} + +static bool +src_process(SRC_STATE *state, SRC_DATA *data, Error &error) +{ + int result = src_process(state, data); + if (result != 0) { + error.Format(libsamplerate_domain, result, + "libsamplerate has failed: %s", + src_strerror(result)); + return false; + } + + return true; +} + +inline ConstBuffer<float> +LibsampleratePcmResampler::Resample2(ConstBuffer<float> src, Error &error) +{ + assert(src.size % channels == 0); + + const unsigned src_frames = src.size / channels; + const unsigned dest_frames = + (src_frames * dest_rate + src_rate - 1) / src_rate; + size_t data_out_size = dest_frames * sizeof(float) * channels; + + data.data_in = const_cast<float *>(src.data); + data.data_out = (float *)buffer.Get(data_out_size); + data.input_frames = src_frames; + data.output_frames = dest_frames; + + if (!src_process(state, &data, error)) + return nullptr; + + return ConstBuffer<float>(data.data_out, + data.output_frames_gen * channels); +} + +ConstBuffer<void> +LibsampleratePcmResampler::Resample(ConstBuffer<void> src, Error &error) +{ + return Resample2(ConstBuffer<float>::FromVoid(src), error).ToVoid(); +} diff --git a/src/pcm/LibsamplerateResampler.hxx b/src/pcm/LibsamplerateResampler.hxx new file mode 100644 index 000000000..0c1f613c8 --- /dev/null +++ b/src/pcm/LibsamplerateResampler.hxx @@ -0,0 +1,55 @@ +/* + * 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_PCM_LIBSAMPLERATE_RESAMPLER_HXX +#define MPD_PCM_LIBSAMPLERATE_RESAMPLER_HXX + +#include "Resampler.hxx" +#include "PcmBuffer.hxx" +#include "AudioFormat.hxx" + +#include <samplerate.h> + +/** + * A resampler using libsamplerate. + */ +class LibsampleratePcmResampler final : public PcmResampler { + unsigned src_rate, dest_rate; + unsigned channels; + + SRC_STATE *state; + SRC_DATA data; + + PcmBuffer buffer; + +public: + virtual AudioFormat Open(AudioFormat &af, unsigned new_sample_rate, + Error &error) override; + virtual void Close() override; + virtual ConstBuffer<void> Resample(ConstBuffer<void> src, + Error &error) override; + +private: + ConstBuffer<float> Resample2(ConstBuffer<float> src, Error &error); +}; + +bool +pcm_resample_lsr_global_init(const char *converter, Error &error); + +#endif diff --git a/src/pcm/PcmBuffer.cxx b/src/pcm/PcmBuffer.cxx index 578c579be..5af9a978d 100644 --- a/src/pcm/PcmBuffer.cxx +++ b/src/pcm/PcmBuffer.cxx @@ -19,7 +19,6 @@ #include "config.h" #include "PcmBuffer.hxx" -#include "poison.h" void * PcmBuffer::Get(size_t new_size) diff --git a/src/pcm/PcmBuffer.hxx b/src/pcm/PcmBuffer.hxx index 717e24938..44a3ebf99 100644 --- a/src/pcm/PcmBuffer.hxx +++ b/src/pcm/PcmBuffer.hxx @@ -49,6 +49,12 @@ public: */ gcc_malloc void *Get(size_t size); + + template<typename T> + gcc_malloc + T *GetT(size_t n) { + return (T *)Get(n * sizeof(T)); + } }; #endif diff --git a/src/pcm/PcmChannels.cxx b/src/pcm/PcmChannels.cxx index eb69985c1..2e433e611 100644 --- a/src/pcm/PcmChannels.cxx +++ b/src/pcm/PcmChannels.cxx @@ -20,7 +20,9 @@ #include "config.h" #include "PcmChannels.hxx" #include "PcmBuffer.hxx" -#include "PcmUtils.hxx" +#include "Traits.hxx" +#include "AudioFormat.hxx" +#include "util/ConstBuffer.hxx" #include <assert.h> @@ -37,254 +39,143 @@ MonoToStereo(D dest, S src, S end) } -static void -pcm_convert_channels_16_2_to_1(int16_t *gcc_restrict dest, - const int16_t *gcc_restrict src, - const int16_t *gcc_restrict src_end) +template<SampleFormat F, class Traits=SampleTraits<F>> +static typename Traits::value_type +StereoToMono(typename Traits::value_type _a, + typename Traits::value_type _b) { - while (src < src_end) { - int32_t a = *src++, b = *src++; + typename Traits::sum_type a(_a); + typename Traits::sum_type b(_b); - *dest++ = (a + b) / 2; - } + return typename Traits::value_type((a + b) / 2); } -static void -pcm_convert_channels_16_n_to_2(int16_t *gcc_restrict dest, - unsigned src_channels, - const int16_t *gcc_restrict src, - const int16_t *gcc_restrict src_end) +template<SampleFormat F, class Traits=SampleTraits<F>> +static typename Traits::pointer_type +StereoToMono(typename Traits::pointer_type dest, + typename Traits::const_pointer_type src, + typename Traits::const_pointer_type end) { - unsigned c; - - assert(src_channels > 0); - - while (src < src_end) { - int32_t sum = 0; - int16_t value; - - for (c = 0; c < src_channels; ++c) - sum += *src++; - value = sum / (int)src_channels; + while (src != end) { + const auto a = *src++; + const auto b = *src++; - /* XXX this is actually only mono ... */ - *dest++ = value; - *dest++ = value; + *dest++ = StereoToMono<F, Traits>(a, b); } -} - -const int16_t * -pcm_convert_channels_16(PcmBuffer &buffer, - unsigned dest_channels, - unsigned src_channels, const int16_t *src, - size_t src_size, size_t *dest_size_r) -{ - assert(src_size % (sizeof(*src) * src_channels) == 0); - - size_t dest_size = src_size / src_channels * dest_channels; - *dest_size_r = dest_size; - - int16_t *dest = (int16_t *)buffer.Get(dest_size); - const int16_t *src_end = pcm_end_pointer(src, src_size); - - if (src_channels == 1 && dest_channels == 2) - MonoToStereo(dest, src, src_end); - else if (src_channels == 2 && dest_channels == 1) - pcm_convert_channels_16_2_to_1(dest, src, src_end); - else if (dest_channels == 2) - pcm_convert_channels_16_n_to_2(dest, src_channels, src, - src_end); - else - return nullptr; return dest; } -static void -pcm_convert_channels_24_2_to_1(int32_t *gcc_restrict dest, - const int32_t *gcc_restrict src, - const int32_t *gcc_restrict src_end) -{ - while (src < src_end) { - int32_t a = *src++, b = *src++; - - *dest++ = (a + b) / 2; - } -} - -static void -pcm_convert_channels_24_n_to_2(int32_t *gcc_restrict dest, - unsigned src_channels, - const int32_t *gcc_restrict src, - const int32_t *gcc_restrict src_end) +template<SampleFormat F, class Traits=SampleTraits<F>> +static typename Traits::pointer_type +NToStereo(typename Traits::pointer_type dest, + unsigned src_channels, + typename Traits::const_pointer_type src, + typename Traits::const_pointer_type end) { - unsigned c; + assert((end - src) % src_channels == 0); - assert(src_channels > 0); - - while (src < src_end) { - int32_t sum = 0; - int32_t value; - - for (c = 0; c < src_channels; ++c) + while (src != end) { + typename Traits::sum_type sum = *src++; + for (unsigned c = 1; c < src_channels; ++c) sum += *src++; - value = sum / (int)src_channels; - /* XXX this is actually only mono ... */ + typename Traits::value_type value(sum / int(src_channels)); + + /* TODO: this is actually only mono ... */ *dest++ = value; *dest++ = value; } -} - -const int32_t * -pcm_convert_channels_24(PcmBuffer &buffer, - unsigned dest_channels, - unsigned src_channels, const int32_t *src, - size_t src_size, size_t *dest_size_r) -{ - assert(src_size % (sizeof(*src) * src_channels) == 0); - - size_t dest_size = src_size / src_channels * dest_channels; - *dest_size_r = dest_size; - - int32_t *dest = (int32_t *)buffer.Get(dest_size); - const int32_t *src_end = (const int32_t *) - pcm_end_pointer(src, src_size); - - if (src_channels == 1 && dest_channels == 2) - MonoToStereo(dest, src, src_end); - else if (src_channels == 2 && dest_channels == 1) - pcm_convert_channels_24_2_to_1(dest, src, src_end); - else if (dest_channels == 2) - pcm_convert_channels_24_n_to_2(dest, src_channels, src, - src_end); - else - return nullptr; return dest; } -static void -pcm_convert_channels_32_2_to_1(int32_t *gcc_restrict dest, - const int32_t *gcc_restrict src, - const int32_t *gcc_restrict src_end) -{ - while (src < src_end) { - int64_t a = *src++, b = *src++; - - *dest++ = (a + b) / 2; - } -} - -static void -pcm_convert_channels_32_n_to_2(int32_t *dest, - unsigned src_channels, const int32_t *src, - const int32_t *src_end) +template<SampleFormat F, class Traits=SampleTraits<F>> +static typename Traits::pointer_type +NToM(typename Traits::pointer_type dest, + unsigned dest_channels, + unsigned src_channels, + typename Traits::const_pointer_type src, + typename Traits::const_pointer_type end) { - unsigned c; + assert((end - src) % src_channels == 0); - assert(src_channels > 0); - - while (src < src_end) { - int64_t sum = 0; - int32_t value; - - for (c = 0; c < src_channels; ++c) + while (src != end) { + typename Traits::sum_type sum = *src++; + for (unsigned c = 1; c < src_channels; ++c) sum += *src++; - value = sum / (int64_t)src_channels; - /* XXX this is actually only mono ... */ - *dest++ = value; - *dest++ = value; + typename Traits::value_type value(sum / int(src_channels)); + + /* TODO: this is actually only mono ... */ + for (unsigned c = 0; c < dest_channels; ++c) + *dest++ = value; } + + return dest; } -const int32_t * -pcm_convert_channels_32(PcmBuffer &buffer, - unsigned dest_channels, - unsigned src_channels, const int32_t *src, - size_t src_size, size_t *dest_size_r) +template<SampleFormat F, class Traits=SampleTraits<F>> +static ConstBuffer<typename Traits::value_type> +ConvertChannels(PcmBuffer &buffer, + unsigned dest_channels, + unsigned src_channels, + ConstBuffer<typename Traits::value_type> src) { - assert(src_size % (sizeof(*src) * src_channels) == 0); - - size_t dest_size = src_size / src_channels * dest_channels; - *dest_size_r = dest_size; + assert(src.size % src_channels == 0); - int32_t *dest = (int32_t *)buffer.Get(dest_size); - const int32_t *src_end = (const int32_t *) - pcm_end_pointer(src, src_size); + const size_t dest_size = src.size / src_channels * dest_channels; + auto dest = buffer.GetT<typename Traits::value_type>(dest_size); if (src_channels == 1 && dest_channels == 2) - MonoToStereo(dest, src, src_end); + MonoToStereo(dest, src.begin(), src.end()); else if (src_channels == 2 && dest_channels == 1) - pcm_convert_channels_32_2_to_1(dest, src, src_end); + StereoToMono<F>(dest, src.begin(), src.end()); else if (dest_channels == 2) - pcm_convert_channels_32_n_to_2(dest, src_channels, src, - src_end); + NToStereo<F>(dest, src_channels, src.begin(), src.end()); else - return nullptr; + NToM<F>(dest, dest_channels, + src_channels, src.begin(), src.end()); - return dest; + return { dest, dest_size }; } -static void -pcm_convert_channels_float_2_to_1(float *gcc_restrict dest, - const float *gcc_restrict src, - const float *gcc_restrict src_end) +ConstBuffer<int16_t> +pcm_convert_channels_16(PcmBuffer &buffer, + unsigned dest_channels, + unsigned src_channels, + ConstBuffer<int16_t> src) { - while (src < src_end) { - double a = *src++, b = *src++; - - *dest++ = (a + b) / 2; - } + return ConvertChannels<SampleFormat::S16>(buffer, dest_channels, + src_channels, src); } -static void -pcm_convert_channels_float_n_to_2(float *dest, - unsigned src_channels, const float *src, - const float *src_end) +ConstBuffer<int32_t> +pcm_convert_channels_24(PcmBuffer &buffer, + unsigned dest_channels, + unsigned src_channels, + ConstBuffer<int32_t> src) { - unsigned c; - - assert(src_channels > 0); - - while (src < src_end) { - double sum = 0; - float value; - - for (c = 0; c < src_channels; ++c) - sum += *src++; - value = sum / (double)src_channels; + return ConvertChannels<SampleFormat::S24_P32>(buffer, dest_channels, + src_channels, src); +} - /* XXX this is actually only mono ... */ - *dest++ = value; - *dest++ = value; - } +ConstBuffer<int32_t> +pcm_convert_channels_32(PcmBuffer &buffer, + unsigned dest_channels, + unsigned src_channels, + ConstBuffer<int32_t> src) +{ + return ConvertChannels<SampleFormat::S32>(buffer, dest_channels, + src_channels, src); } -const float * +ConstBuffer<float> pcm_convert_channels_float(PcmBuffer &buffer, unsigned dest_channels, - unsigned src_channels, const float *src, - size_t src_size, size_t *dest_size_r) + unsigned src_channels, + ConstBuffer<float> src) { - assert(src_size % (sizeof(*src) * src_channels) == 0); - - size_t dest_size = src_size / src_channels * dest_channels; - *dest_size_r = dest_size; - - float *dest = (float *)buffer.Get(dest_size); - const float *src_end = (const float *)pcm_end_pointer(src, src_size); - - if (src_channels == 1 && dest_channels == 2) - MonoToStereo(dest, src, src_end); - else if (src_channels == 2 && dest_channels == 1) - pcm_convert_channels_float_2_to_1(dest, src, src_end); - else if (dest_channels == 2) - pcm_convert_channels_float_n_to_2(dest, src_channels, src, - src_end); - else - return nullptr; - - return dest; + return ConvertChannels<SampleFormat::FLOAT>(buffer, dest_channels, + src_channels, src); } diff --git a/src/pcm/PcmChannels.hxx b/src/pcm/PcmChannels.hxx index c67822825..ca4e06a8c 100644 --- a/src/pcm/PcmChannels.hxx +++ b/src/pcm/PcmChannels.hxx @@ -24,6 +24,7 @@ #include <stddef.h> class PcmBuffer; +template<typename T> struct ConstBuffer; /** * Changes the number of channels in 16 bit PCM data. @@ -32,15 +33,13 @@ class PcmBuffer; * @param dest_channels the number of channels requested * @param src_channels the number of channels in the source buffer * @param src the source PCM buffer - * @param src_size the number of bytes in #src - * @param dest_size_r returns the number of bytes of the destination buffer * @return the destination buffer */ -const int16_t * +ConstBuffer<int16_t> pcm_convert_channels_16(PcmBuffer &buffer, unsigned dest_channels, - unsigned src_channels, const int16_t *src, - size_t src_size, size_t *dest_size_r); + unsigned src_channels, + ConstBuffer<int16_t> src); /** * Changes the number of channels in 24 bit PCM data (aligned at 32 @@ -50,15 +49,13 @@ pcm_convert_channels_16(PcmBuffer &buffer, * @param dest_channels the number of channels requested * @param src_channels the number of channels in the source buffer * @param src the source PCM buffer - * @param src_size the number of bytes in #src - * @param dest_size_r returns the number of bytes of the destination buffer * @return the destination buffer */ -const int32_t * +ConstBuffer<int32_t> pcm_convert_channels_24(PcmBuffer &buffer, unsigned dest_channels, - unsigned src_channels, const int32_t *src, - size_t src_size, size_t *dest_size_r); + unsigned src_channels, + ConstBuffer<int32_t> src); /** * Changes the number of channels in 32 bit PCM data. @@ -67,15 +64,13 @@ pcm_convert_channels_24(PcmBuffer &buffer, * @param dest_channels the number of channels requested * @param src_channels the number of channels in the source buffer * @param src the source PCM buffer - * @param src_size the number of bytes in #src - * @param dest_size_r returns the number of bytes of the destination buffer * @return the destination buffer */ -const int32_t * +ConstBuffer<int32_t> pcm_convert_channels_32(PcmBuffer &buffer, unsigned dest_channels, - unsigned src_channels, const int32_t *src, - size_t src_size, size_t *dest_size_r); + unsigned src_channels, + ConstBuffer<int32_t> src); /** * Changes the number of channels in 32 bit float PCM data. @@ -84,14 +79,12 @@ pcm_convert_channels_32(PcmBuffer &buffer, * @param dest_channels the number of channels requested * @param src_channels the number of channels in the source buffer * @param src the source PCM buffer - * @param src_size the number of bytes in #src - * @param dest_size_r returns the number of bytes of the destination buffer * @return the destination buffer */ -const float * +ConstBuffer<float> pcm_convert_channels_float(PcmBuffer &buffer, unsigned dest_channels, - unsigned src_channels, const float *src, - size_t src_size, size_t *dest_size_r); + unsigned src_channels, + ConstBuffer<float> src); #endif diff --git a/src/pcm/PcmConvert.cxx b/src/pcm/PcmConvert.cxx index 8eafe527c..6eecd3b01 100644 --- a/src/pcm/PcmConvert.cxx +++ b/src/pcm/PcmConvert.cxx @@ -19,289 +19,151 @@ #include "config.h" #include "PcmConvert.hxx" -#include "PcmChannels.hxx" -#include "PcmFormat.hxx" +#include "Domain.hxx" +#include "ConfiguredResampler.hxx" #include "AudioFormat.hxx" +#include "util/ConstBuffer.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" +#include "util/ConstBuffer.hxx" #include <assert.h> #include <math.h> -const Domain pcm_convert_domain("pcm_convert"); +bool +pcm_convert_global_init(Error &error) +{ + return pcm_resampler_global_init(error); +} PcmConvert::PcmConvert() { +#ifndef NDEBUG + src_format.Clear(); + dest_format.Clear(); +#endif } PcmConvert::~PcmConvert() { + assert(!src_format.IsValid()); + assert(!dest_format.IsValid()); } -void -PcmConvert::Reset() +bool +PcmConvert::Open(AudioFormat _src_format, AudioFormat _dest_format, + Error &error) { - dsd.Reset(); - resampler.Reset(); -} + assert(!src_format.IsValid()); + assert(!dest_format.IsValid()); + assert(_src_format.IsValid()); + assert(_dest_format.IsValid()); -inline const int16_t * -PcmConvert::Convert16(const AudioFormat src_format, - const void *src_buffer, size_t src_size, - const AudioFormat dest_format, size_t *dest_size_r, - Error &error) -{ - const int16_t *buf; - size_t len; + src_format = _src_format; + dest_format = _dest_format; + + AudioFormat format = src_format; + if (format.format == SampleFormat::DSD) + format.format = SampleFormat::FLOAT; - assert(dest_format.format == SampleFormat::S16); + enable_resampler = format.sample_rate != dest_format.sample_rate; + if (enable_resampler) { + if (!resampler.Open(format, dest_format.sample_rate, error)) + return false; - buf = pcm_convert_to_16(format_buffer, dither, - src_format.format, - src_buffer, src_size, - &len); - if (buf == nullptr) { - error.Format(pcm_convert_domain, - "Conversion from %s to 16 bit is not implemented", - sample_format_to_string(src_format.format)); - return nullptr; + format.format = resampler.GetOutputSampleFormat(); + format.sample_rate = dest_format.sample_rate; } - if (src_format.channels != dest_format.channels) { - buf = pcm_convert_channels_16(channels_buffer, - dest_format.channels, - src_format.channels, - buf, len, &len); - if (buf == nullptr) { - error.Format(pcm_convert_domain, - "Conversion from %u to %u channels " - "is not implemented", - src_format.channels, - dest_format.channels); - return nullptr; - } + enable_format = format.format != dest_format.format; + if (enable_format && + !format_converter.Open(format.format, dest_format.format, error)) { + if (enable_resampler) + resampler.Close(); + return false; } - if (src_format.sample_rate != dest_format.sample_rate) { - buf = resampler.Resample16(dest_format.channels, - src_format.sample_rate, buf, len, - dest_format.sample_rate, &len, - error); - if (buf == nullptr) - return nullptr; + format.format = dest_format.format; + + enable_channels = format.channels != dest_format.channels; + if (enable_channels && + !channels_converter.Open(format.format, format.channels, + dest_format.channels, error)) { + if (enable_format) + format_converter.Close(); + if (enable_resampler) + resampler.Close(); + return false; } - *dest_size_r = len; - return buf; + return true; } -inline const int32_t * -PcmConvert::Convert24(const AudioFormat src_format, - const void *src_buffer, size_t src_size, - const AudioFormat dest_format, size_t *dest_size_r, - Error &error) +void +PcmConvert::Close() { - const int32_t *buf; - size_t len; - - assert(dest_format.format == SampleFormat::S24_P32); - - buf = pcm_convert_to_24(format_buffer, - src_format.format, - src_buffer, src_size, &len); - if (buf == nullptr) { - error.Format(pcm_convert_domain, - "Conversion from %s to 24 bit is not implemented", - sample_format_to_string(src_format.format)); - return nullptr; - } + if (enable_channels) + channels_converter.Close(); + if (enable_format) + format_converter.Close(); + if (enable_resampler) + resampler.Close(); - if (src_format.channels != dest_format.channels) { - buf = pcm_convert_channels_24(channels_buffer, - dest_format.channels, - src_format.channels, - buf, len, &len); - if (buf == nullptr) { - error.Format(pcm_convert_domain, - "Conversion from %u to %u channels " - "is not implemented", - src_format.channels, - dest_format.channels); - return nullptr; - } - } - - if (src_format.sample_rate != dest_format.sample_rate) { - buf = resampler.Resample24(dest_format.channels, - src_format.sample_rate, buf, len, - dest_format.sample_rate, &len, - error); - if (buf == nullptr) - return nullptr; - } + dsd.Reset(); - *dest_size_r = len; - return buf; +#ifndef NDEBUG + src_format.Clear(); + dest_format.Clear(); +#endif } -inline const int32_t * -PcmConvert::Convert32(const AudioFormat src_format, - const void *src_buffer, size_t src_size, - const AudioFormat dest_format, size_t *dest_size_r, - Error &error) +const void * +PcmConvert::Convert(const void *src, size_t src_size, + size_t *dest_size_r, + Error &error) { - const int32_t *buf; - size_t len; - - assert(dest_format.format == SampleFormat::S32); - - buf = pcm_convert_to_32(format_buffer, - src_format.format, - src_buffer, src_size, &len); - if (buf == nullptr) { - error.Format(pcm_convert_domain, - "Conversion from %s to 32 bit is not implemented", - sample_format_to_string(src_format.format)); - return nullptr; - } - - if (src_format.channels != dest_format.channels) { - buf = pcm_convert_channels_32(channels_buffer, - dest_format.channels, - src_format.channels, - buf, len, &len); - if (buf == nullptr) { - error.Format(pcm_convert_domain, - "Conversion from %u to %u channels " - "is not implemented", - src_format.channels, - dest_format.channels); + ConstBuffer<void> buffer(src, src_size); + AudioFormat format = src_format; + + if (format.format == SampleFormat::DSD) { + auto s = ConstBuffer<uint8_t>::FromVoid(buffer); + auto d = dsd.ToFloat(format.channels, + false, s); + if (d.IsNull()) { + error.Set(pcm_domain, + "DSD to PCM conversion failed"); return nullptr; } - } - if (src_format.sample_rate != dest_format.sample_rate) { - buf = resampler.Resample32(dest_format.channels, - src_format.sample_rate, buf, len, - dest_format.sample_rate, &len, - error); - if (buf == nullptr) - return buf; + buffer = d.ToVoid(); + format.format = SampleFormat::FLOAT; } - *dest_size_r = len; - return buf; -} - -inline const float * -PcmConvert::ConvertFloat(const AudioFormat src_format, - const void *src_buffer, size_t src_size, - const AudioFormat dest_format, size_t *dest_size_r, - Error &error) -{ - const float *buffer = (const float *)src_buffer; - size_t size = src_size; - - assert(dest_format.format == SampleFormat::FLOAT); - - /* convert to float now */ + if (enable_resampler) { + buffer = resampler.Resample(buffer, error); + if (buffer.IsNull()) + return nullptr; - buffer = pcm_convert_to_float(format_buffer, - src_format.format, - buffer, size, &size); - if (buffer == nullptr) { - error.Format(pcm_convert_domain, - "Conversion from %s to float is not implemented", - sample_format_to_string(src_format.format)); - return nullptr; + format.format = resampler.GetOutputSampleFormat(); + format.sample_rate = dest_format.sample_rate; } - /* convert channels */ - - if (src_format.channels != dest_format.channels) { - buffer = pcm_convert_channels_float(channels_buffer, - dest_format.channels, - src_format.channels, - buffer, size, &size); - if (buffer == nullptr) { - error.Format(pcm_convert_domain, - "Conversion from %u to %u channels " - "is not implemented", - src_format.channels, - dest_format.channels); + if (enable_format) { + buffer = format_converter.Convert(buffer, error); + if (buffer.IsNull()) return nullptr; - } - } - /* resample with float, because this is the best format for - libsamplerate */ - - if (src_format.sample_rate != dest_format.sample_rate) { - buffer = resampler.ResampleFloat(dest_format.channels, - src_format.sample_rate, - buffer, size, - dest_format.sample_rate, - &size, error); - if (buffer == nullptr) - return nullptr; + format.format = dest_format.format; } - *dest_size_r = size; - return buffer; -} - -const void * -PcmConvert::Convert(AudioFormat src_format, - const void *src, size_t src_size, - const AudioFormat dest_format, - size_t *dest_size_r, - Error &error) -{ - AudioFormat float_format; - if (src_format.format == SampleFormat::DSD) { - size_t f_size; - const float *f = dsd.ToFloat(src_format.channels, - false, (const uint8_t *)src, - src_size, &f_size); - if (f == nullptr) { - error.Set(pcm_convert_domain, - "DSD to PCM conversion failed"); + if (enable_channels) { + buffer = channels_converter.Convert(buffer, error); + if (buffer.IsNull()) return nullptr; - } - float_format = src_format; - float_format.format = SampleFormat::FLOAT; - - src_format = float_format; - src = f; - src_size = f_size; + format.channels = dest_format.channels; } - switch (dest_format.format) { - case SampleFormat::S16: - return Convert16(src_format, src, src_size, - dest_format, dest_size_r, - error); - - case SampleFormat::S24_P32: - return Convert24(src_format, src, src_size, - dest_format, dest_size_r, - error); - - case SampleFormat::S32: - return Convert32(src_format, src, src_size, - dest_format, dest_size_r, - error); - - case SampleFormat::FLOAT: - return ConvertFloat(src_format, src, src_size, - dest_format, dest_size_r, - error); - - default: - error.Format(pcm_convert_domain, - "PCM conversion to %s is not implemented", - sample_format_to_string(dest_format.format)); - return nullptr; - } + *dest_size_r = buffer.size; + return buffer.data; } diff --git a/src/pcm/PcmConvert.hxx b/src/pcm/PcmConvert.hxx index 40f785179..586c303f2 100644 --- a/src/pcm/PcmConvert.hxx +++ b/src/pcm/PcmConvert.hxx @@ -20,15 +20,18 @@ #ifndef PCM_CONVERT_HXX #define PCM_CONVERT_HXX -#include "PcmDither.hxx" #include "PcmDsd.hxx" -#include "PcmResample.hxx" #include "PcmBuffer.hxx" +#include "FormatConverter.hxx" +#include "ChannelsConverter.hxx" +#include "GlueResampler.hxx" +#include "AudioFormat.hxx" #include <stddef.h> -struct AudioFormat; +template<typename T> struct ConstBuffer; class Error; +class Domain; /** * This object is statically allocated (within another struct), and @@ -38,27 +41,29 @@ class Error; class PcmConvert { PcmDsd dsd; - PcmResampler resampler; + GluePcmResampler resampler; + PcmFormatConverter format_converter; + PcmChannelsConverter channels_converter; - PcmDither dither; + AudioFormat src_format, dest_format; - /** the buffer for converting the sample format */ - PcmBuffer format_buffer; - - /** the buffer for converting the channel count */ - PcmBuffer channels_buffer; + bool enable_resampler, enable_format, enable_channels; public: PcmConvert(); ~PcmConvert(); + /** + * Prepare the object. Call Close() when done. + */ + bool Open(AudioFormat _src_format, AudioFormat _dest_format, + Error &error); /** - * Reset the pcm_convert_state object. Use this at the - * boundary between two distinct songs and each time the - * format changes. + * Close the object after it was prepared with Open(). After + * that, it may be reused by calling Open() again. */ - void Reset(); + void Close(); /** * Converts PCM data between two audio formats. @@ -72,38 +77,12 @@ public: * ignore errors * @return the destination buffer, or NULL on error */ - const void *Convert(AudioFormat src_format, - const void *src, size_t src_size, - AudioFormat dest_format, + const void *Convert(const void *src, size_t src_size, size_t *dest_size_r, Error &error); - -private: - const int16_t *Convert16(AudioFormat src_format, - const void *src_buffer, size_t src_size, - AudioFormat dest_format, - size_t *dest_size_r, - Error &error); - - const int32_t *Convert24(AudioFormat src_format, - const void *src_buffer, size_t src_size, - AudioFormat dest_format, - size_t *dest_size_r, - Error &error); - - const int32_t *Convert32(AudioFormat src_format, - const void *src_buffer, size_t src_size, - AudioFormat dest_format, - size_t *dest_size_r, - Error &error); - - const float *ConvertFloat(AudioFormat src_format, - const void *src_buffer, size_t src_size, - AudioFormat dest_format, - size_t *dest_size_r, - Error &error); }; -extern const class Domain pcm_convert_domain; +bool +pcm_convert_global_init(Error &error); #endif diff --git a/src/pcm/PcmDither.cxx b/src/pcm/PcmDither.cxx index 98d0d443e..d9d4d28ca 100644 --- a/src/pcm/PcmDither.cxx +++ b/src/pcm/PcmDither.cxx @@ -20,18 +20,14 @@ #include "config.h" #include "PcmDither.hxx" #include "PcmPrng.hxx" +#include "Traits.hxx" -inline int16_t -PcmDither::Dither24To16(int_fast32_t sample) +template<typename T, T MIN, T MAX, unsigned scale_bits> +inline T +PcmDither::Dither(T sample) { - constexpr unsigned from_bits = 24; - constexpr unsigned to_bits = 16; - constexpr unsigned scale_bits = from_bits - to_bits; - constexpr int_fast32_t round = 1 << (scale_bits - 1); - constexpr int_fast32_t mask = (1 << scale_bits) - 1; - constexpr int_fast32_t ONE = 1 << (from_bits - 1); - constexpr int_fast32_t MIN = -ONE; - constexpr int_fast32_t MAX = ONE - 1; + constexpr T round = 1 << (scale_bits - 1); + constexpr T mask = (1 << scale_bits) - 1; sample += error[0] - error[1] + error[2]; @@ -39,9 +35,9 @@ PcmDither::Dither24To16(int_fast32_t sample) error[1] = error[0] / 2; /* round */ - int_fast32_t output = sample + round; + T output = sample + round; - int_fast32_t rnd = pcm_prng(random); + const T rnd = pcm_prng(random); output += (rnd & mask) - (random & mask); random = rnd; @@ -63,27 +59,59 @@ PcmDither::Dither24To16(int_fast32_t sample) error[0] = sample - output; - return (int16_t)(output >> scale_bits); + return output >> scale_bits; } -void -PcmDither::Dither24To16(int16_t *dest, const int32_t *src, - const int32_t *src_end) +template<typename ST, unsigned SBITS, unsigned DBITS> +inline ST +PcmDither::DitherShift(ST sample) +{ + static_assert(sizeof(ST) * 8 > SBITS, "Source type too small"); + static_assert(SBITS > DBITS, "Non-positive scale_bits"); + + static constexpr ST MIN = -(ST(1) << (SBITS - 1)); + static constexpr ST MAX = (ST(1) << (SBITS - 1)) - 1; + + return Dither<ST, MIN, MAX, SBITS - DBITS>(sample); +} + +template<typename ST, typename DT> +inline typename DT::value_type +PcmDither::DitherConvert(typename ST::value_type sample) +{ + static_assert(ST::BITS > DT::BITS, + "Sample formats cannot be dithered"); + + constexpr unsigned scale_bits = ST::BITS - DT::BITS; + + return Dither<typename ST::sum_type, ST::MIN, ST::MAX, + scale_bits>(sample); +} + +template<typename ST, typename DT> +inline void +PcmDither::DitherConvert(typename DT::pointer_type dest, + typename ST::const_pointer_type src, + typename ST::const_pointer_type src_end) { while (src < src_end) - *dest++ = Dither24To16(*src++); + *dest++ = DitherConvert<ST, DT>(*src++); } -inline int16_t -PcmDither::Dither32To16(int_fast32_t sample) +inline void +PcmDither::Dither24To16(int16_t *dest, const int32_t *src, + const int32_t *src_end) { - return Dither24To16(sample >> 8); + typedef SampleTraits<SampleFormat::S24_P32> ST; + typedef SampleTraits<SampleFormat::S16> DT; + DitherConvert<ST, DT>(dest, src, src_end); } -void +inline void PcmDither::Dither32To16(int16_t *dest, const int32_t *src, const int32_t *src_end) { - while (src < src_end) - *dest++ = Dither32To16(*src++); + typedef SampleTraits<SampleFormat::S32> ST; + typedef SampleTraits<SampleFormat::S16> DT; + DitherConvert<ST, DT>(dest, src, src_end); } diff --git a/src/pcm/PcmDither.hxx b/src/pcm/PcmDither.hxx index 106382307..473a45ac3 100644 --- a/src/pcm/PcmDither.hxx +++ b/src/pcm/PcmDither.hxx @@ -22,6 +22,8 @@ #include <stdint.h> +enum class SampleFormat : uint8_t; + class PcmDither { int32_t error[3]; int32_t random; @@ -30,6 +32,18 @@ public: constexpr PcmDither() :error{0, 0, 0}, random(0) {} + /** + * Shift the given sample by #SBITS-#DBITS to the right, and + * apply dithering. + * + * @param ST the input sample type + * @param SBITS the input bit width + * @param DBITS the output bit width + * @param sample the input sample value + */ + template<typename ST, unsigned SBITS, unsigned DBITS> + ST DitherShift(ST sample); + void Dither24To16(int16_t *dest, const int32_t *src, const int32_t *src_end); @@ -37,8 +51,34 @@ public: const int32_t *src_end); private: - int16_t Dither24To16(int_fast32_t sample); - int16_t Dither32To16(int_fast32_t sample); + /** + * Shift the given sample by #scale_bits to the right, and + * apply dithering. + * + * @param T the input sample type + * @param MIN the minimum input sample value + * @param MAX the maximum input sample value + * @param scale_bits the number of bits to be discarded + * @param sample the input sample value + */ + template<typename T, T MIN, T MAX, unsigned scale_bits> + T Dither(T sample); + + /** + * Convert the given sample from one sample format to another, + * discarding bits. + * + * @param ST the input #SampleTraits class + * @param ST the output #SampleTraits class + * @param sample the input sample value + */ + template<typename ST, typename DT> + typename DT::value_type DitherConvert(typename ST::value_type sample); + + template<typename ST, typename DT> + void DitherConvert(typename DT::pointer_type dest, + typename ST::const_pointer_type src, + typename ST::const_pointer_type src_end); }; #endif diff --git a/src/pcm/PcmDsd.cxx b/src/pcm/PcmDsd.cxx index 4db274635..5952cad7c 100644 --- a/src/pcm/PcmDsd.cxx +++ b/src/pcm/PcmDsd.cxx @@ -21,11 +21,11 @@ #include "PcmDsd.hxx" #include "dsd2pcm/dsd2pcm.h" #include "util/Macros.hxx" +#include "util/ConstBuffer.hxx" #include <algorithm> #include <assert.h> -#include <string.h> PcmDsd::PcmDsd() { @@ -47,22 +47,20 @@ PcmDsd::Reset() dsd2pcm_reset(dsd2pcm[i]); } -const float * +ConstBuffer<float> PcmDsd::ToFloat(unsigned channels, bool lsbfirst, - const uint8_t *src, size_t src_size, - size_t *dest_size_r) + ConstBuffer<uint8_t> src) { - assert(src != nullptr); - assert(src_size > 0); - assert(src_size % channels == 0); + assert(!src.IsNull()); + assert(!src.IsEmpty()); + assert(src.size % channels == 0); assert(channels <= ARRAY_SIZE(dsd2pcm)); - const unsigned num_samples = src_size; - const unsigned num_frames = src_size / channels; + const unsigned num_samples = src.size; + const unsigned num_frames = src.size / channels; float *dest; const size_t dest_size = num_samples * sizeof(*dest); - *dest_size_r = dest_size; dest = (float *)buffer.Get(dest_size); for (unsigned c = 0; c < channels; ++c) { @@ -73,9 +71,9 @@ PcmDsd::ToFloat(unsigned channels, bool lsbfirst, } dsd2pcm_translate(dsd2pcm[c], num_frames, - src + c, channels, + src.data + c, channels, lsbfirst, dest + c, channels); } - return dest; + return { dest, num_samples }; } diff --git a/src/pcm/PcmDsd.hxx b/src/pcm/PcmDsd.hxx index 26ee11b13..b9b6d51ee 100644 --- a/src/pcm/PcmDsd.hxx +++ b/src/pcm/PcmDsd.hxx @@ -25,22 +25,24 @@ #include <stdint.h> +template<typename T> struct ConstBuffer; + /** * Wrapper for the dsd2pcm library. */ -struct PcmDsd { +class PcmDsd { PcmBuffer buffer; struct dsd2pcm_ctx_s *dsd2pcm[32]; +public: PcmDsd(); ~PcmDsd(); void Reset(); - const float *ToFloat(unsigned channels, bool lsbfirst, - const uint8_t *src, size_t src_size, - size_t *dest_size_r); + ConstBuffer<float> ToFloat(unsigned channels, bool lsbfirst, + ConstBuffer<uint8_t> src); }; #endif diff --git a/src/pcm/PcmDsdUsb.cxx b/src/pcm/PcmDsdUsb.cxx index 2d0f33a15..833d84653 100644 --- a/src/pcm/PcmDsdUsb.cxx +++ b/src/pcm/PcmDsdUsb.cxx @@ -22,6 +22,8 @@ #include "PcmBuffer.hxx" #include "AudioFormat.hxx" +#include <assert.h> + constexpr static inline uint32_t pcm_two_dsd_to_usb_marker1(uint8_t a, uint8_t b) diff --git a/src/pcm/PcmFormat.cxx b/src/pcm/PcmFormat.cxx index 4565c71c6..92d2cd77b 100644 --- a/src/pcm/PcmFormat.cxx +++ b/src/pcm/PcmFormat.cxx @@ -19,54 +19,11 @@ #include "config.h" #include "PcmFormat.hxx" -#include "PcmDither.hxx" #include "PcmBuffer.hxx" #include "PcmUtils.hxx" +#include "Traits.hxx" -#include <type_traits> - -template<SampleFormat F> -struct SampleTraits {}; - -template<> -struct SampleTraits<SampleFormat::S8> { - typedef int8_t value_type; - typedef value_type *pointer_type; - typedef const value_type *const_pointer_type; - - static constexpr size_t SAMPLE_SIZE = sizeof(value_type); - static constexpr unsigned BITS = sizeof(value_type) * 8; -}; - -template<> -struct SampleTraits<SampleFormat::S16> { - typedef int16_t value_type; - typedef value_type *pointer_type; - typedef const value_type *const_pointer_type; - - static constexpr size_t SAMPLE_SIZE = sizeof(value_type); - static constexpr unsigned BITS = sizeof(value_type) * 8; -}; - -template<> -struct SampleTraits<SampleFormat::S32> { - typedef int32_t value_type; - typedef value_type *pointer_type; - typedef const value_type *const_pointer_type; - - static constexpr size_t SAMPLE_SIZE = sizeof(value_type); - static constexpr unsigned BITS = sizeof(value_type) * 8; -}; - -template<> -struct SampleTraits<SampleFormat::S24_P32> { - typedef int32_t value_type; - typedef value_type *pointer_type; - typedef const value_type *const_pointer_type; - - static constexpr size_t SAMPLE_SIZE = sizeof(value_type); - static constexpr unsigned BITS = 24; -}; +#include "PcmDither.cxx" // including the .cxx file to get inlined templates static void pcm_convert_8_to_16(int16_t *out, const int8_t *in, const int8_t *in_end) @@ -100,8 +57,8 @@ ConvertFromFloat(typename Traits::pointer_type dest, const float factor = 1 << (bits - 1); while (src != end) { - int sample(*src++ * factor); - *dest++ = PcmClamp<typename Traits::value_type, int, bits>(sample); + typename Traits::long_type sample(*src++ * factor); + *dest++ = PcmClamp<F, Traits>(sample); } } @@ -442,7 +399,6 @@ ConvertToFloat(float *dest, constexpr float factor = 0.5 / (1 << (Traits::BITS - 2)); while (src != end) *dest++ = float(*src++) * factor; - } template<SampleFormat F, class Traits=SampleTraits<F>> diff --git a/src/pcm/PcmMix.cxx b/src/pcm/PcmMix.cxx index 001794061..c1ffd8633 100644 --- a/src/pcm/PcmMix.cxx +++ b/src/pcm/PcmMix.cxx @@ -19,42 +19,56 @@ #include "config.h" #include "PcmMix.hxx" -#include "PcmVolume.hxx" +#include "Volume.hxx" #include "PcmUtils.hxx" #include "AudioFormat.hxx" +#include "Traits.hxx" +#include "util/Clamp.hxx" +#include "PcmDither.cxx" // including the .cxx file to get inlined templates + +#include <assert.h> #include <math.h> -template<typename T, typename U, unsigned bits> -static T -PcmAddVolume(T _a, T _b, int volume1, int volume2) +template<SampleFormat F, class Traits=SampleTraits<F>> +static typename Traits::value_type +PcmAddVolume(PcmDither &dither, + typename Traits::value_type _a, typename Traits::value_type _b, + int volume1, int volume2) { - U a(_a), b(_b); - - U c = ((a * volume1 + b * volume2) + - pcm_volume_dither() + PCM_VOLUME_1 / 2) - / PCM_VOLUME_1; + typename Traits::long_type a(_a), b(_b); + typename Traits::long_type c(a * volume1 + b * volume2); - return PcmClamp<T, U, bits>(c); + return dither.DitherShift<typename Traits::long_type, + Traits::BITS + PCM_VOLUME_BITS, + Traits::BITS>(c); } -template<typename T, typename U, unsigned bits> +template<SampleFormat F, class Traits=SampleTraits<F>> static void -PcmAddVolume(T *a, const T *b, unsigned n, int volume1, int volume2) +PcmAddVolume(PcmDither &dither, + typename Traits::pointer_type a, + typename Traits::const_pointer_type b, + size_t n, int volume1, int volume2) { for (size_t i = 0; i != n; ++i) - a[i] = PcmAddVolume<T, U, bits>(a[i], b[i], volume1, volume2); + a[i] = PcmAddVolume<F, Traits>(dither, a[i], b[i], + volume1, volume2); } -template<typename T, typename U, unsigned bits> +template<SampleFormat F, class Traits=SampleTraits<F>> static void -PcmAddVolumeVoid(void *a, const void *b, size_t size, int volume1, int volume2) +PcmAddVolumeVoid(PcmDither &dither, + void *a, const void *b, size_t size, int volume1, int volume2) { - constexpr size_t sample_size = sizeof(T); + constexpr size_t sample_size = Traits::SAMPLE_SIZE; assert(size % sample_size == 0); - PcmAddVolume<T, U, bits>((T *)a, (const T *)b, size / sample_size, - volume1, volume2); + PcmAddVolume<F, Traits>(dither, + typename Traits::pointer_type(a), + typename Traits::const_pointer_type(b), + size / sample_size, + volume1, volume2); } static void @@ -72,7 +86,7 @@ pcm_add_vol_float(float *buffer1, const float *buffer2, } static bool -pcm_add_vol(void *buffer1, const void *buffer2, size_t size, +pcm_add_vol(PcmDither &dither, void *buffer1, const void *buffer2, size_t size, int vol1, int vol2, SampleFormat format) { @@ -83,23 +97,27 @@ pcm_add_vol(void *buffer1, const void *buffer2, size_t size, return false; case SampleFormat::S8: - PcmAddVolumeVoid<int8_t, int32_t, 8>(buffer1, buffer2, size, - vol1, vol2); + PcmAddVolumeVoid<SampleFormat::S8>(dither, + buffer1, buffer2, size, + vol1, vol2); return true; case SampleFormat::S16: - PcmAddVolumeVoid<int16_t, int32_t, 16>(buffer1, buffer2, size, - vol1, vol2); + PcmAddVolumeVoid<SampleFormat::S16>(dither, + buffer1, buffer2, size, + vol1, vol2); return true; case SampleFormat::S24_P32: - PcmAddVolumeVoid<int32_t, int64_t, 24>(buffer1, buffer2, size, - vol1, vol2); + PcmAddVolumeVoid<SampleFormat::S24_P32>(dither, + buffer1, buffer2, size, + vol1, vol2); return true; case SampleFormat::S32: - PcmAddVolumeVoid<int32_t, int64_t, 32>(buffer1, buffer2, size, - vol1, vol2); + PcmAddVolumeVoid<SampleFormat::S32>(dither, + buffer1, buffer2, size, + vol1, vol2); return true; case SampleFormat::FLOAT: @@ -114,30 +132,35 @@ pcm_add_vol(void *buffer1, const void *buffer2, size_t size, gcc_unreachable(); } -template<typename T, typename U, unsigned bits> -static T -PcmAdd(T _a, T _b) +template<SampleFormat F, class Traits=SampleTraits<F>> +static typename Traits::value_type +PcmAdd(typename Traits::value_type _a, typename Traits::value_type _b) { - U a(_a), b(_b); - return PcmClamp<T, U, bits>(a + b); + typename Traits::sum_type a(_a), b(_b); + + return PcmClamp<F, Traits>(a + b); } -template<typename T, typename U, unsigned bits> +template<SampleFormat F, class Traits=SampleTraits<F>> static void -PcmAdd(T *a, const T *b, unsigned n) +PcmAdd(typename Traits::pointer_type a, + typename Traits::const_pointer_type b, + size_t n) { for (size_t i = 0; i != n; ++i) - a[i] = PcmAdd<T, U, bits>(a[i], b[i]); + a[i] = PcmAdd<F, Traits>(a[i], b[i]); } -template<typename T, typename U, unsigned bits> +template<SampleFormat F, class Traits=SampleTraits<F>> static void PcmAddVoid(void *a, const void *b, size_t size) { - constexpr size_t sample_size = sizeof(T); + constexpr size_t sample_size = Traits::SAMPLE_SIZE; assert(size % sample_size == 0); - PcmAdd<T, U, bits>((T *)a, (const T *)b, size / sample_size); + PcmAdd<F, Traits>(typename Traits::pointer_type(a), + typename Traits::const_pointer_type(b), + size / sample_size); } static void @@ -162,19 +185,19 @@ pcm_add(void *buffer1, const void *buffer2, size_t size, return false; case SampleFormat::S8: - PcmAddVoid<int8_t, int32_t, 8>(buffer1, buffer2, size); + PcmAddVoid<SampleFormat::S8>(buffer1, buffer2, size); return true; case SampleFormat::S16: - PcmAddVoid<int16_t, int32_t, 16>(buffer1, buffer2, size); + PcmAddVoid<SampleFormat::S16>(buffer1, buffer2, size); return true; case SampleFormat::S24_P32: - PcmAddVoid<int32_t, int64_t, 24>(buffer1, buffer2, size); + PcmAddVoid<SampleFormat::S24_P32>(buffer1, buffer2, size); return true; case SampleFormat::S32: - PcmAddVoid<int32_t, int64_t, 32>(buffer1, buffer2, size); + PcmAddVoid<SampleFormat::S32>(buffer1, buffer2, size); return true; case SampleFormat::FLOAT: @@ -188,10 +211,9 @@ pcm_add(void *buffer1, const void *buffer2, size_t size, } bool -pcm_mix(void *buffer1, const void *buffer2, size_t size, +pcm_mix(PcmDither &dither, void *buffer1, const void *buffer2, size_t size, SampleFormat format, float portion1) { - int vol1; float s; /* portion1 is between 0.0 and 1.0 for crossfading, MixRamp uses -1 @@ -202,8 +224,9 @@ pcm_mix(void *buffer1, const void *buffer2, size_t size, s = sin(M_PI_2 * portion1); s *= s; - vol1 = s * PCM_VOLUME_1 + 0.5; - vol1 = vol1 > PCM_VOLUME_1 ? PCM_VOLUME_1 : (vol1 < 0 ? 0 : vol1); + int vol1 = s * PCM_VOLUME_1S + 0.5; + vol1 = Clamp<int>(vol1, 0, PCM_VOLUME_1S); - return pcm_add_vol(buffer1, buffer2, size, vol1, PCM_VOLUME_1 - vol1, format); + return pcm_add_vol(dither, buffer1, buffer2, size, + vol1, PCM_VOLUME_1S - vol1, format); } diff --git a/src/pcm/PcmMix.hxx b/src/pcm/PcmMix.hxx index 637c88f8a..a6657a979 100644 --- a/src/pcm/PcmMix.hxx +++ b/src/pcm/PcmMix.hxx @@ -25,6 +25,8 @@ #include <stddef.h> +class PcmDither; + /* * Linearly mixes two PCM buffers. Both must have the same length and * the same audio format. The formula is: @@ -44,7 +46,7 @@ */ gcc_warn_unused_result bool -pcm_mix(void *buffer1, const void *buffer2, size_t size, +pcm_mix(PcmDither &dither, void *buffer1, const void *buffer2, size_t size, SampleFormat format, float portion1); #endif diff --git a/src/pcm/PcmPrng.hxx b/src/pcm/PcmPrng.hxx index 0c823250d..73b1456a8 100644 --- a/src/pcm/PcmPrng.hxx +++ b/src/pcm/PcmPrng.hxx @@ -24,7 +24,7 @@ * A very simple linear congruential PRNG. It's good enough for PCM * dithering. */ -static unsigned long +constexpr static inline unsigned long pcm_prng(unsigned long state) { return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL; diff --git a/src/pcm/PcmResample.cxx b/src/pcm/PcmResample.cxx deleted file mode 100644 index 01f269ea9..000000000 --- a/src/pcm/PcmResample.cxx +++ /dev/null @@ -1,173 +0,0 @@ -/* - * 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 "PcmResampleInternal.hxx" - -#ifdef HAVE_LIBSAMPLERATE -#include "ConfigGlobal.hxx" -#include "ConfigOption.hxx" -#endif - -#include <string.h> - -#ifdef HAVE_LIBSAMPLERATE -static bool lsr_enabled; -#endif - -#ifdef HAVE_LIBSAMPLERATE -static bool -pcm_resample_lsr_enabled(void) -{ - return lsr_enabled; -} -#endif - -bool -pcm_resample_global_init(Error &error) -{ -#ifdef HAVE_LIBSAMPLERATE - const char *converter = - config_get_string(CONF_SAMPLERATE_CONVERTER, ""); - - lsr_enabled = strcmp(converter, "internal") != 0; - if (lsr_enabled) - return pcm_resample_lsr_global_init(converter, error); - else - return true; -#else - (void)error; - return true; -#endif -} - -PcmResampler::PcmResampler() -{ -#ifdef HAVE_LIBSAMPLERATE - if (pcm_resample_lsr_enabled()) - pcm_resample_lsr_init(this); -#endif -} - -PcmResampler::~PcmResampler() -{ -#ifdef HAVE_LIBSAMPLERATE - if (pcm_resample_lsr_enabled()) - pcm_resample_lsr_deinit(this); -#endif -} - -void -PcmResampler::Reset() -{ -#ifdef HAVE_LIBSAMPLERATE - pcm_resample_lsr_reset(this); -#endif -} - -const float * -PcmResampler::ResampleFloat(unsigned channels, unsigned src_rate, - const float *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error_r) -{ -#ifdef HAVE_LIBSAMPLERATE - if (pcm_resample_lsr_enabled()) - return pcm_resample_lsr_float(this, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r, - error_r); -#else - (void)error_r; -#endif - - /* sizeof(float)==sizeof(int32_t); the fallback resampler does - not do any math on the sample values, so this hack is - possible: */ - return (const float *) - pcm_resample_fallback_32(this, channels, - src_rate, (const int32_t *)src_buffer, - src_size, - dest_rate, dest_size_r); -} - -const int16_t * -PcmResampler::Resample16(unsigned channels, - unsigned src_rate, const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error_r) -{ -#ifdef HAVE_LIBSAMPLERATE - if (pcm_resample_lsr_enabled()) - return pcm_resample_lsr_16(this, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r, - error_r); -#else - (void)error_r; -#endif - - return pcm_resample_fallback_16(this, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r); -} - -const int32_t * -PcmResampler::Resample32(unsigned channels, unsigned src_rate, - const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error_r) -{ -#ifdef HAVE_LIBSAMPLERATE - if (pcm_resample_lsr_enabled()) - return pcm_resample_lsr_32(this, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r, - error_r); -#else - (void)error_r; -#endif - - return pcm_resample_fallback_32(this, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r); -} - -const int32_t * -PcmResampler::Resample24(unsigned channels, unsigned src_rate, - const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error_r) -{ -#ifdef HAVE_LIBSAMPLERATE - if (pcm_resample_lsr_enabled()) - return pcm_resample_lsr_24(this, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r, - error_r); -#else - (void)error_r; -#endif - - /* reuse the 32 bit code - the resampler code doesn't care if - the upper 8 bits are actually used */ - return pcm_resample_fallback_32(this, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r); -} diff --git a/src/pcm/PcmResample.hxx b/src/pcm/PcmResample.hxx deleted file mode 100644 index e839d6ecd..000000000 --- a/src/pcm/PcmResample.hxx +++ /dev/null @@ -1,133 +0,0 @@ -/* - * 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_PCM_RESAMPLE_HXX -#define MPD_PCM_RESAMPLE_HXX - -#include "check.h" -#include "PcmBuffer.hxx" - -#include <stdint.h> -#include <stddef.h> - -#ifdef HAVE_LIBSAMPLERATE -#include <samplerate.h> -#endif - -class Error; - -/** - * This object is statically allocated (within another struct), and - * holds buffer allocations and the state for the resampler. - */ -struct PcmResampler { -#ifdef HAVE_LIBSAMPLERATE - SRC_STATE *state; - SRC_DATA data; - - PcmBuffer in, out; - - struct { - unsigned src_rate; - unsigned dest_rate; - unsigned channels; - } prev; - - int error; -#endif - - PcmBuffer buffer; - - PcmResampler(); - ~PcmResampler(); - - /** - * @see pcm_convert_reset() - */ - void Reset(); - - /** - * Resamples 32 bit float data. - * - * @param channels the number of channels - * @param src_rate the source sample rate - * @param src the source PCM buffer - * @param src_size the size of #src in bytes - * @param dest_rate the requested destination sample rate - * @param dest_size_r returns the number of bytes of the destination buffer - * @return the destination buffer - */ - const float *ResampleFloat(unsigned channels, unsigned src_rate, - const float *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error_r); - - /** - * Resamples 16 bit PCM data. - * - * @param channels the number of channels - * @param src_rate the source sample rate - * @param src the source PCM buffer - * @param src_size the size of #src in bytes - * @param dest_rate the requested destination sample rate - * @param dest_size_r returns the number of bytes of the destination buffer - * @return the destination buffer - */ - const int16_t *Resample16(unsigned channels, unsigned src_rate, - const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error_r); - - /** - * Resamples 32 bit PCM data. - * - * @param channels the number of channels - * @param src_rate the source sample rate - * @param src the source PCM buffer - * @param src_size the size of #src in bytes - * @param dest_rate the requested destination sample rate - * @param dest_size_r returns the number of bytes of the destination buffer - * @return the destination buffer - */ - const int32_t *Resample32(unsigned channels, unsigned src_rate, - const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error_r); - - /** - * Resamples 24 bit PCM data. - * - * @param channels the number of channels - * @param src_rate the source sample rate - * @param src the source PCM buffer - * @param src_size the size of #src in bytes - * @param dest_rate the requested destination sample rate - * @param dest_size_r returns the number of bytes of the destination buffer - * @return the destination buffer - */ - const int32_t *Resample24(unsigned channels, unsigned src_rate, - const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error_r); -}; - -bool -pcm_resample_global_init(Error &error); - -#endif diff --git a/src/pcm/PcmResampleFallback.cxx b/src/pcm/PcmResampleFallback.cxx deleted file mode 100644 index a62cd64f7..000000000 --- a/src/pcm/PcmResampleFallback.cxx +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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 "PcmResampleInternal.hxx" - -#include <assert.h> - -/* resampling code blatantly ripped from ESD */ -const int16_t * -pcm_resample_fallback_16(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, - size_t *dest_size_r) -{ - unsigned dest_pos = 0; - unsigned src_frames = src_size / channels / sizeof(*src_buffer); - unsigned dest_frames = - (src_frames * dest_rate + src_rate - 1) / src_rate; - unsigned dest_samples = dest_frames * channels; - size_t dest_size = dest_samples * sizeof(*src_buffer); - int16_t *dest_buffer = (int16_t *)state->buffer.Get(dest_size); - - assert((src_size % (sizeof(*src_buffer) * channels)) == 0); - - switch (channels) { - case 1: - while (dest_pos < dest_samples) { - unsigned src_pos = dest_pos * src_rate / dest_rate; - - dest_buffer[dest_pos++] = src_buffer[src_pos]; - } - break; - case 2: - while (dest_pos < dest_samples) { - unsigned src_pos = dest_pos * src_rate / dest_rate; - src_pos &= ~1; - - dest_buffer[dest_pos++] = src_buffer[src_pos]; - dest_buffer[dest_pos++] = src_buffer[src_pos + 1]; - } - break; - } - - *dest_size_r = dest_size; - return dest_buffer; -} - -const int32_t * -pcm_resample_fallback_32(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, - size_t *dest_size_r) -{ - unsigned dest_pos = 0; - unsigned src_frames = src_size / channels / sizeof(*src_buffer); - unsigned dest_frames = - (src_frames * dest_rate + src_rate - 1) / src_rate; - unsigned dest_samples = dest_frames * channels; - size_t dest_size = dest_samples * sizeof(*src_buffer); - int32_t *dest_buffer = (int32_t *)state->buffer.Get(dest_size); - - assert((src_size % (sizeof(*src_buffer) * channels)) == 0); - - switch (channels) { - case 1: - while (dest_pos < dest_samples) { - unsigned src_pos = dest_pos * src_rate / dest_rate; - - dest_buffer[dest_pos++] = src_buffer[src_pos]; - } - break; - case 2: - while (dest_pos < dest_samples) { - unsigned src_pos = dest_pos * src_rate / dest_rate; - src_pos &= ~1; - - dest_buffer[dest_pos++] = src_buffer[src_pos]; - dest_buffer[dest_pos++] = src_buffer[src_pos + 1]; - } - break; - } - - *dest_size_r = dest_size; - return dest_buffer; -} diff --git a/src/pcm/PcmResampleInternal.hxx b/src/pcm/PcmResampleInternal.hxx deleted file mode 100644 index 5090c13d1..000000000 --- a/src/pcm/PcmResampleInternal.hxx +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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 - * - * Internal declarations for the pcm_resample library. The "internal" - * resampler is called "fallback" in the MPD source, so the file name - * of this header is somewhat unrelated to it. - */ - -#ifndef MPD_PCM_RESAMPLE_INTERNAL_HXX -#define MPD_PCM_RESAMPLE_INTERNAL_HXX - -#include "check.h" -#include "PcmResample.hxx" - -#ifdef HAVE_LIBSAMPLERATE - -bool -pcm_resample_lsr_global_init(const char *converter, Error &error); - -void -pcm_resample_lsr_init(PcmResampler *state); - -void -pcm_resample_lsr_deinit(PcmResampler *state); - -void -pcm_resample_lsr_reset(PcmResampler *state); - -const float * -pcm_resample_lsr_float(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const float *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error); - -const int16_t * -pcm_resample_lsr_16(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error); - -const int32_t * -pcm_resample_lsr_32(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, - size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error); - -const int32_t * -pcm_resample_lsr_24(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, - size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error); - -#endif - -const int16_t * -pcm_resample_fallback_16(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, - size_t *dest_size_r); - -const int32_t * -pcm_resample_fallback_32(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, - size_t src_size, - unsigned dest_rate, - size_t *dest_size_r); - -#endif diff --git a/src/pcm/PcmResampleLibsamplerate.cxx b/src/pcm/PcmResampleLibsamplerate.cxx deleted file mode 100644 index 9eac2d545..000000000 --- a/src/pcm/PcmResampleLibsamplerate.cxx +++ /dev/null @@ -1,310 +0,0 @@ -/* - * 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 "PcmResampleInternal.hxx" -#include "PcmUtils.hxx" -#include "util/ASCII.hxx" -#include "util/Error.hxx" -#include "util/Domain.hxx" -#include "Log.hxx" - -#include <assert.h> -#include <stdlib.h> -#include <string.h> - -static int lsr_converter = SRC_SINC_FASTEST; - -static constexpr Domain libsamplerate_domain("libsamplerate"); - -static bool -lsr_parse_converter(const char *s) -{ - assert(s != nullptr); - - if (*s == 0) - return true; - - char *endptr; - long l = strtol(s, &endptr, 10); - if (*endptr == 0 && src_get_name(l) != nullptr) { - lsr_converter = l; - return true; - } - - size_t length = strlen(s); - for (int i = 0;; ++i) { - const char *name = src_get_name(i); - if (name == nullptr) - break; - - if (StringEqualsCaseASCII(s, name, length)) { - lsr_converter = i; - return true; - } - } - - return false; -} - -bool -pcm_resample_lsr_global_init(const char *converter, Error &error) -{ - if (!lsr_parse_converter(converter)) { - error.Format(libsamplerate_domain, - "unknown samplerate converter '%s'", converter); - return false; - } - - FormatDebug(libsamplerate_domain, - "libsamplerate converter '%s'", - src_get_name(lsr_converter)); - - return true; -} - -void -pcm_resample_lsr_init(PcmResampler *state) -{ - state->state = nullptr; - memset(&state->data, 0, sizeof(state->data)); - memset(&state->prev, 0, sizeof(state->prev)); - state->error = 0; -} - -void -pcm_resample_lsr_deinit(PcmResampler *state) -{ - if (state->state != nullptr) - state->state = src_delete(state->state); -} - -void -pcm_resample_lsr_reset(PcmResampler *state) -{ - if (state->state != nullptr) - src_reset(state->state); -} - -static bool -pcm_resample_set(PcmResampler *state, - unsigned channels, unsigned src_rate, unsigned dest_rate, - Error &error_r) -{ - /* (re)set the state/ratio if the in or out format changed */ - if (channels == state->prev.channels && - src_rate == state->prev.src_rate && - dest_rate == state->prev.dest_rate) - return true; - - state->error = 0; - state->prev.channels = channels; - state->prev.src_rate = src_rate; - state->prev.dest_rate = dest_rate; - - if (state->state) - state->state = src_delete(state->state); - - int error; - state->state = src_new(lsr_converter, channels, &error); - if (!state->state) { - error_r.Format(libsamplerate_domain, error, - "libsamplerate initialization has failed: %s", - src_strerror(error)); - return false; - } - - SRC_DATA *data = &state->data; - data->src_ratio = (double)dest_rate / (double)src_rate; - FormatDebug(libsamplerate_domain, - "setting samplerate conversion ratio to %.2lf", - data->src_ratio); - src_set_ratio(state->state, data->src_ratio); - - return true; -} - -static bool -lsr_process(PcmResampler *state, Error &error) -{ - if (state->error == 0) - state->error = src_process(state->state, &state->data); - if (state->error) { - error.Format(libsamplerate_domain, state->error, - "libsamplerate has failed: %s", - src_strerror(state->error)); - return false; - } - - return true; -} - -const float * -pcm_resample_lsr_float(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const float *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error) -{ - SRC_DATA *data = &state->data; - - assert((src_size % (sizeof(*src_buffer) * channels)) == 0); - - if (!pcm_resample_set(state, channels, src_rate, dest_rate, error)) - return nullptr; - - data->input_frames = src_size / sizeof(*src_buffer) / channels; - data->data_in = const_cast<float *>(src_buffer); - - data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate; - size_t data_out_size = data->output_frames * sizeof(float) * channels; - data->data_out = (float *)state->out.Get(data_out_size); - - if (!lsr_process(state, error)) - return nullptr; - - *dest_size_r = data->output_frames_gen * - sizeof(*data->data_out) * channels; - return data->data_out; -} - -const int16_t * -pcm_resample_lsr_16(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const int16_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error) -{ - SRC_DATA *data = &state->data; - - assert((src_size % (sizeof(*src_buffer) * channels)) == 0); - - if (!pcm_resample_set(state, channels, src_rate, dest_rate, - error)) - return nullptr; - - data->input_frames = src_size / sizeof(*src_buffer) / channels; - size_t data_in_size = data->input_frames * sizeof(float) * channels; - data->data_in = (float *)state->in.Get(data_in_size); - - data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate; - size_t data_out_size = data->output_frames * sizeof(float) * channels; - data->data_out = (float *)state->out.Get(data_out_size); - - src_short_to_float_array(src_buffer, data->data_in, - data->input_frames * channels); - - if (!lsr_process(state, error)) - return nullptr; - - int16_t *dest_buffer; - *dest_size_r = data->output_frames_gen * - sizeof(*dest_buffer) * channels; - dest_buffer = (int16_t *)state->buffer.Get(*dest_size_r); - src_float_to_short_array(data->data_out, dest_buffer, - data->output_frames_gen * channels); - - return dest_buffer; -} - -#ifdef HAVE_LIBSAMPLERATE_NOINT - -/* libsamplerate introduced these functions in v0.1.3 */ - -static void -src_int_to_float_array(const int *in, float *out, int len) -{ - while (len-- > 0) - *out++ = *in++ / (float)(1 << (24 - 1)); -} - -static void -src_float_to_int_array (const float *in, int *out, int len) -{ - while (len-- > 0) - *out++ = *in++ * (float)(1 << (24 - 1)); -} - -#endif - -const int32_t * -pcm_resample_lsr_32(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error) -{ - SRC_DATA *data = &state->data; - - assert((src_size % (sizeof(*src_buffer) * channels)) == 0); - - if (!pcm_resample_set(state, channels, src_rate, dest_rate, - error)) - return nullptr; - - data->input_frames = src_size / sizeof(*src_buffer) / channels; - size_t data_in_size = data->input_frames * sizeof(float) * channels; - data->data_in = (float *)state->in.Get(data_in_size); - - data->output_frames = (src_size * dest_rate + src_rate - 1) / src_rate; - size_t data_out_size = data->output_frames * sizeof(float) * channels; - data->data_out = (float *)state->out.Get(data_out_size); - - src_int_to_float_array(src_buffer, data->data_in, - data->input_frames * channels); - - if (!lsr_process(state, error)) - return nullptr; - - int32_t *dest_buffer; - *dest_size_r = data->output_frames_gen * - sizeof(*dest_buffer) * channels; - dest_buffer = (int32_t *)state->buffer.Get(*dest_size_r); - src_float_to_int_array(data->data_out, dest_buffer, - data->output_frames_gen * channels); - - return dest_buffer; -} - -const int32_t * -pcm_resample_lsr_24(PcmResampler *state, - unsigned channels, - unsigned src_rate, - const int32_t *src_buffer, size_t src_size, - unsigned dest_rate, size_t *dest_size_r, - Error &error) -{ - const auto result = pcm_resample_lsr_32(state, channels, - src_rate, src_buffer, src_size, - dest_rate, dest_size_r, - error); - if (result != nullptr) - /* src_float_to_int_array() clamps for 32 bit - integers; now make sure everything's fine for 24 - bit */ - /* TODO: eliminate the 32 bit clamp to reduce overhead */ - PcmClampN<int32_t, int32_t, 24>(const_cast<int32_t *>(result), - result, - *dest_size_r / sizeof(*result)); - - return result; -} diff --git a/src/pcm/PcmUtils.hxx b/src/pcm/PcmUtils.hxx index febe12d7b..0ca5dd4ac 100644 --- a/src/pcm/PcmUtils.hxx +++ b/src/pcm/PcmUtils.hxx @@ -26,6 +26,9 @@ #include <stdint.h> +enum class SampleFormat : uint8_t; +template<SampleFormat F> struct SampleTraits; + /** * Add a byte count to the specified pointer. This is a utility * function to convert a source pointer and a byte count to an "end" @@ -42,37 +45,24 @@ pcm_end_pointer(const T *p, size_t size) * Check if the value is within the range of the provided bit size, * and caps it if necessary. */ -template<typename T, typename U, unsigned bits> +template<SampleFormat F, class Traits=SampleTraits<F>> gcc_const -static inline T -PcmClamp(U x) +static inline typename Traits::value_type +PcmClamp(typename Traits::long_type x) { - constexpr U MIN_VALUE = -(U(1) << (bits - 1)); - constexpr U MAX_VALUE = (U(1) << (bits - 1)) - 1; + typedef typename Traits::value_type T; typedef std::numeric_limits<T> limits; - static_assert(MIN_VALUE >= limits::min(), "out of range"); - static_assert(MAX_VALUE <= limits::max(), "out of range"); + static_assert(Traits::MIN >= limits::min(), "out of range"); + static_assert(Traits::MAX <= limits::max(), "out of range"); - if (gcc_unlikely(x < MIN_VALUE)) - return T(MIN_VALUE); + if (gcc_unlikely(x < Traits::MIN)) + return T(Traits::MIN); - if (gcc_unlikely(x > MAX_VALUE)) - return T(MAX_VALUE); + if (gcc_unlikely(x > Traits::MAX)) + return T(Traits::MAX); return T(x); } -/** - * Check if the values in this buffer are within the range of the - * provided bit size, and clamps them whenever necessary. - */ -template<typename T, typename U, unsigned bits> -static inline void -PcmClampN(T *dest, const U *src, unsigned n) -{ - while (n-- > 0) - *dest++ = PcmClamp<T, U, bits>(*src++); -} - #endif diff --git a/src/pcm/PcmVolume.cxx b/src/pcm/PcmVolume.cxx deleted file mode 100644 index 564880633..000000000 --- a/src/pcm/PcmVolume.cxx +++ /dev/null @@ -1,188 +0,0 @@ -/* - * 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 "PcmVolume.hxx" -#include "PcmUtils.hxx" -#include "AudioFormat.hxx" - -#include <stdint.h> -#include <string.h> - -static void -pcm_volume_change_8(int8_t *buffer, const int8_t *end, int volume) -{ - while (buffer < end) { - int32_t sample = *buffer; - - sample = (sample * volume + pcm_volume_dither() + - PCM_VOLUME_1 / 2) - / PCM_VOLUME_1; - - *buffer++ = PcmClamp<int8_t, int16_t, 8>(sample); - } -} - -static void -pcm_volume_change_16(int16_t *buffer, const int16_t *end, int volume) -{ - while (buffer < end) { - int32_t sample = *buffer; - - sample = (sample * volume + pcm_volume_dither() + - PCM_VOLUME_1 / 2) - / PCM_VOLUME_1; - - *buffer++ = PcmClamp<int16_t, int32_t, 16>(sample); - } -} - -#ifdef __i386__ -/** - * Optimized volume function for i386. Use the EDX:EAX 2*32 bit - * multiplication result instead of emulating 64 bit multiplication. - */ -static inline int32_t -pcm_volume_sample_24(int32_t sample, int32_t volume, gcc_unused int32_t dither) -{ - int32_t result; - - asm(/* edx:eax = sample * volume */ - "imul %2\n" - - /* "add %3, %1\n" dithering disabled for now, because we - have no overflow check - is dithering really important - here? */ - - /* eax = edx:eax / PCM_VOLUME_1 */ - "sal $22, %%edx\n" - "shr $10, %1\n" - "or %%edx, %1\n" - - : "=a"(result) - : "0"(sample), "r"(volume) /* , "r"(dither) */ - : "edx" - ); - - return result; -} -#endif - -static void -pcm_volume_change_24(int32_t *buffer, const int32_t *end, int volume) -{ - while (buffer < end) { -#ifdef __i386__ - /* assembly version for i386 */ - int32_t sample = *buffer; - - sample = pcm_volume_sample_24(sample, volume, - pcm_volume_dither()); -#else - /* portable version */ - int64_t sample = *buffer; - - sample = (sample * volume + pcm_volume_dither() + - PCM_VOLUME_1 / 2) - / PCM_VOLUME_1; -#endif - *buffer++ = PcmClamp<int32_t, int32_t, 24>(sample); - } -} - -static void -pcm_volume_change_32(int32_t *buffer, const int32_t *end, int volume) -{ - while (buffer < end) { -#ifdef __i386__ - /* assembly version for i386 */ - int32_t sample = *buffer; - - *buffer++ = pcm_volume_sample_24(sample, volume, 0); -#else - /* portable version */ - int64_t sample = *buffer; - - sample = (sample * volume + pcm_volume_dither() + - PCM_VOLUME_1 / 2) - / PCM_VOLUME_1; - *buffer++ = PcmClamp<int32_t, int64_t, 32>(sample); -#endif - } -} - -static void -pcm_volume_change_float(float *buffer, const float *end, float volume) -{ - while (buffer < end) { - float sample = *buffer; - sample *= volume; - *buffer++ = sample; - } -} - -bool -pcm_volume(void *buffer, size_t length, - SampleFormat format, - int volume) -{ - if (volume == PCM_VOLUME_1) - return true; - - if (volume <= 0) { - memset(buffer, 0, length); - return true; - } - - const void *end = pcm_end_pointer(buffer, length); - switch (format) { - case SampleFormat::UNDEFINED: - case SampleFormat::DSD: - /* not implemented */ - return false; - - case SampleFormat::S8: - pcm_volume_change_8((int8_t *)buffer, (const int8_t *)end, - volume); - return true; - - case SampleFormat::S16: - pcm_volume_change_16((int16_t *)buffer, (const int16_t *)end, - volume); - return true; - - case SampleFormat::S24_P32: - pcm_volume_change_24((int32_t *)buffer, (const int32_t *)end, - volume); - return true; - - case SampleFormat::S32: - pcm_volume_change_32((int32_t *)buffer, (const int32_t *)end, - volume); - return true; - - case SampleFormat::FLOAT: - pcm_volume_change_float((float *)buffer, (const float *)end, - pcm_volume_to_float(volume)); - return true; - } - - assert(false); - gcc_unreachable(); -} diff --git a/src/pcm/PcmVolume.hxx b/src/pcm/PcmVolume.hxx deleted file mode 100644 index 8cd82acf7..000000000 --- a/src/pcm/PcmVolume.hxx +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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_PCM_VOLUME_HXX -#define MPD_PCM_VOLUME_HXX - -#include "PcmPrng.hxx" -#include "AudioFormat.hxx" - -#include <stdint.h> -#include <stddef.h> - -enum { - /** this value means "100% volume" */ - PCM_VOLUME_1 = 1024, -}; - -struct AudioFormat; - -/** - * Converts a float value (0.0 = silence, 1.0 = 100% volume) to an - * integer volume value (1000 = 100%). - */ -static inline int -pcm_float_to_volume(float volume) -{ - return volume * PCM_VOLUME_1 + 0.5; -} - -static inline float -pcm_volume_to_float(int volume) -{ - return (float)volume / (float)PCM_VOLUME_1; -} - -/** - * Returns the next volume dithering number, between -511 and +511. - * This number is taken from a global PRNG, see pcm_prng(). - */ -static inline int -pcm_volume_dither(void) -{ - static unsigned long state; - uint32_t r; - - r = state = pcm_prng(state); - - return (r & 511) - ((r >> 9) & 511); -} - -/** - * Adjust the volume of the specified PCM buffer. - * - * @param buffer the PCM buffer - * @param length the length of the PCM buffer - * @param format the sample format of the PCM buffer - * @param volume the volume between 0 and #PCM_VOLUME_1 - * @return true on success, false if the audio format is not supported - */ -bool -pcm_volume(void *buffer, size_t length, - SampleFormat format, - int volume); - -#endif diff --git a/src/pcm/Resampler.hxx b/src/pcm/Resampler.hxx new file mode 100644 index 000000000..a74ef4e77 --- /dev/null +++ b/src/pcm/Resampler.hxx @@ -0,0 +1,74 @@ +/* + * 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_PCM_RESAMPLER_HXX +#define MPD_PCM_RESAMPLER_HXX + +#include "util/ConstBuffer.hxx" +#include "Compiler.h" + +struct AudioFormat; +class Error; + +/** + * This is an interface for plugins that convert PCM data to a + * specific sample rate. + */ +class PcmResampler { +public: + virtual ~PcmResampler() {} + + /** + * Opens the resampler, preparing it for Resample(). + * + * @param af the audio format of incoming data; the plugin may + * modify the object to enforce another input format (however, + * it may not request a different input sample rate) + * @param new_sample_rate the requested output sample rate + * @param error location to store the error + * @return the format of outgoing data or + * AudioFormat::Undefined() on error + */ + virtual AudioFormat Open(AudioFormat &af, unsigned new_sample_rate, + Error &error) = 0; + + /** + * Closes the resampler. After that, you may call Open() + * again. + */ + virtual void Close() = 0; + + /** + * Resamples a block of PCM data. + * + * @param src the input buffer + * @param src_size the size of #src_buffer in bytes + * @param dest_size_r the size of the returned buffer + * @param error location to store the error occurring, or nullptr + * to ignore errors. + * @return the destination buffer on success (will be + * invalidated by filter_close() or filter_filter()), nullptr on + * error + */ + gcc_pure + virtual ConstBuffer<void> Resample(ConstBuffer<void> src, + Error &error) = 0; +}; + +#endif diff --git a/src/pcm/SoxrResampler.cxx b/src/pcm/SoxrResampler.cxx new file mode 100644 index 000000000..e82ae1481 --- /dev/null +++ b/src/pcm/SoxrResampler.cxx @@ -0,0 +1,95 @@ +/* + * 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 "SoxrResampler.hxx" +#include "AudioFormat.hxx" +#include "util/ASCII.hxx" +#include "util/Error.hxx" +#include "util/Domain.hxx" +#include "Log.hxx" + +#include <soxr.h> + +#include <assert.h> + +static constexpr Domain soxr_domain("soxr"); + +AudioFormat +SoxrPcmResampler::Open(AudioFormat &af, unsigned new_sample_rate, + Error &error) +{ + assert(af.IsValid()); + assert(audio_valid_sample_rate(new_sample_rate)); + + soxr_error_t e; + soxr = soxr_create(af.sample_rate, new_sample_rate, + af.channels, &e, + nullptr, nullptr, nullptr); + if (soxr == nullptr) { + error.Format(soxr_domain, + "soxr initialization has failed: %s", e); + return AudioFormat::Undefined(); + } + + FormatDebug(soxr_domain, "soxr engine '%s'", soxr_engine(soxr)); + + channels = af.channels; + + ratio = float(new_sample_rate) / float(af.sample_rate); + FormatDebug(soxr_domain, + "samplerate conversion ratio to %.2lf", + ratio); + + /* libsoxr works with floating point samples */ + af.format = SampleFormat::FLOAT; + + AudioFormat result = af; + result.sample_rate = new_sample_rate; + return result; +} + +void +SoxrPcmResampler::Close() +{ + soxr_delete(soxr); +} + +ConstBuffer<void> +SoxrPcmResampler::Resample(ConstBuffer<void> src, Error &error) +{ + const size_t frame_size = channels * sizeof(float); + assert(src.size % frame_size == 0); + + const size_t n_frames = src.size / frame_size; + + const size_t o_frames = size_t(n_frames * ratio + 0.5); + + float *output_buffer = (float *)buffer.Get(o_frames * frame_size); + + size_t i_done, o_done; + soxr_error_t e = soxr_process(soxr, src.data, n_frames, &i_done, + output_buffer, o_frames, &o_done); + if (e != nullptr) { + error.Format(soxr_domain, "soxr error: %s", e); + return nullptr; + } + + return { output_buffer, o_done * frame_size }; +} diff --git a/src/pcm/SoxrResampler.hxx b/src/pcm/SoxrResampler.hxx new file mode 100644 index 000000000..69c173741 --- /dev/null +++ b/src/pcm/SoxrResampler.hxx @@ -0,0 +1,47 @@ +/* + * 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_PCM_SOXR_RESAMPLER_HXX +#define MPD_PCM_SOXR_RESAMPLER_HXX + +#include "Resampler.hxx" +#include "PcmBuffer.hxx" + +struct AudioFormat; + +/** + * A resampler using soxr. + */ +class SoxrPcmResampler final : public PcmResampler { + struct soxr *soxr; + + unsigned channels; + float ratio; + + PcmBuffer buffer; + +public: + virtual AudioFormat Open(AudioFormat &af, unsigned new_sample_rate, + Error &error) override; + virtual void Close() override; + virtual ConstBuffer<void> Resample(ConstBuffer<void> src, + Error &error) override; +}; + +#endif diff --git a/src/pcm/Traits.hxx b/src/pcm/Traits.hxx new file mode 100644 index 000000000..ac1ac532d --- /dev/null +++ b/src/pcm/Traits.hxx @@ -0,0 +1,153 @@ +/* + * 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_PCM_TRAITS_HXX +#define MPD_PCM_TRAITS_HXX + +#include "check.h" +#include "AudioFormat.hxx" + +#include <stdint.h> +#include <stddef.h> + +/** + * This template describes the specified #SampleFormat. This is an + * empty prototype; the specializations contain the real definitions. + * See SampleTraits<uint8_t> for more documentation. + */ +template<SampleFormat F> +struct SampleTraits {}; + +template<> +struct SampleTraits<SampleFormat::S8> { + /** + * The type used for one sample value. + */ + typedef int8_t value_type; + + /** + * A writable pointer. + */ + typedef value_type *pointer_type; + + /** + * A read-only pointer. + */ + typedef const value_type *const_pointer_type; + + /** + * A "long" type that is large and accurate enough for adding + * two values without risking an (integer) overflow or + * (floating point) precision loss. + */ + typedef int sum_type; + + /** + * A "long" type that is large and accurate enough for + * arithmetic without risking an (integer) overflow or + * (floating point) precision loss. + */ + typedef int_least32_t long_type; + + /** + * The size of one sample in bytes. + */ + static constexpr size_t SAMPLE_SIZE = sizeof(value_type); + + /** + * The integer bit depth of one sample. This attribute may + * not exist if this is not an integer sample format. + */ + static constexpr unsigned BITS = sizeof(value_type) * 8; + + /** + * The minimum sample value. + */ + static constexpr value_type MIN = -(sum_type(1) << (BITS - 1)); + + /** + * The maximum sample value. + */ + static constexpr value_type MAX = (sum_type(1) << (BITS - 1)) - 1; +}; + +template<> +struct SampleTraits<SampleFormat::S16> { + typedef int16_t value_type; + typedef value_type *pointer_type; + typedef const value_type *const_pointer_type; + + typedef int_least32_t sum_type; + typedef int_least32_t long_type; + + static constexpr size_t SAMPLE_SIZE = sizeof(value_type); + static constexpr unsigned BITS = sizeof(value_type) * 8; + + static constexpr value_type MIN = -(sum_type(1) << (BITS - 1)); + static constexpr value_type MAX = (sum_type(1) << (BITS - 1)) - 1; +}; + +template<> +struct SampleTraits<SampleFormat::S32> { + typedef int32_t value_type; + typedef value_type *pointer_type; + typedef const value_type *const_pointer_type; + + typedef int_least64_t sum_type; + typedef int_least64_t long_type; + + static constexpr size_t SAMPLE_SIZE = sizeof(value_type); + static constexpr unsigned BITS = sizeof(value_type) * 8; + + static constexpr value_type MIN = -(sum_type(1) << (BITS - 1)); + static constexpr value_type MAX = (sum_type(1) << (BITS - 1)) - 1; +}; + +template<> +struct SampleTraits<SampleFormat::S24_P32> { + typedef int32_t value_type; + typedef value_type *pointer_type; + typedef const value_type *const_pointer_type; + + typedef int_least32_t sum_type; + typedef int_least64_t long_type; + + static constexpr size_t SAMPLE_SIZE = sizeof(value_type); + static constexpr unsigned BITS = 24; + + static constexpr value_type MIN = -(sum_type(1) << (BITS - 1)); + static constexpr value_type MAX = (sum_type(1) << (BITS - 1)) - 1; +}; + +template<> +struct SampleTraits<SampleFormat::FLOAT> { + typedef float value_type; + typedef value_type *pointer_type; + typedef const value_type *const_pointer_type; + + typedef float sum_type; + typedef float long_type; + + static constexpr size_t SAMPLE_SIZE = sizeof(value_type); + + static constexpr value_type MIN = -1; + static constexpr value_type MAX = 1; +}; + +#endif diff --git a/src/pcm/Volume.cxx b/src/pcm/Volume.cxx new file mode 100644 index 000000000..6347657c6 --- /dev/null +++ b/src/pcm/Volume.cxx @@ -0,0 +1,189 @@ +/* + * 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 "Volume.hxx" +#include "Domain.hxx" +#include "PcmUtils.hxx" +#include "Traits.hxx" +#include "util/ConstBuffer.hxx" +#include "util/Error.hxx" + +#include "PcmDither.cxx" // including the .cxx file to get inlined templates + +#include <stdint.h> +#include <string.h> + +template<SampleFormat F, class Traits=SampleTraits<F>> +static inline typename Traits::value_type +pcm_volume_sample(PcmDither &dither, + typename Traits::value_type _sample, + int volume) +{ + typename Traits::long_type sample(_sample); + + return dither.DitherShift<typename Traits::long_type, + Traits::BITS + PCM_VOLUME_BITS, + Traits::BITS>(sample * volume); +} + +template<SampleFormat F, class Traits=SampleTraits<F>> +static void +pcm_volume_change(PcmDither &dither, + typename Traits::pointer_type dest, + typename Traits::const_pointer_type src, + typename Traits::const_pointer_type end, + int volume) +{ + while (src < end) { + const auto sample = *src++; + *dest++ = pcm_volume_sample<F, Traits>(dither, sample, volume); + } +} + +static void +pcm_volume_change_8(PcmDither &dither, + int8_t *dest, const int8_t *src, const int8_t *end, + int volume) +{ + pcm_volume_change<SampleFormat::S8>(dither, dest, src, end, volume); +} + +static void +pcm_volume_change_16(PcmDither &dither, + int16_t *dest, const int16_t *src, const int16_t *end, + int volume) +{ + pcm_volume_change<SampleFormat::S16>(dither, dest, src, end, volume); +} + +static void +pcm_volume_change_24(PcmDither &dither, + int32_t *dest, const int32_t *src, const int32_t *end, + int volume) +{ + pcm_volume_change<SampleFormat::S24_P32>(dither, dest, src, end, + volume); +} + +static void +pcm_volume_change_32(PcmDither &dither, + int32_t *dest, const int32_t *src, const int32_t *end, + int volume) +{ + pcm_volume_change<SampleFormat::S32>(dither, dest, src, end, volume); +} + +static void +pcm_volume_change_float(float *dest, const float *src, const float *end, + float volume) +{ + while (src < end) { + float sample = *src++; + sample *= volume; + *dest++ = sample; + } +} + +bool +PcmVolume::Open(SampleFormat _format, Error &error) +{ + assert(format == SampleFormat::UNDEFINED); + + switch (_format) { + case SampleFormat::UNDEFINED: + case SampleFormat::DSD: + error.Format(pcm_domain, + "Software volume for %s is not implemented", + sample_format_to_string(_format)); + return false; + + case SampleFormat::S8: + case SampleFormat::S16: + case SampleFormat::S24_P32: + case SampleFormat::S32: + case SampleFormat::FLOAT: + break; + } + + format = _format; + return true; +} + +ConstBuffer<void> +PcmVolume::Apply(ConstBuffer<void> src) +{ + if (volume == PCM_VOLUME_1) + return src; + + void *data = buffer.Get(src.size); + + if (volume == 0) { + /* optimized special case: 0% volume = memset(0) */ + /* TODO: is this valid for all sample formats? What + about floating point? */ + memset(data, 0, src.size); + return { data, src.size }; + } + + const void *end = pcm_end_pointer(src.data, src.size); + switch (format) { + case SampleFormat::UNDEFINED: + case SampleFormat::DSD: + assert(false); + gcc_unreachable(); + + case SampleFormat::S8: + pcm_volume_change_8(dither, (int8_t *)data, + (const int8_t *)src.data, + (const int8_t *)end, + volume); + break; + + case SampleFormat::S16: + pcm_volume_change_16(dither, (int16_t *)data, + (const int16_t *)src.data, + (const int16_t *)end, + volume); + break; + + case SampleFormat::S24_P32: + pcm_volume_change_24(dither, (int32_t *)data, + (const int32_t *)src.data, + (const int32_t *)end, + volume); + break; + + case SampleFormat::S32: + pcm_volume_change_32(dither, (int32_t *)data, + (const int32_t *)src.data, + (const int32_t *)end, + volume); + break; + + case SampleFormat::FLOAT: + pcm_volume_change_float((float *)data, + (const float *)src.data, + (const float *)end, + pcm_volume_to_float(volume)); + break; + } + + return { data, src.size }; +} diff --git a/src/pcm/Volume.hxx b/src/pcm/Volume.hxx new file mode 100644 index 000000000..6a6f6196d --- /dev/null +++ b/src/pcm/Volume.hxx @@ -0,0 +1,130 @@ +/* + * 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_PCM_VOLUME_HXX +#define MPD_PCM_VOLUME_HXX + +#include "AudioFormat.hxx" +#include "PcmBuffer.hxx" +#include "PcmDither.hxx" + +#include <stdint.h> +#include <stddef.h> + +#ifndef NDEBUG +#include <assert.h> +#endif + +class Error; +template<typename T> struct ConstBuffer; + +/** + * Number of fractional bits for a fixed-point volume value. + */ +static constexpr unsigned PCM_VOLUME_BITS = 10; + +/** + * This value means "100% volume". + */ +static constexpr unsigned PCM_VOLUME_1 = 1024; +static constexpr int PCM_VOLUME_1S = PCM_VOLUME_1; + +struct AudioFormat; + +/** + * Converts a float value (0.0 = silence, 1.0 = 100% volume) to an + * integer volume value (1000 = 100%). + */ +static inline int +pcm_float_to_volume(float volume) +{ + return volume * PCM_VOLUME_1 + 0.5; +} + +static inline float +pcm_volume_to_float(int volume) +{ + return (float)volume / (float)PCM_VOLUME_1; +} + +/** + * A class that converts samples from one format to another. + */ +class PcmVolume { + SampleFormat format; + + unsigned volume; + + PcmBuffer buffer; + PcmDither dither; + +public: + PcmVolume() + :volume(PCM_VOLUME_1) { +#ifndef NDEBUG + format = SampleFormat::UNDEFINED; +#endif + } + +#ifndef NDEBUG + ~PcmVolume() { + assert(format == SampleFormat::UNDEFINED); + } +#endif + + unsigned GetVolume() const { + return volume; + } + + /** + * @param _volume the volume level in the range + * [0..#PCM_VOLUME_1]; may be bigger than #PCM_VOLUME_1, but + * then it will most likely clip a lot + */ + void SetVolume(unsigned _volume) { + volume = _volume; + } + + /** + * Opens the object, prepare for Apply(). + * + * @param format the sample format + * @param error location to store the error + * @return true on success + */ + bool Open(SampleFormat format, Error &error); + + /** + * Closes the object. After that, you may call Open() again. + */ + void Close() { +#ifndef NDEBUG + assert(format != SampleFormat::UNDEFINED); + format = SampleFormat::UNDEFINED; +#endif + } + + /** + * Apply the volume level. + */ + gcc_pure + ConstBuffer<void> Apply(ConstBuffer<void> src); +}; + +#endif diff --git a/src/playlist/AsxPlaylistPlugin.cxx b/src/playlist/AsxPlaylistPlugin.cxx index 94198b8c3..47983358a 100644 --- a/src/playlist/AsxPlaylistPlugin.cxx +++ b/src/playlist/AsxPlaylistPlugin.cxx @@ -23,7 +23,7 @@ #include "MemorySongEnumerator.hxx" #include "InputStream.hxx" #include "Song.hxx" -#include "tag/Tag.hxx" +#include "tag/TagBuilder.hxx" #include "util/ASCII.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" @@ -31,7 +31,6 @@ #include <glib.h> -#include <assert.h> #include <string.h> static constexpr Domain asx_domain("asx"); @@ -58,7 +57,7 @@ struct AsxParser { * valid if state==ENTRY. TAG_NUM_OF_ITEM_TYPES means there * is no (known) tag. */ - TagType tag; + TagType tag_type; /** * The current song. It is allocated after the "location" @@ -66,6 +65,8 @@ struct AsxParser { */ Song *song; + TagBuilder tag_builder; + AsxParser() :state(ROOT) {} @@ -96,7 +97,7 @@ asx_start_element(gcc_unused GMarkupParseContext *context, if (StringEqualsCaseASCII(element_name, "entry")) { parser->state = AsxParser::ENTRY; parser->song = Song::NewRemote("asx:"); - parser->tag = TAG_NUM_OF_ITEM_TYPES; + parser->tag_type = TAG_NUM_OF_ITEM_TYPES; } break; @@ -107,27 +108,22 @@ asx_start_element(gcc_unused GMarkupParseContext *context, attribute_values, "href"); if (href != nullptr) { - /* create new song object, and copy - the existing tag over; we cannot + /* create new song object; we cannot replace the existing song's URI, because that attribute is immutable */ Song *song = Song::NewRemote(href); - - if (parser->song != nullptr) { - song->tag = parser->song->tag; - parser->song->tag = nullptr; + if (parser->song != nullptr) parser->song->Free(); - } parser->song = song; } } else if (StringEqualsCaseASCII(element_name, "author")) /* is that correct? or should it be COMPOSER or PERFORMER? */ - parser->tag = TAG_ARTIST; + parser->tag_type = TAG_ARTIST; else if (StringEqualsCaseASCII(element_name, "title")) - parser->tag = TAG_TITLE; + parser->tag_type = TAG_TITLE; break; } @@ -146,14 +142,16 @@ asx_end_element(gcc_unused GMarkupParseContext *context, case AsxParser::ENTRY: if (StringEqualsCaseASCII(element_name, "entry")) { - if (strcmp(parser->song->uri, "asx:") != 0) + if (strcmp(parser->song->uri, "asx:") != 0) { + assert(parser->song->tag == nullptr); + parser->song->tag = parser->tag_builder.Commit(); parser->songs.emplace_front(parser->song); - else + } else parser->song->Free(); parser->state = AsxParser::ROOT; } else - parser->tag = TAG_NUM_OF_ITEM_TYPES; + parser->tag_type = TAG_NUM_OF_ITEM_TYPES; break; } @@ -171,11 +169,9 @@ asx_text(gcc_unused GMarkupParseContext *context, break; case AsxParser::ENTRY: - if (parser->tag != TAG_NUM_OF_ITEM_TYPES) { - if (parser->song->tag == nullptr) - parser->song->tag = new Tag(); - parser->song->tag->AddItem(parser->tag, - text, text_len); + if (parser->tag_type != TAG_NUM_OF_ITEM_TYPES) { + parser->tag_builder.AddItem(parser->tag_type, + text, text_len); } break; diff --git a/src/playlist/CuePlaylistPlugin.cxx b/src/playlist/CuePlaylistPlugin.cxx index 42a43bbad..00aa758b0 100644 --- a/src/playlist/CuePlaylistPlugin.cxx +++ b/src/playlist/CuePlaylistPlugin.cxx @@ -21,13 +21,10 @@ #include "CuePlaylistPlugin.hxx" #include "PlaylistPlugin.hxx" #include "SongEnumerator.hxx" -#include "tag/Tag.hxx" -#include "Song.hxx" #include "cue/CueParser.hxx" #include "TextInputStream.hxx" -#include <assert.h> -#include <string.h> +#include <string> class CuePlaylist final : public SongEnumerator { InputStream &is; diff --git a/src/playlist/EmbeddedCuePlaylistPlugin.cxx b/src/playlist/EmbeddedCuePlaylistPlugin.cxx index d758650eb..96d83b968 100644 --- a/src/playlist/EmbeddedCuePlaylistPlugin.cxx +++ b/src/playlist/EmbeddedCuePlaylistPlugin.cxx @@ -27,7 +27,6 @@ #include "EmbeddedCuePlaylistPlugin.hxx" #include "PlaylistPlugin.hxx" #include "SongEnumerator.hxx" -#include "tag/Tag.hxx" #include "tag/TagHandler.hxx" #include "tag/TagId3.hxx" #include "tag/ApeTag.hxx" @@ -38,7 +37,6 @@ #include "fs/AllocatedPath.hxx" #include "util/ASCII.hxx" -#include <assert.h> #include <string.h> class EmbeddedCuePlaylist final : public SongEnumerator { @@ -95,7 +93,7 @@ embcue_playlist_open_uri(const char *uri, gcc_unused Mutex &mutex, gcc_unused Cond &cond) { - if (!PathTraits::IsAbsoluteUTF8(uri)) + if (!PathTraitsUTF8::IsAbsolute(uri)) /* only local files supported */ return nullptr; @@ -118,7 +116,7 @@ embcue_playlist_open_uri(const char *uri, return nullptr; } - playlist->filename = PathTraits::GetBaseUTF8(uri); + playlist->filename = PathTraitsUTF8::GetBase(uri); playlist->next = &playlist->cuesheet[0]; playlist->parser = new CueParser(); diff --git a/src/playlist/ExtM3uPlaylistPlugin.cxx b/src/playlist/ExtM3uPlaylistPlugin.cxx index 8d260fec7..d48db1ed2 100644 --- a/src/playlist/ExtM3uPlaylistPlugin.cxx +++ b/src/playlist/ExtM3uPlaylistPlugin.cxx @@ -23,11 +23,10 @@ #include "SongEnumerator.hxx" #include "Song.hxx" #include "tag/Tag.hxx" +#include "tag/TagBuilder.hxx" #include "util/StringUtil.hxx" #include "TextInputStream.hxx" -#include <glib.h> - #include <string.h> #include <stdlib.h> @@ -74,7 +73,6 @@ extm3u_parse_tag(const char *line) long duration; char *endptr; const char *name; - Tag *tag; duration = strtol(line, &endptr, 10); if (endptr[0] != ',') @@ -91,16 +89,16 @@ extm3u_parse_tag(const char *line) object */ return NULL; - tag = new Tag(); - tag->time = duration; + TagBuilder tag; + tag.SetTime(duration); /* unfortunately, there is no real specification for the EXTM3U format, so we must assume that the string after the comma is opaque, and is just the song name*/ if (*name != 0) - tag->AddItem(TAG_NAME, name); + tag.AddItem(TAG_NAME, name); - return tag; + return tag.Commit(); } Song * @@ -119,7 +117,7 @@ ExtM3uPlaylist::NextSong() line_s = line.c_str(); - if (g_str_has_prefix(line_s, "#EXTINF:")) { + if (StringStartsWith(line_s, "#EXTINF:")) { delete tag; tag = extm3u_parse_tag(line_s + 8); continue; diff --git a/src/playlist/PlsPlaylistPlugin.cxx b/src/playlist/PlsPlaylistPlugin.cxx index d44a34cdf..329bd8bfb 100644 --- a/src/playlist/PlsPlaylistPlugin.cxx +++ b/src/playlist/PlsPlaylistPlugin.cxx @@ -23,7 +23,7 @@ #include "MemorySongEnumerator.hxx" #include "InputStream.hxx" #include "Song.hxx" -#include "tag/Tag.hxx" +#include "tag/TagBuilder.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" #include "Log.hxx" @@ -32,6 +32,8 @@ #include <string> +#include <stdio.h> + static constexpr Domain pls_domain("pls"); static void @@ -75,14 +77,14 @@ pls_parser(GKeyFile *keyfile, std::forward_list<SongPointer> &songs) song = Song::NewRemote(value); g_free(value); + TagBuilder tag; + sprintf(key, "Title%u", num_entries); value = g_key_file_get_string(keyfile, "playlist", key, &error); - if(error == nullptr && value){ - if (song->tag == nullptr) - song->tag = new Tag(); - song->tag->AddItem(TAG_TITLE, value); - } + if (error == nullptr && value != nullptr) + tag.AddItem(TAG_TITLE, value); + /* Ignore errors? Most likely value not present */ if(error) g_error_free(error); error = nullptr; @@ -91,15 +93,14 @@ pls_parser(GKeyFile *keyfile, std::forward_list<SongPointer> &songs) sprintf(key, "Length%u", num_entries); length = g_key_file_get_integer(keyfile, "playlist", key, &error); - if(error == nullptr && length > 0){ - if (song->tag == nullptr) - song->tag = new Tag(); - song->tag->time = length; - } + if (error == nullptr && length > 0) + tag.SetTime(length); + /* Ignore errors? Most likely value not present */ if(error) g_error_free(error); error = nullptr; + song->tag = tag.Commit(); songs.emplace_front(song); num_entries--; } diff --git a/src/playlist/RssPlaylistPlugin.cxx b/src/playlist/RssPlaylistPlugin.cxx index e2a44bfd3..62f636126 100644 --- a/src/playlist/RssPlaylistPlugin.cxx +++ b/src/playlist/RssPlaylistPlugin.cxx @@ -23,7 +23,7 @@ #include "MemorySongEnumerator.hxx" #include "InputStream.hxx" #include "Song.hxx" -#include "tag/Tag.hxx" +#include "tag/TagBuilder.hxx" #include "util/ASCII.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" @@ -31,7 +31,6 @@ #include <glib.h> -#include <assert.h> #include <string.h> static constexpr Domain rss_domain("rss"); @@ -58,7 +57,7 @@ struct RssParser { * valid if state==ITEM. TAG_NUM_OF_ITEM_TYPES means there * is no (known) tag. */ - TagType tag; + TagType tag_type; /** * The current song. It is allocated after the "location" @@ -66,6 +65,8 @@ struct RssParser { */ Song *song; + TagBuilder tag_builder; + RssParser() :state(ROOT) {} }; @@ -95,7 +96,7 @@ rss_start_element(gcc_unused GMarkupParseContext *context, if (StringEqualsCaseASCII(element_name, "item")) { parser->state = RssParser::ITEM; parser->song = Song::NewRemote("rss:"); - parser->tag = TAG_NUM_OF_ITEM_TYPES; + parser->tag_type = TAG_NUM_OF_ITEM_TYPES; } break; @@ -106,25 +107,21 @@ rss_start_element(gcc_unused GMarkupParseContext *context, attribute_values, "url"); if (href != nullptr) { - /* create new song object, and copy - the existing tag over; we cannot + /* create new song object; we cannot replace the existing song's URI, because that attribute is immutable */ Song *song = Song::NewRemote(href); - if (parser->song != nullptr) { - song->tag = parser->song->tag; - parser->song->tag = nullptr; + if (parser->song != nullptr) parser->song->Free(); - } parser->song = song; } } else if (StringEqualsCaseASCII(element_name, "title")) - parser->tag = TAG_TITLE; + parser->tag_type = TAG_TITLE; else if (StringEqualsCaseASCII(element_name, "itunes:author")) - parser->tag = TAG_ARTIST; + parser->tag_type = TAG_ARTIST; break; } @@ -143,14 +140,16 @@ rss_end_element(gcc_unused GMarkupParseContext *context, case RssParser::ITEM: if (StringEqualsCaseASCII(element_name, "item")) { - if (strcmp(parser->song->uri, "rss:") != 0) + if (strcmp(parser->song->uri, "rss:") != 0) { + assert(parser->song->tag == nullptr); + parser->song->tag = parser->tag_builder.Commit(); parser->songs.emplace_front(parser->song); - else + } else parser->song->Free(); parser->state = RssParser::ROOT; } else - parser->tag = TAG_NUM_OF_ITEM_TYPES; + parser->tag_type = TAG_NUM_OF_ITEM_TYPES; break; } @@ -168,12 +167,9 @@ rss_text(gcc_unused GMarkupParseContext *context, break; case RssParser::ITEM: - if (parser->tag != TAG_NUM_OF_ITEM_TYPES) { - if (parser->song->tag == nullptr) - parser->song->tag = new Tag(); - parser->song->tag->AddItem(parser->tag, - text, text_len); - } + if (parser->tag_type != TAG_NUM_OF_ITEM_TYPES) + parser->tag_builder.AddItem(parser->tag_type, + text, text_len); break; } diff --git a/src/playlist/SoundCloudPlaylistPlugin.cxx b/src/playlist/SoundCloudPlaylistPlugin.cxx index f6797b14d..0ab250b22 100644 --- a/src/playlist/SoundCloudPlaylistPlugin.cxx +++ b/src/playlist/SoundCloudPlaylistPlugin.cxx @@ -24,7 +24,8 @@ #include "ConfigData.hxx" #include "InputStream.hxx" #include "Song.hxx" -#include "tag/Tag.hxx" +#include "tag/TagBuilder.hxx" +#include "util/StringUtil.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" #include "Log.hxx" @@ -62,12 +63,13 @@ soundcloud_init(const config_param ¶m) * @return Constructed URL. Must be freed with g_free. */ static char * -soundcloud_resolve(const char* uri) { +soundcloud_resolve(const char* uri) +{ char *u, *ru; - if (g_str_has_prefix(uri, "http://")) { + if (StringStartsWith(uri, "http://")) { u = g_strdup(uri); - } else if (g_str_has_prefix(uri, "soundcloud.com")) { + } else if (StringStartsWith(uri, "soundcloud.com")) { u = g_strconcat("http://", uri, nullptr); } else { /* assume it's just a path on soundcloud.com */ @@ -108,12 +110,13 @@ struct parse_data { std::forward_list<SongPointer> songs; }; -static int handle_integer(void *ctx, - long +static int +handle_integer(void *ctx, + long #ifndef HAVE_YAJL1 - long + long #endif - intval) + intval) { struct parse_data *data = (struct parse_data *) ctx; @@ -128,13 +131,14 @@ static int handle_integer(void *ctx, return 1; } -static int handle_string(void *ctx, const unsigned char* stringval, +static int +handle_string(void *ctx, const unsigned char* stringval, #ifdef HAVE_YAJL1 - unsigned int + unsigned int #else - size_t + size_t #endif - stringlen) + stringlen) { struct parse_data *data = (struct parse_data *) ctx; const char *s = (const char *) stringval; @@ -158,13 +162,14 @@ static int handle_string(void *ctx, const unsigned char* stringval, return 1; } -static int handle_mapkey(void *ctx, const unsigned char* stringval, +static int +handle_mapkey(void *ctx, const unsigned char* stringval, #ifdef HAVE_YAJL1 - unsigned int + unsigned int #else - size_t + size_t #endif - stringlen) + stringlen) { struct parse_data *data = (struct parse_data *) ctx; @@ -181,7 +186,8 @@ static int handle_mapkey(void *ctx, const unsigned char* stringval, return 1; } -static int handle_start_map(void *ctx) +static int +handle_start_map(void *ctx) { struct parse_data *data = (struct parse_data *) ctx; @@ -191,7 +197,8 @@ static int handle_start_map(void *ctx) return 1; } -static int handle_end_map(void *ctx) +static int +handle_end_map(void *ctx) { struct parse_data *data = (struct parse_data *) ctx; @@ -206,19 +213,16 @@ static int handle_end_map(void *ctx) /* got_url == 1, track finished, make it into a song */ data->got_url = 0; - Song *s; - char *u; - - u = g_strconcat(data->stream_url, "?client_id=", - soundcloud_config.apikey.c_str(), nullptr); - s = Song::NewRemote(u); + char *u = g_strconcat(data->stream_url, "?client_id=", + soundcloud_config.apikey.c_str(), nullptr); + Song *s = Song::NewRemote(u); g_free(u); - Tag *t = new Tag(); - t->time = data->duration / 1000; + TagBuilder tag; + tag.SetTime(data->duration / 1000); if (data->title != nullptr) - t->AddItem(TAG_NAME, data->title); - s->tag = t; + tag.AddItem(TAG_NAME, data->title); + s->tag = tag.Commit(); data->songs.emplace_front(s); @@ -249,9 +253,6 @@ static int soundcloud_parse_json(const char *url, yajl_handle hand, Mutex &mutex, Cond &cond) { - char buffer[4096]; - unsigned char *ubuffer = (unsigned char *)buffer; - Error error; InputStream *input_stream = InputStream::Open(url, mutex, cond, error); @@ -268,6 +269,8 @@ soundcloud_parse_json(const char *url, yajl_handle hand, int done = 0; while (!done) { + char buffer[4096]; + unsigned char *ubuffer = (unsigned char *)buffer; const size_t nbytes = input_stream->Read(buffer, sizeof(buffer), error); if (nbytes == 0) { @@ -318,22 +321,22 @@ soundcloud_parse_json(const char *url, yajl_handle hand, * soundcloud://playlist/<playlist-id> * soundcloud://url/<url or path of soundcloud page> */ - static SongEnumerator * soundcloud_open_uri(const char *uri, Mutex &mutex, Cond &cond) { - char *s, *p; - char *scheme, *arg, *rest; - s = g_strdup(uri); - scheme = s; - for (p = s; *p; p++) { + char *s = g_strdup(uri); + char *scheme = s; + + char *p = s; + for (; *p; p++) { if (*p == ':' && *(p+1) == '/' && *(p+2) == '/') { *p = 0; p += 3; break; } } - arg = p; + + char *arg = p; for (; *p; p++) { if (*p == '/') { *p = 0; @@ -341,7 +344,8 @@ soundcloud_open_uri(const char *uri, Mutex &mutex, Cond &cond) break; } } - rest = p; + + char *rest = p; if (strcmp(scheme, "soundcloud") != 0) { FormatWarning(soundcloud_domain, @@ -372,16 +376,15 @@ soundcloud_open_uri(const char *uri, Mutex &mutex, Cond &cond) return nullptr; } - yajl_handle hand; struct parse_data data; - data.got_url = 0; data.title = nullptr; data.stream_url = nullptr; #ifdef HAVE_YAJL1 - hand = yajl_alloc(&parse_callbacks, nullptr, nullptr, (void *) &data); + yajl_handle hand = yajl_alloc(&parse_callbacks, nullptr, nullptr, + &data); #else - hand = yajl_alloc(&parse_callbacks, nullptr, (void *) &data); + yajl_handle hand = yajl_alloc(&parse_callbacks, nullptr, &data); #endif int ret = soundcloud_parse_json(u, hand, mutex, cond); diff --git a/src/playlist/XspfPlaylistPlugin.cxx b/src/playlist/XspfPlaylistPlugin.cxx index dcfab5a80..2935225e4 100644 --- a/src/playlist/XspfPlaylistPlugin.cxx +++ b/src/playlist/XspfPlaylistPlugin.cxx @@ -22,14 +22,13 @@ #include "PlaylistPlugin.hxx" #include "MemorySongEnumerator.hxx" #include "InputStream.hxx" -#include "tag/Tag.hxx" +#include "tag/TagBuilder.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" #include "Log.hxx" #include <glib.h> -#include <assert.h> #include <string.h> static constexpr Domain xspf_domain("xspf"); @@ -57,7 +56,7 @@ struct XspfParser { * valid if state==TRACK. TAG_NUM_OF_ITEM_TYPES means there * is no (known) tag. */ - TagType tag; + TagType tag_type; /** * The current song. It is allocated after the "location" @@ -65,6 +64,8 @@ struct XspfParser { */ Song *song; + TagBuilder tag_builder; + XspfParser() :state(ROOT) {} }; @@ -95,7 +96,7 @@ xspf_start_element(gcc_unused GMarkupParseContext *context, if (strcmp(element_name, "track") == 0) { parser->state = XspfParser::TRACK; parser->song = nullptr; - parser->tag = TAG_NUM_OF_ITEM_TYPES; + parser->tag_type = TAG_NUM_OF_ITEM_TYPES; } break; @@ -104,17 +105,17 @@ xspf_start_element(gcc_unused GMarkupParseContext *context, if (strcmp(element_name, "location") == 0) parser->state = XspfParser::LOCATION; else if (strcmp(element_name, "title") == 0) - parser->tag = TAG_TITLE; + parser->tag_type = TAG_TITLE; else if (strcmp(element_name, "creator") == 0) /* TAG_COMPOSER would be more correct according to the XSPF spec */ - parser->tag = TAG_ARTIST; + parser->tag_type = TAG_ARTIST; else if (strcmp(element_name, "annotation") == 0) - parser->tag = TAG_COMMENT; + parser->tag_type = TAG_COMMENT; else if (strcmp(element_name, "album") == 0) - parser->tag = TAG_ALBUM; + parser->tag_type = TAG_ALBUM; else if (strcmp(element_name, "trackNum") == 0) - parser->tag = TAG_TRACK; + parser->tag_type = TAG_TRACK; break; @@ -148,12 +149,15 @@ xspf_end_element(gcc_unused GMarkupParseContext *context, case XspfParser::TRACK: if (strcmp(element_name, "track") == 0) { - if (parser->song != nullptr) + if (parser->song != nullptr) { + assert(parser->song->tag == nullptr); + parser->song->tag = parser->tag_builder.Commit(); parser->songs.emplace_front(parser->song); + } parser->state = XspfParser::TRACKLIST; } else - parser->tag = TAG_NUM_OF_ITEM_TYPES; + parser->tag_type = TAG_NUM_OF_ITEM_TYPES; break; @@ -178,11 +182,9 @@ xspf_text(gcc_unused GMarkupParseContext *context, case XspfParser::TRACK: if (parser->song != nullptr && - parser->tag != TAG_NUM_OF_ITEM_TYPES) { - if (parser->song->tag == nullptr) - parser->song->tag = new Tag(); - parser->song->tag->AddItem(parser->tag, text, text_len); - } + parser->tag_type != TAG_NUM_OF_ITEM_TYPES) + parser->tag_builder.AddItem(parser->tag_type, + text, text_len); break; diff --git a/src/system/Clock.cxx b/src/system/Clock.cxx index 347997a44..4d89b9b89 100644 --- a/src/system/Clock.cxx +++ b/src/system/Clock.cxx @@ -31,6 +31,28 @@ #endif unsigned +MonotonicClockS(void) +{ +#ifdef WIN32 + return GetTickCount() / 1000; +#elif defined(__APPLE__) /* OS X does not define CLOCK_MONOTONIC */ + static mach_timebase_info_data_t base; + if (base.denom == 0) + (void)mach_timebase_info(&base); + + return (unsigned)((mach_absolute_time() * base.numer / 1000) + / (1000000 * base.denom)); +#elif defined(CLOCK_MONOTONIC) + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec; +#else + /* we have no monotonic clock, fall back to time() */ + return time(nullptr); +#endif +} + +unsigned MonotonicClockMS(void) { #ifdef WIN32 @@ -96,3 +118,29 @@ MonotonicClockUS(void) #endif } +#ifdef WIN32 + +gcc_const +static unsigned +DeltaFileTimeS(FILETIME a, FILETIME b) +{ + ULARGE_INTEGER a2, b2; + b2.LowPart = b.dwLowDateTime; + b2.HighPart = b.dwHighDateTime; + a2.LowPart = a.dwLowDateTime; + a2.HighPart = a.dwHighDateTime; + return (a2.QuadPart - b2.QuadPart) / 10000000; +} + +unsigned +GetProcessUptimeS() +{ + FILETIME creation_time, exit_time, kernel_time, user_time, now; + GetProcessTimes(GetCurrentProcess(), &creation_time, + &exit_time, &kernel_time, &user_time); + GetSystemTimeAsFileTime(&now); + + return DeltaFileTimeS(now, creation_time); +} + +#endif diff --git a/src/system/Clock.hxx b/src/system/Clock.hxx index 7be1127bf..1c3651a99 100644 --- a/src/system/Clock.hxx +++ b/src/system/Clock.hxx @@ -25,6 +25,13 @@ #include <stdint.h> /** + * Returns the value of a monotonic clock in seconds. + */ +gcc_pure +unsigned +MonotonicClockS(); + +/** * Returns the value of a monotonic clock in milliseconds. */ gcc_pure @@ -38,4 +45,15 @@ gcc_pure uint64_t MonotonicClockUS(); +#ifdef WIN32 + +/** + * Returns the uptime of the current process in seconds. + */ +gcc_pure +unsigned +GetProcessUptimeS(); + +#endif + #endif diff --git a/src/system/EPollFD.hxx b/src/system/EPollFD.hxx index 41f7ec377..a6ea51d18 100644 --- a/src/system/EPollFD.hxx +++ b/src/system/EPollFD.hxx @@ -23,6 +23,7 @@ #include <assert.h> #include <sys/epoll.h> #include <unistd.h> +#include <stdint.h> #include "check.h" diff --git a/src/system/FatalError.cxx b/src/system/FatalError.cxx index f02b4b581..b54a677f2 100644 --- a/src/system/FatalError.cxx +++ b/src/system/FatalError.cxx @@ -23,12 +23,15 @@ #include "util/Domain.hxx" #include "LogV.hxx" +#ifdef HAVE_GLIB #include <glib.h> +#endif #include <unistd.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #ifdef WIN32 #include <windows.h> @@ -75,12 +78,16 @@ FatalError(const char *msg, const Error &error) FormatFatalError("%s: %s", msg, error.GetMessage()); } +#ifdef HAVE_GLIB + void FatalError(const char *msg, GError *error) { FormatFatalError("%s: %s", msg, error->message); } +#endif + void FatalSystemError(const char *msg) { @@ -88,7 +95,7 @@ FatalSystemError(const char *msg) #ifdef WIN32 system_error = g_win32_error_message(GetLastError()); #else - system_error = g_strerror(errno); + system_error = strerror(errno); #endif FormatError(fatal_error_domain, "%s: %s", msg, system_error); diff --git a/src/system/FatalError.hxx b/src/system/FatalError.hxx index 2845359ef..4ef290714 100644 --- a/src/system/FatalError.hxx +++ b/src/system/FatalError.hxx @@ -45,9 +45,11 @@ gcc_noreturn void FatalError(const char *msg, const Error &error); +#ifdef HAVE_GLIB gcc_noreturn void FatalError(const char *msg, GError *error); +#endif /** * Call this after a system call has failed that is not supposed to diff --git a/src/system/PeriodClock.hxx b/src/system/PeriodClock.hxx new file mode 100644 index 000000000..61f52933f --- /dev/null +++ b/src/system/PeriodClock.hxx @@ -0,0 +1,149 @@ +/* + * 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_PERIOD_CLOCK_HXX +#define MPD_PERIOD_CLOCK_HXX + +#include "Clock.hxx" + +/** + * This is a stopwatch which saves the timestamp of an event, and can + * check whether a specified time span has passed since then. + */ +class PeriodClock { +protected: + typedef unsigned Stamp; + +private: + Stamp last; + +public: + /** + * Initializes the object, setting the last time stamp to "0", + * i.e. a Check() will always succeed. If you do not want this + * default behaviour, call Update() immediately after creating the + * object. + */ + constexpr + PeriodClock():last(0) {} + +protected: + static Stamp GetNow() { + return MonotonicClockMS(); + } + + constexpr int Elapsed(Stamp now) const { + return last == 0 + ? -1 + : now - last; + } + + constexpr bool Check(Stamp now, unsigned duration) const { + return now >= last + duration; + } + + void Update(Stamp now) { + last = now; + } + +public: + bool IsDefined() const { + return last != 0; + } + + /** + * Resets the clock. + */ + void Reset() { + last = 0; + } + + /** + * Returns the number of milliseconds elapsed since the last + * update(). Returns -1 if update() was never called. + */ + int Elapsed() const { + return Elapsed(GetNow()); + } + + /** + * Combines a call to Elapsed() and Update(). + */ + int ElapsedUpdate() { + const auto now = GetNow(); + int result = Elapsed(now); + Update(now); + return result; + } + + /** + * Checks whether the specified duration has passed since the last + * update. + * + * @param duration the duration in milliseconds + */ + bool Check(unsigned duration) const { + return Check(GetNow(), duration); + } + + /** + * Updates the time stamp, setting it to the current clock. + */ + void Update() { + Update(GetNow()); + } + + /** + * Updates the time stamp, setting it to the current clock plus the + * specified offset. + */ + void UpdateWithOffset(int offset) { + Update(GetNow() + offset); + } + + /** + * Checks whether the specified duration has passed since the last + * update. If yes, it updates the time stamp. + * + * @param duration the duration in milliseconds + */ + bool CheckUpdate(unsigned duration) { + Stamp now = GetNow(); + if (Check(now, duration)) { + Update(now); + return true; + } else + return false; + } + + /** + * Checks whether the specified duration has passed since the last + * update. After that, it updates the time stamp. + * + * @param duration the duration in milliseconds + */ + bool CheckAlwaysUpdate(unsigned duration) { + Stamp now = GetNow(); + bool ret = Check(now, duration); + Update(now); + return ret; + } +}; + +#endif diff --git a/src/system/Resolver.cxx b/src/system/Resolver.cxx index 5e6ea590b..0cfb754ec 100644 --- a/src/system/Resolver.cxx +++ b/src/system/Resolver.cxx @@ -22,11 +22,12 @@ #include "util/Error.hxx" #include "util/Domain.hxx" -#include <glib.h> - #ifndef WIN32 #include <sys/socket.h> #include <netdb.h> +#ifdef HAVE_TCP +#include <netinet/in.h> +#endif #else #include <ws2tcpip.h> #include <winsock.h> @@ -41,17 +42,17 @@ const Domain resolver_domain("resolver"); -char * -sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error) +std::string +sockaddr_to_string(const struct sockaddr *sa, size_t length) { #ifdef HAVE_UN if (sa->sa_family == AF_UNIX) { /* return path of UNIX domain sockets */ const sockaddr_un &s_un = *(const sockaddr_un *)sa; if (length < sizeof(s_un) || s_un.sun_path[0] == 0) - return g_strdup("local"); + return "local"; - return g_strdup(s_un.sun_path); + return s_un.sun_path; } #endif @@ -80,17 +81,23 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error) ret = getnameinfo(sa, length, host, sizeof(host), serv, sizeof(serv), NI_NUMERICHOST|NI_NUMERICSERV); - if (ret != 0) { - error.Set(resolver_domain, ret, gai_strerror(ret)); - return NULL; - } + if (ret != 0) + return "unknown"; #ifdef HAVE_IPV6 - if (strchr(host, ':') != NULL) - return g_strconcat("[", host, "]:", serv, NULL); + if (strchr(host, ':') != NULL) { + std::string result("["); + result.append(host); + result.append("]:"); + result.append(serv); + return result; + } #endif - return g_strconcat(host, ":", serv, NULL); + std::string result(host); + result.push_back(':'); + result.append(serv); + return result; } struct addrinfo * @@ -98,19 +105,19 @@ resolve_host_port(const char *host_port, unsigned default_port, int flags, int socktype, Error &error) { - char *p = g_strdup(host_port); - const char *host = p, *port = NULL; + std::string p(host_port); + const char *host = p.c_str(), *port = nullptr; if (host_port[0] == '[') { /* IPv6 needs enclosing square braces, to differentiate between IP colons and the port separator */ - char *q = strchr(p + 1, ']'); - if (q != NULL && q[1] == ':' && q[2] != 0) { - *q = 0; + size_t q = p.find(']', 1); + if (q != p.npos && p[q + 1] == ':' && p[q + 2] != 0) { + p[q] = 0; + port = host + q + 2; ++host; - port = q + 2; } } @@ -118,10 +125,11 @@ resolve_host_port(const char *host_port, unsigned default_port, /* port is after the colon, but only if it's the only colon (don't split IPv6 addresses) */ - char *q = strchr(p, ':'); - if (q != NULL && q[1] != 0 && strchr(q + 1, ':') == NULL) { - *q = 0; - port = q + 1; + auto q = p.find(':'); + if (q != p.npos && p[q + 1] != 0 && + p.find(':', q + 1) == p.npos) { + p[q] = 0; + port = host + q + 1; } } @@ -142,7 +150,6 @@ resolve_host_port(const char *host_port, unsigned default_port, struct addrinfo *ai; int ret = getaddrinfo(host, port, &hints, &ai); - g_free(p); if (ret != 0) { error.Format(resolver_domain, ret, "Failed to look up '%s': %s", diff --git a/src/system/Resolver.hxx b/src/system/Resolver.hxx index 62ef455a1..044d3f96a 100644 --- a/src/system/Resolver.hxx +++ b/src/system/Resolver.hxx @@ -22,27 +22,27 @@ #include "Compiler.h" +#include <string> + #include <stddef.h> struct sockaddr; struct addrinfo; class Error; +class Domain; -extern const class Domain resolver_domain; +extern const Domain resolver_domain; /** * Converts the specified socket address into a string in the form - * "IP:PORT". The return value must be freed with g_free() when you - * don't need it anymore. + * "IP:PORT". * * @param sa the sockaddr struct * @param length the length of #sa in bytes - * @param error location to store the error occurring, or NULL to - * ignore errors */ -gcc_malloc -char * -sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error); +gcc_pure +std::string +sockaddr_to_string(const sockaddr *sa, size_t length); /** * Resolve a specification in the form "host", "host:port", @@ -54,7 +54,7 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error); * @return an #addrinfo linked list that must be freed with * freeaddrinfo(), or NULL on error */ -struct addrinfo * +addrinfo * resolve_host_port(const char *host_port, unsigned default_port, int flags, int socktype, Error &error); diff --git a/src/system/SignalFD.cxx b/src/system/SignalFD.cxx index b89775dcd..d5953056d 100644 --- a/src/system/SignalFD.cxx +++ b/src/system/SignalFD.cxx @@ -20,7 +20,6 @@ #include "config.h" #ifdef USE_SIGNALFD #include "SignalFD.hxx" -#include "fd_util.h" #include "FatalError.hxx" #include <assert.h> diff --git a/src/system/SocketError.cxx b/src/system/SocketError.cxx index 315a86e1f..bb1fa5abf 100644 --- a/src/system/SocketError.cxx +++ b/src/system/SocketError.cxx @@ -21,7 +21,7 @@ #include "SocketError.hxx" #include "util/Domain.hxx" -#include <glib.h> +#include <string.h> const Domain socket_domain("socket"); @@ -41,6 +41,6 @@ SocketErrorMessage::SocketErrorMessage(socket_error_t code) #else SocketErrorMessage::SocketErrorMessage(socket_error_t code) - :msg(g_strerror(code)) {} + :msg(strerror(code)) {} #endif diff --git a/src/system/SocketError.hxx b/src/system/SocketError.hxx index 22fbd2441..28e1bace0 100644 --- a/src/system/SocketError.hxx +++ b/src/system/SocketError.hxx @@ -21,7 +21,7 @@ #define MPD_SOCKET_ERROR_HXX #include "Compiler.h" -#include "util/Error.hxx" +#include "util/Error.hxx" // IWYU pragma: export #ifdef WIN32 #include <winsock2.h> @@ -31,11 +31,13 @@ typedef DWORD socket_error_t; typedef int socket_error_t; #endif +class Domain; + /** * A #Domain for #Error for socket I/O errors. The code is an errno * value (or WSAGetLastError() on Windows). */ -extern const class Domain socket_domain; +extern const Domain socket_domain; gcc_pure static inline socket_error_t diff --git a/src/system/SocketUtil.cxx b/src/system/SocketUtil.cxx index 9c4002386..ae98f33ff 100644 --- a/src/system/SocketUtil.cxx +++ b/src/system/SocketUtil.cxx @@ -22,8 +22,6 @@ #include "SocketError.hxx" #include "fd_util.h" -#include <glib.h> - #include <unistd.h> #ifndef WIN32 diff --git a/src/tag/Aiff.cxx b/src/tag/Aiff.cxx index 73e46e49f..84c386678 100644 --- a/src/tag/Aiff.cxx +++ b/src/tag/Aiff.cxx @@ -26,9 +26,7 @@ #include <limits> #include <stdint.h> -#include <sys/types.h> #include <sys/stat.h> -#include <unistd.h> #include <string.h> static constexpr Domain aiff_domain("aiff"); diff --git a/src/tag/ApeLoader.cxx b/src/tag/ApeLoader.cxx index 8251efe10..febe4467d 100644 --- a/src/tag/ApeLoader.cxx +++ b/src/tag/ApeLoader.cxx @@ -22,8 +22,6 @@ #include "system/ByteOrder.hxx" #include "fs/FileSystem.hxx" -#include <glib.h> - #include <stdint.h> #include <assert.h> #include <stdio.h> @@ -61,9 +59,9 @@ ape_scan_internal(FILE *fp, ApeTagCallback callback) remaining -= sizeof(footer); assert(remaining > 10); - char *buffer = (char *)g_malloc(remaining); + char *buffer = new char[remaining]; if (fread(buffer, 1, remaining, fp) != remaining) { - g_free(buffer); + delete[] buffer; return false; } @@ -98,7 +96,7 @@ ape_scan_internal(FILE *fp, ApeTagCallback callback) remaining -= size; } - g_free(buffer); + delete[] buffer; return true; } diff --git a/src/tag/ApeTag.hxx b/src/tag/ApeTag.hxx index e35edc381..d2c0365c7 100644 --- a/src/tag/ApeTag.hxx +++ b/src/tag/ApeTag.hxx @@ -34,6 +34,6 @@ extern const struct tag_table ape_tags[]; */ bool tag_ape_scan2(Path path_fs, - const struct tag_handler *handler, void *handler_ctx); + const tag_handler *handler, void *handler_ctx); #endif diff --git a/src/tag/Riff.cxx b/src/tag/Riff.cxx index ac162bc24..0f8f265fc 100644 --- a/src/tag/Riff.cxx +++ b/src/tag/Riff.cxx @@ -26,7 +26,6 @@ #include <limits> #include <stdint.h> -#include <sys/types.h> #include <sys/stat.h> #include <string.h> diff --git a/src/tag/Tag.cxx b/src/tag/Tag.cxx index 6bf070429..acfd24260 100644 --- a/src/tag/Tag.cxx +++ b/src/tag/Tag.cxx @@ -22,9 +22,9 @@ #include "TagPool.hxx" #include "TagString.hxx" #include "TagSettings.h" +#include "TagBuilder.hxx" #include "util/ASCII.hxx" -#include <glib.h> #include <assert.h> #include <string.h> @@ -58,12 +58,6 @@ tag_name_parse_i(const char *name) return TAG_NUM_OF_ITEM_TYPES; } -static size_t -items_size(const Tag &tag) -{ - return tag.num_items * sizeof(TagItem *); -} - void Tag::Clear() { @@ -75,28 +69,18 @@ Tag::Clear() tag_pool_put_item(items[i]); tag_pool_lock.unlock(); - g_free(items); + delete[] items; items = nullptr; num_items = 0; } -Tag::~Tag() -{ - tag_pool_lock.lock(); - for (int i = num_items; --i >= 0; ) - tag_pool_put_item(items[i]); - tag_pool_lock.unlock(); - - g_free(items); -} - Tag::Tag(const Tag &other) :time(other.time), has_playlist(other.has_playlist), items(nullptr), num_items(other.num_items) { if (num_items > 0) { - items = (TagItem **)g_malloc(items_size(other)); + items = new TagItem *[num_items]; tag_pool_lock.lock(); for (unsigned i = 0; i < num_items; i++) @@ -108,46 +92,9 @@ Tag::Tag(const Tag &other) Tag * Tag::Merge(const Tag &base, const Tag &add) { - unsigned n; - - /* allocate new tag object */ - - Tag *ret = new Tag(); - ret->time = add.time > 0 ? add.time : base.time; - ret->num_items = base.num_items + add.num_items; - ret->items = ret->num_items > 0 - ? (TagItem **)g_malloc(items_size(*ret)) - : nullptr; - - tag_pool_lock.lock(); - - /* copy all items from "add" */ - - for (unsigned i = 0; i < add.num_items; ++i) - ret->items[i] = tag_pool_dup_item(add.items[i]); - - n = add.num_items; - - /* copy additional items from "base" */ - - for (unsigned i = 0; i < base.num_items; ++i) - if (!add.HasType(base.items[i]->type)) - ret->items[n++] = tag_pool_dup_item(base.items[i]); - - tag_pool_lock.unlock(); - - assert(n <= ret->num_items); - - if (n < ret->num_items) { - /* some tags were not copied - shrink ret->items */ - assert(n > 0); - - ret->num_items = n; - ret->items = (TagItem **) - g_realloc(ret->items, items_size(*ret)); - } - - return ret; + TagBuilder builder(add); + builder.Complement(base); + return builder.Commit(); } Tag * @@ -183,43 +130,3 @@ Tag::HasType(TagType type) const { return GetValue(type) != nullptr; } - -void -Tag::AddItemInternal(TagType type, const char *value, size_t len) -{ - unsigned int i = num_items; - - char *p = FixTagString(value, len); - if (p != nullptr) { - value = p; - len = strlen(value); - } - - num_items++; - - items = (TagItem **)g_realloc(items, items_size(*this)); - - tag_pool_lock.lock(); - items[i] = tag_pool_get_item(type, value, len); - tag_pool_lock.unlock(); - - g_free(p); -} - -void -Tag::AddItem(TagType type, const char *value, size_t len) -{ - if (ignore_tag_items[type]) - return; - - if (value == nullptr || len == 0) - return; - - AddItemInternal(type, value, len); -} - -void -Tag::AddItem(TagType type, const char *value) -{ - AddItem(type, value, strlen(value)); -} diff --git a/src/tag/Tag.hxx b/src/tag/Tag.hxx index 5846e7a9d..480115c5b 100644 --- a/src/tag/Tag.hxx +++ b/src/tag/Tag.hxx @@ -20,8 +20,8 @@ #ifndef MPD_TAG_HXX #define MPD_TAG_HXX -#include "TagType.h" -#include "TagItem.hxx" +#include "TagType.h" // IWYU pragma: export +#include "TagItem.hxx" // IWYU pragma: export #include "Compiler.h" #include <algorithm> @@ -71,7 +71,9 @@ struct Tag { /** * Free the tag object and all its items. */ - ~Tag(); + ~Tag() { + Clear(); + } Tag &operator=(const Tag &other) = delete; @@ -104,24 +106,6 @@ struct Tag { void Clear(); /** - * Appends a new tag item. - * - * @param type the type of the new tag item - * @param value the value of the tag item (not null-terminated) - * @param len the length of #value - */ - void AddItem(TagType type, const char *value, size_t len); - - /** - * Appends a new tag item. - * - * @param tag the #tag object - * @param type the type of the new tag item - * @param value the value of the tag item (null-terminated) - */ - void AddItem(TagType type, const char *value); - - /** * Merges the data from two tags. If both tags share data for the * same TagType, only data from "add" is used. * @@ -152,9 +136,6 @@ struct Tag { */ gcc_pure bool HasType(TagType type) const; - -private: - void AddItemInternal(TagType type, const char *value, size_t len); }; /** diff --git a/src/tag/TagBuilder.cxx b/src/tag/TagBuilder.cxx index 25e5cc24b..37aa08cee 100644 --- a/src/tag/TagBuilder.cxx +++ b/src/tag/TagBuilder.cxx @@ -29,18 +29,86 @@ #include <assert.h> #include <string.h> -void -TagBuilder::Clear() +TagBuilder::TagBuilder(const Tag &other) + :time(other.time), has_playlist(other.has_playlist) { - time = -1; - has_playlist = false; + items.reserve(other.num_items); tag_pool_lock.lock(); + for (unsigned i = 0, n = other.num_items; i != n; ++i) + items.push_back(tag_pool_dup_item(other.items[i])); + tag_pool_lock.unlock(); +} + +TagBuilder::TagBuilder(Tag &&other) + :time(other.time), has_playlist(other.has_playlist) +{ + /* move all TagItem pointers from the Tag object; we don't + need to contact the tag pool, because all we do is move + references */ + items.reserve(other.num_items); + std::copy_n(other.items, other.num_items, std::back_inserter(items)); + + /* discard the pointers from the Tag object */ + other.num_items = 0; + delete[] other.items; + other.items = nullptr; +} + +TagBuilder & +TagBuilder::operator=(const TagBuilder &other) +{ + /* copy all attributes */ + time = other.time; + has_playlist = other.has_playlist; + items = other.items; + + /* increment the tag pool refcounters */ + tag_pool_lock.lock(); for (auto i : items) - tag_pool_put_item(i); + tag_pool_dup_item(i); tag_pool_lock.unlock(); + return *this; +} + +TagBuilder & +TagBuilder::operator=(TagBuilder &&other) +{ + time = other.time; + has_playlist = other.has_playlist; + items = std::move(other.items); + + return *this; +} + +TagBuilder & +TagBuilder::operator=(Tag &&other) +{ + time = other.time; + has_playlist = other.has_playlist; + + /* move all TagItem pointers from the Tag object; we don't + need to contact the tag pool, because all we do is move + references */ items.clear(); + items.reserve(other.num_items); + std::copy_n(other.items, other.num_items, std::back_inserter(items)); + + /* discard the pointers from the Tag object */ + other.num_items = 0; + delete[] other.items; + other.items = nullptr; + + return *this; +} + +void +TagBuilder::Clear() +{ + time = -1; + has_playlist = false; + RemoveAll(); } void @@ -57,7 +125,7 @@ TagBuilder::Commit(Tag &tag) object */ const unsigned n_items = items.size(); tag.num_items = n_items; - tag.items = g_new(TagItem *, n_items); + tag.items = new TagItem *[n_items]; std::copy_n(items.begin(), n_items, tag.items); items.clear(); @@ -74,6 +142,35 @@ TagBuilder::Commit() return tag; } +bool +TagBuilder::HasType(TagType type) const +{ + for (auto i : items) + if (i->type == type) + return true; + + return false; +} + +void +TagBuilder::Complement(const Tag &other) +{ + if (time <= 0) + time = other.time; + + has_playlist |= other.has_playlist; + + items.reserve(items.size() + other.num_items); + + tag_pool_lock.lock(); + for (unsigned i = 0, n = other.num_items; i != n; ++i) { + TagItem *item = other.items[i]; + if (!HasType(item->type)) + items.push_back(tag_pool_dup_item(item)); + } + tag_pool_lock.unlock(); +} + inline void TagBuilder::AddItemInternal(TagType type, const char *value, size_t length) { @@ -113,3 +210,29 @@ TagBuilder::AddItem(TagType type, const char *value) AddItem(type, value, strlen(value)); } + +void +TagBuilder::RemoveAll() +{ + tag_pool_lock.lock(); + for (auto i : items) + tag_pool_put_item(i); + tag_pool_lock.unlock(); + + items.clear(); +} + +void +TagBuilder::RemoveType(TagType type) +{ + const auto begin = items.begin(), end = items.end(); + + items.erase(std::remove_if(begin, end, + [type](TagItem *item) { + if (item->type != type) + return false; + tag_pool_put_item(item); + return true; + }), + end); +} diff --git a/src/tag/TagBuilder.hxx b/src/tag/TagBuilder.hxx index ffc60a1b2..fe647db08 100644 --- a/src/tag/TagBuilder.hxx +++ b/src/tag/TagBuilder.hxx @@ -63,7 +63,14 @@ public: } TagBuilder(const TagBuilder &other) = delete; - TagBuilder &operator=(const TagBuilder &other) = delete; + + explicit TagBuilder(const Tag &other); + explicit TagBuilder(Tag &&other); + + TagBuilder &operator=(const TagBuilder &other); + TagBuilder &operator=(TagBuilder &&other); + + TagBuilder &operator=(Tag &&other); /** * Returns true if the tag contains no items. This ignores the "time" @@ -109,6 +116,19 @@ public: } /** + * Checks whether the tag contains one or more items with + * the specified type. + */ + gcc_pure + bool HasType(TagType type) const; + + /** + * Copy attributes and items from the other object that do not + * exist in this object. + */ + void Complement(const Tag &other); + + /** * Appends a new tag item. * * @param type the type of the new tag item @@ -127,6 +147,16 @@ public: gcc_nonnull_all void AddItem(TagType type, const char *value); + /** + * Removes all tag items. + */ + void RemoveAll(); + + /** + * Removes all tag items of the specified type. + */ + void RemoveType(TagType type); + private: gcc_nonnull_all void AddItemInternal(TagType type, const char *value, size_t length); diff --git a/src/tag/TagConfig.cxx b/src/tag/TagConfig.cxx index 96fd1847f..0213ab4dc 100644 --- a/src/tag/TagConfig.cxx +++ b/src/tag/TagConfig.cxx @@ -30,8 +30,6 @@ #include <algorithm> -#include <string.h> - void TagLoadConfig() { diff --git a/src/tag/TagConfig.hxx b/src/tag/TagConfig.hxx index 5ec6766d4..3bc86cc40 100644 --- a/src/tag/TagConfig.hxx +++ b/src/tag/TagConfig.hxx @@ -20,8 +20,6 @@ #ifndef MPD_TAG_CONFIG_HXX #define MPD_TAG_CONFIG_HXX -#include "TagType.h" - void TagLoadConfig(); diff --git a/src/tag/TagId3.cxx b/src/tag/TagId3.cxx index df70a95e5..5e9432ce3 100644 --- a/src/tag/TagId3.cxx +++ b/src/tag/TagId3.cxx @@ -21,7 +21,6 @@ #include "TagId3.hxx" #include "TagHandler.hxx" #include "TagTable.hxx" -#include "Tag.hxx" #include "TagBuilder.hxx" #include "util/Error.hxx" #include "util/Domain.hxx" @@ -35,9 +34,10 @@ #include <glib.h> #include <id3tag.h> +#include <string> + #include <stdio.h> #include <stdlib.h> -#include <errno.h> #include <string.h> # ifndef ID3_FRAME_COMPOSER @@ -70,14 +70,11 @@ tag_is_id3v1(struct id3_tag *tag) static id3_utf8_t * tag_id3_getstring(const struct id3_frame *frame, unsigned i) { - union id3_field *field; - const id3_ucs4_t *ucs4; - - field = id3_frame_field(frame, i); + id3_field *field = id3_frame_field(frame, i); if (field == nullptr) return nullptr; - ucs4 = id3_field_getstring(field); + const id3_ucs4_t *ucs4 = id3_field_getstring(field); if (ucs4 == nullptr) return nullptr; @@ -89,17 +86,15 @@ tag_id3_getstring(const struct id3_frame *frame, unsigned i) static id3_utf8_t * import_id3_string(bool is_id3v1, const id3_ucs4_t *ucs4) { - id3_utf8_t *utf8, *utf8_stripped; - id3_latin1_t *isostr; - const char *encoding; + id3_utf8_t *utf8; /* use encoding field here? */ + const char *encoding; if (is_id3v1 && (encoding = config_get_string(CONF_ID3V1_ENCODING, nullptr)) != nullptr) { - isostr = id3_ucs4_latin1duplicate(ucs4); - if (G_UNLIKELY(!isostr)) { + id3_latin1_t *isostr = id3_ucs4_latin1duplicate(ucs4); + if (gcc_unlikely(isostr == nullptr)) return nullptr; - } utf8 = (id3_utf8_t *) g_convert_with_fallback((const char*)isostr, -1, @@ -110,19 +105,19 @@ import_id3_string(bool is_id3v1, const id3_ucs4_t *ucs4) FormatWarning(id3_domain, "Unable to convert %s string to UTF-8: '%s'", encoding, isostr); - g_free(isostr); + free(isostr); return nullptr; } - g_free(isostr); + free(isostr); } else { utf8 = id3_ucs4_utf8duplicate(ucs4); - if (G_UNLIKELY(!utf8)) { + if (gcc_unlikely(utf8 == nullptr)) return nullptr; - } } - utf8_stripped = (id3_utf8_t *)g_strdup(g_strstrip((gchar *)utf8)); - g_free(utf8); + id3_utf8_t *utf8_stripped = (id3_utf8_t *) + g_strdup(g_strstrip((gchar *)utf8)); + free(utf8); return utf8_stripped; } @@ -139,17 +134,12 @@ tag_id3_import_text_frame(struct id3_tag *tag, const struct id3_frame *frame, TagType type, const struct tag_handler *handler, void *handler_ctx) { - id3_ucs4_t const *ucs4; - id3_utf8_t *utf8; - union id3_field const *field; - unsigned int nstrings, i; - if (frame->nfields != 2) return; /* check the encoding field */ - field = id3_frame_field(frame, 0); + const id3_field *field = id3_frame_field(frame, 0); if (field == nullptr || field->type != ID3_FIELD_TYPE_TEXTENCODING) return; @@ -160,16 +150,16 @@ tag_id3_import_text_frame(struct id3_tag *tag, const struct id3_frame *frame, return; /* Get the number of strings available */ - nstrings = id3_field_getnstrings(field); - for (i = 0; i < nstrings; i++) { - ucs4 = id3_field_getstrings(field, i); + const unsigned nstrings = id3_field_getnstrings(field); + for (unsigned i = 0; i < nstrings; i++) { + const id3_ucs4_t *ucs4 = id3_field_getstrings(field, i); if (ucs4 == nullptr) continue; if (type == TAG_GENRE) ucs4 = id3_genre_name(ucs4); - utf8 = import_id3_string(tag_is_id3v1(tag), ucs4); + id3_utf8_t *utf8 = import_id3_string(tag_is_id3v1(tag), ucs4); if (utf8 == nullptr) continue; @@ -209,23 +199,19 @@ tag_id3_import_comment_frame(struct id3_tag *tag, const struct tag_handler *handler, void *handler_ctx) { - id3_ucs4_t const *ucs4; - id3_utf8_t *utf8; - union id3_field const *field; - if (frame->nfields != 4) return; /* for now I only read the 4th field, with the fullstring */ - field = id3_frame_field(frame, 3); + const id3_field *field = id3_frame_field(frame, 3); if (field == nullptr) return; - ucs4 = id3_field_getfullstring(field); + const id3_ucs4_t *ucs4 = id3_field_getfullstring(field); if (ucs4 == nullptr) return; - utf8 = import_id3_string(tag_is_id3v1(tag), ucs4); + id3_utf8_t *utf8 = import_id3_string(tag_is_id3v1(tag), ucs4); if (utf8 == nullptr) return; @@ -277,19 +263,15 @@ tag_id3_import_musicbrainz(struct id3_tag *id3_tag, void *handler_ctx) { for (unsigned i = 0;; ++i) { - const struct id3_frame *frame; - id3_utf8_t *name, *value; - TagType type; - - frame = id3_tag_findframe(id3_tag, "TXXX", i); + const id3_frame *frame = id3_tag_findframe(id3_tag, "TXXX", i); if (frame == nullptr) break; - name = tag_id3_getstring(frame, 1); + id3_utf8_t *name = tag_id3_getstring(frame, 1); if (name == nullptr) continue; - value = tag_id3_getstring(frame, 2); + id3_utf8_t *value = tag_id3_getstring(frame, 2); if (value == nullptr) continue; @@ -297,7 +279,7 @@ tag_id3_import_musicbrainz(struct id3_tag *id3_tag, (const char *)name, (const char *)value); - type = tag_id3_parse_txxx_name((const char*)name); + TagType type = tag_id3_parse_txxx_name((const char*)name); free(name); if (type != TAG_NUM_OF_ITEM_TYPES) @@ -316,21 +298,15 @@ tag_id3_import_ufid(struct id3_tag *id3_tag, const struct tag_handler *handler, void *handler_ctx) { for (unsigned i = 0;; ++i) { - const struct id3_frame *frame; - union id3_field *field; - const id3_latin1_t *name; - const id3_byte_t *value; - id3_length_t length; - - frame = id3_tag_findframe(id3_tag, "UFID", i); + const id3_frame *frame = id3_tag_findframe(id3_tag, "UFID", i); if (frame == nullptr) break; - field = id3_frame_field(frame, 0); + id3_field *field = id3_frame_field(frame, 0); if (field == nullptr) continue; - name = id3_field_getlatin1(field); + const id3_latin1_t *name = id3_field_getlatin1(field); if (name == nullptr || strcmp((const char *)name, "http://musicbrainz.org") != 0) continue; @@ -339,14 +315,15 @@ tag_id3_import_ufid(struct id3_tag *id3_tag, if (field == nullptr) continue; - value = id3_field_getbinarydata(field, &length); + id3_length_t length; + const id3_byte_t *value = + id3_field_getbinarydata(field, &length); if (value == nullptr || length == 0) continue; - char *p = g_strndup((const char *)value, length); + std::string p((const char *)value, length); tag_handler_invoke_tag(handler, handler_ctx, - TAG_MUSICBRAINZ_TRACKID, p); - g_free(p); + TAG_MUSICBRAINZ_TRACKID, p.c_str()); } } @@ -396,70 +373,54 @@ tag_id3_import(struct id3_tag *tag) : tag_builder.Commit(); } -static int +static size_t fill_buffer(void *buf, size_t size, FILE *stream, long offset, int whence) { if (fseek(stream, offset, whence) != 0) return 0; return fread(buf, 1, size, stream); } -static int +static long get_id3v2_footer_size(FILE *stream, long offset, int whence) { id3_byte_t buf[ID3_TAG_QUERYSIZE]; - int bufsize; - - bufsize = fill_buffer(buf, ID3_TAG_QUERYSIZE, stream, offset, whence); - if (bufsize <= 0) return 0; + size_t bufsize = fill_buffer(buf, ID3_TAG_QUERYSIZE, stream, offset, whence); + if (bufsize == 0) return 0; return id3_tag_query(buf, bufsize); } static struct id3_tag * tag_id3_read(FILE *stream, long offset, int whence) { - struct id3_tag *tag; - id3_byte_t query_buffer[ID3_TAG_QUERYSIZE]; - int tag_size; - int query_buffer_size; - /* It's ok if we get less than we asked for */ - query_buffer_size = fill_buffer(query_buffer, ID3_TAG_QUERYSIZE, - stream, offset, whence); + id3_byte_t query_buffer[ID3_TAG_QUERYSIZE]; + size_t query_buffer_size = fill_buffer(query_buffer, ID3_TAG_QUERYSIZE, + stream, offset, whence); if (query_buffer_size <= 0) return nullptr; /* Look for a tag header */ - tag_size = id3_tag_query(query_buffer, query_buffer_size); + long tag_size = id3_tag_query(query_buffer, query_buffer_size); if (tag_size <= 0) return nullptr; /* Found a tag. Allocate a buffer and read it in. */ - id3_byte_t *tag_buffer = (id3_byte_t *)g_malloc(tag_size); - if (!tag_buffer) - return nullptr; - + id3_byte_t *tag_buffer = new id3_byte_t[tag_size]; int tag_buffer_size = fill_buffer(tag_buffer, tag_size, stream, offset, whence); if (tag_buffer_size < tag_size) { - g_free(tag_buffer); + delete[] tag_buffer; return nullptr; } - tag = id3_tag_parse(tag_buffer, tag_buffer_size); - - g_free(tag_buffer); - + id3_tag *tag = id3_tag_parse(tag_buffer, tag_buffer_size); + delete[] tag_buffer; return tag; } static struct id3_tag * tag_id3_find_from_beginning(FILE *stream) { - struct id3_tag *tag; - struct id3_tag *seektag; - struct id3_frame *frame; - int seek; - - tag = tag_id3_read(stream, 0, SEEK_SET); + id3_tag *tag = tag_id3_read(stream, 0, SEEK_SET); if (!tag) { return nullptr; } else if (tag_is_id3v1(tag)) { @@ -469,14 +430,15 @@ tag_id3_find_from_beginning(FILE *stream) } /* We have an id3v2 tag, so let's look for SEEK frames */ + id3_frame *frame; while ((frame = id3_tag_findframe(tag, "SEEK", 0))) { /* Found a SEEK frame, get it's value */ - seek = id3_field_getint(id3_frame_field(frame, 0)); + int seek = id3_field_getint(id3_frame_field(frame, 0)); if (seek < 0) break; /* Get the tag specified by the SEEK frame */ - seektag = tag_id3_read(stream, seek, SEEK_CUR); + id3_tag *seektag = tag_id3_read(stream, seek, SEEK_CUR); if (!seektag || tag_is_id3v1(seektag)) break; @@ -491,20 +453,16 @@ tag_id3_find_from_beginning(FILE *stream) static struct id3_tag * tag_id3_find_from_end(FILE *stream) { - struct id3_tag *tag; - struct id3_tag *v1tag; - int tagsize; - /* Get an id3v1 tag from the end of file for later use */ - v1tag = tag_id3_read(stream, -128, SEEK_END); + id3_tag *v1tag = tag_id3_read(stream, -128, SEEK_END); /* Get the id3v2 tag size from the footer (located before v1tag) */ - tagsize = get_id3v2_footer_size(stream, (v1tag ? -128 : 0) - 10, SEEK_END); + int tagsize = get_id3v2_footer_size(stream, (v1tag ? -128 : 0) - 10, SEEK_END); if (tagsize >= 0) return v1tag; /* Get the tag which the footer belongs to */ - tag = tag_id3_read(stream, tagsize, SEEK_CUR); + id3_tag *tag = tag_id3_read(stream, tagsize, SEEK_CUR); if (!tag) return v1tag; @@ -527,16 +485,16 @@ tag_id3_riff_aiff_load(FILE *file) /* too large, don't allocate so much memory */ return nullptr; - id3_byte_t *buffer = (id3_byte_t *)g_malloc(size); + id3_byte_t *buffer = new id3_byte_t[size]; size_t ret = fread(buffer, size, 1, file); if (ret != 1) { LogWarning(id3_domain, "Failed to read RIFF chunk"); - g_free(buffer); + delete[] buffer; return nullptr; } struct id3_tag *tag = id3_tag_parse(buffer, size); - g_free(buffer); + delete[] buffer; return tag; } diff --git a/src/tag/TagId3.hxx b/src/tag/TagId3.hxx index 749166116..aa564e946 100644 --- a/src/tag/TagId3.hxx +++ b/src/tag/TagId3.hxx @@ -33,10 +33,10 @@ class Error; bool tag_id3_scan(Path path_fs, - const struct tag_handler *handler, void *handler_ctx); + const tag_handler *handler, void *handler_ctx); Tag * -tag_id3_import(struct id3_tag *); +tag_id3_import(id3_tag *); /** * Loads the ID3 tags from the file into a libid3tag object. The @@ -53,8 +53,8 @@ tag_id3_load(Path path_fs, Error &error); * */ void -scan_id3_tag(struct id3_tag *tag, - const struct tag_handler *handler, void *handler_ctx); +scan_id3_tag(id3_tag *tag, + const tag_handler *handler, void *handler_ctx); #else @@ -62,7 +62,7 @@ scan_id3_tag(struct id3_tag *tag, static inline bool tag_id3_scan(gcc_unused Path path_fs, - gcc_unused const struct tag_handler *handler, + gcc_unused const tag_handler *handler, gcc_unused void *handler_ctx) { return false; diff --git a/src/tag/TagRva2.cxx b/src/tag/TagRva2.cxx index 204001aa7..442695581 100644 --- a/src/tag/TagRva2.cxx +++ b/src/tag/TagRva2.cxx @@ -21,10 +21,10 @@ #include "TagRva2.hxx" #include "ReplayGainInfo.hxx" +#include <id3tag.h> + #include <stdint.h> #include <string.h> -#include <glib.h> -#include <id3tag.h> enum rva2_channel { CHANNEL_OTHER = 0x00, diff --git a/src/tag/TagRva2.hxx b/src/tag/TagRva2.hxx index 98154041a..a1d67a777 100644 --- a/src/tag/TagRva2.hxx +++ b/src/tag/TagRva2.hxx @@ -32,6 +32,6 @@ struct ReplayGainInfo; * @return true on success */ bool -tag_rva2_parse(struct id3_tag *tag, ReplayGainInfo &replay_gain_info); +tag_rva2_parse(id3_tag *tag, ReplayGainInfo &replay_gain_info); #endif diff --git a/src/util/growing_fifo.h b/src/util/Clamp.hxx index 723c3b3ff..3217ef9f7 100644 --- a/src/util/growing_fifo.h +++ b/src/util/Clamp.hxx @@ -1,6 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * http://www.musicpd.org + * Copyright (C) 2012 Max Kellermann <max@duempel.org> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -28,46 +27,23 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -/** \file - * - * Helper functions for our FIFO buffer library (fifo_buffer.h) that - * allows growing the buffer on demand. - * - * This library is not thread safe. - */ - -#ifndef MPD_GROWING_FIFO_H -#define MPD_GROWING_FIFO_H +#ifndef CLAMP_HPP +#define CLAMP_HPP -#include <stddef.h> - -struct fifo_buffer; - -/** - * Allocate a new #fifo_buffer with the default size. - */ -struct fifo_buffer * -growing_fifo_new(void); - -/** - * Prepares writing to the buffer, see fifo_buffer_write() for - * details. The difference is that this function will automatically - * grow the buffer if it is too small. - * - * The caller is responsible for limiting the capacity of the buffer. - * - * @param length the number of bytes that will be written - * @return a pointer to the end of the buffer (will not be NULL) - */ -void * -growing_fifo_write(struct fifo_buffer **buffer_p, size_t length); +#include "Compiler.h" /** - * A helper function that combines growing_fifo_write(), memcpy(), - * fifo_buffer_append(). + * Clamps the specified value in a range. Returns #min or #max if the + * value is outside. */ -void -growing_fifo_append(struct fifo_buffer **buffer_p, - const void *data, size_t length); +template<typename T> +static inline constexpr const T & +Clamp(const T &value, const T &min, const T &max) +{ + return gcc_unlikely(value < min) + ? min + : (gcc_unlikely(value > max) + ? max : value); +} #endif diff --git a/src/util/ConstBuffer.hxx b/src/util/ConstBuffer.hxx new file mode 100644 index 000000000..bd3c405e5 --- /dev/null +++ b/src/util/ConstBuffer.hxx @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2013 Max Kellermann <max@duempel.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef CONST_BUFFER_HPP +#define CONST_BUFFER_HPP + +#include "Compiler.h" + +#include <cstddef> + +#ifndef NDEBUG +#include <assert.h> +#endif + +/** + * A reference to a memory area that is read-only. + */ +template<typename T> +struct ConstBuffer { + typedef size_t size_type; + typedef const T *pointer_type; + typedef pointer_type const_pointer_type; + typedef pointer_type iterator; + typedef pointer_type const_iterator; + + pointer_type data; + size_type size; + + ConstBuffer() = default; + + constexpr ConstBuffer(std::nullptr_t):data(nullptr), size(0) {} + + constexpr ConstBuffer(pointer_type _data, size_type _size) + :data(_data), size(_size) {} + + constexpr static ConstBuffer Null() { + return ConstBuffer(nullptr, 0); + } + + /** + * Cast a ConstBuffer<void> to a ConstBuffer<T>. A "void" + * buffer records its size in bytes, and when casting to "T", + * the assertion below ensures that the size is a multiple of + * sizeof(T). + */ +#ifdef NDEBUG + constexpr +#endif + static ConstBuffer<T> FromVoid(ConstBuffer<void> other) { + static_assert(sizeof(T) > 0, "Empty base type"); +#ifndef NDEBUG + assert(other.size % sizeof(T) == 0); +#endif + return ConstBuffer<T>(pointer_type(other.data), + other.size / sizeof(T)); + } + + constexpr ConstBuffer<void> ToVoid() const { + static_assert(sizeof(T) > 0, "Empty base type"); + return ConstBuffer<void>(data, size * sizeof(T)); + } + + constexpr bool IsNull() const { + return data == nullptr; + } + + constexpr bool IsEmpty() const { + return size == 0; + } + + constexpr iterator begin() const { + return data; + } + + constexpr iterator end() const { + return data + size; + } + + constexpr const_iterator cbegin() const { + return data; + } + + constexpr const_iterator cend() const { + return data + size; + } + + constexpr operator ConstBuffer<void>() const { + return { data, size }; + } +}; + +#endif diff --git a/src/util/Domain.hxx b/src/util/Domain.hxx index bbdbf8371..6dce7b731 100644 --- a/src/util/Domain.hxx +++ b/src/util/Domain.hxx @@ -1,24 +1,34 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project - * http://www.musicpd.org + * Copyright (C) 2013 Max Kellermann <max@duempel.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. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: * - * 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. + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * 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. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef MPD_DOMAIN_HXX -#define MPD_DOMAIN_HXX +#ifndef DOMAIN_HXX +#define DOMAIN_HXX class Domain { const char *const name; diff --git a/src/util/DynamicFifoBuffer.hxx b/src/util/DynamicFifoBuffer.hxx new file mode 100644 index 000000000..df50328b4 --- /dev/null +++ b/src/util/DynamicFifoBuffer.hxx @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2003-2013 Max Kellermann <max@duempel.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FIFO_BUFFER_HPP +#define FIFO_BUFFER_HPP + +#include "WritableBuffer.hxx" + +#include <utility> +#include <algorithm> + +#include <assert.h> +#include <stddef.h> +#include <stdint.h> + +/** + * A first-in-first-out buffer: you can append data at the end, and + * read data from the beginning. This class automatically shifts the + * buffer as needed. It is not thread safe. + */ +template<typename T> +class DynamicFifoBuffer { +public: + typedef size_t size_type; + typedef WritableBuffer<T> Range; + typedef typename Range::pointer_type pointer_type; + typedef typename Range::const_pointer_type const_pointer_type; + +protected: + size_type head, tail, capacity; + T *data; + +public: + explicit DynamicFifoBuffer(size_type _capacity) + :head(0), tail(0), capacity(_capacity), + data(new T[capacity]) {} + ~DynamicFifoBuffer() { + delete[] data; + } + + DynamicFifoBuffer(const DynamicFifoBuffer &) = delete; + + size_type GetCapacity() { + return capacity; + } + + void Grow(size_type new_capacity) { + assert(new_capacity > capacity); + + T *new_data = new T[new_capacity]; + std::move(data + head, data + tail, new_data); + delete[] data; + data = new_data; + capacity = new_capacity; + tail -= head; + head = 0; + } + + void Clear() { + head = tail = 0; + } + + bool IsEmpty() const { + return head == tail; + } + + bool IsFull() const { + return head == 0 && tail == capacity; + } + + /** + * Prepares writing. Returns a buffer range which may be written. + * When you are finished, call append(). + */ + Range Write() { + Shift(); + return Range(data + tail, capacity - tail); + } + + /** + * Expands the tail of the buffer, after data has been written to + * the buffer returned by write(). + */ + void Append(size_type n) { + assert(tail <= capacity); + assert(n <= capacity); + assert(tail + n <= capacity); + + tail += n; + } + + void WantWrite(size_type n) { + if (tail + n <= capacity) + /* enough space after the tail */ + return; + + const size_type in_use = tail - head; + const size_type required_capacity = in_use + n; + if (capacity >= required_capacity) { + Shift(); + } else { + size_type new_capacity = capacity; + do { + new_capacity <<= 1; + } while (new_capacity < required_capacity); + + Grow(new_capacity); + } + } + + /** + * Write data to the bfufer, growing it as needed. Returns a + * writable pointer. + */ + pointer_type Write(size_type n) { + WantWrite(n); + return data + tail; + } + + /** + * Append data to the buffer, growing it as needed. + */ + void Append(const_pointer_type p, size_type n) { + std::copy_n(p, n, Write(n)); + Append(n); + } + + /** + * Return a buffer range which may be read. The buffer pointer is + * writable, to allow modifications while parsing. + */ + Range Read() { + return Range(data + head, tail - head); + } + + /** + * Marks a chunk as consumed. + */ + void Consume(size_type n) { + assert(tail <= capacity); + assert(head <= tail); + assert(n <= tail); + assert(head + n <= tail); + + head += n; + } + + size_type Read(pointer_type p, size_type n) { + auto range = Read(); + if (n > range.size) + n = range.size; + std::copy_n(range.data, n, p); + Consume(n); + return n; + } + +protected: + void Shift() { + if (head == 0) + return; + + assert(head <= capacity); + assert(tail <= capacity); + assert(tail >= head); + + std::move(data + head, data + tail, data); + + tail -= head; + head = 0; + } +}; + +#endif diff --git a/src/util/Error.cxx b/src/util/Error.cxx index 5675f4d81..649276b20 100644 --- a/src/util/Error.cxx +++ b/src/util/Error.cxx @@ -1,31 +1,44 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project - * http://www.musicpd.org + * Copyright (C) 2013 Max Kellermann <max@duempel.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. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: * - * 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. + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * 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. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" #include "Error.hxx" #include "Domain.hxx" +#ifdef WIN32 #include <glib.h> +#endif #include <errno.h> #include <stdarg.h> #include <stdio.h> +#include <string.h> const Domain errno_domain("errno"); @@ -70,7 +83,7 @@ Error::FormatPrefix(const char *fmt, ...) void Error::SetErrno(int e) { - Set(errno_domain, e, g_strerror(e)); + Set(errno_domain, e, strerror(e)); } void @@ -82,7 +95,7 @@ Error::SetErrno() void Error::SetErrno(int e, const char *prefix) { - Format(errno_domain, e, "%s: %s", prefix, g_strerror(e)); + Format(errno_domain, e, "%s: %s", prefix, strerror(e)); } void diff --git a/src/util/Error.hxx b/src/util/Error.hxx index ec8867c6c..898a8f1c1 100644 --- a/src/util/Error.hxx +++ b/src/util/Error.hxx @@ -1,30 +1,40 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project - * http://www.musicpd.org + * Copyright (C) 2013 Max Kellermann <max@duempel.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. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: * - * 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. + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * 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. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef MPD_ERROR_HXX -#define MPD_ERROR_HXX +#ifndef ERROR_HXX +#define ERROR_HXX #include "check.h" #include "Compiler.h" #include <string> -#include <algorithm> +#include <utility> #include <assert.h> diff --git a/src/util/FormatString.cxx b/src/util/FormatString.cxx index c13d0fb52..ea6d41860 100644 --- a/src/util/FormatString.cxx +++ b/src/util/FormatString.cxx @@ -19,10 +19,13 @@ #include "FormatString.hxx" -#include <string.h> #include <stdio.h> #include <stdlib.h> +#ifdef WIN32 +#include <string.h> +#endif + char * FormatNewV(const char *fmt, va_list args) { diff --git a/src/util/OptionDef.hxx b/src/util/OptionDef.hxx new file mode 100644 index 000000000..ca9e6083f --- /dev/null +++ b/src/util/OptionDef.hxx @@ -0,0 +1,63 @@ +/* + * 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_UTIL_OPTIONDEF_HXX +#define MPD_UTIL_OPTIONDEF_HXX + +/** + * Command line option definition. + */ +class OptionDef +{ + const char *long_option; + char short_option; + const char *desc; +public: + constexpr OptionDef(const char *_long_option, const char *_desc) + : long_option(_long_option), + short_option(0), + desc(_desc) { } + + constexpr OptionDef(const char *_long_option, + char _short_option, const char *_desc) + : long_option(_long_option), + short_option(_short_option), + desc(_desc) { } + + bool HasLongOption() const { return long_option != nullptr; } + bool HasShortOption() const { return short_option != 0; } + bool HasDescription() const { return desc != nullptr; } + + const char *GetLongOption() const { + assert(HasLongOption()); + return long_option; + } + + char GetShortOption() const { + assert(HasShortOption()); + return short_option; + } + + const char *GetDescription() const { + assert(HasDescription()); + return desc; + } +}; + +#endif diff --git a/src/util/OptionParser.cxx b/src/util/OptionParser.cxx new file mode 100644 index 000000000..b8addb9a2 --- /dev/null +++ b/src/util/OptionParser.cxx @@ -0,0 +1,59 @@ +/* + * 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 "OptionParser.hxx" +#include "OptionDef.hxx" + +#include <string.h> + +bool OptionParser::CheckOption(const OptionDef &opt) +{ + assert(option != nullptr); + + if (is_long) + return opt.HasLongOption() && + strcmp(option, opt.GetLongOption()) == 0; + + return opt.HasShortOption() && + option[0] == opt.GetShortOption() && + option[1] == '\0'; +} + +bool OptionParser::ParseNext() +{ + assert(HasEntries()); + char *arg = *argv; + ++argv; + --argc; + if (arg[0] == '-') { + if (arg[1] == '-') { + option = arg + 2; + is_long = true; + } + else { + option = arg + 1; + is_long = false; + } + option_raw = arg; + return true; + } + option = nullptr; + option_raw = nullptr; + return false; +} diff --git a/src/util/OptionParser.hxx b/src/util/OptionParser.hxx new file mode 100644 index 000000000..74091bc29 --- /dev/null +++ b/src/util/OptionParser.hxx @@ -0,0 +1,88 @@ +/* + * 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_UTIL_OPTIONPARSER_HXX +#define MPD_UTIL_OPTIONPARSER_HXX + +#include <assert.h> + +class OptionDef; + +/** + * Command line option parser. + */ +class OptionParser +{ + int argc; + char **argv; + char *option; + char *option_raw; + bool is_long; +public: + /** + * Constructs #OptionParser. + */ + OptionParser(int _argc, char **_argv) + : argc(_argc - 1), argv(_argv + 1), + option(nullptr), option_raw(nullptr), is_long(false) { } + + /** + * Checks if there are command line entries to process. + */ + bool HasEntries() const { return argc > 0; } + + /** + * Gets the last parsed option. + */ + char *GetOption() { + assert(option_raw != nullptr); + return option_raw; + } + + /** + * Checks if current option is a specified option. + */ + bool CheckOption(const OptionDef& opt); + + /** + * Checks if current option is a specified option + * or specified alternative option. + */ + bool CheckOption(const OptionDef& opt, const OptionDef &alt_opt) { + return CheckOption(opt) || CheckOption(alt_opt); + } + + /** + * Parses current command line entry. + * Returns true on success, false otherwise. + * Regardless of result, advances current position to the next + * command line entry. + */ + bool ParseNext(); + + /** + * Checks if specified string is a command line option. + */ + static bool IsOption(const char *s) { + assert(s != nullptr); + return s[0] == '-'; + } +}; + +#endif diff --git a/src/util/PeakBuffer.cxx b/src/util/PeakBuffer.cxx index a3659b8f4..5e86503e8 100644 --- a/src/util/PeakBuffer.cxx +++ b/src/util/PeakBuffer.cxx @@ -18,46 +18,39 @@ */ #include "PeakBuffer.hxx" -#include "HugeAllocator.hxx" -#include "fifo_buffer.h" +#include "DynamicFifoBuffer.hxx" #include <algorithm> #include <assert.h> -#include <stdint.h> #include <string.h> PeakBuffer::~PeakBuffer() { - if (normal_buffer != nullptr) - fifo_buffer_free(normal_buffer); - - if (peak_buffer != nullptr) - HugeFree(peak_buffer, peak_size); + delete normal_buffer; + delete peak_buffer; } bool PeakBuffer::IsEmpty() const { - return (normal_buffer == nullptr || - fifo_buffer_is_empty(normal_buffer)) && - (peak_buffer == nullptr || - fifo_buffer_is_empty(peak_buffer)); + return (normal_buffer == nullptr || normal_buffer->IsEmpty()) && + (peak_buffer == nullptr || peak_buffer->IsEmpty()); } -const void * -PeakBuffer::Read(size_t *length_r) const +WritableBuffer<void> +PeakBuffer::Read() const { if (normal_buffer != nullptr) { - const void *p = fifo_buffer_read(normal_buffer, length_r); - if (p != nullptr) - return p; + const auto p = normal_buffer->Read(); + if (!p.IsEmpty()) + return p.ToVoid(); } if (peak_buffer != nullptr) { - const void *p = fifo_buffer_read(peak_buffer, length_r); - if (p != nullptr) - return p; + const auto p = peak_buffer->Read(); + if (!p.IsEmpty()) + return p.ToVoid(); } return nullptr; @@ -66,15 +59,15 @@ PeakBuffer::Read(size_t *length_r) const void PeakBuffer::Consume(size_t length) { - if (normal_buffer != nullptr && !fifo_buffer_is_empty(normal_buffer)) { - fifo_buffer_consume(normal_buffer, length); + if (normal_buffer != nullptr && !normal_buffer->IsEmpty()) { + normal_buffer->Consume(length); return; } - if (peak_buffer != nullptr && !fifo_buffer_is_empty(peak_buffer)) { - fifo_buffer_consume(peak_buffer, length); - if (fifo_buffer_is_empty(peak_buffer)) { - HugeFree(peak_buffer, peak_size); + if (peak_buffer != nullptr && !peak_buffer->IsEmpty()) { + peak_buffer->Consume(length); + if (peak_buffer->IsEmpty()) { + delete peak_buffer; peak_buffer = nullptr; } @@ -83,7 +76,7 @@ PeakBuffer::Consume(size_t length) } static size_t -AppendTo(fifo_buffer *buffer, const void *data, size_t length) +AppendTo(DynamicFifoBuffer<uint8_t> &buffer, const void *data, size_t length) { assert(data != nullptr); assert(length > 0); @@ -91,14 +84,13 @@ AppendTo(fifo_buffer *buffer, const void *data, size_t length) size_t total = 0; do { - size_t max_length; - void *p = fifo_buffer_write(buffer, &max_length); - if (p == nullptr) + const auto p = buffer.Write(); + if (p.IsEmpty()) break; - const size_t nbytes = std::min(length, max_length); - memcpy(p, data, nbytes); - fifo_buffer_append(buffer, nbytes); + const size_t nbytes = std::min(length, p.size); + memcpy(p.data, data, nbytes); + buffer.Append(nbytes); data = (const uint8_t *)data + nbytes; length -= nbytes; @@ -114,15 +106,15 @@ PeakBuffer::Append(const void *data, size_t length) if (length == 0) return true; - if (peak_buffer != nullptr && !fifo_buffer_is_empty(peak_buffer)) { - size_t nbytes = AppendTo(peak_buffer, data, length); + if (peak_buffer != nullptr && !peak_buffer->IsEmpty()) { + size_t nbytes = AppendTo(*peak_buffer, data, length); return nbytes == length; } if (normal_buffer == nullptr) - normal_buffer = fifo_buffer_new(normal_size); + normal_buffer = new DynamicFifoBuffer<uint8_t>(normal_size); - size_t nbytes = AppendTo(normal_buffer, data, length); + size_t nbytes = AppendTo(*normal_buffer, data, length); if (nbytes > 0) { data = (const uint8_t *)data + nbytes; length -= nbytes; @@ -131,13 +123,11 @@ PeakBuffer::Append(const void *data, size_t length) } if (peak_buffer == nullptr && peak_size > 0) { - peak_buffer = (fifo_buffer *)HugeAllocate(peak_size); + peak_buffer = new DynamicFifoBuffer<uint8_t>(peak_size); if (peak_buffer == nullptr) return false; - - fifo_buffer_init(peak_buffer, peak_size); } - nbytes = AppendTo(peak_buffer, data, length); + nbytes = AppendTo(*peak_buffer, data, length); return nbytes == length; } diff --git a/src/util/PeakBuffer.hxx b/src/util/PeakBuffer.hxx index a3f385e3e..66f4c1a9f 100644 --- a/src/util/PeakBuffer.hxx +++ b/src/util/PeakBuffer.hxx @@ -20,11 +20,14 @@ #ifndef MPD_PEAK_BUFFER_HXX #define MPD_PEAK_BUFFER_HXX +#include "WritableBuffer.hxx" #include "Compiler.h" #include <stddef.h> +#include <stdint.h> -struct fifo_buffer; +template<typename T> struct WritableBuffer; +template<typename T> class DynamicFifoBuffer; /** * A FIFO-like buffer that will allocate more memory on demand to @@ -34,7 +37,7 @@ struct fifo_buffer; class PeakBuffer { size_t normal_size, peak_size; - fifo_buffer *normal_buffer, *peak_buffer; + DynamicFifoBuffer<uint8_t> *normal_buffer, *peak_buffer; public: PeakBuffer(size_t _normal_size, size_t _peak_size) @@ -57,7 +60,9 @@ public: gcc_pure bool IsEmpty() const; - const void *Read(size_t *length_r) const; + gcc_pure + WritableBuffer<void> Read() const; + void Consume(size_t length); bool Append(const void *data, size_t length); diff --git a/src/util/SplitString.cxx b/src/util/SplitString.cxx new file mode 100644 index 000000000..c8314f28f --- /dev/null +++ b/src/util/SplitString.cxx @@ -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. + */ + +#include "SplitString.hxx" + +#include <string.h> + +SplitString::SplitString(const char *s, char separator) + :first(nullptr) +{ + const char *x = strchr(s, separator); + if (x == nullptr) + return; + + size_t length = x - s; + second = x + 1; + + first = new char[length + 1]; + memcpy(first, s, length); + first[length] = 0; +} diff --git a/src/util/SplitString.hxx b/src/util/SplitString.hxx new file mode 100644 index 000000000..a8512b13c --- /dev/null +++ b/src/util/SplitString.hxx @@ -0,0 +1,71 @@ +/* + * 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_SPLIT_STRING_HXX +#define MPD_SPLIT_STRING_HXX + +#include "Compiler.h" + +#include <assert.h> + +/** + * Split a given constant string at a separator character. Duplicates + * the first part to be able to null-terminate it. + */ +class SplitString { + char *first; + const char *second; + +public: + SplitString(const char *s, char separator); + + ~SplitString() { + delete[] first; + } + + /** + * Was the separator found? + */ + bool IsDefined() const { + return first != nullptr; + } + + /** + * Is the first part empty? + */ + bool IsEmpty() const { + assert(IsDefined()); + + return *first == 0; + } + + const char *GetFirst() const { + assert(IsDefined()); + + return first; + } + + const char *GetSecond() const { + assert(IsDefined()); + + return second; + } +}; + +#endif diff --git a/src/util/StringUtil.cxx b/src/util/StringUtil.cxx index 7e295bf90..512a75f5c 100644 --- a/src/util/StringUtil.cxx +++ b/src/util/StringUtil.cxx @@ -22,6 +22,7 @@ #include "ASCII.hxx" #include <assert.h> +#include <string.h> const char * strchug_fast(const char *p) @@ -33,6 +34,13 @@ strchug_fast(const char *p) } bool +StringStartsWith(const char *haystack, const char *needle) +{ + const size_t length = strlen(needle); + return memcmp(haystack, needle, length) == 0; +} + +bool string_array_contains(const char *const* haystack, const char *needle) { assert(haystack != nullptr); diff --git a/src/util/StringUtil.hxx b/src/util/StringUtil.hxx index 1c67910a9..f25160107 100644 --- a/src/util/StringUtil.hxx +++ b/src/util/StringUtil.hxx @@ -40,6 +40,10 @@ strchug_fast(char *p) return const_cast<char *>(strchug_fast((const char *)p)); } +gcc_pure +bool +StringStartsWith(const char *haystack, const char *needle); + /** * Checks whether a string array contains the specified string. * diff --git a/src/util/Tokenizer.cxx b/src/util/Tokenizer.cxx index 1c8af23fd..a57f5b20c 100644 --- a/src/util/Tokenizer.cxx +++ b/src/util/Tokenizer.cxx @@ -24,11 +24,6 @@ #include "Error.hxx" #include "Domain.hxx" -#include <glib.h> - -#include <assert.h> -#include <string.h> - static constexpr Domain tokenizer_domain("tokenizer"); static inline bool diff --git a/src/util/UriUtil.cxx b/src/util/UriUtil.cxx index 7997bd5c9..89d2a473a 100644 --- a/src/util/UriUtil.cxx +++ b/src/util/UriUtil.cxx @@ -27,6 +27,16 @@ bool uri_has_scheme(const char *uri) return strstr(uri, "://") != nullptr; } +std::string +uri_get_scheme(const char *uri) +{ + const char *end = strstr(uri, "://"); + if (end == nullptr) + end = uri; + + return std::string(uri, end); +} + /* suffixes should be ascii only characters */ const char * uri_get_suffix(const char *uri) diff --git a/src/util/UriUtil.hxx b/src/util/UriUtil.hxx index 78d0a6bff..20e468103 100644 --- a/src/util/UriUtil.hxx +++ b/src/util/UriUtil.hxx @@ -31,6 +31,13 @@ gcc_pure bool uri_has_scheme(const char *uri); +/** + * Returns the scheme name of the specified URI, or an empty string. + */ +gcc_pure +std::string +uri_get_scheme(const char *uri); + gcc_pure const char * uri_get_suffix(const char *uri); diff --git a/src/util/WritableBuffer.hxx b/src/util/WritableBuffer.hxx index 4e529cfad..7267813f5 100644 --- a/src/util/WritableBuffer.hxx +++ b/src/util/WritableBuffer.hxx @@ -32,7 +32,11 @@ #include "Compiler.h" -#include <stddef.h> +#include <cstddef> + +#ifndef NDEBUG +#include <assert.h> +#endif /** * A reference to a memory area that is writable. @@ -41,47 +45,72 @@ */ template<typename T> struct WritableBuffer { - typedef size_t size_type; - typedef T *pointer_type; - typedef const T *const_pointer_type; - typedef pointer_type iterator; - typedef const_pointer_type const_iterator; + typedef size_t size_type; + typedef T *pointer_type; + typedef const T *const_pointer_type; + typedef pointer_type iterator; + typedef const_pointer_type const_iterator; + + pointer_type data; + size_type size; + + WritableBuffer() = default; - pointer_type data; - size_type size; + constexpr WritableBuffer(std::nullptr_t):data(nullptr), size(0) {} - WritableBuffer() = default; + constexpr WritableBuffer(pointer_type _data, size_type _size) + :data(_data), size(_size) {} - constexpr WritableBuffer(pointer_type _data, size_type _size) - :data(_data), size(_size) {} + constexpr static WritableBuffer Null() { + return { nullptr, 0 }; + } + + /** + * Cast a WritableBuffer<void> to a WritableBuffer<T>. A "void" + * buffer records its size in bytes, and when casting to "T", + * the assertion below ensures that the size is a multiple of + * sizeof(T). + */ +#ifdef NDEBUG + constexpr +#endif + static WritableBuffer<T> FromVoid(WritableBuffer<void> other) { + static_assert(sizeof(T) > 0, "Empty base type"); +#ifndef NDEBUG + assert(other.size % sizeof(T) == 0); +#endif + return WritableBuffer<T>(pointer_type(other.data), + other.size / sizeof(T)); + } - constexpr static WritableBuffer Null() { - return { nullptr, 0 }; - } + constexpr WritableBuffer<void> ToVoid() const { + static_assert(sizeof(T) > 0, "Empty base type"); + return WritableBuffer<void>(data, size * sizeof(T)); + } - constexpr bool IsNull() const { - return data == nullptr; - } + constexpr bool IsNull() const { + return data == nullptr; + } - constexpr bool IsEmpty() const { - return size == 0; - } + constexpr bool IsEmpty() const { + return size == 0; + } - constexpr iterator begin() const { - return data; - } + constexpr iterator begin() const { + return data; + } - constexpr iterator end() const { - return data + size; - } + constexpr iterator end() const { + return data + size; + } - constexpr const_iterator cbegin() const { - return data; - } + constexpr const_iterator cbegin() const { + return data; + } - constexpr const_iterator cend() const { - return data + size; - } + constexpr const_iterator cend() const { + return data + size; + } }; #endif diff --git a/src/util/fifo_buffer.c b/src/util/fifo_buffer.c deleted file mode 100644 index 162ddf946..000000000 --- a/src/util/fifo_buffer.c +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * http://www.musicpd.org - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "config.h" -#include "fifo_buffer.h" - -#include <glib.h> - -#include <assert.h> -#include <string.h> - -struct fifo_buffer { - size_t size, start, end; - unsigned char buffer[sizeof(size_t)]; -}; - -struct fifo_buffer * -fifo_buffer_new(size_t size) -{ - struct fifo_buffer *buffer; - - assert(size > 0); - - buffer = (struct fifo_buffer *)g_malloc(sizeof(*buffer) - - sizeof(buffer->buffer) + size); - - buffer->size = size; - buffer->start = 0; - buffer->end = 0; - - return buffer; -} - -void -fifo_buffer_init(struct fifo_buffer *buffer, size_t size) -{ - buffer->size = size - (sizeof(*buffer) - sizeof(buffer->buffer)); - buffer->start = 0; - buffer->end = 0; -} - -static void -fifo_buffer_move(struct fifo_buffer *buffer); - -struct fifo_buffer * -fifo_buffer_realloc(struct fifo_buffer *buffer, size_t new_size) -{ - if (buffer == NULL) - return new_size > 0 - ? fifo_buffer_new(new_size) - : NULL; - - /* existing data must fit in new size */ - assert(new_size >= buffer->end - buffer->start); - - if (new_size == 0) { - fifo_buffer_free(buffer); - return NULL; - } - - /* compress the buffer when we're shrinking and the tail of - the buffer would exceed the new size */ - if (buffer->end > new_size) - fifo_buffer_move(buffer); - - /* existing data must fit in new size: second check */ - assert(buffer->end <= new_size); - - buffer = g_realloc(buffer, sizeof(*buffer) - sizeof(buffer->buffer) + - new_size); - buffer->size = new_size; - return buffer; -} - -void -fifo_buffer_free(struct fifo_buffer *buffer) -{ - assert(buffer != NULL); - - g_free(buffer); -} - -size_t -fifo_buffer_capacity(const struct fifo_buffer *buffer) -{ - assert(buffer != NULL); - - return buffer->size; -} - -size_t -fifo_buffer_available(const struct fifo_buffer *buffer) -{ - assert(buffer != NULL); - - return buffer->end - buffer->start; -} - -void -fifo_buffer_clear(struct fifo_buffer *buffer) -{ - assert(buffer != NULL); - - buffer->start = 0; - buffer->end = 0; -} - -const void * -fifo_buffer_read(const struct fifo_buffer *buffer, size_t *length_r) -{ - assert(buffer != NULL); - assert(buffer->end >= buffer->start); - assert(length_r != NULL); - - if (buffer->start == buffer->end) - /* the buffer is empty */ - return NULL; - - *length_r = buffer->end - buffer->start; - return buffer->buffer + buffer->start; -} - -void -fifo_buffer_consume(struct fifo_buffer *buffer, size_t length) -{ - assert(buffer != NULL); - assert(buffer->end >= buffer->start); - assert(buffer->start + length <= buffer->end); - - buffer->start += length; -} - -/** - * Move data to the beginning of the buffer, to make room at the end. - */ -static void -fifo_buffer_move(struct fifo_buffer *buffer) -{ - if (buffer->start == 0) - return; - - if (buffer->end > buffer->start) - memmove(buffer->buffer, - buffer->buffer + buffer->start, - buffer->end - buffer->start); - - buffer->end -= buffer->start; - buffer->start = 0; -} - -void * -fifo_buffer_write(struct fifo_buffer *buffer, size_t *max_length_r) -{ - assert(buffer != NULL); - assert(buffer->end <= buffer->size); - assert(max_length_r != NULL); - - if (buffer->end == buffer->size) { - fifo_buffer_move(buffer); - if (buffer->end == buffer->size) - return NULL; - } else if (buffer->start > 0 && buffer->start == buffer->end) { - buffer->start = 0; - buffer->end = 0; - } - - *max_length_r = buffer->size - buffer->end; - return buffer->buffer + buffer->end; -} - -void -fifo_buffer_append(struct fifo_buffer *buffer, size_t length) -{ - assert(buffer != NULL); - assert(buffer->end >= buffer->start); - assert(buffer->end + length <= buffer->size); - - buffer->end += length; -} - -bool -fifo_buffer_is_empty(struct fifo_buffer *buffer) -{ - return buffer->start == buffer->end; -} - -bool -fifo_buffer_is_full(struct fifo_buffer *buffer) -{ - return buffer->start == 0 && buffer->end == buffer->size; -} diff --git a/src/util/fifo_buffer.h b/src/util/fifo_buffer.h deleted file mode 100644 index ccea97d86..000000000 --- a/src/util/fifo_buffer.h +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * http://www.musicpd.org - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/** \file - * - * This is a general purpose FIFO buffer library. You may append data - * at the end, while another instance reads data from the beginning. - * It is optimized for zero-copy usage: you get pointers to the real - * buffer, where you may operate on. - * - * This library is not thread safe. - */ - -#ifndef MPD_FIFO_BUFFER_H -#define MPD_FIFO_BUFFER_H - -#include <stdbool.h> -#include <stddef.h> - -struct fifo_buffer; - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Creates a new #fifo_buffer object. Free this object with - * fifo_buffer_free(). - * - * @param size the size of the buffer in bytes - * @return the new #fifo_buffer object - */ -struct fifo_buffer * -fifo_buffer_new(size_t size); - -void -fifo_buffer_init(struct fifo_buffer *buffer, size_t size); - -/** - * Change the capacity of the #fifo_buffer, while preserving existing - * data. - * - * @param buffer the old buffer, may be NULL - * @param new_size the requested new size of the #fifo_buffer; must - * not be smaller than the data which is stored in the old buffer - * @return the new buffer, may be NULL if the requested new size is 0 - */ -struct fifo_buffer * -fifo_buffer_realloc(struct fifo_buffer *buffer, size_t new_size); - -/** - * Frees the resources consumed by this #fifo_buffer object. - */ -void -fifo_buffer_free(struct fifo_buffer *buffer); - -/** - * Return the capacity of the buffer, i.e. the size that was passed to - * fifo_buffer_new(). - */ -size_t -fifo_buffer_capacity(const struct fifo_buffer *buffer); - -/** - * Return the number of bytes currently stored in the buffer. - */ -size_t -fifo_buffer_available(const struct fifo_buffer *buffer); - -/** - * Clears all data currently in this #fifo_buffer object. This does - * not overwrite the actuall buffer; it just resets the internal - * pointers. - */ -void -fifo_buffer_clear(struct fifo_buffer *buffer); - -/** - * Reads from the beginning of the buffer. To remove consumed data - * from the buffer, call fifo_buffer_consume(). - * - * @param buffer the #fifo_buffer object - * @param length_r the maximum amount to read is returned here - * @return a pointer to the beginning of the buffer, or NULL if the - * buffer is empty - */ -const void * -fifo_buffer_read(const struct fifo_buffer *buffer, size_t *length_r); - -/** - * Marks data at the beginning of the buffer as "consumed". - * - * @param buffer the #fifo_buffer object - * @param length the number of bytes which were consumed - */ -void -fifo_buffer_consume(struct fifo_buffer *buffer, size_t length); - -/** - * Prepares writing to the buffer. This returns a buffer which you - * can write to. To commit the write operation, call - * fifo_buffer_append(). - * - * @param buffer the #fifo_buffer object - * @param max_length_r the maximum amount to write is returned here - * @return a pointer to the end of the buffer, or NULL if the buffer - * is already full - */ -void * -fifo_buffer_write(struct fifo_buffer *buffer, size_t *max_length_r); - -/** - * Commits the write operation initiated by fifo_buffer_write(). - * - * @param buffer the #fifo_buffer object - * @param length the number of bytes which were written - */ -void -fifo_buffer_append(struct fifo_buffer *buffer, size_t length); - -/** - * Checks if the buffer is empty. - */ -bool -fifo_buffer_is_empty(struct fifo_buffer *buffer); - -/** - * Checks if the buffer is full. - */ -bool -fifo_buffer_is_full(struct fifo_buffer *buffer); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/util/growing_fifo.c b/src/util/growing_fifo.c deleted file mode 100644 index 88431f60e..000000000 --- a/src/util/growing_fifo.c +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * http://www.musicpd.org - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "growing_fifo.h" -#include "fifo_buffer.h" - -#include <assert.h> -#include <string.h> - -/** - * Align buffer sizes at 8 kB boundaries. Must be a power of two. - */ -static const size_t GROWING_FIFO_ALIGN = 8192; - -/** - * Align the specified size to the next #GROWING_FIFO_ALIGN boundary. - */ -static size_t -align(size_t size) -{ - return ((size - 1) | (GROWING_FIFO_ALIGN - 1)) + 1; -} - -struct fifo_buffer * -growing_fifo_new(void) -{ - return fifo_buffer_new(GROWING_FIFO_ALIGN); -} - -void * -growing_fifo_write(struct fifo_buffer **buffer_p, size_t length) -{ - assert(buffer_p != NULL); - - struct fifo_buffer *buffer = *buffer_p; - assert(buffer != NULL); - - size_t max_length; - void *p = fifo_buffer_write(buffer, &max_length); - if (p != NULL && max_length >= length) - return p; - - /* grow */ - size_t new_size = fifo_buffer_available(buffer) + length; - assert(new_size > fifo_buffer_capacity(buffer)); - *buffer_p = buffer = fifo_buffer_realloc(buffer, align(new_size)); - - /* try again */ - p = fifo_buffer_write(buffer, &max_length); - assert(p != NULL); - assert(max_length >= length); - - return p; -} - -void -growing_fifo_append(struct fifo_buffer **buffer_p, - const void *data, size_t length) -{ - void *p = growing_fifo_write(buffer_p, length); - memcpy(p, data, length); - fifo_buffer_append(*buffer_p, length); -} diff --git a/test/DumpDatabase.cxx b/test/DumpDatabase.cxx index 21a12d294..ec7de7e45 100644 --- a/test/DumpDatabase.cxx +++ b/test/DumpDatabase.cxx @@ -39,16 +39,6 @@ using std::endl; #include <stdlib.h> -static void -my_log_func(const gchar *log_domain, gcc_unused GLogLevelFlags log_level, - const gchar *message, gcc_unused gpointer user_data) -{ - if (log_domain != NULL) - g_printerr("%s: %s\n", log_domain, message); - else - g_printerr("%s\n", message); -} - static bool DumpDirectory(const Directory &directory, Error &) { @@ -94,8 +84,6 @@ main(int argc, char **argv) g_thread_init(nullptr); #endif - g_log_set_default_handler(my_log_func, nullptr); - /* initialize MPD */ config_global_init(); diff --git a/test/FakeDecoderAPI.cxx b/test/FakeDecoderAPI.cxx new file mode 100644 index 000000000..3045722cf --- /dev/null +++ b/test/FakeDecoderAPI.cxx @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2003-2012 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 "DecoderAPI.hxx" +#include "InputStream.hxx" +#include "util/Error.hxx" +#include "Compiler.h" + +#include <glib.h> + +#include <unistd.h> + +void +decoder_initialized(gcc_unused Decoder &decoder, + gcc_unused const AudioFormat audio_format, + gcc_unused bool seekable, + gcc_unused float total_time) +{ +} + +DecoderCommand +decoder_get_command(gcc_unused Decoder &decoder) +{ + return DecoderCommand::NONE; +} + +void +decoder_command_finished(gcc_unused Decoder &decoder) +{ +} + +double +decoder_seek_where(gcc_unused Decoder &decoder) +{ + return 1.0; +} + +void +decoder_seek_error(gcc_unused Decoder &decoder) +{ +} + +size_t +decoder_read(gcc_unused Decoder *decoder, + InputStream &is, + void *buffer, size_t length) +{ + return is.LockRead(buffer, length, IgnoreError()); +} + +bool +decoder_read_full(Decoder *decoder, InputStream &is, + void *_buffer, size_t size) +{ + uint8_t *buffer = (uint8_t *)_buffer; + + while (size > 0) { + size_t nbytes = decoder_read(decoder, is, buffer, size); + if (nbytes == 0) + return false; + + buffer += nbytes; + size -= nbytes; + } + + return true; +} + +bool +decoder_skip(Decoder *decoder, InputStream &is, size_t size) +{ + while (size > 0) { + char buffer[1024]; + size_t nbytes = decoder_read(decoder, is, buffer, + std::min(sizeof(buffer), size)); + if (nbytes == 0) + return false; + + size -= nbytes; + } + + return true; +} + +void +decoder_timestamp(gcc_unused Decoder &decoder, + gcc_unused double t) +{ +} + +DecoderCommand +decoder_data(gcc_unused Decoder &decoder, + gcc_unused InputStream *is, + const void *data, size_t datalen, + gcc_unused uint16_t kbit_rate) +{ + gcc_unused ssize_t nbytes = write(1, data, datalen); + return DecoderCommand::NONE; +} + +DecoderCommand +decoder_tag(gcc_unused Decoder &decoder, + gcc_unused InputStream *is, + gcc_unused Tag &&tag) +{ + return DecoderCommand::NONE; +} + +void +decoder_replay_gain(gcc_unused Decoder &decoder, + const ReplayGainInfo *rgi) +{ + const ReplayGainTuple *tuple = &rgi->tuples[REPLAY_GAIN_ALBUM]; + if (tuple->IsDefined()) + fprintf(stderr, "replay_gain[album]: gain=%f peak=%f\n", + tuple->gain, tuple->peak); + + tuple = &rgi->tuples[REPLAY_GAIN_TRACK]; + if (tuple->IsDefined()) + fprintf(stderr, "replay_gain[track]: gain=%f peak=%f\n", + tuple->gain, tuple->peak); +} + +void +decoder_mixramp(gcc_unused Decoder &decoder, gcc_unused MixRampInfo &&mix_ramp) +{ +} diff --git a/test/dump_playlist.cxx b/test/dump_playlist.cxx index d11562930..f0401249e 100644 --- a/test/dump_playlist.cxx +++ b/test/dump_playlist.cxx @@ -24,7 +24,6 @@ #include "Directory.hxx" #include "InputStream.hxx" #include "ConfigGlobal.hxx" -#include "DecoderAPI.hxx" #include "DecoderList.hxx" #include "InputInit.hxx" #include "IOThread.hxx" @@ -43,98 +42,6 @@ Directory::Directory() {} Directory::~Directory() {} -static void -my_log_func(const gchar *log_domain, gcc_unused GLogLevelFlags log_level, - const gchar *message, gcc_unused gpointer user_data) -{ - if (log_domain != NULL) - g_printerr("%s: %s\n", log_domain, message); - else - g_printerr("%s\n", message); -} - -void -decoder_initialized(gcc_unused Decoder &decoder, - gcc_unused const AudioFormat audio_format, - gcc_unused bool seekable, - gcc_unused float total_time) -{ -} - -DecoderCommand -decoder_get_command(gcc_unused Decoder &decoder) -{ - return DecoderCommand::NONE; -} - -void -decoder_command_finished(gcc_unused Decoder &decoder) -{ -} - -double -decoder_seek_where(gcc_unused Decoder &decoder) -{ - return 1.0; -} - -void -decoder_seek_error(gcc_unused Decoder &decoder) -{ -} - -size_t -decoder_read(gcc_unused Decoder *decoder, - InputStream &is, - void *buffer, size_t length) -{ - return is.LockRead(buffer, length, IgnoreError()); -} - -void -decoder_timestamp(gcc_unused Decoder &decoder, - gcc_unused double t) -{ -} - -DecoderCommand -decoder_data(gcc_unused Decoder &decoder, - gcc_unused InputStream *is, - const void *data, size_t datalen, - gcc_unused uint16_t kbit_rate) -{ - gcc_unused ssize_t nbytes = write(1, data, datalen); - return DecoderCommand::NONE; -} - -DecoderCommand -decoder_tag(gcc_unused Decoder &decoder, - gcc_unused InputStream *is, - gcc_unused Tag &&tag) -{ - return DecoderCommand::NONE; -} - -void -decoder_replay_gain(gcc_unused Decoder &decoder, - const ReplayGainInfo *rgi) -{ - const ReplayGainTuple *tuple = &rgi->tuples[REPLAY_GAIN_ALBUM]; - if (tuple->IsDefined()) - g_printerr("replay_gain[album]: gain=%f peak=%f\n", - tuple->gain, tuple->peak); - - tuple = &rgi->tuples[REPLAY_GAIN_TRACK]; - if (tuple->IsDefined()) - g_printerr("replay_gain[track]: gain=%f peak=%f\n", - tuple->gain, tuple->peak); -} - -void -decoder_mixramp(gcc_unused Decoder &decoder, gcc_unused MixRampInfo &&mix_ramp) -{ -} - int main(int argc, char **argv) { const char *uri; @@ -142,8 +49,8 @@ int main(int argc, char **argv) Song *song; if (argc != 3) { - g_printerr("Usage: dump_playlist CONFIG URI\n"); - return 1; + fprintf(stderr, "Usage: dump_playlist CONFIG URI\n"); + return EXIT_FAILURE; } const Path config_path = Path::FromFS(argv[1]); @@ -155,16 +62,14 @@ int main(int argc, char **argv) g_thread_init(NULL); #endif - g_log_set_default_handler(my_log_func, NULL); - /* initialize MPD */ config_global_init(); Error error; if (!ReadConfigFile(config_path, error)) { - g_printerr("%s\n", error.GetMessage()); - return 1; + LogError(error); + return EXIT_FAILURE; } io_thread_init(); @@ -172,7 +77,7 @@ int main(int argc, char **argv) if (!input_stream_global_init(error)) { LogError(error); - return 2; + return EXIT_FAILURE; } playlist_list_global_init(); @@ -192,7 +97,8 @@ int main(int argc, char **argv) if (error.IsDefined()) LogError(error); else - g_printerr("InputStream::Open() failed\n"); + fprintf(stderr, + "InputStream::Open() failed\n"); return 2; } @@ -203,7 +109,7 @@ int main(int argc, char **argv) playlist = playlist_list_open_stream(*is, uri); if (playlist == NULL) { is->Close(); - g_printerr("Failed to open playlist\n"); + fprintf(stderr, "Failed to open playlist\n"); return 2; } } @@ -211,18 +117,18 @@ int main(int argc, char **argv) /* dump the playlist */ while ((song = playlist->NextSong()) != NULL) { - g_print("%s\n", song->uri); + printf("%s\n", song->uri); if (song->end_ms > 0) - g_print("range: %u:%02u..%u:%02u\n", - song->start_ms / 60000, - (song->start_ms / 1000) % 60, - song->end_ms / 60000, - (song->end_ms / 1000) % 60); + printf("range: %u:%02u..%u:%02u\n", + song->start_ms / 60000, + (song->start_ms / 1000) % 60, + song->end_ms / 60000, + (song->end_ms / 1000) % 60); else if (song->start_ms > 0) - g_print("range: %u:%02u..\n", - song->start_ms / 60000, - (song->start_ms / 1000) % 60); + printf("range: %u:%02u..\n", + song->start_ms / 60000, + (song->start_ms / 1000) % 60); if (song->tag != NULL) tag_save(stdout, *song->tag); diff --git a/test/dump_rva2.cxx b/test/dump_rva2.cxx index e1ba5336a..c1683c895 100644 --- a/test/dump_rva2.cxx +++ b/test/dump_rva2.cxx @@ -24,11 +24,10 @@ #include "ConfigGlobal.hxx" #include "util/Error.hxx" #include "fs/Path.hxx" +#include "Log.hxx" #include <id3tag.h> -#include <glib.h> - #ifdef HAVE_LOCALE_H #include <locale.h> #endif @@ -50,8 +49,8 @@ int main(int argc, char **argv) #endif if (argc != 2) { - g_printerr("Usage: read_rva2 FILE\n"); - return 1; + fprintf(stderr, "Usage: read_rva2 FILE\n"); + return EXIT_FAILURE; } const char *path = argv[1]; @@ -60,9 +59,9 @@ int main(int argc, char **argv) struct id3_tag *tag = tag_id3_load(Path::FromFS(path), error); if (tag == NULL) { if (error.IsDefined()) - g_printerr("%s\n", error.GetMessage()); + LogError(error); else - g_printerr("No ID3 tag found\n"); + fprintf(stderr, "No ID3 tag found\n"); return EXIT_FAILURE; } @@ -74,19 +73,19 @@ int main(int argc, char **argv) id3_tag_delete(tag); if (!success) { - g_printerr("No RVA2 tag found\n"); + fprintf(stderr, "No RVA2 tag found\n"); return EXIT_FAILURE; } const ReplayGainTuple *tuple = &replay_gain.tuples[REPLAY_GAIN_ALBUM]; if (tuple->IsDefined()) - g_printerr("replay_gain[album]: gain=%f peak=%f\n", - tuple->gain, tuple->peak); + fprintf(stderr, "replay_gain[album]: gain=%f peak=%f\n", + tuple->gain, tuple->peak); tuple = &replay_gain.tuples[REPLAY_GAIN_TRACK]; if (tuple->IsDefined()) - g_printerr("replay_gain[track]: gain=%f peak=%f\n", - tuple->gain, tuple->peak); + fprintf(stderr, "replay_gain[track]: gain=%f peak=%f\n", + tuple->gain, tuple->peak); return EXIT_SUCCESS; } diff --git a/test/dump_text_file.cxx b/test/dump_text_file.cxx index bb84f5cce..3da965f03 100644 --- a/test/dump_text_file.cxx +++ b/test/dump_text_file.cxx @@ -39,16 +39,6 @@ #include <stdlib.h> static void -my_log_func(const gchar *log_domain, gcc_unused GLogLevelFlags log_level, - const gchar *message, gcc_unused gpointer user_data) -{ - if (log_domain != NULL) - g_printerr("%s: %s\n", log_domain, message); - else - g_printerr("%s\n", message); -} - -static void dump_text_file(TextInputStream &is) { std::string line; @@ -99,8 +89,8 @@ int main(int argc, char **argv) int ret; if (argc != 2) { - g_printerr("Usage: run_input URI\n"); - return 1; + fprintf(stderr, "Usage: run_input URI\n"); + return EXIT_FAILURE; } /* initialize GLib */ @@ -109,8 +99,6 @@ int main(int argc, char **argv) g_thread_init(NULL); #endif - g_log_set_default_handler(my_log_func, NULL); - /* initialize MPD */ config_global_init(); @@ -141,8 +129,8 @@ int main(int argc, char **argv) if (error.IsDefined()) LogError(error); else - g_printerr("input_stream::Open() failed\n"); - ret = 2; + fprintf(stderr, "input_stream::Open() failed\n"); + ret = EXIT_FAILURE; } /* deinitialize everything */ diff --git a/test/read_conf.cxx b/test/read_conf.cxx index d5eacec67..1f137e9a2 100644 --- a/test/read_conf.cxx +++ b/test/read_conf.cxx @@ -21,40 +21,26 @@ #include "ConfigGlobal.hxx" #include "fs/Path.hxx" #include "util/Error.hxx" - -#include <glib.h> +#include "Log.hxx" #include <assert.h> -static void -my_log_func(gcc_unused const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, gcc_unused gpointer user_data) -{ - if (log_level > G_LOG_LEVEL_WARNING) - return; - - g_printerr("%s\n", message); -} - int main(int argc, char **argv) { if (argc != 3) { - g_printerr("Usage: read_conf FILE SETTING\n"); - return 1; + fprintf(stderr, "Usage: read_conf FILE SETTING\n"); + return EXIT_FAILURE; } const Path config_path = Path::FromFS(argv[1]); const char *name = argv[2]; - g_log_set_default_handler(my_log_func, NULL); - config_global_init(); Error error; if (!ReadConfigFile(config_path, error)) { - g_printerr("%s:", error.GetMessage()); - return 1; + LogError(error); + return EXIT_FAILURE; } ConfigOption option = ParseConfigOptionName(name); @@ -63,11 +49,11 @@ int main(int argc, char **argv) : nullptr; int ret; if (value != NULL) { - g_print("%s\n", value); - ret = 0; + printf("%s\n", value); + ret = EXIT_SUCCESS; } else { - g_printerr("No such setting: %s\n", name); - ret = 2; + fprintf(stderr, "No such setting: %s\n", name); + ret = EXIT_FAILURE; } config_global_finish(); diff --git a/test/read_mixer.cxx b/test/read_mixer.cxx index 8426443ae..95aacf6db 100644 --- a/test/read_mixer.cxx +++ b/test/read_mixer.cxx @@ -21,12 +21,13 @@ #include "MixerControl.hxx" #include "MixerList.hxx" #include "FilterRegistry.hxx" -#include "pcm/PcmVolume.hxx" +#include "pcm/Volume.hxx" #include "GlobalEvents.hxx" #include "Main.hxx" #include "event/Loop.hxx" #include "ConfigData.hxx" #include "util/Error.hxx" +#include "Log.hxx" #include <glib.h> @@ -101,22 +102,13 @@ filter_plugin_by_name(gcc_unused const char *name) return NULL; } -bool -pcm_volume(gcc_unused void *buffer, gcc_unused size_t length, - gcc_unused SampleFormat format, - gcc_unused int volume) -{ - assert(false); - return false; -} - int main(int argc, gcc_unused char **argv) { int volume; if (argc != 2) { - g_printerr("Usage: read_mixer PLUGIN\n"); - return 1; + fprintf(stderr, "Usage: read_mixer PLUGIN\n"); + return EXIT_FAILURE; } #if !GLIB_CHECK_VERSION(2,32,0) @@ -129,14 +121,14 @@ int main(int argc, gcc_unused char **argv) Mixer *mixer = mixer_new(&alsa_mixer_plugin, nullptr, config_param(), error); if (mixer == NULL) { - g_printerr("mixer_new() failed: %s\n", error.GetMessage()); - return 2; + LogError(error, "mixer_new() failed"); + return EXIT_FAILURE; } if (!mixer_open(mixer, error)) { mixer_free(mixer); - g_printerr("failed to open the mixer: %s\n", error.GetMessage()); - return 2; + LogError(error, "failed to open the mixer"); + return EXIT_FAILURE; } volume = mixer_get_volume(mixer, error); @@ -149,13 +141,12 @@ int main(int argc, gcc_unused char **argv) if (volume < 0) { if (error.IsDefined()) { - g_printerr("failed to read volume: %s\n", - error.GetMessage()); + LogError(error, "failed to read volume"); } else - g_printerr("failed to read volume\n"); - return 2; + fprintf(stderr, "failed to read volume\n"); + return EXIT_FAILURE; } - g_print("%d\n", volume); + printf("%d\n", volume); return 0; } diff --git a/test/read_tags.cxx b/test/read_tags.cxx index 90f1424d9..dac9aba00 100644 --- a/test/read_tags.cxx +++ b/test/read_tags.cxx @@ -20,7 +20,7 @@ #include "config.h" #include "IOThread.hxx" #include "DecoderList.hxx" -#include "DecoderAPI.hxx" +#include "DecoderPlugin.hxx" #include "InputInit.hxx" #include "InputStream.hxx" #include "AudioFormat.hxx" @@ -42,98 +42,25 @@ #include <locale.h> #endif -void -decoder_initialized(gcc_unused Decoder &decoder, - gcc_unused const AudioFormat audio_format, - gcc_unused bool seekable, - gcc_unused float total_time) -{ -} - -DecoderCommand -decoder_get_command(gcc_unused Decoder &decoder) -{ - return DecoderCommand::NONE; -} - -void -decoder_command_finished(gcc_unused Decoder &decoder) -{ -} - -double -decoder_seek_where(gcc_unused Decoder &decoder) -{ - return 1.0; -} - -void -decoder_seek_error(gcc_unused Decoder &decoder) -{ -} - -size_t -decoder_read(gcc_unused Decoder *decoder, - InputStream &is, - void *buffer, size_t length) -{ - return is.LockRead(buffer, length, IgnoreError()); -} - -void -decoder_timestamp(gcc_unused Decoder &decoder, - gcc_unused double t) -{ -} - -DecoderCommand -decoder_data(gcc_unused Decoder &decoder, - gcc_unused InputStream *is, - const void *data, size_t datalen, - gcc_unused uint16_t kbit_rate) -{ - gcc_unused ssize_t nbytes = write(1, data, datalen); - return DecoderCommand::NONE; -} - -DecoderCommand -decoder_tag(gcc_unused Decoder &decoder, - gcc_unused InputStream *is, - gcc_unused Tag &&tag) -{ - return DecoderCommand::NONE; -} - -void -decoder_replay_gain(gcc_unused Decoder &decoder, - gcc_unused const ReplayGainInfo *replay_gain_info) -{ -} - -void -decoder_mixramp(gcc_unused Decoder &decoder, gcc_unused MixRampInfo &&mix_ramp) -{ -} - static bool empty = true; static void print_duration(unsigned seconds, gcc_unused void *ctx) { - g_print("duration=%d\n", seconds); + printf("duration=%d\n", seconds); } static void print_tag(TagType type, const char *value, gcc_unused void *ctx) { - g_print("[%s]=%s\n", tag_item_names[type], value); + printf("[%s]=%s\n", tag_item_names[type], value); empty = false; } static void print_pair(const char *name, const char *value, gcc_unused void *ctx) { - g_print("\"%s\"=%s\n", name, value); + printf("\"%s\"=%s\n", name, value); } static const struct tag_handler print_handler = { @@ -153,8 +80,8 @@ int main(int argc, char **argv) #endif if (argc != 3) { - g_printerr("Usage: read_tags DECODER FILE\n"); - return 1; + fprintf(stderr, "Usage: read_tags DECODER FILE\n"); + return EXIT_FAILURE; } decoder_name = argv[1]; @@ -177,8 +104,8 @@ int main(int argc, char **argv) plugin = decoder_plugin_from_name(decoder_name); if (plugin == NULL) { - g_printerr("No such decoder: %s\n", decoder_name); - return 1; + fprintf(stderr, "No such decoder: %s\n", decoder_name); + return EXIT_FAILURE; } bool success = plugin->ScanFile(path, print_handler, nullptr); @@ -189,9 +116,8 @@ int main(int argc, char **argv) InputStream *is = InputStream::Open(path, mutex, cond, error); if (is == NULL) { - g_printerr("Failed to open %s: %s\n", - path, error.GetMessage()); - return 1; + FormatError(error, "Failed to open %s", path); + return EXIT_FAILURE; } mutex.lock(); @@ -201,8 +127,7 @@ int main(int argc, char **argv) if (!is->Check(error)) { mutex.unlock(); - g_printerr("Failed to read %s: %s\n", - path, error.GetMessage()); + FormatError(error, "Failed to read %s", path); return EXIT_FAILURE; } @@ -217,8 +142,8 @@ int main(int argc, char **argv) io_thread_deinit(); if (!success) { - g_printerr("Failed to read tags\n"); - return 1; + fprintf(stderr, "Failed to read tags\n"); + return EXIT_FAILURE; } if (empty) { diff --git a/test/run_convert.cxx b/test/run_convert.cxx index 0e873a3b3..67783592c 100644 --- a/test/run_convert.cxx +++ b/test/run_convert.cxx @@ -30,25 +30,14 @@ #include "ConfigGlobal.hxx" #include "util/FifoBuffer.hxx" #include "util/Error.hxx" +#include "Log.hxx" #include "stdbin.h" -#include <glib.h> - #include <assert.h> #include <stddef.h> #include <stdlib.h> #include <unistd.h> -static void -my_log_func(const gchar *log_domain, gcc_unused GLogLevelFlags log_level, - const gchar *message, gcc_unused gpointer user_data) -{ - if (log_domain != NULL) - g_printerr("%s: %s\n", log_domain, message); - else - g_printerr("%s\n", message); -} - const char * config_get_string(gcc_unused enum ConfigOption option, const char *default_value) @@ -62,26 +51,23 @@ int main(int argc, char **argv) const void *output; if (argc != 3) { - g_printerr("Usage: run_convert IN_FORMAT OUT_FORMAT <IN >OUT\n"); + fprintf(stderr, + "Usage: run_convert IN_FORMAT OUT_FORMAT <IN >OUT\n"); return 1; } - g_log_set_default_handler(my_log_func, NULL); - Error error; if (!audio_format_parse(in_audio_format, argv[1], false, error)) { - g_printerr("Failed to parse audio format: %s\n", - error.GetMessage()); - return 1; + LogError(error, "Failed to parse audio format"); + return EXIT_FAILURE; } AudioFormat out_audio_format_mask; if (!audio_format_parse(out_audio_format_mask, argv[2], true, error)) { - g_printerr("Failed to parse audio format: %s\n", - error.GetMessage()); - return 1; + LogError(error, "Failed to parse audio format"); + return EXIT_FAILURE; } out_audio_format = in_audio_format; @@ -90,6 +76,10 @@ int main(int argc, char **argv) const size_t in_frame_size = in_audio_format.GetFrameSize(); PcmConvert state; + if (!state.Open(in_audio_format, out_audio_format_mask, error)) { + LogError(error, "Failed to open PcmConvert"); + return EXIT_FAILURE; + } FifoBuffer<uint8_t, 4096> buffer; @@ -115,15 +105,18 @@ int main(int argc, char **argv) buffer.Consume(src.size); size_t length; - output = state.Convert(in_audio_format, src.data, src.size, - out_audio_format, &length, error); + output = state.Convert(src.data, src.size, + &length, error); if (output == NULL) { - g_printerr("Failed to convert: %s\n", error.GetMessage()); - return 2; + state.Close(); + LogError(error, "Failed to convert"); + return EXIT_FAILURE; } gcc_unused ssize_t ignored = write(1, output, length); } + state.Close(); + return EXIT_SUCCESS; } diff --git a/test/run_decoder.cxx b/test/run_decoder.cxx index 2f7330a1d..fb29bd3b3 100644 --- a/test/run_decoder.cxx +++ b/test/run_decoder.cxx @@ -29,21 +29,14 @@ #include "Log.hxx" #include "stdbin.h" +#ifdef HAVE_GLIB #include <glib.h> +#endif #include <assert.h> #include <unistd.h> #include <stdlib.h> - -static void -my_log_func(const gchar *log_domain, gcc_unused GLogLevelFlags log_level, - const gchar *message, gcc_unused gpointer user_data) -{ - if (log_domain != NULL) - g_printerr("%s: %s\n", log_domain, message); - else - g_printerr("%s\n", message); -} +#include <stdio.h> struct Decoder { const char *uri; @@ -64,9 +57,9 @@ decoder_initialized(Decoder &decoder, assert(!decoder.initialized); assert(audio_format.IsValid()); - g_printerr("audio_format=%s duration=%f\n", - audio_format_to_string(audio_format, &af_string), - duration); + fprintf(stderr, "audio_format=%s duration=%f\n", + audio_format_to_string(audio_format, &af_string), + duration); decoder.initialized = true; } @@ -101,6 +94,40 @@ decoder_read(gcc_unused Decoder *decoder, return is.LockRead(buffer, length, IgnoreError()); } +bool +decoder_read_full(Decoder *decoder, InputStream &is, + void *_buffer, size_t size) +{ + uint8_t *buffer = (uint8_t *)_buffer; + + while (size > 0) { + size_t nbytes = decoder_read(decoder, is, buffer, size); + if (nbytes == 0) + return false; + + buffer += nbytes; + size -= nbytes; + } + + return true; +} + +bool +decoder_skip(Decoder *decoder, InputStream &is, size_t size) +{ + while (size > 0) { + char buffer[1024]; + size_t nbytes = decoder_read(decoder, is, buffer, + std::min(sizeof(buffer), size)); + if (nbytes == 0) + return false; + + size -= nbytes; + } + + return true; +} + void decoder_timestamp(gcc_unused Decoder &decoder, gcc_unused double t) @@ -131,13 +158,13 @@ decoder_replay_gain(gcc_unused Decoder &decoder, { const ReplayGainTuple *tuple = &rgi->tuples[REPLAY_GAIN_ALBUM]; if (tuple->IsDefined()) - g_printerr("replay_gain[album]: gain=%f peak=%f\n", - tuple->gain, tuple->peak); + fprintf(stderr, "replay_gain[album]: gain=%f peak=%f\n", + tuple->gain, tuple->peak); tuple = &rgi->tuples[REPLAY_GAIN_TRACK]; if (tuple->IsDefined()) - g_printerr("replay_gain[track]: gain=%f peak=%f\n", - tuple->gain, tuple->peak); + fprintf(stderr, "replay_gain[track]: gain=%f peak=%f\n", + tuple->gain, tuple->peak); } void @@ -150,19 +177,19 @@ int main(int argc, char **argv) const char *decoder_name; if (argc != 3) { - g_printerr("Usage: run_decoder DECODER URI >OUT\n"); - return 1; + fprintf(stderr, "Usage: run_decoder DECODER URI >OUT\n"); + return EXIT_FAILURE; } Decoder decoder; decoder_name = argv[1]; decoder.uri = argv[2]; +#ifdef HAVE_GLIB #if !GLIB_CHECK_VERSION(2,32,0) g_thread_init(NULL); #endif - - g_log_set_default_handler(my_log_func, NULL); +#endif io_thread_init(); io_thread_start(); @@ -170,15 +197,15 @@ int main(int argc, char **argv) Error error; if (!input_stream_global_init(error)) { LogError(error); - return 2; + return EXIT_FAILURE; } decoder_plugin_init_all(); decoder.plugin = decoder_plugin_from_name(decoder_name); if (decoder.plugin == NULL) { - g_printerr("No such decoder: %s\n", decoder_name); - return 1; + fprintf(stderr, "No such decoder: %s\n", decoder_name); + return EXIT_FAILURE; } decoder.initialized = false; @@ -195,17 +222,17 @@ int main(int argc, char **argv) if (error.IsDefined()) LogError(error); else - g_printerr("InputStream::Open() failed\n"); + fprintf(stderr, "InputStream::Open() failed\n"); - return 1; + return EXIT_FAILURE; } decoder.plugin->StreamDecode(decoder, *is); is->Close(); } else { - g_printerr("Decoder plugin is not usable\n"); - return 1; + fprintf(stderr, "Decoder plugin is not usable\n"); + return EXIT_FAILURE; } decoder_plugin_deinit_all(); @@ -213,8 +240,8 @@ int main(int argc, char **argv) io_thread_deinit(); if (!decoder.initialized) { - g_printerr("Decoding failed\n"); - return 1; + fprintf(stderr, "Decoding failed\n"); + return EXIT_FAILURE; } return 0; diff --git a/test/run_encoder.cxx b/test/run_encoder.cxx index 838ee708e..166af08ef 100644 --- a/test/run_encoder.cxx +++ b/test/run_encoder.cxx @@ -24,10 +24,9 @@ #include "AudioParser.hxx" #include "ConfigData.hxx" #include "util/Error.hxx" +#include "Log.hxx" #include "stdbin.h" -#include <glib.h> - #include <stddef.h> #include <unistd.h> @@ -50,8 +49,9 @@ int main(int argc, char **argv) /* parse command line */ if (argc > 3) { - g_printerr("Usage: run_encoder [ENCODER] [FORMAT] <IN >OUT\n"); - return 1; + fprintf(stderr, + "Usage: run_encoder [ENCODER] [FORMAT] <IN >OUT\n"); + return EXIT_FAILURE; } if (argc > 1) @@ -63,8 +63,8 @@ int main(int argc, char **argv) const auto plugin = encoder_plugin_get(encoder_name); if (plugin == NULL) { - g_printerr("No such encoder: %s\n", encoder_name); - return 1; + fprintf(stderr, "No such encoder: %s\n", encoder_name); + return EXIT_FAILURE; } config_param param; @@ -73,9 +73,8 @@ int main(int argc, char **argv) Error error; const auto encoder = encoder_init(*plugin, param, error); if (encoder == NULL) { - g_printerr("Failed to initialize encoder: %s\n", - error.GetMessage()); - return 1; + LogError(error, "Failed to initialize encoder"); + return EXIT_FAILURE; } /* open the encoder */ @@ -83,16 +82,14 @@ int main(int argc, char **argv) AudioFormat audio_format(44100, SampleFormat::S16, 2); if (argc > 2) { if (!audio_format_parse(audio_format, argv[2], false, error)) { - g_printerr("Failed to parse audio format: %s\n", - error.GetMessage()); - return 1; + LogError(error, "Failed to parse audio format"); + return EXIT_FAILURE; } } if (!encoder_open(encoder, audio_format, error)) { - g_printerr("Failed to open encoder: %s\n", - error.GetMessage()); - return 1; + LogError(error, "Failed to open encoder"); + return EXIT_FAILURE; } encoder_to_stdout(*encoder); @@ -102,19 +99,20 @@ int main(int argc, char **argv) ssize_t nbytes; while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) { if (!encoder_write(encoder, buffer, nbytes, error)) { - g_printerr("encoder_write() failed: %s\n", - error.GetMessage()); - return 1; + LogError(error, "encoder_write() failed"); + return EXIT_FAILURE; } encoder_to_stdout(*encoder); } if (!encoder_end(encoder, error)) { - g_printerr("encoder_flush() failed: %s\n", - error.GetMessage()); - return 1; + LogError(error, "encoder_flush() failed"); + return EXIT_FAILURE; } encoder_to_stdout(*encoder); + + encoder_close(encoder); + encoder_finish(encoder); } diff --git a/test/run_filter.cxx b/test/run_filter.cxx index 085fc256b..6781700d8 100644 --- a/test/run_filter.cxx +++ b/test/run_filter.cxx @@ -25,11 +25,12 @@ #include "AudioFormat.hxx" #include "FilterPlugin.hxx" #include "FilterInternal.hxx" -#include "pcm/PcmVolume.hxx" +#include "pcm/Volume.hxx" #include "MixerControl.hxx" #include "stdbin.h" #include "util/Error.hxx" #include "system/FatalError.hxx" +#include "Log.hxx" #include <glib.h> @@ -45,16 +46,6 @@ mixer_set_volume(gcc_unused Mixer *mixer, return true; } -static void -my_log_func(const gchar *log_domain, gcc_unused GLogLevelFlags log_level, - const gchar *message, gcc_unused gpointer user_data) -{ - if (log_domain != NULL) - g_printerr("%s: %s\n", log_domain, message); - else - g_printerr("%s\n", message); -} - static const struct config_param * find_named_config_block(ConfigOption option, const char *name) { @@ -76,14 +67,14 @@ load_filter(const char *name) param = find_named_config_block(CONF_AUDIO_FILTER, name); if (param == NULL) { - g_printerr("No such configured filter: %s\n", name); + fprintf(stderr, "No such configured filter: %s\n", name); return nullptr; } Error error; Filter *filter = filter_configured_new(*param, error); if (filter == NULL) { - g_printerr("Failed to load filter: %s\n", error.GetMessage()); + LogError(error, "Failed to load filter"); return NULL; } @@ -97,8 +88,8 @@ int main(int argc, char **argv) char buffer[4096]; if (argc < 3 || argc > 4) { - g_printerr("Usage: run_filter CONFIG NAME [FORMAT] <IN\n"); - return 1; + fprintf(stderr, "Usage: run_filter CONFIG NAME [FORMAT] <IN\n"); + return EXIT_FAILURE; } const Path config_path = Path::FromFS(argv[1]); @@ -111,8 +102,6 @@ int main(int argc, char **argv) g_thread_init(NULL); #endif - g_log_set_default_handler(my_log_func, NULL); - /* read configuration file (mpd.conf) */ config_global_init(); @@ -124,9 +113,8 @@ int main(int argc, char **argv) if (argc > 3) { Error error; if (!audio_format_parse(audio_format, argv[3], false, error)) { - g_printerr("Failed to parse audio format: %s\n", - error.GetMessage()); - return 1; + LogError(error, "Failed to parse audio format"); + return EXIT_FAILURE; } } @@ -134,20 +122,20 @@ int main(int argc, char **argv) Filter *filter = load_filter(argv[2]); if (filter == NULL) - return 1; + return EXIT_FAILURE; /* open the filter */ Error error; const AudioFormat out_audio_format = filter->Open(audio_format, error); if (!out_audio_format.IsDefined()) { - g_printerr("Failed to open filter: %s\n", error.GetMessage()); + LogError(error, "Failed to open filter"); delete filter; - return 1; + return EXIT_FAILURE; } - g_printerr("audio_format=%s\n", - audio_format_to_string(out_audio_format, &af_string)); + fprintf(stderr, "audio_format=%s\n", + audio_format_to_string(out_audio_format, &af_string)); /* play */ @@ -163,15 +151,16 @@ int main(int argc, char **argv) dest = filter->FilterPCM(buffer, (size_t)nbytes, &length, error); if (dest == NULL) { - g_printerr("Filter failed: %s\n", error.GetMessage()); + LogError(error, "Filter failed"); filter->Close(); delete filter; - return 1; + return EXIT_FAILURE; } nbytes = write(1, dest, length); if (nbytes < 0) { - g_printerr("Failed to write: %s\n", g_strerror(errno)); + fprintf(stderr, "Failed to write: %s\n", + strerror(errno)); filter->Close(); delete filter; return 1; diff --git a/test/run_inotify.cxx b/test/run_inotify.cxx index c57e6e9ef..8dcc371cc 100644 --- a/test/run_inotify.cxx +++ b/test/run_inotify.cxx @@ -24,8 +24,6 @@ #include "util/Error.hxx" #include "Log.hxx" -#include <glib.h> - #include <sys/inotify.h> static constexpr unsigned IN_MASK = @@ -39,7 +37,7 @@ static void my_inotify_callback(gcc_unused int wd, unsigned mask, const char *name, gcc_unused void *ctx) { - g_print("mask=0x%x name='%s'\n", mask, name); + printf("mask=0x%x name='%s'\n", mask, name); } int main(int argc, char **argv) @@ -47,8 +45,8 @@ int main(int argc, char **argv) const char *path; if (argc != 2) { - g_printerr("Usage: run_inotify PATH\n"); - return 1; + fprintf(stderr, "Usage: run_inotify PATH\n"); + return EXIT_FAILURE; } path = argv[1]; @@ -62,17 +60,18 @@ int main(int argc, char **argv) nullptr, error); if (source == NULL) { LogError(error); - return 2; + return EXIT_FAILURE; } int descriptor = source->Add(path, IN_MASK, error); if (descriptor < 0) { delete source; LogError(error); - return 2; + return EXIT_FAILURE; } event_loop.Run(); delete source; + return EXIT_SUCCESS; } diff --git a/test/run_input.cxx b/test/run_input.cxx index 3817ed418..fcf4107c4 100644 --- a/test/run_input.cxx +++ b/test/run_input.cxx @@ -33,21 +33,13 @@ #include "ArchiveList.hxx" #endif +#ifdef HAVE_GLIB #include <glib.h> +#endif #include <unistd.h> #include <stdlib.h> -static void -my_log_func(const gchar *log_domain, gcc_unused GLogLevelFlags log_level, - const gchar *message, gcc_unused gpointer user_data) -{ - if (log_domain != NULL) - g_printerr("%s: %s\n", log_domain, message); - else - g_printerr("%s\n", message); -} - static int dump_input_stream(InputStream *is) { @@ -71,14 +63,14 @@ dump_input_stream(InputStream *is) /* print meta data */ if (!is->mime.empty()) - g_printerr("MIME type: %s\n", is->mime.c_str()); + fprintf(stderr, "MIME type: %s\n", is->mime.c_str()); /* read data and tags from the stream */ while (!is->IsEOF()) { Tag *tag = is->ReadTag(); if (tag != NULL) { - g_printerr("Received a tag:\n"); + fprintf(stderr, "Received a tag:\n"); tag_save(stderr, *tag); delete tag; } @@ -114,17 +106,17 @@ int main(int argc, char **argv) int ret; if (argc != 2) { - g_printerr("Usage: run_input URI\n"); - return 1; + fprintf(stderr, "Usage: run_input URI\n"); + return EXIT_FAILURE; } /* initialize GLib */ +#ifdef HAVE_GLIB #if !GLIB_CHECK_VERSION(2,32,0) g_thread_init(NULL); #endif - - g_log_set_default_handler(my_log_func, NULL); +#endif /* initialize MPD */ @@ -155,8 +147,8 @@ int main(int argc, char **argv) if (error.IsDefined()) LogError(error); else - g_printerr("input_stream::Open() failed\n"); - ret = 2; + fprintf(stderr, "input_stream::Open() failed\n"); + ret = EXIT_FAILURE; } /* deinitialize everything */ diff --git a/test/run_normalize.cxx b/test/run_normalize.cxx index 3193fefd2..091c3d61a 100644 --- a/test/run_normalize.cxx +++ b/test/run_normalize.cxx @@ -33,6 +33,7 @@ #include <glib.h> #include <stddef.h> +#include <stdio.h> #include <unistd.h> #include <string.h> @@ -43,7 +44,7 @@ int main(int argc, char **argv) ssize_t nbytes; if (argc > 2) { - g_printerr("Usage: run_normalize [FORMAT] <IN >OUT\n"); + fprintf(stderr, "Usage: run_normalize [FORMAT] <IN >OUT\n"); return 1; } @@ -51,7 +52,7 @@ int main(int argc, char **argv) if (argc > 1) { Error error; if (!audio_format_parse(audio_format, argv[1], false, error)) { - g_printerr("Failed to parse audio format: %s\n", + fprintf(stderr, "Failed to parse audio format: %s\n", error.GetMessage()); return 1; } diff --git a/test/run_output.cxx b/test/run_output.cxx index 7982bd7de..c7865bf2c 100644 --- a/test/run_output.cxx +++ b/test/run_output.cxx @@ -36,6 +36,7 @@ #include "PlayerControl.hxx" #include "stdbin.h" #include "util/Error.hxx" +#include "Log.hxx" #include <glib.h> @@ -83,7 +84,7 @@ load_audio_output(const char *name) param = find_named_config_block(CONF_AUDIO_OUTPUT, name); if (param == NULL) { - g_printerr("No such configured audio output: %s\n", name); + fprintf(stderr, "No such configured audio output: %s\n", name); return nullptr; } @@ -93,7 +94,7 @@ load_audio_output(const char *name) struct audio_output *ao = audio_output_new(*param, dummy_player_control, error); if (ao == nullptr) - g_printerr("%s\n", error.GetMessage()); + LogError(error); return ao; } @@ -105,21 +106,19 @@ run_output(struct audio_output *ao, AudioFormat audio_format) Error error; if (!ao_plugin_enable(ao, error)) { - g_printerr("Failed to enable audio output: %s\n", - error.GetMessage()); + LogError(error, "Failed to enable audio output"); return false; } if (!ao_plugin_open(ao, audio_format, error)) { ao_plugin_disable(ao); - g_printerr("Failed to open audio output: %s\n", - error.GetMessage()); + LogError(error, "Failed to open audio output"); return false; } struct audio_format_string af_string; - g_printerr("audio_format=%s\n", - audio_format_to_string(audio_format, &af_string)); + fprintf(stderr, "audio_format=%s\n", + audio_format_to_string(audio_format, &af_string)); size_t frame_size = audio_format.GetFrameSize(); @@ -145,8 +144,7 @@ run_output(struct audio_output *ao, AudioFormat audio_format) if (consumed == 0) { ao_plugin_close(ao); ao_plugin_disable(ao); - g_printerr("Failed to play: %s\n", - error.GetMessage()); + LogError(error, "Failed to play"); return false; } @@ -168,8 +166,8 @@ int main(int argc, char **argv) Error error; if (argc < 3 || argc > 4) { - g_printerr("Usage: run_output CONFIG NAME [FORMAT] <IN\n"); - return 1; + fprintf(stderr, "Usage: run_output CONFIG NAME [FORMAT] <IN\n"); + return EXIT_FAILURE; } const Path config_path = Path::FromFS(argv[1]); @@ -184,8 +182,8 @@ int main(int argc, char **argv) config_global_init(); if (!ReadConfigFile(config_path, error)) { - g_printerr("%s\n", error.GetMessage()); - return 1; + LogError(error); + return EXIT_FAILURE; } main_loop = new EventLoop(EventLoop::Default()); @@ -203,9 +201,8 @@ int main(int argc, char **argv) if (argc > 3) { if (!audio_format_parse(audio_format, argv[3], false, error)) { - g_printerr("Failed to parse audio format: %s\n", - error.GetMessage()); - return 1; + LogError(error, "Failed to parse audio format"); + return EXIT_FAILURE; } } diff --git a/test/run_resolver.cxx b/test/run_resolver.cxx index 7da2fd5b2..aba7238ec 100644 --- a/test/run_resolver.cxx +++ b/test/run_resolver.cxx @@ -22,8 +22,6 @@ #include "util/Error.hxx" #include "Log.hxx" -#include <glib.h> - #ifdef WIN32 #include <ws2tcpip.h> #include <winsock.h> @@ -37,7 +35,7 @@ int main(int argc, char **argv) { if (argc != 2) { - g_printerr("Usage: run_resolver HOST\n"); + fprintf(stderr, "Usage: run_resolver HOST\n"); return EXIT_FAILURE; } @@ -51,16 +49,8 @@ int main(int argc, char **argv) } for (const struct addrinfo *i = ai; i != NULL; i = i->ai_next) { - char *p = sockaddr_to_string(i->ai_addr, i->ai_addrlen, - error); - if (p == NULL) { - freeaddrinfo(ai); - LogError(error); - return EXIT_FAILURE; - } - - g_print("%s\n", p); - g_free(p); + const auto s = sockaddr_to_string(i->ai_addr, i->ai_addrlen); + printf("%s\n", s.c_str()); } freeaddrinfo(ai); diff --git a/test/software_volume.cxx b/test/software_volume.cxx index 19a0be88c..1a336f2df 100644 --- a/test/software_volume.cxx +++ b/test/software_volume.cxx @@ -24,15 +24,16 @@ */ #include "config.h" -#include "pcm/PcmVolume.hxx" +#include "pcm/Volume.hxx" #include "AudioParser.hxx" #include "AudioFormat.hxx" +#include "util/ConstBuffer.hxx" #include "util/Error.hxx" #include "stdbin.h" - -#include <glib.h> +#include "Log.hxx" #include <stddef.h> +#include <stdlib.h> #include <unistd.h> int main(int argc, char **argv) @@ -41,28 +42,29 @@ int main(int argc, char **argv) ssize_t nbytes; if (argc > 2) { - g_printerr("Usage: software_volume [FORMAT] <IN >OUT\n"); - return 1; + fprintf(stderr, "Usage: software_volume [FORMAT] <IN >OUT\n"); + return EXIT_FAILURE; } Error error; AudioFormat audio_format(48000, SampleFormat::S16, 2); if (argc > 1) { if (!audio_format_parse(audio_format, argv[1], false, error)) { - g_printerr("Failed to parse audio format: %s\n", - error.GetMessage()); - return 1; + LogError(error, "Failed to parse audio format"); + return EXIT_FAILURE; } } - while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) { - if (!pcm_volume(buffer, nbytes, - audio_format.format, - PCM_VOLUME_1 / 2)) { - g_printerr("pcm_volume() has failed\n"); - return 2; - } + PcmVolume pv; + if (!pv.Open(audio_format.format, error)) { + fprintf(stderr, "%s\n", error.GetMessage()); + return EXIT_FAILURE; + } - gcc_unused ssize_t ignored = write(1, buffer, nbytes); + while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) { + auto dest = pv.Apply({buffer, size_t(nbytes)}); + gcc_unused ssize_t ignored = write(1, dest.data, dest.size); } + + pv.Close(); } diff --git a/test/test_pcm_channels.cxx b/test/test_pcm_channels.cxx index 85c872674..355553687 100644 --- a/test/test_pcm_channels.cxx +++ b/test/test_pcm_channels.cxx @@ -22,67 +22,60 @@ #include "test_pcm_util.hxx" #include "pcm/PcmChannels.hxx" #include "pcm/PcmBuffer.hxx" +#include "util/ConstBuffer.hxx" void PcmChannelsTest::TestChannels16() { - constexpr unsigned N = 256; + constexpr size_t N = 256; const auto src = TestDataBuffer<int16_t, N * 2>(); PcmBuffer buffer; /* stereo to mono */ - size_t dest_size; - const int16_t *dest = - pcm_convert_channels_16(buffer, 1, 2, src, sizeof(src), - &dest_size); - CPPUNIT_ASSERT(dest != NULL); - CPPUNIT_ASSERT_EQUAL(sizeof(src) / 2, dest_size); + auto dest = pcm_convert_channels_16(buffer, 1, 2, { src, N * 2 }); + CPPUNIT_ASSERT(!dest.IsNull()); + CPPUNIT_ASSERT_EQUAL(N, dest.size); for (unsigned i = 0; i < N; ++i) CPPUNIT_ASSERT_EQUAL(int16_t((src[i * 2] + src[i * 2 + 1]) / 2), - dest[i]); + dest.data[i]); /* mono to stereo */ - dest = pcm_convert_channels_16(buffer, 2, 1, src, sizeof(src), - &dest_size); - CPPUNIT_ASSERT(dest != NULL); - CPPUNIT_ASSERT_EQUAL(sizeof(src) * 2, dest_size); + dest = pcm_convert_channels_16(buffer, 2, 1, { src, N * 2 }); + CPPUNIT_ASSERT(!dest.IsNull()); + CPPUNIT_ASSERT_EQUAL(N * 4, dest.size); for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2]); - CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2 + 1]); + CPPUNIT_ASSERT_EQUAL(src[i], dest.data[i * 2]); + CPPUNIT_ASSERT_EQUAL(src[i], dest.data[i * 2 + 1]); } } void PcmChannelsTest::TestChannels32() { - constexpr unsigned N = 256; + constexpr size_t N = 256; const auto src = TestDataBuffer<int32_t, N * 2>(); PcmBuffer buffer; /* stereo to mono */ - size_t dest_size; - const int32_t *dest = - pcm_convert_channels_32(buffer, 1, 2, src, sizeof(src), - &dest_size); - CPPUNIT_ASSERT(dest != NULL); - CPPUNIT_ASSERT_EQUAL(sizeof(src) / 2, dest_size); + auto dest = pcm_convert_channels_32(buffer, 1, 2, { src, N * 2 }); + CPPUNIT_ASSERT(!dest.IsNull()); + CPPUNIT_ASSERT_EQUAL(N, dest.size); for (unsigned i = 0; i < N; ++i) CPPUNIT_ASSERT_EQUAL(int32_t(((int64_t)src[i * 2] + (int64_t)src[i * 2 + 1]) / 2), - dest[i]); + dest.data[i]); /* mono to stereo */ - dest = pcm_convert_channels_32(buffer, 2, 1, src, sizeof(src), - &dest_size); - CPPUNIT_ASSERT(dest != NULL); - CPPUNIT_ASSERT_EQUAL(sizeof(src) * 2, dest_size); + dest = pcm_convert_channels_32(buffer, 2, 1, { src, N * 2 }); + CPPUNIT_ASSERT(!dest.IsNull()); + CPPUNIT_ASSERT_EQUAL(N * 4, dest.size); for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2]); - CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2 + 1]); + CPPUNIT_ASSERT_EQUAL(src[i], dest.data[i * 2]); + CPPUNIT_ASSERT_EQUAL(src[i], dest.data[i * 2 + 1]); } } diff --git a/test/test_pcm_dither.cxx b/test/test_pcm_dither.cxx index 710deffcc..bf7484885 100644 --- a/test/test_pcm_dither.cxx +++ b/test/test_pcm_dither.cxx @@ -19,7 +19,7 @@ #include "test_pcm_all.hxx" #include "test_pcm_util.hxx" -#include "pcm/PcmDither.hxx" +#include "pcm/PcmDither.cxx" void PcmDitherTest::TestDither24() diff --git a/test/test_pcm_mix.cxx b/test/test_pcm_mix.cxx index 2a8a11388..542c4de80 100644 --- a/test/test_pcm_mix.cxx +++ b/test/test_pcm_mix.cxx @@ -21,6 +21,7 @@ #include "test_pcm_all.hxx" #include "test_pcm_util.hxx" #include "pcm/PcmMix.hxx" +#include "pcm/PcmDither.hxx" template<typename T, SampleFormat format, typename G=RandomInt<T>> static void @@ -30,23 +31,26 @@ TestPcmMix(G g=G()) const auto src1 = TestDataBuffer<T, N>(g); const auto src2 = TestDataBuffer<T, N>(g); + PcmDither dither; + /* portion1=1.0: result must be equal to src1 */ auto result = src1; - bool success = pcm_mix(result.begin(), src2.begin(), sizeof(result), + bool success = pcm_mix(dither, + result.begin(), src2.begin(), sizeof(result), format, 1.0); CPPUNIT_ASSERT(success); - AssertEqualWithTolerance(result, src1, 1); + AssertEqualWithTolerance(result, src1, 3); /* portion1=0.0: result must be equal to src2 */ result = src1; - success = pcm_mix(result.begin(), src2.begin(), sizeof(result), + success = pcm_mix(dither, result.begin(), src2.begin(), sizeof(result), format, 0.0); CPPUNIT_ASSERT(success); - AssertEqualWithTolerance(result, src2, 1); + AssertEqualWithTolerance(result, src2, 3); /* portion1=0.5 */ result = src1; - success = pcm_mix(result.begin(), src2.begin(), sizeof(result), + success = pcm_mix(dither, result.begin(), src2.begin(), sizeof(result), format, 0.5); CPPUNIT_ASSERT(success); @@ -54,7 +58,7 @@ TestPcmMix(G g=G()) for (unsigned i = 0; i < N; ++i) expected[i] = (int64_t(src1[i]) + int64_t(src2[i])) / 2; - AssertEqualWithTolerance(result, expected, 1); + AssertEqualWithTolerance(result, expected, 3); } void diff --git a/test/test_pcm_volume.cxx b/test/test_pcm_volume.cxx index 764d8b127..c3a261f7a 100644 --- a/test/test_pcm_volume.cxx +++ b/test/test_pcm_volume.cxx @@ -17,169 +17,109 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "config.h" #include "test_pcm_all.hxx" -#include "pcm/PcmVolume.hxx" +#include "pcm/Volume.hxx" +#include "pcm/Traits.hxx" +#include "util/ConstBuffer.hxx" +#include "util/Error.hxx" #include "test_pcm_util.hxx" #include <algorithm> #include <string.h> -void -PcmVolumeTest::TestVolume8() +template<SampleFormat F, class Traits=SampleTraits<F>, + typename G=RandomInt<typename Traits::value_type>> +static void +TestVolume(G g=G()) { - constexpr unsigned N = 256; - static int8_t zero[N]; - const auto src = TestDataBuffer<int8_t, N>(); + typedef typename Traits::value_type value_type; + + PcmVolume pv; + CPPUNIT_ASSERT(pv.Open(F, IgnoreError())); - int8_t dest[N]; + constexpr size_t N = 256; + static value_type zero[N]; + const auto _src = TestDataBuffer<value_type, N>(g); + const ConstBuffer<void> src(_src, sizeof(_src)); - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S8, 0)); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, zero, sizeof(zero))); + pv.SetVolume(0); + auto dest = pv.Apply(src); + CPPUNIT_ASSERT_EQUAL(src.size, dest.size); + CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, zero, sizeof(zero))); - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S8, PCM_VOLUME_1)); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, src, sizeof(src))); + pv.SetVolume(PCM_VOLUME_1); + dest = pv.Apply(src); + CPPUNIT_ASSERT_EQUAL(src.size, dest.size); + CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, src.data, src.size)); - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S8, PCM_VOLUME_1 / 2)); + pv.SetVolume(PCM_VOLUME_1 / 2); + dest = pv.Apply(src); + CPPUNIT_ASSERT_EQUAL(src.size, dest.size); + const auto _dest = ConstBuffer<value_type>::FromVoid(dest); for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT(dest[i] >= (src[i] - 1) / 2); - CPPUNIT_ASSERT(dest[i] <= src[i] / 2 + 1); + const auto expected = (_src[i] + 1) / 2; + CPPUNIT_ASSERT(_dest.data[i] >= expected - 4); + CPPUNIT_ASSERT(_dest.data[i] <= expected + 4); } + + pv.Close(); } void -PcmVolumeTest::TestVolume16() +PcmVolumeTest::TestVolume8() { - constexpr unsigned N = 256; - static int16_t zero[N]; - const auto src = TestDataBuffer<int16_t, N>(); - - int16_t dest[N]; - - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S16, 0)); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, zero, sizeof(zero))); - - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S16, PCM_VOLUME_1)); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, src, sizeof(src))); - - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S16, PCM_VOLUME_1 / 2)); + TestVolume<SampleFormat::S8>(); +} - for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT(dest[i] >= (src[i] - 1) / 2); - CPPUNIT_ASSERT(dest[i] <= src[i] / 2 + 1); - } +void +PcmVolumeTest::TestVolume16() +{ + TestVolume<SampleFormat::S16>(); } void PcmVolumeTest::TestVolume24() { - constexpr unsigned N = 256; - static int32_t zero[N]; - const auto src = TestDataBuffer<int32_t, N>(RandomInt24()); - - int32_t dest[N]; - - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S24_P32, 0)); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, zero, sizeof(zero))); - - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S24_P32, PCM_VOLUME_1)); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, src, sizeof(src))); - - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S24_P32, PCM_VOLUME_1 / 2)); - - for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT(dest[i] >= (src[i] - 1) / 2); - CPPUNIT_ASSERT(dest[i] <= src[i] / 2 + 1); - } + TestVolume<SampleFormat::S24_P32>(RandomInt24()); } void PcmVolumeTest::TestVolume32() { - constexpr unsigned N = 256; - static int32_t zero[N]; - const auto src = TestDataBuffer<int32_t, N>(); - - int32_t dest[N]; - - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S32, 0)); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, zero, sizeof(zero))); - - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S32, PCM_VOLUME_1)); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, src, sizeof(src))); - - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::S32, PCM_VOLUME_1 / 2)); - - for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT(dest[i] >= (src[i] - 1) / 2); - CPPUNIT_ASSERT(dest[i] <= src[i] / 2 + 1); - } + TestVolume<SampleFormat::S32>(); } void PcmVolumeTest::TestVolumeFloat() { - constexpr unsigned N = 256; - static float zero[N]; - const auto src = TestDataBuffer<float, N>(RandomFloat()); + PcmVolume pv; + CPPUNIT_ASSERT(pv.Open(SampleFormat::FLOAT, IgnoreError())); - float dest[N]; + constexpr size_t N = 256; + static float zero[N]; + const auto _src = TestDataBuffer<float, N>(RandomFloat()); + const ConstBuffer<void> src(_src, sizeof(_src)); - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::FLOAT, 0)); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, zero, sizeof(zero))); + pv.SetVolume(0); + auto dest = pv.Apply(src); + CPPUNIT_ASSERT_EQUAL(src.size, dest.size); + CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, zero, sizeof(zero))); - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::FLOAT, PCM_VOLUME_1)); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, src, sizeof(src))); + pv.SetVolume(PCM_VOLUME_1); + dest = pv.Apply(src); + CPPUNIT_ASSERT_EQUAL(src.size, dest.size); + CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, src.data, src.size)); - std::copy(src.begin(), src.end(), dest); - CPPUNIT_ASSERT_EQUAL(true, - pcm_volume(dest, sizeof(dest), - SampleFormat::FLOAT, - PCM_VOLUME_1 / 2)); + pv.SetVolume(PCM_VOLUME_1 / 2); + dest = pv.Apply(src); + CPPUNIT_ASSERT_EQUAL(src.size, dest.size); + const auto _dest = ConstBuffer<float>::FromVoid(dest); for (unsigned i = 0; i < N; ++i) - CPPUNIT_ASSERT_DOUBLES_EQUAL(src[i] / 2, dest[i], 1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(_src[i] / 2, _dest.data[i], 1); + + pv.Close(); } diff --git a/test/test_vorbis_encoder.cxx b/test/test_vorbis_encoder.cxx index 1d95f6deb..bb8731c2f 100644 --- a/test/test_vorbis_encoder.cxx +++ b/test/test_vorbis_encoder.cxx @@ -24,6 +24,7 @@ #include "ConfigData.hxx" #include "stdbin.h" #include "tag/Tag.hxx" +#include "tag/TagBuilder.hxx" #include "util/Error.hxx" #include <stddef.h> @@ -81,8 +82,13 @@ main(gcc_unused int argc, gcc_unused char **argv) encoder_to_stdout(*encoder); Tag tag; - tag.AddItem(TAG_ARTIST, "Foo"); - tag.AddItem(TAG_TITLE, "Bar"); + + { + TagBuilder tag_builder; + tag_builder.AddItem(TAG_ARTIST, "Foo"); + tag_builder.AddItem(TAG_TITLE, "Bar"); + tag_builder.Commit(tag); + } success = encoder_tag(encoder, &tag, IgnoreError()); assert(success); diff --git a/test/visit_archive.cxx b/test/visit_archive.cxx index 6e66c4696..bd890de4f 100644 --- a/test/visit_archive.cxx +++ b/test/visit_archive.cxx @@ -35,16 +35,6 @@ #include <unistd.h> #include <stdlib.h> -static void -my_log_func(const gchar *log_domain, gcc_unused GLogLevelFlags log_level, - const gchar *message, gcc_unused gpointer user_data) -{ - if (log_domain != NULL) - g_printerr("%s: %s\n", log_domain, message); - else - g_printerr("%s\n", message); -} - class MyArchiveVisitor final : public ArchiveVisitor { public: virtual void VisitArchiveEntry(const char *path_utf8) override { @@ -71,8 +61,6 @@ main(int argc, char **argv) g_thread_init(NULL); #endif - g_log_set_default_handler(my_log_func, NULL); - /* initialize MPD */ config_global_init(); |