videostream.c 30 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
/*
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.
*/

#include "mediastreamer2/mediastream.h"
#include "mediastreamer2/msfilter.h"
22
#include "mediastreamer2/msinterfaces.h"
aymeric's avatar
aymeric committed
23 24 25
#include "mediastreamer2/msvideo.h"
#include "mediastreamer2/msrtp.h"
#include "mediastreamer2/msvideoout.h"
26
#include "mediastreamer2/msextdisplay.h"
Ghislain MARY's avatar
Ghislain MARY committed
27
#include "private.h"
Simon Morlat's avatar
Simon Morlat committed
28

Guillaume Beraudo's avatar
Guillaume Beraudo committed
29
#include <ortp/zrtp.h>
aymeric's avatar
aymeric committed
30

Simon Morlat's avatar
Simon Morlat committed
31

Ghislain MARY's avatar
Ghislain MARY committed
32
void video_stream_free(VideoStream *stream) {
33 34 35 36 37 38 39 40
	/* Prevent filters from being destroyed two times */
	if (stream->source_performs_encoding == TRUE) {
		stream->ms.encoder = NULL;
	}
	if (stream->output_performs_decoding == TRUE) {
		stream->ms.decoder = NULL;
	}

Ghislain MARY's avatar
Ghislain MARY committed
41
	media_stream_free(&stream->ms);
42

aymeric's avatar
aymeric committed
43 44 45 46
	if (stream->source != NULL)
		ms_filter_destroy (stream->source);
	if (stream->output != NULL)
		ms_filter_destroy (stream->output);
47 48
	if (stream->sizeconv != NULL)
		ms_filter_destroy (stream->sizeconv);
aymeric's avatar
aymeric committed
49 50 51 52
	if (stream->pixconv!=NULL)
		ms_filter_destroy(stream->pixconv);
	if (stream->tee!=NULL)
		ms_filter_destroy(stream->tee);
53 54 55 56
	if (stream->tee2!=NULL)
		ms_filter_destroy(stream->tee2);
	if (stream->jpegwriter!=NULL)
		ms_filter_destroy(stream->jpegwriter);
57 58
	if (stream->output2!=NULL)
		ms_filter_destroy(stream->output2);
59 60
	if (stream->display_name!=NULL)
		ms_free(stream->display_name);
61

aymeric's avatar
aymeric committed
62 63 64
	ms_free (stream);
}

65 66
static void event_cb(void *ud, MSFilter* f, unsigned int event, void *eventdata){
	VideoStream *st=(VideoStream*)ud;
Simon Morlat's avatar
Simon Morlat committed
67
	ms_message("event_cb called %u", event);
68 69 70 71 72
	if (st->eventcb!=NULL){
		st->eventcb(st->event_pointer,f,event,eventdata);
	}
}

Simon Morlat's avatar
Simon Morlat committed
73
static void video_stream_process_rtcp(MediaStream *media_stream, mblk_t *m){
jehan's avatar
jehan committed
74
	VideoStream *stream = (VideoStream *)media_stream;
aymeric's avatar
aymeric committed
75 76 77 78 79 80
	do{
		if (rtcp_is_SR(m)){
			const report_block_t *rb;
			rb=rtcp_SR_get_report_block(m,0);
			if (rb){
				unsigned int ij;
81
				float rt=rtp_session_get_round_trip_propagation(stream->ms.sessions.rtp_session);
aymeric's avatar
aymeric committed
82 83
				float flost;
				ij=report_block_get_interarrival_jitter(rb);
aymeric's avatar
aymeric committed
84
				flost=(float)(100.0*report_block_get_fraction_lost(rb)/256.0);
Simon Morlat's avatar
Simon Morlat committed
85
				ms_message("video_stream_process_rtcp[%p]: interarrival jitter=%u , lost packets percentage since last report=%f, round trip time=%f seconds",stream,ij,flost,rt);
Ghislain MARY's avatar
Ghislain MARY committed
86 87
				if (stream->ms.rc)
					ms_bitrate_controller_process_rtcp(stream->ms.rc,m);
aymeric's avatar
aymeric committed
88 89 90 91 92
			}
		}
	}while(rtcp_next_packet(m));
}

93
static void stop_preload_graph(VideoStream *stream){
94
	ms_ticker_detach(stream->ms.sessions.ticker,stream->ms.rtprecv);
Ghislain MARY's avatar
Ghislain MARY committed
95 96 97 98
	ms_filter_unlink(stream->ms.rtprecv,0,stream->ms.voidsink,0);
	ms_filter_destroy(stream->ms.voidsink);
	ms_filter_destroy(stream->ms.rtprecv);
	stream->ms.voidsink=stream->ms.rtprecv=NULL;
99 100
}

aymeric's avatar
aymeric committed
101
void video_stream_iterate(VideoStream *stream){
102
	media_stream_iterate(&stream->ms);
aymeric's avatar
aymeric committed
103 104
}

Simon Morlat's avatar
Simon Morlat committed
105
const char *video_stream_get_default_video_renderer(void){
106
#ifdef WIN32
Simon Morlat's avatar
Simon Morlat committed
107
	return "MSDrawDibDisplay";
108
#elif defined(ANDROID)
Simon Morlat's avatar
Simon Morlat committed
109
	return "MSAndroidDisplay";
110
#elif __APPLE__ && !defined(__ios)
Simon Morlat's avatar
Simon Morlat committed
111
	return "MSOSXGLDisplay";
112
#elif defined (HAVE_XV)
Simon Morlat's avatar
Simon Morlat committed
113
	return "MSX11Video";
114
#elif defined(HAVE_GL)
Simon Morlat's avatar
Simon Morlat committed
115
	return "MSGLXVideo";
Simon Morlat's avatar
Simon Morlat committed
116
#elif defined(__ios)
Simon Morlat's avatar
Simon Morlat committed
117
	return "IOSDisplay";
Simon Morlat's avatar
Simon Morlat committed
118
#else
Simon Morlat's avatar
Simon Morlat committed
119
	return "MSVideoOut";
120 121 122
#endif
}

