diff options
author | Max Kellermann <max@duempel.org> | 2014-08-26 21:52:28 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-08-26 22:27:04 +0200 |
commit | 0c2d767f6fca030a8da3202c05b3eb80ba176ef1 (patch) | |
tree | 481acab9b4a1fadf8e208142bde510167b406d7e | |
parent | 02e697032f7f375e67acc349ada6d849edf51aa3 (diff) |
DecoderAPI: use std::chrono::duration for decoder_seek*()
For type safety and code readability.
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | src/Chrono.hxx | 66 | ||||
-rw-r--r-- | src/decoder/DecoderAPI.cxx | 24 | ||||
-rw-r--r-- | src/decoder/DecoderAPI.hxx | 15 | ||||
-rw-r--r-- | src/decoder/plugins/FfmpegDecoderPlugin.cxx | 14 | ||||
-rw-r--r-- | src/decoder/plugins/GmeDecoderPlugin.cxx | 2 | ||||
-rw-r--r-- | src/decoder/plugins/MadDecoderPlugin.cxx | 26 | ||||
-rw-r--r-- | src/decoder/plugins/ModplugDecoderPlugin.cxx | 2 | ||||
-rw-r--r-- | src/decoder/plugins/Mp4v2DecoderPlugin.cxx | 5 | ||||
-rw-r--r-- | src/decoder/plugins/SidplayDecoderPlugin.cxx | 4 | ||||
-rw-r--r-- | test/FakeDecoderAPI.cxx | 12 |
11 files changed, 110 insertions, 61 deletions
diff --git a/Makefile.am b/Makefile.am index 4bf87b3f1..89acbaa8d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -178,6 +178,7 @@ libmpd_a_SOURCES = \ src/TagStream.cxx src/TagStream.hxx \ src/TimePrint.cxx src/TimePrint.hxx \ src/mixer/Volume.cxx src/mixer/Volume.hxx \ + src/Chrono.hxx \ src/SongFilter.cxx src/SongFilter.hxx \ src/PlaylistFile.cxx src/PlaylistFile.hxx diff --git a/src/Chrono.hxx b/src/Chrono.hxx new file mode 100644 index 000000000..765522cf0 --- /dev/null +++ b/src/Chrono.hxx @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2003-2014 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_CHRONO_HXX +#define MPD_CHRONO_HXX + +#include <chrono> +#include <cstdint> + +/** + * A time stamp within a song. Granularity is 1 millisecond and the + * maximum value is about 49 days. + */ +class SongTime : public std::chrono::duration<std::uint32_t, std::milli> { + typedef std::chrono::duration<std::uint32_t, std::milli> Base; + typedef Base::rep rep; + +public: + SongTime() = default; + + template<typename T> + explicit constexpr SongTime(T t):Base(t) {} + + static constexpr SongTime FromS(unsigned s) { + return SongTime(rep(s) * 1000); + } + + static constexpr SongTime FromS(float s) { + return SongTime(rep(s * 1000)); + } + + static constexpr SongTime FromS(double s) { + return SongTime(rep(s * 1000)); + } + + static constexpr SongTime FromMS(rep ms) { + return SongTime(ms); + } + + constexpr rep ToMS() const { + return count(); + } + + constexpr unsigned ToScale(unsigned base) const { + // TODO: case to 64 bit to avoid integer overflow? + return count() * base / 1000; + } +}; + +#endif diff --git a/src/decoder/DecoderAPI.cxx b/src/decoder/DecoderAPI.cxx index 7349b00db..c9bdb46e8 100644 --- a/src/decoder/DecoderAPI.cxx +++ b/src/decoder/DecoderAPI.cxx @@ -204,37 +204,21 @@ decoder_command_finished(Decoder &decoder) dc.Unlock(); } -double decoder_seek_where(gcc_unused Decoder & decoder) +SongTime +decoder_seek_time(Decoder &decoder) { const DecoderControl &dc = decoder.dc; assert(dc.pipe != nullptr); if (decoder.initial_seek_running) - return dc.start_ms / 1000.; + return SongTime(dc.start_ms); assert(dc.command == DecoderCommand::SEEK); decoder.seeking = true; - return dc.seek_where; -} - -unsigned -decoder_seek_where_ms(Decoder &decoder) -{ - const DecoderControl &dc = decoder.dc; - - assert(dc.pipe != nullptr); - - if (decoder.initial_seek_running) - return dc.start_ms; - - assert(dc.command == DecoderCommand::SEEK); - - decoder.seeking = true; - - return unsigned(dc.seek_where * 1000); + return SongTime::FromS(dc.seek_where); } uint64_t diff --git a/src/decoder/DecoderAPI.hxx b/src/decoder/DecoderAPI.hxx index 2464ee6fa..ca33e26c7 100644 --- a/src/decoder/DecoderAPI.hxx +++ b/src/decoder/DecoderAPI.hxx @@ -37,6 +37,7 @@ #include "AudioFormat.hxx" #include "MixRampInfo.hxx" #include "config/ConfigData.hxx" +#include "Chrono.hxx" // IWYU pragma: end_exports @@ -84,21 +85,11 @@ decoder_command_finished(Decoder &decoder); * Call this when you have received the DecoderCommand::SEEK command. * * @param decoder the decoder object - * @return the destination position for the week - */ -gcc_pure -double -decoder_seek_where(Decoder &decoder); - -/** - * Call this when you have received the DecoderCommand::SEEK command. - * - * @param decoder the decoder object * @return the destination position for the seek in milliseconds */ gcc_pure -unsigned -decoder_seek_where_ms(Decoder &decoder); +SongTime +decoder_seek_time(Decoder &decoder); /** * Call this when you have received the DecoderCommand::SEEK command. diff --git a/src/decoder/plugins/FfmpegDecoderPlugin.cxx b/src/decoder/plugins/FfmpegDecoderPlugin.cxx index 2787d3705..e328da24b 100644 --- a/src/decoder/plugins/FfmpegDecoderPlugin.cxx +++ b/src/decoder/plugins/FfmpegDecoderPlugin.cxx @@ -210,11 +210,19 @@ time_from_ffmpeg(int64_t t, const AVRational time_base) / (double)1024; } +template<typename Ratio> +static constexpr AVRational +RatioToAVRational() +{ + return { Ratio::num, Ratio::den }; +} + gcc_const static int64_t -time_to_ffmpeg(double t_ms, const AVRational time_base) +time_to_ffmpeg(SongTime t, const AVRational time_base) { - return av_rescale_q(t_ms, (AVRational){1, 1000}, + return av_rescale_q(t.count(), + RatioToAVRational<SongTime::period>(), time_base); } @@ -547,7 +555,7 @@ ffmpeg_decode(Decoder &decoder, InputStream &input) if (cmd == DecoderCommand::SEEK) { int64_t where = - time_to_ffmpeg(decoder_seek_where_ms(decoder), + time_to_ffmpeg(decoder_seek_time(decoder), av_stream->time_base) + start_time_fallback(*av_stream); diff --git a/src/decoder/plugins/GmeDecoderPlugin.cxx b/src/decoder/plugins/GmeDecoderPlugin.cxx index e10efcbed..5588e2a1e 100644 --- a/src/decoder/plugins/GmeDecoderPlugin.cxx +++ b/src/decoder/plugins/GmeDecoderPlugin.cxx @@ -194,7 +194,7 @@ gme_file_decode(Decoder &decoder, Path path_fs) cmd = decoder_data(decoder, nullptr, buf, sizeof(buf), 0); if (cmd == DecoderCommand::SEEK) { - unsigned where = decoder_seek_where_ms(decoder); + unsigned where = decoder_seek_time(decoder).ToMS(); gme_err = gme_seek(emu, where); if (gme_err != nullptr) LogWarning(gme_domain, gme_err); diff --git a/src/decoder/plugins/MadDecoderPlugin.cxx b/src/decoder/plugins/MadDecoderPlugin.cxx index 5fa409435..7509faabb 100644 --- a/src/decoder/plugins/MadDecoderPlugin.cxx +++ b/src/decoder/plugins/MadDecoderPlugin.cxx @@ -67,6 +67,13 @@ static constexpr Domain mad_domain("mad"); static bool gapless_playback; +gcc_const +static SongTime +ToSongTime(mad_timer_t t) +{ + return SongTime::FromMS(mad_timer_count(t, MAD_UNITS_MILLISECONDS)); +} + static inline int32_t mad_fixed_to_24_sample(mad_fixed_t sample) { @@ -116,8 +123,8 @@ struct MadDecoder { unsigned char input_buffer[READ_BUFFER_SIZE]; int32_t output_buffer[MP3_DATA_OUTPUT_BUFFER_SIZE]; float total_time; - unsigned elapsed_time; - unsigned seek_where; + SongTime elapsed_time; + SongTime seek_time; enum muteframe mute_frame; long *frame_offsets; mad_timer_t *times; @@ -159,7 +166,7 @@ struct MadDecoder { bool DecodeFirstFrame(Tag **tag); gcc_pure - long TimeToFrame(unsigned t) const; + long TimeToFrame(SongTime t) const; void UpdateTimerNextFrame(); @@ -847,13 +854,12 @@ mad_decoder_total_file_time(InputStream &is) } long -MadDecoder::TimeToFrame(unsigned t) const +MadDecoder::TimeToFrame(SongTime t) const { unsigned long i; for (i = 0; i < highest_frame; ++i) { - unsigned frame_time = - mad_timer_count(times[i], MAD_UNITS_MILLISECONDS); + auto frame_time = ToSongTime(times[i]); if (frame_time >= t) break; } @@ -884,7 +890,7 @@ MadDecoder::UpdateTimerNextFrame() timer = times[current_frame]; current_frame++; - elapsed_time = mad_timer_count(timer, MAD_UNITS_MILLISECONDS); + elapsed_time = ToSongTime(timer); } DecoderCommand @@ -980,7 +986,7 @@ MadDecoder::Read() mute_frame = MUTEFRAME_NONE; break; case MUTEFRAME_SEEK: - if (elapsed_time >= seek_where) + if (elapsed_time >= seek_time) mute_frame = MUTEFRAME_NONE; break; case MUTEFRAME_NONE: @@ -989,7 +995,7 @@ MadDecoder::Read() assert(input_stream.IsSeekable()); unsigned long j = - TimeToFrame(decoder_seek_where_ms(*decoder)); + TimeToFrame(decoder_seek_time(*decoder)); if (j < highest_frame) { if (Seek(frame_offsets[j])) { current_frame = j; @@ -997,7 +1003,7 @@ MadDecoder::Read() } else decoder_seek_error(*decoder); } else { - seek_where = decoder_seek_where_ms(*decoder); + seek_time = decoder_seek_time(*decoder); mute_frame = MUTEFRAME_SEEK; decoder_command_finished(*decoder); } diff --git a/src/decoder/plugins/ModplugDecoderPlugin.cxx b/src/decoder/plugins/ModplugDecoderPlugin.cxx index 8c56f34c1..c88249fa5 100644 --- a/src/decoder/plugins/ModplugDecoderPlugin.cxx +++ b/src/decoder/plugins/ModplugDecoderPlugin.cxx @@ -166,7 +166,7 @@ mod_decode(Decoder &decoder, InputStream &is) 0); if (cmd == DecoderCommand::SEEK) { - ModPlug_Seek(f, decoder_seek_where_ms(decoder)); + ModPlug_Seek(f, decoder_seek_time(decoder).ToMS()); decoder_command_finished(decoder); } diff --git a/src/decoder/plugins/Mp4v2DecoderPlugin.cxx b/src/decoder/plugins/Mp4v2DecoderPlugin.cxx index e968be20b..ef5284437 100644 --- a/src/decoder/plugins/Mp4v2DecoderPlugin.cxx +++ b/src/decoder/plugins/Mp4v2DecoderPlugin.cxx @@ -165,9 +165,8 @@ mp4_file_decode(Decoder &mpd_decoder, Path path_fs) unsigned int data_length = 0; if (cmd == DecoderCommand::SEEK) { - const unsigned offset_ms = - decoder_seek_where_ms(mpd_decoder); - const MP4Timestamp offset = (offset_ms * scale) / 1000; + const MP4Timestamp offset = + decoder_seek_time(mpd_decoder).ToScale(scale); sample = MP4GetSampleIdFromTime(handle, track, offset, false); diff --git a/src/decoder/plugins/SidplayDecoderPlugin.cxx b/src/decoder/plugins/SidplayDecoderPlugin.cxx index e3e3b8d96..b0a398f56 100644 --- a/src/decoder/plugins/SidplayDecoderPlugin.cxx +++ b/src/decoder/plugins/SidplayDecoderPlugin.cxx @@ -314,8 +314,8 @@ sidplay_file_decode(Decoder &decoder, Path path_fs) if (cmd == DecoderCommand::SEEK) { unsigned data_time = player.time(); - unsigned target_time = (unsigned) - (decoder_seek_where(decoder) * timebase); + unsigned target_time = + decoder_seek_time(decoder).ToScale(timebase); /* can't rewind so return to zero and seek forward */ if(target_time<data_time) { diff --git a/test/FakeDecoderAPI.cxx b/test/FakeDecoderAPI.cxx index 54e75eb49..2516e9e62 100644 --- a/test/FakeDecoderAPI.cxx +++ b/test/FakeDecoderAPI.cxx @@ -55,16 +55,10 @@ decoder_command_finished(gcc_unused Decoder &decoder) { } -double -decoder_seek_where(gcc_unused Decoder &decoder) +SongTime +decoder_seek_time(gcc_unused Decoder &decoder) { - return 1.0; -} - -unsigned -decoder_seek_where_ms(gcc_unused Decoder &decoder) -{ - return 1; + return SongTime(); } uint64_t |