static int rtcp_xr_voip_metrics_init(uint8_t *buf, RtpSession *session) { JBParameters jbparams; uint32_t expected_packets; uint32_t lost_packets; rtcp_xr_voip_metrics_report_block_t *block = (rtcp_xr_voip_metrics_report_block_t *)buf; float rtt = rtp_session_get_round_trip_propagation(session); uint16_t int_rtt = (rtt >= 0) ? (rtt * 1000) : 0; float qi = -1; float lq_qi = -1; rtp_session_get_jitter_buffer_params(session, &jbparams); if (session->rtcp.xr_media_callbacks.average_qi != NULL) { qi = session->rtcp.xr_media_callbacks.average_qi(session->rtcp.xr_media_callbacks.userdata); } if (session->rtcp.xr_media_callbacks.average_lq_qi != NULL) { lq_qi = session->rtcp.xr_media_callbacks.average_lq_qi(session->rtcp.xr_media_callbacks.userdata); } block->bh.bt = RTCP_XR_VOIP_METRICS; block->bh.flags = 0; // Reserved bits block->bh.length = htons(8); block->ssrc = htonl(rtp_session_get_recv_ssrc(session)); block->gmin = RTCP_XR_GMIN; // Fill RX config block->rx_config = 0; if (jbparams.adaptive) { block->rx_config |= RTCP_XR_VOIP_METRICS_CONFIG_JBA_ADA; } else { block->rx_config |= RTCP_XR_VOIP_METRICS_CONFIG_JBA_NON; } if (session->rtcp.xr_media_callbacks.plc != NULL) { switch (session->rtcp.xr_media_callbacks.plc(session->rtcp.xr_media_callbacks.userdata)) { default: case OrtpRtcpXrNoPlc: block->rx_config |= RTCP_XR_VOIP_METRICS_CONFIG_PLC_UNS; break; case OrtpRtcpXrSilencePlc: block->rx_config |= RTCP_XR_VOIP_METRICS_CONFIG_PLC_DIS; break; case OrtpRtcpXrEnhancedPlc: block->rx_config |= RTCP_XR_VOIP_METRICS_CONFIG_PLC_ENH; break; } } else { block->rx_config |= RTCP_XR_VOIP_METRICS_CONFIG_PLC_UNS; } // Fill JB fields block->jb_nominal = htons((uint16_t)jbparams.nom_size); if (jbparams.adaptive) { block->jb_maximum = htons((session->rtp.jittctl.adapt_jitt_comp_ts * 1000) / session->rtp.jittctl.clock_rate); } else { block->jb_maximum = block->jb_nominal; } block->jb_abs_max = htons(65535); if (session->rtcp_xr_stats.rcv_count > 0) { expected_packets = session->rtcp_xr_stats.last_rcv_seq - session->rtcp_xr_stats.first_rcv_seq + 1; lost_packets = expected_packets - session->rtcp_xr_stats.rcv_count; block->loss_rate = calc_rate((double)lost_packets, (double)expected_packets); block->discard_rate = calc_rate((double)session->rtcp_xr_stats.discarded_count, (double)expected_packets); // TODO: fill burst_density, gap_density, burst_duration, gap_duration block->burst_density = 0; block->gap_density = 0; block->burst_duration = htons(0); block->gap_duration = htons(0); block->round_trip_delay = htons(int_rtt); // TODO: fill end_system_delay block->end_system_delay = htons(0); if (session->rtcp.xr_media_callbacks.signal_level != NULL) { block->signal_level = session->rtcp.xr_media_callbacks.signal_level(session->rtcp.xr_media_callbacks.userdata); } else { block->signal_level = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; } if (session->rtcp.xr_media_callbacks.noise_level != NULL) { block->noise_level = session->rtcp.xr_media_callbacks.noise_level(session->rtcp.xr_media_callbacks.userdata); } else { block->noise_level = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; } block->rerl = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; if (qi < 0) { block->r_factor = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; } else { block->r_factor = (uint8_t)(qi * 20); } block->ext_r_factor = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; if (lq_qi < 0) { block->mos_lq = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; } else { block->mos_lq = (uint8_t)(lq_qi * 10); if (block->mos_lq < 10) block->mos_lq = 10; } if (qi < 0) { block->mos_cq = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; } else { block->mos_cq = (uint8_t)(qi * 10); if (block->mos_cq < 10) block->mos_cq = 10; } } else { block->loss_rate = 0; block->discard_rate = 0; block->burst_density = 0; block->gap_density = 0; block->burst_duration = htons(0); block->gap_duration = htons(0); block->round_trip_delay = htons(0); block->end_system_delay = htons(0); block->signal_level = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; block->noise_level = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; block->rerl = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; block->r_factor = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; block->ext_r_factor = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; block->mos_lq = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; block->mos_cq = ORTP_RTCP_XR_UNAVAILABLE_PARAMETER; } return sizeof(rtcp_xr_voip_metrics_report_block_t); }
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){ PayloadType *pt; RtpSession *rtps=stream->ms.session; MSPixFmt format; MSVideoSize disp_size; int tmp; JBParameters jbp; const int socket_buf_size=2000000; if (cam==NULL){ cam=ms_web_cam_manager_get_default_cam ( ms_web_cam_manager_get()); } pt=rtp_profile_get_payload(profile,payload); if (pt==NULL){ ms_error("videostream.c: undefined payload type."); return -1; } 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; } rtp_session_set_profile(rtps,profile); if (rem_rtp_port>0) rtp_session_set_remote_addr_full(rtps,rem_rtp_ip,rem_rtp_port,rem_rtcp_ip,rem_rtcp_port); rtp_session_set_payload_type(rtps,payload); rtp_session_set_jitter_compensation(rtps,jitt_comp); rtp_session_signal_connect(stream->ms.session,"payload_type_changed", (RtpCallback)mediastream_payload_type_changed,(unsigned long)&stream->ms); rtp_session_get_jitter_buffer_params(stream->ms.session,&jbp); jbp.max_packets=1000;//needed for high resolution video rtp_session_set_jitter_buffer_params(stream->ms.session,&jbp); rtp_session_set_rtp_socket_recv_buffer_size(stream->ms.session,socket_buf_size); rtp_session_set_rtp_socket_send_buffer_size(stream->ms.session,socket_buf_size); if (stream->dir==VideoStreamSendRecv || stream->dir==VideoStreamSendOnly){ MSConnectionHelper ch; /*plumb the outgoing stream */ if (rem_rtp_port>0) ms_filter_call_method(stream->ms.rtpsend,MS_RTP_SEND_SET_SESSION,stream->ms.session); if (stream->source_performs_encoding == FALSE) { stream->ms.encoder=ms_filter_create_encoder(pt->mime_type); if ((stream->ms.encoder==NULL) ){ /* 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; } } /* creates the filters */ stream->cam=cam; stream->source = ms_web_cam_create_reader(cam); stream->tee = ms_filter_new(MS_TEE_ID); if (stream->source_performs_encoding == TRUE) { stream->ms.encoder = stream->source; /* Consider the encoder is the source */ } if (pt->normal_bitrate>0){ MSVideoConfiguration *vconf_list = NULL; ms_message("Limiting bitrate of video encoder to %i bits/s",pt->normal_bitrate); ms_filter_call_method(stream->ms.encoder, MS_VIDEO_ENCODER_GET_CONFIGURATION_LIST, &vconf_list); if (vconf_list != NULL) { MSVideoConfiguration vconf = ms_video_find_best_configuration_for_bitrate(vconf_list, pt->normal_bitrate); 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); } } if (pt->send_fmtp){ ms_filter_call_method(stream->ms.encoder,MS_FILTER_ADD_FMTP,pt->send_fmtp); } if (stream->use_preview_window){ if (stream->rendercb==NULL){ stream->output2=ms_filter_new_from_name (stream->display_name); } } configure_video_source (stream); /* 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); if (stream->source_performs_encoding == FALSE) { ms_connection_helper_link(&ch, stream->ms.encoder, 0, 0); } ms_connection_helper_link(&ch, stream->ms.rtpsend, 0, -1); if (stream->output2){ if (stream->preview_window_id!=0){ 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); } } if (stream->dir==VideoStreamSendRecv || stream->dir==VideoStreamRecvOnly){ MSConnectionHelper ch; MSVideoDisplayDecodingSupport decoding_support; if (stream->rendercb!=NULL){ stream->output=ms_filter_new(MS_EXT_DISPLAY_ID); ms_filter_set_notify_callback(stream->output,ext_display_cb,stream); }else{ stream->output=ms_filter_new_from_name (stream->display_name); } /* Don't allow null output */ if(stream->output == NULL) { ms_fatal("No video display filter could be instantiated. Please check build-time configuration"); } /* 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); 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); ms_filter_destroy(stream->output); return -1; } } ms_filter_set_notify_callback(stream->ms.decoder, event_cb, stream); stream->ms.rtprecv = ms_filter_new (MS_RTP_RECV_ID); ms_filter_call_method(stream->ms.rtprecv,MS_RTP_RECV_SET_SESSION,stream->ms.session); 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); } /* set parameters to the decoder*/ if (pt->send_fmtp){ ms_filter_call_method(stream->ms.decoder,MS_FILTER_ADD_FMTP,pt->send_fmtp); } if (pt->recv_fmtp!=NULL) ms_filter_call_method(stream->ms.decoder,MS_FILTER_ADD_FMTP,(void*)pt->recv_fmtp); /*force the decoder to output YUV420P */ format=MS_YUV420P; ms_filter_call_method(stream->ms.decoder,MS_FILTER_SET_PIX_FMT,&format); /*configure the display window */ if(stream->output != NULL) { disp_size.width=MS_VIDEO_SIZE_CIF_W; disp_size.height=MS_VIDEO_SIZE_CIF_H; tmp=1; ms_filter_call_method(stream->output,MS_FILTER_SET_VIDEO_SIZE,&disp_size); ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_ENABLE_AUTOFIT,&tmp); 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){ ms_filter_call_method(stream->output, MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID,&stream->window_id); } if (stream->display_filter_auto_rotate_enabled) { ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_SET_DEVICE_ORIENTATION,&stream->device_orientation); } } /* and connect the filters */ ms_connection_helper_start (&ch); ms_connection_helper_link (&ch,stream->ms.rtprecv,-1,0); if (stream->output_performs_decoding == FALSE) { ms_connection_helper_link (&ch,stream->ms.decoder,0,0); } if (stream->tee2){ ms_connection_helper_link (&ch,stream->tee2,0,0); ms_filter_link(stream->tee2,1,stream->jpegwriter,0); } if (stream->output!=NULL) ms_connection_helper_link (&ch,stream->output,0,-1); /* the video source must be send for preview , if it exists*/ if (stream->tee!=NULL && stream->output!=NULL && stream->output2==NULL) ms_filter_link(stream->tee,1,stream->output,1); } if (stream->dir == VideoStreamSendOnly) { stream->ms.rtprecv = ms_filter_new (MS_RTP_RECV_ID); ms_filter_call_method(stream->ms.rtprecv, MS_RTP_RECV_SET_SESSION, stream->ms.session); stream->ms.voidsink = ms_filter_new(MS_VOID_SINK_ID); ms_filter_link(stream->ms.rtprecv, 0, stream->ms.voidsink, 0); } /* create the ticker */ if (stream->ms.ticker==NULL) start_ticker(&stream->ms); stream->ms.start_time=ms_time(NULL); stream->ms.is_beginning=TRUE; /* attach the graphs */ if (stream->source) ms_ticker_attach (stream->ms.ticker, stream->source); if (stream->ms.rtprecv) ms_ticker_attach (stream->ms.ticker, stream->ms.rtprecv); return 0; }
int video_stream_start (VideoStream *stream, RtpProfile *profile, const char *remip, int remport, int rem_rtcp_port, int payload, int jitt_comp, MSWebCam *cam){ PayloadType *pt; RtpSession *rtps=stream->session; MSPixFmt format; MSVideoSize disp_size; int tmp; JBParameters jbp; const int socket_buf_size=2000000; if (cam==NULL){ cam=ms_web_cam_manager_get_default_cam ( ms_web_cam_manager_get()); } pt=rtp_profile_get_payload(profile,payload); if (pt==NULL){ ms_error("videostream.c: undefined payload type."); return -1; } rtp_session_set_profile(rtps,profile); if (remport>0) rtp_session_set_remote_addr_full(rtps,remip,remport,rem_rtcp_port); rtp_session_set_payload_type(rtps,payload); rtp_session_set_jitter_compensation(rtps,jitt_comp); rtp_session_signal_connect(stream->session,"payload_type_changed", (RtpCallback)payload_type_changed,(unsigned long)stream); rtp_session_set_recv_buf_size(stream->session,MAX_RTP_SIZE); rtp_session_get_jitter_buffer_params(stream->session,&jbp); jbp.max_packets=1000;//needed for high resolution video rtp_session_set_jitter_buffer_params(stream->session,&jbp); rtp_session_set_rtp_socket_recv_buffer_size(stream->session,socket_buf_size); rtp_session_set_rtp_socket_send_buffer_size(stream->session,socket_buf_size); if (stream->dir==VideoStreamSendRecv || stream->dir==VideoStreamSendOnly){ /*plumb the outgoing stream */ if (remport>0) ms_filter_call_method(stream->rtpsend,MS_RTP_SEND_SET_SESSION,stream->session); stream->encoder=ms_filter_create_encoder(pt->mime_type); if ((stream->encoder==NULL) ){ /* 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; } /* creates the filters */ stream->source = ms_web_cam_create_reader(cam); stream->tee = ms_filter_new(MS_TEE_ID); if (pt->normal_bitrate>0){ ms_message("Limiting bitrate of video encoder to %i bits/s",pt->normal_bitrate); ms_filter_call_method(stream->encoder,MS_FILTER_SET_BITRATE,&pt->normal_bitrate); } if (pt->send_fmtp){ ms_filter_call_method(stream->encoder,MS_FILTER_ADD_FMTP,pt->send_fmtp); } if (stream->use_preview_window){ if (stream->rendercb==NULL){ stream->output2=ms_filter_new_from_name (stream->display_name); } } configure_video_source (stream); /* and then connect all */ 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); ms_filter_link (stream->tee, 0 ,stream->encoder, 0 ); ms_filter_link (stream->encoder,0, stream->rtpsend,0); if (stream->output2){ if (stream->window_id!=0){ 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); } } if (stream->dir==VideoStreamSendRecv || stream->dir==VideoStreamRecvOnly){ MSConnectionHelper ch; /*plumb the incoming stream */ stream->decoder=ms_filter_create_decoder(pt->mime_type); if ((stream->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; } stream->rtprecv = ms_filter_new (MS_RTP_RECV_ID); ms_filter_call_method(stream->rtprecv,MS_RTP_RECV_SET_SESSION,stream->session); if(stream->conf_mode==FALSE) { stream->tee2=ms_filter_new(MS_TEE_ID); stream->jpegwriter=ms_filter_new(MS_JPEG_WRITER_ID); } if (stream->rendercb!=NULL){ stream->output=ms_filter_new(MS_EXT_DISPLAY_ID); ms_filter_set_notify_callback (stream->output,ext_display_cb,stream); }else{ if(stream->conf_mode) stream->output=ms_filter_new(MS_VOID_SINK_ID); else stream->output=ms_filter_new_from_name (stream->display_name); } /* set parameters to the decoder*/ if (pt->send_fmtp){ ms_filter_call_method(stream->decoder,MS_FILTER_ADD_FMTP,pt->send_fmtp); } if (pt->recv_fmtp!=NULL) ms_filter_call_method(stream->decoder,MS_FILTER_ADD_FMTP,(void*)pt->recv_fmtp); /*force the decoder to output YUV420P */ format=MS_YUV420P; ms_filter_call_method(stream->decoder,MS_FILTER_SET_PIX_FMT,&format); /*configure the display window */ disp_size.width=MS_VIDEO_SIZE_CIF_W; disp_size.height=MS_VIDEO_SIZE_CIF_H; tmp=1; ms_filter_call_method(stream->output,MS_FILTER_SET_VIDEO_SIZE,&disp_size); ms_filter_call_method(stream->output,MS_VIDEO_DISPLAY_ENABLE_AUTOFIT,&tmp); 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){ ms_filter_call_method(stream->output, MS_VIDEO_DISPLAY_SET_NATIVE_WINDOW_ID,&stream->window_id); } /* and connect the filters */ ms_connection_helper_start (&ch); ms_connection_helper_link (&ch,stream->rtprecv,-1,0); ms_connection_helper_link (&ch,stream->decoder,0,0); if (stream->tee2){ ms_connection_helper_link (&ch,stream->tee2,0,0); ms_filter_link(stream->tee2,1,stream->jpegwriter,0); //Êä³öÊÓƵ¼ÖÆÖ¡. if(stream->video_record) ms_filter_link(stream->tee2,2,stream->video_record,0); } ms_connection_helper_link (&ch,stream->output,0,-1); /* the video source must be send for preview , if it exists*/ if (stream->tee!=NULL && stream->output2==NULL) ms_filter_link(stream->tee,1,stream->output,1); } /* create the ticker */ stream->ticker = ms_ticker_new(); ms_ticker_set_name(stream->ticker,"Video MSTicker"); //¼æÈÝPVX Ðëµ÷Õû±àÂë¼ä¸ô //stream->ticker->interval = 3; #ifdef ENABLE_UDT if(stream->enable_udt){ stream->udt = ms_filter_new(MS_UDT_TRANSPORT_ID); ms_filter_call_method(stream->udt,MS_UDT_SET_RTP_SESSION,stream->session); } #endif //ENABLE_UDT /* attach the graphs */ if (stream->source) ms_ticker_attach (stream->ticker, stream->source); if (stream->rtprecv) ms_ticker_attach (stream->ticker, stream->rtprecv); #ifdef ENABLE_UDT //×îºóÔËÐУ¬±ÜÃâ×èÈûUI if (stream->udt) ms_ticker_attach (stream->ticker, stream->udt); #endif // ENABLE_UDT return 0; }