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/audio/cast_audio_manager.h"
7 #include "chromecast/base/task_runner_impl.h"
8 #include "chromecast/media/audio/cast_audio_output_stream.h"
9 #include "chromecast/public/cast_media_shlib.h"
10 #include "chromecast/public/media/media_pipeline_backend.h"
11 #include "chromecast/public/media/media_pipeline_device_params.h"
14 // TODO(alokp): Query the preferred value from media backend.
15 const int kDefaultSampleRate
= 48000;
17 // Define bounds for the output buffer size (in frames).
18 // Note: These values are copied from AudioManagerPulse implementation.
19 // TODO(alokp): Query the preferred value from media backend.
20 static const int kMinimumOutputBufferSize
= 512;
21 static const int kMaximumOutputBufferSize
= 8192;
22 static const int kDefaultOutputBufferSize
= 2048;
25 namespace chromecast
{
28 CastAudioManager::CastAudioManager(::media::AudioLogFactory
* audio_log_factory
)
29 : AudioManagerBase(audio_log_factory
) {}
31 CastAudioManager::~CastAudioManager() {
35 bool CastAudioManager::HasAudioOutputDevices() {
39 bool CastAudioManager::HasAudioInputDevices() {
43 void CastAudioManager::ShowAudioInputSettings() {
44 LOG(WARNING
) << "No support for input audio devices";
47 void CastAudioManager::GetAudioInputDeviceNames(
48 ::media::AudioDeviceNames
* device_names
) {
49 DCHECK(device_names
->empty());
50 LOG(WARNING
) << "No support for input audio devices";
53 ::media::AudioParameters
CastAudioManager::GetInputStreamParameters(
54 const std::string
& device_id
) {
55 LOG(WARNING
) << "No support for input audio devices";
56 // Need to send a valid AudioParameters object even when it will unused.
57 return ::media::AudioParameters(
58 ::media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
59 ::media::CHANNEL_LAYOUT_STEREO
, 48000, 16, 1024);
62 scoped_ptr
<MediaPipelineBackend
>
63 CastAudioManager::CreateMediaPipelineBackend() {
64 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
66 if (!audio_task_runner_
)
67 audio_task_runner_
.reset(new TaskRunnerImpl());
69 MediaPipelineDeviceParams
device_params(
70 MediaPipelineDeviceParams::kModeIgnorePts
, audio_task_runner_
.get());
71 return scoped_ptr
<MediaPipelineBackend
>(
72 CastMediaShlib::CreateMediaPipelineBackend(device_params
));
75 ::media::AudioOutputStream
* CastAudioManager::MakeLinearOutputStream(
76 const ::media::AudioParameters
& params
) {
77 DCHECK_EQ(::media::AudioParameters::AUDIO_PCM_LINEAR
, params
.format());
78 return new CastAudioOutputStream(params
, this);
81 ::media::AudioOutputStream
* CastAudioManager::MakeLowLatencyOutputStream(
82 const ::media::AudioParameters
& params
,
83 const std::string
& device_id
) {
84 DCHECK_EQ(::media::AudioParameters::AUDIO_PCM_LOW_LATENCY
, params
.format());
85 return new CastAudioOutputStream(params
, this);
88 ::media::AudioInputStream
* CastAudioManager::MakeLinearInputStream(
89 const ::media::AudioParameters
& params
,
90 const std::string
& device_id
) {
91 LOG(WARNING
) << "No support for input audio devices";
95 ::media::AudioInputStream
* CastAudioManager::MakeLowLatencyInputStream(
96 const ::media::AudioParameters
& params
,
97 const std::string
& device_id
) {
98 LOG(WARNING
) << "No support for input audio devices";
102 ::media::AudioParameters
CastAudioManager::GetPreferredOutputStreamParameters(
103 const std::string
& output_device_id
,
104 const ::media::AudioParameters
& input_params
) {
105 ::media::ChannelLayout channel_layout
= ::media::CHANNEL_LAYOUT_STEREO
;
106 int sample_rate
= kDefaultSampleRate
;
107 int buffer_size
= kDefaultOutputBufferSize
;
108 int bits_per_sample
= 16;
109 if (input_params
.IsValid()) {
111 // - the channel layout
112 // - the number of bits per sample
113 // We support stereo only with 16 bits per sample.
114 sample_rate
= input_params
.sample_rate();
115 buffer_size
= std::min(
116 kMaximumOutputBufferSize
,
117 std::max(kMinimumOutputBufferSize
, input_params
.frames_per_buffer()));
120 ::media::AudioParameters
output_params(
121 ::media::AudioParameters::AUDIO_PCM_LOW_LATENCY
, channel_layout
,
122 sample_rate
, bits_per_sample
, buffer_size
);
123 return output_params
;
127 } // namespace chromecast