Simon Morlat's avatar
Simon Morlat committed
123 124 125 126
static void choose_display_name(VideoStream *stream){
	stream->display_name=ms_strdup(video_stream_get_default_video_renderer());
}

127
VideoStream *video_stream_new(int loc_rtp_port, int loc_rtcp_port, bool_t use_ipv6){
128 129 130 131 132 133 134 135 136
	MSMediaStreamSessions sessions={0};
	VideoStream *obj;
	sessions.rtp_session=create_duplex_rtpsession(loc_rtp_port,loc_rtcp_port,use_ipv6);
	obj=video_stream_new_with_sessions(&sessions);
	obj->ms.owns_sessions=TRUE;
	return obj;
}

VideoStream *video_stream_new_with_sessions(const MSMediaStreamSessions *sessions){
aymeric's avatar
aymeric committed
137
	VideoStream *stream = (VideoStream *)ms_new0 (VideoStream, 1);
Ghislain MARY's avatar
Ghislain MARY committed
138
	stream->ms.type = VideoStreamType;
139 140
	stream->ms.sessions=*sessions;
	stream->ms.qi=ms_quality_indicator_new(stream->ms.sessions.rtp_session);
Ghislain MARY's avatar
Ghislain MARY committed
141 142 143
	stream->ms.evq=ortp_ev_queue_new();
	stream->ms.rtpsend=ms_filter_new(MS_RTP_SEND_ID);
	stream->ms.ice_check_list=NULL;
144
	rtp_session_register_event_queue(stream->ms.sessions.rtp_session,stream->ms.evq);
Ghislain MARY's avatar
Ghislain MARY committed
145
	MS_VIDEO_SIZE_ASSIGN(stream->sent_vsize, CIF);
146
	stream->dir=VideoStreamSendRecv;
147
	stream->display_filter_auto_rotate_enabled=0;
148 149
	stream->source_performs_encoding = FALSE;
	stream->output_performs_decoding = FALSE;
150
	choose_display_name(stream);
Simon Morlat's avatar
Simon Morlat committed
151
	stream->ms.process_rtcp=video_stream_process_rtcp;
aymeric's avatar
aymeric committed
152 153 154
	return stream;
}

smorlat's avatar
smorlat committed
155
void video_stream_set_sent_video_size(VideoStream *stream, MSVideoSize vsize){
156
	ms_message("Setting video size %dx%d", vsize.width, vsize.height);
='s avatar
= committed
157
	stream->sent_vsize=vsize;
smorlat's avatar
smorlat committed
158 159
}

Ghislain MARY's avatar
Ghislain MARY committed
160
MSVideoSize video_stream_get_sent_video_size(const VideoStream *stream) {
Ghislain MARY's avatar
Ghislain MARY committed
161 162
	MSVideoSize vsize;
	MS_VIDEO_SIZE_ASSIGN(vsize, UNKNOWN);
163 164 165 166 167 168
	if (stream->ms.encoder != NULL) {
		ms_filter_call_method(stream->ms.encoder, MS_FILTER_GET_VIDEO_SIZE, &vsize);
	}
	return vsize;
}

Ghislain MARY's avatar
Ghislain MARY committed
169
MSVideoSize video_stream_get_received_video_size(const VideoStream *stream) {
Ghislain MARY's avatar
Ghislain MARY committed
170 171
	MSVideoSize vsize;
	MS_VIDEO_SIZE_ASSIGN(vsize, UNKNOWN);
172 173 174 175 176 177
	if (stream->ms.decoder != NULL) {
		ms_filter_call_method(stream->ms.decoder, MS_FILTER_GET_VIDEO_SIZE, &vsize);
	}
	return vsize;
}

aymeric's avatar
aymeric committed
178
void video_stream_set_relay_session_id(VideoStream *stream, const char *id){
Ghislain MARY's avatar
Ghislain MARY committed
179
	ms_filter_call_method(stream->ms.rtpsend, MS_RTP_SEND_SET_RELAY_SESSION_ID,(void*)id);
aymeric's avatar
aymeric committed
180 181
}

smorlat's avatar
smorlat committed
182
void video_stream_enable_self_view(VideoStream *stream, bool_t val){
='s avatar
= committed
183
	MSFilter *out=stream->output;
184 185 186 187
	stream->corner=val ? 0 : -1;
	if (out){
		ms_filter_call_method(out,MS_VIDEO_DISPLAY_SET_LOCAL_VIEW_MODE,&stream->corner);
	}
smorlat's avatar
smorlat committed
188
}
aymeric's avatar
aymeric committed
189

190 191 192 193 194
void video_stream_set_render_callback (VideoStream *s, VideoStreamRenderCallback cb, void *user_pointer){
	s->rendercb=cb;
	s->render_pointer=user_pointer;
}

195 196 197 198 199
void video_stream_set_event_callback (VideoStream *s, VideoStreamEventCallback cb, void *user_pointer){
	s->eventcb=cb;
	s->event_pointer=user_pointer;
}

200 201 202 203 204 205 206 207 208
void video_stream_set_display_filter_name(VideoStream *s, const char *fname){
	if (s->display_name!=NULL){
		ms_free(s->display_name);
		s->display_name=NULL;
	}
	if (fname!=NULL)
		s->display_name=ms_strdup(fname);
}

209 210

static void ext_display_cb(void *ud, MSFilter* f, unsigned int event, void *eventdata){
211 212 213 214 215 216 217 218 219
	MSExtDisplayOutput *output=(MSExtDisplayOutput*)eventdata;
	VideoStream *st=(VideoStream*)ud;
	if (st->rendercb!=NULL){
		st->rendercb(st->render_pointer,
		            output->local_view.w!=0 ? &output->local_view : NULL,
		            output->remote_view.w!=0 ? &output->remote_view : NULL);
	}
}

