videostream.c 35.1 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);
	}
}

73 74 75 76 77 78 79
static void internal_event_cb(void *ud, MSFilter *f, unsigned int event, void *eventdata) {
	VideoStream *stream = (VideoStream *)ud;
	const MSVideoCodecSLI *sli;
	const MSVideoCodecRPSI *rpsi;

	switch (event) {
		case MS_VIDEO_DECODER_SEND_PLI:
Ghislain MARY's avatar
Ghislain MARY committed
80
			ms_message("Request sending of PLI on videostream [%p]", stream);
81 82 83 84
			video_stream_send_pli(stream);
			break;
		case MS_VIDEO_DECODER_SEND_SLI:
			sli = (const MSVideoCodecSLI *)eventdata;
Ghislain MARY's avatar
Ghislain MARY committed
85
			ms_message("Request sending of SLI on videostream [%p]", stream);
86 87 88 89
			video_stream_send_sli(stream, sli->first, sli->number, sli->picture_id);
			break;
		case MS_VIDEO_DECODER_SEND_RPSI:
			rpsi = (const MSVideoCodecRPSI *)eventdata;
Ghislain MARY's avatar
Ghislain MARY committed
90
			ms_message("Request sending of RPSI on videostream [%p]", stream);
91 92 93 94 95
			video_stream_send_rpsi(stream, rpsi->bit_string, rpsi->bit_string_len);
			break;
	}
}

Simon Morlat's avatar
Simon Morlat committed
96
static void video_stream_process_rtcp(MediaStream *media_stream, mblk_t *m){
jehan's avatar
jehan committed
97
	VideoStream *stream = (VideoStream *)media_stream;
98 99 100 101 102 103 104 105 106 107 108 109 110 111
	int i;

	if (rtcp_is_PSFB(m)) {
		if (rtcp_PSFB_get_type(m) == RTCP_PSFB_FIR) {
			/* Special case for FIR where the packet sender ssrc must be equal to 0. */
			if ((rtcp_PSFB_get_media_source_ssrc(m) == rtp_session_get_send_ssrc(stream->ms.sessions.rtp_session))
				&& (rtcp_PSFB_get_packet_sender_ssrc(m) == 0)) {
				for (i = 0; ; i++) {
					rtcp_fb_fir_fci_t *fci = rtcp_PSFB_fir_get_fci(m, i);
					if (fci == NULL) break;
					if (rtcp_fb_fir_fci_get_ssrc(fci) == rtp_session_get_recv_ssrc(stream->ms.sessions.rtp_session)) {
						uint8_t seq_nr = rtcp_fb_fir_fci_get_seq_nr(fci);
						ms_filter_call_method(stream->ms.encoder, MS_VIDEO_ENCODER_NOTIFY_FIR, &seq_nr);
						break;
112 113
					}
				}
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
			}
		} else {
			if ((rtcp_PSFB_get_media_source_ssrc(m) == rtp_session_get_send_ssrc(stream->ms.sessions.rtp_session))
				&& (rtcp_PSFB_get_packet_sender_ssrc(m) == rtp_session_get_recv_ssrc(stream->ms.sessions.rtp_session))) {
				switch (rtcp_PSFB_get_type(m)) {
					case RTCP_PSFB_PLI:
						ms_filter_call_method_noarg(stream->ms.encoder, MS_VIDEO_ENCODER_NOTIFY_PLI);
						break;
					case RTCP_PSFB_SLI:
						for (i = 0; ; i++) {
							rtcp_fb_sli_fci_t *fci = rtcp_PSFB_sli_get_fci(m, i);
							MSVideoCodecSLI sli;
							if (fci == NULL) break;
							sli.first = rtcp_fb_sli_fci_get_first(fci);
							sli.number = rtcp_fb_sli_fci_get_number(fci);
							sli.picture_id = rtcp_fb_sli_fci_get_picture_id(fci);
							ms_filter_call_method(stream->ms.encoder, MS_VIDEO_ENCODER_NOTIFY_SLI, &sli);
Ghislain MARY's avatar
Ghislain MARY committed
131
						}
132 133 134 135 136 137 138 139
						break;
					case RTCP_PSFB_RPSI:
					{
						rtcp_fb_rpsi_fci_t *fci = rtcp_PSFB_rpsi_get_fci(m);
						MSVideoCodecRPSI rpsi;
						rpsi.bit_string = rtcp_fb_rpsi_fci_get_bit_string(fci);
						rpsi.bit_string_len = rtcp_PSFB_rpsi_get_fci_bit_string_len(m);
						ms_filter_call_method(stream->ms.encoder, MS_VIDEO_ENCODER_NOTIFY_RPSI, &rpsi);
140
					}
141 142 143
						break;
					default:
						break;
144 145
				}
			}
aymeric's avatar
aymeric committed
146
		}
147
	}
aymeric's avatar
aymeric committed
148 149
}

