wav_playlist.c

来自「基于sip协议的网络电话源码」· C语言 代码 · 共 629 行 · 第 1/2 页

C
629
字号
/* $Id: wav_playlist.c 919 2007-01-31 21:09:04Z bennylp $ *//*  * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> * * Original author: *  David Clark <vdc1048 @ tx.rr.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  */#include <pjmedia/wav_playlist.h>#include <pjmedia/errno.h>#include <pjmedia/wave.h>#include <pj/assert.h>#include <pj/file_access.h>#include <pj/file_io.h>#include <pj/log.h>#include <pj/pool.h>#include <pj/string.h>#define THIS_FILE	    "wav_playlist.c"#define SIGNATURE	    PJMEDIA_PORT_SIGNATURE('P', 'l', 's', 't')#define BYTES_PER_SAMPLE    2#if 1#   define TRACE_(x)	PJ_LOG(4,x)#else#   define TRACE_(x)#endif#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0    static void samples_to_host(pj_int16_t *samples, unsigned count)    {	unsigned i;	for (i=0; i<count; ++i) {	    samples[i] = pj_swap16(samples[i]);	}    }#else#   define samples_to_host(samples,count)#endifstruct playlist_port{    pjmedia_port     base;    unsigned	     options;    pj_bool_t	     eof;    pj_size_t	     bufsize;    char	    *buf;    char	    *readpos;    pj_off_t        *fsize_list;    unsigned        *start_data_list;    pj_off_t        *fpos_list;    pj_oshandle_t   *fd_list;	    /* list of file descriptors	*/    int              current_file;  /* index of current file.	*/    int              max_file;	    /* how many files.		*/    pj_status_t	   (*cb)(pjmedia_port*, void*);};static pj_status_t file_list_get_frame(pjmedia_port *this_port,				       pjmedia_frame *frame);static pj_status_t file_list_on_destroy(pjmedia_port *this_port);static struct playlist_port *create_file_list_port(pj_pool_t *pool,						   const pj_str_t *name){    struct playlist_port *port;    port = pj_pool_zalloc(pool, sizeof(struct playlist_port));    if (!port)	return NULL;    /* Put in default values.     * These will be overriden once the file is read.     */    pjmedia_port_info_init(&port->base.info, name, SIGNATURE,			   8000, 1, 16, 80);    port->base.get_frame = &file_list_get_frame;    port->base.on_destroy = &file_list_on_destroy;    return port;}/* * Fill buffer for file_list operations. */static pj_status_t file_fill_buffer(struct playlist_port *fport){    pj_ssize_t size_left = fport->bufsize;    unsigned size_to_read;    pj_ssize_t size;    pj_status_t status;    int current_file = fport->current_file;    /* Can't read file if EOF and loop flag is disabled */    if (fport->eof)	return PJ_EEOF;    while (size_left > 0)    {	/* Calculate how many bytes to read in this run. */	size = size_to_read = size_left;	status = pj_file_read(fport->fd_list[current_file],			      &fport->buf[fport->bufsize-size_left],			      &size);	if (status != PJ_SUCCESS)	    return status;		if (size < 0)	{	    /* Should return more appropriate error code here.. */	    return PJ_ECANCELLED;	}		size_left -= size;	fport->fpos_list[current_file] += size;		/* If size is less than size_to_read, it indicates that we've	 * encountered EOF. Rewind the file.	 */	if (size < (pj_ssize_t)size_to_read)	{	    /* Rewind the file for the next iteration */	    fport->fpos_list[current_file] = 		fport->start_data_list[current_file];	    pj_file_setpos(fport->fd_list[current_file], 			   fport->fpos_list[current_file], PJ_SEEK_SET);	    /* Move to next file */	    current_file++;	    fport->current_file = current_file;	    if (fport->current_file == fport->max_file)	    {		/* All files have been played. Call callback, if any. */		if (fport->cb)		{		    PJ_LOG(5,(THIS_FILE,			      "File port %.*s EOF, calling callback",			      (int)fport->base.info.name.slen,			      fport->base.info.name.ptr));		    		    fport->eof = PJ_TRUE;		    status = (*fport->cb)(&fport->base,					  fport->base.port_data.pdata);		    if (status != PJ_SUCCESS)		    {			/* This will crash if file port is destroyed in the			 * callback, that's why we set the eof flag before			 * calling the callback:			 fport->eof = PJ_TRUE;			 */			return status;		    }		    fport->eof = PJ_FALSE;		}		if (fport->options & PJMEDIA_FILE_NO_LOOP)		{		    PJ_LOG(5,(THIS_FILE, "File port %.*s EOF, stopping..",			      (int)fport->base.info.name.slen,			      fport->base.info.name.ptr));		    fport->eof = PJ_TRUE;		    return PJ_EEOF;		}		else		{		    PJ_LOG(5,(THIS_FILE, "File port %.*s EOF, rewinding..",			      (int)fport->base.info.name.slen,			      fport->base.info.name.ptr));		    		    /* start with first file again. */		    fport->current_file = current_file = 0;		    fport->fpos_list[0] = fport->start_data_list[0];		    pj_file_setpos(fport->fd_list[0], fport->fpos_list[0],				   PJ_SEEK_SET);		}					    } /* if current_file == max_file */	} /* size < size_to_read */    } /* while () */        /* Convert samples to host rep */    samples_to_host((pj_int16_t*)fport->buf, fport->bufsize/BYTES_PER_SAMPLE);        return PJ_SUCCESS;}/* * Create wave list player. */PJ_DEF(pj_status_t) pjmedia_wav_playlist_create(pj_pool_t *pool,						const pj_str_t *port_label,						const pj_str_t file_list[],						int file_count,						unsigned ptime,						unsigned options,						pj_ssize_t buff_size,						pjmedia_port **p_port){    struct playlist_port *fport;    pj_off_t pos;    pj_status_t status;    int index;    pj_bool_t has_wave_info = PJ_FALSE;    pj_str_t tmp_port_label;    char filename[PJ_MAXPATH];	/* filename for open operations.    */    /* Check arguments. */    PJ_ASSERT_RETURN(pool && file_list && file_count && p_port, PJ_EINVAL);    /* Normalize port_label */    if (port_label == NULL || port_label->slen == 0) {	tmp_port_label = pj_str("WAV playlist");	port_label = &tmp_port_label;    }    /* Be sure all files exist	*/    for (index=0; index<file_count; index++) {	PJ_ASSERT_RETURN(file_list[index].slen < PJ_MAXPATH, PJ_ENAMETOOLONG);	pj_memcpy(filename, file_list[index].ptr, file_list[index].slen);	filename[file_list[index].slen] = '\0';    	/* Check the file really exists. */    	if (!pj_file_exists(filename)) {	    PJ_LOG(4,(THIS_FILE,		      "WAV playlist error: file '%s' not found",	      	      filename));	    return PJ_ENOTFOUND;    	}    }    /* Normalize ptime */    if (ptime == 0)	ptime = 20;    /* Create fport instance. */    fport = create_file_list_port(pool, port_label);    if (!fport) {	return PJ_ENOMEM;    }    /* start with the first file. */    fport->current_file = 0;    fport->max_file = file_count;    /* Create file descriptor list */    fport->fd_list = pj_pool_zalloc(pool, sizeof(pj_oshandle_t)*file_count);    if (!fport->fd_list) {	return PJ_ENOMEM;    }    /* Create file size list */    fport->fsize_list = pj_pool_alloc(pool, sizeof(pj_off_t)*file_count);    if (!fport->fsize_list) {	return PJ_ENOMEM;    }    /* Create start of WAVE data list */    fport->start_data_list = pj_pool_alloc(pool, sizeof(unsigned)*file_count);    if (!fport->start_data_list) {	return PJ_ENOMEM;    }    /* Create file position list */    fport->fpos_list = pj_pool_alloc(pool, sizeof(pj_off_t)*file_count);    if (!fport->fpos_list) {	return PJ_ENOMEM;    }    /* Create file buffer once for this operation.     */    if (buff_size < 1) buff_size = PJMEDIA_FILE_PORT_BUFSIZE;    fport->bufsize = buff_size;    /* Create buffer. */    fport->buf = pj_pool_alloc(pool, fport->bufsize);    if (!fport->buf) {	return PJ_ENOMEM;    }    /* Initialize port */    fport->options = options;    fport->readpos = fport->buf;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?