mediastream.c 11.6 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 20 21 22 23
/*
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.
*/

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

smorlat's avatar
smorlat committed
24 25
#include <math.h>

aymeric's avatar
aymeric committed
26
#include "mediastreamer2/mediastream.h"
smorlat's avatar
smorlat committed
27
#include "mediastreamer2/msequalizer.h"
28
#include "mediastreamer2/msvolume.h"
aymeric's avatar
aymeric committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#ifdef VIDEO_ENABLED
#include "mediastreamer2/msv4l.h"
#endif

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

static int cond=1;

44
static const char * capture_card=NULL;
Simon Morlat's avatar
Simon Morlat committed
45
static const char * playback_card=NULL;
46
static float ng_threshold=-1;
47 48
static bool_t use_ng=FALSE;

49 50 51 52
/* starting values echo canceller */
static int ec_len_ms=0, ec_delay_ms=250, ec_framesize;


aymeric's avatar
aymeric committed
53 54 55 56 57 58 59 60
static void stop_handler(int signum)
{
	cond--;
	if (cond<0) exit(-1);
}

static bool_t parse_addr(const char *addr, char *ip, int len, int *port)
{
smorlat's avatar
smorlat committed
61
	const char *semicolon=NULL;
aymeric's avatar
aymeric committed
62 63
	int iplen;
	int slen;
smorlat's avatar
smorlat committed
64
	const char *p;
aymeric's avatar
aymeric committed
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

	*port=0;
	semicolon=strchr(addr,':');
	for (p=addr+strlen(addr)-1;p>addr;p--){
		if (*p==':') {
			semicolon=p;
			break;
		}
	}
	if (semicolon==NULL) return FALSE;
	iplen=semicolon-addr;
	slen=MIN(iplen,len-1);
	strncpy(ip,addr,slen);
	ip[slen]='\0';
	*port=atoi(semicolon+1);
	return TRUE;
}

static void display_items(void *user_data, uint32_t csrc, rtcp_sdes_type_t t, const char *content, uint8_t content_len){
	char str[256];
	int len=MIN(sizeof(str)-1,content_len);
	strncpy(str,content,len);
	str[len]='\0';
	switch(t){
		case RTCP_SDES_CNAME:
			ms_message("Found CNAME=%s",str);
		break;
		case RTCP_SDES_TOOL:
			ms_message("Found TOOL=%s",str);
		break;
		case RTCP_SDES_NOTE:
			ms_message("Found NOTE=%s",str);
		break;
		default:
			ms_message("Unhandled SDES item (%s)",str);
	}
}

static void parse_rtcp(mblk_t *m){
	do{
		if (rtcp_is_RR(m)){
			ms_message("Receiving RTCP RR");
		}else if (rtcp_is_SR(m)){
			ms_message("Receiving RTCP SR");
		}else if (rtcp_is_SDES(m)){
			ms_message("Receiving RTCP SDES");
			rtcp_sdes_parse(m,display_items,NULL);
		}else {
			ms_message("Receiving unhandled RTCP message");
		}
	}while(rtcp_next_packet(m));
}

static void parse_events(OrtpEvQueue *q){
	OrtpEvent *ev;
	while((ev=ortp_ev_queue_get(q))!=NULL){
		OrtpEventData *d=ortp_event_get_data(ev);
		switch(ortp_event_get_type(ev)){
			case ORTP_EVENT_RTCP_PACKET_RECEIVED:
				parse_rtcp(d->packet);
			break;
			default:
				ms_warning("Unhandled ortp event.");
		}
		ortp_event_destroy(ev);
	}
}

const char *usage="mediastream --local <port> --remote <ip:port> --payload <payload type number>\n"
								"[ --fmtp <fmtpline>]\n"
smorlat's avatar
smorlat committed
135 136
								"[ --jitter <miliseconds>]\n"
								"[ --width <pixels>]\n"
smorlat's avatar
smorlat committed
137
								"[ --height <pixels> ]\n"
