summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMisael Lopez Cruz2013-11-05 20:32:55 -0600
committerMisael Lopez Cruz2013-11-11 13:01:16 -0600
commit50db622848f29e112a98ef910dbaa3e82fe3be28 (patch)
tree25b78d565c92c4321d0c13b532e71ba2a741e5cb
parent3dadd189dc6a30f7eb1e07f2afaac04207c3b21e (diff)
downloaddevice-ti-common-open-50db622848f29e112a98ef910dbaa3e82fe3be28.tar.gz
device-ti-common-open-50db622848f29e112a98ef910dbaa3e82fe3be28.tar.xz
device-ti-common-open-50db622848f29e112a98ef910dbaa3e82fe3be28.zip
audio: utils: Add null pcm ports
Null PCM playback and capture ports are dummy ports that produce and consume audio buffers at the same rate that an actual hardware port would. They can be used to provide valid sources or sinks to PCM readers/writers when actual hardware ports are not available. Change-Id: Ibe54104b8542534fe0414bdc6ad0beedaba303d7 Signed-off-by: Misael Lopez Cruz <misael.lopez@ti.com>
-rw-r--r--audio/utils/Android.mk1
-rw-r--r--audio/utils/include/tiaudioutils/NullPcm.h230
-rw-r--r--audio/utils/src/NullPcm.cpp96
3 files changed, 327 insertions, 0 deletions
diff --git a/audio/utils/Android.mk b/audio/utils/Android.mk
index c038c56..13983ca 100644
--- a/audio/utils/Android.mk
+++ b/audio/utils/Android.mk
@@ -21,6 +21,7 @@ LOCAL_MODULE := libtiaudioutils
21LOCAL_SRC_FILES := \ 21LOCAL_SRC_FILES := \
22 src/Base.cpp \ 22 src/Base.cpp \
23 src/Pcm.cpp \ 23 src/Pcm.cpp \
24 src/NullPcm.cpp \
24 src/ALSAPcm.cpp \ 25 src/ALSAPcm.cpp \
25 src/ALSAMixer.cpp \ 26 src/ALSAMixer.cpp \
26 src/SimpleStream.cpp \ 27 src/SimpleStream.cpp \
diff --git a/audio/utils/include/tiaudioutils/NullPcm.h b/audio/utils/include/tiaudioutils/NullPcm.h
new file mode 100644
index 0000000..8995759
--- /dev/null
+++ b/audio/utils/include/tiaudioutils/NullPcm.h
@@ -0,0 +1,230 @@
1/*
2 * Copyright (C) 2013 Texas Instruments
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * \file NullPcm.h
19 * \brief Classes for accessing the PCM interface of a null card
20 *
21 * Contains classes required to play and capture data from a null sound card.
22 */
23
24#ifndef _TIAUDIOUTILS_NULLPCM_H_
25#define _TIAUDIOUTILS_NULLPCM_H_
26
27#include <limits.h>
28
29#include <tiaudioutils/Base.h>
30#include <tiaudioutils/Pcm.h>
31
32namespace tiaudioutils {
33
34/**
35 * \class NullInPort
36 * \brief Null capture port
37 *
38 * The null PCM port used for capture. Buffers are filled with silence by the
39 * null port at the same rate that an actual hardware port would.
40 */
41class NullInPort : public PcmInPort {
42 public:
43 /**
44 * \brief Null input port constructor
45 *
46 * Constructs a null input/capture port.
47 */
48 NullInPort();
49
50 /**
51 * \brief Null input port destructor
52 *
53 * Destroys a null input/capture port object.
54 */
55 virtual ~NullInPort() {}
56
57 /**
58 * \brief Get the id of the sound card
59 *
60 * Gets the id of the sound card that this port belongs to. Neither the
61 * null port or the card actually exist in the system (as opposite to
62 * the ALSA dummy/null card), so the returned id is chosen not to
63 * conflict with any valid card that may be registered in the system.
64 *
65 * \return The sound card id
66 */
67 uint32_t getCardId() const { return UINT_MAX; }
68
69 /**
70 * \brief Get the port id
71 *
72 * Gets the null input/capture port id. The id itself is valid but
73 * meaningless since there is no sound card in the system for the null PCM.
74 *
75 * \return The PCM port id
76 */
77 uint32_t getPortId() const { return 0; }
78
79 /**
80 * \brief Open the null port for capture
81 *
82 * Opens the null PCM port for capture with the given parameters. There is
83 * no hardware port actually opened by this method. The PCM parameters
84 * used to open the port are used to calculate the buffer time in read().
85 *
86 * \param params PcmParams used to open the port
87 * \return 0 on success, otherwise negative error code
88 */
89 int open(const PcmParams &params);
90
91 /**
92 * \brief Close the null PCM port
93 *
94 * Closes the null PCM port opened by calling open(). There is no hardware
95 * port actually closed by this method.
96 */
97 void close();
98
99 /**
100 * \brief Test if the PCM port is open and ready
101 *
102 * Tests if the PCM port is open and ready for capture.
103 *
104 * \return true if the port is open, false otherwise
105 */
106 bool isOpen() const { return mOpen; }
107
108 /**
109 * \brief Read audio data from the null PCM input port
110 *
111 * Reads data from the null port which produces a buffer filled with silence.
112 * No actual data is read from hardware, but the call blocks for the time
113 * equivalent to the requested frames.
114 *
115 * \param buffer Pointer to the destination buffer
116 * \param frames Number of frames to be read
117 * \return 0 on success, otherwise negative error code
118 */
119 int read(void *buffer, size_t frames);
120
121 private:
122 NullInPort(const NullInPort &port);
123 NullInPort& operator=(const NullInPort &port);
124
125 protected:
126 bool mOpen; /**< State of the port: open or closed */
127 PcmParams mParams; /**< PCM params of the port */
128 Mutex mLock; /**< Synchronize PCM port use */
129};
130
131/**
132 * \class NullOutPort
133 * \brief Null playback port
134 *
135 * The null PCM port used for playback. Buffers are consumed by the null port
136 * at the same rate that an actual hardware port would.
137 */
138class NullOutPort : public PcmOutPort {
139 public:
140 /**
141 * \brief Null output port constructor
142 *
143 * Constructs a null output/playback port.
144 */
145 NullOutPort();
146
147 /**
148 * \brief Null output port destructor
149 *
150 * Destroys a null output/playback port object.
151 */
152 virtual ~NullOutPort() {}
153
154 /**
155 * \brief Get the id of the sound card
156 *
157 *
158 * Gets the id of the sound card that this port belongs to. Neither the
159 * null port or the card actually exist in the system (as opposite to
160 * the ALSA dummy/null card), so the returned id is chosen not to
161 * conflict with any valid card that may be registered in the system.
162 *
163 * \return The sound card id
164 */
165 uint32_t getCardId() const { return UINT_MAX; }
166
167 /**
168 * \brief Get the port id
169 *
170 * Gets the null output/playback port id. The id itself is valid but
171 * meaningless since there is no sound card in the system for the null PCM.
172 *
173 * \return The PCM port id
174 */
175 uint32_t getPortId() const { return 0; }
176
177 /**
178 * \brief Open the null port for playback
179 *
180 * Opens the null PCM port for playback with the given parameters. There is
181 * no hardware port actually opened by this method. The PCM parameters
182 * used to open the port are used to calculate the buffer time in write().
183 *
184 * \param params PcmParams used to open the port
185 * \return 0 on success, otherwise negative error code
186 */
187 int open(const PcmParams &params);
188
189 /**
190 * \brief Close the null PCM port
191 *
192 * Closes the null PCM port opened by calling open(). There is no hardware
193 * port actually closed by this method.
194 */
195 void close();
196
197 /**
198 * \brief Test if the PCM port is open and ready
199 *
200 * Tests if the PCM port is open and ready for playback.
201 *
202 * \return true if the port is open, false otherwise
203 */
204 bool isOpen() const { return mOpen; }
205
206 /**
207 * \brief Write audio data to the null PCM output port
208 *
209 * Writes data to the null output port. No data write actually occurs
210 * but the call blocks for the time equivalent to the requested frames.
211 *
212 * \param buffer Pointer to the source buffer
213 * \param frames Number of frames to be written
214 * \return Number of frames written, otherwise negative error code
215 */
216 int write(const void *buffer, size_t frames);
217
218 private:
219 NullOutPort(const NullOutPort &port);
220 NullOutPort& operator=(const NullOutPort &port);
221
222 protected:
223 bool mOpen; /**< State of the port: open or closed */
224 PcmParams mParams; /**< PCM params of the port */
225 Mutex mLock; /**< Synchronize PCM port use */
226};
227
228} /* namespace tiaudioutils */
229
230#endif /* _TIAUDIOUTILS_NULLPCM_H_ */
diff --git a/audio/utils/src/NullPcm.cpp b/audio/utils/src/NullPcm.cpp
new file mode 100644
index 0000000..3e0a0b0
--- /dev/null
+++ b/audio/utils/src/NullPcm.cpp
@@ -0,0 +1,96 @@
1/*
2 * Copyright (C) 2013 Texas Instruments
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// #define LOG_NDEBUG 0
18// #define VERY_VERBOSE_LOGGING
19
20#include <errno.h>
21
22#include <tiaudioutils/Log.h>
23#include <tiaudioutils/NullPcm.h>
24
25namespace tiaudioutils {
26
27NullInPort::NullInPort()
28 : mOpen(false)
29{
30}
31
32int NullInPort::open(const PcmParams &params)
33{
34 AutoMutex lock(mLock);
35 mParams = params;
36 mOpen = true;
37
38 return 0;
39}
40
41void NullInPort::close()
42{
43 AutoMutex lock(mLock);
44 mOpen = false;
45}
46
47int NullInPort::read(void *buffer, size_t frames)
48{
49 AutoMutex lock(mLock);
50 if (!mOpen) {
51 ALOGE("NullInPort: port is closed, cannot read");
52 return -EAGAIN;
53 }
54
55 memset(buffer, 0, mParams.framesToBytes(frames));
56 usleep((frames * 1000000) / mParams.sampleRate);
57
58 return frames;
59}
60
61/* ---------------------------------------------------------------------------------------- */
62
63NullOutPort::NullOutPort()
64 : mOpen(false)
65{
66}
67
68int NullOutPort::open(const PcmParams &params)
69{
70 AutoMutex lock(mLock);
71 mParams = params;
72 mOpen = true;
73
74 return 0;
75}
76
77void NullOutPort::close()
78{
79 AutoMutex lock(mLock);
80 mOpen = false;
81}
82
83int NullOutPort::write(const void *buffer, size_t frames)
84{
85 AutoMutex lock(mLock);
86 if (!mOpen) {
87 ALOGE("NullOutPort: port is closed, cannot write");
88 return -EAGAIN;
89 }
90
91 usleep((frames * 1000000) / mParams.sampleRate);
92
93 return frames;
94}
95
96} /* namespace tiaudioutils */