mspcapfileplayer.c 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
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.
*/

#if defined(HAVE_CONFIG_H)
#include "mediastreamer-config.h"
#endif


#include "mediastreamer2/mspcapfileplayer.h"
#include "mediastreamer2/msfileplayer.h"
#include "waveheader.h"
#include "mediastreamer2/msticker.h"
29
#include <ortp/rtp.h>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

#include <pcap/pcap.h>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>



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

struct _PlayerData{
	int fd;
	MSPlayerState state;
	int rate;
	int hsize;
	int pause_time;
	int count;
	int samplesize;
	int datalink_size;
	unsigned to_port;
	uint32_t ts;
	bool_t swap;
	bool_t is_raw;
	pcap_t *pcap;
	struct pcap_pkthdr *pcap_hdr;
	const u_char *pcap_data;
	bool_t pcap_started;
	uint64_t pcap_initial_time;
	uint32_t pcap_initial_ts;
	uint16_t pcap_seq;
	struct timeval pcap_initial_timeval;
	MSPCAPFilePlayerTimeRef pcap_timeref;
	MSPCAPFilePlayerLayer pcap_layer;
64
	uint32_t ts_offset;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
};

typedef struct _PlayerData PlayerData;

static void player_init(MSFilter *f){
	PlayerData *d=ms_new0(PlayerData,1);
	d->fd=-1;
	d->state=MSPlayerClosed;
	d->swap=FALSE;
	d->rate=8000;
	d->samplesize=2;
	d->datalink_size=0;
	d->to_port=0;
	d->hsize=0;
	d->pause_time=0;
	d->count=0;
	d->ts=0;
	d->is_raw=TRUE;
	d->pcap = NULL;
	d->pcap_hdr = NULL;
	d->pcap_data = NULL;
	d->pcap_started = FALSE;
	d->pcap_initial_ts = 0;
	d->pcap_seq = 0;
	d->pcap_timeref = MSPCAPFilePlayerTimeRefRTP;
	d->pcap_layer = MSPCAPFilePlayerLayerPayload;
	f->data=d;
}

static uint link_layer_size(int datalink) {
	switch (datalink) {
		//cf http://www.tcpdump.org/linktypes.html
		case 1: return 14; //LINKTYPE_ETHERNET: Ethernet
		case 113: return 16; //LINKTYPE_LINUX_SLL: Linux "cooked" capture encapsulation
		default:
			ms_error("Unsupported network type %d, assuming no link header...", datalink);
			return 0;
	}
}

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

	if (d->fd!=-1){
		player_close(f,NULL);
	}
	if ((fd=open(file,O_RDONLY|O_BINARY))==-1){
114
		ms_warning("MSPCAPFilePlayer[%p]: failed to open %s: %s",f,file,strerror(errno));
115 116 117 118 119 120 121 122 123 124 125 126
		return -1;
	}

	d->state=MSPlayerPaused;
	d->fd=fd;
	d->ts=0;
	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) {
127
			ms_error("MSPCAPFilePlayer[%p]: failed to open pcap file: %s",f,err);
128 129 130 131 132 133
			d->fd=-1;
			close(fd);
			return -1;
		}
		d->datalink_size = link_layer_size(pcap_datalink(d->pcap));
		ms_filter_notify_no_arg(f,MS_FILTER_OUTPUT_FMT_CHANGED);
134
		ms_message("MSPCAPFilePlayer[%p]: %s opened: rate=%i",f,file,d->rate);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
		return 0;
	} else {
		ms_error("Unsupported file extension: %s", file);
		return -1;
	}
}

static int player_start(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
	if (d->state==MSPlayerPaused)
		d->state=MSPlayerPlaying;
	return 0;
}

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;
}

static int player_close(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
161 162 163 164
	if (d->pcap) {
		pcap_close(d->pcap);
		d->pcap = NULL;
	}
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	if (d->fd!=-1)	close(d->fd);
	d->fd=-1;
	d->state=MSPlayerClosed;
	return 0;
}

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

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