220
void video_stream_set_direction(VideoStream *vs, VideoStreamDir dir){
='s avatar
= committed
221
	vs->dir=dir;
222 223
}

Simon Morlat's avatar
Simon Morlat committed
224 225 226 227
static MSVideoSize get_compatible_size(MSVideoSize maxsize, MSVideoSize wished_size){
	int max_area=maxsize.width*maxsize.height;
	int whished_area=wished_size.width*wished_size.height;
	if (whished_area>max_area){
228
		return maxsize;
Simon Morlat's avatar
Simon Morlat committed
229 230 231 232
	}
	return wished_size;
}

233 234 235 236 237 238 239 240 241 242
static MSVideoSize get_with_same_orientation(MSVideoSize size, MSVideoSize refsize){
	if (ms_video_size_get_orientation(refsize)!=ms_video_size_get_orientation(size)){
		int tmp;
		tmp=size.width;
		size.width=size.height;
		size.height=tmp;
	}
	return size;
}

243 244 245 246
static void configure_video_source(VideoStream *stream){
	MSVideoSize vsize,cam_vsize;
	float fps=15;
	MSPixFmt format;
247
	bool_t encoder_has_builtin_converter = FALSE;
248

249 250
	/* transmit orientation to source filter */
	ms_filter_call_method(stream->source,MS_VIDEO_CAPTURE_SET_DEVICE_ORIENTATION,&stream->device_orientation);
Simon Morlat's avatar
Simon Morlat committed
251 252 253
	/* initialize the capture device orientation for preview */
	if( ms_filter_has_method(stream->source, MS_VIDEO_DISPLAY_SET_DEVICE_ORIENTATION) )
		ms_filter_call_method(stream->source,MS_VIDEO_DISPLAY_SET_DEVICE_ORIENTATION,&stream->device_orientation);
254

Simon Morlat's avatar
Simon Morlat committed
255
	/* transmit its preview window id if any to source filter*/
256 257 258
	if (stream->preview_window_id!=0){
		video_stream_set_native_preview_window_id(stream, stream->preview_window_id);
	}
259

Ghislain MARY's avatar
Ghislain MARY committed
260 261
	ms_filter_call_method(stream->ms.encoder, MS_VIDEO_ENCODER_HAS_BUILTIN_CONVERTER, &encoder_has_builtin_converter);
	ms_filter_call_method(stream->ms.encoder,MS_FILTER_GET_VIDEO_SIZE,&vsize);
Simon Morlat's avatar
Simon Morlat committed
262
	vsize=get_compatible_size(vsize,stream->sent_vsize);
263 264 265
	ms_filter_call_method(stream->source,MS_FILTER_SET_VIDEO_SIZE,&vsize);
	/*the camera may not support the target size and suggest a one close to the target */
	ms_filter_call_method(stream->source,MS_FILTER_GET_VIDEO_SIZE,&cam_vsize);
266 267
	if (cam_vsize.width*cam_vsize.height<=vsize.width*vsize.height &&
			cam_vsize.width != vsize.width){
268
		vsize=cam_vsize;
269
		ms_message("Output video size adjusted to match camera resolution (%ix%i)\n",vsize.width,vsize.height);
270
	} else if (cam_vsize.width*cam_vsize.height>vsize.width*vsize.height){
271
#if TARGET_IPHONE_SIMULATOR || defined(__arm__)
272 273 274 275 276 277
		ms_error("Camera is proposing a size bigger than encoder's suggested size (%ix%i > %ix%i) "
		           "Using the camera size as fallback because cropping or resizing is not implemented for arm.",
		           cam_vsize.width,cam_vsize.height,vsize.width,vsize.height);
		vsize=cam_vsize;
#else
		vsize=get_with_same_orientation(vsize,cam_vsize);
278
		ms_warning("Camera video size greater than encoder one. A scaling filter will be used!\n");
279
#endif
280
	}
Ghislain MARY's avatar
Ghislain MARY committed
281 282
	ms_filter_call_method(stream->ms.encoder,MS_FILTER_SET_VIDEO_SIZE,&vsize);
	ms_filter_call_method(stream->ms.encoder,MS_FILTER_GET_FPS,&fps);
283 284 285 286 287 288 289
	ms_message("Setting sent vsize=%ix%i, fps=%f",vsize.width,vsize.height,fps);
	/* configure the filters */
	if (ms_filter_get_id(stream->source)!=MS_STATIC_IMAGE_ID) {
		ms_filter_call_method(stream->source,MS_FILTER_SET_FPS,&fps);
	}
	/* get the output format for webcam reader */
	ms_filter_call_method(stream->source,MS_FILTER_GET_PIX_FMT,&format);
290

291
	if ((encoder_has_builtin_converter == TRUE) || (stream->source_performs_encoding == TRUE)) {
Ghislain MARY's avatar
Ghislain MARY committed
292
		ms_filter_call_method(stream->ms.encoder, MS_FILTER_SET_PIX_FMT, &format);
293 294 295 296 297 298 299 300 301 302 303
	} else {
		if (format==MS_MJPEG){
			stream->pixconv=ms_filter_new(MS_MJPEG_DEC_ID);
		}else{
			stream->pixconv = ms_filter_new(MS_PIX_CONV_ID);
			/*set it to the pixconv */
			ms_filter_call_method(stream->pixconv,MS_FILTER_SET_PIX_FMT,&format);
			ms_filter_call_method(stream->pixconv,MS_FILTER_SET_VIDEO_SIZE,&cam_vsize);
		}
		stream->sizeconv=ms_filter_new(MS_SIZE_CONV_ID);
		ms_filter_call_method(stream->sizeconv,MS_FILTER_SET_VIDEO_SIZE,&vsize);
304
	}
Ghislain MARY's avatar
Ghislain MARY committed
305 306 307
	if (stream->ms.rc){
		ms_bitrate_controller_destroy(stream->ms.rc);
		stream->ms.rc=NULL;
308
	}
Ghislain MARY's avatar
Ghislain MARY committed
309
	if (stream->ms.use_rc){
310
		stream->ms.rc=ms_av_bitrate_controller_new(NULL,NULL,stream->ms.sessions.rtp_session,stream->ms.encoder);
311
	}
312 313
}

314 315
int video_stream_start (VideoStream *stream, RtpProfile *profile, const char *rem_rtp_ip, int rem_rtp_port,
	const char *rem_rtcp_ip, int rem_rtcp_port, int payload, int jitt_comp, MSWebCam *cam){
aymeric's avatar
aymeric committed
316
	PayloadType *pt;
317
	RtpSession *rtps=stream->ms.sessions.rtp_session;
aymeric's avatar
aymeric committed
318
	MSPixFmt format;
319
	MSVideoSize disp_size;
smorlat's avatar
smorlat committed
320 321
	JBParameters jbp;
	const int socket_buf_size=2000000;
aymeric's avatar
aymeric committed
322

323 324
	if (cam==NULL){
		cam=ms_web_cam_manager_get_default_cam (
325
		      ms_web_cam_manager_get());
326 327
	}

aymeric's avatar
aymeric committed
328 329 330 331 332
	pt=rtp_profile_get_payload(profile,payload);
	if (pt==NULL){
		ms_error("videostream.c: undefined payload type.");
		return -1;
	}
333

334 335 336 337
	if ((cam != NULL) && (cam->desc->encode_to_mime_type != NULL) && (cam->desc->encode_to_mime_type(cam, pt->mime_type) == TRUE)) {
		stream->source_performs_encoding = TRUE;
	}

aymeric's avatar
aymeric committed
338
	rtp_session_set_profile(rtps,profile);
Simon Morlat's avatar
Simon Morlat committed
339 340
	if (rem_rtp_port>0)
		rtp_session_set_remote_addr_full(rtps,rem_rtp_ip,rem_rtp_port,rem_rtcp_ip,rem_rtcp_port);
aymeric's avatar
aymeric committed
341 342 343
	rtp_session_set_payload_type(rtps,payload);
	rtp_session_set_jitter_compensation(rtps,jitt_comp);

344
	rtp_session_signal_connect(stream->ms.sessions.rtp_session,"payload_type_changed",
345
			(RtpCallback)mediastream_payload_type_changed,(unsigned long)&stream->ms);
aymeric's avatar
aymeric committed
346

347
	rtp_session_get_jitter_buffer_params(stream->ms.sessions.rtp_session,&jbp);
smorlat's avatar
smorlat committed
348
	jbp.max_packets=1000;//needed for high resolution video
349 350 351
	rtp_session_set_jitter_buffer_params(stream->ms.sessions.rtp_session,&jbp);
	rtp_session_set_rtp_socket_recv_buffer_size(stream->ms.sessions.rtp_session,socket_buf_size);
	rtp_session_set_rtp_socket_send_buffer_size(stream->ms.sessions.rtp_session,socket_buf_size);
352

353
	if (stream->dir==VideoStreamSendRecv || stream->dir==VideoStreamSendOnly){
354
		MSConnectionHelper ch;
355 356
		/*plumb the outgoing stream */

357
		if (rem_rtp_port>0) ms_filter_call_method(stream->ms.rtpsend,MS_RTP_SEND_SET_SESSION,stream->ms.sessions.rtp_session);
358 359
		if (stream->source_performs_encoding == FALSE) {
			stream->ms.encoder=ms_filter_create_encoder(pt->mime_type);
Simon Morlat's avatar
Simon Morlat committed
360
			if (stream->ms.encoder==NULL){
361 362 363 364
				/* big problem: we don't have a registered codec for this payload...*/
				ms_error("videostream.c: No encoder available for payload %i:%s.",payload,pt->mime_type);
				return -1;
			}
365 366
		}
		/* creates the filters */
367
		stream->cam=cam;
368 369
		stream->source = ms_web_cam_create_reader(cam);
		stream->tee = ms_filter_new(MS_TEE_ID);
370 371 372
		if (stream->source_performs_encoding == TRUE) {
			stream->ms.encoder = stream->source;	/* Consider the encoder is the source */
		}
373

374
		if (pt->normal_bitrate>0){
375
			MSVideoConfiguration *vconf_list = NULL;
376
			ms_message("Limiting bitrate of video encoder to %i bits/s",pt->normal_bitrate);
377 378
			ms_filter_call_method(stream->ms.encoder, MS_VIDEO_ENCODER_GET_CONFIGURATION_LIST, &vconf_list);
			if (vconf_list != NULL) {
379
				MSVideoConfiguration vconf = ms_video_find_best_configuration_for_bitrate(vconf_list, pt->normal_bitrate);
380 381 382 383
				ms_filter_call_method(stream->ms.encoder, MS_VIDEO_ENCODER_SET_CONFIGURATION, &vconf);
			} else {
				ms_filter_call_method(stream->ms.encoder, MS_FILTER_SET_BITRATE, &pt->normal_bitrate);
			}
384 385
		}
		if (pt->send_fmtp){
Ghislain MARY's avatar
Ghislain MARY committed
386
			ms_filter_call_method(stream->ms.encoder,MS_FILTER_ADD_FMTP,pt->send_fmtp);
387
		}
388 389 390 391 392
		if (stream->use_preview_window){
			if (stream->rendercb==NULL){
				stream->output2=ms_filter_new_from_name (stream->display_name);
			}
		}
393

394
		configure_video_source (stream);
395 396 397 398 399 400 401 402 403 404
		/* and then connect all */
		ms_connection_helper_start(&ch);
		ms_connection_helper_link(&ch, stream->source, -1, 0);
		if (stream->pixconv) {
			ms_connection_helper_link(&ch, stream->pixconv, 0, 0);
		}
		if (stream->sizeconv) {
			ms_connection_helper_link(&ch, stream->sizeconv, 0, 0);
		}
		ms_connection_helper_link(&ch, stream->tee, 0, 0);
405 406 407
		if (stream->source_performs_encoding == FALSE) {
			ms_connection_helper_link(&ch, stream->ms.encoder, 0, 0);
		}
Ghislain MARY's avatar
Ghislain MARY committed
408
		ms_connection_helper_link(&ch, stream->ms.rtpsend, 0, -1);
409
		if (stream->output2){
410
			if (stream->preview_window_id!=0){
411 412 413 414
				ms_filter_call_method(stream->output2, MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID,&stream->preview_window_id);
			}
			ms_filter_link(stream->tee,1,stream->output2,0);
		}
aymeric's avatar
aymeric committed
415
	}
416
	if (stream->dir==VideoStreamSendRecv || stream->dir==VideoStreamRecvOnly){
417
		MSConnectionHelper ch;
418
		MSVideoDisplayDecodingSupport decoding_support;
419

420 421
		if (stream->rendercb!=NULL){
			stream->output=ms_filter_new(MS_EXT_DISPLAY_ID);
422
			ms_filter_add_notify_callback(stream->output,ext_display_cb,stream,TRUE);
423 424 425
		}else{
			stream->output=ms_filter_new_from_name (stream->display_name);
		}
426 427 428 429 430 431

		/* Don't allow null output */
		if(stream->output == NULL) {
			ms_fatal("No video display filter could be instantiated. Please check build-time configuration");
		}

432 433 434 435 436 437 438 439 440 441 442
		/* Check if the output filter can perform the decoding process */
		decoding_support.mime_type = pt->mime_type;
		decoding_support.supported = FALSE;
		ms_filter_call_method(stream->output, MS_VIDEO_DISPLAY_SUPPORT_DECODING, &decoding_support);
		stream->output_performs_decoding = decoding_support.supported;

		/*plumb the incoming stream */
		if (stream->output_performs_decoding == TRUE) {
			stream->ms.decoder = stream->output;	/* Consider the decoder is the output */
		} else {
			stream->ms.decoder=ms_filter_create_decoder(pt->mime_type);
Simon Morlat's avatar
Simon Morlat committed
443
			if (stream->ms.decoder==NULL){
444 445 446 447 448 449
				/* big problem: we don't have a registered decoderfor this payload...*/
				ms_error("videostream.c: No decoder available for payload %i:%s.",payload,pt->mime_type);
				ms_filter_destroy(stream->output);
				return -1;
			}
		}
450
		ms_filter_add_notify_callback(stream->ms.decoder, event_cb, stream,FALSE);
451 452

		stream->ms.rtprecv = ms_filter_new (MS_RTP_RECV_ID);
453
		ms_filter_call_method(stream->ms.rtprecv,MS_RTP_RECV_SET_SESSION,stream->ms.sessions.rtp_session);
454 455 456 457 458 459 460

		if (stream->output_performs_decoding == FALSE) {
			stream->jpegwriter=ms_filter_new(MS_JPEG_WRITER_ID);
			if (stream->jpegwriter)
				stream->tee2=ms_filter_new(MS_TEE_ID);
		}

461 462
		/* set parameters to the decoder*/
		if (pt->send_fmtp){
Ghislain MARY's avatar
Ghislain MARY committed
463
			ms_filter_call_method(stream->ms.decoder,MS_FILTER_ADD_FMTP,pt->send_fmtp);
464 465
		}
		if (pt->recv_fmtp!=NULL)
Ghislain MARY's avatar
Ghislain MARY committed
466
			ms_filter_call_method(stream->ms.decoder,MS_FILTER_ADD_FMTP,(void*)pt->recv_fmtp);
467

468 469
		/*force the decoder to output YUV420P */
		format=MS_YUV420P;
Ghislain MARY's avatar
Ghislain MARY committed
470
		ms_filter_call_method(stream->ms.decoder,MS_FILTER_SET_PIX_FMT,&format);
471 472

		/*configure the display window */
473
		if(stream->output != NULL) {
474
			int autofit = 1;
475 476 477 478 479 480
			disp_size.width=MS_VIDEO_SIZE_CIF_W;
			disp_size.height=MS_VIDEO_SIZE_CIF_H;
			ms_filter_call_method(stream->output,MS_FILTER_SET_VIDEO_SIZE,&disp_size);
			ms_filter_call_method(stream->output,MS_FILTER_SET_PIX_FMT,&format);
			ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_SET_LOCAL_VIEW_MODE,&stream->corner);
			if (stream->window_id!=0){
481
				autofit = 0;
482 483
				ms_filter_call_method(stream->output, MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID,&stream->window_id);
			}
484
			ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_ENABLE_AUTOFIT,&autofit);
485 486 487
			if (stream->display_filter_auto_rotate_enabled) {
				ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_SET_DEVICE_ORIENTATION,&stream->device_orientation);
			}
488
		}
489
		/* and connect the filters */
490
		ms_connection_helper_start (&ch);
Ghislain MARY's avatar
Ghislain MARY committed
491
		ms_connection_helper_link (&ch,stream->ms.rtprecv,-1,0);
492 493 494
		if (stream->output_performs_decoding == FALSE) {
			ms_connection_helper_link (&ch,stream->ms.decoder,0,0);
		}
495 496 497 498
		if (stream->tee2){
			ms_connection_helper_link (&ch,stream->tee2,0,0);
			ms_filter_link(stream->tee2,1,stream->jpegwriter,0);
		}
499 500
		if (stream->output!=NULL)
			ms_connection_helper_link (&ch,stream->output,0,-1);
501
		/* the video source must be send for preview , if it exists*/
502
		if (stream->tee!=NULL && stream->output!=NULL && stream->output2==NULL)
503
			ms_filter_link(stream->tee,1,stream->output,1);
aymeric's avatar
aymeric committed
504
	}
505
	if (stream->dir == VideoStreamSendOnly) {
Ghislain MARY's avatar
Ghislain MARY committed
506
		stream->ms.rtprecv = ms_filter_new (MS_RTP_RECV_ID);
507
		ms_filter_call_method(stream->ms.rtprecv, MS_RTP_RECV_SET_SESSION, stream->ms.sessions.rtp_session);
Ghislain MARY's avatar
Ghislain MARY committed
508 509
		stream->ms.voidsink = ms_filter_new(MS_VOID_SINK_ID);
		ms_filter_link(stream->ms.rtprecv, 0, stream->ms.voidsink, 0);
510
	}
