ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chromecast / media / cma / pipeline / audio_pipeline_impl.cc
blob404031faa56d06d23903106a253bf83906dbe4db
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"
7 #include "base/bind.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 {
16 namespace media {
18 namespace {
19 const size_t kMaxAudioFrameSize = 32 * 1024;
22 AudioPipelineImpl::AudioPipelineImpl(AudioPipelineDevice* audio_device)
23 : audio_device_(audio_device),
24 weak_factory_(this) {
25 av_pipeline_impl_.reset(new AvPipelineImpl(
26 audio_device_,
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(
46 base::TimeDelta time,
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();
54 // Start playing.
55 if (av_pipeline_impl_->GetState() == AvPipelineImpl::kError)
56 return false;
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);
61 return false;
63 av_pipeline_impl_->TransitionToState(AvPipelineImpl::kPlaying);
65 return true;
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);
72 return;
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);
85 return;
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();
107 if (frame_provider)
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);
113 return;
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())
138 return;
140 MediaComponentDevice::Statistics device_stats;
141 if (!audio_device_->GetStatistics(&device_stats))
142 return;
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);
156 } // namespace media
157 } // namespace chromecast