static void player_process(MSFilter *f){
	PlayerData *d=(PlayerData*)f->data;

	ms_filter_lock(f);

	if (d->state==MSPlayerPlaying) {
		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 iteration. */
					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_PLAYER_EOF);
					ms_filter_notify_no_arg(f, MS_FILE_PLAYER_EOF);
203
					d->state = MSPlayerPaused;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
				} else if (res == -1) {
					ms_error("Error while reading next pcap packet: %s", pcap_geterr(d->pcap));
				} else if (res > 0) {
					const u_char *link_layer_header = &d->pcap_data[0];
					const u_char *ip_header = link_layer_header + d->datalink_size; // sizeof(link_layer_header)
					const u_char *udp_header = ip_header + 20; // sizeof(ipv4_header)
					const u_char *rtp_header = udp_header + 8; // sizeof(udp_header)
					const u_char *payload = rtp_header + 12; // sizeof(rtp_header)

					int headers_size = payload - link_layer_header;
					unsigned to_port = ntohs(*(uint16_t*)(udp_header + 2));
					if (d->to_port != 0 && d->to_port != to_port) {
						// Skip packets with invalid from port
						ms_debug("Outputting wrong-from-port packet (got %u but expected %u), ignoring", to_port, d->to_port);
						d->pcap_hdr = NULL;
						d->pcap_data = NULL;
					} else if (((int)d->pcap_hdr->caplen >= headers_size) && ((rtp_header[0] >> 6) == 2)) {
						// Check headers size and RTP version
						mblk_t *om = NULL;
						uint16_t pcap_seq = ntohs(*((uint16_t*)(rtp_header + 2)));
						uint32_t ts = ntohl(*((uint32_t*)(rtp_header + 4)));
						uint32_t diff_ms;
						bool_t markbit=rtp_header[1]>>7;
						int headers_size = payload - link_layer_header;
						int bytes_pcap = d->pcap_hdr->caplen - headers_size;
						if (d->pcap_started == FALSE) {
							d->pcap_started = TRUE;
							d->pcap_initial_ts = ts;
							d->pcap_initial_time = f->ticker->time;

							d->pcap_seq = pcap_seq;
							ms_message("initial ts=%u, seq=%u",ts,pcap_seq);
							d->pcap_initial_timeval.tv_sec = d->pcap_hdr->ts.tv_sec;
							d->pcap_initial_timeval.tv_usec = d->pcap_hdr->ts.tv_usec;
						}
						diff_ms = (d->pcap_timeref == MSPCAPFilePlayerTimeRefRTP) ?
							 ((ts - d->pcap_initial_ts) * 1000) / d->rate
							: 1000 * (d->pcap_hdr->ts.tv_sec - d->pcap_initial_timeval.tv_sec)
							+ (d->pcap_hdr->ts.tv_usec - d->pcap_initial_timeval.tv_usec) / 1000;

						if ((f->ticker->time - d->pcap_initial_time) >= diff_ms) {
							if (d->pcap_layer == MSPCAPFilePlayerLayerRTP) {
								int headers_size = link_layer_header - rtp_header;
								int bytes_pcap = d->pcap_hdr->caplen + headers_size;
248 249 250
								
								*((uint32_t*)(rtp_header + 4)) = htonl(ts + d->ts_offset);

251 252 253 254 255
								om = allocb(bytes_pcap, 0);
								memcpy(om->b_wptr, rtp_header, bytes_pcap);
								om->b_wptr += bytes_pcap;
							} else {
								if (pcap_seq >= d->pcap_seq) {
256
									rtp_header_t *rtph = (rtp_header_t*)rtp_header;
257 258 259
									om = allocb(bytes_pcap, 0);
									memcpy(om->b_wptr, payload, bytes_pcap);
									om->b_wptr += bytes_pcap;
260 261
									mblk_set_cseq(om, ntohs(rtph->seq_number));
									mblk_set_timestamp_info(om, ntohl(rtph->timestamp));
262 263 264 265 266 267 268 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
									mblk_set_marker_info(om,markbit);
								}
							}
							if (om != NULL) {
								ms_debug("Outputting RTP packet of size %i, seq=%u markbit=%i", bytes_pcap, pcap_seq, (int)markbit);
								ms_queue_put(f->outputs[0], om);
							}
							d->pcap_seq = pcap_seq;
							d->pcap_hdr = NULL;
							d->pcap_data = NULL;
						} else {
							ms_debug("Outputting non-RTP packet, stopping");
							cont = FALSE;
						}
					} else {
						// Ignore wrong RTP packet
						ms_debug("Outputting wrong-RTP packet, ignoring");
						d->pcap_hdr = NULL;
						d->pcap_data = NULL;
					}
				}
			} while ((res > 0) && cont);
		}
	}
	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_set_sr(MSFilter *f, void *arg) {
	PlayerData *d = (PlayerData *)f->data;
	if (d->is_raw) d->rate = *((int *)arg);
	else return -1;
299
	ms_message("MSPCAPFilePlayer[%p]: new rate=%i",f,d->rate);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	return 0;
}

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

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

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

321 322 323 324 325 326
static int player_set_ts_offset(MSFilter *f, void *arg){
	PlayerData *d=(PlayerData*)f->data;
	d->ts_offset = *(uint32_t*)arg;
	return 0;
}

327 328 329 330 331 332
static MSFilterMethod player_methods[]={
	{ MS_FILTER_GET_SAMPLE_RATE, player_get_sr},
	{ MS_FILTER_SET_SAMPLE_RATE, player_set_sr},
	{ MS_PCAP_FILE_PLAYER_SET_LAYER, player_set_layer},
	{ MS_PCAP_FILE_PLAYER_SET_TIMEREF, player_set_timeref},
	{ MS_PCAP_FILE_PLAYER_SET_TO_PORT, player_set_to_port},
333
	{ MS_PCAP_FILE_PLAYER_SET_TS_OFFSET, player_set_ts_offset},
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

	/* this 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 },
	{ 0, NULL }
};

#ifdef WIN32

MSFilterDesc ms_pcap_file_player_desc={
	MS_PCAP_FILE_PLAYER_ID,
	"MSPCAPFilePlayer",
	N_("PCAP file player"),
	MS_FILTER_OTHER,
	NULL,
	0,
	1,
	player_init,
	NULL,
	player_process,
	NULL,
	player_uninit,
	player_methods
};

#else

MSFilterDesc ms_pcap_file_player_desc={
	.id=MS_PCAP_FILE_PLAYER_ID,
	.name="MSPCAPFilePlayer",
	.text=N_("PCAP files player"),
	.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_pcap_file_player_desc)