150
static void stop_preload_graph(VideoStream *stream){
151
	ms_ticker_detach(stream->ms.sessions.ticker,stream->ms.rtprecv);
Ghislain MARY's avatar
Ghislain MARY committed
152 153 154 155
	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;
156 157
}

aymeric's avatar
aymeric committed
158
void video_stream_iterate(VideoStream *stream){
159
	media_stream_iterate(&stream->ms);
aymeric's avatar
aymeric committed
160 161
}

Simon Morlat's avatar
Simon Morlat committed
162
const char *video_stream_get_default_video_renderer(void){
163
#ifdef WIN32
Simon Morlat's avatar
Simon Morlat committed
164
	return "MSDrawDibDisplay";
165
#elif defined(ANDROID)
Simon Morlat's avatar
Simon Morlat committed
166
	return "MSAndroidDisplay";
167
#elif __APPLE__ && !defined(__ios)
Simon Morlat's avatar
Simon Morlat committed
168
	return "MSOSXGLDisplay";
169
#elif defined (HAVE_XV)
Simon Morlat's avatar
Simon Morlat committed
170
	return "MSX11Video";
171
#elif defined(HAVE_GL)
Simon Morlat's avatar
Simon Morlat committed
172
	return "MSGLXVideo";
Simon Morlat's avatar
Simon Morlat committed
173
#elif defined(__ios)
Simon Morlat's avatar
Simon Morlat committed
174
	return "IOSDisplay";
Simon Morlat's avatar
Simon Morlat committed
175
#else
Simon Morlat's avatar
Simon Morlat committed
176
	return "MSVideoOut";
177 178 179
#endif
}

Simon Morlat's avatar
Simon Morlat committed
180 181 182 183
static void choose_display_name(VideoStream *stream){
	stream->display_name=ms_strdup(video_stream_get_default_video_renderer());
}

184
VideoStream *video_stream_new(int loc_rtp_port, int loc_rtcp_port, bool_t use_ipv6){
185 186 187 188 189 190 191 192 193
	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
194
	VideoStream *stream = (VideoStream *)ms_new0 (VideoStream, 1);
Ghislain MARY's avatar
Ghislain MARY committed
195
	stream->ms.type = VideoStreamType;
196 197
	stream->ms.sessions=*sessions;
	stream->ms.qi=ms_quality_indicator_new(stream->ms.sessions.rtp_session);
Ghislain MARY's avatar
Ghislain MARY committed
198 199 200
	stream->ms.evq=ortp_ev_queue_new();
	stream->ms.rtpsend=ms_filter_new(MS_RTP_SEND_ID);
	stream->ms.ice_check_list=NULL;
201
	rtp_session_register_event_queue(stream->ms.sessions.rtp_session,stream->ms.evq);
Ghislain MARY's avatar
Ghislain MARY committed
202
	MS_VIDEO_SIZE_ASSIGN(stream->sent_vsize, CIF);
203
	stream->dir=VideoStreamSendRecv;
204
	stream->display_filter_auto_rotate_enabled=0;
205 206
	stream->source_performs_encoding = FALSE;
	stream->output_performs_decoding = FALSE;
207
	choose_display_name(stream);
Simon Morlat's avatar
Simon Morlat committed
208
	stream->ms.process_rtcp=video_stream_process_rtcp;
aymeric's avatar
aymeric committed
209 210 211
	return stream;
}

smorlat's avatar
smorlat committed
212
void video_stream_set_sent_video_size(VideoStream *stream, MSVideoSize vsize){
213
	ms_message("Setting video size %dx%d", vsize.width, vsize.height);
='s avatar
= committed
214
	stream->sent_vsize=vsize;
smorlat's avatar
smorlat committed
215 216
}

