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

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

Issue 2692593002: Media Remoting: End to end integration tests. (Closed)
Patch Set: Addressed comments. Rebased. Created 3 years, 8 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
OLDNEW
(Empty)
1 // Copyright 2017 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/remote_stream_provider.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/callback_helpers.h"
10 #include "media/base/decoder_buffer.h"
11 #include "media/base/video_rotation.h"
12 #include "media/remoting/proto_enum_utils.h"
13 #include "media/remoting/proto_utils.h"
14
15 namespace media {
16 namespace remoting {
17
18 namespace {
19 // The number of frames requested in each ReadUntil RPC message.
20 constexpr int kNumFramesInEachReadUntil = 10;
21 }
22
23 // An implementation of media::DemuxerStream on Media Remoting receiver.
24 // Receives data from mojo data pipe, and returns one frame or/and status when
25 // Read() is called.
26 class MediaStream final : public DemuxerStream {
27 public:
28 MediaStream(RpcBroker* rpc_broker,
29 Type type,
30 int remote_handle,
31 const base::Closure& error_callback);
32 ~MediaStream() override;
33
34 // DemuxerStream implementation.
35 void Read(const ReadCB& read_cb) override;
36 AudioDecoderConfig audio_decoder_config() override;
37 VideoDecoderConfig video_decoder_config() override;
38 DemuxerStream::Type type() const override;
39 Liveness liveness() const override;
40 bool SupportsConfigChanges() override;
41 VideoRotation video_rotation() override;
42
43 void Initialize(const base::Closure& init_done_cb);
44 void FlushUntil(int count);
45 void AppendBuffer(scoped_refptr<DecoderBuffer> buffer);
46
47 private:
48 // RPC messages handlers.
49 void OnReceivedRpc(std::unique_ptr<pb::RpcMessage> message);
50 void OnInitializeCallback(std::unique_ptr<pb::RpcMessage> message);
51 void OnReadUntilCallback(std::unique_ptr<pb::RpcMessage> message);
52
53 // Issues the ReadUntil RPC message when read is pending and buffer is empty.
54 void SendReadUntil();
55
56 // Run and reset the read callback.
57 void CompleteRead(DemuxerStream::Status status);
58
59 // Update the audio/video decoder config When config changes in the mid
60 // stream, the new config will be stored in
61 // |next_audio/video_decoder_config_|. Old config will be droped when all
62 // associated frames are consumed.
63 void UpdateConfig(const pb::AudioDecoderConfig* audio_message,
64 const pb::VideoDecoderConfig* video_message);
65
66 // Called when any error occurs.
67 void OnError(const std::string& error);
68
69 RpcBroker* const rpc_broker_; // Outlives this class.
70 const Type type_;
71 const int remote_handle_;
72 const int rpc_handle_;
73
74 // Set when Initialize() is called, and will be run only once after
75 // initialization is done.
76 base::Closure init_done_callback_;
77
78 // The read until count in the last ReadUntil RPC message.
79 int last_read_until_count_ = 0;
80
81 // Indicates whether Audio/VideoDecoderConfig changed and the frames with the
82 // old config are not yet consumed. The new config is stored in the end of
83 // |audio/video_decoder_config_|;
84 bool config_changed_ = false;
85
86 // Indicates whether a ReadUntil RPC message was sent without receiving the
87 // ReadUntilCallback message yet.
88 bool read_until_sent_ = false;
89
90 // Set when Read() is called. Run only once when read completes.
91 ReadCB read_complete_callback_;
92
93 base::Closure error_callback_; // Called only once when first error occurs.
94
95 std::deque<scoped_refptr<DecoderBuffer>> buffers_;
96
97 // Current audio/video config.
98 AudioDecoderConfig audio_decoder_config_;
99 VideoDecoderConfig video_decoder_config_;
100
101 // Stores the new auido/video config when config changes.
102 AudioDecoderConfig next_audio_decoder_config_;
103 VideoDecoderConfig next_video_decoder_config_;
104
105 base::WeakPtrFactory<MediaStream> weak_factory_;
106
107 DISALLOW_COPY_AND_ASSIGN(MediaStream);
108 };
109
110 MediaStream::MediaStream(RpcBroker* rpc_broker,
111 Type type,
112 int remote_handle,
113 const base::Closure& error_callback)
114 : rpc_broker_(rpc_broker),
115 type_(type),
116 remote_handle_(remote_handle),
117 rpc_handle_(rpc_broker_->GetUniqueHandle()),
118 error_callback_(error_callback),
119 weak_factory_(this) {
120 DCHECK(remote_handle_ != RpcBroker::kInvalidHandle);
121 rpc_broker_->RegisterMessageReceiverCallback(
122 rpc_handle_,
123 base::Bind(&MediaStream::OnReceivedRpc, weak_factory_.GetWeakPtr()));
124 }
125
126 MediaStream::~MediaStream() {
127 rpc_broker_->UnregisterMessageReceiverCallback(rpc_handle_);
128 }
129
130 void MediaStream::Initialize(const base::Closure& init_done_cb) {
131 DCHECK(!init_done_cb.is_null());
132 if (!init_done_callback_.is_null()) {
133 OnError("Duplicate initialization");
134 return;
135 }
136 init_done_callback_ = init_done_cb;
137
138 DVLOG(3) << __func__ << "Issues RpcMessage::RPC_DS_INITIALIZE with "
139 << "remote_handle=" << remote_handle_
140 << " rpc_handle=" << rpc_handle_;
141 std::unique_ptr<pb::RpcMessage> rpc(new pb::RpcMessage());
142 rpc->set_handle(remote_handle_);
143 rpc->set_proc(pb::RpcMessage::RPC_DS_INITIALIZE);
144 rpc->set_integer_value(rpc_handle_);
145 rpc_broker_->SendMessageToRemote(std::move(rpc));
146 }
147
148 void MediaStream::OnReceivedRpc(std::unique_ptr<pb::RpcMessage> message) {
149 DCHECK(message->handle() == rpc_handle_);
150
151 switch (message->proc()) {
152 case pb::RpcMessage::RPC_DS_INITIALIZE_CALLBACK:
153 OnInitializeCallback(std::move(message));
154 break;
155 case pb::RpcMessage::RPC_DS_READUNTIL_CALLBACK:
156 OnReadUntilCallback(std::move(message));
157 break;
158 default:
159 VLOG(3) << __func__ << "Unknow RPC message.";
160 }
161 }
162
163 void MediaStream::OnInitializeCallback(
164 std::unique_ptr<pb::RpcMessage> message) {
165 DVLOG(3) << __func__ << "Receives RPC_DS_INITIALIZE_CALLBACK message.";
166 const pb::DemuxerStreamInitializeCallback callback_message =
167 message->demuxerstream_initializecb_rpc();
168 if (callback_message.type() != type_) {
169 OnError("Wrong type");
170 return;
171 }
172 if ((type_ == DemuxerStream::AUDIO &&
173 audio_decoder_config_.IsValidConfig()) ||
174 (type_ == DemuxerStream::VIDEO &&
175 video_decoder_config_.IsValidConfig())) {
176 OnError("Duplicate Iniitialize");
177 return;
178 }
179 if (init_done_callback_.is_null()) {
180 OnError("Iniitialize callback missing");
181 return;
182 }
183
184 if (type_ == DemuxerStream::AUDIO &&
185 callback_message.has_audio_decoder_config()) {
186 const pb::AudioDecoderConfig audio_message =
187 callback_message.audio_decoder_config();
188 UpdateConfig(&audio_message, nullptr);
189 } else if (type_ == DemuxerStream::VIDEO &&
190 callback_message.has_video_decoder_config()) {
191 const pb::VideoDecoderConfig video_message =
192 callback_message.video_decoder_config();
193 UpdateConfig(nullptr, &video_message);
194 } else {
195 OnError("config missing");
196 return;
197 }
198 base::ResetAndReturn(&init_done_callback_).Run();
199 }
200
201 void MediaStream::OnReadUntilCallback(std::unique_ptr<pb::RpcMessage> message) {
202 DVLOG(3) << __func__ << ": Receives RPC_DS_READUNTIL_CALLBACK message.";
203 if (!read_until_sent_) {
204 OnError("Unexpected ReadUntilCallback");
205 return;
206 }
207 read_until_sent_ = false;
208 const pb::DemuxerStreamReadUntilCallback callback_message =
209 message->demuxerstream_readuntilcb_rpc();
210 last_read_until_count_ = callback_message.count();
211 if (ToDemuxerStreamStatus(callback_message.status()) == kConfigChanged) {
212 config_changed_ = true;
213 if (callback_message.has_audio_decoder_config()) {
214 const pb::AudioDecoderConfig audio_message =
215 callback_message.audio_decoder_config();
216 UpdateConfig(&audio_message, nullptr);
217 }
218 if (callback_message.has_video_decoder_config()) {
219 const pb::VideoDecoderConfig video_message =
220 callback_message.video_decoder_config();
221 UpdateConfig(nullptr, &video_message);
222 }
223 if (buffers_.empty() && !read_complete_callback_.is_null())
224 CompleteRead(DemuxerStream::kConfigChanged);
225 return;
226 }
227 if (buffers_.empty() && !read_complete_callback_.is_null())
228 SendReadUntil();
229 }
230
231 void MediaStream::UpdateConfig(const pb::AudioDecoderConfig* audio_message,
232 const pb::VideoDecoderConfig* video_message) {
233 if (type_ == AUDIO) {
234 DCHECK(audio_message && !video_message);
235 AudioDecoderConfig audio_config;
236 ConvertProtoToAudioDecoderConfig(*audio_message, &audio_config);
237 if (!audio_config.IsValidConfig()) {
238 OnError("Invalid audio config");
239 return;
240 }
241 if (config_changed_) {
242 DCHECK(audio_decoder_config_.IsValidConfig());
243 DCHECK(!next_audio_decoder_config_.IsValidConfig());
244 next_audio_decoder_config_ = audio_config;
245 } else {
246 DCHECK(!audio_decoder_config_.IsValidConfig());
247 audio_decoder_config_ = audio_config;
248 }
249 } else if (type_ == VIDEO) {
250 DCHECK(video_message && !audio_message);
251 VideoDecoderConfig video_config;
252 ConvertProtoToVideoDecoderConfig(*video_message, &video_config);
253 if (!video_config.IsValidConfig()) {
254 OnError("Invalid video config");
255 return;
256 }
257 if (config_changed_) {
258 DCHECK(video_decoder_config_.IsValidConfig());
259 DCHECK(!next_video_decoder_config_.IsValidConfig());
260 next_video_decoder_config_ = video_config;
261 } else {
262 DCHECK(!video_decoder_config_.IsValidConfig());
263 video_decoder_config_ = video_config;
264 }
265 } else {
266 NOTREACHED() << ": Only supports video or audio stream.";
267 }
268 }
269
270 void MediaStream::SendReadUntil() {
271 if (read_until_sent_)
272 return;
273 DVLOG(3) << "Issues RPC_DS_READUNTIL RPC message to remote_handle_="
274 << remote_handle_ << " with callback handle=" << rpc_handle_
275 << " count=" << last_read_until_count_;
276
277 std::unique_ptr<pb::RpcMessage> rpc(new pb::RpcMessage());
278 rpc->set_handle(remote_handle_);
279 rpc->set_proc(pb::RpcMessage::RPC_DS_READUNTIL);
280 auto* message = rpc->mutable_demuxerstream_readuntil_rpc();
281 last_read_until_count_ += kNumFramesInEachReadUntil;
282 message->set_count(last_read_until_count_);
283 message->set_callback_handle(rpc_handle_);
284 rpc_broker_->SendMessageToRemote(std::move(rpc));
285 read_until_sent_ = true;
286 }
287
288 void MediaStream::FlushUntil(int count) {
289 while (!buffers_.empty()) {
290 buffers_.pop_front();
291 }
292
293 last_read_until_count_ = count;
294 if (!read_complete_callback_.is_null())
295 CompleteRead(DemuxerStream::kAborted);
296 read_until_sent_ = false;
297 }
298
299 void MediaStream::Read(const ReadCB& read_cb) {
300 if (!read_complete_callback_.is_null()) {
miu 2017/04/03 21:05:01 Is this possible? There was just a DCHECK(read_com
xjz 2017/04/04 01:07:56 Done. Changed it back to DCHECK.
301 DVLOG(1) << __func__ << ": Ignore since already in the reading state.";
302 return;
303 }
304 DCHECK(!read_cb.is_null());
305 read_complete_callback_ = read_cb;
306 if (buffers_.empty() && config_changed_) {
307 CompleteRead(DemuxerStream::kConfigChanged);
308 return;
309 }
310
311 // Wait for more data.
312 if (buffers_.empty()) {
313 SendReadUntil();
314 return;
315 }
316
317 CompleteRead(DemuxerStream::kOk);
318 }
319
320 void MediaStream::CompleteRead(DemuxerStream::Status status) {
321 DVLOG(3) << __func__ << ": " << status;
322 switch (status) {
323 case DemuxerStream::kConfigChanged:
324 if (type_ == AUDIO) {
325 DCHECK(next_audio_decoder_config_.IsValidConfig());
326 audio_decoder_config_ = next_audio_decoder_config_;
327 next_audio_decoder_config_ = AudioDecoderConfig();
miu 2017/04/03 21:05:01 nit: Setting the next_audio_decoder_config_ to an
xjz 2017/04/04 01:07:55 Done.
328 } else {
329 DCHECK(next_video_decoder_config_.IsValidConfig());
330 video_decoder_config_ = next_video_decoder_config_;
331 next_video_decoder_config_ = VideoDecoderConfig();
332 }
333 config_changed_ = false;
334 base::ResetAndReturn(&read_complete_callback_).Run(status, nullptr);
335 return;
336 case DemuxerStream::kAborted:
337 base::ResetAndReturn(&read_complete_callback_).Run(status, nullptr);
338 return;
339 case DemuxerStream::kOk:
340 DCHECK(!buffers_.empty());
341 scoped_refptr<DecoderBuffer> frame_data = buffers_.front();
342 buffers_.pop_front();
343 base::ResetAndReturn(&read_complete_callback_).Run(status, frame_data);
344 return;
345 }
346 }
347
348 AudioDecoderConfig MediaStream::audio_decoder_config() {
349 DVLOG(3) << __func__;
350 DCHECK(type_ == DemuxerStream::AUDIO);
351 return audio_decoder_config_;
352 }
353
354 VideoDecoderConfig MediaStream::video_decoder_config() {
355 DVLOG(3) << __func__;
356 DCHECK(type_ == DemuxerStream::VIDEO);
357 return video_decoder_config_;
358 }
359
360 DemuxerStream::Type MediaStream::type() const {
361 return type_;
362 }
363
364 DemuxerStream::Liveness MediaStream::liveness() const {
365 return DemuxerStream::LIVENESS_LIVE;
366 }
367
368 bool MediaStream::SupportsConfigChanges() {
369 return true;
370 }
371
372 VideoRotation MediaStream::video_rotation() {
373 return VideoRotation::VIDEO_ROTATION_0;
374 }
375
376 void MediaStream::AppendBuffer(scoped_refptr<DecoderBuffer> buffer) {
377 DVLOG(3) << __func__;
378 buffers_.push_back(buffer);
379 if (!read_complete_callback_.is_null())
380 CompleteRead(DemuxerStream::kOk);
381 }
382
383 void MediaStream::OnError(const std::string& error) {
384 VLOG(1) << __func__ << ": " << error;
385 if (error_callback_.is_null())
386 return;
387 base::ResetAndReturn(&error_callback_).Run();
388 }
389
390 StreamProvider::StreamProvider(RpcBroker* rpc_broker,
391 const base::Closure& error_callback)
392 : rpc_broker_(rpc_broker),
393 error_callback_(error_callback),
394 weak_factory_(this) {}
395
396 StreamProvider::~StreamProvider() {}
397
398 void StreamProvider::Initialize(int remote_audio_handle,
399 int remote_video_handle,
400 const base::Closure& callback) {
401 DVLOG(3) << __func__ << ": remote_audio_handle=" << remote_audio_handle
402 << " remote_video_handle=" << remote_video_handle;
403 if (!init_done_callback_.is_null()) {
404 OnError("Duplicate initialization.");
405 return;
406 }
407 if (remote_audio_handle == RpcBroker::kInvalidHandle &&
408 remote_video_handle == RpcBroker::kInvalidHandle) {
409 OnError("Invalid handle.");
410 return;
411 }
412
413 init_done_callback_ = callback;
414 if (remote_audio_handle != RpcBroker::kInvalidHandle) {
415 audio_stream_.reset(new MediaStream(
416 rpc_broker_, DemuxerStream::AUDIO, remote_audio_handle,
417 base::Bind(&StreamProvider::OnError, weak_factory_.GetWeakPtr(),
418 "Media stream error")));
419 audio_stream_->Initialize(base::Bind(
420 &StreamProvider::AudioStreamInitialized, weak_factory_.GetWeakPtr()));
421 }
422 if (remote_video_handle != RpcBroker::kInvalidHandle) {
423 video_stream_.reset(new MediaStream(
424 rpc_broker_, DemuxerStream::VIDEO, remote_video_handle,
425 base::Bind(&StreamProvider::OnError, weak_factory_.GetWeakPtr(),
426 "Media stream error")));
427 video_stream_->Initialize(base::Bind(
428 &StreamProvider::VideoStreamInitialized, weak_factory_.GetWeakPtr()));
429 }
430 }
431
432 void StreamProvider::OnError(const std::string& error) {
433 VLOG(1) << __func__ << ": " << error;
434 if (error_callback_.is_null())
435 return;
436 base::ResetAndReturn(&error_callback_).Run();
437 }
438
439 void StreamProvider::AudioStreamInitialized() {
440 if (init_done_callback_.is_null()) {
miu 2017/04/03 21:05:01 ditto: DCHECK() only here, since this is not handl
xjz 2017/04/04 01:07:56 Done.
441 OnError("Audio stream init callback missing");
442 return;
443 }
444 audio_stream_initialized_ = true;
445 if (video_stream_initialized_ || !video_stream_)
446 base::ResetAndReturn(&init_done_callback_).Run();
447 }
448
449 void StreamProvider::VideoStreamInitialized() {
450 if (init_done_callback_.is_null()) {
miu 2017/04/03 21:05:01 ditto: DCHECK() only here...
xjz 2017/04/04 01:07:55 Done.
451 OnError("Video stream init callback missing");
452 return;
453 }
454 video_stream_initialized_ = true;
455 if (audio_stream_initialized_ || !audio_stream_)
456 base::ResetAndReturn(&init_done_callback_).Run();
457 }
458
459 std::vector<DemuxerStream*> StreamProvider::GetAllStreams() {
460 std::vector<DemuxerStream*> streams;
461 if (audio_stream_)
462 streams.push_back(audio_stream_.get());
463 if (video_stream_)
464 streams.push_back(video_stream_.get());
465 return streams;
466 }
467
468 void StreamProvider::AppendBuffer(DemuxerStream::Type type,
469 scoped_refptr<DecoderBuffer> buffer) {
470 if (type == DemuxerStream::AUDIO)
471 audio_stream_->AppendBuffer(buffer);
472 else if (type == DemuxerStream::VIDEO)
473 video_stream_->AppendBuffer(buffer);
474 else
475 NOTREACHED() << ": Only supports video or audio stream.";
476 }
477
478 void StreamProvider::FlushUntil(DemuxerStream::Type type, int count) {
479 DVLOG(3) << __func__ << ": type=" << type << " count=" << count;
480 if (type == DemuxerStream::AUDIO)
481 audio_stream_->FlushUntil(count);
482 else if (type == DemuxerStream::VIDEO)
483 video_stream_->FlushUntil(count);
484 else
485 NOTREACHED() << ": Only supports video or audio stream.";
486 }
487
488 } // namespace remoting
489 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698