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