msfileplayer.c 10.2 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

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.
*/

Simon Morlat's avatar
Simon Morlat committed
20 21 22 23
#if defined(HAVE_CONFIG_H)
#include "mediastreamer-config.h"
#endif

aymeric's avatar
aymeric committed
24 25 26 27
#include "mediastreamer2/msfileplayer.h"
#include "mediastreamer2/waveheader.h"
#include "mediastreamer2/msticker.h"

28
#ifdef HAVE_PCAP
Ghislain MARY's avatar
Ghislain MARY committed
29 30 31
#include <pcap/pcap.h>
#endif

aymeric's avatar
aymeric committed
32 33 34 35 36

static int player_close(MSFilter *f, void *arg);

struct _PlayerData{
	int fd;
Simon Morlat's avatar
Simon Morlat committed
37
	MSPlayerState state;
aymeric's avatar
aymeric committed
38 39 40 41 42
	int rate;
	int nchannels;
	int hsize;
	int loop_after;
	int pause_time;
43
	int count;
44 45
	int samplesize;
	uint32_t ts;
aymeric's avatar
aymeric committed
46
	bool_t swap;
47
#ifdef HAVE_PCAP
Ghislain MARY's avatar
Ghislain MARY committed
48 49 50 51 52 53 54
	pcap_t *pcap;
	struct pcap_pkthdr *pcap_hdr;
	const u_char *pcap_data;
	bool_t pcap_started;
	uint64_t pcap_initial_ts;
	uint16_t pcap_seq;
#endif
aymeric's avatar
aymeric committed
55 56 57 58 59 60 61
};

typedef struct _PlayerData PlayerData;

static void player_init(MSFilter *f){
	PlayerData *d=ms_new(PlayerData,1);
	d->fd=-1;
Simon Morlat's avatar
Simon Morlat committed
62
	d->state=MSPlayerClosed;
aymeric's avatar
aymeric committed
63 64 65
	d->swap=FALSE;
	d->rate=8000;
	d->nchannels=1;
66
	d->samplesize=2;
aymeric's avatar
aymeric committed
67 68 69
	d->hsize=0;
	d->loop_after=-1; /*by default, don't loop*/
	d->pause_time=0;
70
	d->count=0;
71
	d->ts=0;
72
#ifdef HAVE_PCAP
Ghislain MARY's avatar
Ghislain MARY committed
73 74 75 76 77 78 79
	d->pcap = NULL;
	d->pcap_hdr = NULL;
	d->pcap_data = NULL;
	d->pcap_started = FALSE;
	d->pcap_initial_ts = 0;
	d->pcap_seq = 0;
#endif
aymeric's avatar
aymeric committed
80 81 82 83
	f->data=d;	
}

