blob: 119ffab63edb9d9be3cd0cdcf2c0101e0b5e617a [file] [log] [blame]
Benny Prijono06e62d72008-01-11 09:01:50 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono06e62d72008-01-11 09:01:50 +00005 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * \page page_pjmedia_samples_encdec_c Samples: Encoding and Decoding
23 *
24 * This sample shows how to use codec.
25 *
26 * This file is pjsip-apps/src/samples/encdec.c
27 *
28 * \includelineno encdec.c
29 */
30
31#include <pjlib.h>
32#include <pjmedia.h>
33#include <pjmedia-codec.h>
34
35#define THIS_FILE "encdec.c"
36
37static const char *desc =
38 " encdec \n"
39 " \n"
40 " PURPOSE: \n"
41 " Encode input WAV with a codec, and decode the result to another WAV \n"
42 "\n"
43 "\n"
44 " USAGE: \n"
45 " encdec codec input.wav output.wav \n"
46 "\n"
47 "\n"
48 " where:\n"
49 " codec Set the codec name. \n"
50 " input.wav Set the input WAV filename. \n"
51 " output.wav Set the output WAV filename. \n"
52
53 "\n"
54;
55
Benny Prijono4c5351f2008-01-11 16:30:22 +000056//#undef PJ_TRACE
57//#define PJ_TRACE 1
58
59#ifndef PJ_TRACE
60# define PJ_TRACE 0
61#endif
62
63#if PJ_TRACE
Benny Prijono06e62d72008-01-11 09:01:50 +000064# define TRACE_(expr) PJ_LOG(4,expr)
65#else
66# define TRACE_(expr)
67#endif
68
69
70static void err(const char *op, pj_status_t status)
71{
72 char errmsg[PJ_ERR_MSG_SIZE];
73 pj_strerror(status, errmsg, sizeof(errmsg));
74 PJ_LOG(3,("", "%s error: %s", op, errmsg));
75}
76
77#define CHECK(op) do { \
78 status = op; \
79 if (status != PJ_SUCCESS) { \
80 err(#op, status); \
81 return status; \
82 } \
83 } \
84 while (0)
85
86static pjmedia_endpt *mept;
87static unsigned file_msec_duration;
88
89static pj_status_t enc_dec_test(const char *codec_id,
90 const char *filein,
91 const char *fileout)
92{
93 pj_pool_t *pool;
94 pjmedia_codec_mgr *cm;
95 pjmedia_codec *codec;
Benny Prijono4c5351f2008-01-11 16:30:22 +000096 const pjmedia_codec_info *pci;
Benny Prijono06e62d72008-01-11 09:01:50 +000097 pjmedia_codec_param param;
98 unsigned cnt, samples_per_frame;
99 pj_str_t tmp;
100 pjmedia_port *wavin, *wavout;
Benny Prijono4c5351f2008-01-11 16:30:22 +0000101 unsigned lost_pct;
Benny Prijono06e62d72008-01-11 09:01:50 +0000102 pj_status_t status;
103
Benny Prijono4c5351f2008-01-11 16:30:22 +0000104#define T file_msec_duration/1000, file_msec_duration%1000
105
Benny Prijono06e62d72008-01-11 09:01:50 +0000106 pool = pjmedia_endpt_create_pool(mept, "encdec", 1000, 1000);
107
108 cm = pjmedia_endpt_get_codec_mgr(mept);
109
Benny Prijono4c5351f2008-01-11 16:30:22 +0000110#ifdef LOST_PCT
111 lost_pct = LOST_PCT;
112#else
113 lost_pct = 0;
114#endif
115
Benny Prijono06e62d72008-01-11 09:01:50 +0000116 cnt = 1;
117 CHECK( pjmedia_codec_mgr_find_codecs_by_id(cm, pj_cstr(&tmp, codec_id),
118 &cnt, &pci, NULL) );
119 CHECK( pjmedia_codec_mgr_get_default_param(cm, pci, &param) );
120
121 samples_per_frame = param.info.clock_rate * param.info.frm_ptime / 1000;
122
123 /* Control VAD */
Benny Prijono4c5351f2008-01-11 16:30:22 +0000124 param.setting.vad = 1;
Benny Prijono06e62d72008-01-11 09:01:50 +0000125
126 /* Open wav for reading */
127 CHECK( pjmedia_wav_player_port_create(pool, filein,
128 param.info.frm_ptime,
129 PJMEDIA_FILE_NO_LOOP, 0, &wavin) );
130
131 /* Open wav for writing */
132 CHECK( pjmedia_wav_writer_port_create(pool, fileout,
133 param.info.clock_rate,
134 param.info.channel_cnt,
135 samples_per_frame,
136 16, 0, 0, &wavout) );
137
138 /* Alloc codec */
139 CHECK( pjmedia_codec_mgr_alloc_codec(cm, pci, &codec) );
140 CHECK( codec->op->init(codec, pool) );
141 CHECK( codec->op->open(codec, &param) );
142
143 for (;;) {
144 pjmedia_frame frm_pcm, frm_bit, out_frm, frames[4];
145 pj_int16_t pcmbuf[320];
146 pj_timestamp ts;
147 pj_uint8_t bitstream[160];
148
149 frm_pcm.buf = (char*)pcmbuf;
150 frm_pcm.size = samples_per_frame * 2;
151
152 /* Read from WAV */
153 if (pjmedia_port_get_frame(wavin, &frm_pcm) != PJ_SUCCESS)
154 break;
155 if (frm_pcm.type != PJMEDIA_FRAME_TYPE_AUDIO)
156 break;;
157
Benny Prijono4c5351f2008-01-11 16:30:22 +0000158 /* Update duration */
159 file_msec_duration += samples_per_frame * 1000 /
160 param.info.clock_rate;
161
Benny Prijono06e62d72008-01-11 09:01:50 +0000162 /* Encode */
163 frm_bit.buf = bitstream;
164 frm_bit.size = sizeof(bitstream);
165 CHECK(codec->op->encode(codec, &frm_pcm, sizeof(bitstream), &frm_bit));
166
Benny Prijono4c5351f2008-01-11 16:30:22 +0000167 /* On DTX, write zero frame to wavout to maintain duration */
168 if (frm_bit.size == 0 || frm_bit.type != PJMEDIA_FRAME_TYPE_AUDIO) {
169 out_frm.buf = (char*)pcmbuf;
170 out_frm.size = 160;
171 CHECK( pjmedia_port_put_frame(wavout, &out_frm) );
172 TRACE_((THIS_FILE, "%d.%03d read: %u, enc: %u",
173 T, frm_pcm.size, frm_bit.size));
174 continue;
175 }
176
Benny Prijono06e62d72008-01-11 09:01:50 +0000177 /* Parse the bitstream (not really necessary for this case
178 * since we always decode 1 frame, but it's still good
179 * for testing)
180 */
181 ts.u64 = 0;
182 cnt = PJ_ARRAY_SIZE(frames);
183 CHECK( codec->op->parse(codec, bitstream, frm_bit.size, &ts, &cnt,
184 frames) );
185 CHECK( (cnt==1 ? PJ_SUCCESS : -1) );
186
Benny Prijono4c5351f2008-01-11 16:30:22 +0000187 /* Decode or simulate packet loss */
Benny Prijono06e62d72008-01-11 09:01:50 +0000188 out_frm.buf = (char*)pcmbuf;
189 out_frm.size = sizeof(pcmbuf);
Benny Prijono4c5351f2008-01-11 16:30:22 +0000190
Benny Prijonoa2687702008-01-12 11:23:02 +0000191 if ((pj_rand() % 100) < (int)lost_pct) {
Benny Prijono4c5351f2008-01-11 16:30:22 +0000192 /* Simulate loss */
193 CHECK( codec->op->recover(codec, sizeof(pcmbuf), &out_frm) );
194 TRACE_((THIS_FILE, "%d.%03d Packet lost", T));
195 } else {
196 /* Decode */
197 CHECK( codec->op->decode(codec, &frames[0], sizeof(pcmbuf),
198 &out_frm) );
199 }
Benny Prijono06e62d72008-01-11 09:01:50 +0000200
201 /* Write to WAV */
202 CHECK( pjmedia_port_put_frame(wavout, &out_frm) );
203
Benny Prijono4c5351f2008-01-11 16:30:22 +0000204 TRACE_((THIS_FILE, "%d.%03d read: %u, enc: %u, dec/write: %u",
205 T, frm_pcm.size, frm_bit.size, out_frm.size));
Benny Prijono06e62d72008-01-11 09:01:50 +0000206 }
207
208 /* Close wavs */
209 pjmedia_port_destroy(wavout);
210 pjmedia_port_destroy(wavin);
211
212 /* Close codec */
213 codec->op->close(codec);
214 pjmedia_codec_mgr_dealloc_codec(cm, codec);
215
216 /* Release pool */
217 pj_pool_release(pool);
218
219 return PJ_SUCCESS;
220}
221
222
223int main(int argc, char *argv[])
224{
225 pj_caching_pool cp;
226 pj_time_val t0, t1;
227 pj_status_t status;
228
229 if (argc != 4) {
230 puts(desc);
231 return 1;
232 }
233
234 CHECK( pj_init() );
235
236 pj_caching_pool_init(&cp, NULL, 0);
237
238 CHECK( pjmedia_endpt_create(&cp.factory, NULL, 1, &mept) );
239
240 /* Register all codecs */
241#if PJMEDIA_HAS_G711_CODEC
242 CHECK( pjmedia_codec_g711_init(mept) );
243#endif
244#if PJMEDIA_HAS_GSM_CODEC
245 CHECK( pjmedia_codec_gsm_init(mept) );
246#endif
247#if PJMEDIA_HAS_ILBC_CODEC
248 CHECK( pjmedia_codec_ilbc_init(mept, 30) );
249#endif
250#if PJMEDIA_HAS_SPEEX_CODEC
251 CHECK( pjmedia_codec_speex_init(mept, 0, 5, 5) );
252#endif
Benny Prijono7ffd7752008-03-17 14:07:53 +0000253#if PJMEDIA_HAS_G722_CODEC
254 CHECK( pjmedia_codec_g722_init(mept) );
255#endif
Benny Prijono06e62d72008-01-11 09:01:50 +0000256
257 pj_gettimeofday(&t0);
258 status = enc_dec_test(argv[1], argv[2], argv[3]);
259 pj_gettimeofday(&t1);
260 PJ_TIME_VAL_SUB(t1, t0);
261
262 pjmedia_endpt_destroy(mept);
263 pj_caching_pool_destroy(&cp);
264 pj_shutdown();
265
266 if (status == PJ_SUCCESS) {
267 puts("");
268 puts("Success");
269 printf("Duration: %ds.%03d\n", file_msec_duration/1000,
270 file_msec_duration%1000);
Benny Prijono4c5351f2008-01-11 16:30:22 +0000271 printf("Time: %lds.%03ld\n", t1.sec, t1.msec);
Benny Prijono06e62d72008-01-11 09:01:50 +0000272 }
273
274 return 0;
275}
276