[M120 Migration][MM][CAPI] Fix the logic for media using capi player.
[platform/framework/web/chromium-efl.git] / tizen_src / chromium_impl / media / filters / media_player_bridge_capi_tv.cc
1 // Copyright 2023 Samsung Electronics Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/filters/media_player_bridge_capi_tv.h"
6
7 #include "tizen_src/chromium_impl/media/filters/media_player_tizen_client.h"
8
9 namespace {
10 const int kSeekableTimeUpdateInterval = 500;
11 }
12
13 namespace media {
14
15 MediaPlayerBridgeCapiTV::MediaPlayerBridgeCapiTV(const GURL& url,
16                                                  std::string& user_agent,
17                                                  double volume)
18     : MediaPlayerBridgeCapi(url, user_agent, volume), weak_factory_(this) {
19   LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__;
20 }
21
22 MediaPlayerBridgeCapiTV::~MediaPlayerBridgeCapiTV() {
23   LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__;
24   weak_factory_.InvalidateWeakPtrs();
25 }
26
27 void MediaPlayerBridgeCapiTV::SetContentMimeType(const std::string& mime_type) {
28   LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__
29             << " mime_type : " << mime_type;
30   mime_type_ = mime_type;
31 }
32
33 void MediaPlayerBridgeCapiTV::Prepare() {
34   MediaPlayerBridgeCapi::Prepare();
35 }
36
37 void MediaPlayerBridgeCapiTV::Release() {
38   StopSeekableTimeUpdateTimer();
39   MediaPlayerBridgeCapi::Release();
40 }
41
42 bool MediaPlayerBridgeCapiTV::Play() {
43   if (!MediaPlayerBridgeCapi::Play())
44     return false;
45   return true;
46 }
47
48 void MediaPlayerBridgeCapiTV::PlaybackCompleteUpdate() {
49   MediaPlayerBridgeCapi::PlaybackCompleteUpdate();
50 }  // namespace media
51
52 void MediaPlayerBridgeCapiTV::PlayerPrepared() {
53   MediaPlayerBridgeCapi::PlayerPrepared();
54
55   GetAdaptiveStreamingInfo();
56   if (/*!blink::IsHbbTV() && */is_live_stream_) {
57     UpdateSeekableTime();
58     StartSeekableTimeUpdateTimer();
59   }
60 }
61
62 void MediaPlayerBridgeCapiTV::StartSeekableTimeUpdateTimer() {
63   if (!seekable_time_update_timer_.IsRunning()) {
64     seekable_time_update_timer_.Start(
65         FROM_HERE, base::Milliseconds(kSeekableTimeUpdateInterval), this,
66         &MediaPlayerBridgeCapiTV::OnSeekableTimeUpdateTimerFired);
67   }
68 }
69
70 void MediaPlayerBridgeCapiTV::StopSeekableTimeUpdateTimer() {
71   if (seekable_time_update_timer_.IsRunning())
72     seekable_time_update_timer_.Stop();
73 }
74
75 void MediaPlayerBridgeCapiTV::OnSeekableTimeUpdateTimerFired() {
76   UpdateSeekableTime();
77 }
78
79 void MediaPlayerBridgeCapiTV::GetAdaptiveStreamingInfo() {
80   int ret = player_get_adaptive_streaming_info(player_, &is_live_stream_,
81                                                PLAYER_ADAPTIVE_INFO_IS_LIVE);
82   if (ret != PLAYER_ERROR_NONE || is_live_stream_) {
83     LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__
84               << " A live stream.";
85   }
86 }
87
88 void MediaPlayerBridgeCapiTV::UpdateSeekableTime() {
89   if (GetPlayerState() == PLAYER_STATE_NONE || !is_live_stream_)
90     return;
91
92   int64_t min = 0, max = 0;
93   if (GetLiveStreamingDuration(&min, &max)) {
94     if (min_seekable_time_.InMilliseconds() != min ||
95         max_seekable_time_.InMilliseconds() != max) {
96       min_seekable_time_ = base::Milliseconds(min);
97       max_seekable_time_ = base::Milliseconds(max);
98       if (GetMediaPlayerClient())
99       GetMediaPlayerClient()->OnSeekableTimeChange(
100           min_seekable_time_, max_seekable_time_, is_live_stream_);
101     }
102
103     return;
104   }
105
106   GetAdaptiveStreamingInfo();
107   if (!is_live_stream_) {
108     LOG(INFO) << "(" << static_cast<void*>(this) << ") " << __func__
109               << " changed to static stream";
110     if (GetMediaPlayerClient())
111       GetMediaPlayerClient()->OnSeekableTimeChange({}, {}, is_live_stream_);
112     MediaPlayerBridgeCapi::UpdateDuration();
113   }
114 }
115
116 bool MediaPlayerBridgeCapiTV::GetLiveStreamingDuration(int64_t* min,
117                                                        int64_t* max) {
118   DCHECK(min);
119   DCHECK(max);
120   constexpr const size_t kDurationBufferSize = 64;
121   char live_duration[kDurationBufferSize] = {
122       0,
123   };
124   char* live_ptr = live_duration;
125   const auto err = player_get_adaptive_streaming_info(
126       player_, live_duration, PLAYER_ADAPTIVE_INFO_LIVE_DURATION);
127   if (err != PLAYER_ERROR_NONE) {
128     LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
129                << " |player_get_adaptive_streaming_info| failed " << err;
130     return false;
131   }
132
133   const std::string duration(live_duration);
134   const std::string::size_type delimiter = duration.find('|');
135   if (delimiter == std::string::npos) {
136     LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
137                << " Failed to find delimiter | in duration: " << duration;
138     return false;
139   }
140
141   const std::string duration_min = duration.substr(0, delimiter);
142   if (duration_min.empty()) {
143     LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
144                << " Failed empty min sub str: " << duration << ", "
145                << delimiter;
146     return false;
147   }
148
149   int64_t out_min = 0;
150   if (!base::StringToInt64(duration_min, &out_min)) {
151     LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
152                << " Failed to get min from duration: " << duration;
153     return false;
154   }
155
156   const std::string duration_max = duration.substr(delimiter + 1);
157   if (duration_max.empty()) {
158     LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
159                << " empty max sub str: " << duration << ", " << delimiter;
160     return false;
161   }
162
163   int64_t out_max = 0;
164   if (!base::StringToInt64(duration_max, &out_max)) {
165     LOG(ERROR) << "(" << static_cast<void*>(this) << ") " << __func__
166                << " Failed to get max from duration: " << duration;
167     return false;
168   }
169
170   *min = out_min;
171   *max = out_max;
172   return true;
173 }
174
175 }  // namespace media