511

aymeric's avatar
aymeric committed
512
	/* create the ticker */
513
	if (stream->ms.sessions.ticker==NULL) media_stream_start_ticker(&stream->ms);
514 515 516
	
	stream->ms.start_time=ms_time(NULL);
	stream->ms.is_beginning=TRUE;
517

518 519
	/* attach the graphs */
	if (stream->source)
520
		ms_ticker_attach (stream->ms.sessions.ticker, stream->source);
Ghislain MARY's avatar
Ghislain MARY committed
521
	if (stream->ms.rtprecv)
522 523 524
		ms_ticker_attach (stream->ms.sessions.ticker, stream->ms.rtprecv);
	
	stream->ms.state=MSStreamStarted;
aymeric's avatar
aymeric committed
525 526 527
	return 0;
}

528 529
void video_stream_prepare_video(VideoStream *stream){
	video_stream_unprepare_video(stream);
Ghislain MARY's avatar
Ghislain MARY committed
530
	stream->ms.rtprecv=ms_filter_new(MS_RTP_RECV_ID);
531 532
	rtp_session_set_payload_type(stream->ms.sessions.rtp_session,0);
	ms_filter_call_method(stream->ms.rtprecv,MS_RTP_RECV_SET_SESSION,stream->ms.sessions.rtp_session);
Ghislain MARY's avatar
Ghislain MARY committed
533 534
	stream->ms.voidsink=ms_filter_new(MS_VOID_SINK_ID);
	ms_filter_link(stream->ms.rtprecv,0,stream->ms.voidsink,0);
535 536 537
	media_stream_start_ticker(&stream->ms);
	ms_ticker_attach(stream->ms.sessions.ticker,stream->ms.rtprecv);
	stream->ms.state=MSStreamPreparing;
538 539
}

540
void video_stream_unprepare_video(VideoStream *stream){
541
	if (stream->ms.state==MSStreamPreparing) {
542
		stop_preload_graph(stream);
543
		stream->ms.state=MSStreamInitialized;
544 545 546
	}
}

547 548 549 550 551
void video_stream_update_video_params(VideoStream *stream){
	/*calling video_stream_change_camera() does the job of unplumbing/replumbing and configuring the new graph*/
	video_stream_change_camera(stream,stream->cam);
}

552
void video_stream_change_camera(VideoStream *stream, MSWebCam *cam){
Simon Morlat's avatar
Simon Morlat committed
553
	bool_t keep_source=(cam==stream->cam);
554 555
	bool_t encoder_has_builtin_converter = (!stream->pixconv && !stream->sizeconv);

556 557
	if (stream->ms.sessions.ticker && stream->source){
		ms_ticker_detach(stream->ms.sessions.ticker,stream->source);
558
		/*unlink source filters and subsequent post processin filters */
559
		if (encoder_has_builtin_converter || (stream->source_performs_encoding == TRUE)) {
560 561 562 563 564 565
			ms_filter_unlink(stream->source, 0, stream->tee, 0);
		} else {
			ms_filter_unlink (stream->source, 0, stream->pixconv, 0);
			ms_filter_unlink (stream->pixconv, 0, stream->sizeconv, 0);
			ms_filter_unlink (stream->sizeconv, 0, stream->tee, 0);
		}
566
		/*destroy the filters */
Simon Morlat's avatar
Simon Morlat committed
567
		if (!keep_source) ms_filter_destroy(stream->source);
568
		if (!encoder_has_builtin_converter && (stream->source_performs_encoding == FALSE)) {
569 570 571
			ms_filter_destroy(stream->pixconv);
			ms_filter_destroy(stream->sizeconv);
		}
572 573

		/*re create new ones and configure them*/
Simon Morlat's avatar
Simon Morlat committed
574
		if (!keep_source) stream->source = ms_web_cam_create_reader(cam);
575
		stream->cam=cam;
576 577 578 579 580 581 582 583 584 585 586

		/* update orientation */
		if (stream->source){
			ms_filter_call_method(stream->source,MS_VIDEO_CAPTURE_SET_DEVICE_ORIENTATION,&stream->device_orientation);
			if (!stream->display_filter_auto_rotate_enabled)
				ms_filter_call_method(stream->source,MS_VIDEO_DISPLAY_SET_DEVICE_ORIENTATION,&stream->device_orientation);
		}
		if (stream->output && stream->display_filter_auto_rotate_enabled) {
			ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_SET_DEVICE_ORIENTATION,&stream->device_orientation);
		}

587
		configure_video_source(stream);
588

589
		if (encoder_has_builtin_converter || (stream->source_performs_encoding == TRUE)) {
590 591 592 593 594 595 596
			ms_filter_link (stream->source, 0, stream->tee, 0);
		}
		else {
			ms_filter_link (stream->source, 0, stream->pixconv, 0);
			ms_filter_link (stream->pixconv, 0, stream->sizeconv, 0);
			ms_filter_link (stream->sizeconv, 0, stream->tee, 0);
		}
597

598
		ms_ticker_attach(stream->ms.sessions.ticker,stream->source);
599 600 601
	}
}

aymeric's avatar
aymeric committed
602
void video_stream_send_vfu(VideoStream *stream){
Ghislain MARY's avatar
Ghislain MARY committed
603 604
	if (stream->ms.encoder)
		ms_filter_call_method_noarg(stream->ms.encoder, MS_VIDEO_ENCODER_REQ_VFU);
aymeric's avatar
aymeric committed
605 606 607 608 609
}

