From 9675068706acadb9e6a5dc3ef483271b25e9a165 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 1 Jan 2018 16:15:32 -0800 Subject: Print the running time and duration in alffplay --- examples/alffplay.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'examples/alffplay.cpp') diff --git a/examples/alffplay.cpp b/examples/alffplay.cpp index 8c5fd454..b507101b 100644 --- a/examples/alffplay.cpp +++ b/examples/alffplay.cpp @@ -297,6 +297,8 @@ struct MovieState { nanoseconds getMasterClock(); + nanoseconds getDuration(); + int streamComponentOpen(int stream_index); int parse_handler(); }; @@ -1168,6 +1170,9 @@ nanoseconds MovieState::getMasterClock() return getClock(); } +nanoseconds MovieState::getDuration() +{ return std::chrono::duration>(mFormatCtx->duration); } + int MovieState::streamComponentOpen(int stream_index) { if(stream_index < 0 || (unsigned int)stream_index >= mFormatCtx->nb_streams) @@ -1348,6 +1353,35 @@ int MovieState::parse_handler() return 0; } + +// Helper class+method to print the time with human-readable formatting. +struct PrettyTime { + seconds_d64 mTime; +}; +inline std::ostream &operator<<(std::ostream &os, const PrettyTime &rhs) +{ + using hours = std::chrono::hours; + using minutes = std::chrono::minutes; + using std::chrono::duration_cast; + + seconds t = duration_cast(rhs.mTime); + if(t.count() < 0) + { + os << '-'; + t *= -1; + } + + // Only handle up to hour formatting + if(t >= hours(1)) + os << duration_cast(t).count() << 'h' << std::setfill('0') << std::setw(2) + << (duration_cast(t).count() % 60) << 'm'; + else + os << duration_cast(t).count() << 'm' << std::setfill('0'); + os << std::setw(2) << (duration_cast(t).count() % 60) << 's' << std::setw(0) + << std::setfill(' '); + return os; +} + } // namespace @@ -1477,8 +1511,14 @@ int main(int argc, char *argv[]) Next, Quit } eom_action = EomAction::Next; SDL_Event event; - while(SDL_WaitEvent(&event) == 1) + while(1) { + int have_evt = SDL_WaitEventTimeout(&event, 10); + + std::cout<< "\r "<getMasterClock()}<<" / "<< + PrettyTime{movState->getDuration()} <