1
/* $Id: sound.h 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJMEDIA_SOUND_H__
21
#define __PJMEDIA_SOUND_H__
26
* @brief Legacy sound device API
28
#include <pjmedia-audiodev/audiodev.h>
29
#include <pjmedia/types.h>
35
* @defgroup PJMED_SND Portable Sound Hardware Abstraction
36
* @ingroup PJMED_SND_PORT
37
* @brief PJMEDIA abstraction for sound device hardware
40
* <strong>Warning: this sound device API has been deprecated
41
* and replaced by PJMEDIA Audio Device API. Please see
42
* http://trac.pjsip.org/repos/wiki/Audio_Dev_API for more
43
* information.</strong>
45
* This section describes lower level abstraction for sound device
46
* hardware. Application normally uses the higher layer @ref
47
* PJMED_SND_PORT abstraction since it works seamlessly with
50
* The sound hardware abstraction basically runs <b>asychronously</b>,
51
* and application must register callbacks to be called to receive/
52
* supply audio frames from/to the sound hardware.
54
* A full duplex sound stream (created with #pjmedia_snd_open())
55
* requires application to supply two callbacks:
56
* - <b><tt>rec_cb</tt></b> callback to be called when it has finished
57
* capturing one media frame, and
58
* - <b><tt>play_cb</tt></b> callback to be called when it needs media
59
* frame to be played to the sound playback hardware.
61
* Half duplex sound stream (created with #pjmedia_snd_open_rec() or
62
* #pjmedia_snd_open_player()) will only need one of the callback to
65
* After sound stream is created, application need to call
66
* #pjmedia_snd_stream_start() to start capturing/playing back media
67
* frames from/to the sound device.
70
/** Opaque declaration for pjmedia_snd_stream. */
71
typedef struct pjmedia_snd_stream pjmedia_snd_stream;
74
* Device information structure returned by #pjmedia_snd_get_dev_info.
76
typedef struct pjmedia_snd_dev_info
78
char name[64]; /**< Device name. */
79
unsigned input_count; /**< Max number of input channels. */
80
unsigned output_count; /**< Max number of output channels. */
81
unsigned default_samples_per_sec;/**< Default sampling rate. */
82
} pjmedia_snd_dev_info;
85
* Stream information, can be retrieved from a live stream by calling
86
* #pjmedia_snd_stream_get_info().
88
typedef struct pjmedia_snd_stream_info
90
pjmedia_dir dir; /**< Stream direction. */
91
int play_id; /**< Playback dev id, or -1 for rec only*/
92
int rec_id; /**< Capture dev id, or -1 for play only*/
93
unsigned clock_rate; /**< Actual clock rate. */
94
unsigned channel_count; /**< Number of channels. */
95
unsigned samples_per_frame; /**< Samples per frame. */
96
unsigned bits_per_sample; /**< Bits per sample. */
97
unsigned rec_latency; /**< Record latency, in samples. */
98
unsigned play_latency; /**< Playback latency, in samples. */
99
} pjmedia_snd_stream_info;
102
* This callback is called by player stream when it needs additional data
103
* to be played by the device. Application must fill in the whole of output
104
* buffer with sound samples.
106
* @param user_data User data associated with the stream.
107
* @param timestamp Timestamp, in samples.
108
* @param output Buffer to be filled out by application.
109
* @param size The size requested in bytes, which will be equal to
110
* the size of one whole packet.
112
* @return Non-zero to stop the stream.
114
typedef pj_status_t (*pjmedia_snd_play_cb)(/* in */ void *user_data,
115
/* in */ pj_uint32_t timestamp,
116
/* out */ void *output,
117
/* out */ unsigned size);
120
* This callback is called by recorder stream when it has captured the whole
121
* packet worth of audio samples.
123
* @param user_data User data associated with the stream.
124
* @param timestamp Timestamp, in samples.
125
* @param output Buffer containing the captured audio samples.
126
* @param size The size of the data in the buffer, in bytes.
128
* @return Non-zero to stop the stream.
130
typedef pj_status_t (*pjmedia_snd_rec_cb)(/* in */ void *user_data,
131
/* in */ pj_uint32_t timestamp,
132
/* in */ void *input,
133
/* in*/ unsigned size);
136
* Init the sound library.
138
* @param factory The sound factory.
140
* @return Zero on success.
142
PJ_DECL(pj_status_t) pjmedia_snd_init(pj_pool_factory *factory);
146
* Get the number of devices detected by the library.
148
* @return Number of devices.
150
PJ_DECL(int) pjmedia_snd_get_dev_count(void);
156
* @param index The index of the device, which should be in the range
157
* from zero to #pjmedia_snd_get_dev_count - 1.
159
PJ_DECL(const pjmedia_snd_dev_info*) pjmedia_snd_get_dev_info(unsigned index);
163
* Set sound device latency, this function must be called before sound device
164
* opened, or otherwise default latency setting will be used, @see
165
* PJMEDIA_SND_DEFAULT_REC_LATENCY & PJMEDIA_SND_DEFAULT_PLAY_LATENCY.
167
* Choosing latency value is not straightforward, it should accomodate both
168
* minimum latency and stability. Lower latency tends to cause sound device
169
* less reliable (producing audio dropouts) on CPU load disturbance. Moreover,
170
* the best latency setting may vary based on many aspects, e.g: sound card,
171
* CPU, OS, kernel, etc.
173
* @param input_latency The latency of input device, in ms, set to 0
174
* for default PJMEDIA_SND_DEFAULT_REC_LATENCY.
175
* @param output_latency The latency of output device, in ms, set to 0
176
* for default PJMEDIA_SND_DEFAULT_PLAY_LATENCY.
178
* @return PJ_SUCCESS on success.
180
PJ_DECL(pj_status_t) pjmedia_snd_set_latency(unsigned input_latency,
181
unsigned output_latency);
185
* Create sound stream for both capturing audio and audio playback, from the
186
* same device. This is the recommended way to create simultaneous recorder
187
* and player streams (instead of creating separate capture and playback
188
* streams), because it works on backends that does not allow
189
* a device to be opened more than once.
191
* @param rec_id Device index for recorder/capture stream, or
192
* -1 to use the first capable device.
193
* @param play_id Device index for playback stream, or -1 to use
194
* the first capable device.
195
* @param clock_rate Sound device's clock rate to set.
196
* @param channel_count Set number of channels, 1 for mono, or 2 for
197
* stereo. The channel count determines the format
199
* @param samples_per_frame Number of samples per frame.
200
* @param bits_per_sample Set the number of bits per sample. The normal
201
* value for this parameter is 16 bits per sample.
202
* @param rec_cb Callback to handle captured audio samples.
203
* @param play_cb Callback to be called when the sound player needs
204
* more audio samples to play.
205
* @param user_data User data to be associated with the stream.
206
* @param p_snd_strm Pointer to receive the stream instance.
208
* @return PJ_SUCCESS on success.
210
PJ_DECL(pj_status_t) pjmedia_snd_open(int rec_id,
213
unsigned channel_count,
214
unsigned samples_per_frame,
215
unsigned bits_per_sample,
216
pjmedia_snd_rec_cb rec_cb,
217
pjmedia_snd_play_cb play_cb,
219
pjmedia_snd_stream **p_snd_strm);
223
* Create a unidirectional audio stream for capturing audio samples from
226
* @param index Device index, or -1 to let the library choose the
227
* first available device.
228
* @param clock_rate Sound device's clock rate to set.
229
* @param channel_count Set number of channels, 1 for mono, or 2 for
230
* stereo. The channel count determines the format
232
* @param samples_per_frame Number of samples per frame.
233
* @param bits_per_sample Set the number of bits per sample. The normal
234
* value for this parameter is 16 bits per sample.
235
* @param rec_cb Callback to handle captured audio samples.
236
* @param user_data User data to be associated with the stream.
237
* @param p_snd_strm Pointer to receive the stream instance.
239
* @return PJ_SUCCESS on success.
241
PJ_DECL(pj_status_t) pjmedia_snd_open_rec( int index,
243
unsigned channel_count,
244
unsigned samples_per_frame,
245
unsigned bits_per_sample,
246
pjmedia_snd_rec_cb rec_cb,
248
pjmedia_snd_stream **p_snd_strm);
251
* Create a unidirectional audio stream for playing audio samples to the
254
* @param index Device index, or -1 to let the library choose the
255
* first available device.
256
* @param clock_rate Sound device's clock rate to set.
257
* @param channel_count Set number of channels, 1 for mono, or 2 for
258
* stereo. The channel count determines the format
260
* @param samples_per_frame Number of samples per frame.
261
* @param bits_per_sample Set the number of bits per sample. The normal
262
* value for this parameter is 16 bits per sample.
263
* @param play_cb Callback to be called when the sound player needs
264
* more audio samples to play.
265
* @param user_data User data to be associated with the stream.
266
* @param p_snd_strm Pointer to receive the stream instance.
268
* @return PJ_SUCCESS on success.
270
PJ_DECL(pj_status_t) pjmedia_snd_open_player( int index,
272
unsigned channel_count,
273
unsigned samples_per_frame,
274
unsigned bits_per_sample,
275
pjmedia_snd_play_cb play_cb,
277
pjmedia_snd_stream **p_snd_strm );
281
* Get information about live stream.
283
* @param strm The stream to be queried.
284
* @param pi Pointer to stream information to be filled up with
285
* information about the stream.
287
* @return PJ_SUCCESS on success or the appropriate error code.
289
PJ_DECL(pj_status_t) pjmedia_snd_stream_get_info(pjmedia_snd_stream *strm,
290
pjmedia_snd_stream_info *pi);
296
* @param stream The recorder or player stream.
298
* @return Zero on success.
300
PJ_DECL(pj_status_t) pjmedia_snd_stream_start(pjmedia_snd_stream *stream);
305
* @param stream The recorder or player stream.
307
* @return Zero on success.
309
PJ_DECL(pj_status_t) pjmedia_snd_stream_stop(pjmedia_snd_stream *stream);
312
* Destroy the stream.
314
* @param stream The recorder of player stream.
316
* @return Zero on success.
318
PJ_DECL(pj_status_t) pjmedia_snd_stream_close(pjmedia_snd_stream *stream);
321
* Deinitialize sound library.
323
* @return Zero on success.
325
PJ_DECL(pj_status_t) pjmedia_snd_deinit(void);
336
#endif /* __PJMEDIA_SOUND_H__ */