1
/* $Id: wav_playlist.c 3917 2011-12-20 10:01:35Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
7
* David Clark <vdc1048 @ tx.rr.com>
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
#include <pjmedia/wav_playlist.h>
24
#include <pjmedia/errno.h>
25
#include <pjmedia/wave.h>
26
#include <pj/assert.h>
27
#include <pj/file_access.h>
28
#include <pj/file_io.h>
31
#include <pj/string.h>
33
#define THIS_FILE "wav_playlist.c"
35
#define SIGNATURE PJMEDIA_SIG_PORT_WAV_PLAYLIST
36
#define BYTES_PER_SAMPLE 2
40
# define TRACE_(x) PJ_LOG(4,x)
45
#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
46
static void samples_to_host(pj_int16_t *samples, unsigned count)
49
for (i=0; i<count; ++i) {
50
samples[i] = pj_swap16(samples[i]);
54
# define samples_to_host(samples,count)
68
unsigned *start_data_list;
70
pj_oshandle_t *fd_list; /* list of file descriptors */
71
int current_file; /* index of current file. */
72
int max_file; /* how many files. */
74
pj_status_t (*cb)(pjmedia_port*, void*);
78
static pj_status_t file_list_get_frame(pjmedia_port *this_port,
79
pjmedia_frame *frame);
80
static pj_status_t file_list_on_destroy(pjmedia_port *this_port);
83
static struct playlist_port *create_file_list_port(pj_pool_t *pool,
86
struct playlist_port *port;
88
port = PJ_POOL_ZALLOC_T(pool, struct playlist_port);
92
/* Put in default values.
93
* These will be overriden once the file is read.
95
pjmedia_port_info_init(&port->base.info, name, SIGNATURE,
98
port->base.get_frame = &file_list_get_frame;
99
port->base.on_destroy = &file_list_on_destroy;
106
* Fill buffer for file_list operations.
108
static pj_status_t file_fill_buffer(struct playlist_port *fport)
110
pj_ssize_t size_left = fport->bufsize;
111
unsigned size_to_read;
114
int current_file = fport->current_file;
116
/* Can't read file if EOF and loop flag is disabled */
120
while (size_left > 0)
122
/* Calculate how many bytes to read in this run. */
123
size = size_to_read = size_left;
124
status = pj_file_read(fport->fd_list[current_file],
125
&fport->buf[fport->bufsize-size_left],
127
if (status != PJ_SUCCESS)
132
/* Should return more appropriate error code here.. */
133
return PJ_ECANCELLED;
137
fport->fpos_list[current_file] += size;
139
/* If size is less than size_to_read, it indicates that we've
140
* encountered EOF. Rewind the file.
142
if (size < (pj_ssize_t)size_to_read)
144
/* Rewind the file for the next iteration */
145
fport->fpos_list[current_file] =
146
fport->start_data_list[current_file];
147
pj_file_setpos(fport->fd_list[current_file],
148
fport->fpos_list[current_file], PJ_SEEK_SET);
150
/* Move to next file */
152
fport->current_file = current_file;
154
if (fport->current_file == fport->max_file)
156
/* Clear the remaining part of the buffer first, to prevent
157
* old samples from being played. If the playback restarts,
158
* this will be overwritten by new reading.
161
pj_bzero(&fport->buf[fport->bufsize-size_left],
165
/* All files have been played. Call callback, if any. */
169
"File port %.*s EOF, calling callback",
170
(int)fport->base.info.name.slen,
171
fport->base.info.name.ptr));
173
fport->eof = PJ_TRUE;
175
status = (*fport->cb)(&fport->base,
176
fport->base.port_data.pdata);
178
if (status != PJ_SUCCESS)
180
/* This will crash if file port is destroyed in the
181
* callback, that's why we set the eof flag before
182
* calling the callback:
183
fport->eof = PJ_TRUE;
188
fport->eof = PJ_FALSE;
192
if (fport->options & PJMEDIA_FILE_NO_LOOP)
194
PJ_LOG(5,(THIS_FILE, "File port %.*s EOF, stopping..",
195
(int)fport->base.info.name.slen,
196
fport->base.info.name.ptr));
197
fport->eof = PJ_TRUE;
202
PJ_LOG(5,(THIS_FILE, "File port %.*s EOF, rewinding..",
203
(int)fport->base.info.name.slen,
204
fport->base.info.name.ptr));
206
/* start with first file again. */
207
fport->current_file = current_file = 0;
208
fport->fpos_list[0] = fport->start_data_list[0];
209
pj_file_setpos(fport->fd_list[0], fport->fpos_list[0],
213
} /* if current_file == max_file */
215
} /* size < size_to_read */
219
/* Convert samples to host rep */
220
samples_to_host((pj_int16_t*)fport->buf, fport->bufsize/BYTES_PER_SAMPLE);
227
* Create wave list player.
229
PJ_DEF(pj_status_t) pjmedia_wav_playlist_create(pj_pool_t *pool,
230
const pj_str_t *port_label,
231
const pj_str_t file_list[],
235
pj_ssize_t buff_size,
236
pjmedia_port **p_port)
238
struct playlist_port *fport;
239
pjmedia_audio_format_detail *afd;
243
pj_bool_t has_wave_info = PJ_FALSE;
244
pj_str_t tmp_port_label;
245
char filename[PJ_MAXPATH]; /* filename for open operations. */
248
/* Check arguments. */
249
PJ_ASSERT_RETURN(pool && file_list && file_count && p_port, PJ_EINVAL);
251
/* Normalize port_label */
252
if (port_label == NULL || port_label->slen == 0) {
253
tmp_port_label = pj_str("WAV playlist");
254
port_label = &tmp_port_label;
257
/* Be sure all files exist */
258
for (index=0; index<file_count; index++) {
260
PJ_ASSERT_RETURN(file_list[index].slen < PJ_MAXPATH, PJ_ENAMETOOLONG);
262
pj_memcpy(filename, file_list[index].ptr, file_list[index].slen);
263
filename[file_list[index].slen] = '\0';
265
/* Check the file really exists. */
266
if (!pj_file_exists(filename)) {
268
"WAV playlist error: file '%s' not found",
274
/* Normalize ptime */
278
/* Create fport instance. */
279
fport = create_file_list_port(pool, port_label);
284
afd = pjmedia_format_get_audio_format_detail(&fport->base.info.fmt, 1);
286
/* start with the first file. */
287
fport->current_file = 0;
288
fport->max_file = file_count;
290
/* Create file descriptor list */
291
fport->fd_list = (pj_oshandle_t*)
292
pj_pool_zalloc(pool, sizeof(pj_oshandle_t)*file_count);
293
if (!fport->fd_list) {
297
/* Create file size list */
298
fport->fsize_list = (pj_off_t*)
299
pj_pool_alloc(pool, sizeof(pj_off_t)*file_count);
300
if (!fport->fsize_list) {
304
/* Create start of WAVE data list */
305
fport->start_data_list = (unsigned*)
306
pj_pool_alloc(pool, sizeof(unsigned)*file_count);
307
if (!fport->start_data_list) {
311
/* Create file position list */
312
fport->fpos_list = (pj_off_t*)
313
pj_pool_alloc(pool, sizeof(pj_off_t)*file_count);
314
if (!fport->fpos_list) {
318
/* Create file buffer once for this operation.
320
if (buff_size < 1) buff_size = PJMEDIA_FILE_PORT_BUFSIZE;
321
fport->bufsize = buff_size;
325
fport->buf = (char*) pj_pool_alloc(pool, fport->bufsize);
330
/* Initialize port */
331
fport->options = options;
332
fport->readpos = fport->buf;
335
/* ok run this for all files to be sure all are good for playback. */
336
for (index=file_count-1; index>=0; index--) {
338
pjmedia_wave_hdr wavehdr;
339
pj_ssize_t size_to_read, size_read;
341
/* we end with the last one so we are good to go if still in function*/
342
pj_memcpy(filename, file_list[index].ptr, file_list[index].slen);
343
filename[file_list[index].slen] = '\0';
345
/* Get the file size. */
346
fport->current_file = index;
347
fport->fsize_list[index] = pj_file_size(filename);
349
/* Size must be more than WAVE header size */
350
if (fport->fsize_list[index] <= sizeof(pjmedia_wave_hdr)) {
351
status = PJMEDIA_ENOTVALIDWAVE;
356
status = pj_file_open( pool, filename, PJ_O_RDONLY,
357
&fport->fd_list[index]);
358
if (status != PJ_SUCCESS)
361
/* Read the file header plus fmt header only. */
362
size_read = size_to_read = sizeof(wavehdr) - 8;
363
status = pj_file_read( fport->fd_list[index], &wavehdr, &size_read);
364
if (status != PJ_SUCCESS) {
368
if (size_read != size_to_read) {
369
status = PJMEDIA_ENOTVALIDWAVE;
373
/* Normalize WAVE header fields values from little-endian to host
376
pjmedia_wave_hdr_file_to_host(&wavehdr);
378
/* Validate WAVE file. */
379
if (wavehdr.riff_hdr.riff != PJMEDIA_RIFF_TAG ||
380
wavehdr.riff_hdr.wave != PJMEDIA_WAVE_TAG ||
381
wavehdr.fmt_hdr.fmt != PJMEDIA_FMT_TAG)
384
"actual value|expected riff=%x|%x, wave=%x|%x fmt=%x|%x",
385
wavehdr.riff_hdr.riff, PJMEDIA_RIFF_TAG,
386
wavehdr.riff_hdr.wave, PJMEDIA_WAVE_TAG,
387
wavehdr.fmt_hdr.fmt, PJMEDIA_FMT_TAG));
388
status = PJMEDIA_ENOTVALIDWAVE;
392
/* Must be PCM with 16bits per sample */
393
if (wavehdr.fmt_hdr.fmt_tag != 1 ||
394
wavehdr.fmt_hdr.bits_per_sample != 16)
396
status = PJMEDIA_EWAVEUNSUPP;
400
/* Block align must be 2*nchannels */
401
if (wavehdr.fmt_hdr.block_align !=
402
wavehdr.fmt_hdr.nchan * BYTES_PER_SAMPLE)
404
status = PJMEDIA_EWAVEUNSUPP;
408
/* If length of fmt_header is greater than 16, skip the remaining
411
if (wavehdr.fmt_hdr.len > 16) {
412
size_to_read = wavehdr.fmt_hdr.len - 16;
413
status = pj_file_setpos(fport->fd_list[index], size_to_read,
415
if (status != PJ_SUCCESS) {
420
/* Repeat reading the WAVE file until we have 'data' chunk */
422
pjmedia_wave_subchunk subchunk;
424
status = pj_file_read(fport->fd_list[index], &subchunk,
426
if (status != PJ_SUCCESS || size_read != 8) {
427
status = PJMEDIA_EWAVETOOSHORT;
431
/* Normalize endianness */
432
PJMEDIA_WAVE_NORMALIZE_SUBCHUNK(&subchunk);
434
/* Break if this is "data" chunk */
435
if (subchunk.id == PJMEDIA_DATA_TAG) {
436
wavehdr.data_hdr.data = PJMEDIA_DATA_TAG;
437
wavehdr.data_hdr.len = subchunk.len;
441
/* Otherwise skip the chunk contents */
442
size_to_read = subchunk.len;
443
status = pj_file_setpos(fport->fd_list[index], size_to_read,
445
if (status != PJ_SUCCESS) {
450
/* Current file position now points to start of data */
451
status = pj_file_getpos(fport->fd_list[index], &pos);
452
fport->start_data_list[index] = (unsigned)pos;
454
/* Validate length. */
455
if (wavehdr.data_hdr.len != fport->fsize_list[index] -
456
fport->start_data_list[index])
458
status = PJMEDIA_EWAVEUNSUPP;
461
if (wavehdr.data_hdr.len < 400) {
462
status = PJMEDIA_EWAVETOOSHORT;
466
/* It seems like we have a valid WAVE file. */
468
/* Update port info if we don't have one, otherwise check
469
* that the WAV file has the same attributes as previous files.
471
if (!has_wave_info) {
472
afd->channel_count = wavehdr.fmt_hdr.nchan;
473
afd->clock_rate = wavehdr.fmt_hdr.sample_rate;
474
afd->bits_per_sample = wavehdr.fmt_hdr.bits_per_sample;
475
afd->frame_time_usec = ptime * 1000;
476
afd->avg_bps = afd->max_bps = afd->clock_rate *
478
afd->bits_per_sample;
480
has_wave_info = PJ_TRUE;
484
/* Check that this file has the same characteristics as the other
487
if (wavehdr.fmt_hdr.nchan != afd->channel_count ||
488
wavehdr.fmt_hdr.sample_rate != afd->clock_rate ||
489
wavehdr.fmt_hdr.bits_per_sample != afd->bits_per_sample)
491
/* This file has different characteristics than the other
495
"WAV playlist error: file '%s' has differrent number"
496
" of channels, sample rate, or bits per sample",
498
status = PJMEDIA_EWAVEUNSUPP;
505
/* Set initial position of the file. */
506
fport->fpos_list[index] = fport->start_data_list[index];
509
/* Fill up the buffer. */
510
status = file_fill_buffer(fport);
511
if (status != PJ_SUCCESS) {
517
*p_port = &fport->base;
520
"WAV playlist '%.*s' created: samp.rate=%d, ch=%d, bufsize=%uKB",
521
(int)port_label->slen,
525
fport->bufsize / 1000));
530
for (index=0; index<file_count; ++index) {
531
if (fport->fd_list[index] != 0)
532
pj_file_close(fport->fd_list[index]);
540
* Register a callback to be called when the file reading has reached the
541
* end of the last file.
543
PJ_DEF(pj_status_t) pjmedia_wav_playlist_set_eof_cb(pjmedia_port *port,
545
pj_status_t (*cb)(pjmedia_port *port,
548
struct playlist_port *fport;
551
PJ_ASSERT_RETURN(port, PJ_EINVAL);
553
/* Check that this is really a playlist port */
554
PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVALIDOP);
556
fport = (struct playlist_port*) port;
558
fport->base.port_data.pdata = user_data;
566
* Get frame from file for file_list operation
568
static pj_status_t file_list_get_frame(pjmedia_port *this_port,
569
pjmedia_frame *frame)
571
struct playlist_port *fport = (struct playlist_port*)this_port;
575
pj_assert(fport->base.info.signature == SIGNATURE);
577
//frame_size = fport->base.info.bytes_per_frame;
578
//pj_assert(frame->size == frame_size);
579
frame_size = frame->size;
581
/* Copy frame from buffer. */
582
frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
583
frame->size = frame_size;
584
frame->timestamp.u64 = 0;
586
if (fport->readpos + frame_size <= fport->buf + fport->bufsize) {
588
/* Read contiguous buffer. */
589
pj_memcpy(frame->buf, fport->readpos, frame_size);
591
/* Fill up the buffer if all has been read. */
592
fport->readpos += frame_size;
593
if (fport->readpos == fport->buf + fport->bufsize) {
594
fport->readpos = fport->buf;
596
status = file_fill_buffer(fport);
597
if (status != PJ_SUCCESS) {
598
frame->type = PJMEDIA_FRAME_TYPE_NONE;
607
* First stage: read until end of buffer.
609
endread = (fport->buf+fport->bufsize) - fport->readpos;
610
pj_memcpy(frame->buf, fport->readpos, endread);
612
/* Second stage: fill up buffer, and read from the start of buffer. */
613
status = file_fill_buffer(fport);
614
if (status != PJ_SUCCESS) {
615
pj_bzero(((char*)frame->buf)+endread, frame_size-endread);
619
pj_memcpy(((char*)frame->buf)+endread, fport->buf, frame_size-endread);
620
fport->readpos = fport->buf + (frame_size - endread);
630
static pj_status_t file_list_on_destroy(pjmedia_port *this_port)
632
struct playlist_port *fport = (struct playlist_port*) this_port;
635
pj_assert(this_port->info.signature == SIGNATURE);
637
for (index=0; index<fport->max_file; index++)
638
pj_file_close(fport->fd_list[index]);