📄 sap.c
字号:
msg_Err( p_sap, "%s", vlc_gai_strerror( i ) ); return VLC_EGENERIC; } msg_Dbg( p_sap, "using SAP address: %s", psz_addr); /* XXX: Check for dupes */ p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t)); p_sap_session->p_address = NULL; /* Add the address to the buffer */ for( i = 0; i < p_sap->i_addresses; i++) { if( !strcmp( psz_addr, p_sap->pp_addresses[i]->psz_address ) ) { p_sap_session->p_address = p_sap->pp_addresses[i]; break; } } if( p_sap_session->p_address == NULL ) { sap_address_t *p_address = (sap_address_t *) malloc( sizeof(sap_address_t) ); if( !p_address ) { msg_Err( p_sap, "out of memory" ); return VLC_ENOMEM; } p_address->psz_address = strdup( psz_addr ); p_address->i_wfd = net_ConnectUDP( p_sap, psz_addr, SAP_PORT, 255 ); if( p_address->i_wfd != -1 ) { char *ptr; net_StopRecv( p_address->i_wfd ); net_GetSockAddress( p_address->i_wfd, p_address->psz_machine, NULL ); /* removes scope if present */ ptr = strchr( p_address->psz_machine, '%' ); if( ptr != NULL ) *ptr = '\0'; } if( p_sap->b_control == VLC_TRUE ) { p_address->i_rfd = net_OpenUDP( p_sap, psz_addr, SAP_PORT, "", 0 ); if( p_address->i_rfd != -1 ) net_StopSend( p_address->i_rfd ); p_address->i_buff = 0; p_address->b_enabled = VLC_TRUE; p_address->b_ready = VLC_FALSE; p_address->i_limit = 10000; /* 10000 bps */ p_address->t1 = 0; } else { p_address->b_enabled = VLC_TRUE; p_address->b_ready = VLC_TRUE; p_address->i_interval = config_GetInt( p_sap,"sap-interval"); p_address->i_rfd = -1; } if( p_address->i_wfd == -1 || (p_address->i_rfd == -1 && p_sap->b_control ) ) { msg_Warn( p_sap, "disabling address" ); p_address->b_enabled = VLC_FALSE; } INSERT_ELEM( p_sap->pp_addresses, p_sap->i_addresses, p_sap->i_addresses, p_address ); p_sap_session->p_address = p_address; } /* Build the SAP Headers */ i_header_size = ( b_ipv6 ? 16 : 4 ) + 20; psz_head = (char *) malloc( i_header_size * sizeof( char ) ); if( psz_head == NULL ) { msg_Err( p_sap, "out of memory" ); return VLC_ENOMEM; } /* SAPv1, not encrypted, not compressed */ psz_head[0] = b_ipv6 ? 0x30 : 0x20; psz_head[1] = 0x00; /* No authentification length */ i_hash = mdate(); psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */ psz_head[3] = (i_hash & 0xFF); /* Msg id hash 2 */#if defined (HAVE_INET_PTON) || defined (WIN32) if( b_ipv6 ) { inet_pton( AF_INET6, /* can't fail */ p_sap_session->p_address->psz_machine, psz_head + 4 ); } else#endif { inet_pton( AF_INET, /* can't fail */ p_sap_session->p_address->psz_machine, psz_head + 4 ); } memcpy( psz_head + (b_ipv6 ? 20 : 8), "application/sdp", 15 ); /* If needed, build the SDP */ if( p_session->psz_sdp == NULL ) { p_session->psz_sdp = SDPGenerate( p_sap, p_session, p_sap_session->p_address, b_ssm ); if( p_session->psz_sdp == NULL ) { vlc_mutex_unlock( &p_sap->object_lock ); return VLC_ENOMEM; } } p_sap_session->psz_sdp = strdup( p_session->psz_sdp ); p_sap_session->i_last = 0; psz_head[ i_header_size-1 ] = '\0'; p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp); p_sap_session->psz_data = (uint8_t *)malloc( sizeof(char)* p_sap_session->i_length ); /* Build the final message */ memcpy( p_sap_session->psz_data, psz_head, i_header_size ); memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp, strlen( p_sap_session->psz_sdp) ); free( psz_head ); /* Enqueue the announce */ INSERT_ELEM( p_sap->pp_sessions, p_sap->i_sessions, p_sap->i_sessions, p_sap_session ); msg_Dbg( p_sap,"%i addresses, %i sessions", p_sap->i_addresses,p_sap->i_sessions); /* Remember the SAP session for later deletion */ p_session->p_sap = p_sap_session; vlc_mutex_unlock( &p_sap->object_lock ); return VLC_SUCCESS;}/* Remove a SAP Announce */static int announce_SAPAnnounceDel( sap_handler_t *p_sap, session_descriptor_t *p_session ){ int i; vlc_mutex_lock( &p_sap->object_lock ); msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap); /* Dequeue the announce */ for( i = 0; i< p_sap->i_sessions; i++) { if( p_session->p_sap == p_sap->pp_sessions[i] ) { REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions, i ); FREE( p_session->p_sap->psz_sdp ); FREE( p_session->p_sap->psz_data ); free( p_session->p_sap ); break; } } /* XXX: Dequeue the address too if it is not used anymore * TODO: - address refcount - send a SAP deletion packet */ msg_Dbg( p_sap,"%i announcements remaining", p_sap->i_sessions ); vlc_mutex_unlock( &p_sap->object_lock ); return VLC_SUCCESS;}static int announce_SendSAPAnnounce( sap_handler_t *p_sap, sap_session_t *p_session ){ int i_ret; /* This announce has never been sent yet */ if( p_session->i_last == 0 ) { p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000; p_session->i_last = 1; return VLC_SUCCESS; } if( p_session->i_next < mdate() ) {#ifdef EXTRA_DEBUG msg_Dbg( p_sap, "sending announce");#endif i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL, p_session->psz_data, p_session->i_length ); if( i_ret != (int)p_session->i_length ) { msg_Warn( p_sap, "SAP send failed on address %s (%i %i)", p_session->p_address->psz_address, i_ret, p_session->i_length ); } p_session->i_last = p_session->i_next; p_session->i_next = p_session->i_last + p_session->p_address->i_interval*1000000; } else { return VLC_SUCCESS; } return VLC_SUCCESS;}static char *SDPGenerate( sap_handler_t *p_sap, const session_descriptor_t *p_session, const sap_address_t *p_addr, vlc_bool_t b_ssm ){ int64_t i_sdp_id = mdate(); int i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff); char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri, *psz_sdp; char ipv; char *sfilter = NULL; int res; psz_group = p_session->psz_group; psz_name = p_session->psz_name; /* FIXME: really check that psz_uri is a real IP address * FIXME: make a common function to obtain a canonical IP address */ ipv = ( strchr( p_session->psz_uri, ':' ) != NULL) ? '6' : '4'; if( *p_session->psz_uri == '[' ) { char *ptr; strlcpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) ); ptr = strchr( psz_uribuf, '%' ); if( ptr != NULL) *ptr = '\0'; ptr = strchr( psz_uribuf, ']' ); if( ptr != NULL) *ptr = '\0'; psz_uri = psz_uribuf; } else psz_uri = p_session->psz_uri; if (b_ssm) { if (asprintf (&sfilter, "a=source-filter: incl IN IP%c * %s\r\n", ipv, p_addr->psz_machine) == -1) return NULL; } /* see the lists in modules/stream_out/rtp.c for compliance stuff */ res = asprintf (&psz_sdp, "v=0\r\n" "o=- "I64Fd" %d IN IP%c %s\r\n" "s=%s\r\n" "c=IN IP%c %s%s\r\n" "t=0 0\r\n" "a=tool:"PACKAGE_STRING"\r\n" "a=recvonly\r\n" "a=type:broadcast\r\n" "%s" "%s%s%s" "m=video %d %s %d\r\n", i_sdp_id, i_sdp_version, ipv, p_addr->psz_machine, psz_name, ipv, psz_uri, (ipv == 4) ? "/255" : "", (sfilter != NULL) ? sfilter : "", psz_group ? "a=x-plgroup:" : "", psz_group ? psz_group : "", psz_group ? "\r\n" : "", p_session->i_port, p_session->b_rtp ? "RTP/AVP" : "udp", p_session->i_payload); if (sfilter != NULL) free (sfilter); if (res == -1) return NULL; msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(psz_sdp), psz_sdp ); return psz_sdp;}static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address ){ int i_read; uint8_t buffer[SAP_MAX_BUFFER]; int i_tot = 0; mtime_t i_temp; int i_rate; if( p_address->t1 == 0 ) { p_address->t1 = mdate(); return VLC_SUCCESS; } do { /* Might be too slow if we have huge data */ i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, NULL, buffer, SAP_MAX_BUFFER, 0 ); i_tot += i_read; } while( i_read > 0 && i_tot < SAP_MAX_BUFFER ); i_temp = mdate(); /* We calculate the rate every 5 seconds */ if( i_temp - p_address->t1 < 5000000 ) { p_address->i_buff += i_tot; return VLC_SUCCESS; } /* Bits/second */ i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) / (i_temp - p_address->t1 )); p_address->i_limit = 10000; p_address->i_interval = ((1000*i_rate / p_address->i_limit) * (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL; if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 ) { p_address->i_interval = MAX_INTERVAL; }#ifdef EXTRA_DEBUG msg_Dbg( p_sap,"%s:%i: rate=%i, interval = %i s", p_address->psz_address,SAP_PORT, i_rate, p_address->i_interval );#endif p_address->b_ready = VLC_TRUE; p_address->t1 = i_temp; p_address->i_buff = 0; return VLC_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -