blob: a88e77d08ebf8a020470793326d76874a5d47d9b [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
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#include <pjmedia/wav_port.h>
21#include <pjmedia/alaw_ulaw.h>
22#include <pjmedia/errno.h>
23#include <pjmedia/wave.h>
24#include <pj/assert.h>
25#include <pj/file_access.h>
26#include <pj/file_io.h>
27#include <pj/log.h>
28#include <pj/pool.h>
29#include <pj/string.h>
30
31
32#define THIS_FILE "wav_writer.c"
33#define SIGNATURE PJMEDIA_SIG_PORT_WAV_WRITER
34
35
36struct file_port
37{
38 pjmedia_port base;
39 pjmedia_wave_fmt_tag fmt_tag;
40 pj_uint16_t bytes_per_sample;
41
42 pj_size_t bufsize;
43 char *buf;
44 char *writepos;
45 pj_size_t total;
46
47 pj_oshandle_t fd;
48
49 pj_size_t cb_size;
50 pj_status_t (*cb)(pjmedia_port*, void*);
51};
52
53static pj_status_t file_put_frame(pjmedia_port *this_port,
54 pjmedia_frame *frame);
55static pj_status_t file_get_frame(pjmedia_port *this_port,
56 pjmedia_frame *frame);
57static pj_status_t file_on_destroy(pjmedia_port *this_port);
58
59
60/*
61 * Create file writer port.
62 */
63PJ_DEF(pj_status_t) pjmedia_wav_writer_port_create( pj_pool_t *pool,
64 const char *filename,
65 unsigned sampling_rate,
66 unsigned channel_count,
67 unsigned samples_per_frame,
68 unsigned bits_per_sample,
69 unsigned flags,
70 pj_ssize_t buff_size,
71 pjmedia_port **p_port )
72{
73 struct file_port *fport;
74 pjmedia_wave_hdr wave_hdr;
75 pj_ssize_t size;
76 pj_str_t name;
77 pj_status_t status;
78
79 /* Check arguments. */
80 PJ_ASSERT_RETURN(pool && filename && p_port, PJ_EINVAL);
81
82 /* Only supports 16bits per sample for now.
83 * See flush_buffer().
84 */
85 PJ_ASSERT_RETURN(bits_per_sample == 16, PJ_EINVAL);
86
87 /* Create file port instance. */
88 fport = PJ_POOL_ZALLOC_T(pool, struct file_port);
89 PJ_ASSERT_RETURN(fport != NULL, PJ_ENOMEM);
90
91 /* Initialize port info. */
92 pj_strdup2(pool, &name, filename);
93 pjmedia_port_info_init(&fport->base.info, &name, SIGNATURE,
94 sampling_rate, channel_count, bits_per_sample,
95 samples_per_frame);
96
97 fport->base.get_frame = &file_get_frame;
98 fport->base.put_frame = &file_put_frame;
99 fport->base.on_destroy = &file_on_destroy;
100
101 if (flags == PJMEDIA_FILE_WRITE_ALAW) {
102 fport->fmt_tag = PJMEDIA_WAVE_FMT_TAG_ALAW;
103 fport->bytes_per_sample = 1;
104 } else if (flags == PJMEDIA_FILE_WRITE_ULAW) {
105 fport->fmt_tag = PJMEDIA_WAVE_FMT_TAG_ULAW;
106 fport->bytes_per_sample = 1;
107 } else {
108 fport->fmt_tag = PJMEDIA_WAVE_FMT_TAG_PCM;
109 fport->bytes_per_sample = 2;
110 }
111
112 /* Open file in write and read mode.
113 * We need the read mode because we'll modify the WAVE header once
114 * the recording has completed.
115 */
116 status = pj_file_open(pool, filename, PJ_O_WRONLY, &fport->fd);
117 if (status != PJ_SUCCESS)
118 return status;
119
120 /* Initialize WAVE header */
121 pj_bzero(&wave_hdr, sizeof(pjmedia_wave_hdr));
122 wave_hdr.riff_hdr.riff = PJMEDIA_RIFF_TAG;
123 wave_hdr.riff_hdr.file_len = 0; /* will be filled later */
124 wave_hdr.riff_hdr.wave = PJMEDIA_WAVE_TAG;
125
126 wave_hdr.fmt_hdr.fmt = PJMEDIA_FMT_TAG;
127 wave_hdr.fmt_hdr.len = 16;
128 wave_hdr.fmt_hdr.fmt_tag = (pj_uint16_t)fport->fmt_tag;
129 wave_hdr.fmt_hdr.nchan = (pj_int16_t)channel_count;
130 wave_hdr.fmt_hdr.sample_rate = sampling_rate;
131 wave_hdr.fmt_hdr.bytes_per_sec = sampling_rate * channel_count *
132 fport->bytes_per_sample;
133 wave_hdr.fmt_hdr.block_align = (pj_uint16_t)
134 (fport->bytes_per_sample * channel_count);
135 wave_hdr.fmt_hdr.bits_per_sample = (pj_uint16_t)
136 (fport->bytes_per_sample * 8);
137
138 wave_hdr.data_hdr.data = PJMEDIA_DATA_TAG;
139 wave_hdr.data_hdr.len = 0; /* will be filled later */
140
141
142 /* Convert WAVE header from host byte order to little endian
143 * before writing the header.
144 */
145 pjmedia_wave_hdr_host_to_file(&wave_hdr);
146
147
148 /* Write WAVE header */
149 if (fport->fmt_tag != PJMEDIA_WAVE_FMT_TAG_PCM) {
150 pjmedia_wave_subchunk fact_chunk;
151 pj_uint32_t tmp = 0;
152
153 fact_chunk.id = PJMEDIA_FACT_TAG;
154 fact_chunk.len = 4;
155
156 PJMEDIA_WAVE_NORMALIZE_SUBCHUNK(&fact_chunk);
157
158 /* Write WAVE header without DATA chunk header */
159 size = sizeof(pjmedia_wave_hdr) - sizeof(wave_hdr.data_hdr);
160 status = pj_file_write(fport->fd, &wave_hdr, &size);
161 if (status != PJ_SUCCESS) {
162 pj_file_close(fport->fd);
163 return status;
164 }
165
166 /* Write FACT chunk if it stores compressed data */
167 size = sizeof(fact_chunk);
168 status = pj_file_write(fport->fd, &fact_chunk, &size);
169 if (status != PJ_SUCCESS) {
170 pj_file_close(fport->fd);
171 return status;
172 }
173 size = 4;
174 status = pj_file_write(fport->fd, &tmp, &size);
175 if (status != PJ_SUCCESS) {
176 pj_file_close(fport->fd);
177 return status;
178 }
179
180 /* Write DATA chunk header */
181 size = sizeof(wave_hdr.data_hdr);
182 status = pj_file_write(fport->fd, &wave_hdr.data_hdr, &size);
183 if (status != PJ_SUCCESS) {
184 pj_file_close(fport->fd);
185 return status;
186 }
187 } else {
188 size = sizeof(pjmedia_wave_hdr);
189 status = pj_file_write(fport->fd, &wave_hdr, &size);
190 if (status != PJ_SUCCESS) {
191 pj_file_close(fport->fd);
192 return status;
193 }
194 }
195
196 /* Set buffer size. */
197 if (buff_size < 1) buff_size = PJMEDIA_FILE_PORT_BUFSIZE;
198 fport->bufsize = buff_size;
199
200 /* Check that buffer size is greater than bytes per frame */
201 pj_assert(fport->bufsize >= PJMEDIA_PIA_AVG_FSZ(&fport->base.info));
202
203
204 /* Allocate buffer and set initial write position */
205 fport->buf = (char*) pj_pool_alloc(pool, fport->bufsize);
206 if (fport->buf == NULL) {
207 pj_file_close(fport->fd);
208 return PJ_ENOMEM;
209 }
210 fport->writepos = fport->buf;
211
212 /* Done. */
213 *p_port = &fport->base;
214
215 PJ_LOG(4,(THIS_FILE,
216 "File writer '%.*s' created: samp.rate=%d, bufsize=%uKB",
217 (int)fport->base.info.name.slen,
218 fport->base.info.name.ptr,
219 PJMEDIA_PIA_SRATE(&fport->base.info),
220 fport->bufsize / 1000));
221
222
223 return PJ_SUCCESS;
224}
225
226
227
228/*
229 * Get current writing position.
230 */
231PJ_DEF(pj_ssize_t) pjmedia_wav_writer_port_get_pos( pjmedia_port *port )
232{
233 struct file_port *fport;
234
235 /* Sanity check */
236 PJ_ASSERT_RETURN(port, -PJ_EINVAL);
237
238 /* Check that this is really a writer port */
239 PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, -PJ_EINVALIDOP);
240
241 fport = (struct file_port*) port;
242
243 return fport->total;
244}
245
246
247/*
248 * Register callback.
249 */
250PJ_DEF(pj_status_t) pjmedia_wav_writer_port_set_cb( pjmedia_port *port,
251 pj_size_t pos,
252 void *user_data,
253 pj_status_t (*cb)(pjmedia_port *port,
254 void *usr_data))
255{
256 struct file_port *fport;
257
258 /* Sanity check */
259 PJ_ASSERT_RETURN(port && cb, PJ_EINVAL);
260
261 /* Check that this is really a writer port */
262 PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVALIDOP);
263
264 fport = (struct file_port*) port;
265
266 fport->cb_size = pos;
267 fport->base.port_data.pdata = user_data;
268 fport->cb = cb;
269
270 return PJ_SUCCESS;
271}
272
273
274#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
275 static void swap_samples(pj_int16_t *samples, unsigned count)
276 {
277 unsigned i;
278 for (i=0; i<count; ++i) {
279 samples[i] = pj_swap16(samples[i]);
280 }
281 }
282#else
283# define swap_samples(samples,count)
284#endif
285
286/*
287 * Flush the contents of the buffer to the file.
288 */
289static pj_status_t flush_buffer(struct file_port *fport)
290{
291 pj_ssize_t bytes = fport->writepos - fport->buf;
292 pj_status_t status;
293
294 /* Convert samples to little endian */
295 swap_samples((pj_int16_t*)fport->buf, bytes/fport->bytes_per_sample);
296
297 /* Write to file. */
298 status = pj_file_write(fport->fd, fport->buf, &bytes);
299
300 /* Reset writepos */
301 fport->writepos = fport->buf;
302
303 return status;
304}
305
306/*
307 * Put a frame into the buffer. When the buffer is full, flush the buffer
308 * to the file.
309 */
310static pj_status_t file_put_frame(pjmedia_port *this_port,
311 pjmedia_frame *frame)
312{
313 struct file_port *fport = (struct file_port *)this_port;
314 pj_size_t frame_size;
315
316 if (fport->fmt_tag == PJMEDIA_WAVE_FMT_TAG_PCM)
317 frame_size = frame->size;
318 else
319 frame_size = frame->size >> 1;
320
321 /* Flush buffer if we don't have enough room for the frame. */
322 if (fport->writepos + frame_size > fport->buf + fport->bufsize) {
323 pj_status_t status;
324 status = flush_buffer(fport);
325 if (status != PJ_SUCCESS)
326 return status;
327 }
328
329 /* Check if frame is not too large. */
330 PJ_ASSERT_RETURN(fport->writepos+frame_size <= fport->buf+fport->bufsize,
331 PJMEDIA_EFRMFILETOOBIG);
332
333 /* Copy frame to buffer. */
334 if (fport->fmt_tag == PJMEDIA_WAVE_FMT_TAG_PCM) {
335 pj_memcpy(fport->writepos, frame->buf, frame->size);
336 } else {
337 unsigned i;
338 pj_int16_t *src = (pj_int16_t*)frame->buf;
339 pj_uint8_t *dst = (pj_uint8_t*)fport->writepos;
340
341 if (fport->fmt_tag == PJMEDIA_WAVE_FMT_TAG_ULAW) {
342 for (i = 0; i < frame_size; ++i) {
343 *dst++ = pjmedia_linear2ulaw(*src++);
344 }
345 } else {
346 for (i = 0; i < frame_size; ++i) {
347 *dst++ = pjmedia_linear2alaw(*src++);
348 }
349 }
350
351 }
352 fport->writepos += frame_size;
353
354 /* Increment total written, and check if we need to call callback */
355 fport->total += frame_size;
356 if (fport->cb && fport->total >= fport->cb_size) {
357 pj_status_t (*cb)(pjmedia_port*, void*);
358 pj_status_t status;
359
360 cb = fport->cb;
361 fport->cb = NULL;
362
363 status = (*cb)(this_port, this_port->port_data.pdata);
364 return status;
365 }
366
367 return PJ_SUCCESS;
368}
369
370/*
371 * Get frame, basicy is a no-op operation.
372 */
373static pj_status_t file_get_frame(pjmedia_port *this_port,
374 pjmedia_frame *frame)
375{
376 PJ_UNUSED_ARG(this_port);
377 PJ_UNUSED_ARG(frame);
378 return PJ_EINVALIDOP;
379}
380
381/*
382 * Close the port, modify file header with updated file length.
383 */
384static pj_status_t file_on_destroy(pjmedia_port *this_port)
385{
386 enum { FILE_LEN_POS = 4, DATA_LEN_POS = 40 };
387 struct file_port *fport = (struct file_port *)this_port;
388 pj_off_t file_size;
389 pj_ssize_t bytes;
390 pj_uint32_t wave_file_len;
391 pj_uint32_t wave_data_len;
392 pj_status_t status;
393 pj_uint32_t data_len_pos = DATA_LEN_POS;
394
395 /* Flush remaining buffers. */
396 if (fport->writepos != fport->buf)
397 flush_buffer(fport);
398
399 /* Get file size. */
400 status = pj_file_getpos(fport->fd, &file_size);
401 if (status != PJ_SUCCESS) {
402 pj_file_close(fport->fd);
403 return status;
404 }
405
406 /* Calculate wave fields */
407 wave_file_len = (pj_uint32_t)(file_size - 8);
408 wave_data_len = (pj_uint32_t)(file_size - sizeof(pjmedia_wave_hdr));
409
410#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
411 wave_file_len = pj_swap32(wave_file_len);
412 wave_data_len = pj_swap32(wave_data_len);
413#endif
414
415 /* Seek to the file_len field. */
416 status = pj_file_setpos(fport->fd, FILE_LEN_POS, PJ_SEEK_SET);
417 if (status != PJ_SUCCESS) {
418 pj_file_close(fport->fd);
419 return status;
420 }
421
422 /* Write file_len */
423 bytes = sizeof(wave_file_len);
424 status = pj_file_write(fport->fd, &wave_file_len, &bytes);
425 if (status != PJ_SUCCESS) {
426 pj_file_close(fport->fd);
427 return status;
428 }
429
430 /* Write samples_len in FACT chunk */
431 if (fport->fmt_tag != PJMEDIA_WAVE_FMT_TAG_PCM) {
432 enum { SAMPLES_LEN_POS = 44};
433 pj_uint32_t wav_samples_len;
434
435 /* Adjust wave_data_len & data_len_pos since there is FACT chunk */
436 wave_data_len -= 12;
437 data_len_pos += 12;
438 wav_samples_len = wave_data_len;
439
440 /* Seek to samples_len field. */
441 status = pj_file_setpos(fport->fd, SAMPLES_LEN_POS, PJ_SEEK_SET);
442 if (status != PJ_SUCCESS) {
443 pj_file_close(fport->fd);
444 return status;
445 }
446
447 /* Write samples_len */
448 bytes = sizeof(wav_samples_len);
449 status = pj_file_write(fport->fd, &wav_samples_len, &bytes);
450 if (status != PJ_SUCCESS) {
451 pj_file_close(fport->fd);
452 return status;
453 }
454 }
455
456 /* Seek to data_len field. */
457 status = pj_file_setpos(fport->fd, data_len_pos, PJ_SEEK_SET);
458 if (status != PJ_SUCCESS) {
459 pj_file_close(fport->fd);
460 return status;
461 }
462
463 /* Write file_len */
464 bytes = sizeof(wave_data_len);
465 status = pj_file_write(fport->fd, &wave_data_len, &bytes);
466 if (status != PJ_SUCCESS) {
467 pj_file_close(fport->fd);
468 return status;
469 }
470
471 /* Close file */
472 status = pj_file_close(fport->fd);
473 if (status != PJ_SUCCESS)
474 return status;
475
476 /* Done. */
477 return PJ_SUCCESS;
478}
479