static int read_wav_header(PlayerData *d){
smorlat's avatar
smorlat committed
84 85 86 87 88 89 90 91 92 93 94 95 96
	char header1[sizeof(riff_t)];
	char header2[sizeof(format_t)];
	char header3[sizeof(data_t)];
	int count;
	
	riff_t *riff_chunk=(riff_t*)header1;
	format_t *format_chunk=(format_t*)header2;
	data_t *data_chunk=(data_t*)header3;
	
	unsigned long len=0;
	
	len = read(d->fd, header1, sizeof(header1)) ;
	if (len != sizeof(header1)){
smorlat's avatar
smorlat committed
97
		goto not_a_wav;
smorlat's avatar
smorlat committed
98 99 100
	}
	
	if (0!=strncmp(riff_chunk->riff, "RIFF", 4) || 0!=strncmp(riff_chunk->wave, "WAVE", 4)){	
smorlat's avatar
smorlat committed
101
		goto not_a_wav;
smorlat's avatar
smorlat committed
102 103 104 105 106
	}
	
	len = read(d->fd, header2, sizeof(header2)) ;            
	if (len != sizeof(header2)){
		ms_warning("Wrong wav header: cannot read file");
smorlat's avatar
smorlat committed
107
		goto not_a_wav;
smorlat's avatar
smorlat committed
108 109 110 111
	}
	
	d->rate=le_uint32(format_chunk->rate);
	d->nchannels=le_uint16(format_chunk->channel);
112 113
	if (d->nchannels==0) goto not_a_wav;
	d->samplesize=le_uint16(format_chunk->blockalign)/d->nchannels;
smorlat's avatar
smorlat committed
114 115 116 117 118 119 120 121 122 123 124
	
	if (format_chunk->len-0x10>0)
	{
		lseek(d->fd,(format_chunk->len-0x10),SEEK_CUR);
	}
	
	d->hsize=sizeof(wave_header_t)-0x10+format_chunk->len;
	
	len = read(d->fd, header3, sizeof(header3)) ;
	if (len != sizeof(header3)){
		ms_warning("Wrong wav header: cannot read file");
smorlat's avatar
smorlat committed
125
		goto not_a_wav;
smorlat's avatar
smorlat committed
126 127 128 129 130 131 132 133 134 135 136 137
	}
	count=0;
	while (strncmp(data_chunk->data, "data", 4)!=0 && count<30)
	{
		ms_warning("skipping chunk=%s len=%i", data_chunk->data, data_chunk->len);
		lseek(d->fd,data_chunk->len,SEEK_CUR);
		count++;
		d->hsize=d->hsize+len+data_chunk->len;
	
		len = read(d->fd, header3, sizeof(header3)) ;
		if (len != sizeof(header3)){
			ms_warning("Wrong wav header: cannot read file");
smorlat's avatar
smorlat committed
138
			goto not_a_wav;
smorlat's avatar
smorlat committed
139 140 141 142 143 144 145
		}
	}
	#ifdef WORDS_BIGENDIAN
	if (le_uint16(format_chunk->blockalign)==le_uint16(format_chunk->channel) * 2)
		d->swap=TRUE;
	#endif
	return 0;
smorlat's avatar
smorlat committed
146 147 148 149

	not_a_wav:
		/*rewind*/
		lseek(d->fd,0,SEEK_SET);
150
		d->hsize=0;
smorlat's avatar
smorlat committed
151
		return -1;
aymeric's avatar
aymeric committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165
}

static int player_open(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
	int fd;
	const char *file=(const char*)arg;

	if (d->fd>=0){
		player_close(f,NULL);
	}
	if ((fd=open(file,O_RDONLY))==-1){
		ms_warning("Failed to open %s",file);
		return -1;
	}
Simon Morlat's avatar
Simon Morlat committed
166
	d->state=MSPlayerPaused;
aymeric's avatar
aymeric committed
167
	d->fd=fd;
168
	d->ts=0;
169
#ifdef HAVE_PCAP
Ghislain MARY's avatar
Ghislain MARY committed
170 171 172 173 174 175 176 177 178 179 180 181
	d->pcap = NULL;
	d->pcap_started = FALSE;
	if (strstr(file, ".pcap")) {
		char err[PCAP_ERRBUF_SIZE];
		d->pcap = pcap_open_offline(file, err);
		if (d->pcap == NULL) {
			ms_error("Failed to open pcap file: %s", err);
			close(fd);
			return -1;
		}
	} else
#endif
smorlat's avatar
smorlat committed
182 183 184
	if (read_wav_header(d)!=0 && strstr(file,".wav")){
		ms_warning("File %s has .wav extension but wav header could be found.",file);
	}
aymeric's avatar
aymeric committed
185 186 187 188 189 190
	ms_message("%s opened: rate=%i,channel=%i",file,d->rate,d->nchannels);
	return 0;
}

static int player_start(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
Simon Morlat's avatar
Simon Morlat committed
191 192
	if (d->state==MSPlayerPaused)
		d->state=MSPlayerPlaying;
aymeric's avatar
aymeric committed
193 194 195 196 197 198
	return 0;
}

