summaryrefslogtreecommitdiff
path: root/pjmedia/src/pjmedia/sound_legacy.c
blob: d5bcf3dcfd434a3138786a38b51f17ce2d2311ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* $Id$ */
/* 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

/*
 * This is implementation of legacy sound device API, for applications
 * that still use the old/deprecated sound device API. This implementation
 * uses the new Audio Device API.
 *
 * Please see http://trac.pjsip.org/repos/wiki/Audio_Dev_API for more
 * information.
 */

#include <pjmedia/sound.h>
#include <pjmedia-audiodev/errno.h>
#include <pj/assert.h>

#if PJMEDIA_HAS_LEGACY_SOUND_API

static struct legacy_subsys
{
    pjmedia_snd_dev_info     info[4];
    unsigned		     info_counter;
    unsigned		     user_rec_latency;
    unsigned		     user_play_latency;
} g_sys;

struct pjmedia_snd_stream
{
    pj_pool_t		*pool;
    pjmedia_aud_stream	*aud_strm;
    pjmedia_snd_rec_cb	 user_rec_cb;
    pjmedia_snd_play_cb  user_play_cb;
    void		*user_user_data;
};

PJ_DEF(pj_status_t) pjmedia_snd_init(pj_pool_factory *factory)
{
    return pjmedia_aud_subsys_init(factory);
}

PJ_DEF(pj_status_t) pjmedia_snd_deinit(void)
{
    return pjmedia_aud_subsys_shutdown();
}

PJ_DEF(int) pjmedia_snd_get_dev_count(void)
{
    return pjmedia_aud_dev_count();
}

PJ_DEF(const pjmedia_snd_dev_info*) pjmedia_snd_get_dev_info(unsigned index)
{
    pjmedia_snd_dev_info *oi = &g_sys.info[g_sys.info_counter];
    pjmedia_aud_dev_info di;

    g_sys.info_counter = (g_sys.info_counter+1) % PJ_ARRAY_SIZE(g_sys.info);

    if (pjmedia_aud_dev_get_info(index, &di) != PJ_SUCCESS)
	return NULL;

    pj_bzero(oi, sizeof(*oi));
    pj_ansi_strncpy(oi->name, di.name, sizeof(oi->name));
    oi->name[sizeof(oi->name)-1] = '\0';
    oi->input_count = di.input_count;
    oi->output_count = di.output_count;
    oi->default_samples_per_sec = di.default_samples_per_sec;

    return oi;
}


static pj_status_t snd_play_cb(void *user_data,
			       pjmedia_frame *frame)
{
    pjmedia_snd_stream *strm = (pjmedia_snd_stream*)user_data;

    frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
    return strm->user_play_cb(strm->user_user_data, 
			      frame->timestamp.u32.lo,
			      frame->buf,
			      (unsigned)frame->size);
}

static pj_status_t snd_rec_cb(void *user_data,
			      pjmedia_frame *frame)
{
    pjmedia_snd_stream *strm = (pjmedia_snd_stream*)user_data;
    return strm->user_rec_cb(strm->user_user_data, 
			     frame->timestamp.u32.lo,
			     frame->buf,
			     (unsigned)frame->size);
}

static pj_status_t open_stream( pjmedia_dir dir,
			        int rec_id,
				int play_id,
				unsigned clock_rate,
				unsigned channel_count,
				unsigned samples_per_frame,
				unsigned bits_per_sample,
				pjmedia_snd_rec_cb rec_cb,
				pjmedia_snd_play_cb play_cb,
				void *user_data,
				pjmedia_snd_stream **p_snd_strm)
{
    pj_pool_t *pool;
    pjmedia_snd_stream *snd_strm;
    pjmedia_aud_param param;
    pj_status_t status;

    /* Normalize rec_id & play_id */
    if (dir & PJMEDIA_DIR_CAPTURE && rec_id < 0)
	rec_id = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV;
    if (dir & PJMEDIA_DIR_PLAYBACK && play_id < 0)
	play_id = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;

    /* Initialize parameters */
    if (dir & PJMEDIA_DIR_CAPTURE) {
	status = pjmedia_aud_dev_default_param(rec_id, &param);
    } else {
	status = pjmedia_aud_dev_default_param(play_id, &param);
    }
    if (status != PJ_SUCCESS)
	return status;

    param.dir = dir;
    param.rec_id = rec_id;
    param.play_id = play_id;
    param.clock_rate = clock_rate;
    param.channel_count = channel_count;
    param.samples_per_frame = samples_per_frame;
    param.bits_per_sample = bits_per_sample;

    /* Latencies setting */
    if ((dir & PJMEDIA_DIR_CAPTURE) && g_sys.user_rec_latency) {
	param.flags |= PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY;
	param.input_latency_ms = g_sys.user_rec_latency;
    }
    if ((dir & PJMEDIA_DIR_PLAYBACK) && g_sys.user_play_latency) {
	param.flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
	param.output_latency_ms = g_sys.user_play_latency;
    }

    /* Create sound wrapper */
    pool = pj_pool_create(pjmedia_aud_subsys_get_pool_factory(),
			  "legacy-snd", 512, 512, NULL);
    snd_strm = PJ_POOL_ZALLOC_T(pool, pjmedia_snd_stream);
    snd_strm->pool = pool;
    snd_strm->user_rec_cb = rec_cb;
    snd_strm->user_play_cb = play_cb;
    snd_strm->user_user_data = user_data;

    /* Create the stream */
    status = pjmedia_aud_stream_create(&param, &snd_rec_cb, 
				       &snd_play_cb, snd_strm,
				       &snd_strm->aud_strm);
    if (status != PJ_SUCCESS) {
	pj_pool_release(pool);
	return status;
    }

    *p_snd_strm = snd_strm;
    return PJ_SUCCESS;
}

