Fixed Media Player behavior

This commit is contained in:
brunoherbelin
2020-03-14 10:55:26 +01:00
parent 1acfed925d
commit a435daa494
3 changed files with 37 additions and 27 deletions

View File

@@ -220,7 +220,7 @@ bool ImGuiToolkit::TimelineSlider(const char* label, guint64 *time, guint64 begi
bool value_changed = ImGui::SliderBehavior(slider_bbox, id, ImGuiDataType_Float, &time_slider, &time_zero, bool value_changed = ImGui::SliderBehavior(slider_bbox, id, ImGuiDataType_Float, &time_slider, &time_zero,
&time_end, "%.2f", 1.f, ImGuiSliderFlags_None, &grab_bb); &time_end, "%.2f", 1.f, ImGuiSliderFlags_None, &grab_bb);
if (value_changed){ if (value_changed){
//ImGuiToolkit::Log("slider %f %ld \n", time_slider, static_cast<guint64> ( static_cast<double>(time_slider) * static_cast<double>(duration) )); // g_print("slider %f %ld \n", time_slider, static_cast<guint64> ( static_cast<double>(time_slider) * static_cast<double>(duration) ));
*time = static_cast<guint64> ( static_cast<double>(time_slider) * static_cast<double>(duration) ); *time = static_cast<guint64> ( static_cast<double>(time_slider) * static_cast<double>(duration) );
} }

View File

@@ -365,8 +365,13 @@ void MediaPlayer::SeekTo(GstClockTime pos)
if (!seekable) if (!seekable)
return; return;
// remember pos
GstClockTime previous_pos = Position();
// apply seek
GstClockTime target = CLAMP(pos, 0, duration); GstClockTime target = CLAMP(pos, 0, duration);
execute_seek_command(target); execute_seek_command(target);
} }
void MediaPlayer::FastForward() void MediaPlayer::FastForward()
@@ -467,7 +472,7 @@ void MediaPlayer::execute_seek_command(GstClockTime target)
else if ( ABS_DIFF(target, Position()) < frame_duration) { else if ( ABS_DIFF(target, Position()) < frame_duration) {
// ignore request // ignore request
#ifdef MEDIA_PLAYER_DEBUG #ifdef MEDIA_PLAYER_DEBUG
Log::Info("%s: Media Player ignored seek to current position\n", id); Log::Info("%s: Media Player ignored seek to current position\n", id.c_str());
#endif #endif
return; return;
} }

View File