Ghislain MARY's avatar
Ghislain MARY committed
217
MSVideoSize video_stream_get_sent_video_size(const VideoStream *stream) {
Ghislain MARY's avatar
Ghislain MARY committed
218 219
	MSVideoSize vsize;
	MS_VIDEO_SIZE_ASSIGN(vsize, UNKNOWN);
220 221 222 223 224 225
	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
226
MSVideoSize video_stream_get_received_video_size(const VideoStream *stream) {
Ghislain MARY's avatar
Ghislain MARY committed
227 228
	MSVideoSize vsize;
	MS_VIDEO_SIZE_ASSIGN(vsize, UNKNOWN);
229 230 231 232 233 234
	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
235
void video_stream_set_relay_session_id(VideoStream *stream, const char *id){
Ghislain MARY's avatar
Ghislain MARY committed
236
	ms_filter_call_method(stream->ms.rtpsend, MS_RTP_SEND_SET_RELAY_SESSION_ID,(void*)id);
aymeric's avatar
aymeric committed
237 238
}

smorlat's avatar
smorlat committed
239
void video_stream_enable_self_view(VideoStream *stream, bool_t val){
='s avatar
= committed
240
	MSFilter *out=stream->output;
241 242 243 244
	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
245
}
aymeric's avatar
aymeric committed
246

247 248 249 250 251
void video_stream_set_render_callback (VideoStream *s, VideoStreamRenderCallback cb, void *user_pointer){
	s->rendercb=cb;
	s->render_pointer=user_pointer;
}

252 253 254 255 256
void video_stream_set_event_callback (VideoStream *s, VideoStreamEventCallback cb, void *user_pointer){
	s->eventcb=cb;
	s->event_pointer=user_pointer;
}

257 258 259 260 261 262 263 264 265
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);
}

266 267

static void ext_display_cb(void *ud, MSFilter* f, unsigned int event, void *eventdata){
268 269 270 271 272 273 274 275 276
	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);
	}
}

277
void video_stream_set_direction(VideoStream *vs, VideoStreamDir dir){
='s avatar
= committed
278
	vs->dir=dir;
279 280
}

Simon Morlat's avatar
Simon Morlat committed
281 282 283 284
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){
285
		return maxsize;
Simon Morlat's avatar
Simon Morlat committed
286 287 288 289
	}
	return wished_size;
}

290 291 292 293 294 295 296 297 298 299
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;
}

300 301 302 303
static void configure_video_source(VideoStream *stream){
	MSVideoSize vsize,cam_vsize;
	float fps=15;
	MSPixFmt format;
304
	MSVideoEncoderPixFmt encoder_supports_source_format;
305
	int ret;
306

307 308
	/* 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
309 310 311
	/* 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);
312

Simon Morlat's avatar
Simon Morlat committed
313
	/* transmit its preview window id if any to source filter*/
314 315 316
	if (stream->preview_window_id!=0){
		video_stream_set_native_preview_window_id(stream, stream->preview_window_id);
	}
317

Ghislain MARY's avatar
Ghislain MARY committed
318
	ms_filter_call_method(stream->ms.encoder,MS_FILTER_GET_VIDEO_SIZE,&vsize);
Simon Morlat's avatar
Simon Morlat committed
319
	vsize=get_compatible_size(vsize,stream->sent_vsize);
320 321 322
	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);
323 324
	if (cam_vsize.width*cam_vsize.height<=vsize.width*vsize.height &&
			cam_vsize.width != vsize.width){
325
		vsize=cam_vsize;
326
		ms_message("Output video size adjusted to match camera resolution (%ix%i)\n",vsize.width,vsize.height);
327
	} else if (cam_vsize.width*cam_vsize.height>vsize.width*vsize.height){
328
#if TARGET_IPHONE_SIMULATOR || defined(__arm__)
329 330 331 332 333 334
		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);
335
		ms_warning("Camera video size greater than encoder one. A scaling filter will be used!\n");
336
#endif
337
	}
Ghislain MARY's avatar
Ghislain MARY committed
338 339
	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);
340 341 342 343 344 345 346
	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);
347

348 349 350
	encoder_supports_source_format.supported = FALSE;
	encoder_supports_source_format.pixfmt = format;

351 352 353 354 355 356
	ret = ms_filter_call_method(stream->ms.encoder, MS_VIDEO_ENCODER_SUPPORTS_PIXFMT, &encoder_supports_source_format);

	if (ret == -1) {
		// Encoder doesn't have MS_VIDEO_ENCODER_SUPPORTS_PIXFMT method
		encoder_supports_source_format.supported = (format == MS_YUV420P);
	}
