pcap_playback.c 11.3 KB
Newer Older
Ghislain MARY's avatar
Ghislain MARY committed
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
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2012  Belledonne Communications, Grenoble, France

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

#ifdef HAVE_CONFIG_H
#include "mediastreamer-config.h"
#endif

#include <math.h>

26
#include "common.h"
Ghislain MARY's avatar
Ghislain MARY committed
27 28 29
#include "mediastreamer2/msequalizer.h"
#include "mediastreamer2/msfileplayer.h"
#include "mediastreamer2/msvolume.h"
30
#include "mediastreamer2/msfilerec.h"
Ghislain MARY's avatar
Ghislain MARY committed
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
#ifdef VIDEO_ENABLED
#include "mediastreamer2/msv4l.h"
#endif

#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#ifndef WIN32
#include <unistd.h>
#else
#include <malloc.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ortp/b64.h>


static int cond = 1;


typedef struct _MediastreamDatas {
	int payload;
	bool_t is_verbose;
Simon Morlat's avatar
Simon Morlat committed
56
	bool_t avpf;
Ghislain MARY's avatar
Ghislain MARY committed
57
	char *playback_card;
58
	char *outfile;
Ghislain MARY's avatar
Ghislain MARY committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	char *infile;
	PayloadType *pt;
	RtpProfile *profile;
	MSFilter *read;
	MSFilter *write;
	MSFilter *decoder;
	MSTicker *ticker;
} MediastreamDatas;

// MAIN METHODS
/* init default arguments */
static MediastreamDatas *init_default_args();
/* parse args */
static bool_t parse_args(int argc, char **argv, MediastreamDatas *out);
/* setup streams */
static void setup_media_streams(MediastreamDatas *args);
/* run loop */
static void run_non_interactive_loop(MediastreamDatas *args);
/* exit */
static void clear_mediastreams(MediastreamDatas *args);

// HELPER METHODS
static void stop_handler(int signum);

const char *usage = "pcap_playback --infile <pcapfile>\n"
                    "--payload <payload type number or payload name like 'audio/pmcu/8000'>\n"
85
		    		"--avpf [assume RTP/AVPF profile]\n"
Ghislain MARY's avatar
Ghislain MARY committed
86
                    "[ --playback-card <name> ]\n"
87
					"[ --outfile <name> ] limited to wav file for now\n"
Ghislain MARY's avatar
Ghislain MARY committed
88 89 90 91 92 93 94 95 96 97 98
                    "[ --verbose (most verbose messages) ]\n"
                    ;


int main(int argc, char *argv[])
{
	MediastreamDatas *args;
	cond = 1;

	args = init_default_args();

99 100
	if (!parse_args(argc, argv, args)) {
		printf("%s", usage);
Ghislain MARY's avatar
Ghislain MARY committed
101
		return 0;
102
	}
Ghislain MARY's avatar
Ghislain MARY committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

	setup_media_streams(args);
	run_non_interactive_loop(args);
	clear_mediastreams(args);

	free(args);

	return 0;
}


static MediastreamDatas *init_default_args()
{
	MediastreamDatas *args = (MediastreamDatas *) ms_malloc0(sizeof(MediastreamDatas));

	args->payload = 0;
	args->is_verbose = FALSE;
	args->playback_card = NULL;
	args->infile = NULL;
	args->pt = NULL;
	args->profile = NULL;

	args->read = NULL;
	args->write = NULL;
	args->decoder = NULL;
	args->ticker = NULL;

	return args;
}

