OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/android/media_codec_player.h" | 5 #include "media/base/android/media_codec_player.h" |
6 | 6 |
| 7 #include "base/barrier_closure.h" |
7 #include "base/bind.h" | 8 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/threading/thread.h" |
| 13 #include "media/base/android/media_codec_audio_decoder.h" |
| 14 #include "media/base/android/media_codec_video_decoder.h" |
| 15 #include "media/base/android/media_player_manager.h" |
| 16 #include "media/base/buffers.h" |
11 | 17 |
12 #define RUN_ON_MEDIA_THREAD(METHOD, ...) \ | 18 #define RUN_ON_MEDIA_THREAD(METHOD, ...) \ |
13 do { \ | 19 do { \ |
14 if (!GetMediaTaskRunner()->BelongsToCurrentThread()) { \ | 20 if (!GetMediaTaskRunner()->BelongsToCurrentThread()) { \ |
15 GetMediaTaskRunner()->PostTask( \ | 21 DCHECK(ui_task_runner_->BelongsToCurrentThread()); \ |
16 FROM_HERE, \ | 22 GetMediaTaskRunner()->PostTask( \ |
17 base::Bind(&MediaCodecPlayer:: METHOD, weak_this_, ##__VA_ARGS__)); \ | 23 FROM_HERE, base::Bind(&MediaCodecPlayer::METHOD, media_weak_this_, \ |
18 return; \ | 24 ##__VA_ARGS__)); \ |
19 } \ | 25 return; \ |
20 } while(0) | 26 } \ |
21 | 27 } while (0) |
22 | 28 |
23 namespace media { | 29 namespace media { |
24 | 30 |
25 class MediaThread : public base::Thread { | 31 class MediaThread : public base::Thread { |
26 public: | 32 public: |
27 MediaThread() : base::Thread("BrowserMediaThread") { | 33 MediaThread() : base::Thread("BrowserMediaThread") { |
28 Start(); | 34 Start(); |
29 } | 35 } |
30 }; | 36 }; |
31 | 37 |
32 // Create media thread | 38 // Create media thread |
33 base::LazyInstance<MediaThread>::Leaky | 39 base::LazyInstance<MediaThread>::Leaky |
34 g_media_thread = LAZY_INSTANCE_INITIALIZER; | 40 g_media_thread = LAZY_INSTANCE_INITIALIZER; |
35 | 41 |
36 | 42 |
37 scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner() { | 43 scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner() { |
38 return g_media_thread.Pointer()->task_runner(); | 44 return g_media_thread.Pointer()->task_runner(); |
39 } | 45 } |
40 | 46 |
41 // MediaCodecPlayer implementation. | 47 // MediaCodecPlayer implementation. |
42 | 48 |
43 MediaCodecPlayer::MediaCodecPlayer( | 49 MediaCodecPlayer::MediaCodecPlayer( |
44 int player_id, | 50 int player_id, |
45 MediaPlayerManager* manager, | 51 base::WeakPtr<MediaPlayerManager> manager, |
46 const RequestMediaResourcesCB& request_media_resources_cb, | 52 const RequestMediaResourcesCB& request_media_resources_cb, |
47 scoped_ptr<DemuxerAndroid> demuxer, | 53 scoped_ptr<DemuxerAndroid> demuxer, |
48 const GURL& frame_url) | 54 const GURL& frame_url) |
49 : MediaPlayerAndroid(player_id, | 55 : MediaPlayerAndroid(player_id, |
50 manager, | 56 manager.get(), |
51 request_media_resources_cb, | 57 request_media_resources_cb, |
52 frame_url), | 58 frame_url), |
53 ui_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 59 ui_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
54 demuxer_(demuxer.Pass()), | 60 demuxer_(demuxer.Pass()), |
55 weak_factory_(this) { | 61 state_(STATE_PAUSED), |
56 // UI thread | 62 interpolator_(&default_tick_clock_), |
| 63 pending_start_(false), |
| 64 media_weak_factory_(this) { |
57 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 65 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
58 | 66 |
59 DVLOG(1) << "MediaCodecPlayer::MediaCodecPlayer: player_id:" << player_id; | 67 DVLOG(1) << "MediaCodecPlayer::MediaCodecPlayer: player_id:" << player_id; |
60 | 68 |
61 weak_this_ = weak_factory_.GetWeakPtr(); | 69 request_resources_cb_ = base::Bind(request_media_resources_cb_, player_id); |
| 70 |
| 71 completion_cb_ = |
| 72 base::Bind(&MediaPlayerManager::OnPlaybackComplete, manager, player_id); |
| 73 attach_listener_cb_ = base::Bind(&MediaPlayerAndroid::AttachListener, |
| 74 WeakPtrForUIThread(), nullptr); |
| 75 detach_listener_cb_ = |
| 76 base::Bind(&MediaPlayerAndroid::DetachListener, WeakPtrForUIThread()); |
| 77 metadata_changed_cb_ = base::Bind(&MediaPlayerAndroid::OnMediaMetadataChanged, |
| 78 WeakPtrForUIThread()); |
| 79 time_update_cb_ = |
| 80 base::Bind(&MediaPlayerAndroid::OnTimeUpdate, WeakPtrForUIThread()); |
| 81 |
| 82 media_weak_this_ = media_weak_factory_.GetWeakPtr(); |
62 | 83 |
63 // Finish initializaton on Media thread | 84 // Finish initializaton on Media thread |
64 GetMediaTaskRunner()->PostTask( | 85 GetMediaTaskRunner()->PostTask( |
65 FROM_HERE, base::Bind(&MediaCodecPlayer::Initialize, weak_this_)); | 86 FROM_HERE, base::Bind(&MediaCodecPlayer::Initialize, media_weak_this_)); |
66 } | 87 } |
67 | 88 |
68 MediaCodecPlayer::~MediaCodecPlayer() | 89 MediaCodecPlayer::~MediaCodecPlayer() |
69 { | 90 { |
70 // Media thread | |
71 DVLOG(1) << "MediaCodecPlayer::~MediaCodecPlayer"; | 91 DVLOG(1) << "MediaCodecPlayer::~MediaCodecPlayer"; |
72 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 92 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
73 } | 93 } |
74 | 94 |
75 void MediaCodecPlayer::Initialize() { | 95 void MediaCodecPlayer::Initialize() { |
76 // Media thread | |
77 DVLOG(1) << __FUNCTION__; | 96 DVLOG(1) << __FUNCTION__; |
78 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 97 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
79 | 98 |
| 99 interpolator_.SetUpperBound(base::TimeDelta()); |
| 100 |
| 101 CreateDecoders(); |
| 102 |
| 103 // This call might in turn call MediaCodecPlayer::OnDemuxerConfigsAvailable() |
| 104 // which propagates configs into decoders. Therefore CreateDecoders() should |
| 105 // be called first. |
80 demuxer_->Initialize(this); | 106 demuxer_->Initialize(this); |
81 } | 107 } |
82 | 108 |
83 // MediaPlayerAndroid implementation. | 109 // The implementation of MediaPlayerAndroid interface. |
84 | 110 |
85 void MediaCodecPlayer::DeleteOnCorrectThread() { | 111 void MediaCodecPlayer::DeleteOnCorrectThread() { |
86 // UI thread | |
87 DVLOG(1) << __FUNCTION__; | 112 DVLOG(1) << __FUNCTION__; |
88 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 113 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
89 | 114 |
90 // The listener-related portion of the base class has to be | 115 DetachListener(); |
91 // destroyed on UI thread. | 116 |
| 117 // The base class part that deals with MediaPlayerListener |
| 118 // has to be destroyed on UI thread. |
92 DestroyListenerOnUIThread(); | 119 DestroyListenerOnUIThread(); |
93 | 120 |
94 // Post deletion onto Media thread | 121 // Post deletion onto Media thread |
95 GetMediaTaskRunner()->DeleteSoon(FROM_HERE, this); | 122 GetMediaTaskRunner()->DeleteSoon(FROM_HERE, this); |
96 } | 123 } |
97 | 124 |
98 void MediaCodecPlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) { | 125 void MediaCodecPlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) { |
99 RUN_ON_MEDIA_THREAD(SetVideoSurface, base::Passed(&surface)); | 126 RUN_ON_MEDIA_THREAD(SetVideoSurface, base::Passed(&surface)); |
100 | 127 |
101 // Media thread | 128 DVLOG(1) << __FUNCTION__ << (surface.IsEmpty() ? " empty" : " non-empty"); |
102 DVLOG(1) << __FUNCTION__; | |
103 | 129 |
104 NOTIMPLEMENTED(); | 130 // I assume that if video decoder already has the surface, |
| 131 // there will be two calls: |
| 132 // (1) SetVideoSurface(0) |
| 133 // (2) SetVideoSurface(new_surface) |
| 134 video_decoder_->SetPendingSurface(surface.Pass()); |
| 135 |
| 136 if (video_decoder_->HasPendingSurface() && |
| 137 state_ == STATE_WAITING_FOR_SURFACE) { |
| 138 SetState(STATE_PLAYING); |
| 139 StartPlaybackDecoders(); |
| 140 } |
105 } | 141 } |
106 | 142 |
107 void MediaCodecPlayer::Start() { | 143 void MediaCodecPlayer::Start() { |
108 RUN_ON_MEDIA_THREAD(Start); | 144 RUN_ON_MEDIA_THREAD(Start); |
109 | 145 |
110 // Media thread | |
111 DVLOG(1) << __FUNCTION__; | 146 DVLOG(1) << __FUNCTION__; |
112 | 147 |
113 NOTIMPLEMENTED(); | 148 switch (state_) { |
| 149 case STATE_PAUSED: |
| 150 if (HasAudio() || HasVideo()) { |
| 151 SetState(STATE_PREFETCHING); |
| 152 StartPrefetchDecoders(); |
| 153 } else { |
| 154 SetState(STATE_WAITING_FOR_CONFIG); |
| 155 } |
| 156 break; |
| 157 case STATE_STOPPING: |
| 158 SetPendingStart(true); |
| 159 break; |
| 160 default: |
| 161 // Ignore |
| 162 break; |
| 163 } |
114 } | 164 } |
115 | 165 |
116 void MediaCodecPlayer::Pause(bool is_media_related_action) { | 166 void MediaCodecPlayer::Pause(bool is_media_related_action) { |
117 RUN_ON_MEDIA_THREAD(Pause, is_media_related_action); | 167 RUN_ON_MEDIA_THREAD(Pause, is_media_related_action); |
118 | 168 |
119 // Media thread | |
120 DVLOG(1) << __FUNCTION__; | 169 DVLOG(1) << __FUNCTION__; |
121 | 170 |
122 NOTIMPLEMENTED(); | 171 switch (state_) { |
| 172 case STATE_PREFETCHING: |
| 173 SetState(STATE_PAUSED); |
| 174 StopDecoders(); |
| 175 break; |
| 176 case STATE_WAITING_FOR_SURFACE: |
| 177 SetState(STATE_PAUSED); |
| 178 StopDecoders(); |
| 179 break; |
| 180 case STATE_PLAYING: |
| 181 SetState(STATE_STOPPING); |
| 182 RequestToStopDecoders(); |
| 183 break; |
| 184 default: |
| 185 // Ignore |
| 186 break; |
| 187 } |
123 } | 188 } |
124 | 189 |
125 void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp) { | 190 void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp) { |
126 RUN_ON_MEDIA_THREAD(SeekTo, timestamp); | 191 RUN_ON_MEDIA_THREAD(SeekTo, timestamp); |
127 | 192 |
128 // Media thread | |
129 DVLOG(1) << __FUNCTION__ << " " << timestamp; | 193 DVLOG(1) << __FUNCTION__ << " " << timestamp; |
130 | |
131 NOTIMPLEMENTED(); | 194 NOTIMPLEMENTED(); |
132 } | 195 } |
133 | 196 |
134 void MediaCodecPlayer::Release() { | 197 void MediaCodecPlayer::Release() { |
135 RUN_ON_MEDIA_THREAD(Release); | 198 RUN_ON_MEDIA_THREAD(Release); |
136 | 199 |
137 // Media thread | |
138 DVLOG(1) << __FUNCTION__; | 200 DVLOG(1) << __FUNCTION__; |
139 | 201 |
140 NOTIMPLEMENTED(); | 202 SetState(STATE_PAUSED); |
| 203 ReleaseDecoderResources(); |
141 } | 204 } |
142 | 205 |
143 void MediaCodecPlayer::SetVolume(double volume) { | 206 void MediaCodecPlayer::SetVolume(double volume) { |
144 RUN_ON_MEDIA_THREAD(SetVolume, volume); | 207 RUN_ON_MEDIA_THREAD(SetVolume, volume); |
145 | 208 |
146 // Media thread | |
147 DVLOG(1) << __FUNCTION__ << " " << volume; | 209 DVLOG(1) << __FUNCTION__ << " " << volume; |
148 | 210 audio_decoder_->SetVolume(volume); |
149 NOTIMPLEMENTED(); | |
150 } | 211 } |
151 | 212 |
152 int MediaCodecPlayer::GetVideoWidth() { | 213 int MediaCodecPlayer::GetVideoWidth() { |
153 // UI thread | |
154 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 214 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
155 | 215 return metadata_cache_.video_size.width(); |
156 NOTIMPLEMENTED(); | |
157 return 320; | |
158 } | 216 } |
159 | 217 |
160 int MediaCodecPlayer::GetVideoHeight() { | 218 int MediaCodecPlayer::GetVideoHeight() { |
161 // UI thread | |
162 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 219 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
163 | 220 return metadata_cache_.video_size.height(); |
164 NOTIMPLEMENTED(); | |
165 return 240; | |
166 } | 221 } |
167 | 222 |
168 base::TimeDelta MediaCodecPlayer::GetCurrentTime() { | 223 base::TimeDelta MediaCodecPlayer::GetCurrentTime() { |
169 // UI thread, Media thread | 224 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
170 NOTIMPLEMENTED(); | 225 return current_time_cache_; |
171 return base::TimeDelta(); | |
172 } | 226 } |
173 | 227 |
174 base::TimeDelta MediaCodecPlayer::GetDuration() { | 228 base::TimeDelta MediaCodecPlayer::GetDuration() { |
175 // UI thread | |
176 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 229 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
177 | 230 return metadata_cache_.duration; |
178 NOTIMPLEMENTED(); | |
179 return base::TimeDelta(); | |
180 } | 231 } |
181 | 232 |
182 bool MediaCodecPlayer::IsPlaying() { | 233 bool MediaCodecPlayer::IsPlaying() { |
183 // UI thread | |
184 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 234 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
185 NOTIMPLEMENTED(); | 235 return state_ == STATE_PLAYING; |
186 return false; | |
187 } | 236 } |
188 | 237 |
189 bool MediaCodecPlayer::CanPause() { | 238 bool MediaCodecPlayer::CanPause() { |
190 // UI thread | |
191 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 239 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
192 NOTIMPLEMENTED(); | 240 NOTIMPLEMENTED(); |
193 return false; | 241 return false; |
194 } | 242 } |
195 | 243 |
196 bool MediaCodecPlayer::CanSeekForward() { | 244 bool MediaCodecPlayer::CanSeekForward() { |
197 // UI thread | |
198 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 245 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
199 NOTIMPLEMENTED(); | 246 NOTIMPLEMENTED(); |
200 return false; | 247 return false; |
201 } | 248 } |
202 | 249 |
203 bool MediaCodecPlayer::CanSeekBackward() { | 250 bool MediaCodecPlayer::CanSeekBackward() { |
204 // UI thread | |
205 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 251 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
206 NOTIMPLEMENTED(); | 252 NOTIMPLEMENTED(); |
207 return false; | 253 return false; |
208 } | 254 } |
209 | 255 |
210 bool MediaCodecPlayer::IsPlayerReady() { | 256 bool MediaCodecPlayer::IsPlayerReady() { |
211 // UI thread | |
212 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 257 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
213 NOTIMPLEMENTED(); | 258 // This method is called to check whether it's safe to release the player when |
| 259 // the OS needs more resources. This class can be released at any time. |
214 return true; | 260 return true; |
215 } | 261 } |
216 | 262 |
217 void MediaCodecPlayer::SetCdm(BrowserCdm* cdm) { | 263 void MediaCodecPlayer::SetCdm(BrowserCdm* cdm) { |
218 // UI thread | |
219 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 264 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
220 NOTIMPLEMENTED(); | 265 NOTIMPLEMENTED(); |
221 } | 266 } |
222 | 267 |
223 // Callbacks from Demuxer. | 268 // Callbacks from Demuxer. |
224 | 269 |
225 void MediaCodecPlayer::OnDemuxerConfigsAvailable( | 270 void MediaCodecPlayer::OnDemuxerConfigsAvailable( |
226 const DemuxerConfigs& configs) { | 271 const DemuxerConfigs& configs) { |
227 // Media thread | 272 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
228 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 273 |
229 | 274 DVLOG(1) << __FUNCTION__; |
230 NOTIMPLEMENTED(); | 275 |
| 276 duration_ = configs.duration; |
| 277 |
| 278 SetDemuxerConfigs(configs); |
| 279 |
| 280 // Update cache and notify manager on UI thread |
| 281 gfx::Size video_size = HasVideo() ? configs.video_size : gfx::Size(); |
| 282 ui_task_runner_->PostTask( |
| 283 FROM_HERE, base::Bind(metadata_changed_cb_, duration_, video_size)); |
231 } | 284 } |
232 | 285 |
233 void MediaCodecPlayer::OnDemuxerDataAvailable(const DemuxerData& data) { | 286 void MediaCodecPlayer::OnDemuxerDataAvailable(const DemuxerData& data) { |
234 // Media thread | 287 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
235 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 288 |
236 NOTIMPLEMENTED(); | 289 DCHECK_LT(0u, data.access_units.size()); |
| 290 CHECK_GE(1u, data.demuxer_configs.size()); |
| 291 |
| 292 DVLOG(2) << "Player::" << __FUNCTION__; |
| 293 |
| 294 if (data.type == DemuxerStream::AUDIO) |
| 295 audio_decoder_->OnDemuxerDataAvailable(data); |
| 296 |
| 297 if (data.type == DemuxerStream::VIDEO) |
| 298 video_decoder_->OnDemuxerDataAvailable(data); |
237 } | 299 } |
238 | 300 |
239 void MediaCodecPlayer::OnDemuxerSeekDone( | 301 void MediaCodecPlayer::OnDemuxerSeekDone( |
240 base::TimeDelta actual_browser_seek_time) { | 302 base::TimeDelta actual_browser_seek_time) { |
241 // Media thread | 303 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
242 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 304 |
| 305 DVLOG(1) << __FUNCTION__ << " actual_time:" << actual_browser_seek_time; |
| 306 |
243 NOTIMPLEMENTED(); | 307 NOTIMPLEMENTED(); |
244 } | 308 } |
245 | 309 |
246 void MediaCodecPlayer::OnDemuxerDurationChanged( | 310 void MediaCodecPlayer::OnDemuxerDurationChanged( |
247 base::TimeDelta duration) { | 311 base::TimeDelta duration) { |
248 // Media thread | 312 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
249 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 313 DVLOG(1) << __FUNCTION__ << " duration:" << duration; |
250 NOTIMPLEMENTED(); | 314 |
251 } | 315 duration_ = duration; |
| 316 } |
| 317 |
| 318 // Events from Player, called on UI thread |
| 319 |
| 320 void MediaCodecPlayer::OnMediaMetadataChanged(base::TimeDelta duration, |
| 321 const gfx::Size& video_size) { |
| 322 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 323 |
| 324 if (duration != kNoTimestamp()) |
| 325 metadata_cache_.duration = duration; |
| 326 |
| 327 if (!video_size.IsEmpty()) |
| 328 metadata_cache_.video_size = video_size; |
| 329 |
| 330 manager()->OnMediaMetadataChanged(player_id(), metadata_cache_.duration, |
| 331 metadata_cache_.video_size.width(), |
| 332 metadata_cache_.video_size.height(), true); |
| 333 } |
| 334 |
| 335 void MediaCodecPlayer::OnTimeUpdate(base::TimeDelta current_timestamp, |
| 336 base::TimeTicks current_time_ticks) { |
| 337 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 338 |
| 339 current_time_cache_ = current_timestamp; |
| 340 manager()->OnTimeUpdate(player_id(), current_timestamp, current_time_ticks); |
| 341 } |
| 342 |
| 343 // Events from Decoders, called on Media thread |
| 344 |
| 345 void MediaCodecPlayer::RequestDemuxerData(DemuxerStream::Type stream_type) { |
| 346 DVLOG(2) << __FUNCTION__ << " streamType:" << stream_type; |
| 347 |
| 348 // Use this method instead of directly binding with |
| 349 // DemuxerAndroid::RequestDemuxerData() to avoid the race condition on |
| 350 // deletion: |
| 351 // 1. DeleteSoon is posted from UI to Media thread. |
| 352 // 2. RequestDemuxerData callback is posted from Decoder to Media thread. |
| 353 // 3. DeleteSoon arrives, we delete the player and detach from |
| 354 // BrowserDemuxerAndroid. |
| 355 // 4. RequestDemuxerData is processed by the media thread queue. Since the |
| 356 // weak_ptr was invalidated in (3), this is a no-op. If we used |
| 357 // DemuxerAndroid::RequestDemuxerData() it would arrive and will try to |
| 358 // call the client, but the client (i.e. this player) would not exist. |
| 359 demuxer_->RequestDemuxerData(stream_type); |
| 360 } |
| 361 |
| 362 void MediaCodecPlayer::OnPrefetchDone() { |
| 363 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 364 DVLOG(1) << __FUNCTION__; |
| 365 |
| 366 if (state_ != STATE_PREFETCHING) |
| 367 return; // Ignore |
| 368 |
| 369 if (!HasAudio() && !HasVideo()) { |
| 370 // No configuration at all after prefetching. |
| 371 // This is an error, initial configuration is expected |
| 372 // before the first data chunk. |
| 373 GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_); |
| 374 return; |
| 375 } |
| 376 |
| 377 if (HasVideo() && !HasPendingSurface()) { |
| 378 SetState(STATE_WAITING_FOR_SURFACE); |
| 379 return; |
| 380 } |
| 381 |
| 382 SetState(STATE_PLAYING); |
| 383 StartPlaybackDecoders(); |
| 384 } |
| 385 |
| 386 void MediaCodecPlayer::OnStopDone() { |
| 387 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 388 DVLOG(1) << __FUNCTION__; |
| 389 |
| 390 if (!(audio_decoder_->IsStopped() && video_decoder_->IsStopped())) |
| 391 return; // Wait until other stream is stopped |
| 392 |
| 393 // At this point decoder threads should not be running |
| 394 if (interpolator_.interpolating()) |
| 395 interpolator_.StopInterpolating(); |
| 396 |
| 397 switch (state_) { |
| 398 case STATE_STOPPING: |
| 399 if (HasPendingStart()) { |
| 400 SetPendingStart(false); |
| 401 SetState(STATE_PREFETCHING); |
| 402 StartPrefetchDecoders(); |
| 403 } else { |
| 404 SetState(STATE_PAUSED); |
| 405 } |
| 406 break; |
| 407 case STATE_PLAYING: |
| 408 // Unexpected stop means completion |
| 409 SetState(STATE_PAUSED); |
| 410 break; |
| 411 default: |
| 412 DVLOG(0) << __FUNCTION__ << " illegal state: " << AsString(state_); |
| 413 NOTREACHED(); |
| 414 break; |
| 415 } |
| 416 |
| 417 // DetachListener to UI thread |
| 418 ui_task_runner_->PostTask(FROM_HERE, detach_listener_cb_); |
| 419 |
| 420 if (AudioFinished() && VideoFinished()) |
| 421 ui_task_runner_->PostTask(FROM_HERE, completion_cb_); |
| 422 } |
| 423 |
| 424 void MediaCodecPlayer::OnError() { |
| 425 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 426 DVLOG(1) << __FUNCTION__; |
| 427 |
| 428 // STATE_ERROR blocks all events |
| 429 SetState(STATE_ERROR); |
| 430 |
| 431 ReleaseDecoderResources(); |
| 432 } |
| 433 |
| 434 void MediaCodecPlayer::OnStarvation(DemuxerStream::Type type) { |
| 435 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 436 DVLOG(1) << __FUNCTION__ << " stream type:" << type; |
| 437 |
| 438 if (state_ != STATE_PLAYING) |
| 439 return; // Ignore |
| 440 |
| 441 SetState(STATE_STOPPING); |
| 442 RequestToStopDecoders(); |
| 443 SetPendingStart(true); |
| 444 } |
| 445 |
| 446 void MediaCodecPlayer::OnTimeIntervalUpdate(DemuxerStream::Type type, |
| 447 base::TimeDelta now_playing, |
| 448 base::TimeDelta last_buffered) { |
| 449 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 450 |
| 451 interpolator_.SetBounds(now_playing, last_buffered); |
| 452 |
| 453 // Post to UI thread |
| 454 ui_task_runner_->PostTask(FROM_HERE, |
| 455 base::Bind(time_update_cb_, GetInterpolatedTime(), |
| 456 base::TimeTicks::Now())); |
| 457 } |
| 458 |
| 459 void MediaCodecPlayer::OnVideoCodecCreated() { |
| 460 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 461 |
| 462 // This callback requests resources by releasing other players. |
| 463 ui_task_runner_->PostTask(FROM_HERE, request_resources_cb_); |
| 464 } |
| 465 |
| 466 void MediaCodecPlayer::OnVideoResolutionChanged(const gfx::Size& size) { |
| 467 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 468 |
| 469 DVLOG(1) << __FUNCTION__ << " " << size.width() << "x" << size.height(); |
| 470 |
| 471 // Update cache and notify manager on UI thread |
| 472 ui_task_runner_->PostTask( |
| 473 FROM_HERE, base::Bind(metadata_changed_cb_, kNoTimestamp(), size)); |
| 474 } |
| 475 |
| 476 // State machine operations, called on Media thread |
| 477 |
| 478 void MediaCodecPlayer::SetState(PlayerState new_state) { |
| 479 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 480 |
| 481 DVLOG(1) << "SetState:" << AsString(state_) << " -> " << AsString(new_state); |
| 482 state_ = new_state; |
| 483 } |
| 484 |
| 485 void MediaCodecPlayer::SetPendingSurface(gfx::ScopedJavaSurface surface) { |
| 486 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 487 DVLOG(1) << __FUNCTION__; |
| 488 |
| 489 video_decoder_->SetPendingSurface(surface.Pass()); |
| 490 } |
| 491 |
| 492 bool MediaCodecPlayer::HasPendingSurface() { |
| 493 return video_decoder_->HasPendingSurface(); |
| 494 } |
| 495 |
| 496 void MediaCodecPlayer::SetPendingStart(bool need_to_start) { |
| 497 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 498 DVLOG(1) << __FUNCTION__ << ": " << need_to_start; |
| 499 pending_start_ = need_to_start; |
| 500 } |
| 501 |
| 502 bool MediaCodecPlayer::HasPendingStart() { |
| 503 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 504 return pending_start_; |
| 505 } |
| 506 |
| 507 bool MediaCodecPlayer::HasAudio() { |
| 508 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 509 return audio_decoder_->HasStream(); |
| 510 } |
| 511 |
| 512 bool MediaCodecPlayer::HasVideo() { |
| 513 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 514 return video_decoder_->HasStream(); |
| 515 } |
| 516 |
| 517 void MediaCodecPlayer::SetDemuxerConfigs(const DemuxerConfigs& configs) { |
| 518 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 519 DVLOG(1) << __FUNCTION__ << " " << configs; |
| 520 |
| 521 DCHECK(audio_decoder_); |
| 522 DCHECK(video_decoder_); |
| 523 |
| 524 // At least one valid codec must be present. |
| 525 DCHECK(configs.audio_codec != kUnknownAudioCodec || |
| 526 configs.video_codec != kUnknownVideoCodec); |
| 527 |
| 528 if (configs.audio_codec != kUnknownAudioCodec) |
| 529 audio_decoder_->SetDemuxerConfigs(configs); |
| 530 |
| 531 if (configs.video_codec != kUnknownVideoCodec) |
| 532 video_decoder_->SetDemuxerConfigs(configs); |
| 533 |
| 534 if (state_ == STATE_WAITING_FOR_CONFIG) { |
| 535 SetState(STATE_PREFETCHING); |
| 536 StartPrefetchDecoders(); |
| 537 } |
| 538 } |
| 539 |
| 540 void MediaCodecPlayer::StartPrefetchDecoders() { |
| 541 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 542 DVLOG(1) << __FUNCTION__; |
| 543 |
| 544 bool do_audio = false; |
| 545 bool do_video = false; |
| 546 int count = 0; |
| 547 if (!AudioFinished()) { |
| 548 do_audio = true; |
| 549 ++count; |
| 550 } |
| 551 if (!VideoFinished()) { |
| 552 do_video = true; |
| 553 ++count; |
| 554 } |
| 555 |
| 556 DCHECK_LT(0, count); // at least one decoder should be active |
| 557 |
| 558 base::Closure prefetch_cb = base::BarrierClosure( |
| 559 count, base::Bind(&MediaCodecPlayer::OnPrefetchDone, media_weak_this_)); |
| 560 |
| 561 if (do_audio) |
| 562 audio_decoder_->Prefetch(prefetch_cb); |
| 563 |
| 564 if (do_video) |
| 565 video_decoder_->Prefetch(prefetch_cb); |
| 566 } |
| 567 |
| 568 void MediaCodecPlayer::StartPlaybackDecoders() { |
| 569 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 570 DVLOG(1) << __FUNCTION__; |
| 571 |
| 572 // Configure all streams before the start since |
| 573 // we may discover that browser seek is required. |
| 574 |
| 575 bool do_audio = !AudioFinished(); |
| 576 bool do_video = !VideoFinished(); |
| 577 |
| 578 // If there is nothing to play, the state machine should determine |
| 579 // this at the prefetch state and never call this method. |
| 580 DCHECK(do_audio || do_video); |
| 581 |
| 582 if (do_audio) { |
| 583 MediaCodecDecoder::ConfigStatus status = audio_decoder_->Configure(); |
| 584 if (status != MediaCodecDecoder::CONFIG_OK) { |
| 585 GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_); |
| 586 return; |
| 587 } |
| 588 } |
| 589 |
| 590 if (do_video) { |
| 591 MediaCodecDecoder::ConfigStatus status = video_decoder_->Configure(); |
| 592 if (status != MediaCodecDecoder::CONFIG_OK) { |
| 593 GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_); |
| 594 return; |
| 595 } |
| 596 } |
| 597 |
| 598 // At this point decoder threads should not be running. |
| 599 if (!interpolator_.interpolating()) |
| 600 interpolator_.StartInterpolating(); |
| 601 |
| 602 base::TimeDelta current_time = GetInterpolatedTime(); |
| 603 |
| 604 if (do_audio) { |
| 605 if (!audio_decoder_->Start(current_time)) { |
| 606 GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_); |
| 607 return; |
| 608 } |
| 609 |
| 610 // Attach listener on UI thread |
| 611 ui_task_runner_->PostTask(FROM_HERE, attach_listener_cb_); |
| 612 } |
| 613 |
| 614 if (do_video) { |
| 615 if (!video_decoder_->Start(current_time)) { |
| 616 GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_); |
| 617 return; |
| 618 } |
| 619 } |
| 620 } |
| 621 |
| 622 void MediaCodecPlayer::StopDecoders() { |
| 623 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 624 DVLOG(1) << __FUNCTION__; |
| 625 |
| 626 audio_decoder_->SyncStop(); |
| 627 video_decoder_->SyncStop(); |
| 628 } |
| 629 |
| 630 void MediaCodecPlayer::RequestToStopDecoders() { |
| 631 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 632 DVLOG(1) << __FUNCTION__; |
| 633 |
| 634 bool do_audio = false; |
| 635 bool do_video = false; |
| 636 |
| 637 if (audio_decoder_->IsPrefetchingOrPlaying()) |
| 638 do_audio = true; |
| 639 if (video_decoder_->IsPrefetchingOrPlaying()) |
| 640 do_video = true; |
| 641 |
| 642 if (!do_audio && !do_video) { |
| 643 GetMediaTaskRunner()->PostTask( |
| 644 FROM_HERE, base::Bind(&MediaCodecPlayer::OnStopDone, media_weak_this_)); |
| 645 return; |
| 646 } |
| 647 |
| 648 if (do_audio) |
| 649 audio_decoder_->RequestToStop(); |
| 650 if (do_video) |
| 651 video_decoder_->RequestToStop(); |
| 652 } |
| 653 |
| 654 void MediaCodecPlayer::ReleaseDecoderResources() { |
| 655 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 656 DVLOG(1) << __FUNCTION__; |
| 657 |
| 658 if (audio_decoder_) |
| 659 audio_decoder_->ReleaseDecoderResources(); |
| 660 |
| 661 if (video_decoder_) |
| 662 video_decoder_->ReleaseDecoderResources(); |
| 663 |
| 664 // At this point decoder threads should not be running |
| 665 if (interpolator_.interpolating()) |
| 666 interpolator_.StopInterpolating(); |
| 667 } |
| 668 |
| 669 void MediaCodecPlayer::CreateDecoders() { |
| 670 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 671 DVLOG(1) << __FUNCTION__; |
| 672 |
| 673 error_cb_ = base::Bind(&MediaCodecPlayer::OnError, media_weak_this_); |
| 674 |
| 675 audio_decoder_.reset(new MediaCodecAudioDecoder( |
| 676 GetMediaTaskRunner(), base::Bind(&MediaCodecPlayer::RequestDemuxerData, |
| 677 media_weak_this_, DemuxerStream::AUDIO), |
| 678 base::Bind(&MediaCodecPlayer::OnStarvation, media_weak_this_, |
| 679 DemuxerStream::AUDIO), |
| 680 base::Bind(&MediaCodecPlayer::OnStopDone, media_weak_this_), error_cb_, |
| 681 base::Bind(&MediaCodecPlayer::OnTimeIntervalUpdate, media_weak_this_, |
| 682 DemuxerStream::AUDIO))); |
| 683 |
| 684 video_decoder_.reset(new MediaCodecVideoDecoder( |
| 685 GetMediaTaskRunner(), base::Bind(&MediaCodecPlayer::RequestDemuxerData, |
| 686 media_weak_this_, DemuxerStream::VIDEO), |
| 687 base::Bind(&MediaCodecPlayer::OnStarvation, media_weak_this_, |
| 688 DemuxerStream::VIDEO), |
| 689 base::Bind(&MediaCodecPlayer::OnStopDone, media_weak_this_), error_cb_, |
| 690 MediaCodecDecoder::SetTimeCallback(), // null callback |
| 691 base::Bind(&MediaCodecPlayer::OnVideoResolutionChanged, media_weak_this_), |
| 692 base::Bind(&MediaCodecPlayer::OnVideoCodecCreated, media_weak_this_))); |
| 693 } |
| 694 |
| 695 bool MediaCodecPlayer::AudioFinished() { |
| 696 return audio_decoder_->IsCompleted() || !audio_decoder_->HasStream(); |
| 697 } |
| 698 |
| 699 bool MediaCodecPlayer::VideoFinished() { |
| 700 return video_decoder_->IsCompleted() || !video_decoder_->HasStream(); |
| 701 } |
| 702 |
| 703 base::TimeDelta MediaCodecPlayer::GetInterpolatedTime() { |
| 704 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 705 |
| 706 base::TimeDelta interpolated_time = interpolator_.GetInterpolatedTime(); |
| 707 return std::min(interpolated_time, duration_); |
| 708 } |
| 709 |
| 710 #undef RETURN_STRING |
| 711 #define RETURN_STRING(x) \ |
| 712 case x: \ |
| 713 return #x; |
| 714 |
| 715 const char* MediaCodecPlayer::AsString(PlayerState state) { |
| 716 switch (state) { |
| 717 RETURN_STRING(STATE_PAUSED); |
| 718 RETURN_STRING(STATE_WAITING_FOR_CONFIG); |
| 719 RETURN_STRING(STATE_PREFETCHING); |
| 720 RETURN_STRING(STATE_PLAYING); |
| 721 RETURN_STRING(STATE_STOPPING); |
| 722 RETURN_STRING(STATE_WAITING_FOR_SURFACE); |
| 723 RETURN_STRING(STATE_ERROR); |
| 724 } |
| 725 return nullptr; // crash early |
| 726 } |
| 727 |
| 728 #undef RETURN_STRING |
252 | 729 |
253 } // namespace media | 730 } // namespace media |
OLD | NEW |