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 "media/remoting/remoting_cdm.h" | |
11 | |
12 namespace media { | |
13 | |
14 RemotingRendererController::RemotingRendererController( | |
xhwang
2016/11/01 08:21:29
For future CL, please consider do renames in a sep
xjz
2016/11/01 21:55:53
Acknowledged.
| |
15 scoped_refptr<RemotingSourceImpl> remoting_source) | |
16 : remoting_source_(remoting_source), weak_factory_(this) { | |
17 remoting_source_->AddClient(this); | |
18 } | |
19 | |
20 RemotingRendererController::~RemotingRendererController() { | |
21 DCHECK(thread_checker_.CalledOnValidThread()); | |
22 remoting_source_->RemoveClient(this); | |
23 } | |
24 | |
25 void RemotingRendererController::OnStarted(bool success) { | |
26 DCHECK(thread_checker_.CalledOnValidThread()); | |
27 | |
28 if (success) { | |
29 VLOG(1) << "Remoting started successively."; | |
30 if (remote_rendering_started_) { | |
31 DCHECK(!switch_renderer_cb_.is_null()); | |
32 switch_renderer_cb_.Run(); | |
33 } else { | |
34 remoting_source_->StopRemoting(this); | |
35 } | |
36 } else { | |
37 VLOG(1) << "Failed to start remoting."; | |
38 remote_rendering_started_ = false; | |
39 } | |
40 } | |
41 | |
42 void RemotingRendererController::OnSessionStateChanged() { | |
43 DCHECK(thread_checker_.CalledOnValidThread()); | |
44 | |
45 VLOG(1) << "OnSessionStateChanged: " << remoting_source_->state(); | |
46 UpdateAndMaybeSwitch(); | |
47 } | |
48 | |
49 void RemotingRendererController::OnEnteredFullscreen() { | |
50 DCHECK(thread_checker_.CalledOnValidThread()); | |
51 | |
52 is_fullscreen_ = true; | |
53 UpdateAndMaybeSwitch(); | |
54 } | |
55 | |
56 void RemotingRendererController::OnExitedFullscreen() { | |
57 DCHECK(thread_checker_.CalledOnValidThread()); | |
58 | |
59 is_fullscreen_ = false; | |
60 UpdateAndMaybeSwitch(); | |
61 } | |
62 | |
63 void RemotingRendererController::OnSetCdm(CdmContext* cdm_context) { | |
64 DCHECK(thread_checker_.CalledOnValidThread()); | |
65 | |
66 auto* cdm = RemotingCdm::From(cdm_context); | |
xhwang
2016/11/01 08:21:29
Is GetRemotingSource() the only thing you need fro
xjz
2016/11/01 21:55:53
Done.
| |
67 if (!cdm) | |
68 return; | |
69 | |
70 remoting_source_->RemoveClient(this); | |
71 remoting_source_ = cdm->GetRemotingSource(); | |
72 remoting_source_->AddClient(this); // Calls OnSessionStateChanged(). | |
73 UpdateAndMaybeSwitch(); | |
74 } | |
75 | |
76 void RemotingRendererController::SetSwitchRendererCallback( | |
77 const base::Closure& cb) { | |
78 DCHECK(thread_checker_.CalledOnValidThread()); | |
79 DCHECK(!cb.is_null()); | |
80 | |
81 switch_renderer_cb_ = cb; | |
82 UpdateAndMaybeSwitch(); | |
83 } | |
84 | |
85 void RemotingRendererController::OnMetadataChanged( | |
86 const PipelineMetadata& metadata) { | |
87 DCHECK(thread_checker_.CalledOnValidThread()); | |
88 | |
89 has_video_ = | |
90 metadata.has_video && metadata.video_decoder_config.IsValidConfig(); | |
91 has_audio_ = | |
92 metadata.has_audio && metadata.audio_decoder_config.IsValidConfig(); | |
93 | |
94 is_encrypted_ = false; | |
95 if (has_video_) { | |
96 video_decoder_config_ = metadata.video_decoder_config; | |
97 is_encrypted_ |= video_decoder_config_.is_encrypted(); | |
98 } | |
99 if (has_audio_) { | |
100 audio_decoder_config_ = metadata.audio_decoder_config; | |
101 is_encrypted_ |= audio_decoder_config_.is_encrypted(); | |
102 } | |
103 UpdateAndMaybeSwitch(); | |
104 } | |
105 | |
106 bool RemotingRendererController::IsVideoCodecSupported() { | |
107 DCHECK(thread_checker_.CalledOnValidThread()); | |
108 DCHECK(has_video_); | |
109 | |
110 switch (video_decoder_config_.codec()) { | |
111 case VideoCodec::kCodecH264: | |
112 case VideoCodec::kCodecVP8: | |
113 return true; | |
114 default: | |
115 VLOG(2) << "Remoting does not support video codec: " | |
116 << video_decoder_config_.codec(); | |
117 return false; | |
118 } | |
119 } | |
120 | |
121 bool RemotingRendererController::IsAudioCodecSupported() { | |
122 DCHECK(thread_checker_.CalledOnValidThread()); | |
123 DCHECK(has_audio_); | |
124 | |
125 switch (audio_decoder_config_.codec()) { | |
126 case AudioCodec::kCodecAAC: | |
127 case AudioCodec::kCodecMP3: | |
128 case AudioCodec::kCodecPCM: | |
129 case AudioCodec::kCodecVorbis: | |
130 case AudioCodec::kCodecFLAC: | |
131 case AudioCodec::kCodecAMR_NB: | |
132 case AudioCodec::kCodecAMR_WB: | |
133 case AudioCodec::kCodecPCM_MULAW: | |
134 case AudioCodec::kCodecGSM_MS: | |
135 case AudioCodec::kCodecPCM_S16BE: | |
136 case AudioCodec::kCodecPCM_S24BE: | |
137 case AudioCodec::kCodecOpus: | |
138 case AudioCodec::kCodecEAC3: | |
139 case AudioCodec::kCodecPCM_ALAW: | |
140 case AudioCodec::kCodecALAC: | |
141 case AudioCodec::kCodecAC3: | |
142 return true; | |
143 default: | |
144 VLOG(2) << "Remoting does not support audio codec: " | |
145 << audio_decoder_config_.codec(); | |
146 return false; | |
147 } | |
148 } | |
149 | |
150 bool RemotingRendererController::ShouldBeRemoting() { | |
151 DCHECK(thread_checker_.CalledOnValidThread()); | |
152 | |
153 if (switch_renderer_cb_.is_null()) | |
154 return false; // No way to switch to a RemotingRenderImpl. | |
155 | |
156 const RemotingSessionState state = remoting_source_->state(); | |
157 if (is_encrypted_) { | |
158 // Due to technical limitations when playing encrypted content, once a | |
159 // remoting session has been started, always return true here to indicate | |
160 // that the RemotingRendererImpl should be used. In the stopped states, | |
161 // RemotingRendererImpl will display an interstitial to notify the user that | |
162 // local rendering cannot be resumed. | |
163 return state == RemotingSessionState::SESSION_STARTED || | |
164 state == RemotingSessionState::SESSION_STOPPING || | |
165 state == RemotingSessionState::SESSION_PERMANENTLY_STOPPED; | |
166 } | |
167 | |
168 switch (state) { | |
169 case SESSION_UNAVAILABLE: | |
170 return false; // Cannot remote media without a remote sink. | |
171 case SESSION_CAN_START: | |
172 case SESSION_STARTING: | |
173 case SESSION_STARTED: | |
174 break; // Media remoting is possible, assuming other requirments are met. | |
175 case SESSION_STOPPING: | |
176 case SESSION_PERMANENTLY_STOPPED: | |
177 return false; // Use local rendering after stopping remoting. | |
178 } | |
179 if ((!has_audio_ && !has_video_) || | |
180 (has_video_ && !IsVideoCodecSupported()) || | |
181 (has_audio_ && !IsAudioCodecSupported())) | |
182 return false; | |
183 | |
184 // Normally, entering fullscreen is the signal that starts remote rendering. | |
185 // However, current technical limitations require encrypted content be remoted | |
186 // without waiting for a user signal. | |
187 return is_fullscreen_; | |
188 } | |
189 | |
190 void RemotingRendererController::UpdateAndMaybeSwitch() { | |
191 DCHECK(thread_checker_.CalledOnValidThread()); | |
192 | |
193 bool should_be_remoting = ShouldBeRemoting(); | |
194 | |
195 if (remote_rendering_started_ == should_be_remoting) | |
196 return; | |
197 | |
198 // Switch between local renderer and remoting renderer. | |
199 remote_rendering_started_ = should_be_remoting; | |
200 | |
201 if (remote_rendering_started_) { | |
202 DCHECK(!switch_renderer_cb_.is_null()); | |
203 if (remoting_source_->state() == | |
204 RemotingSessionState::SESSION_PERMANENTLY_STOPPED) { | |
205 switch_renderer_cb_.Run(); | |
206 return; | |
207 } | |
208 // |switch_renderer_cb_.Run()| will be called after remoting is started | |
209 // successfully. | |
210 remoting_source_->StartRemoting(this); | |
211 } else { | |
212 // For encrypted content, it's only valid to switch to remoting renderer, | |
213 // and never back to the local renderer. The RemotingCdmController will | |
214 // force-stop the session when remoting has ended; so no need to call | |
215 // StopRemoting() from here. | |
216 DCHECK(!is_encrypted_); | |
217 switch_renderer_cb_.Run(); | |
218 remoting_source_->StopRemoting(this); | |
219 } | |
220 } | |
221 | |
222 } // namespace media | |
OLD | NEW |