summaryrefslogtreecommitdiff
path: root/pjmedia/src/test/audio_tool.c
blob: 43e4cb7d5af8832feb63e4055a60e4763ed0452d (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* $Id$ */
/* 
 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
 *
 * 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 
 */
#include <pjmedia.h>
#include <pjlib.h>
#include <stdio.h>

#define THIS_FILE	"audio_tool.c"

static pj_caching_pool caching_pool;
static pj_pool_factory *pf;
static FILE *fhnd;
static pj_med_mgr_t *mm;
static pjmedia_codec *codec;
static pjmedia_codec_param cattr;


#define WRITE_ORIGINAL_PCM 0
#if WRITE_ORIGINAL_PCM
static FILE *fhnd_pcm;
#endif

static char talker_sdp[] = 
    "v=0\r\n"
    "o=- 0 0 IN IP4 127.0.0.1\r\n"
    "s=-\r\n"
    "c=IN IP4 127.0.0.1\r\n"
    "t=0 0\r\n"
    "m=audio 4002 RTP/AVP 0\r\n"
    "a=rtpmap:0 PCMU/8000\r\n"
    "a=sendonly\r\n";
static char listener_sdp[] = 
    "v=0\r\n"
    "o=- 0 0 IN IP4 127.0.0.1\r\n"
    "s=-\r\n"
    "c=IN IP4 127.0.0.1\r\n"
    "t=0 0\r\n"
    "m=audio 4000 RTP/AVP 0\r\n"
    "a=rtpmap:0 PCMU/8000\r\n"
    "a=recvonly\r\n";

static pj_status_t play_callback(/* in */   void *user_data,
				 /* in */   pj_uint32_t timestamp,
				 /* out */  void *frame,
				 /* out */  unsigned size)
{
    char pkt[160];
    struct pjmedia_frame in, out;
    int frmsz = cattr.avg_bps / 8 * cattr.ptime / 1000;

    if (fread(pkt, frmsz, 1, fhnd) != 1) {
	puts("EOF");
	return -1;
    } else {
	in.type = PJMEDIA_FRAME_TYPE_AUDIO;
	in.buf = pkt;
	in.size = frmsz;
	out.buf = frame;
	if (codec->op->decode (codec, &in, size, &out) != 0)
	    return -1;

	size = out.size;
	return 0;
    }
}

static pj_status_t rec_callback( /* in */   void *user_data,
			         /* in */   pj_uint32_t timestamp,
			         /* in */   const void *frame,
			         /* in*/    unsigned size)
{
    char pkt[160];
    struct pjmedia_frame in, out;
    //int frmsz = cattr.avg_bps / 8 * cattr.ptime / 1000;

#if WRITE_ORIGINAL_PCM
    fwrite(frame, size, 1, fhnd_pcm);
#endif

    in.type = PJMEDIA_FRAME_TYPE_AUDIO;
    in.buf = (void*)frame;
    in.size = size;
    out.buf = pkt;

    if (codec->op->encode(codec, &in, sizeof(pkt), &out) != 0)
	return -1;

    if (fwrite(pkt, out.size, 1, fhnd) != 1)
	return -1;
    return 0;
}

static pj_status_t init()
{
    pjmedia_codec_mgr *cm;
    pjmedia_codec_info id;
    int i;

    pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0);
    pf = &caching_pool.factory;

    if (pj_snd_init(&caching_pool.factory))
	return -1;

    PJ_LOG(3,(THIS_FILE, "Dumping audio devices:"));
    for (i=0; i<pj_snd_get_dev_count(); ++i) {
	const pj_snd_dev_info *info;
	info = pj_snd_get_dev_info(i);
	PJ_LOG(3,(THIS_FILE, "  %d: %s\t(%d in, %d out", 
			     i, info->name, 
			     info->input_count, info->output_count));
    }

    mm = pj_med_mgr_create (&caching_pool.factory);
    cm = pj_med_mgr_get_codec_mgr (mm);

    id.type = PJMEDIA_TYPE_AUDIO;
    id.pt = 0;
    id.encoding_name = pj_str("PCMU");
    id.sample_rate = 8000;

    codec = pjmedia_codec_mgr_alloc_codec (cm, &id);
    codec->op->default_attr(codec, &cattr);
    codec->op->open(codec, &cattr);
    return 0;
}

