1 // Copyright 2015 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.
5 #include "chromecast/media/base/switching_media_renderer.h"
7 #include "base/logging.h"
8 #include "media/base/audio_decoder_config.h"
9 #include "media/base/demuxer_stream.h"
10 #include "media/base/demuxer_stream_provider.h"
12 namespace chromecast
{
15 SwitchingMediaRenderer::SwitchingMediaRenderer(
16 scoped_ptr
<::media::Renderer
> default_renderer
,
17 scoped_ptr
<::media::Renderer
> cma_renderer
)
18 : default_renderer_(default_renderer
.Pass()),
19 cma_renderer_(cma_renderer
.Pass()) {
20 DCHECK(default_renderer_
);
21 DCHECK(cma_renderer_
);
24 SwitchingMediaRenderer::~SwitchingMediaRenderer() {
27 void SwitchingMediaRenderer::Initialize(
28 ::media::DemuxerStreamProvider
* demuxer_stream_provider
,
29 const ::media::PipelineStatusCB
& init_cb
,
30 const ::media::StatisticsCB
& statistics_cb
,
31 const ::media::BufferingStateCB
& buffering_state_cb
,
32 const base::Closure
& ended_cb
,
33 const ::media::PipelineStatusCB
& error_cb
,
34 const base::Closure
& waiting_for_decryption_key_cb
) {
35 // At this point the DemuxerStreamProvider should be fully initialized, so we
36 // have enough information to decide which renderer to use.
37 demuxer_stream_provider_
= demuxer_stream_provider
;
38 DCHECK(demuxer_stream_provider_
);
39 ::media::DemuxerStream
* audio_stream
=
40 demuxer_stream_provider_
->GetStream(::media::DemuxerStream::AUDIO
);
41 ::media::DemuxerStream
* video_stream
=
42 demuxer_stream_provider_
->GetStream(::media::DemuxerStream::VIDEO
);
43 if (audio_stream
&& !video_stream
&&
44 (audio_stream
->audio_decoder_config().codec() == ::media::kCodecFLAC
||
45 audio_stream
->audio_decoder_config().codec() == ::media::kCodecOpus
)) {
46 // We'll use the default Chrome media renderer with software audio decoding
47 cma_renderer_
.reset();
49 // We'll use the CMA-based rendering with hardware decoding
50 default_renderer_
.reset();
53 return GetRenderer()->Initialize(
54 demuxer_stream_provider
, init_cb
, statistics_cb
, buffering_state_cb
,
55 ended_cb
, error_cb
, waiting_for_decryption_key_cb
);
58 ::media::Renderer
* SwitchingMediaRenderer::GetRenderer() const {
59 DCHECK(default_renderer_
|| cma_renderer_
);
61 return cma_renderer_
.get();
63 DCHECK(default_renderer_
);
64 return default_renderer_
.get();
67 void SwitchingMediaRenderer::SetCdm(
68 ::media::CdmContext
* cdm_context
,
69 const ::media::CdmAttachedCB
& cdm_attached_cb
) {
70 GetRenderer()->SetCdm(cdm_context
, cdm_attached_cb
);
73 void SwitchingMediaRenderer::Flush(const base::Closure
& flush_cb
) {
74 GetRenderer()->Flush(flush_cb
);
77 void SwitchingMediaRenderer::StartPlayingFrom(base::TimeDelta time
) {
78 GetRenderer()->StartPlayingFrom(time
);
81 void SwitchingMediaRenderer::SetPlaybackRate(double playback_rate
) {
82 GetRenderer()->SetPlaybackRate(playback_rate
);
85 void SwitchingMediaRenderer::SetVolume(float volume
) {
86 GetRenderer()->SetVolume(volume
);
89 base::TimeDelta
SwitchingMediaRenderer::GetMediaTime() {
90 return GetRenderer()->GetMediaTime();
93 bool SwitchingMediaRenderer::HasAudio() {
94 return GetRenderer()->HasAudio();
97 bool SwitchingMediaRenderer::HasVideo() {
98 return GetRenderer()->HasVideo();
102 } // namespace chromecast