357 358

	if ((encoder_supports_source_format.supported == TRUE) || (stream->source_performs_encoding == TRUE)) {
Ghislain MARY's avatar
Ghislain MARY committed
359
		ms_filter_call_method(stream->ms.encoder, MS_FILTER_SET_PIX_FMT, &format);
360 361 362 363 364 365 366 367 368 369 370
	} 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);
371
	}
Ghislain MARY's avatar
Ghislain MARY committed
372 373 374
	if (stream->ms.rc){
		ms_bitrate_controller_destroy(stream->ms.rc);
		stream->ms.rc=NULL;
375
	}
Ghislain MARY's avatar
Ghislain MARY committed
376
	if (stream->ms.use_rc){
377
		stream->ms.rc=ms_av_bitrate_controller_new(NULL,NULL,stream->ms.sessions.rtp_session,stream->ms.encoder);
378
	}
379 380
}

381 382
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
383
	PayloadType *pt;
384
	RtpSession *rtps=stream->ms.sessions.rtp_session;
aymeric's avatar
aymeric committed
385
	MSPixFmt format;
386
	MSVideoSize disp_size;
smorlat's avatar
smorlat committed
387 388
	JBParameters jbp;
	const int socket_buf_size=2000000;
389
	bool_t avpf_enabled = FALSE;
aymeric's avatar
aymeric committed
390

391 392
	if (cam==NULL){
		cam=ms_web_cam_manager_get_default_cam (
393
		      ms_web_cam_manager_get());
394 395
	}

aymeric's avatar
aymeric committed
396 397 398 399 400
	pt=rtp_profile_get_payload(profile,payload);
	if (pt==NULL){
		ms_error("videostream.c: undefined payload type.");
		return -1;
	}
401
	if (pt->flags & PAYLOAD_TYPE_RTCP_FEEDBACK_ENABLED) avpf_enabled = TRUE;
402

403 404 405 406
	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
407
	rtp_session_set_profile(rtps,profile);
Simon Morlat's avatar
Simon Morlat committed
408 409
	if (rem_rtp_port>0)
		rtp_session_set_remote_addr_full(rtps,rem_rtp_ip,rem_rtp_port,rem_rtcp_ip,rem_rtcp_port);
410 411 412 413 414
	if (rem_rtcp_port > 0) {
		rtp_session_enable_rtcp(rtps, TRUE);
	} else {
		rtp_session_enable_rtcp(rtps, FALSE);
	}
aymeric's avatar
aymeric committed
415 416 417
	rtp_session_set_payload_type(rtps,payload);
	rtp_session_set_jitter_compensation(rtps,jitt_comp);

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

421
	rtp_session_get_jitter_buffer_params(stream->ms.sessions.rtp_session,&jbp);
smorlat's avatar
smorlat committed
422
	jbp.max_packets=1000;//needed for high resolution video
423 424 425
	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);
426