static pj_status_t deinit()
{
    pjmedia_codec_mgr *cm;
    cm = pj_med_mgr_get_codec_mgr (mm);
    codec->op->close(codec);
    pjmedia_codec_mgr_dealloc_codec (cm, codec);
    pj_med_mgr_destroy (mm);
    pj_caching_pool_destroy(&caching_pool);
    return 0;
}

static pj_status_t record_file (const char *filename)
{
    pj_snd_stream *stream;
    pj_snd_stream_info info;
    int status;
    char s[10];

    printf("Recording to file %s...\n", filename);

    fhnd = fopen(filename, "wb");
    if (!fhnd)
	return -1;

#if WRITE_ORIGINAL_PCM
    fhnd_pcm = fopen("ORIGINAL.PCM", "wb");
    if (!fhnd_pcm)
	return -1;
#endif

    pj_bzero(&info, sizeof(info));
    info.bits_per_sample = 16;
    info.bytes_per_frame = 2;
    info.frames_per_packet = 160;
    info.samples_per_frame = 1;
    info.samples_per_sec = 8000;

    stream = pj_snd_open_recorder(-1, &info, &rec_callback, NULL);
    if (!stream)
	return -1;

    status = pj_snd_stream_start(stream);
    if (status != 0)
	goto on_error;

    puts("Press <ENTER> to exit recording");
    fgets(s, sizeof(s), stdin);

    pj_snd_stream_stop(stream);
    pj_snd_stream_close(stream);

#if WRITE_ORIGINAL_PCM
    fclose(fhnd_pcm);
#endif
    fclose(fhnd);
    return 0;

on_error:
    pj_snd_stream_stop(stream);
    pj_snd_stream_close(stream);
    return -1;
}


static pj_status_t play_file (const char *filename)
{
    pj_snd_stream *stream;
    pj_snd_stream_info info;
    int status;
    char s[10];

    printf("Playing file %s...\n", filename);

    fhnd = fopen(filename, "rb");
    if (!fhnd)
	return -1;

    pj_bzero(&info, sizeof(info));
    info.bits_per_sample = 16;
    info.bytes_per_frame = 2;
    info.frames_per_packet = 160;
    info.samples_per_frame = 1;
    info.samples_per_sec = 8000;

    stream = pj_snd_open_player(-1, &info, &play_callback, NULL);
    if (!stream)
	return -1;

    status = pj_snd_stream_start(stream);
    if (status != 0)
	goto on_error;

    puts("Press <ENTER> to exit playing");
    fgets(s, sizeof(s), stdin);

    pj_snd_stream_stop(stream);
    pj_snd_stream_close(stream);

    fclose(fhnd);
    return 0;

on_error:
    pj_snd_stream_stop(stream);
    pj_snd_stream_close(stream);
    return -1;
}