void
video_stream_stop (VideoStream * stream)
{
610 611
	stream->eventcb = NULL;
	stream->event_pointer = NULL;
612 613
	if (stream->ms.sessions.ticker){
		if (stream->ms.state == MSStreamPreparing) {
614
			stop_preload_graph(stream);
615 616
		} else {
			if (stream->source)
617
				ms_ticker_detach(stream->ms.sessions.ticker,stream->source);
Ghislain MARY's avatar
Ghislain MARY committed
618
			if (stream->ms.rtprecv)
619
				ms_ticker_detach(stream->ms.sessions.ticker,stream->ms.rtprecv);
620

Ghislain MARY's avatar
Ghislain MARY committed
621 622 623
			if (stream->ms.ice_check_list != NULL) {
				ice_check_list_print_route(stream->ms.ice_check_list, "Video session's route");
				stream->ms.ice_check_list = NULL;
624
			}
625
			rtp_stats_display(rtp_session_get_stats(stream->ms.sessions.rtp_session),
Yann Diorcet's avatar
Yann Diorcet committed
626
				"             VIDEO SESSION'S RTP STATISTICS                ");
627 628

			if (stream->source){
629 630 631 632 633 634 635 636 637 638
				MSConnectionHelper ch;
				ms_connection_helper_start(&ch);
				ms_connection_helper_unlink(&ch, stream->source, -1, 0);
				if (stream->pixconv) {
					ms_connection_helper_unlink(&ch, stream->pixconv, 0, 0);
				}
				if (stream->sizeconv) {
					ms_connection_helper_unlink(&ch, stream->sizeconv, 0, 0);
				}
				ms_connection_helper_unlink(&ch, stream->tee, 0, 0);
639 640 641
				if (stream->source_performs_encoding == FALSE) {
					ms_connection_helper_unlink(&ch, stream->ms.encoder, 0, 0);
				}
Ghislain MARY's avatar
Ghislain MARY committed
642
				ms_connection_helper_unlink(&ch, stream->ms.rtpsend, 0, -1);
643 644 645
				if (stream->output2){
					ms_filter_unlink(stream->tee,1,stream->output2,0);
				}
646
			}
Ghislain MARY's avatar
Ghislain MARY committed
647 648 649
			if (stream->ms.voidsink) {
				ms_filter_unlink(stream->ms.rtprecv, 0, stream->ms.voidsink, 0);
			} else if (stream->ms.rtprecv){
650 651
				MSConnectionHelper h;
				ms_connection_helper_start (&h);
Ghislain MARY's avatar
Ghislain MARY committed
652
				ms_connection_helper_unlink (&h,stream->ms.rtprecv,-1,0);
653 654 655
				if (stream->output_performs_decoding == FALSE) {
					ms_connection_helper_unlink (&h,stream->ms.decoder,0,0);
				}
656 657 658 659 660 661 662 663
				if (stream->tee2){
					ms_connection_helper_unlink (&h,stream->tee2,0,0);
					ms_filter_unlink(stream->tee2,1,stream->jpegwriter,0);
				}
				if(stream->output)
					ms_connection_helper_unlink (&h,stream->output,0,-1);
				if (stream->tee && stream->output && stream->output2==NULL)
					ms_filter_unlink(stream->tee,1,stream->output,1);
664
			}
665
		}
aymeric's avatar
aymeric committed
666 667 668 669 670
	}
	video_stream_free (stream);
}


671 672 673 674 675 676 677
void video_stream_show_video(VideoStream *stream, bool_t show){
	if (stream->output){
		ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_SHOW_VIDEO,&show);
	}
}


678 679 680
unsigned long video_stream_get_native_window_id(VideoStream *stream){
	unsigned long id;
	if (stream->output){
unknown's avatar
unknown committed
681
		if (ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_GET_NATIVE_WINDOW_ID,&id)==0)
682 683
			return id;
	}
684 685 686 687 688
	return stream->window_id;
}

void video_stream_set_native_window_id(VideoStream *stream, unsigned long id){
	stream->window_id=id;
689 690 691
	if (stream->output){
		ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID,&id);
	}
692 693 694 695
}

void video_stream_set_native_preview_window_id(VideoStream *stream, unsigned long id){
	stream->preview_window_id=id;
696
#ifndef __ios	
697 698 699
	if (stream->output2){
		ms_filter_call_method(stream->output2,MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID,&id);
	}
700
#endif
701 702
	if (stream->source){
		ms_filter_call_method(stream->source,MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID,&id);
703
	}
704 705
}

Simon Morlat's avatar
wip  
Simon Morlat committed
706
unsigned long video_stream_get_native_preview_window_id(VideoStream *stream){
Simon Morlat's avatar
Simon Morlat committed
707
	unsigned long id=0;
Simon Morlat's avatar
Simon Morlat committed
708 709 710 711 712
	if (stream->output2){
		if (ms_filter_call_method(stream->output2,MS_VIDEO_DISPLAY_GET_NATIVE_WINDOW_ID,&id)==0)
			return id;
	}
	if (stream->source){
713
		if (ms_filter_has_method(stream->source,MS_VIDEO_DISPLAY_GET_NATIVE_WINDOW_ID) 
Simon Morlat's avatar
Simon Morlat committed
714
		    && ms_filter_call_method(stream->source,MS_VIDEO_DISPLAY_GET_NATIVE_WINDOW_ID,&id)==0)
715 716 717 718 719 720
			return id;
	}
	return stream->preview_window_id;
}

void video_stream_use_preview_video_window(VideoStream *stream, bool_t yesno){
='s avatar
= committed
721
	stream->use_preview_window=yesno;
722
}
aymeric's avatar
aymeric committed
723

724
void video_stream_set_device_rotation(VideoStream *stream, int orientation){
725
	stream->device_orientation = orientation;
726 727
}