427
	if (stream->dir==VideoStreamSendRecv || stream->dir==VideoStreamSendOnly){
428
		MSConnectionHelper ch;
429 430
		/*plumb the outgoing stream */

431
		if (rem_rtp_port>0) ms_filter_call_method(stream->ms.rtpsend,MS_RTP_SEND_SET_SESSION,stream->ms.sessions.rtp_session);
432 433
		if (stream->source_performs_encoding == FALSE) {
			stream->ms.encoder=ms_filter_create_encoder(pt->mime_type);
Simon Morlat's avatar
Simon Morlat committed
434
			if (stream->ms.encoder==NULL){
435 436 437 438
				/* 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;
			}
439 440
		}
		/* creates the filters */
441
		stream->cam=cam;
442 443
		stream->source = ms_web_cam_create_reader(cam);
		stream->tee = ms_filter_new(MS_TEE_ID);
444 445 446
		if (stream->source_performs_encoding == TRUE) {
			stream->ms.encoder = stream->source;	/* Consider the encoder is the source */
		}
447

448
		if (pt->normal_bitrate>0){
449
			MSVideoConfiguration *vconf_list = NULL;
450
			ms_message("Limiting bitrate of video encoder to %i bits/s",pt->normal_bitrate);
451 452
			ms_filter_call_method(stream->ms.encoder, MS_VIDEO_ENCODER_GET_CONFIGURATION_LIST, &vconf_list);
			if (vconf_list != NULL) {
453
				MSVideoConfiguration vconf = ms_video_find_best_configuration_for_bitrate(vconf_list, pt->normal_bitrate);
454 455 456 457
				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);
			}
458
			rtp_session_set_target_upload_bandwidth(stream->ms.sessions.rtp_session, pt->normal_bitrate);
459 460
		}
		if (pt->send_fmtp){
Ghislain MARY's avatar
Ghislain MARY committed
461
			ms_filter_call_method(stream->ms.encoder,MS_FILTER_ADD_FMTP,pt->send_fmtp);
462
		}
463
		ms_filter_call_method(stream->ms.encoder, MS_VIDEO_ENCODER_ENABLE_AVPF, &avpf_enabled);
464 465 466 467 468
		if (stream->use_preview_window){
			if (stream->rendercb==NULL){
				stream->output2=ms_filter_new_from_name (stream->display_name);
			}
		}
469

470
		configure_video_source (stream);
471 472 473 474 475 476 477 478 479 480
		/* 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);
481 482 483
		if (stream->source_performs_encoding == FALSE) {
			ms_connection_helper_link(&ch, stream->ms.encoder, 0, 0);
		}
Ghislain MARY's avatar
Ghislain MARY committed
484
		ms_connection_helper_link(&ch, stream->ms.rtpsend, 0, -1);
485
		if (stream->output2){
486
			if (stream->preview_window_id!=0){
487 488 489 490
				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
491
	}
492
	if (stream->dir==VideoStreamSendRecv || stream->dir==VideoStreamRecvOnly){
493 494
		MSConnectionHelper ch;

495 496 497 498 499 500 501 502 503
		/* create decoder first */
		stream->ms.decoder=ms_filter_create_decoder(pt->mime_type);
		if (stream->ms.decoder==NULL){
			/* 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);
			return -1;
		}

		/* display logic */
504
		if (stream->rendercb!=NULL){
505
			/* rendering logic delegated to user supplied callback */
506
			stream->output=ms_filter_new(MS_EXT_DISPLAY_ID);
507
			ms_filter_add_notify_callback(stream->output,ext_display_cb,stream,TRUE);
508
		}else{
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
			/* no user supplied callback -> create filter */
			MSVideoDisplayDecodingSupport decoding_support;

			/* Check if the decoding filter can perform the rendering */
			decoding_support.mime_type = pt->mime_type;
			decoding_support.supported = FALSE;
			ms_filter_call_method(stream->ms.decoder, MS_VIDEO_DECODER_SUPPORT_RENDERING, &decoding_support);
			stream->output_performs_decoding = decoding_support.supported;

			if (stream->output_performs_decoding) {
				stream->output = stream->ms.decoder;
			} else {
				/* Create default display filter */
				stream->output = ms_filter_new_from_name (stream->display_name);
			}
524
		}
525 526 527 528 529 530

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

531
		ms_filter_add_notify_callback(stream->ms.decoder, event_cb, stream, FALSE);
532 533
		/* It is important that the internal_event_cb is called synchronously! */
		ms_filter_add_notify_callback(stream->ms.decoder, internal_event_cb, stream, TRUE);
534 535

		stream->ms.rtprecv = ms_filter_new (MS_RTP_RECV_ID);
536
		ms_filter_call_method(stream->ms.rtprecv,MS_RTP_RECV_SET_SESSION,stream->ms.sessions.rtp_session);
537 538 539 540 541 542 543

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

544 545 546 547 548
		/* Define target upload bandwidth for RTCP packets sending. */
		if (pt->normal_bitrate > 0) {
			rtp_session_set_target_upload_bandwidth(stream->ms.sessions.rtp_session, pt->normal_bitrate);
		}

549 550
		/* set parameters to the decoder*/
		if (pt->send_fmtp){
Ghislain MARY's avatar
Ghislain MARY committed
551
			ms_filter_call_method(stream->ms.decoder,MS_FILTER_ADD_FMTP,pt->send_fmtp);
552 553
		}
		if (pt->recv_fmtp!=NULL)
Ghislain MARY's avatar
Ghislain MARY committed
554
			ms_filter_call_method(stream->ms.decoder,MS_FILTER_ADD_FMTP,(void*)pt->recv_fmtp);
555
		ms_filter_call_method(stream->ms.decoder, MS_VIDEO_DECODER_ENABLE_AVPF, &avpf_enabled);
556

557 558
		/*force the decoder to output YUV420P */
		format=MS_YUV420P;
Ghislain MARY's avatar
Ghislain MARY committed
559
		ms_filter_call_method(stream->ms.decoder,MS_FILTER_SET_PIX_FMT,&format);
560 561

		/*configure the display window */
562
		if(stream->output != NULL) {
563
			int autofit = 1;
564 565 566
			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);
567 568 569 570 571 572 573 574 575 576 577

			/* if pixconv is used, force yuv420 */
			if (stream->pixconv || !stream->source)
				ms_filter_call_method(stream->output,MS_FILTER_SET_PIX_FMT,&format);
			/* else, use format from input */
			else {
				MSPixFmt source_format;
				ms_filter_call_method(stream->source,MS_FILTER_GET_PIX_FMT,&source_format);
				ms_filter_call_method(stream->output,MS_FILTER_SET_PIX_FMT,&source_format);
			}

578 579
			ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_SET_LOCAL_VIEW_MODE,&stream->corner);
			if (stream->window_id!=0){
580
				autofit = 0;
581 582
				ms_filter_call_method(stream->output, MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID,&stream->window_id);
			}
583
			ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_ENABLE_AUTOFIT,&autofit);
584 585 586
			if (stream->display_filter_auto_rotate_enabled) {
				ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_SET_DEVICE_ORIENTATION,&stream->device_orientation);
			}
