~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.2.1/pjmedia/src/pjmedia/sound_legacy.c

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: sound_legacy.c 4537 2013-06-19 06:47:43Z riza $ */
 
2
/* 
 
3
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 
18
 */
 
19
 
 
20
/*
 
21
 * This is implementation of legacy sound device API, for applications
 
22
 * that still use the old/deprecated sound device API. This implementation
 
23
 * uses the new Audio Device API.
 
24
 *
 
25
 * Please see http://trac.pjsip.org/repos/wiki/Audio_Dev_API for more
 
26
 * information.
 
27
 */
 
28
 
 
29
#include <pjmedia/sound.h>
 
30
#include <pjmedia-audiodev/errno.h>
 
31
#include <pj/assert.h>
 
32
 
 
33
#if PJMEDIA_HAS_LEGACY_SOUND_API
 
34
 
 
35
static struct legacy_subsys
 
36
{
 
37
    pjmedia_snd_dev_info     info[4];
 
38
    unsigned                 info_counter;
 
39
    unsigned                 user_rec_latency;
 
40
    unsigned                 user_play_latency;
 
41
} g_sys;
 
42
 
 
43
struct pjmedia_snd_stream
 
44
{
 
45
    pj_pool_t           *pool;
 
46
    pjmedia_aud_stream  *aud_strm;
 
47
    pjmedia_snd_rec_cb   user_rec_cb;
 
48
    pjmedia_snd_play_cb  user_play_cb;
 
49
    void                *user_user_data;
 
50
};
 
51
 
 
52
PJ_DEF(pj_status_t) pjmedia_snd_init(pj_pool_factory *factory)
 
53
{
 
54
    return pjmedia_aud_subsys_init(factory);
 
55
}
 
56
 
 
57
PJ_DEF(pj_status_t) pjmedia_snd_deinit(void)
 
58
{
 
59
    return pjmedia_aud_subsys_shutdown();
 
60
}
 
61
 
 
62
PJ_DEF(int) pjmedia_snd_get_dev_count(void)
 
63
{
 
64
    return pjmedia_aud_dev_count();
 
65
}
 
66
 
 
67
PJ_DEF(const pjmedia_snd_dev_info*) pjmedia_snd_get_dev_info(unsigned index)
 
68
{
 
69
    pjmedia_snd_dev_info *oi = &g_sys.info[g_sys.info_counter];
 
70
    pjmedia_aud_dev_info di;
 
71
 
 
72
    g_sys.info_counter = (g_sys.info_counter+1) % PJ_ARRAY_SIZE(g_sys.info);
 
73
 
 
74
    if (pjmedia_aud_dev_get_info(index, &di) != PJ_SUCCESS)
 
75
        return NULL;
 
76
 
 
77
    pj_bzero(oi, sizeof(*oi));
 
78
    pj_ansi_strncpy(oi->name, di.name, sizeof(oi->name));
 
79
    oi->name[sizeof(oi->name)-1] = '\0';
 
80
    oi->input_count = di.input_count;
 
81
    oi->output_count = di.output_count;
 
82
    oi->default_samples_per_sec = di.default_samples_per_sec;
 
83
 
 
84
    return oi;
 
85
}
 
86
 
 
87
 
 
88
static pj_status_t snd_play_cb(void *user_data,
 
89
                               pjmedia_frame *frame)
 
90
{
 
91
    pjmedia_snd_stream *strm = (pjmedia_snd_stream*)user_data;
 
92
 
 
93
    frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
 
94
    return strm->user_play_cb(strm->user_user_data, 
 
95
                              frame->timestamp.u32.lo,
 
96
                              frame->buf,
 
97
                              (unsigned)frame->size);
 
98
}
 
99
 
 
100
static pj_status_t snd_rec_cb(void *user_data,
 
101
                              pjmedia_frame *frame)
 
102
{
 
103
    pjmedia_snd_stream *strm = (pjmedia_snd_stream*)user_data;
 
104
    return strm->user_rec_cb(strm->user_user_data, 
 
105
                             frame->timestamp.u32.lo,
 
106
                             frame->buf,
 
107
                             (unsigned)frame->size);
 
108
}
 