728 729 730 731 732 733 734 735 736 737
int video_stream_get_camera_sensor_rotation(VideoStream *stream) {
	int rotation = -1;
	if (stream->source) {
		if (ms_filter_has_method(stream->source, MS_VIDEO_CAPTURE_GET_CAMERA_SENSOR_ROTATION)
			&& ms_filter_call_method(stream->source, MS_VIDEO_CAPTURE_GET_CAMERA_SENSOR_ROTATION, &rotation) == 0)
			return rotation;
	}
	return -1;
}

Simon Morlat's avatar
Simon Morlat committed
738 739
VideoPreview * video_preview_new(void){
	VideoPreview *stream = (VideoPreview *)ms_new0 (VideoPreview, 1);
Ghislain MARY's avatar
Ghislain MARY committed
740
	MS_VIDEO_SIZE_ASSIGN(stream->sent_vsize, CIF);
Simon Morlat's avatar
Simon Morlat committed
741 742 743 744
	choose_display_name(stream);
	return stream;
}

aymeric's avatar
aymeric committed
745

Simon Morlat's avatar
Simon Morlat committed
746
void video_preview_start(VideoPreview *stream, MSWebCam *device){
aymeric's avatar
aymeric committed
747
	MSPixFmt format;
aymeric's avatar
aymeric committed
748
	float fps=(float)29.97;
smorlat's avatar
smorlat committed
749
	int mirroring=1;
Simon Morlat's avatar
Simon Morlat committed
750
	int corner=-1;
Simon Morlat's avatar
Simon Morlat committed
751 752 753
	MSVideoSize disp_size=stream->sent_vsize;
	MSVideoSize vsize=disp_size;
	const char *displaytype=stream->display_name;
aymeric's avatar
aymeric committed
754 755

	stream->source = ms_web_cam_create_reader(device);
756

757

aymeric's avatar
aymeric committed
758
	/* configure the filters */
smorlat's avatar
smorlat committed
759
	ms_filter_call_method(stream->source,MS_FILTER_SET_VIDEO_SIZE,&vsize);
760 761
	if (ms_filter_get_id(stream->source)!=MS_STATIC_IMAGE_ID)
		ms_filter_call_method(stream->source,MS_FILTER_SET_FPS,&fps);
smorlat's avatar
smorlat committed
762
	ms_filter_call_method(stream->source,MS_FILTER_GET_PIX_FMT,&format);
763
	ms_filter_call_method(stream->source,MS_FILTER_GET_VIDEO_SIZE,&vsize);
aymeric's avatar
aymeric committed
764 765 766 767 768 769 770
	if (format==MS_MJPEG){
		stream->pixconv=ms_filter_new(MS_MJPEG_DEC_ID);
	}else{
		stream->pixconv=ms_filter_new(MS_PIX_CONV_ID);
		ms_filter_call_method(stream->pixconv,MS_FILTER_SET_PIX_FMT,&format);
		ms_filter_call_method(stream->pixconv,MS_FILTER_SET_VIDEO_SIZE,&vsize);
	}
771

aymeric's avatar
aymeric committed
772
	format=MS_YUV420P;
Simon Morlat's avatar
Simon Morlat committed
773

774
	stream->output2=ms_filter_new_from_name (displaytype);
Simon Morlat's avatar
Simon Morlat committed
775 776 777 778
	ms_filter_call_method(stream->output2,MS_FILTER_SET_PIX_FMT,&format);
	ms_filter_call_method(stream->output2,MS_FILTER_SET_VIDEO_SIZE,&disp_size);
	ms_filter_call_method(stream->output2,MS_VIDEO_DISPLAY_ENABLE_MIRRORING,&mirroring);
	ms_filter_call_method(stream->output2,MS_VIDEO_DISPLAY_SET_LOCAL_VIEW_MODE,&corner);
aymeric's avatar
aymeric committed
779
	/* and then connect all */
780

aymeric's avatar
aymeric committed
781
	ms_filter_link(stream->source,0, stream->pixconv,0);
Simon Morlat's avatar
Simon Morlat committed
782
	ms_filter_link(stream->pixconv, 0, stream->output2, 0);
783

784
	if (stream->preview_window_id!=0){
785
		video_stream_set_native_preview_window_id(stream, stream->preview_window_id);
786
	}
aymeric's avatar
aymeric committed
787
	/* create the ticker */
788 789 790 791
	stream->ms.sessions.ticker = ms_ticker_new();
	ms_ticker_set_name(stream->ms.sessions.ticker,"Video MSTicker");
	ms_ticker_attach (stream->ms.sessions.ticker, stream->source);
	stream->ms.state=MSStreamStarted;
aymeric's avatar
aymeric committed
792 793 794
}

void video_preview_stop(VideoStream *stream){
795
	ms_ticker_detach(stream->ms.sessions.ticker, stream->source);
aymeric's avatar
aymeric committed
796
	ms_filter_unlink(stream->source,0,stream->pixconv,0);
Simon Morlat's avatar
Simon Morlat committed
797
	ms_filter_unlink(stream->pixconv,0,stream->output2,0);
aymeric's avatar
aymeric committed
798 799 800
	video_stream_free(stream);
}

801 802
int video_stream_recv_only_start(VideoStream *videostream, RtpProfile *profile, const char *addr, int port, int used_pt, int jitt_comp){
	video_stream_set_direction(videostream,VideoStreamRecvOnly);
803
	return video_stream_start(videostream,profile,addr,port,addr,port+1,used_pt,jitt_comp,NULL);
804 805 806
}

int video_stream_send_only_start(VideoStream *videostream,
807
				RtpProfile *profile, const char *addr, int port, int rtcp_port,
808 809
				int used_pt, int  jitt_comp, MSWebCam *device){
	video_stream_set_direction (videostream,VideoStreamSendOnly);
810
	return video_stream_start(videostream,profile,addr,port,addr,rtcp_port,used_pt,jitt_comp,device);
811 812