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

Side by Side Diff: media/renderers/renderer_impl.cc

Issue 2491043003: MediaResource refactoring to support multiple streams (Closed)
Patch Set: rebase Created 3 years, 10 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/renderers/renderer_impl.h" 5 #include "media/renderers/renderer_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/callback_helpers.h" 11 #include "base/callback_helpers.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "media/base/audio_decoder_config.h" 17 #include "media/base/audio_decoder_config.h"
18 #include "media/base/audio_renderer.h" 18 #include "media/base/audio_renderer.h"
19 #include "media/base/bind_to_current_loop.h" 19 #include "media/base/bind_to_current_loop.h"
20 #include "media/base/demuxer_stream_provider.h"
21 #include "media/base/media_log.h" 20 #include "media/base/media_log.h"
21 #include "media/base/media_resource.h"
22 #include "media/base/media_switches.h" 22 #include "media/base/media_switches.h"
23 #include "media/base/renderer_client.h" 23 #include "media/base/renderer_client.h"
24 #include "media/base/time_source.h" 24 #include "media/base/time_source.h"
25 #include "media/base/video_decoder_config.h" 25 #include "media/base/video_decoder_config.h"
26 #include "media/base/video_renderer.h" 26 #include "media/base/video_renderer.h"
27 #include "media/base/wall_clock_time_source.h" 27 #include "media/base/wall_clock_time_source.h"
28 28
29 namespace media { 29 namespace media {
30 30
31 // See |video_underflow_threshold_|. 31 // See |video_underflow_threshold_|.
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 video_renderer_.reset(); 122 video_renderer_.reset();
123 audio_renderer_.reset(); 123 audio_renderer_.reset();
124 124
125 if (!init_cb_.is_null()) { 125 if (!init_cb_.is_null()) {
126 FinishInitialization(PIPELINE_ERROR_ABORT); 126 FinishInitialization(PIPELINE_ERROR_ABORT);
127 } else if (!flush_cb_.is_null()) { 127 } else if (!flush_cb_.is_null()) {
128 base::ResetAndReturn(&flush_cb_).Run(); 128 base::ResetAndReturn(&flush_cb_).Run();
129 } 129 }
130 } 130 }
131 131
132 void RendererImpl::Initialize(DemuxerStreamProvider* demuxer_stream_provider, 132 void RendererImpl::Initialize(MediaResource* media_resource,
133 RendererClient* client, 133 RendererClient* client,
134 const PipelineStatusCB& init_cb) { 134 const PipelineStatusCB& init_cb) {
135 DVLOG(1) << __func__; 135 DVLOG(1) << __func__;
136 DCHECK(task_runner_->BelongsToCurrentThread()); 136 DCHECK(task_runner_->BelongsToCurrentThread());
137 DCHECK_EQ(state_, STATE_UNINITIALIZED); 137 DCHECK_EQ(state_, STATE_UNINITIALIZED);
138 DCHECK(!init_cb.is_null()); 138 DCHECK(!init_cb.is_null());
139 DCHECK(client); 139 DCHECK(client);
140 140
141 client_ = client; 141 client_ = client;
142 demuxer_stream_provider_ = demuxer_stream_provider; 142 media_resource_ = media_resource;
143 init_cb_ = init_cb; 143 init_cb_ = init_cb;
144 144
145 if (HasEncryptedStream() && !cdm_context_) { 145 if (HasEncryptedStream() && !cdm_context_) {
146 state_ = STATE_INIT_PENDING_CDM; 146 state_ = STATE_INIT_PENDING_CDM;
147 return; 147 return;
148 } 148 }
149 149
150 state_ = STATE_INITIALIZING; 150 state_ = STATE_INITIALIZING;
151 InitializeAudioRenderer(); 151 InitializeAudioRenderer();
152 } 152 }
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 wall_clock_times->push_back(base::TimeTicks() + media_time); 343 wall_clock_times->push_back(base::TimeTicks() + media_time);
344 } 344 }
345 } 345 }
346 return true; 346 return true;
347 } 347 }
348 348
349 return time_source_->GetWallClockTimes(media_timestamps, wall_clock_times); 349 return time_source_->GetWallClockTimes(media_timestamps, wall_clock_times);
350 } 350 }
351 351
352 bool RendererImpl::HasEncryptedStream() { 352 bool RendererImpl::HasEncryptedStream() {
353 DemuxerStream* audio_stream = 353 std::vector<DemuxerStream*> demuxer_streams = media_resource_->GetStreams();
354 demuxer_stream_provider_->GetStream(DemuxerStream::AUDIO);
355 if (audio_stream && audio_stream->audio_decoder_config().is_encrypted())
356 return true;
357 354
358 DemuxerStream* video_stream = 355 for (const auto& s : demuxer_streams) {
359 demuxer_stream_provider_->GetStream(DemuxerStream::VIDEO); 356 if (s->type() == DemuxerStream::AUDIO &&
360 if (video_stream && video_stream->video_decoder_config().is_encrypted()) 357 s->audio_decoder_config().is_encrypted())
361 return true; 358 return true;
359 if (s->type() == DemuxerStream::VIDEO &&
360 s->video_decoder_config().is_encrypted())
361 return true;
362 }
xhwang 2017/02/01 18:26:04 Do we care about disabled streams? This is another
servolk 2017/02/01 22:29:21 Actually I think this, and the unit tests make a g
362 363
363 return false; 364 return false;
364 } 365 }
365 366
366 void RendererImpl::FinishInitialization(PipelineStatus status) { 367 void RendererImpl::FinishInitialization(PipelineStatus status) {
367 DCHECK(!init_cb_.is_null()); 368 DCHECK(!init_cb_.is_null());
368 369
369 if (!pending_cdm_attached_cb_.is_null()) 370 if (!pending_cdm_attached_cb_.is_null())
370 base::ResetAndReturn(&pending_cdm_attached_cb_).Run(status == PIPELINE_OK); 371 base::ResetAndReturn(&pending_cdm_attached_cb_).Run(status == PIPELINE_OK);
371 372
372 base::ResetAndReturn(&init_cb_).Run(status); 373 base::ResetAndReturn(&init_cb_).Run(status);
373 } 374 }
374 375
375 void RendererImpl::InitializeAudioRenderer() { 376 void RendererImpl::InitializeAudioRenderer() {
376 DVLOG(1) << __func__; 377 DVLOG(1) << __func__;
377 DCHECK(task_runner_->BelongsToCurrentThread()); 378 DCHECK(task_runner_->BelongsToCurrentThread());
378 DCHECK_EQ(state_, STATE_INITIALIZING); 379 DCHECK_EQ(state_, STATE_INITIALIZING);
379 DCHECK(!init_cb_.is_null()); 380 DCHECK(!init_cb_.is_null());
380 381
381 PipelineStatusCB done_cb = 382 PipelineStatusCB done_cb =
382 base::Bind(&RendererImpl::OnAudioRendererInitializeDone, weak_this_); 383 base::Bind(&RendererImpl::OnAudioRendererInitializeDone, weak_this_);
383 384
384 DemuxerStream* audio_stream = 385 std::vector<DemuxerStream*> streams = media_resource_->GetStreams();
xhwang 2017/02/01 18:26:04 ditto about comment and TODO
385 demuxer_stream_provider_->GetStream(DemuxerStream::AUDIO); 386 DemuxerStream* audio_stream = nullptr;
387 for (const auto& s : streams) {
388 if (s->type() == DemuxerStream::AUDIO && s->enabled()) {
389 audio_stream = s;
390 break;
391 }
392 }
386 if (!audio_stream) { 393 if (!audio_stream) {
387 audio_renderer_.reset(); 394 audio_renderer_.reset();
388 task_runner_->PostTask(FROM_HERE, base::Bind(done_cb, PIPELINE_OK)); 395 task_runner_->PostTask(FROM_HERE, base::Bind(done_cb, PIPELINE_OK));
389 return; 396 return;
390 } 397 }
391 398
392 audio_stream->SetStreamStatusChangeCB(base::Bind(
393 &RendererImpl::OnStreamStatusChanged, weak_this_, audio_stream));
394
395 audio_renderer_client_.reset( 399 audio_renderer_client_.reset(
396 new RendererClientInternal(DemuxerStream::AUDIO, this)); 400 new RendererClientInternal(DemuxerStream::AUDIO, this));
397 // Note: After the initialization of a renderer, error events from it may 401 // Note: After the initialization of a renderer, error events from it may
398 // happen at any time and all future calls must guard against STATE_ERROR. 402 // happen at any time and all future calls must guard against STATE_ERROR.
399 audio_renderer_->Initialize(audio_stream, cdm_context_, 403 audio_renderer_->Initialize(audio_stream, cdm_context_,
400 audio_renderer_client_.get(), done_cb); 404 audio_renderer_client_.get(), done_cb);
401 } 405 }
402 406
403 void RendererImpl::OnAudioRendererInitializeDone(PipelineStatus status) { 407 void RendererImpl::OnAudioRendererInitializeDone(PipelineStatus status) {
404 DVLOG(1) << __func__ << ": " << status; 408 DVLOG(1) << __func__ << ": " << status;
(...skipping 18 matching lines...) Expand all
423 427
424 void RendererImpl::InitializeVideoRenderer() { 428 void RendererImpl::InitializeVideoRenderer() {
425 DVLOG(1) << __func__; 429 DVLOG(1) << __func__;
426 DCHECK(task_runner_->BelongsToCurrentThread()); 430 DCHECK(task_runner_->BelongsToCurrentThread());
427 DCHECK_EQ(state_, STATE_INITIALIZING); 431 DCHECK_EQ(state_, STATE_INITIALIZING);
428 DCHECK(!init_cb_.is_null()); 432 DCHECK(!init_cb_.is_null());
429 433
430 PipelineStatusCB done_cb = 434 PipelineStatusCB done_cb =
431 base::Bind(&RendererImpl::OnVideoRendererInitializeDone, weak_this_); 435 base::Bind(&RendererImpl::OnVideoRendererInitializeDone, weak_this_);
432 436
433 DemuxerStream* video_stream = 437 std::vector<DemuxerStream*> streams = media_resource_->GetStreams();
434 demuxer_stream_provider_->GetStream(DemuxerStream::VIDEO); 438 DemuxerStream* video_stream = nullptr;
439 for (const auto& s : streams) {
440 if (s->type() == DemuxerStream::VIDEO && s->enabled()) {
441 video_stream = s;
442 break;
443 }
444 }
445
435 if (!video_stream) { 446 if (!video_stream) {
436 video_renderer_.reset(); 447 video_renderer_.reset();
437 task_runner_->PostTask(FROM_HERE, base::Bind(done_cb, PIPELINE_OK)); 448 task_runner_->PostTask(FROM_HERE, base::Bind(done_cb, PIPELINE_OK));
438 return; 449 return;
439 } 450 }
440 451
441 video_stream->SetStreamStatusChangeCB(base::Bind(
442 &RendererImpl::OnStreamStatusChanged, weak_this_, video_stream));
443
444 video_renderer_client_.reset( 452 video_renderer_client_.reset(
445 new RendererClientInternal(DemuxerStream::VIDEO, this)); 453 new RendererClientInternal(DemuxerStream::VIDEO, this));
446 video_renderer_->Initialize( 454 video_renderer_->Initialize(
447 video_stream, cdm_context_, video_renderer_client_.get(), 455 video_stream, cdm_context_, video_renderer_client_.get(),
448 base::Bind(&RendererImpl::GetWallClockTimes, base::Unretained(this)), 456 base::Bind(&RendererImpl::GetWallClockTimes, base::Unretained(this)),
449 done_cb); 457 done_cb);
450 } 458 }
451 459
452 void RendererImpl::OnVideoRendererInitializeDone(PipelineStatus status) { 460 void RendererImpl::OnVideoRendererInitializeDone(PipelineStatus status) {
453 DVLOG(1) << __func__ << ": " << status; 461 DVLOG(1) << __func__ << ": " << status;
454 DCHECK(task_runner_->BelongsToCurrentThread()); 462 DCHECK(task_runner_->BelongsToCurrentThread());
455 463
456 // OnError() may be fired at any time by the renderers, even if they thought 464 // OnError() may be fired at any time by the renderers, even if they thought
457 // they initialized successfully (due to delayed output device setup). 465 // they initialized successfully (due to delayed output device setup).
458 if (state_ != STATE_INITIALIZING) { 466 if (state_ != STATE_INITIALIZING) {
459 DCHECK(init_cb_.is_null()); 467 DCHECK(init_cb_.is_null());
460 audio_renderer_.reset(); 468 audio_renderer_.reset();
461 video_renderer_.reset(); 469 video_renderer_.reset();
462 return; 470 return;
463 } 471 }
464 472
465 DCHECK(!init_cb_.is_null()); 473 DCHECK(!init_cb_.is_null());
466 474
467 if (status != PIPELINE_OK) { 475 if (status != PIPELINE_OK) {
468 FinishInitialization(status); 476 FinishInitialization(status);
469 return; 477 return;
470 } 478 }
471 479
480 media_resource_->SetStreamStatusChangeCB(
481 base::Bind(&RendererImpl::OnStreamStatusChanged, weak_this_));
482
472 if (audio_renderer_) { 483 if (audio_renderer_) {
473 time_source_ = audio_renderer_->GetTimeSource(); 484 time_source_ = audio_renderer_->GetTimeSource();
474 } else if (!time_source_) { 485 } else if (!time_source_) {
475 wall_clock_time_source_.reset(new WallClockTimeSource()); 486 wall_clock_time_source_.reset(new WallClockTimeSource());
476 time_source_ = wall_clock_time_source_.get(); 487 time_source_ = wall_clock_time_source_.get();
477 } 488 }
478 489
479 state_ = STATE_PLAYING; 490 state_ = STATE_PLAYING;
480 DCHECK(time_source_); 491 DCHECK(time_source_);
481 DCHECK(audio_renderer_ || video_renderer_); 492 DCHECK(audio_renderer_ || video_renderer_);
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 DCHECK(task_runner_->BelongsToCurrentThread()); 856 DCHECK(task_runner_->BelongsToCurrentThread());
846 client_->OnVideoNaturalSizeChange(size); 857 client_->OnVideoNaturalSizeChange(size);
847 } 858 }
848 859
849 void RendererImpl::OnVideoOpacityChange(bool opaque) { 860 void RendererImpl::OnVideoOpacityChange(bool opaque) {
850 DCHECK(task_runner_->BelongsToCurrentThread()); 861 DCHECK(task_runner_->BelongsToCurrentThread());
851 client_->OnVideoOpacityChange(opaque); 862 client_->OnVideoOpacityChange(opaque);
852 } 863 }
853 864
854 } // namespace media 865 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698