static int player_stop(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
	ms_filter_lock(f);
Simon Morlat's avatar
Simon Morlat committed
199 200
	if (d->state!=MSPlayerClosed){
		d->state=MSPlayerPaused;
aymeric's avatar
aymeric committed
201 202 203 204 205 206
		lseek(d->fd,d->hsize,SEEK_SET);
	}
	ms_filter_unlock(f);
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
207 208 209 210 211 212 213 214 215 216
static int player_pause(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
	ms_filter_lock(f);
	if (d->state==MSPlayerPlaying){
		d->state=MSPlayerPaused;
	}
	ms_filter_unlock(f);
	return 0;
}

aymeric's avatar
aymeric committed
217 218 219
static int player_close(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
	player_stop(f,NULL);
220
#ifdef HAVE_PCAP
Ghislain MARY's avatar
Ghislain MARY committed
221 222
	if (d->pcap) pcap_close(d->pcap);
#endif
aymeric's avatar
aymeric committed
223 224
	if (d->fd>=0)	close(d->fd);
	d->fd=-1;
Simon Morlat's avatar
Simon Morlat committed
225 226 227 228 229 230 231
	d->state=MSPlayerClosed;
	return 0;
}

static int player_get_state(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
	*(int*)arg=d->state;
aymeric's avatar
aymeric committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	return 0;
}

static void player_uninit(MSFilter *f){
	PlayerData *d=(PlayerData*)f->data;
	if (d->fd>=0) player_close(f,NULL);
	ms_free(d);
}

static void swap_bytes(unsigned char *bytes, int len){
	int i;
	unsigned char tmp;
	for(i=0;i<len;i+=2){
		tmp=bytes[i];
		bytes[i]=bytes[i+1];
		bytes[i+1]=tmp;
	}
}

static void player_process(MSFilter *f){
	PlayerData *d=(PlayerData*)f->data;
253 254 255 256 257 258 259 260 261 262 263
	int nsamples=(f->ticker->interval*d->rate*d->nchannels)/1000;
	int bytes;
	/*send an even number of samples each tick. At 22050Hz the number of samples per 10 ms chunk is odd.
	Odd size buffer of samples cause troubles to alsa. Fixing in alsa is difficult, so workaround here.
	*/
	if (nsamples & 0x1 ) { //odd number of samples
		if (d->count & 0x1 )
			nsamples++;
		else
			nsamples--;
	}
264
	bytes=nsamples*d->samplesize;
265
	d->count++;
aymeric's avatar
aymeric committed
266
	ms_filter_lock(f);
Simon Morlat's avatar
Simon Morlat committed
267
	if (d->state==MSPlayerPlaying){
268
#ifdef HAVE_PCAP
Ghislain MARY's avatar
Ghislain MARY committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
		if (d->pcap) {
			int res;
			bool_t cont = TRUE;
			do {
				if (d->pcap_data && d->pcap_hdr) {
					/* The PCAP data has already been read at previous interation. */
					res = 1;
				} else {
					res = pcap_next_ex(d->pcap, &d->pcap_hdr, &d->pcap_data);
				}
				if (res == -2) {
					ms_filter_notify_no_arg(f, MS_FILE_PLAYER_EOF);
				} else if (res > 0) {
					if (d->pcap_hdr->caplen > 54) {
						int bytes = d->pcap_hdr->caplen - 54;
						mblk_t *om;
						uint64_t ts = (uint64_t)(d->pcap_hdr->ts.tv_sec * 1000) + (uint64_t)(d->pcap_hdr->ts.tv_usec / 1000);
						uint16_t pcap_seq = ntohs(*((uint16_t*)&d->pcap_data[44]));
						if (d->pcap_started == FALSE) {
							d->pcap_started = TRUE;
							d->pcap_initial_ts = ts;
							d->pcap_seq = pcap_seq;
						}
						if ((d->pcap_initial_ts + f->ticker->time) > ts) {
							if (pcap_seq >= d->pcap_seq) {
								om = allocb(bytes, 0);
								memcpy(om->b_wptr, &d->pcap_data[54], bytes);
								om->b_wptr += bytes;
								mblk_set_timestamp_info(om, ts);
								ms_queue_put(f->outputs[0], om);
							}
							d->pcap_seq = pcap_seq;
							d->pcap_hdr = NULL;
							d->pcap_data = NULL;
						} else {
							cont = FALSE;
						}
					}
aymeric's avatar
aymeric committed
307
				}
Ghislain MARY's avatar
Ghislain MARY committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
			} while ((res > 0) && cont);
		} else
#endif
		{
			int err;
			mblk_t *om=allocb(bytes,0);
			if (d->pause_time>0){
				err=bytes;
				memset(om->b_wptr,0,bytes);
				d->pause_time-=f->ticker->interval;
			}else{
				err=read(d->fd,om->b_wptr,bytes);
				if (d->swap) swap_bytes(om->b_wptr,bytes);
			}
			if (err>=0){
				if (err!=0){
					if (err<bytes)
						memset(om->b_wptr+err,0,bytes-err);
					om->b_wptr+=bytes;
					mblk_set_timestamp_info(om,d->ts);
					d->ts+=nsamples;
					ms_queue_put(f->outputs[0],om);
				}else freemsg(om);
				if (err<bytes){
					ms_filter_notify_no_arg(f,MS_FILE_PLAYER_EOF);
					lseek(d->fd,d->hsize,SEEK_SET);

					/* special value for playing file only once */
					if (d->loop_after<0)
					{
						d->state=MSPlayerPaused;
						ms_filter_unlock(f);
						return;
					}
aymeric's avatar
aymeric committed
342

Ghislain MARY's avatar
Ghislain MARY committed
343 344 345
					if (d->loop_after>=0){
						d->pause_time=d->loop_after;
					}
aymeric's avatar
aymeric committed
346
				}
Ghislain MARY's avatar
Ghislain MARY committed
347 348
			}else{
				ms_warning("Fail to read %i bytes: %s",bytes,strerror(errno));
aymeric's avatar
aymeric committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
			}
		}
	}
	ms_filter_unlock(f);
}

static int player_get_sr(MSFilter *f, void*arg){
	PlayerData *d=(PlayerData*)f->data;
	*((int*)arg)=d->rate;
	return 0;
}

static int player_loop(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
	d->loop_after=*((int*)arg);
	return 0;
}

static int player_eof(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
Simon Morlat's avatar
Simon Morlat committed
369
	if (d->fd<0 && d->state==MSPlayerClosed)
aymeric's avatar
aymeric committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
		*((int*)arg) = TRUE; /* 1 */
	else
		*((int*)arg) = FALSE; /* 0 */
	return 0;
}

static int player_get_nch(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
	*((int*)arg)=d->nchannels;
	return 0;
}

static MSFilterMethod player_methods[]={
	{	MS_FILE_PLAYER_OPEN,	player_open	},
	{	MS_FILE_PLAYER_START,	player_start	},
	{	MS_FILE_PLAYER_STOP,	player_stop	},
	{	MS_FILE_PLAYER_CLOSE,	player_close	},
	{	MS_FILTER_GET_SAMPLE_RATE, player_get_sr},
	{	MS_FILTER_GET_NCHANNELS, player_get_nch	},
	{	MS_FILE_PLAYER_LOOP,	player_loop	},
	{	MS_FILE_PLAYER_DONE,	player_eof	},
Simon Morlat's avatar
Simon Morlat committed
391 392 393 394 395 396
	/* this wav file player implements the MSFilterPlayerInterface*/
	{ MS_PLAYER_OPEN , player_open },
	{ MS_PLAYER_START , player_start },
	{ MS_PLAYER_PAUSE, player_pause },
	{ MS_PLAYER_CLOSE, player_close },
	{ MS_PLAYER_GET_STATE, player_get_state },
aymeric's avatar
aymeric committed
397 398 399 400 401 402 403 404
	{	0,			NULL		}
};

#ifdef WIN32

MSFilterDesc ms_file_player_desc={
	MS_FILE_PLAYER_ID,
	"MSFilePlayer",
405
	N_("Raw files and wav reader"),
aymeric's avatar
aymeric committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	MS_FILTER_OTHER,
	NULL,
    0,
	1,
	player_init,
	NULL,
    player_process,
	NULL,
    player_uninit,
	player_methods
};

#else

MSFilterDesc ms_file_player_desc={
	.id=MS_FILE_PLAYER_ID,
	.name="MSFilePlayer",
423
	.text=N_("Raw files and wav reader"),
aymeric's avatar
aymeric committed
424 425 426 427 428 429 430 431 432 433 434 435
	.category=MS_FILTER_OTHER,
	.ninputs=0,
	.noutputs=1,
	.init=player_init,
	.process=player_process,
	.uninit=player_uninit,
	.methods=player_methods
};

#endif

MS_FILTER_DESC_EXPORT(ms_file_player_desc)