109
 
 
110
static pj_status_t open_stream( pjmedia_dir dir,
 
111
                                int rec_id,
 
112
                                int play_id,
 
113
                                unsigned clock_rate,
 
114
                                unsigned channel_count,
 
115
                                unsigned samples_per_frame,
 
116
                                unsigned bits_per_sample,
 
117
                                pjmedia_snd_rec_cb rec_cb,
 
118
                                pjmedia_snd_play_cb play_cb,
 
119
                                void *user_data,
 
120
                                pjmedia_snd_stream **p_snd_strm)
 
121
{
 
122
    pj_pool_t *pool;
 
123
    pjmedia_snd_stream *snd_strm;
 
124
    pjmedia_aud_param param;
 
125
    pj_status_t status;
 
126
 
 
127
    /* Initialize parameters */
 
128
    if (dir & PJMEDIA_DIR_CAPTURE) {
 
129
        status = pjmedia_aud_dev_default_param(rec_id, &param);
 
130
    } else {
 
131
        status = pjmedia_aud_dev_default_param(play_id, &param);
 
132
    }
 
133
    if (status != PJ_SUCCESS)
 
134
        return status;
 
135
 
 
136
    param.dir = dir;
 
137
    param.rec_id = rec_id;
 
138
    param.play_id = play_id;
 
139
    param.clock_rate = clock_rate;
 
140
    param.channel_count = channel_count;
 
141
    param.samples_per_frame = samples_per_frame;
 
142
    param.bits_per_sample = bits_per_sample;
 
143
 
 
144
    /* Latencies setting */
 
145
    if ((dir & PJMEDIA_DIR_CAPTURE) && g_sys.user_rec_latency) {
 
146
        param.flags |= PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY;
 
147
        param.input_latency_ms = g_sys.user_rec_latency;
 
148
    }
 
149
    if ((dir & PJMEDIA_DIR_PLAYBACK) && g_sys.user_play_latency) {
 
150
        param.flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
 
151
        param.output_latency_ms = g_sys.user_play_latency;
 
152
    }
 
153
 
 
154
    /* Create sound wrapper */
 
155
    pool = pj_pool_create(pjmedia_aud_subsys_get_pool_factory(),
 
156
                          "legacy-snd", 512, 512, NULL);
 
157
    snd_strm = PJ_POOL_ZALLOC_T(pool, pjmedia_snd_stream);
 
158
    snd_strm->pool = pool;
 
159
    snd_strm->user_rec_cb = rec_cb;
 
160
    snd_strm->user_play_cb = play_cb;
 
161
    snd_strm->user_user_data = user_data;
 
162
 
 
163
    /* Create the stream */
 
164
    status = pjmedia_aud_stream_create(&param, &snd_rec_cb, 
 
165
                                       &snd_play_cb, snd_strm,
 
166
                                       &snd_strm->aud_strm);
 
167
    if (status != PJ_SUCCESS) {
 
168
        pj_pool_release(pool);
 
169
        return status;
 
170
    }
 
171
 
 
172
    *p_snd_strm = snd_strm;
 
173
    return PJ_SUCCESS;
 
174
}
 
175
 
 
176
PJ_DEF(pj_status_t) pjmedia_snd_open_rec( int index,
 
177
                                          unsigned clock_rate,
 
178
                                          unsigned channel_count,
 
179
                                          unsigned samples_per_frame,
 
180
                                          unsigned bits_per_sample,
 
181
                                          pjmedia_snd_rec_cb rec_cb,
 
182
                                          void *user_data,
 
183
                                          pjmedia_snd_stream **p_snd_strm)
 
184
{
 
185
    return open_stream(PJMEDIA_DIR_CAPTURE, index, PJMEDIA_AUD_INVALID_DEV,
 
186
                       clock_rate, channel_count, samples_per_frame,
 
187
                       bits_per_sample, rec_cb, NULL,
 
188
                       user_data, p_snd_strm);
 
189
}
 
190
 
 
191
PJ_DEF(pj_status_t) pjmedia_snd_open_player( int index,
 
192
                                        unsigned clock_rate,
 
193
                                        unsigned channel_count,
 
194
                                        unsigned samples_per_frame,
 
195
                                        unsigned bits_per_sample,
 
196
                                        pjmedia_snd_play_cb play_cb,
 
197
                                        void *user_data,
 
198
                                        pjmedia_snd_stream **p_snd_strm )
 