static bool_t parse_args(int argc, char **argv, MediastreamDatas *out)
{
	int i;

	if (argc < 2) {
		printf("%s", usage);
		return FALSE;
	}

	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "--payload") == 0) {
			i++;
			if (isdigit(argv[i][0])) {
				out->payload = atoi(argv[i]);
147 148 149
			}else {
				out->payload=114;
				out->pt=ms_tools_parse_custom_payload(argv[i]);
Ghislain MARY's avatar
Ghislain MARY committed
150 151 152 153 154 155 156
			}
		} else if (strcmp(argv[i], "--playback-card") == 0) {
			i++;
			out->playback_card = argv[i];
		} else if (strcmp(argv[i], "--infile") == 0) {
			i++;
			out->infile = argv[i];
157 158 159
		} else if (strcmp(argv[i], "--outfile") == 0) {
			i++;
			out->outfile = argv[i];
Ghislain MARY's avatar
Ghislain MARY committed
160 161 162 163 164
		} else if (strcmp(argv[i], "--verbose") == 0) {
			out->is_verbose = TRUE;
		} else if (strcmp(argv[i], "--help") == 0) {
			printf("%s", usage);
			return FALSE;
Simon Morlat's avatar
Simon Morlat committed
165 166
		}else if (strcmp(argv[i],"--avpf")==0){
			out->avpf=TRUE;
Ghislain MARY's avatar
Ghislain MARY committed
167 168
		}
	}
169 170 171 172
	if (out->playback_card && out->outfile ) {
		ms_error("Cannot define both playback card and output file, select only one");
		return FALSE;
	}
Ghislain MARY's avatar
Ghislain MARY committed
173 174 175 176 177 178 179 180 181 182 183
	return TRUE;
}


static void reader_notify_cb(void *user_data, MSFilter *f, unsigned int event, void *eventdata)
{
	if (event == MS_FILE_PLAYER_EOF) {
		cond = 0;
	}
}

Simon Morlat's avatar
Simon Morlat committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
static void video_decoder_callback(void *user_data, MSFilter *f, unsigned int event, void *eventdata){
	MSVideoCodecRPSI *rpsi;
	uint16_t picture_id=0;
	switch(event){
		case MS_VIDEO_DECODER_SEND_RPSI:
			rpsi=(MSVideoCodecRPSI*)eventdata;
			if (rpsi->bit_string_len == 8) {
				picture_id = *((uint8_t *)rpsi->bit_string);
			} else if (rpsi->bit_string_len == 16) {
				picture_id = ntohs(*((uint16_t *)rpsi->bit_string));
			}
			ms_message("Decoder would like to send RPSI for pic-id=%u",picture_id);
		break;
		case MS_VIDEO_DECODER_SEND_SLI:
			ms_message("Decoder would like to send SLI");
		break;
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
203 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
static void setup_media_streams(MediastreamDatas *args)
{
	MSConnectionHelper h;
	MSTickerParams params = {0};

	/*create the rtp session */
	ortp_init();
	if (args->is_verbose) {
		ortp_set_log_level_mask(ORTP_DEBUG | ORTP_MESSAGE | ORTP_WARNING | ORTP_ERROR | ORTP_FATAL);
	} else {
		ortp_set_log_level_mask(ORTP_MESSAGE | ORTP_WARNING | ORTP_ERROR | ORTP_FATAL);
	}

	rtp_profile_set_payload(&av_profile, 110, &payload_type_speex_nb);
	rtp_profile_set_payload(&av_profile, 111, &payload_type_speex_wb);
	rtp_profile_set_payload(&av_profile, 112, &payload_type_ilbc);
	rtp_profile_set_payload(&av_profile, 113, &payload_type_amr);
	rtp_profile_set_payload(&av_profile, 115, &payload_type_lpc1015);
#ifdef VIDEO_ENABLED
	rtp_profile_set_payload(&av_profile, 26, &payload_type_jpeg);
	rtp_profile_set_payload(&av_profile, 98, &payload_type_h263_1998);
	rtp_profile_set_payload(&av_profile, 97, &payload_type_theora);
	rtp_profile_set_payload(&av_profile, 99, &payload_type_mp4v);
	rtp_profile_set_payload(&av_profile, 100, &payload_type_x_snow);
	rtp_profile_set_payload(&av_profile, 102, &payload_type_h264);
	rtp_profile_set_payload(&av_profile, 103, &payload_type_vp8);
#endif
	args->profile = rtp_profile_clone_full(&av_profile);

	ms_init();
	ms_filter_enable_statistics(TRUE);
	ms_filter_reset_statistics();

	signal(SIGINT, stop_handler);
237 238
	if (args->pt==NULL)
		args->pt = rtp_profile_get_payload(args->profile, args->payload);
Ghislain MARY's avatar
Ghislain MARY committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	if (args->pt == NULL) {
		printf("Error: no payload defined with number %i.", args->payload);
		exit(-1);
	}

	if (args->pt->type == PAYLOAD_VIDEO) {
#ifdef VIDEO_ENABLED
		const char *display_name;
		MSPixFmt format;
		MSVideoSize disp_size;
		int tmp = 1;

#if defined(HAVE_GL)
		display_name = "MSGLXVideo";
#elif defined(HAVE_XV)
		display_name = "MSX11Video";
255 256
#elif __APPLE__ && !defined(__ios)
		display_name ="MSOSXGLDisplay";
Ghislain MARY's avatar
Ghislain MARY committed
257 258 259 260 261 262
#else
		display_name = "MSVideoOut";
#endif
		args->read = ms_filter_new(MS_FILE_PLAYER_ID);
		args->write = ms_filter_new_from_name(display_name);
		args->decoder = ms_filter_create_decoder(args->pt->mime_type);
Simon Morlat's avatar
Simon Morlat committed
263 264 265 266 267 268 269
		if (args->decoder==NULL){
			fprintf(stderr,"No decoder available for %s.\n",args->pt->mime_type);
			exit(-1);
		}
		if (ms_filter_call_method(args->decoder,MS_VIDEO_DECODER_ENABLE_AVPF,&args->avpf)==0){
			ms_filter_add_notify_callback(args->decoder, video_decoder_callback, NULL, TRUE);
		}
Ghislain MARY's avatar
Ghislain MARY committed
270 271
		ms_filter_call_method_noarg(args->read, MS_FILE_PLAYER_CLOSE);
		ms_filter_call_method(args->read, MS_FILE_PLAYER_OPEN, args->infile);
272
		ms_filter_call_method(args->read, MS_FILTER_SET_SAMPLE_RATE, &args->pt->clock_rate);
273
		ms_filter_call_method_noarg(args->read, MS_FILE_PLAYER_START);
jehan's avatar
jehan committed
274
		ms_filter_add_notify_callback(args->read, reader_notify_cb, NULL,FALSE);
Ghislain MARY's avatar
Ghislain MARY committed
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

		/*force the decoder to output YUV420P */
		format = MS_YUV420P;
		ms_filter_call_method(args->decoder, MS_FILTER_SET_PIX_FMT, &format);

		/*configure the display window */
		disp_size.width = MS_VIDEO_SIZE_CIF_W;
		disp_size.height = MS_VIDEO_SIZE_CIF_H;
		ms_filter_call_method(args->write, MS_FILTER_SET_VIDEO_SIZE, &disp_size);
		ms_filter_call_method(args->write, MS_VIDEO_DISPLAY_ENABLE_AUTOFIT, &tmp);
		ms_filter_call_method(args->write, MS_FILTER_SET_PIX_FMT, &format);

		params.name = "Video MSTicker";
		params.prio  = MS_TICKER_PRIO_REALTIME;
		args->ticker = ms_ticker_new_with_params(&params);

		ms_connection_helper_start(&h);
		ms_connection_helper_link(&h, args->read, -1, 0);
		ms_connection_helper_link(&h, args->decoder, 0, 0);
		ms_connection_helper_link(&h, args->write, 0, -1);
		ms_ticker_attach(args->ticker, args->read);
#else
		printf("Error: video support not compiled.\n");
#endif
	} else {
		MSSndCardManager *manager = ms_snd_card_manager_get();
301 302 303 304 305 306 307 308 309 310
		MSSndCard *play =NULL;
		if (args->outfile) {
			args->write=ms_filter_new(MS_FILE_REC_ID);
			if (ms_filter_call_method(args->write, MS_FILE_REC_OPEN, args->outfile)) {
				ms_error("Cannot open file [%s] in write mode",args->outfile);
				exit(-1);
			}

		} else {
			play = args->playback_card ==NULL ? ms_snd_card_manager_get_default_playback_card(manager) :
Ghislain MARY's avatar
Ghislain MARY committed
311
		                  ms_snd_card_manager_get_card(manager, args->playback_card);
312
			args->write = ms_snd_card_create_writer(play);
Ghislain MARY's avatar
Ghislain MARY committed
313

314
		}
Ghislain MARY's avatar
Ghislain MARY committed
315 316 317 318 319
		args->read = ms_filter_new(MS_FILE_PLAYER_ID);
		args->decoder = ms_filter_create_decoder(args->pt->mime_type);
		ms_filter_call_method_noarg(args->read, MS_FILE_PLAYER_CLOSE);
		ms_filter_call_method(args->read, MS_FILE_PLAYER_OPEN, args->infile);
		ms_filter_call_method_noarg(args->read, MS_FILE_PLAYER_START);
320
		ms_filter_call_method(args->read, MS_FILTER_SET_SAMPLE_RATE, &args->pt->clock_rate);
Ghislain MARY's avatar
Ghislain MARY committed
321 322 323 324 325
		ms_filter_call_method(args->decoder, MS_FILTER_SET_SAMPLE_RATE, &args->pt->clock_rate);
		ms_filter_call_method(args->decoder, MS_FILTER_SET_NCHANNELS, &args->pt->channels);
		ms_filter_call_method(args->write, MS_FILTER_SET_SAMPLE_RATE, &args->pt->clock_rate);
		ms_filter_call_method(args->write, MS_FILTER_SET_NCHANNELS, &args->pt->channels);

326 327 328
		if (ms_filter_get_id(args->write) == MS_FILE_REC_ID) {
			ms_filter_call_method_noarg(args->write, MS_FILE_REC_START);
		}
Ghislain MARY's avatar
Ghislain MARY committed
329 330 331 332 333 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
		params.name = "Audio MSTicker";
		params.prio  = MS_TICKER_PRIO_REALTIME;
		args->ticker = ms_ticker_new_with_params(&params);

		ms_connection_helper_start(&h);
		ms_connection_helper_link(&h, args->read, -1, 0);
		ms_connection_helper_link(&h, args->decoder, 0, 0);
		ms_connection_helper_link(&h, args->write, 0, -1);
		ms_ticker_attach(args->ticker, args->read);
	}
}


static void run_non_interactive_loop(MediastreamDatas *args)
{
	while (cond) {
		int n;
		for (n = 0; n < 100; ++n) {
#ifdef WIN32
			MSG msg;
			Sleep(10);
			while (PeekMessage(&msg, NULL, 0, 0, 1)) {
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
#else
			struct timespec ts;
			ts.tv_sec = 0;
			ts.tv_nsec = 10000000;
			nanosleep(&ts, NULL);
#endif
		}
	}
}

static void clear_mediastreams(MediastreamDatas *args)
{
	MSConnectionHelper h;

	ms_message("stopping all...\n");

	if (args->read) {
		ms_ticker_detach(args->ticker, args->read);
		ms_connection_helper_start(&h);
		ms_connection_helper_unlink(&h, args->read, -1, 0);
		ms_connection_helper_unlink(&h, args->decoder, 0, 0);
		ms_connection_helper_unlink(&h, args->write, 0, -1);
376 377 378
		if (ms_filter_get_id(args->write) == MS_FILE_REC_ID) {
			ms_filter_call_method_noarg(args->write, MS_FILE_REC_CLOSE);
		}
Ghislain MARY's avatar
Ghislain MARY committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	}
	rtp_profile_destroy(args->profile);

	ms_exit();
}

// HELPER METHODS
static void stop_handler(int signum)
{
	cond--;
	if (cond < 0) {
		ms_error("Brutal exit (%d)\n", cond);
		exit(-1);
	}
}