Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(576)

Side by Side Diff: media/remoting/remoting_controller.cc

Issue 2389473002: Media Remoting: Add RemotingController. (Closed)
Patch Set: Rebased. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/remoting/remoting_controller.h ('k') | media/remoting/remoting_controller_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_controller.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/single_thread_task_runner.h"
10
11 namespace media {
12
13 RemotingController::RemotingController(
14 mojom::RemotingSourceRequest source_request,
15 mojom::RemoterPtr remoter)
16 : binding_(this, std::move(source_request)),
17 remoter_(std::move(remoter)),
18 task_runner_(base::ThreadTaskRunnerHandle::Get()),
19 weak_factory_(this) {}
20
21 RemotingController::~RemotingController() {}
22
23 void RemotingController::OnSinkAvailable() {
24 DCHECK(task_runner_->BelongsToCurrentThread());
25
26 is_sink_available_ = true;
27 UpdateAndMaybeSwitch();
28 }
29
30 void RemotingController::OnSinkGone() {
31 DCHECK(task_runner_->BelongsToCurrentThread());
32
33 is_sink_available_ = false;
34 UpdateAndMaybeSwitch();
35 }
36
37 void RemotingController::OnStarted() {
38 DCHECK(task_runner_->BelongsToCurrentThread());
39
40 VLOG(1) << "Remoting started successively.";
41 if (is_remoting_)
42 switch_renderer_cb_.Run();
43 else
44 remoter_->Stop(mojom::RemotingStopReason::LOCAL_PLAYBACK);
45 }
46
47 void RemotingController::OnStartFailed(mojom::RemotingStartFailReason reason) {
48 DCHECK(task_runner_->BelongsToCurrentThread());
49
50 VLOG(1) << "Failed to start remoting:" << reason;
51 is_remoting_ = false;
52 }
53
54 void RemotingController::OnMessageFromSink(
55 const std::vector<uint8_t>& message) {
56 DCHECK(task_runner_->BelongsToCurrentThread());
57
58 // TODO(xjz): Merge with Eric's CL to handle the RPC messages here.
59 NOTIMPLEMENTED();
60 }
61
62 void RemotingController::OnStopped(mojom::RemotingStopReason reason) {
63 DCHECK(task_runner_->BelongsToCurrentThread());
64
65 VLOG(1) << "Remoting stopped: " << reason;
66 is_remoting_ = false;
67 }
68
69 void RemotingController::OnEnteredFullscreen() {
70 DCHECK(task_runner_->BelongsToCurrentThread());
71
72 is_fullscreen_ = true;
73 UpdateAndMaybeSwitch();
74 }
75
76 void RemotingController::OnExitedFullscreen() {
77 DCHECK(task_runner_->BelongsToCurrentThread());
78
79 is_fullscreen_ = false;
80 UpdateAndMaybeSwitch();
81 }
82
83 void RemotingController::OnSetCdm(CdmContext* cdm_context) {
84 DCHECK(task_runner_->BelongsToCurrentThread());
85
86 // TODO(xjz): Not implemented. Will add in up-coming change.
87 NOTIMPLEMENTED();
88 }
89
90 void RemotingController::SetSwitchRendererCallback(
91 const SwitchRendererCallback& cb) {
92 DCHECK(task_runner_->BelongsToCurrentThread());
93 DCHECK(!cb.is_null());
94
95 switch_renderer_cb_ = cb;
96 }
97
98 void RemotingController::OnMetadataChanged(const PipelineMetadata& metadata) {
99 DCHECK(task_runner_->BelongsToCurrentThread());
100
101 has_video_ = metadata.has_video;
102 has_audio_ = metadata.has_audio;
103 if (!has_video_ && !has_audio_)
104 return;
105
106 if (has_video_) {
107 DCHECK(metadata.video_decoder_config.IsValidConfig());
108 video_decoder_config_ = metadata.video_decoder_config;
109 is_encrypted_ |= video_decoder_config_.is_encrypted();
110 }
111 if (has_audio_) {
112 DCHECK(metadata.audio_decoder_config.IsValidConfig());
113 audio_decoder_config_ = metadata.audio_decoder_config;
114 is_encrypted_ |= audio_decoder_config_.is_encrypted();
115 }
116 UpdateAndMaybeSwitch();
117 }
118
119 bool RemotingController::IsVideoCodecSupported() {
120 DCHECK(task_runner_->BelongsToCurrentThread());
121 DCHECK(has_video_);
122
123 switch (video_decoder_config_.codec()) {
124 case VideoCodec::kCodecH264:
125 case VideoCodec::kCodecVP8:
126 return true;
127 default:
128 VLOG(2) << "Remoting does not support video codec: "
129 << video_decoder_config_.codec();
130 return false;
131 }
132 }
133
134 bool RemotingController::IsAudioCodecSupported() {
135 DCHECK(task_runner_->BelongsToCurrentThread());
136 DCHECK(has_audio_);
137
138 switch (audio_decoder_config_.codec()) {
139 case AudioCodec::kCodecAAC:
140 case AudioCodec::kCodecMP3:
141 case AudioCodec::kCodecPCM:
142 case AudioCodec::kCodecVorbis:
143 case AudioCodec::kCodecFLAC:
144 case AudioCodec::kCodecAMR_NB:
145 case AudioCodec::kCodecAMR_WB:
146 case AudioCodec::kCodecPCM_MULAW:
147 case AudioCodec::kCodecGSM_MS:
148 case AudioCodec::kCodecPCM_S16BE:
149 case AudioCodec::kCodecPCM_S24BE:
150 case AudioCodec::kCodecOpus:
151 case AudioCodec::kCodecEAC3:
152 case AudioCodec::kCodecPCM_ALAW:
153 case AudioCodec::kCodecALAC:
154 case AudioCodec::kCodecAC3:
155 return true;
156 default:
157 VLOG(2) << "Remoting does not support audio codec: "
158 << audio_decoder_config_.codec();
159 return false;
160 }
161 }
162
163 bool RemotingController::ShouldBeRemoting() {
164 DCHECK(task_runner_->BelongsToCurrentThread());
165
166 // TODO(xjz): The control logic for EME will be added in a later CL.
167 if (is_encrypted_)
168 return false;
169
170 if (!is_sink_available_)
171 return false;
172 if (!is_fullscreen_)
173 return false;
174 if (has_video_ && !IsVideoCodecSupported())
175 return false;
176 if (has_audio_ && !IsAudioCodecSupported())
177 return false;
178 return true;
179 }
180
181 void RemotingController::UpdateAndMaybeSwitch() {
182 DCHECK(task_runner_->BelongsToCurrentThread());
183
184 // TODO(xjz): The switching logic for encrypted content will be added in a
185 // later CL.
186
187 // Demuxer is not initialized yet.
188 if (!has_audio_ && !has_video_)
189 return;
190
191 DCHECK(!switch_renderer_cb_.is_null());
192
193 bool should_be_remoting = ShouldBeRemoting();
194 if (is_remoting_ == should_be_remoting)
195 return;
196
197 // Switch between local and remoting.
198 is_remoting_ = should_be_remoting;
199 if (is_remoting_) {
200 // |swithc_renderer_cb_.Run()| will be called after remoting is started
201 // successfully.
202 remoter_->Start();
203 } else {
204 switch_renderer_cb_.Run();
205 remoter_->Stop(mojom::RemotingStopReason::LOCAL_PLAYBACK);
206 }
207 }
208
209 } // namespace media
OLDNEW
« no previous file with comments | « media/remoting/remoting_controller.h ('k') | media/remoting/remoting_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698