587
		}
588
		/* and connect the filters */
589
		ms_connection_helper_start (&ch);
Ghislain MARY's avatar
Ghislain MARY committed
590
		ms_connection_helper_link (&ch,stream->ms.rtprecv,-1,0);
591 592 593
		if (stream->output_performs_decoding == FALSE) {
			ms_connection_helper_link (&ch,stream->ms.decoder,0,0);
		}
594 595 596 597
		if (stream->tee2){
			ms_connection_helper_link (&ch,stream->tee2,0,0);
			ms_filter_link(stream->tee2,1,stream->jpegwriter,0);
		}
598 599
		if (stream->output!=NULL)
			ms_connection_helper_link (&ch,stream->output,0,-1);
600
		/* the video source must be send for preview , if it exists*/
601
		if (stream->tee!=NULL && stream->output!=NULL && stream->output2==NULL)
602
			ms_filter_link(stream->tee,1,stream->output,1);
aymeric's avatar
aymeric committed
603
	}
604
	if (stream->dir == VideoStreamSendOnly) {
Ghislain MARY's avatar
Ghislain MARY committed
605
		stream->ms.rtprecv = ms_filter_new (MS_RTP_RECV_ID);
606
		ms_filter_call_method(stream->ms.rtprecv, MS_RTP_RECV_SET_SESSION, stream->ms.sessions.rtp_session);
Ghislain MARY's avatar
Ghislain MARY committed
607 608
		stream->ms.voidsink = ms_filter_new(MS_VOID_SINK_ID);
		ms_filter_link(stream->ms.rtprecv, 0, stream->ms.voidsink, 0);
609
	}
610

aymeric's avatar
aymeric committed
611
	/* create the ticker */
612
	if (stream->ms.sessions.ticker==NULL) media_stream_start_ticker(&stream->ms);
613

614
	stream->ms.start_time=ms_time(NULL);
615

616 617
	/* attach the graphs */
	if (stream->source)
618
		ms_ticker_attach (stream->ms.sessions.ticker, stream->source);
Ghislain MARY's avatar
Ghislain MARY committed
619
	if (stream->ms.rtprecv)
620
		ms_ticker_attach (stream->ms.sessions.ticker, stream->ms.rtprecv);
621

622
	stream->ms.state=MSStreamStarted;
aymeric's avatar
aymeric committed
623 624 625
	return 0;
}

