OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. 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/remoting/remoting_renderer_controller.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/threading/thread_checker.h" | |
10 #include "base/time/time.h" | |
11 #include "media/remoting/remoting_cdm_context.h" | |
12 | |
13 namespace media { | |
14 | |
15 RemotingRendererController::RemotingRendererController( | |
16 scoped_refptr<RemotingSourceImpl> remoting_source) | |
17 : remoting_source_(remoting_source), weak_factory_(this) { | |
18 remoting_source_->AddClient(this); | |
19 } | |
20 | |
21 RemotingRendererController::~RemotingRendererController() { | |
22 DCHECK(thread_checker_.CalledOnValidThread()); | |
23 metrics_recorder_.WillStopSession(remoting::MEDIA_ELEMENT_DESTROYED); | |
24 remoting_source_->RemoveClient(this); | |
25 } | |
26 | |
27 void RemotingRendererController::OnStarted(bool success) { | |
28 DCHECK(thread_checker_.CalledOnValidThread()); | |
29 | |
30 if (success) { | |
31 VLOG(1) << "Remoting started successively."; | |
32 if (remote_rendering_started_) { | |
33 metrics_recorder_.DidStartSession(); | |
34 DCHECK(!switch_renderer_cb_.is_null()); | |
35 switch_renderer_cb_.Run(); | |
36 } else { | |
37 remoting_source_->StopRemoting(this); | |
38 } | |
39 } else { | |
40 VLOG(1) << "Failed to start remoting."; | |
41 remote_rendering_started_ = false; | |
42 metrics_recorder_.WillStopSession(remoting::START_RACE); | |
43 } | |
44 } | |
45 | |
46 void RemotingRendererController::OnSessionStateChanged() { | |
47 DCHECK(thread_checker_.CalledOnValidThread()); | |
48 UpdateFromSessionState(remoting::SINK_AVAILABLE, remoting::ROUTE_TERMINATED); | |
49 } | |
50 | |
51 void RemotingRendererController::UpdateFromSessionState( | |
52 remoting::StartTrigger start_trigger, | |
53 remoting::StopTrigger stop_trigger) { | |
54 VLOG(1) << "UpdateFromSessionState: " << remoting_source_->state(); | |
55 if (!sink_available_changed_cb_.is_null()) | |
56 sink_available_changed_cb_.Run(IsRemoteSinkAvailable()); | |
57 | |
58 UpdateInterstitial(base::nullopt); | |
59 UpdateAndMaybeSwitch(start_trigger, stop_trigger); | |
60 } | |
61 | |
62 bool RemotingRendererController::IsRemoteSinkAvailable() { | |
63 DCHECK(thread_checker_.CalledOnValidThread()); | |
64 | |
65 switch (remoting_source_->state()) { | |
66 case SESSION_CAN_START: | |
67 case SESSION_STARTING: | |
68 case SESSION_STARTED: | |
69 return true; | |
70 case SESSION_UNAVAILABLE: | |
71 case SESSION_STOPPING: | |
72 case SESSION_PERMANENTLY_STOPPED: | |
73 return false; | |
74 } | |
75 | |
76 return false; // To suppress compile warning. | |
77 } | |
78 | |
79 void RemotingRendererController::OnEnteredFullscreen() { | |
80 DCHECK(thread_checker_.CalledOnValidThread()); | |
81 | |
82 is_fullscreen_ = true; | |
83 // See notes in OnBecameDominantVisibleContent() for why this is forced: | |
84 is_dominant_content_ = true; | |
85 UpdateAndMaybeSwitch(remoting::ENTERED_FULLSCREEN, | |
86 remoting::UNKNOWN_STOP_TRIGGER); | |
87 } | |
88 | |
89 void RemotingRendererController::OnExitedFullscreen() { | |
90 DCHECK(thread_checker_.CalledOnValidThread()); | |
91 | |
92 is_fullscreen_ = false; | |
93 // See notes in OnBecameDominantVisibleContent() for why this is forced: | |
94 is_dominant_content_ = false; | |
95 UpdateAndMaybeSwitch(remoting::UNKNOWN_START_TRIGGER, | |
96 remoting::EXITED_FULLSCREEN); | |
97 } | |
98 | |
99 void RemotingRendererController::OnBecameDominantVisibleContent( | |
100 bool is_dominant) { | |
101 DCHECK(thread_checker_.CalledOnValidThread()); | |
102 | |
103 // Two scenarios where "dominance" status mixes with fullscreen transitions: | |
104 // | |
105 // 1. Just before/after entering fullscreen, the element will, of course, | |
106 // become the dominant on-screen content via automatic page layout. | |
107 // 2. Just before/after exiting fullscreen, the element may or may not | |
108 // shrink in size enough to become non-dominant. However, exiting | |
109 // fullscreen was caused by a user action that explicitly indicates a | |
110 // desire to exit remoting, so even if the element is still dominant, | |
111 // remoting should be shut down. | |
112 // | |
113 // Thus, to achieve the desired behaviors, |is_dominant_content_| is force-set | |
114 // in OnEnteredFullscreen() and OnExitedFullscreen(), and changes to it here | |
115 // are ignored while in fullscreen. | |
116 if (is_fullscreen_) | |
117 return; | |
118 | |
119 is_dominant_content_ = is_dominant; | |
120 UpdateAndMaybeSwitch(remoting::BECAME_DOMINANT_CONTENT, | |
121 remoting::BECAME_AUXILIARY_CONTENT); | |
122 } | |
123 | |
124 void RemotingRendererController::OnSetCdm(CdmContext* cdm_context) { | |
125 DCHECK(thread_checker_.CalledOnValidThread()); | |
126 | |
127 auto* remoting_cdm_context = RemotingCdmContext::From(cdm_context); | |
128 if (!remoting_cdm_context) | |
129 return; | |
130 | |
131 remoting_source_->RemoveClient(this); | |
132 remoting_source_ = remoting_cdm_context->GetRemotingSource(); | |
133 remoting_source_->AddClient(this); | |
134 UpdateFromSessionState(remoting::CDM_READY, remoting::DECRYPTION_ERROR); | |
135 } | |
136 | |
137 void RemotingRendererController::OnRemotePlaybackDisabled(bool disabled) { | |
138 DCHECK(thread_checker_.CalledOnValidThread()); | |
139 | |
140 is_remote_playback_disabled_ = disabled; | |
141 metrics_recorder_.OnRemotePlaybackDisabled(disabled); | |
142 UpdateAndMaybeSwitch(remoting::ENABLED_BY_PAGE, remoting::DISABLED_BY_PAGE); | |
143 } | |
144 | |
145 void RemotingRendererController::OnSetPoster(const GURL& poster_url) { | |
146 DCHECK(thread_checker_.CalledOnValidThread()); | |
147 | |
148 if (poster_url != poster_url_) { | |
149 poster_url_ = poster_url; | |
150 if (poster_url_.is_empty()) | |
151 UpdateInterstitial(SkBitmap()); | |
152 else | |
153 DownloadPosterImage(); | |
154 } | |
155 } | |
156 | |
157 void RemotingRendererController::SetSwitchRendererCallback( | |
158 const base::Closure& cb) { | |
159 DCHECK(thread_checker_.CalledOnValidThread()); | |
160 DCHECK(!cb.is_null()); | |
161 | |
162 switch_renderer_cb_ = cb; | |
163 // Note: Not calling UpdateAndMaybeSwitch() here since this method should be | |
164 // called during the initialization phase of this RemotingRendererController; | |
165 // and definitely before a whole lot of other things that are needed to | |
166 // trigger a switch. | |
167 } | |
168 | |
169 void RemotingRendererController::SetRemoteSinkAvailableChangedCallback( | |
170 const base::Callback<void(bool)>& cb) { | |
171 DCHECK(thread_checker_.CalledOnValidThread()); | |
172 | |
173 sink_available_changed_cb_ = cb; | |
174 if (!sink_available_changed_cb_.is_null()) | |
175 sink_available_changed_cb_.Run(IsRemoteSinkAvailable()); | |
176 } | |
177 | |
178 base::WeakPtr<remoting::RpcBroker> RemotingRendererController::GetRpcBroker() | |
179 const { | |
180 DCHECK(thread_checker_.CalledOnValidThread()); | |
181 | |
182 return remoting_source_->GetRpcBroker()->GetWeakPtr(); | |
183 } | |
184 | |
185 void RemotingRendererController::StartDataPipe( | |
186 std::unique_ptr<mojo::DataPipe> audio_data_pipe, | |
187 std::unique_ptr<mojo::DataPipe> video_data_pipe, | |
188 const RemotingSourceImpl::DataPipeStartCallback& done_callback) { | |
189 DCHECK(thread_checker_.CalledOnValidThread()); | |
190 | |
191 remoting_source_->StartDataPipe(std::move(audio_data_pipe), | |
192 std::move(video_data_pipe), done_callback); | |
193 } | |
194 | |
195 void RemotingRendererController::OnMetadataChanged( | |
196 const PipelineMetadata& metadata) { | |
197 DCHECK(thread_checker_.CalledOnValidThread()); | |
198 | |
199 const gfx::Size old_size = pipeline_metadata_.natural_size; | |
200 const bool was_audio_codec_supported = has_audio() && IsAudioCodecSupported(); | |
201 const bool was_video_codec_supported = has_video() && IsVideoCodecSupported(); | |
202 pipeline_metadata_ = metadata; | |
203 const bool is_audio_codec_supported = has_audio() && IsAudioCodecSupported(); | |
204 const bool is_video_codec_supported = has_video() && IsVideoCodecSupported(); | |
205 metrics_recorder_.OnPipelineMetadataChanged(metadata); | |
206 | |
207 is_encrypted_ = false; | |
208 if (has_video()) | |
209 is_encrypted_ |= metadata.video_decoder_config.is_encrypted(); | |
210 if (has_audio()) | |
211 is_encrypted_ |= metadata.audio_decoder_config.is_encrypted(); | |
212 | |
213 if (pipeline_metadata_.natural_size != old_size) | |
214 UpdateInterstitial(base::nullopt); | |
215 | |
216 remoting::StartTrigger start_trigger = remoting::UNKNOWN_START_TRIGGER; | |
217 if (!was_audio_codec_supported && is_audio_codec_supported) | |
218 start_trigger = remoting::SUPPORTED_AUDIO_CODEC; | |
219 if (!was_video_codec_supported && is_video_codec_supported) { | |
220 start_trigger = start_trigger == remoting::SUPPORTED_AUDIO_CODEC | |
221 ? remoting::SUPPORTED_AUDIO_AND_VIDEO_CODECS | |
222 : remoting::SUPPORTED_VIDEO_CODEC; | |
223 } | |
224 remoting::StopTrigger stop_trigger = remoting::UNKNOWN_STOP_TRIGGER; | |
225 if (was_audio_codec_supported && !is_audio_codec_supported) | |
226 stop_trigger = remoting::UNSUPPORTED_AUDIO_CODEC; | |
227 if (was_video_codec_supported && !is_video_codec_supported) { | |
228 stop_trigger = stop_trigger == remoting::UNSUPPORTED_AUDIO_CODEC | |
229 ? remoting::UNSUPPORTED_AUDIO_AND_VIDEO_CODECS | |
230 : remoting::UNSUPPORTED_VIDEO_CODEC; | |
231 } | |
232 UpdateAndMaybeSwitch(start_trigger, stop_trigger); | |
233 } | |
234 | |
235 bool RemotingRendererController::IsVideoCodecSupported() { | |
236 DCHECK(thread_checker_.CalledOnValidThread()); | |
237 DCHECK(has_video()); | |
238 | |
239 switch (pipeline_metadata_.video_decoder_config.codec()) { | |
240 case VideoCodec::kCodecH264: | |
241 case VideoCodec::kCodecVP8: | |
242 return true; | |
243 default: | |
244 VLOG(2) << "Remoting does not support video codec: " | |
245 << pipeline_metadata_.video_decoder_config.codec(); | |
246 return false; | |
247 } | |
248 } | |
249 | |
250 bool RemotingRendererController::IsAudioCodecSupported() { | |
251 DCHECK(thread_checker_.CalledOnValidThread()); | |
252 DCHECK(has_audio()); | |
253 | |
254 switch (pipeline_metadata_.audio_decoder_config.codec()) { | |
255 case AudioCodec::kCodecAAC: | |
256 case AudioCodec::kCodecMP3: | |
257 case AudioCodec::kCodecPCM: | |
258 case AudioCodec::kCodecVorbis: | |
259 case AudioCodec::kCodecFLAC: | |
260 case AudioCodec::kCodecAMR_NB: | |
261 case AudioCodec::kCodecAMR_WB: | |
262 case AudioCodec::kCodecPCM_MULAW: | |
263 case AudioCodec::kCodecGSM_MS: | |
264 case AudioCodec::kCodecPCM_S16BE: | |
265 case AudioCodec::kCodecPCM_S24BE: | |
266 case AudioCodec::kCodecOpus: | |
267 case AudioCodec::kCodecEAC3: | |
268 case AudioCodec::kCodecPCM_ALAW: | |
269 case AudioCodec::kCodecALAC: | |
270 case AudioCodec::kCodecAC3: | |
271 return true; | |
272 default: | |
273 VLOG(2) << "Remoting does not support audio codec: " | |
274 << pipeline_metadata_.audio_decoder_config.codec(); | |
275 return false; | |
276 } | |
277 } | |
278 | |
279 void RemotingRendererController::OnPlaying() { | |
280 DCHECK(thread_checker_.CalledOnValidThread()); | |
281 | |
282 is_paused_ = false; | |
283 UpdateAndMaybeSwitch(remoting::PLAY_COMMAND, remoting::UNKNOWN_STOP_TRIGGER); | |
284 } | |
285 | |
286 void RemotingRendererController::OnPaused() { | |
287 DCHECK(thread_checker_.CalledOnValidThread()); | |
288 | |
289 is_paused_ = true; | |
290 } | |
291 | |
292 bool RemotingRendererController::ShouldBeRemoting() { | |
293 DCHECK(thread_checker_.CalledOnValidThread()); | |
294 | |
295 if (switch_renderer_cb_.is_null()) { | |
296 DCHECK(!remote_rendering_started_); | |
297 return false; // No way to switch to a RemotingRenderImpl. | |
298 } | |
299 | |
300 const RemotingSessionState state = remoting_source_->state(); | |
301 if (is_encrypted_) { | |
302 // Due to technical limitations when playing encrypted content, once a | |
303 // remoting session has been started, always return true here to indicate | |
304 // that the RemotingRendererImpl should be used. In the stopped states, | |
305 // RemotingRendererImpl will display an interstitial to notify the user that | |
306 // local rendering cannot be resumed. | |
307 // | |
308 // TODO(miu): Revisit this once more of the encrypted-remoting impl is | |
309 // in-place. For example, this will prevent metrics from recording session | |
310 // stop reasons. | |
311 return state == RemotingSessionState::SESSION_STARTED || | |
312 state == RemotingSessionState::SESSION_STOPPING || | |
313 state == RemotingSessionState::SESSION_PERMANENTLY_STOPPED; | |
314 } | |
315 | |
316 if (encountered_renderer_fatal_error_) | |
317 return false; | |
318 | |
319 switch (state) { | |
320 case SESSION_UNAVAILABLE: | |
321 return false; // Cannot remote media without a remote sink. | |
322 case SESSION_CAN_START: | |
323 case SESSION_STARTING: | |
324 case SESSION_STARTED: | |
325 break; // Media remoting is possible, assuming other requirments are met. | |
326 case SESSION_STOPPING: | |
327 case SESSION_PERMANENTLY_STOPPED: | |
328 return false; // Use local rendering after stopping remoting. | |
329 } | |
330 | |
331 switch (remoting_source_->sink_capabilities()) { | |
332 case mojom::RemotingSinkCapabilities::NONE: | |
333 return false; | |
334 case mojom::RemotingSinkCapabilities::RENDERING_ONLY: | |
335 case mojom::RemotingSinkCapabilities::CONTENT_DECRYPTION_AND_RENDERING: | |
336 break; // The sink is capable of remote rendering. | |
337 } | |
338 | |
339 if ((!has_audio() && !has_video()) || | |
340 (has_video() && !IsVideoCodecSupported()) || | |
341 (has_audio() && !IsAudioCodecSupported())) { | |
342 return false; | |
343 } | |
344 | |
345 if (is_remote_playback_disabled_) | |
346 return false; | |
347 | |
348 // Normally, entering fullscreen or being the dominant visible content is the | |
349 // signal that starts remote rendering. However, current technical limitations | |
350 // require encrypted content be remoted without waiting for a user signal. | |
351 return is_fullscreen_ || is_dominant_content_; | |
352 } | |
353 | |
354 void RemotingRendererController::UpdateAndMaybeSwitch( | |
355 remoting::StartTrigger start_trigger, | |
356 remoting::StopTrigger stop_trigger) { | |
357 DCHECK(thread_checker_.CalledOnValidThread()); | |
358 | |
359 bool should_be_remoting = ShouldBeRemoting(); | |
360 | |
361 if (remote_rendering_started_ == should_be_remoting) | |
362 return; | |
363 | |
364 // Only switch to remoting when media is playing. Since the renderer is | |
365 // created when video starts loading/playing, receiver will display a black | |
366 // screen before video starts playing if switching to remoting when paused. | |
367 // Thus, the user experience is improved by not starting remoting until | |
368 // playback resumes. | |
369 if (should_be_remoting && is_paused_) | |
370 return; | |
371 | |
372 // Switch between local renderer and remoting renderer. | |
373 remote_rendering_started_ = should_be_remoting; | |
374 | |
375 if (remote_rendering_started_) { | |
376 DCHECK(!switch_renderer_cb_.is_null()); | |
377 if (remoting_source_->state() == | |
378 RemotingSessionState::SESSION_PERMANENTLY_STOPPED) { | |
379 switch_renderer_cb_.Run(); | |
380 return; | |
381 } | |
382 DCHECK_NE(start_trigger, remoting::UNKNOWN_START_TRIGGER); | |
383 metrics_recorder_.WillStartSession(start_trigger); | |
384 // |switch_renderer_cb_.Run()| will be called after remoting is started | |
385 // successfully. | |
386 remoting_source_->StartRemoting(this); | |
387 } else { | |
388 // For encrypted content, it's only valid to switch to remoting renderer, | |
389 // and never back to the local renderer. The RemotingCdmController will | |
390 // force-stop the session when remoting has ended; so no need to call | |
391 // StopRemoting() from here. | |
392 DCHECK(!is_encrypted_); | |
393 DCHECK_NE(stop_trigger, remoting::UNKNOWN_STOP_TRIGGER); | |
394 metrics_recorder_.WillStopSession(stop_trigger); | |
395 switch_renderer_cb_.Run(); | |
396 remoting_source_->StopRemoting(this); | |
397 } | |
398 } | |
399 | |
400 void RemotingRendererController::SetShowInterstitialCallback( | |
401 const ShowInterstitialCallback& cb) { | |
402 DCHECK(thread_checker_.CalledOnValidThread()); | |
403 show_interstitial_cb_ = cb; | |
404 UpdateInterstitial(SkBitmap()); | |
405 if (!poster_url_.is_empty()) | |
406 DownloadPosterImage(); | |
407 } | |
408 | |
409 void RemotingRendererController::SetDownloadPosterCallback( | |
410 const DownloadPosterCallback& cb) { | |
411 DCHECK(thread_checker_.CalledOnValidThread()); | |
412 DCHECK(download_poster_cb_.is_null()); | |
413 download_poster_cb_ = cb; | |
414 if (!poster_url_.is_empty()) | |
415 DownloadPosterImage(); | |
416 } | |
417 | |
418 void RemotingRendererController::UpdateInterstitial( | |
419 const base::Optional<SkBitmap>& image) { | |
420 DCHECK(thread_checker_.CalledOnValidThread()); | |
421 if (show_interstitial_cb_.is_null() || | |
422 pipeline_metadata_.natural_size.IsEmpty()) | |
423 return; | |
424 | |
425 RemotingInterstitialType type = RemotingInterstitialType::BETWEEN_SESSIONS; | |
426 switch (remoting_source_->state()) { | |
427 case SESSION_STARTED: | |
428 type = RemotingInterstitialType::IN_SESSION; | |
429 break; | |
430 case SESSION_PERMANENTLY_STOPPED: | |
431 type = RemotingInterstitialType::ENCRYPTED_MEDIA_FATAL_ERROR; | |
432 break; | |
433 case SESSION_UNAVAILABLE: | |
434 case SESSION_CAN_START: | |
435 case SESSION_STARTING: | |
436 case SESSION_STOPPING: | |
437 break; | |
438 } | |
439 | |
440 show_interstitial_cb_.Run(image, pipeline_metadata_.natural_size, type); | |
441 } | |
442 | |
443 void RemotingRendererController::DownloadPosterImage() { | |
444 if (download_poster_cb_.is_null() || show_interstitial_cb_.is_null()) | |
445 return; | |
446 DCHECK(!poster_url_.is_empty()); | |
447 | |
448 const base::TimeTicks download_start_time = base::TimeTicks::Now(); | |
449 download_poster_cb_.Run( | |
450 poster_url_, | |
451 base::Bind(&RemotingRendererController::OnPosterImageDownloaded, | |
452 weak_factory_.GetWeakPtr(), poster_url_, download_start_time)); | |
453 } | |
454 | |
455 void RemotingRendererController::OnPosterImageDownloaded( | |
456 const GURL& download_url, | |
457 base::TimeTicks download_start_time, | |
458 const SkBitmap& image) { | |
459 DCHECK(thread_checker_.CalledOnValidThread()); | |
460 | |
461 metrics_recorder_.OnPosterImageDownloaded( | |
462 base::TimeTicks::Now() - download_start_time, !image.drawsNothing()); | |
463 if (download_url != poster_url_) | |
464 return; // The poster image URL has changed during the download. | |
465 UpdateInterstitial(image); | |
466 } | |
467 | |
468 void RemotingRendererController::OnRendererFatalError( | |
469 remoting::StopTrigger stop_trigger) { | |
470 DCHECK(thread_checker_.CalledOnValidThread()); | |
471 | |
472 // Do not act on errors caused by things like Mojo pipes being closed during | |
473 // shutdown. | |
474 if (!remote_rendering_started_) | |
475 return; | |
476 | |
477 encountered_renderer_fatal_error_ = true; | |
478 UpdateAndMaybeSwitch(remoting::UNKNOWN_START_TRIGGER, stop_trigger); | |
479 } | |
480 | |
481 } // namespace media | |
OLD | NEW |