Debian Codegraph @

Xpjproject/pjsip-apps/src/symsndtest/app_main.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 #include <pjmedia-audiodev/audiodev.h>
20 #include <pjmedia/delaybuf.h>
21 #include <pj/assert.h>
22 #include <pj/errno.h>
23 #include <pj/os.h>
24 #include <pj/log.h>
25 #include <pj/string.h>
26 #include <pj/unicode.h>
27 #include <e32cons.h>
28
29 #define THIS_FILE "app_main.cpp"
30 #define CLOCK_RATE 8000
31 #define CHANNEL_COUNT 1
32 #define PTIME 20
33 #define SAMPLES_PER_FRAME (CLOCK_RATE*PTIME/1000)
34 #define BITS_PER_SAMPLE 16
35
36 extern CConsoleBase* console;
37
38 static pj_caching_pool cp;
39 static pjmedia_aud_stream *strm;
40 static unsigned rec_cnt, play_cnt;
41 static pj_time_val t_start;
42 static pjmedia_aud_param param;
43 static pj_pool_t *pool;
44 static pjmedia_delay_buf *delaybuf;
45 static char frame_buf[256];
46
47 static void copy_frame_ext(pjmedia_frame_ext *f_dst,
48 const pjmedia_frame_ext *f_src)
49 {
50 pj_bzero(f_dst, sizeof(*f_dst));
51 if (f_src->subframe_cnt) {
52 f_dst->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
53 for (unsigned i = 0; i < f_src->subframe_cnt; ++i) {
54 pjmedia_frame_ext_subframe *sf;
55 sf = pjmedia_frame_ext_get_subframe(f_src, i);
56 pjmedia_frame_ext_append_subframe(f_dst, sf->data, sf->bitlen,
57 param.samples_per_frame);
58 }
59 } else {
60 f_dst->base.type = PJMEDIA_FRAME_TYPE_NONE;
61 }
62 }
63
64 /* Logging callback */
65 static void log_writer(int level, const char *buf, unsigned len)
66 {
67 static wchar_t buf16[PJ_LOG_MAX_SIZE];
68
69 PJ_UNUSED_ARG(level);
70
71 pj_ansi_to_unicode(buf, len, buf16, PJ_ARRAY_SIZE(buf16));
72
73 TPtrC16 aBuf((const TUint16*)buf16, (TInt)len);
74 console->Write(aBuf);
75 }
76
77 /* perror util */
78 static void app_perror(const char *title, pj_status_t status)
79 {
80 char errmsg[PJ_ERR_MSG_SIZE];
81 pj_strerror(status, errmsg, sizeof(errmsg));
82 PJ_LOG(1,(THIS_FILE, "Error: %s: %s", title, errmsg));
83 }
84
85 /* Application init */
86 static pj_status_t app_init()
87 {
88 unsigned i, count;
89 pj_status_t status;
90
91 /* Redirect log */
92 pj_log_set_log_func((void (*)(int,const char*,int)) &log_writer);
93 pj_log_set_decor(PJ_LOG_HAS_NEWLINE);
94 pj_log_set_level(3);
95
96 /* Init pjlib */
97 status = pj_init();
98 if (status != PJ_SUCCESS) {
99 app_perror("pj_init()", status);
100 return status;
101 }
102
103 pj_caching_pool_init(&cp, NULL, 0);
104
105 /* Init sound subsystem */
106 status = pjmedia_aud_subsys_init(&cp.factory);
107 if (status != PJ_SUCCESS) {
108 app_perror("pjmedia_snd_init()", status);
109 pj_caching_pool_destroy(&cp);
110 pj_shutdown();
111 return status;
112 }
113
114 count = pjmedia_aud_dev_count();
115 PJ_LOG(3,(THIS_FILE, "Device count: %d", count));
116 for (i=0; i<count; ++i) {
117 pjmedia_aud_dev_info info;
118 pj_status_t status;
119
120 status = pjmedia_aud_dev_get_info(i, &info);
121 pj_assert(status == PJ_SUCCESS);
122 PJ_LOG(3, (THIS_FILE, "%d: %s %d/%d %dHz",
123 i, info.name, info.input_count, info.output_count,
124 info.default_samples_per_sec));
125
126 unsigned j;
127
128 /* Print extended formats supported by this audio device */
129 PJ_LOG(3, (THIS_FILE, " Extended formats supported:"));
130 for (j = 0; j < info.ext_fmt_cnt; ++j) {
131 const char *fmt_name = NULL;
132
133 switch (info.ext_fmt[j].id) {
134 case PJMEDIA_FORMAT_PCMA:
135 fmt_name = "PCMA";
136 break;
137 case PJMEDIA_FORMAT_PCMU:
138 fmt_name = "PCMU";
139 break;
140 case PJMEDIA_FORMAT_AMR:
141 fmt_name = "AMR-NB";
142 break;
143 case PJMEDIA_FORMAT_G729:
144 fmt_name = "G729";
145 break;
146 case PJMEDIA_FORMAT_ILBC:
147 fmt_name = "ILBC";
148 break;
149 case PJMEDIA_FORMAT_PCM:
150 fmt_name = "PCM";
151 break;
152 default:
153 fmt_name = "Unknown";
154 break;
155 }
156 PJ_LOG(3, (THIS_FILE, " - %s", fmt_name));
157 }
158 }
159
160 /* Create pool */
161 pool = pj_pool_create(&cp.factory, THIS_FILE, 512, 512, NULL);
162 if (pool == NULL) {
163 app_perror("pj_pool_create()", status);
164 pj_caching_pool_destroy(&cp);
165 pj_shutdown();
166 return status;
167 }
168
169 /* Init delay buffer */
170 status = pjmedia_delay_buf_create(pool, THIS_FILE, CLOCK_RATE,
171 SAMPLES_PER_FRAME, CHANNEL_COUNT,
172 0, 0, &delaybuf);
173 if (status != PJ_SUCCESS) {
174 app_perror("pjmedia_delay_buf_create()", status);
175 //pj_caching_pool_destroy(&cp);
176 //pj_shutdown();
177 //return status;
178 }
179
180 return PJ_SUCCESS;
181 }
182
183
184 /* Sound capture callback */
185 static pj_status_t rec_cb(void *user_data,
186 pjmedia_frame *frame)
187 {
188 PJ_UNUSED_ARG(user_data);
189
190 if (param.ext_fmt.id == PJMEDIA_FORMAT_PCM) {
191 pjmedia_delay_buf_put(delaybuf, (pj_int16_t*)frame->buf);
192
193 if (frame->size != SAMPLES_PER_FRAME*2) {
194 PJ_LOG(3, (THIS_FILE, "Size captured = %u",
195 frame->size));
196 }
197 } else {
198 pjmedia_frame_ext *f_src = (pjmedia_frame_ext*)frame;
199 pjmedia_frame_ext *f_dst = (pjmedia_frame_ext*)frame_buf;
200
201 copy_frame_ext(f_dst, f_src);
202 }
203
204 ++rec_cnt;
205 return PJ_SUCCESS;
206 }
207
208 /* Play cb */
209 static pj_status_t play_cb(void *user_data,
210 pjmedia_frame *frame)
211 {
212 PJ_UNUSED_ARG(user_data);
213
214 if (param.ext_fmt.id == PJMEDIA_FORMAT_PCM) {
215 pjmedia_delay_buf_get(delaybuf, (pj_int16_t*)frame->buf);
216 frame->size = SAMPLES_PER_FRAME*2;
217 frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
218 } else {
219 pjmedia_frame_ext *f_src = (pjmedia_frame_ext*)frame_buf;
220 pjmedia_frame_ext *f_dst = (pjmedia_frame_ext*)frame;
221
222 copy_frame_ext(f_dst, f_src);
223 }
224
225 ++play_cnt;
226 return PJ_SUCCESS;
227 }
228
229 /* Start sound */
230 static pj_status_t snd_start(unsigned flag)
231 {
232 pj_status_t status;
233
234 if (strm != NULL) {
235 app_perror("snd already open", PJ_EINVALIDOP);
236 return PJ_EINVALIDOP;
237 }
238
239 pjmedia_aud_dev_default_param(0, &param);
240 param.channel_count = CHANNEL_COUNT;
241 param.clock_rate = CLOCK_RATE;
242 param.samples_per_frame = SAMPLES_PER_FRAME;
243 param.dir = (pjmedia_dir) flag;
244 param.ext_fmt.id = PJMEDIA_FORMAT_AMR;
245 param.ext_fmt.bitrate = 12200;
246 param.output_route = PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER;
247
248 status = pjmedia_aud_stream_create(&param, &rec_cb, &play_cb, NULL, &strm);
249 if (status != PJ_SUCCESS) {
250 app_perror("snd open", status);
251 return status;
252 }
253
254 rec_cnt = play_cnt = 0;
255 pj_gettimeofday(&t_start);
256
257 pjmedia_delay_buf_reset(delaybuf);
258
259 status = pjmedia_aud_stream_start(strm);
260 if (status != PJ_SUCCESS) {
261 app_perror("snd start", status);
262 pjmedia_aud_stream_destroy(strm);
263 strm = NULL;
264 return status;
265 }
266
267 return PJ_SUCCESS;
268 }
269
270 /* Stop sound */
271 static pj_status_t snd_stop()
272 {
273 pj_time_val now;
274 pj_status_t status;
275
276 if (strm == NULL) {
277 app_perror("snd not open", PJ_EINVALIDOP);
278 return PJ_EINVALIDOP;
279 }
280
281 status = pjmedia_aud_stream_stop(strm);
282 if (status != PJ_SUCCESS) {
283 app_perror("snd failed to stop", status);
284 }
285 status = pjmedia_aud_stream_destroy(strm);
286 strm = NULL;
287
288 pj_gettimeofday(&now);
289 PJ_TIME_VAL_SUB(now, t_start);
290
291 PJ_LOG(3,(THIS_FILE, "Duration: %d.%03d", now.sec, now.msec));
292 PJ_LOG(3,(THIS_FILE, "Captured: %d", rec_cnt));
293 PJ_LOG(3,(THIS_FILE, "Played: %d", play_cnt));
294
295 return status;
296 }
297
298 /* Shutdown application */
299 static void app_fini()
300 {
301 if (strm)
302 snd_stop();
303
304 pjmedia_aud_subsys_shutdown();
305 pjmedia_delay_buf_destroy(delaybuf);
306 pj_pool_release(pool);
307 pj_caching_pool_destroy(&cp);
308 pj_shutdown();
309 }
310
311
312 ////////////////////////////////////////////////////////////////////////////
313 /*
314 * The interractive console UI
315 */
316 #include <e32base.h>
317
318 class ConsoleUI : public CActive
319 {
320 public:
321 ConsoleUI(CConsoleBase *con);
322
323 // Run console UI
324 void Run();
325
326 // Stop
327 void Stop();
328
329 protected:
330 // Cancel asynchronous read.
331 void DoCancel();
332
333 // Implementation: called when read has completed.
334 void RunL();
335
336 private:
337 CConsoleBase *con_;
338 };
339
340
341 ConsoleUI::ConsoleUI(CConsoleBase *con)
342 : CActive(EPriorityUserInput), con_(con)
343 {
344 CActiveScheduler::Add(this);
345 }
346
347 // Run console UI
348 void ConsoleUI::Run()
349 {
350 con_->Read(iStatus);
351 SetActive();
352 }
353
354 // Stop console UI
355 void ConsoleUI::Stop()
356 {
357 DoCancel();
358 }
359
360 // Cancel asynchronous read.
361 void ConsoleUI::DoCancel()
362 {
363 con_->ReadCancel();
364 }
365
366 static void PrintMenu()
367 {
368 PJ_LOG(3, (THIS_FILE, "\n\n"
369 "Menu:\n"
370 " a Start bidir sound\n"
371 " t Start recorder\n"
372 " p Start player\n"
373 " d Stop & close sound\n"
374 " w Quit\n"));
375 }
376
377 // Implementation: called when read has completed.
378 void ConsoleUI::RunL()
379 {
380 TKeyCode kc = con_->KeyCode();
381 pj_bool_t reschedule = PJ_TRUE;
382
383 switch (kc) {
384 case 'w':
385 snd_stop();
386 CActiveScheduler::Stop();
387 reschedule = PJ_FALSE;
388 break;
389 case 'a':
390 snd_start(PJMEDIA_DIR_CAPTURE_PLAYBACK);
391 break;
392 case 't':
393 snd_start(PJMEDIA_DIR_CAPTURE);
394 break;
395 case 'p':
396 snd_start(PJMEDIA_DIR_PLAYBACK);
397 break;
398 case 'd':
399 snd_stop();
400 break;
401 default:
402 PJ_LOG(3,(THIS_FILE, "Keycode '%c' (%d) is pressed",
403 kc, kc));
404 break;
405 }
406
407 PrintMenu();
408
409 if (reschedule)
410 Run();
411 }
412
413
414 ////////////////////////////////////////////////////////////////////////////
415 int app_main()
416 {
417 if (app_init() != PJ_SUCCESS)
418 return -1;
419
420 // Run the UI
421 ConsoleUI *con = new ConsoleUI(console);
422
423 con->Run();
424
425 PrintMenu();
426 CActiveScheduler::Start();
427
428 delete con;
429
430 app_fini();
431 return 0;
432 }
433