📄 sap.c
字号:
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 = ( p_method->i_ip_version == 6 ? 20 : 8 ) + strlen( psz_type ) + 1; psz_head = (char *) malloc( i_header_size * sizeof( char ) ); if( ! psz_head ) { msg_Err( p_sap, "out of memory" ); return VLC_ENOMEM; } psz_head[0] = 0x20; /* Means SAPv1, IPv4, not encrypted, not compressed */ 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( p_method->i_ip_version == 6 ) { /* in_addr_t ip_server = inet_addr( ip ); */ psz_head[0] |= 0x10; /* Set IPv6 */ psz_head[4] = 0x01; /* Source IP FIXME: we should get the real address */ psz_head[5] = 0x02; /* idem */ psz_head[6] = 0x03; /* idem */ psz_head[7] = 0x04; /* idem */ psz_head[8] = 0x01; /* Source IP FIXME: we should get the real address */ psz_head[9] = 0x02; /* idem */ psz_head[10] = 0x03; /* idem */ psz_head[11] = 0x04; /* idem */ psz_head[12] = 0x01; /* Source IP FIXME: we should get the real address */ psz_head[13] = 0x02; /* idem */ psz_head[14] = 0x03; /* idem */ psz_head[15] = 0x04; /* idem */ psz_head[16] = 0x01; /* Source IP FIXME: we should get the real address */ psz_head[17] = 0x02; /* idem */ psz_head[18] = 0x03; /* idem */ psz_head[19] = 0x04; /* idem */ strncpy( psz_head + 20, psz_type, 15 ); } else { /* in_addr_t ip_server = inet_addr( ip) */ /* Source IP FIXME: we should get the real address */ psz_head[4] = 0x01; /* ip_server */ psz_head[5] = 0x02; /* ip_server>>8 */ psz_head[6] = 0x03; /* ip_server>>16 */ psz_head[7] = 0x04; /* ip_server>>24 */ strncpy( psz_head + 8, psz_type, 15 ); } 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 = (char *)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,"Addresses: %i Sessions: %i", 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 announces 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 != 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 int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session ){ int64_t i_sdp_id = mdate(); int i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff); char *psz_group, *psz_name; char ipv; psz_group = convert_to_utf8( p_sap, p_session->psz_group ); psz_name = convert_to_utf8( p_sap, p_session->psz_name ); ipv = ( strchr( p_session->psz_uri, ':' ) != NULL) ? '6' : '4'; /* see the lists in modules/stream_out/rtp.c for compliance stuff */ p_session->psz_sdp = (char *)malloc( sizeof("v=0\r\n" "o=- 45383436098 45398 IN IP4 127.0.0.1\r\n" /* FIXME */ "s=\r\n" "t=0 0\r\n" "c=IN IP4 /\r\n" "m=video udp\r\n" "a=tool:"PACKAGE_STRING"\r\n" "a=type:broadcast\r\n") + strlen( psz_name ) + strlen( p_session->psz_uri ) + 300 + ( psz_group ? strlen( psz_group ) : 0 ) ); if( p_session->psz_sdp == NULL || psz_name == NULL ) { msg_Err( p_sap, "out of memory" ); FREE( psz_name ); FREE( psz_group ); return VLC_ENOMEM; } sprintf( p_session->psz_sdp, "v=0\r\n" "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n" "s=%s\r\n" "t=0 0\r\n" "c=IN IP%c %s/%d\r\n" "m=video %d udp %d\r\n" "a=tool:"PACKAGE_STRING"\r\n" "a=type:broadcast\r\n", i_sdp_id, i_sdp_version, psz_name, ipv, p_session->psz_uri, p_session->i_ttl, p_session->i_port, p_session->i_payload ); free( psz_name ); if( psz_group ) { sprintf( p_session->psz_sdp, "%sa=x-plgroup:%s\r\n", p_session->psz_sdp, psz_group ); free( psz_group ); } msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(p_session->psz_sdp), p_session->psz_sdp ); return VLC_SUCCESS;}static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address ){ int i_read; char 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,p_address->i_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;}static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local ){ char *psz_unicode, *psz_in, *psz_out; size_t ret, i_in, i_out; if( psz_local == NULL ) return NULL; if ( p_this->iconvHandle == (vlc_iconv_t)(-1) ) return strdup( psz_local ); psz_in = psz_local; i_in = strlen( psz_local ); i_out = 6 * i_in; psz_unicode = malloc( i_out + 1 ); if( psz_unicode == NULL ) return strdup( psz_local ); psz_out = psz_unicode; ret = vlc_iconv( p_this->iconvHandle, &psz_in, &i_in, &psz_out, &i_out); if( ret == (size_t)(-1) || i_in ) { msg_Warn( p_this, "Failed to convert \"%s\" to UTF-8", psz_local ); return strdup( psz_local ); } *psz_out = '\0'; return psz_unicode;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -