1
/* $Id: wav_writer.c 3664 2011-07-19 03:42:28Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
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>
29
#include <pj/string.h>
32
#define THIS_FILE "wav_writer.c"
33
#define SIGNATURE PJMEDIA_SIG_PORT_WAV_WRITER
39
pjmedia_wave_fmt_tag fmt_tag;
40
pj_uint16_t bytes_per_sample;
50
pj_status_t (*cb)(pjmedia_port*, void*);
53
static pj_status_t file_put_frame(pjmedia_port *this_port,
54
pjmedia_frame *frame);
55
static pj_status_t file_get_frame(pjmedia_port *this_port,
56
pjmedia_frame *frame);
57
static pj_status_t file_on_destroy(pjmedia_port *this_port);
61
* Create file writer port.
63
PJ_DEF(pj_status_t) pjmedia_wav_writer_port_create( pj_pool_t *pool,
65
unsigned sampling_rate,
66
unsigned channel_count,
67
unsigned samples_per_frame,
68
unsigned bits_per_sample,
71
pjmedia_port **p_port )
73
struct file_port *fport;
74
pjmedia_wave_hdr wave_hdr;
79
/* Check arguments. */
80
PJ_ASSERT_RETURN(pool && filename && p_port, PJ_EINVAL);
82
/* Only supports 16bits per sample for now.
85
PJ_ASSERT_RETURN(bits_per_sample == 16, PJ_EINVAL);
87
/* Create file port instance. */
88
fport = PJ_POOL_ZALLOC_T(pool, struct file_port);
89
PJ_ASSERT_RETURN(fport != NULL, PJ_ENOMEM);
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,
97
fport->base.get_frame = &file_get_frame;
98
fport->base.put_frame = &file_put_frame;
99
fport->base.on_destroy = &file_on_destroy;
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;
108
fport->fmt_tag = PJMEDIA_WAVE_FMT_TAG_PCM;
109
fport->bytes_per_sample = 2;
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.
116
status = pj_file_open(pool, filename, PJ_O_WRONLY, &fport->fd);
117
if (status != PJ_SUCCESS)
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;
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);
138
wave_hdr.data_hdr.data = PJMEDIA_DATA_TAG;
139
wave_hdr.data_hdr.len = 0; /* will be filled later */
142
/* Convert WAVE header from host byte order to little endian
143
* before writing the header.
145
pjmedia_wave_hdr_host_to_file(&wave_hdr);
148
/* Write WAVE header */
149
if (fport->fmt_tag != PJMEDIA_WAVE_FMT_TAG_PCM) {
150
pjmedia_wave_subchunk fact_chunk;
153
fact_chunk.id = PJMEDIA_FACT_TAG;
156
PJMEDIA_WAVE_NORMALIZE_SUBCHUNK(&fact_chunk);
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);
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);
174
status = pj_file_write(fport->fd, &tmp, &size);
175
if (status != PJ_SUCCESS) {
176
pj_file_close(fport->fd);
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);
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);
196
/* Set buffer size. */
197
if (buff_size < 1) buff_size = PJMEDIA_FILE_PORT_BUFSIZE;
198
fport->bufsize = buff_size;
200
/* Check that buffer size is greater than bytes per frame */
201
pj_assert(fport->bufsize >= PJMEDIA_PIA_AVG_FSZ(&fport->base.info));
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);
210
fport->writepos = fport->buf;
213
*p_port = &fport->base;
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));
229
* Get current writing position.
231
PJ_DEF(pj_ssize_t) pjmedia_wav_writer_port_get_pos( pjmedia_port *port )
233
struct file_port *fport;
236
PJ_ASSERT_RETURN(port, -PJ_EINVAL);
238
/* Check that this is really a writer port */
239
PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, -PJ_EINVALIDOP);
241
fport = (struct file_port*) port;
250
PJ_DEF(pj_status_t) pjmedia_wav_writer_port_set_cb( pjmedia_port *port,
253
pj_status_t (*cb)(pjmedia_port *port,
256
struct file_port *fport;
259
PJ_ASSERT_RETURN(port && cb, PJ_EINVAL);
261
/* Check that this is really a writer port */
262
PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVALIDOP);
264
fport = (struct file_port*) port;
266
fport->cb_size = pos;
267
fport->base.port_data.pdata = user_data;
274
#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
275
static void swap_samples(pj_int16_t *samples, unsigned count)
278
for (i=0; i<count; ++i) {
279
samples[i] = pj_swap16(samples[i]);
283
# define swap_samples(samples,count)
287
* Flush the contents of the buffer to the file.
289
static pj_status_t flush_buffer(struct file_port *fport)
291
pj_ssize_t bytes = fport->writepos - fport->buf;
294
/* Convert samples to little endian */
295
swap_samples((pj_int16_t*)fport->buf, bytes/fport->bytes_per_sample);
298
status = pj_file_write(fport->fd, fport->buf, &bytes);
301
fport->writepos = fport->buf;
307
* Put a frame into the buffer. When the buffer is full, flush the buffer
310
static pj_status_t file_put_frame(pjmedia_port *this_port,
311
pjmedia_frame *frame)
313
struct file_port *fport = (struct file_port *)this_port;
316
if (fport->fmt_tag == PJMEDIA_WAVE_FMT_TAG_PCM)
317
frame_size = frame->size;
319
frame_size = frame->size >> 1;
321
/* Flush buffer if we don't have enough room for the frame. */
322
if (fport->writepos + frame_size > fport->buf + fport->bufsize) {
324
status = flush_buffer(fport);
325
if (status != PJ_SUCCESS)
329
/* Check if frame is not too large. */
330
PJ_ASSERT_RETURN(fport->writepos+frame_size <= fport->buf+fport->bufsize,
331
PJMEDIA_EFRMFILETOOBIG);
333
/* Copy frame to buffer. */
334
if (fport->fmt_tag == PJMEDIA_WAVE_FMT_TAG_PCM) {
335
pj_memcpy(fport->writepos, frame->buf, frame->size);
338
pj_int16_t *src = (pj_int16_t*)frame->buf;
339
pj_uint8_t *dst = (pj_uint8_t*)fport->writepos;
341
if (fport->fmt_tag == PJMEDIA_WAVE_FMT_TAG_ULAW) {
342
for (i = 0; i < frame_size; ++i) {
343
*dst++ = pjmedia_linear2ulaw(*src++);
346
for (i = 0; i < frame_size; ++i) {
347
*dst++ = pjmedia_linear2alaw(*src++);
352
fport->writepos += frame_size;
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*);
363
status = (*cb)(this_port, this_port->port_data.pdata);
371
* Get frame, basicy is a no-op operation.
373
static pj_status_t file_get_frame(pjmedia_port *this_port,
374
pjmedia_frame *frame)
376
PJ_UNUSED_ARG(this_port);
377
PJ_UNUSED_ARG(frame);
378
return PJ_EINVALIDOP;
382
* Close the port, modify file header with updated file length.
384
static pj_status_t file_on_destroy(pjmedia_port *this_port)
386
enum { FILE_LEN_POS = 4, DATA_LEN_POS = 40 };
387
struct file_port *fport = (struct file_port *)this_port;
390
pj_uint32_t wave_file_len;
391
pj_uint32_t wave_data_len;
393
pj_uint32_t data_len_pos = DATA_LEN_POS;
395
/* Flush remaining buffers. */
396
if (fport->writepos != fport->buf)
400
status = pj_file_getpos(fport->fd, &file_size);
401
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
403
/* Calculate wave fields */
404
wave_file_len = (pj_uint32_t)(file_size - 8);
405
wave_data_len = (pj_uint32_t)(file_size - sizeof(pjmedia_wave_hdr));
407
#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
408
wave_file_len = pj_swap32(wave_file_len);
409
wave_data_len = pj_swap32(wave_data_len);
412
/* Seek to the file_len field. */
413
status = pj_file_setpos(fport->fd, FILE_LEN_POS, PJ_SEEK_SET);
414
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
417
bytes = sizeof(wave_file_len);
418
status = pj_file_write(fport->fd, &wave_file_len, &bytes);
419
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
421
/* Write samples_len in FACT chunk */
422
if (fport->fmt_tag != PJMEDIA_WAVE_FMT_TAG_PCM) {
423
enum { SAMPLES_LEN_POS = 44};
424
pj_uint32_t wav_samples_len;
426
/* Adjust wave_data_len & data_len_pos since there is FACT chunk */
429
wav_samples_len = wave_data_len;
431
/* Seek to samples_len field. */
432
status = pj_file_setpos(fport->fd, SAMPLES_LEN_POS, PJ_SEEK_SET);
433
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
435
/* Write samples_len */
436
bytes = sizeof(wav_samples_len);
437
status = pj_file_write(fport->fd, &wav_samples_len, &bytes);
438
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
441
/* Seek to data_len field. */
442
status = pj_file_setpos(fport->fd, data_len_pos, PJ_SEEK_SET);
443
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
446
bytes = sizeof(wave_data_len);
447
status = pj_file_write(fport->fd, &wave_data_len, &bytes);
448
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
451
status = pj_file_close(fport->fd);
452
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);