138 139
								"[ --bitrate <bits per seconds>]\n"
								"[ --ec (enable echo canceller)]\n"
140 141
								"[ --agc (enable automatic gain control)]\n"
								"[ --ng (enable noise gate)]\n"
142
								"[ --ng-threshold <(float) [0-1]> (noise gate threshold)]\n"
Simon Morlat's avatar
Simon Morlat committed
143 144
								"[ --capture-card <name>] \n"
								"[ --playback-card <name>] \n";
aymeric's avatar
aymeric committed
145

146 147
static void run_media_streams(int localport, const char *remote_ip, int remoteport, int payload, const char *fmtp,
          int jitter, int bitrate, MSVideoSize vs, bool_t ec, bool_t agc, bool_t eq);
aymeric's avatar
aymeric committed
148 149 150 151 152 153 154 155 156

int main(int argc, char * argv[])
{
	int i;
	int localport=0,remoteport=0,payload=0;
	char ip[50];
	const char *fmtp=NULL;
	int jitter=50;
	int bitrate=0;
smorlat's avatar
smorlat committed
157
	MSVideoSize vs;
aymeric's avatar
aymeric committed
158
	bool_t ec=FALSE;
159
	bool_t agc=FALSE;
smorlat's avatar
smorlat committed
160
	bool_t eq=FALSE;
aymeric's avatar
aymeric committed
161 162 163 164 165 166 167
	/*create the rtp session */
	ortp_init();
	ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
	rtp_profile_set_payload(&av_profile,115,&payload_type_lpc1015);
	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);
Simon Morlat's avatar
Simon Morlat committed
168
	rtp_profile_set_payload(&av_profile,113,&payload_type_amr);
aymeric's avatar
aymeric committed
169
#ifdef VIDEO_ENABLED
aymeric's avatar
aymeric committed
170
	rtp_profile_set_payload(&av_profile,26,&payload_type_jpeg);
aymeric's avatar
aymeric committed
171 172 173 174 175 176
	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);
#endif
smorlat's avatar
smorlat committed
177 178 179

	vs.width=MS_VIDEO_SIZE_CIF_W;
	vs.height=MS_VIDEO_SIZE_CIF_H;
aymeric's avatar
aymeric committed
180
	if (argc<4) {
Simon Morlat's avatar
Simon Morlat committed
181
		printf("%s",usage);
aymeric's avatar
aymeric committed
182 183 184 185 186 187 188 189 190
		return -1;
	}
	for (i=1;i<argc;i++){
		if (strcmp(argv[i],"--local")==0){
			i++;
			localport=atoi(argv[i]);
		}else if (strcmp(argv[i],"--remote")==0){
			i++;
			if (!parse_addr(argv[i],ip,sizeof(ip),&remoteport)) {
Simon Morlat's avatar
Simon Morlat committed
191
				printf("%s",usage);
aymeric's avatar
aymeric committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
				return -1;
			}
			printf("Remote addr: ip=%s port=%i\n",ip,remoteport);
		}else if (strcmp(argv[i],"--payload")==0){
			i++;
			payload=atoi(argv[i]);
		}else if (strcmp(argv[i],"--fmtp")==0){
			i++;
			fmtp=argv[i];
		}else if (strcmp(argv[i],"--jitter")==0){
			i++;
			jitter=atoi(argv[i]);
		}else if (strcmp(argv[i],"--bitrate")==0){
			i++;
			bitrate=atoi(argv[i]);
smorlat's avatar
smorlat committed
207 208 209 210 211 212
		}else if (strcmp(argv[i],"--width")==0){
			i++;
			vs.width=atoi(argv[i]);
		}else if (strcmp(argv[i],"--height")==0){
			i++;
			vs.height=atoi(argv[i]);
213 214 215
		}else if (strcmp(argv[i],"--capture-card")==0){
			i++;
			capture_card=argv[i];
Simon Morlat's avatar
Simon Morlat committed
216 217 218
		}else if (strcmp(argv[i],"--playback-card")==0){
			i++;
			playback_card=argv[i];
aymeric's avatar
aymeric committed
219 220
		}else if (strcmp(argv[i],"--ec")==0){
			ec=TRUE;
221 222
		}else if (strcmp(argv[i],"--agc")==0){
			agc=TRUE;
smorlat's avatar
smorlat committed
223 224
		}else if (strcmp(argv[i],"--eq")==0){
			eq=TRUE;
225 226
		}else if (strcmp(argv[i],"--ng")==0){
			use_ng=1;
227 228 229
		}else if (strcmp(argv[i],"--ng-threshold")==0){
			i++;
			ng_threshold=atof(argv[i]);
aymeric's avatar
aymeric committed
230 231
		}
	}
232

233
	run_media_streams(localport,ip,remoteport,payload,fmtp,jitter,bitrate,vs,ec,agc,eq);
aymeric's avatar
aymeric committed
234 235 236
	return 0;
}

237 238
static void run_media_streams(int localport, const char *remote_ip, int remoteport, int payload, const char *fmtp,
          int jitter, int bitrate, MSVideoSize vs, bool_t ec, bool_t agc, bool_t eq)
aymeric's avatar
aymeric committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
{
	AudioStream *audio=NULL;
#ifdef VIDEO_ENABLED
	VideoStream *video=NULL;
#endif
	RtpSession *session=NULL;
	PayloadType *pt;
	RtpProfile *profile=rtp_profile_clone_full(&av_profile);
	OrtpEvQueue *q=ortp_ev_queue_new();	

	ms_init();
	signal(SIGINT,stop_handler);
	pt=rtp_profile_get_payload(profile,payload);
	if (pt==NULL){
		printf("Error: no payload defined with number %i.",payload);
		exit(-1);
	}
	if (fmtp!=NULL) payload_type_set_send_fmtp(pt,fmtp);
	if (bitrate>0) pt->normal_bitrate=bitrate;

	if (pt->type!=PAYLOAD_VIDEO){
260
		MSSndCardManager *manager=ms_snd_card_manager_get();
261 262
		MSSndCard *capt= capture_card==NULL ? ms_snd_card_manager_get_default_capture_card(manager) :
				ms_snd_card_manager_get_card(manager,capture_card);
Simon Morlat's avatar
Simon Morlat committed
263 264
		MSSndCard *play= playback_card==NULL ? ms_snd_card_manager_get_default_playback_card(manager) :
				ms_snd_card_manager_get_card(manager,playback_card);
265 266
		audio=audio_stream_new(localport,ms_is_ipv6(remote_ip));
		audio_stream_enable_automatic_gain_control(audio,agc);
267
		audio_stream_enable_noise_gate(audio,use_ng);
Simon Morlat's avatar
Simon Morlat committed
268 269
		audio_stream_set_echo_canceller_params(audio,ec_len_ms,ec_delay_ms,ec_framesize);
		printf("Starting audio stream.\n");
270
		audio_stream_start_now(audio,profile,remote_ip,remoteport,remoteport+1,payload,jitter,
Simon Morlat's avatar
Simon Morlat committed
271
			play,
272
			capt,
273
			 ec);
274 275 276 277 278
		if (audio) {
			if (use_ng && ng_threshold!=-1)
				ms_filter_call_method(audio->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_threshold);
			session=audio->session;
		}
aymeric's avatar
aymeric committed
279 280
	}else{
#ifdef VIDEO_ENABLED
smorlat's avatar
smorlat committed
281 282 283 284
		if (eq){
			ms_fatal("Cannot put an audio equalizer in a video stream !");
			exit(-1);
		}
aymeric's avatar
aymeric committed
285 286
		printf("Starting video stream.\n");
		video=video_stream_new(localport, ms_is_ipv6(remote_ip));
smorlat's avatar
smorlat committed
287
		video_stream_set_sent_video_size(video,vs);
aymeric's avatar
aymeric committed
288 289 290 291 292 293 294 295 296 297 298
		video_stream_start(video,profile,
					remote_ip,
					remoteport,remoteport+1,
					payload,
					jitter,
					ms_web_cam_manager_get_default_cam(ms_web_cam_manager_get()));
		session=video->session;
#else
		printf("Error: video support not compiled.\n");
#endif
	}
299 300 301 302 303 304 305 306 307 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 342 343 344 345 346 347 348
  if (eq || ec){ /*read from stdin interactive commands */
    char commands[128];
    commands[127]='\0';
    ms_sleep(1);  /* ensure following text be printed after ortp messages */
    if (eq)
      printf("\nPlease enter equalizer requests, such as 'eq active 1', 'eq active 0', 'eq 1200 0.1 200'\n");
    if (ec)
      printf("\nPlease enter echo canceller requests: ec reset; ec <delay ms> <tail_length ms'\n");
    while(fgets(commands,sizeof(commands)-1,stdin)!=NULL){
      int active,freq,freq_width;
      int delay_ms, tail_ms;
      float gain;
      if (sscanf(commands,"eq active %i",&active)==1){
        audio_stream_enable_equalizer(audio,active);
        printf("OK\n");
      }else if (sscanf(commands,"eq %i %f %i",&freq,&gain,&freq_width)==3){
        audio_stream_equalizer_set_gain(audio,freq,gain,freq_width);
        printf("OK\n");
      }else if (sscanf(commands,"eq %i %f",&freq,&gain)==2){
        audio_stream_equalizer_set_gain(audio,freq,gain,0);
        printf("OK\n");
      }else if (strstr(commands,"dump")){
        int n=0,i;
        float *t;
        ms_filter_call_method(audio->equalizer,MS_EQUALIZER_GET_NUM_FREQUENCIES,&n);
        t=(float*)alloca(sizeof(float)*n);
        ms_filter_call_method(audio->equalizer,MS_EQUALIZER_DUMP_STATE,t);
        for(i=0;i<n;++i){
          if (fabs(t[i]-1)>0.01){
            printf("%i:%f:0 ",(i*pt->clock_rate)/(2*n),t[i]);
          }
        }
        printf("\nOK\n");
      }else if (sscanf(commands,"ec reset %i",&active)==1){
          //audio_stream_enable_equalizer(audio,active);
          //printf("OK\n");
      }else if (sscanf(commands,"ec active %i",&active)==1){
          //audio_stream_enable_equalizer(audio,active);
          //printf("OK\n");
      }else if (sscanf(commands,"ec %i %i",&delay_ms,&tail_ms)==2){
        audio_stream_set_echo_canceller_params(audio,tail_ms,delay_ms,128);
        // revisit: workaround with old method call to force echo reset
        delay_ms*=8;
        ms_filter_call_method(audio->ec,MS_FILTER_SET_PLAYBACKDELAY,&delay_ms);
        printf("OK\n");
      }else if (strstr(commands,"quit")){
        break;
      }else printf("Cannot understand this.\n");
    }
	}else{  /* no interactive stuff - continuous debug output */
smorlat's avatar
smorlat committed
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
		rtp_session_register_event_queue(session,q);
		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
	#if defined(VIDEO_ENABLED)
				if (video) video_stream_iterate(video);
	#endif
			}
			ortp_global_stats_display();
			if (session){
				printf("Bandwidth usage: download=%f kbits/sec, upload=%f kbits/sec\n",
					rtp_session_compute_recv_bandwidth(session)*1e-3,
					rtp_session_compute_send_bandwidth(session)*1e-3);
				parse_events(q);
aymeric's avatar
aymeric committed
377 378
			}
		}
smorlat's avatar
smorlat committed
379
					}
aymeric's avatar
aymeric committed
380
	
381
	printf("stopping all...\n");
aymeric's avatar
aymeric committed
382 383 384 385 386 387 388 389
	
	if (audio) audio_stream_stop(audio);
#ifdef VIDEO_ENABLED
	if (video) video_stream_stop(video);
#endif
	ortp_ev_queue_destroy(q);
	rtp_profile_destroy(profile);
}