Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(898)

Side by Side Diff: media/audio/linux/alsa_util.cc

Issue 3299005: Implement audio recording for Linux via ALSA. (Closed)
Patch Set: . Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/audio/linux/alsa_util.h ('k') | media/audio/linux/alsa_wrapper.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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.
4
5 #include "media/audio/linux/alsa_util.h"
6
7 #include <string>
8
9 #include "base/logging.h"
10 #include "media/audio/linux/alsa_wrapper.h"
11
12 namespace {
13
14 snd_pcm_t* OpenDevice(AlsaWrapper* wrapper,
15 const char* device_name,
16 snd_pcm_stream_t type,
17 int channels,
18 int sample_rate,
19 snd_pcm_format_t pcm_format,
20 int latency_us) {
21 snd_pcm_t* handle = NULL;
22 int error = wrapper->PcmOpen(&handle, device_name, type, SND_PCM_NONBLOCK);
23 if (error < 0) {
24 LOG(WARNING) << "PcmOpen: " << device_name << ","
25 << wrapper->StrError(error);
26 return NULL;
27 }
28
29 error = wrapper->PcmSetParams(handle, pcm_format,
30 SND_PCM_ACCESS_RW_INTERLEAVED, channels,
31 sample_rate, 1, latency_us);
32 if (error < 0) {
33 LOG(WARNING) << "PcmSetParams: " << device_name << ", "
34 << wrapper->StrError(error) << " - Format: " << pcm_format
35 << " Channels: " << channels << " Latency: " << latency_us;
36 if (!alsa_util::CloseDevice(wrapper, handle)) {
37 // TODO(ajwong): Retry on certain errors?
38 LOG(WARNING) << "Unable to close audio device. Leaking handle.";
39 }
40 return NULL;
41 }
42
43 return handle;
44 }
45
46 }
awong 2010/09/02 21:16:08 // namespace
47
48 namespace alsa_util {
49
50 snd_pcm_format_t BitsToFormat(int bits_per_sample) {
51 switch (bits_per_sample) {
52 case 8:
53 return SND_PCM_FORMAT_U8;
54
55 case 16:
56 return SND_PCM_FORMAT_S16;
57
58 case 24:
59 return SND_PCM_FORMAT_S24;
60
61 case 32:
62 return SND_PCM_FORMAT_S32;
63
64 default:
65 return SND_PCM_FORMAT_UNKNOWN;
66 }
67 }
68
69 int CloseDevice(AlsaWrapper* wrapper, snd_pcm_t* handle) {
70 std::string device_name = wrapper->PcmName(handle);
71 int error = wrapper->PcmClose(handle);
72 if (error < 0) {
73 LOG(ERROR) << "PcmClose: " << device_name << ", "
74 << wrapper->StrError(error);
75 }
76
77 return error;
78 }
79
80 snd_pcm_t* OpenCaptureDevice(AlsaWrapper* wrapper,
81 const char* device_name,
82 int channels,
83 int sample_rate,
84 snd_pcm_format_t pcm_format,
85 int latency_us) {
86 return OpenDevice(wrapper, device_name, SND_PCM_STREAM_CAPTURE, channels,
87 sample_rate, pcm_format, latency_us);
88 }
89
90 snd_pcm_t* OpenPlaybackDevice(AlsaWrapper* wrapper,
91 const char* device_name,
92 int channels,
93 int sample_rate,
94 snd_pcm_format_t pcm_format,
95 int latency_us) {
96 return OpenDevice(wrapper, device_name, SND_PCM_STREAM_PLAYBACK, channels,
97 sample_rate, pcm_format, latency_us);
98 }
99
100 }
awong 2010/09/02 21:16:08 // namespace alsa_util remove extra newline.
101
OLDNEW
« no previous file with comments | « media/audio/linux/alsa_util.h ('k') | media/audio/linux/alsa_wrapper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698