626 627
void video_stream_prepare_video(VideoStream *stream){
	video_stream_unprepare_video(stream);
Ghislain MARY's avatar
Ghislain MARY committed
628
	stream->ms.rtprecv=ms_filter_new(MS_RTP_RECV_ID);
629
	rtp_session_set_payload_type(stream->ms.sessions.rtp_session,0);
630
	rtp_session_enable_rtcp(stream->ms.sessions.rtp_session, FALSE);
631
	ms_filter_call_method(stream->ms.rtprecv,MS_RTP_RECV_SET_SESSION,stream->ms.sessions.rtp_session);
Ghislain MARY's avatar
Ghislain MARY committed
632 633
	stream->ms.voidsink=ms_filter_new(MS_VOID_SINK_ID);
	ms_filter_link(stream->ms.rtprecv,0,stream->ms.voidsink,0);
634 635 636
	media_stream_start_ticker(&stream->ms);
	ms_ticker_attach(stream->ms.sessions.ticker,stream->ms.rtprecv);
	stream->ms.state=MSStreamPreparing;
637 638
}

639
void video_stream_unprepare_video(VideoStream *stream){
640
	if (stream->ms.state==MSStreamPreparing) {
641
		stop_preload_graph(stream);
642
		stream->ms.state=MSStreamInitialized;
643 644 645
	}
}

646 647 648 649 650
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);
}