static int create_ses_by_remote_sdp(int local_port, char *sdp)
{
    pj_media_session_t *ses = NULL;
    pjsdp_session_desc *sdp_ses;
    pj_media_sock_info skinfo;
    pj_pool_t *pool;
    char s[4];
    const pj_media_stream_info *info[2];
    int i, count;

    pool = pj_pool_create(pf, "sdp", 1024, 0, NULL);
    if (!pool) {
	PJ_LOG(1,(THIS_FILE, "Unable to create pool"));
	return -1;
    }

    pj_bzero(&skinfo, sizeof(skinfo));
    skinfo.rtp_sock = skinfo.rtcp_sock = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, 0);
    if (skinfo.rtp_sock == PJ_INVALID_SOCKET) {
	PJ_LOG(1,(THIS_FILE, "Unable to create socket"));
	goto on_error;
    }

    pj_sockaddr_init2(&skinfo.rtp_addr_name, "0.0.0.0", local_port);
    if (pj_sock_bind(skinfo.rtp_sock, (struct pj_sockaddr*)&skinfo.rtp_addr_name, sizeof(pj_sockaddr_in)) != 0) {
	PJ_LOG(1,(THIS_FILE, "Unable to bind socket"));
	goto on_error;
    }

    sdp_ses = pjsdp_parse(sdp, strlen(sdp), pool);
    if (!sdp_ses) {
	PJ_LOG(1,(THIS_FILE, "Error parsing SDP"));
	goto on_error;
    }

    ses = pj_media_session_create_from_sdp(mm, sdp_ses, &skinfo);
    if (!ses) {
	PJ_LOG(1,(THIS_FILE, "Unable to create session from SDP"));
	goto on_error;
    }

    if (pj_media_session_activate(ses) != 0) {
	PJ_LOG(1,(THIS_FILE, "Error activating session"));
	goto on_error;
    }

    count = pj_media_session_enum_streams(ses, 2, info);
    printf("\nDumping streams: \n");
    for (i=0; i<count; ++i) {
	const char *dir;
	char *local_ip;

	switch (info[i]->dir) {
	case PJMEDIA_DIR_NONE:
	    dir = "- NONE -"; break;
	case PJMEDIA_DIR_ENCODING:
	    dir = "SENDONLY"; break;
	case PJMEDIA_DIR_DECODING:
	    dir = "RECVONLY"; break;
	case PJMEDIA_DIR_ENCODING_DECODING:
	    dir = "SENDRECV"; break;
	default:
	    dir = "?UNKNOWN"; break;
	}

	local_ip = pj_sockaddr_get_str_addr(&info[i]->sock_info.rtp_addr_name);

	printf("  Stream %d: %.*s %s local=%s:%d remote=%.*s:%d\n",
	       i, info[i]->type.slen, info[i]->type.ptr,
	       dir, 
	       local_ip, pj_sockaddr_get_port(&info[i]->sock_info.rtp_addr_name),
	       info[i]->rem_addr.slen, info[i]->rem_addr.ptr, info[i]->rem_port);
    }

    puts("Press <ENTER> to quit");
    fgets(s, sizeof(s), stdin);

    pj_media_session_destroy(ses);
    pj_sock_close(skinfo.rtp_sock);
    pj_pool_release(pool);

    return 0;

on_error:
    if (ses)
	pj_media_session_destroy(ses);
    if (skinfo.rtp_sock != PJ_INVALID_SOCKET)
	pj_sock_close(skinfo.rtp_sock);
    if (pool)
	pj_pool_release(pool);
    return -1;
}

#if WRITE_ORIGINAL_PCM
static pj_status_t convert(const char *src, const char *dst)
{
    char pcm[320];
    char frame[160];
    struct pjmedia_frame in, out;

    fhnd_pcm = fopen(src, "rb");
    if (!fhnd_pcm)
	return -1;
    fhnd = fopen(dst, "wb");
    if (!fhnd)
	return -1;

    while (fread(pcm, 320, 1, fhnd_pcm) == 1) {

	in.type = PJMEDIA_FRAME_TYPE_AUDIO;
	in.buf = pcm;
	in.size = 320;
	out.buf = frame;

	if (codec->op->encode(codec, &in, 160, &out) != 0)
	    break;

	if (fwrite(frame, out.size, 1, fhnd) != 1)
	    break;

    }

    fclose(fhnd);
    fclose(fhnd_pcm);
    return 0;
}
#endif

static void usage(const char *exe)
{
    printf("Usage: %s <command> <file>\n", exe);
    puts("where:");
    puts("  <command>     play|record|send|recv");
}

int main(int argc, char *argv[])
{
    if (argc < 2) {
	usage(argv[0]);
	return 1;
    }

    pj_init();

    init();

    if (stricmp(argv[1], "record")==0) {
	record_file("FILE.PCM");
    } else if (stricmp(argv[1], "play")==0) {
	play_file("FILE.PCM");
    } else if (stricmp(argv[1], "send")==0) {
	create_ses_by_remote_sdp(4002, listener_sdp);
    } else if (stricmp(argv[1], "recv")==0) {
	create_ses_by_remote_sdp(4000, talker_sdp);
    } else {
	usage(argv[0]);
    }
    deinit();
    return 0;
}