PJ_DEF(pj_status_t) pjmedia_snd_open_rec( int index,
					  unsigned clock_rate,
					  unsigned channel_count,
					  unsigned samples_per_frame,
					  unsigned bits_per_sample,
					  pjmedia_snd_rec_cb rec_cb,
					  void *user_data,
					  pjmedia_snd_stream **p_snd_strm)
{
    return open_stream(PJMEDIA_DIR_CAPTURE, index, PJMEDIA_AUD_INVALID_DEV,
		       clock_rate, channel_count, samples_per_frame,
		       bits_per_sample, rec_cb, NULL,
		       user_data, p_snd_strm);
}

PJ_DEF(pj_status_t) pjmedia_snd_open_player( int index,
					unsigned clock_rate,
					unsigned channel_count,
					unsigned samples_per_frame,
					unsigned bits_per_sample,
					pjmedia_snd_play_cb play_cb,
					void *user_data,
					pjmedia_snd_stream **p_snd_strm )
{
    return open_stream(PJMEDIA_DIR_PLAYBACK, PJMEDIA_AUD_INVALID_DEV, index, 
		       clock_rate, channel_count, samples_per_frame,
		       bits_per_sample, NULL, play_cb,
		       user_data, p_snd_strm);
}

PJ_DEF(pj_status_t) pjmedia_snd_open( int rec_id,
				      int play_id,
				      unsigned clock_rate,
				      unsigned channel_count,
				      unsigned samples_per_frame,
				      unsigned bits_per_sample,
				      pjmedia_snd_rec_cb rec_cb,
				      pjmedia_snd_play_cb play_cb,
				      void *user_data,
				      pjmedia_snd_stream **p_snd_strm)
{
    return open_stream(PJMEDIA_DIR_CAPTURE_PLAYBACK, rec_id, play_id,
		       clock_rate, channel_count, samples_per_frame,
		       bits_per_sample, rec_cb, play_cb, 
		       user_data, p_snd_strm);
}

PJ_DEF(pj_status_t) pjmedia_snd_stream_start(pjmedia_snd_stream *stream)
{
    return pjmedia_aud_stream_start(stream->aud_strm);
}

PJ_DEF(pj_status_t) pjmedia_snd_stream_stop(pjmedia_snd_stream *stream)
{
    return pjmedia_aud_stream_stop(stream->aud_strm);
}

PJ_DEF(pj_status_t) pjmedia_snd_stream_get_info(pjmedia_snd_stream *strm,
						pjmedia_snd_stream_info *pi)
{
    pjmedia_aud_param param;
    pj_status_t status;

    status = pjmedia_aud_stream_get_param(strm->aud_strm, &param);
    if (status != PJ_SUCCESS)
	return status;

    pj_bzero(pi, sizeof(*pi));
    pi->dir = param.dir;
    pi->play_id = param.play_id;
    pi->rec_id = param.rec_id;
    pi->clock_rate = param.clock_rate;
    pi->channel_count = param.channel_count;
    pi->samples_per_frame = param.samples_per_frame;
    pi->bits_per_sample = param.bits_per_sample;

    if (param.flags & PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY) {
	pi->rec_latency = param.input_latency_ms;
    }
    if (param.flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY) {
	pi->play_latency = param.output_latency_ms;
    }

    return PJ_SUCCESS;
}


PJ_DEF(pj_status_t) pjmedia_snd_stream_close(pjmedia_snd_stream *stream)
{
    pj_status_t status;

    status = pjmedia_aud_stream_destroy(stream->aud_strm);
    if (status != PJ_SUCCESS)
	return status;

    pj_pool_release(stream->pool);
    return PJ_SUCCESS;
}

PJ_DEF(pj_status_t) pjmedia_snd_set_latency(unsigned input_latency, 
					    unsigned output_latency)
{
    g_sys.user_rec_latency = input_latency;
    g_sys.user_play_latency = output_latency;
    return PJ_SUCCESS;
}

#endif	/* PJMEDIA_HAS_LEGACY_SOUND_API */