From dd294a880323ac33a465a39874242ea2d5a6b8cb Mon Sep 17 00:00:00 2001 From: Hidenori Matsubayashi Date: Thu, 5 Aug 2021 07:55:18 +0900 Subject: [PATCH] [video_player] Improve error handling (#23) Improved an error handling when getting current position. --- .../video_player/elinux/gst_video_player.cc | 10 +++++++++- .../elinux/video_player_elinux_plugin.cc | 19 +++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/packages/video_player/elinux/gst_video_player.cc b/packages/video_player/elinux/gst_video_player.cc index 0a9e303..ecf57d1 100644 --- a/packages/video_player/elinux/gst_video_player.cc +++ b/packages/video_player/elinux/gst_video_player.cc @@ -96,9 +96,14 @@ bool GstVideoPlayer::SetPlaybackRate(double rate) { return false; } + auto position = GetCurrentPosition(); + if (position < 0) { + return false; + } + if (!gst_element_seek(gst_.pipeline, rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_SET, - GetCurrentPosition() * GST_MSECOND, GST_SEEK_TYPE_SET, + position * GST_MSECOND, GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE)) { std::cerr << "Failed to set playback rate to " << rate << " (gst_element_seek failed)" << std::endl; @@ -138,8 +143,11 @@ int64_t GstVideoPlayer::GetDuration() { int64_t GstVideoPlayer::GetCurrentPosition() { gint64 position = 0; + + // Sometimes we get an error when playing streaming videos. if (!gst_element_query_position(gst_.pipeline, GST_FORMAT_TIME, &position)) { std::cerr << "Failed to get current position" << std::endl; + return -1; } // TODO: We need to handle this code in the proper plase. diff --git a/packages/video_player/elinux/video_player_elinux_plugin.cc b/packages/video_player/elinux/video_player_elinux_plugin.cc index df8fc39..b43ffdb 100644 --- a/packages/video_player/elinux/video_player_elinux_plugin.cc +++ b/packages/video_player/elinux/video_player_elinux_plugin.cc @@ -484,12 +484,19 @@ void VideoPlayerPlugin::HandlePositionMethodCall( flutter::EncodableMap result; if (players_.find(texture_id) != players_.end()) { - PositionMessage send_message; - send_message.SetTextureId(texture_id); - send_message.SetPosition( - players_[texture_id]->player->GetCurrentPosition()); - result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), - send_message.ToMap()); + auto position = players_[texture_id]->player->GetCurrentPosition(); + if (position < 0) { + auto error_message = "Failed to get current position with texture id: " + + std::to_string(texture_id); + result.emplace(flutter::EncodableValue(kEncodableMapkeyError), + flutter::EncodableValue(WrapError(error_message))); + } else { + PositionMessage send_message; + send_message.SetTextureId(texture_id); + send_message.SetPosition(position); + result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + send_message.ToMap()); + } } else { auto error_message = "Couldn't find the player with texture id: " + std::to_string(texture_id);