@@ -201,9 +201,11 @@ void drawMediaBackgound()
void drawMediaPlayer() void drawMediaPlayer()
{ {
if ( !testmedia.isOpen() ) if ( !testmedia.isOpen() )
return; return;
testmedia.Update(); testmedia.Update();
ImGui::Begin("Media Player"); ImGui::Begin("Media Player");
@@ -217,13 +219,14 @@ void drawMediaPlayer()
testmedia.Rewind(); testmedia.Rewind();
ImGui::SameLine(0, spacing); ImGui::SameLine(0, spacing);
static bool media_playing = testmedia.isPlaying(); // remember playing mode of the GUI
static bool media_playing_mode = testmedia.isPlaying();
// display buttons Play/Stop depending on current playing state // display buttons Play/Stop depending on current playing mode
if (media_playing) { if (media_playing_mode) {
if (ImGui::Button(ICON_FA_STOP " Stop")) if (ImGui::Button(ICON_FA_STOP " Stop"))
testmedia.Play(false); media_playing_mode = false;
ImGui::SameLine(0, spacing); ImGui::SameLine(0, spacing);
ImGui::PushButtonRepeat(true); ImGui::PushButtonRepeat(true);
@@ -234,7 +237,7 @@ void drawMediaPlayer()
else { else {
if (ImGui::Button(ICON_FA_PLAY " Play")) if (ImGui::Button(ICON_FA_PLAY " Play"))
testmedia.Play(true); media_playing_mode = true;
ImGui::SameLine(0, spacing); ImGui::SameLine(0, spacing);
ImGui::PushButtonRepeat(true); ImGui::PushButtonRepeat(true);
@@ -271,30 +274,32 @@ void drawMediaPlayer()
} }
guint64 current_t = testmedia.Position(); guint64 current_t = testmedia.Position();
guint64 t = current_t; guint64 seek_t = current_t;
guint64 begin = testmedia.Duration() / 5; guint64 begin = testmedia.Duration() / 5;
guint64 end = 4 * testmedia.Duration() / 5; guint64 end = 4 * testmedia.Duration() / 5;
static bool slider_was_pressed = false; bool slider_pressed = ImGuiToolkit::TimelineSlider( "timeline", &seek_t, begin, end,
bool slider_pressed = ImGuiToolkit::TimelineSlider( "timeline", &t, begin, end,
testmedia.Duration(), testmedia.FrameDuration()); testmedia.Duration(), testmedia.FrameDuration());
// change status only // if the seek target time is different from the current position time
if (slider_pressed != slider_was_pressed) { // (i.e. the difference is less than one frame)
if ( ABS_DIFF (current_t, seek_t) > testmedia.FrameDuration() ) {
// if was playing, pause during slider press // request seek (ASYNC)
if ( media_playing ) testmedia.SeekTo(seek_t);
testmedia.Play( !slider_pressed ); }
slider_was_pressed = slider_pressed; // play/stop command should be following the playing mode (buttons)
// AND force to stop when the slider is pressed
bool media_play = media_playing_mode & (!slider_pressed);
// apply play action to media only if status should change
// NB: The seek command performed an ASYNC state change, but
// gst_element_get_state called in isPlaying() will wait for the state change to complete.
if ( testmedia.isPlaying() != media_play ) {
testmedia.Play( media_play );
} }
// normal update of media status
if (!slider_was_pressed)
media_playing = testmedia.isPlaying();
// seek only if position is new
if (current_t != t)
testmedia.SeekTo(t);
// display info // display info
ImGui::Text("Dimension %d x %d", testmedia.Width(), testmedia.Height()); ImGui::Text("Dimension %d x %d", testmedia.Width(), testmedia.Height());
@@ -337,8 +342,8 @@ int main(int, char**)
// testmedia.Open("file:///home/bhbn/Videos/balls.gif"); // testmedia.Open("file:///home/bhbn/Videos/balls.gif");
// testmedia.Open("file:///home/bhbn/Videos/SIGGRAPH92_1.avi"); // testmedia.Open("file:///home/bhbn/Videos/SIGGRAPH92_1.avi");
// testmedia.Open("file:///home/bhbn/Videos/fish.mp4"); // testmedia.Open("file:///home/bhbn/Videos/fish.mp4");
testmedia.Open("file:///home/bhbn/Videos/iss.mov"); // testmedia.Open("file:///home/bhbn/Videos/iss.mov");
// testmedia.Open("file:///home/bhbn/Videos/TearsOfSteel_720p_h265.mkv"); testmedia.Open("file:///home/bhbn/Videos/TearsOfSteel_720p_h265.mkv");
// testmedia.Open("file:///home/bhbn/Videos/TestFormats/_h264GoldenLamps.mkv"); // testmedia.Open("file:///home/bhbn/Videos/TestFormats/_h264GoldenLamps.mkv");
// testmedia.Open("file:///home/bhbn/Videos/TestEncoding/vpxvp9high.webm"); // testmedia.Open("file:///home/bhbn/Videos/TestEncoding/vpxvp9high.webm");
@@ -347,8 +352,8 @@ int main(int, char**)
Rendering::AddDrawCallback(drawMediaPlayer); Rendering::AddDrawCallback(drawMediaPlayer);
// 2 // 2
// testmedia2.Open("file:///home/bhbn/Videos/iss.mov"); testmedia2.Open("file:///home/bhbn/Videos/iss.mov");
testmedia2.Open("file:///home/bhbn/Images/svg/drawing.svg"); // testmedia2.Open("file:///home/bhbn/Images/svg/drawing.svg");
// testmedia2.Open("file:///home/bhbn/Videos/Upgrade.2018.720p.AMZN.WEB-DL.DDP5.1.H.264-NTG.m4v"); // testmedia2.Open("file:///home/bhbn/Videos/Upgrade.2018.720p.AMZN.WEB-DL.DDP5.1.H.264-NTG.m4v");
testmedia2.Play(true); testmedia2.Play(true);
// create our geometries // create our geometries