651
void video_stream_change_camera(VideoStream *stream, MSWebCam *cam){
Simon Morlat's avatar
Simon Morlat committed
652
	bool_t keep_source=(cam==stream->cam);
653 654
	bool_t encoder_has_builtin_converter = (!stream->pixconv && !stream->sizeconv);

655 656
	if (stream->ms.sessions.ticker && stream->source){
		ms_ticker_detach(stream->ms.sessions.ticker,stream->source);
657
		/*unlink source filters and subsequent post processin filters */
658
		if (encoder_has_builtin_converter || (stream->source_performs_encoding == TRUE)) {
659 660 661 662 663 664
			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);
		}
665
		/*destroy the filters */
Simon Morlat's avatar
Simon Morlat committed
666
		if (!keep_source) ms_filter_destroy(stream->source);
667
		if (!encoder_has_builtin_converter && (stream->source_performs_encoding == FALSE)) {
668 669 670
			ms_filter_destroy(stream->pixconv);
			ms_filter_destroy(stream->sizeconv);
		}
671 672

		/*re create new ones and configure them*/
Simon Morlat's avatar
Simon Morlat committed
673
		if (!keep_source) stream->source = ms_web_cam_create_reader(cam);
674
		stream->cam=cam;
675 676 677 678 679 680 681 682 683 684 685

		/* 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);
		}

686
		configure_video_source(stream);
687

688
		if (encoder_has_builtin_converter || (stream->source_performs_encoding == TRUE)) {
689 690 691 692 693 694 695
			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);
		}
696

697
		ms_ticker_attach(stream->ms.sessions.ticker,stream->source);
698 699 700
	}
}

701 702 703 704 705 706 707 708 709 710 711 712
void video_stream_send_fir(VideoStream *stream) {
	if (stream->ms.sessions.rtp_session != NULL) {
		rtp_session_send_rtcp_fb_fir(stream->ms.sessions.rtp_session);
	}
}

void video_stream_send_pli(VideoStream *stream) {
	if (stream->ms.sessions.rtp_session != NULL) {
		rtp_session_send_rtcp_fb_pli(stream->ms.sessions.rtp_session);
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
713 714 715 716 717 718 719 720 721 722 723 724
void video_stream_send_sli(VideoStream *stream, uint16_t first, uint16_t number, uint8_t picture_id) {
	if (stream->ms.sessions.rtp_session != NULL) {
		rtp_session_send_rtcp_fb_sli(stream->ms.sessions.rtp_session, first, number, picture_id);
	}
}

void video_stream_send_rpsi(VideoStream *stream, uint8_t *bit_string, uint16_t bit_string_len) {
	if (stream->ms.sessions.rtp_session != NULL) {
		rtp_session_send_rtcp_fb_rpsi(stream->ms.sessions.rtp_session, bit_string, bit_string_len);
	}
}

aymeric's avatar
aymeric committed
725
void video_stream_send_vfu(VideoStream *stream){
Ghislain MARY's avatar
Ghislain MARY committed
726 727
	if (stream->ms.encoder)
		ms_filter_call_method_noarg(stream->ms.encoder, MS_VIDEO_ENCODER_REQ_VFU);
aymeric's avatar
aymeric committed
728 729 730 731 732
}

void
video_stream_stop (VideoStream * stream)
{
733 734
	stream->eventcb = NULL;
	stream->event_pointer = NULL;
735 736
	if (stream->ms.sessions.ticker){
		if (stream->ms.state == MSStreamPreparing) {
737
			stop_preload_graph(stream);
738 739
		} else {
			if (stream->source)
740
				ms_ticker_detach(stream->ms.sessions.ticker,stream->source);
Ghislain MARY's avatar
Ghislain MARY committed
741
			if (stream->ms.rtprecv)
742
				ms_ticker_detach(stream->ms.sessions.ticker,stream->ms.rtprecv);
743

Ghislain MARY's avatar
Ghislain MARY committed
744 745 746
			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;
747
			}
748
			rtp_stats_display(rtp_session_get_stats(stream->ms.sessions.rtp_session),
Yann Diorcet's avatar
Yann Diorcet committed
749
				"             VIDEO SESSION'S RTP STATISTICS                ");
750 751

			if (stream->source){
752 753 754 755 756 757 758 759 760 761
				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);
762 763 764
				if (stream->source_performs_encoding == FALSE) {
					ms_connection_helper_unlink(&ch, stream->ms.encoder, 0, 0);
				}
Ghislain MARY's avatar
Ghislain MARY committed
765
				ms_connection_helper_unlink(&ch, stream->ms.rtpsend, 0, -1);
766 767 768
				if (stream->output2){
					ms_filter_unlink(stream->tee,1,stream->output2,0);
				}
769
			}
Ghislain MARY's avatar
Ghislain MARY committed
770 771 772
			if (stream->ms.voidsink) {
				ms_filter_unlink(stream->ms.rtprecv, 0, stream->ms.voidsink, 0);
			} else if (stream->ms.rtprecv){
773 774
				MSConnectionHelper h;
				ms_connection_helper_start (&h);
Ghislain MARY's avatar
Ghislain MARY committed
775
				ms_connection_helper_unlink (&h,stream->ms.rtprecv,-1,0);
776 777 778
				if (stream->output_performs_decoding == FALSE) {
					ms_connection_helper_unlink (&h,stream->ms.decoder,0,0);
				}
779 780 781 782 783 784 785 786
				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);
787
			}
788
		}
aymeric's avatar
aymeric committed
789 790 791 792 793
	}
	video_stream_free (stream);
}


794 795 796 797 798 799 800
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);
	}
}


801 802 803
unsigned long video_stream_get_native_window_id(VideoStream *stream){
	unsigned long id;
	if (stream->output){
unknown's avatar
unknown committed
804
		if (ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_GET_NATIVE_WINDOW_ID,&id)==0)
805 806
			return id;
	}
807 808 809 810 811
	return stream->window_id;
}

void video_stream_set_native_window_id(VideoStream *stream, unsigned long id){
	stream->window_id=id;
812 813 814
	if (stream->output){
		ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID,&id);
	}
815 816 817 818
}

void video_stream_set_native_preview_window_id(VideoStream *stream, unsigned long id){
	stream->preview_window_id=id;
819
#ifndef __ios
820 821 822
	if (stream->output2){
		ms_filter_call_method(stream->output2,MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID,&id);
	}
823
#endif
824 825
	if (stream->source){
		ms_filter_call_method(stream->source,MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID,&id);
826
	}
827 828
}

Simon Morlat's avatar
wip  
Simon Morlat committed
829
unsigned long video_stream_get_native_preview_window_id(VideoStream *stream){
Simon Morlat's avatar
Simon Morlat committed
830
	unsigned long id=0;
Simon Morlat's avatar
Simon Morlat committed
831 832 833 834 835
	if (stream->output2){
		if (ms_filter_call_method(stream->output2,MS_VIDEO_DISPLAY_GET_NATIVE_WINDOW_ID,&id)==0)
			return id;
	}
	if (stream->source){
836
		if (ms_filter_has_method(stream->source,MS_VIDEO_DISPLAY_GET_NATIVE_WINDOW_ID)
Simon Morlat's avatar
Simon Morlat committed
837
		    && ms_filter_call_method(stream->source,MS_VIDEO_DISPLAY_GET_NATIVE_WINDOW_ID,&id)==0)
838 839 840 841 842 843
			return id;
	}
	return stream->preview_window_id;
}

void video_stream_use_preview_video_window(VideoStream *stream, bool_t yesno){
='s avatar
= committed
844
	stream->use_preview_window=yesno;
845
}
aymeric's avatar
aymeric committed
846

847
void video_stream_set_device_rotation(VideoStream *stream, int orientation){
848
	stream->device_orientation = orientation;
849 850
}