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
3 // found in the LICENSE file.
5 #include "chromecast/media/cma/pipeline/audio_pipeline_impl.h"
8 #include "chromecast/media/cma/backend/audio_pipeline_device.h"
9 #include "chromecast/media/cma/base/buffering_defs.h"
10 #include "chromecast/media/cma/base/cma_logging.h"
11 #include "chromecast/media/cma/base/coded_frame_provider.h"
12 #include "chromecast/media/cma/pipeline/av_pipeline_impl.h"
13 #include "media/base/audio_decoder_config.h"
15 namespace chromecast
{
19 const size_t kMaxAudioFrameSize
= 32 * 1024;
22 AudioPipelineImpl::AudioPipelineImpl(AudioPipelineDevice
* audio_device
)
23 : audio_device_(audio_device
),
25 av_pipeline_impl_
.reset(new AvPipelineImpl(
27 base::Bind(&AudioPipelineImpl::OnUpdateConfig
, base::Unretained(this))));
28 weak_this_
= weak_factory_
.GetWeakPtr();
31 AudioPipelineImpl::~AudioPipelineImpl() {
34 void AudioPipelineImpl::SetCodedFrameProvider(
35 scoped_ptr
<CodedFrameProvider
> frame_provider
) {
36 av_pipeline_impl_
->SetCodedFrameProvider(
37 frame_provider
.Pass(), kAppAudioBufferSize
, kMaxAudioFrameSize
);
40 void AudioPipelineImpl::SetClient(const AvPipelineClient
& client
) {
41 audio_client_
= client
;
42 av_pipeline_impl_
->SetClient(client
);
45 bool AudioPipelineImpl::StartPlayingFrom(
47 const scoped_refptr
<BufferingState
>& buffering_state
) {
48 CMALOG(kLogControl
) << "AudioPipelineImpl::StartPlayingFrom t0="
49 << time
.InMilliseconds();
51 // Reset the pipeline statistics.
52 previous_stats_
= ::media::PipelineStatistics();
55 if (av_pipeline_impl_
->GetState() == AvPipelineImpl::kError
)
57 DCHECK_EQ(av_pipeline_impl_
->GetState(), AvPipelineImpl::kFlushed
);
59 if (!av_pipeline_impl_
->StartPlayingFrom(time
, buffering_state
)) {
60 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kError
);
63 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kPlaying
);
68 void AudioPipelineImpl::Flush(const ::media::PipelineStatusCB
& status_cb
) {
69 CMALOG(kLogControl
) << "AudioPipelineImpl::Flush";
70 if (av_pipeline_impl_
->GetState() == AvPipelineImpl::kError
) {
71 status_cb
.Run(::media::PIPELINE_ERROR_ABORT
);
74 DCHECK_EQ(av_pipeline_impl_
->GetState(), AvPipelineImpl::kPlaying
);
75 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kFlushing
);
76 av_pipeline_impl_
->Flush(
77 base::Bind(&AudioPipelineImpl::OnFlushDone
, weak_this_
, status_cb
));
80 void AudioPipelineImpl::OnFlushDone(
81 const ::media::PipelineStatusCB
& status_cb
) {
82 CMALOG(kLogControl
) << "AudioPipelineImpl::OnFlushDone";
83 if (av_pipeline_impl_
->GetState() == AvPipelineImpl::kError
) {
84 status_cb
.Run(::media::PIPELINE_ERROR_ABORT
);
87 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kFlushed
);
88 status_cb
.Run(::media::PIPELINE_OK
);
91 void AudioPipelineImpl::Stop() {
92 CMALOG(kLogControl
) << "AudioPipelineImpl::Stop";
93 av_pipeline_impl_
->Stop();
94 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kStopped
);
97 void AudioPipelineImpl::SetCdm(BrowserCdmCast
* media_keys
) {
98 av_pipeline_impl_
->SetCdm(media_keys
);
101 void AudioPipelineImpl::Initialize(
102 const ::media::AudioDecoderConfig
& audio_config
,
103 scoped_ptr
<CodedFrameProvider
> frame_provider
,
104 const ::media::PipelineStatusCB
& status_cb
) {
105 CMALOG(kLogControl
) << "AudioPipelineImpl::Initialize "
106 << audio_config
.AsHumanReadableString();
108 SetCodedFrameProvider(frame_provider
.Pass());
110 if (!audio_device_
->SetConfig(audio_config
) ||
111 !av_pipeline_impl_
->Initialize()) {
112 status_cb
.Run(::media::PIPELINE_ERROR_INITIALIZATION_FAILED
);
115 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kFlushed
);
116 status_cb
.Run(::media::PIPELINE_OK
);
119 void AudioPipelineImpl::SetVolume(float volume
) {
120 audio_device_
->SetStreamVolumeMultiplier(volume
);
123 void AudioPipelineImpl::OnUpdateConfig(
124 const ::media::AudioDecoderConfig
& audio_config
,
125 const ::media::VideoDecoderConfig
& video_config
) {
126 if (audio_config
.IsValidConfig()) {
127 CMALOG(kLogControl
) << "AudioPipelineImpl::OnUpdateConfig "
128 << audio_config
.AsHumanReadableString();
130 bool success
= audio_device_
->SetConfig(audio_config
);
131 if (!success
&& !audio_client_
.playback_error_cb
.is_null())
132 audio_client_
.playback_error_cb
.Run(::media::PIPELINE_ERROR_DECODE
);
136 void AudioPipelineImpl::UpdateStatistics() {
137 if (audio_client_
.statistics_cb
.is_null())
140 MediaComponentDevice::Statistics device_stats
;
141 if (!audio_device_
->GetStatistics(&device_stats
))
144 ::media::PipelineStatistics current_stats
;
145 current_stats
.audio_bytes_decoded
= device_stats
.decoded_bytes
;
147 ::media::PipelineStatistics delta_stats
;
148 delta_stats
.audio_bytes_decoded
=
149 current_stats
.audio_bytes_decoded
- previous_stats_
.audio_bytes_decoded
;
151 previous_stats_
= current_stats
;
153 audio_client_
.statistics_cb
.Run(delta_stats
);
157 } // namespace chromecast