199
{
 
200
    return open_stream(PJMEDIA_DIR_PLAYBACK, PJMEDIA_AUD_INVALID_DEV, index, 
 
201
                       clock_rate, channel_count, samples_per_frame,
 
202
                       bits_per_sample, NULL, play_cb,
 
203
                       user_data, p_snd_strm);
 
204
}
 
205
 
 
206
PJ_DEF(pj_status_t) pjmedia_snd_open( int rec_id,
 
207
                                      int play_id,
 
208
                                      unsigned clock_rate,
 
209
                                      unsigned channel_count,
 
210
                                      unsigned samples_per_frame,
 
211
                                      unsigned bits_per_sample,
 
212
                                      pjmedia_snd_rec_cb rec_cb,
 
213
                                      pjmedia_snd_play_cb play_cb,
 
214
                                      void *user_data,
 
215
                                      pjmedia_snd_stream **p_snd_strm)
 
216
{
 
217
    return open_stream(PJMEDIA_DIR_CAPTURE_PLAYBACK, rec_id, play_id,
 
218
                       clock_rate, channel_count, samples_per_frame,
 
219
                       bits_per_sample, rec_cb, play_cb, 
 
220
                       user_data, p_snd_strm);
 
221
}
 
222
 
 
223
PJ_DEF(pj_status_t) pjmedia_snd_stream_start(pjmedia_snd_stream *stream)
 
224
{
 
225
    return pjmedia_aud_stream_start(stream->aud_strm);
 
226
}
 
227
 
 
228
PJ_DEF(pj_status_t) pjmedia_snd_stream_stop(pjmedia_snd_stream *stream)
 
229
{
 
230
    return pjmedia_aud_stream_stop(stream->aud_strm);
 
231
}
 
232
 
 
233
PJ_DEF(pj_status_t) pjmedia_snd_stream_get_info(pjmedia_snd_stream *strm,
 
234
                                                pjmedia_snd_stream_info *pi)
 
235
{
 
236
    pjmedia_aud_param param;
 
237
    pj_status_t status;
 
238
 
 
239
    status = pjmedia_aud_stream_get_param(strm->aud_strm, &param);
 
240
    if (status != PJ_SUCCESS)
 
241
        return status;
 
242
 
 
243
    pj_bzero(pi, sizeof(*pi));
 
244
    pi->dir = param.dir;
 
245
    pi->play_id = param.play_id;
 
246
    pi->rec_id = param.rec_id;
 
247
    pi->clock_rate = param.clock_rate;
 
248
    pi->channel_count = param.channel_count;
 
249
    pi->samples_per_frame = param.samples_per_frame;
 
250
    pi->bits_per_sample = param.bits_per_sample;
 
251
 
 
252
    if (param.flags & PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY) {
 
253
        pi->rec_latency = param.input_latency_ms;
 
254
    }
 
255
    if (param.flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY) {
 
256
        pi->play_latency = param.output_latency_ms;
 
257
    }
 
258
 
 
259
    return PJ_SUCCESS;
 
260
}
 
261
 
 
262
 
 
263
PJ_DEF(pj_status_t) pjmedia_snd_stream_close(pjmedia_snd_stream *stream)
 
264
{
 
265
    pj_status_t status;
 
266
 
 
267
    status = pjmedia_aud_stream_destroy(stream->aud_strm);
 
268
    if (status != PJ_SUCCESS)
 
269
        return status;
 
270
 
 
271
    pj_pool_release(stream->pool);
 
272
    return PJ_SUCCESS;
 
273
}
 
274
 
 
275
PJ_DEF(pj_status_t) pjmedia_snd_set_latency(unsigned input_latency, 
 
276
                                            unsigned output_latency)
 
277
{
 
278
    g_sys.user_rec_latency = input_latency;
 
279
    g_sys.user_play_latency = output_latency;
 
280
    return PJ_SUCCESS;
 
281
}
 
282
 
 
283
#endif  /* PJMEDIA_HAS_LEGACY_SOUND_API */
 
284