OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/audio/linux/alsa_util.h" | 5 #include "media/audio/linux/alsa_util.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "media/audio/linux/alsa_wrapper.h" | 10 #include "media/audio/linux/alsa_wrapper.h" |
11 | 11 |
12 namespace { | 12 namespace alsa_util { |
13 | 13 |
14 snd_pcm_t* OpenDevice(AlsaWrapper* wrapper, | 14 static snd_pcm_t* OpenDevice(AlsaWrapper* wrapper, |
15 const char* device_name, | 15 const char* device_name, |
16 snd_pcm_stream_t type, | 16 snd_pcm_stream_t type, |
17 int channels, | 17 int channels, |
18 int sample_rate, | 18 int sample_rate, |
19 snd_pcm_format_t pcm_format, | 19 snd_pcm_format_t pcm_format, |
20 int latency_us) { | 20 int latency_us) { |
21 snd_pcm_t* handle = NULL; | 21 snd_pcm_t* handle = NULL; |
22 int error = wrapper->PcmOpen(&handle, device_name, type, SND_PCM_NONBLOCK); | 22 int error = wrapper->PcmOpen(&handle, device_name, type, SND_PCM_NONBLOCK); |
23 if (error < 0) { | 23 if (error < 0) { |
24 LOG(WARNING) << "PcmOpen: " << device_name << "," | 24 LOG(WARNING) << "PcmOpen: " << device_name << "," |
25 << wrapper->StrError(error); | 25 << wrapper->StrError(error); |
26 return NULL; | 26 return NULL; |
27 } | 27 } |
28 | 28 |
29 error = wrapper->PcmSetParams(handle, pcm_format, | 29 error = wrapper->PcmSetParams(handle, pcm_format, |
30 SND_PCM_ACCESS_RW_INTERLEAVED, channels, | 30 SND_PCM_ACCESS_RW_INTERLEAVED, channels, |
31 sample_rate, 1, latency_us); | 31 sample_rate, 1, latency_us); |
32 if (error < 0) { | 32 if (error < 0) { |
33 LOG(WARNING) << "PcmSetParams: " << device_name << ", " | 33 LOG(WARNING) << "PcmSetParams: " << device_name << ", " |
34 << wrapper->StrError(error) << " - Format: " << pcm_format | 34 << wrapper->StrError(error) << " - Format: " << pcm_format |
35 << " Channels: " << channels << " Latency: " << latency_us; | 35 << " Channels: " << channels << " Latency: " << latency_us; |
36 if (!alsa_util::CloseDevice(wrapper, handle)) { | 36 if (!alsa_util::CloseDevice(wrapper, handle)) { |
37 // TODO(ajwong): Retry on certain errors? | 37 // TODO(ajwong): Retry on certain errors? |
38 LOG(WARNING) << "Unable to close audio device. Leaking handle."; | 38 LOG(WARNING) << "Unable to close audio device. Leaking handle."; |
39 } | 39 } |
40 return NULL; | 40 return NULL; |
41 } | 41 } |
42 | 42 |
43 return handle; | 43 return handle; |
44 } | 44 } |
45 | 45 |
46 } // namespace | |
47 | |
48 namespace alsa_util { | |
49 | |
50 snd_pcm_format_t BitsToFormat(int bits_per_sample) { | 46 snd_pcm_format_t BitsToFormat(int bits_per_sample) { |
51 switch (bits_per_sample) { | 47 switch (bits_per_sample) { |
52 case 8: | 48 case 8: |
53 return SND_PCM_FORMAT_U8; | 49 return SND_PCM_FORMAT_U8; |
54 | 50 |
55 case 16: | 51 case 16: |
56 return SND_PCM_FORMAT_S16; | 52 return SND_PCM_FORMAT_S16; |
57 | 53 |
58 case 24: | 54 case 24: |
59 return SND_PCM_FORMAT_S24; | 55 return SND_PCM_FORMAT_S24; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 const char* device_name, | 87 const char* device_name, |
92 int channels, | 88 int channels, |
93 int sample_rate, | 89 int sample_rate, |
94 snd_pcm_format_t pcm_format, | 90 snd_pcm_format_t pcm_format, |
95 int latency_us) { | 91 int latency_us) { |
96 return OpenDevice(wrapper, device_name, SND_PCM_STREAM_PLAYBACK, channels, | 92 return OpenDevice(wrapper, device_name, SND_PCM_STREAM_PLAYBACK, channels, |
97 sample_rate, pcm_format, latency_us); | 93 sample_rate, pcm_format, latency_us); |
98 } | 94 } |
99 | 95 |
100 } // namespace alsa_util | 96 } // namespace alsa_util |
OLD | NEW |