Commit dacbb93b authored by JP Dinger's avatar JP Dinger

rtmp_amf_flv: Try and refactor a bit by reducing overly long

indexed accesses and packing up repetetive free() calls. Also
some cosmetics. Should not add functional changes but should make
it a bit more readable.
parent 0d96c8d2
...@@ -228,13 +228,6 @@ const uint8_t FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME = 0x30; ...@@ -228,13 +228,6 @@ const uint8_t FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME = 0x30;
/***************************************************************************** /*****************************************************************************
* static RTMP functions: * static RTMP functions:
******************************************************************************/ ******************************************************************************/
static void rtmp_handler_null ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
static void rtmp_handler_chunk_size ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
static void rtmp_handler_invoke ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
static void rtmp_handler_audio_data ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
static void rtmp_handler_video_data ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
static void rtmp_handler_notify ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
static rtmp_packet_t *rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index, uint32_t timestamp, uint8_t content_type, uint32_t src_dst, rtmp_body_t *body ); static rtmp_packet_t *rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index, uint32_t timestamp, uint8_t content_type, uint32_t src_dst, rtmp_body_t *body );
static block_t *rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length_buffer ); static block_t *rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length_buffer );
...@@ -386,6 +379,24 @@ rtmp_handshake_active( vlc_object_t *p_this, int fd ) ...@@ -386,6 +379,24 @@ rtmp_handshake_active( vlc_object_t *p_this, int fd )
return 0; return 0;
} }
static int
write_rtmp( rtmp_control_thread_t *p_thread, uint8_t *buf,
rtmp_packet_t *pkt, const char *errmsg )
{
int32_t enclen = pkt->length_encoded;
int ret = net_Write( p_thread, p_thread->fd, NULL, buf, enclen );
free( pkt->body->body );
free( pkt->body );
free( pkt );
free( buf );
if( ret != enclen )
{
msg_Err( p_thread, errmsg );
return 0;
}
return 1;
}
int int
rtmp_connect_active( rtmp_control_thread_t *p_thread ) rtmp_connect_active( rtmp_control_thread_t *p_thread )
{ {
...@@ -393,7 +404,6 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread ) ...@@ -393,7 +404,6 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread )
rtmp_body_t *rtmp_body; rtmp_body_t *rtmp_body;
uint8_t *tmp_buffer; uint8_t *tmp_buffer;
char *tmp_url; char *tmp_url;
ssize_t i_ret;
/* Build NetConnection.connect call */ /* Build NetConnection.connect call */
rtmp_body = rtmp_body_new( -1 ); rtmp_body = rtmp_body_new( -1 );
...@@ -501,20 +511,9 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread ) ...@@ -501,20 +511,9 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread )
tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet ); tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
/* Call NetConnection.connect */ /* Call NetConnection.connect */
i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded ); if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
if( i_ret != rtmp_packet->length_encoded ) "failed send call NetConnection.connect" ) )
{
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
msg_Err( p_thread, "failed send call NetConnection.connect" );
return -1; return -1;
}
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
/* Wait for NetConnection.connect result */ /* Wait for NetConnection.connect result */
vlc_mutex_lock( &p_thread->lock ); vlc_mutex_lock( &p_thread->lock );
...@@ -557,20 +556,10 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread ) ...@@ -557,20 +556,10 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread )
tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet ); tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
/* Call NetStream.createStream */ /* Call NetStream.createStream */
i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded ); if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
if( i_ret != rtmp_packet->length_encoded ) "failed send call NetStream.createStream" ) )
{
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
msg_Err( p_thread, "failed send call NetStream.createStream" );
return -1; return -1;
}
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
/*TODO: read server stream number*/ /*TODO: read server stream number*/
/* Build ping packet */ /* Build ping packet */
rtmp_body = rtmp_body_new( -1 ); rtmp_body = rtmp_body_new( -1 );
...@@ -587,20 +576,9 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread ) ...@@ -587,20 +576,9 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread )
tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet ); tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
/* Send ping packet */ /* Send ping packet */
i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded ); if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
if( i_ret != rtmp_packet->length_encoded ) "failed send ping BUFFER_TIME_CLIENT" ) )
{
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
msg_Err( p_thread, "failed send ping BUFFER_TIME_CLIENT" );
return -1; return -1;
}
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
/* Build NetStream.play call */ /* Build NetStream.play call */
rtmp_body = rtmp_body_new( -1 ); rtmp_body = rtmp_body_new( -1 );
...@@ -632,20 +610,10 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread ) ...@@ -632,20 +610,10 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread )
tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet ); tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
/* Call NetStream.play */ /* Call NetStream.play */
i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded ); if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
if( i_ret != rtmp_packet->length_encoded ) "failed send call NetStream.play" ) )
{
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
msg_Err( p_thread, "failed send call NetStream.play" );
return -1; return -1;
}
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
/* Build ping packet */ /* Build ping packet */
rtmp_body = rtmp_body_new( -1 ); rtmp_body = rtmp_body_new( -1 );
...@@ -662,20 +630,9 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread ) ...@@ -662,20 +630,9 @@ rtmp_connect_active( rtmp_control_thread_t *p_thread )
tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet ); tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
/* Send ping packet */ /* Send ping packet */
i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded ); if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
if( i_ret != rtmp_packet->length_encoded ) "failed send ping BUFFER_TIME_CLIENT" ) )
{
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
msg_Err( p_thread, "failed send ping BUFFER_TIME_CLIENT" );
return -1; return -1;
}
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
/* Wait for NetStream.play.start result */ /* Wait for NetStream.play.start result */
vlc_cond_wait( &p_thread->wait, &p_thread->lock ); vlc_cond_wait( &p_thread->wait, &p_thread->lock );
...@@ -712,6 +669,14 @@ rtmp_connect_passive( rtmp_control_thread_t *p_thread ) ...@@ -712,6 +669,14 @@ rtmp_connect_passive( rtmp_control_thread_t *p_thread )
return 0; return 0;
} }
static void
rtmp_packet_free( rtmp_packet_t *pkt )
{
free( pkt->body->body );
free( pkt->body );
free( pkt );
}
/* TODO /* TODO
int int
rtmp_seek( access_t *p_access, int64_t i_pos ) rtmp_seek( access_t *p_access, int64_t i_pos )
...@@ -754,26 +719,13 @@ msg_Warn(p_access, "i_pos %lld", i_pos); ...@@ -754,26 +719,13 @@ msg_Warn(p_access, "i_pos %lld", i_pos);
tmp_buffer = rtmp_encode_packet( p_access, rtmp_packet ); tmp_buffer = rtmp_encode_packet( p_access, rtmp_packet );
// Call NetStream.seek // // Call NetStream.seek //
i_ret = net_Write( p_access, p_sys->fd, NULL, tmp_buffer, rtmp_packet->length_encoded ); if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
if( i_ret < rtmp_packet->length_encoded ) "failed call NetStream.seek" ) )
{
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
msg_Err( p_access, "failed call NetStream.seek" );
return -1; return -1;
}
free( rtmp_packet->body->body );
free( rtmp_packet->body );
free( rtmp_packet );
free( tmp_buffer );
// Receive TODO: see what // // Receive TODO: see what //
rtmp_packet = rtmp_read_net_packet( p_access, p_sys->fd ); rtmp_packet = rtmp_read_net_packet( p_access, p_sys->fd );
free( rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( rtmp_packet->body );
free( rtmp_packet );
return 0; return 0;
} }
...@@ -933,28 +885,28 @@ rtmp_read_net_packet( rtmp_control_thread_t *p_thread ) ...@@ -933,28 +885,28 @@ rtmp_read_net_packet( rtmp_control_thread_t *p_thread )
int stream_index; int stream_index;
int bytes_left; int bytes_left;
uint8_t p_read[12]; uint8_t p_read[12];
rtmp_packet_t *rtmp_packet; rtmp_packet_t *header;
ssize_t i_ret; ssize_t i_ret;
for(;;) for(;;)
{ {
i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read, 1, true ); i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read, 1, true );
if( i_ret != 1 ) if( i_ret != 1 )
goto error; goto error;
length_header = rtmp_decode_header_size( (vlc_object_t *) p_thread, p_read[0] & RTMP_HEADER_SIZE_MASK ); length_header = rtmp_decode_header_size( (vlc_object_t *) p_thread,
p_read[0] & RTMP_HEADER_SIZE_MASK );
stream_index = p_read[0] & RTMP_HEADER_STREAM_INDEX_MASK; stream_index = p_read[0] & RTMP_HEADER_STREAM_INDEX_MASK;
header = p_thread->rtmp_headers_recv+stream_index;
i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read + 1, length_header - 1, true ); i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read + 1, length_header - 1, true );
if( i_ret != length_header - 1 ) if( i_ret != length_header - 1 )
goto error; goto error;
/* Update timestamp if not is an interchunk packet */ /* Update timestamp if not is an interchunk packet */
if( length_header == 1 && p_thread->rtmp_headers_recv[stream_index].body == NULL ) if( length_header == 1 && header->body == NULL )
{ {
p_thread->rtmp_headers_recv[stream_index].timestamp += header->timestamp += header->timestamp_relative;
p_thread->rtmp_headers_recv[stream_index].timestamp_relative;
} }
/* Length 4 and 8 headers have relative timestamp */ /* Length 4 and 8 headers have relative timestamp */
...@@ -962,17 +914,16 @@ rtmp_read_net_packet( rtmp_control_thread_t *p_thread ) ...@@ -962,17 +914,16 @@ rtmp_read_net_packet( rtmp_control_thread_t *p_thread )
{ {
p_read[0] = 0; p_read[0] = 0;
p_thread->rtmp_headers_recv[stream_index].timestamp_relative = ntoh32( *(uint32_t *) p_read ); header->timestamp_relative = ntoh32( *(uint32_t *) p_read );
p_thread->rtmp_headers_recv[stream_index].timestamp += header->timestamp += header->timestamp_relative;
p_thread->rtmp_headers_recv[stream_index].timestamp_relative;
} }
if( length_header >= 8 ) if( length_header >= 8 )
{ {
p_read[3] = 0; p_read[3] = 0;
p_thread->rtmp_headers_recv[stream_index].length_body = ntoh32( *(uint32_t *) (p_read + 3) ); header->length_body = ntoh32( *(uint32_t *) (p_read + 3) );
p_thread->rtmp_headers_recv[stream_index].content_type = p_read[7]; header->content_type = p_read[7];
} }
/* Length 12 headers have absolute timestamp */ /* Length 12 headers have absolute timestamp */
...@@ -980,47 +931,44 @@ rtmp_read_net_packet( rtmp_control_thread_t *p_thread ) ...@@ -980,47 +931,44 @@ rtmp_read_net_packet( rtmp_control_thread_t *p_thread )
{ {
p_read[0] = 0; p_read[0] = 0;
p_thread->rtmp_headers_recv[stream_index].timestamp = ntoh32( *(uint32_t *) p_read ); header->timestamp = ntoh32( *(uint32_t *) p_read );
p_thread->rtmp_headers_recv[stream_index].src_dst = ntoh32( *(uint32_t *) (p_read + 8) ); header->src_dst = ntoh32( *(uint32_t *) (p_read + 8) );
} }
if( p_thread->rtmp_headers_recv[stream_index].body == NULL ) if( header->body == NULL )
{ {
p_thread->rtmp_headers_recv[stream_index].body = header->body = rtmp_body_new( header->length_body );
rtmp_body_new( p_thread->rtmp_headers_recv[stream_index].length_body );
} }
bytes_left = p_thread->rtmp_headers_recv[stream_index].body->length_buffer - bytes_left = header->body->length_buffer - header->body->length_body;
p_thread->rtmp_headers_recv[stream_index].body->length_body;
if( bytes_left > p_thread->chunk_size_recv ) if( bytes_left > p_thread->chunk_size_recv )
bytes_left = p_thread->chunk_size_recv; bytes_left = p_thread->chunk_size_recv;
i_ret = net_Read( p_thread, p_thread->fd, NULL, i_ret = net_Read( p_thread, p_thread->fd, NULL,
p_thread->rtmp_headers_recv[stream_index].body->body + header->body->body + header->body->length_body, bytes_left, true );
p_thread->rtmp_headers_recv[stream_index].body->length_body,
bytes_left, true );
if( i_ret != bytes_left ) if( i_ret != bytes_left )
goto error; goto error;
p_thread->rtmp_headers_recv[stream_index].body->length_body += bytes_left; header->body->length_body += bytes_left;
if( p_thread->rtmp_headers_recv[stream_index].length_body == p_thread->rtmp_headers_recv[stream_index].body->length_body ) if( header->length_body == header->body->length_body )
{ {
rtmp_packet = (rtmp_packet_t *) malloc( sizeof( rtmp_packet_t ) ); rtmp_packet_t *rpkt = (rtmp_packet_t*)malloc(sizeof(rtmp_packet_t));
if( !rtmp_packet ) goto error; if( !rpkt ) goto error;
rtmp_packet->stream_index = stream_index; rpkt->stream_index = stream_index;
rtmp_packet->timestamp = p_thread->rtmp_headers_recv[stream_index].timestamp; rpkt->timestamp = header->timestamp;
rtmp_packet->timestamp_relative = p_thread->rtmp_headers_recv[stream_index].timestamp_relative; rpkt->timestamp_relative = header->timestamp_relative;
rtmp_packet->content_type = p_thread->rtmp_headers_recv[stream_index].content_type; rpkt->content_type = header->content_type;
rtmp_packet->src_dst = p_thread->rtmp_headers_recv[stream_index].src_dst; rpkt->src_dst = header->src_dst;
rtmp_packet->length_body = p_thread->rtmp_headers_recv[stream_index].length_body; rpkt->length_body = header->length_body;
rtmp_packet->body = p_thread->rtmp_headers_recv[stream_index].body; rpkt->body = header->body;
p_thread->rtmp_headers_recv[stream_index].body = NULL; header->body = NULL;
return rtmp_packet; return rpkt;
} }
} }
...@@ -1029,44 +977,21 @@ error: ...@@ -1029,44 +977,21 @@ error:
return NULL; return NULL;
} }
void
rtmp_init_handler( rtmp_handler_t *rtmp_handler )
{
rtmp_handler[RTMP_CONTENT_TYPE_CHUNK_SIZE] = rtmp_handler_chunk_size;
rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_02] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_BYTES_READ] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_PING] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_SERVER_BW] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_CLIENT_BW] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_07] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_AUDIO_DATA] = rtmp_handler_audio_data;
rtmp_handler[RTMP_CONTENT_TYPE_VIDEO_DATA] = rtmp_handler_video_data;
rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_0A_0E] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_FLEX_STREAM] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_FLEX_SHARED_OBJECT] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_MESSAGE] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_NOTIFY] = rtmp_handler_notify;
rtmp_handler[RTMP_CONTENT_TYPE_SHARED_OBJECT] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_INVOKE] = rtmp_handler_invoke;
}
static void static void
rtmp_handler_null( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ) rtmp_handler_null( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
{ {
VLC_UNUSED(p_thread); VLC_UNUSED(p_thread);
free( rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( rtmp_packet->body );
free( rtmp_packet );
} }
static void static void
rtmp_handler_chunk_size( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ) rtmp_handler_chunk_size( rtmp_control_thread_t *p_thread,
rtmp_packet_t *rtmp_packet )
{ {
p_thread->chunk_size_recv = ntoh32( *(uint32_t *) (rtmp_packet->body->body) ); p_thread->chunk_size_recv =
ntoh32( *(uint32_t *) (rtmp_packet->body->body) );
free( rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( rtmp_packet->body );
free( rtmp_packet );
} }
static void static void
...@@ -1087,9 +1012,7 @@ rtmp_handler_audio_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_pa ...@@ -1087,9 +1012,7 @@ rtmp_handler_audio_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_pa
p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body ); p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
block_FifoPut( p_thread->p_fifo_input, p_buffer ); block_FifoPut( p_thread->p_fifo_input, p_buffer );
free( rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( rtmp_packet->body );
free( rtmp_packet );
} }
static void static void
...@@ -1109,9 +1032,7 @@ rtmp_handler_video_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_pa ...@@ -1109,9 +1032,7 @@ rtmp_handler_video_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_pa
p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body ); p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
block_FifoPut( p_thread->p_fifo_input, p_buffer ); block_FifoPut( p_thread->p_fifo_input, p_buffer );
free( rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( rtmp_packet->body );
free( rtmp_packet );
} }
static void static void
...@@ -1125,9 +1046,7 @@ rtmp_handler_notify( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ...@@ -1125,9 +1046,7 @@ rtmp_handler_notify( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet
p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body ); p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
block_FifoPut( p_thread->p_fifo_input, p_buffer ); block_FifoPut( p_thread->p_fifo_input, p_buffer );
free( rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( rtmp_packet->body );
free( rtmp_packet );
} }
static void static void
...@@ -1163,9 +1082,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ...@@ -1163,9 +1082,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet
msg_Err( p_thread, "failed send connection bandwith" ); msg_Err( p_thread, "failed send connection bandwith" );
goto error; goto error;
} }
free( tmp_rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( tmp_rtmp_packet->body );
free( tmp_rtmp_packet );
free( tmp_buffer ); free( tmp_buffer );
/* Server bandwith */ /* Server bandwith */
...@@ -1179,9 +1096,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ...@@ -1179,9 +1096,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet
msg_Err( p_thread, "failed send server bandwith" ); msg_Err( p_thread, "failed send server bandwith" );
goto error; goto error;
} }
free( tmp_rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( tmp_rtmp_packet->body );
free( tmp_rtmp_packet );
free( tmp_buffer ); free( tmp_buffer );
/* Clear stream */ /* Clear stream */
...@@ -1195,9 +1110,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ...@@ -1195,9 +1110,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet
msg_Err( p_thread, "failed send clear stream" ); msg_Err( p_thread, "failed send clear stream" );
goto error; goto error;
} }
free( tmp_rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( tmp_rtmp_packet->body );
free( tmp_rtmp_packet );
free( tmp_buffer ); free( tmp_buffer );
/* Reply NetConnection.connect */ /* Reply NetConnection.connect */
...@@ -1211,9 +1124,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ...@@ -1211,9 +1124,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet
msg_Err( p_thread, "failed send reply NetConnection.connect" ); msg_Err( p_thread, "failed send reply NetConnection.connect" );
goto error; goto error;
} }
free( tmp_rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( tmp_rtmp_packet->body );
free( tmp_rtmp_packet );
free( tmp_buffer ); free( tmp_buffer );
} }
else if( strcmp( "createStream", string ) == 0 ) else if( strcmp( "createStream", string ) == 0 )
...@@ -1232,9 +1143,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ...@@ -1232,9 +1143,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet
msg_Err( p_thread, "failed send reply createStream" ); msg_Err( p_thread, "failed send reply createStream" );
goto error; goto error;
} }
free( tmp_rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( tmp_rtmp_packet->body );
free( tmp_rtmp_packet );
free( tmp_buffer ); free( tmp_buffer );
/* Reset stream */ /* Reset stream */
...@@ -1248,9 +1157,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ...@@ -1248,9 +1157,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet
msg_Err( p_thread, "failed send reset stream" ); msg_Err( p_thread, "failed send reset stream" );
goto error; goto error;
} }
free( tmp_rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( tmp_rtmp_packet->body );
free( tmp_rtmp_packet );
free( tmp_buffer ); free( tmp_buffer );
/* Clear stream */ /* Clear stream */
...@@ -1264,9 +1171,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ...@@ -1264,9 +1171,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet
msg_Err( p_thread, "failed send clear stream" ); msg_Err( p_thread, "failed send clear stream" );
goto error; goto error;
} }
free( tmp_rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( tmp_rtmp_packet->body );
free( tmp_rtmp_packet );
free( tmp_buffer ); free( tmp_buffer );
} }
else if( strcmp( "publish", string ) == 0 ) else if( strcmp( "publish", string ) == 0 )
...@@ -1302,9 +1207,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ...@@ -1302,9 +1207,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet
msg_Err( p_thread, "failed send reply NetStream.play.reset" ); msg_Err( p_thread, "failed send reply NetStream.play.reset" );
goto error; goto error;
} }
free( tmp_rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( tmp_rtmp_packet->body );
free( tmp_rtmp_packet );
free( tmp_buffer ); free( tmp_buffer );
/* Reply NetStream.play.start */ /* Reply NetStream.play.start */
...@@ -1318,9 +1221,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ...@@ -1318,9 +1221,7 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet
msg_Err( p_thread, "failed send reply NetStream.play.start" ); msg_Err( p_thread, "failed send reply NetStream.play.start" );
goto error; goto error;
} }
free( tmp_rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( tmp_rtmp_packet->body );
free( tmp_rtmp_packet );
free( tmp_buffer ); free( tmp_buffer );
free( string2 ); free( string2 );
...@@ -1445,27 +1346,25 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ...@@ -1445,27 +1346,25 @@ rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet
} }
} }
free( rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( rtmp_packet->body );
free( rtmp_packet );
return; return;
error: error:
free( string ); free( string );
free( tmp_rtmp_packet->body->body ); rtmp_packet_free( rtmp_packet );
free( tmp_rtmp_packet->body );
free( tmp_rtmp_packet );
free( tmp_buffer ); free( tmp_buffer );
} }
/* length header calculated automatically based on last packet in the same channel */ /* length header calculated automatically based on last packet in the same channel */
/* timestamps passed are always absolute */ /* timestamps passed are always absolute */
static rtmp_packet_t * static rtmp_packet_t *
rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index, uint32_t timestamp, uint8_t content_type, uint32_t src_dst, rtmp_body_t *body ) rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index,
uint32_t timestamp, uint8_t content_type,
uint32_t src_dst, rtmp_body_t *body )
{ {
int interchunk_headers; int interchunk_headers;
rtmp_packet_t *rtmp_packet; rtmp_packet_t *rtmp_packet;
rtmp_packet_t *rtmp_send = p_thread->rtmp_headers_send+stream_index;
rtmp_packet = (rtmp_packet_t *) malloc( sizeof( rtmp_packet_t ) ); rtmp_packet = (rtmp_packet_t *) malloc( sizeof( rtmp_packet_t ) );
if( !rtmp_packet ) return NULL; if( !rtmp_packet ) return NULL;
...@@ -1474,31 +1373,31 @@ rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index, uint32_t ...@@ -1474,31 +1373,31 @@ rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index, uint32_t
if( body->length_body % p_thread->chunk_size_send == 0 ) if( body->length_body % p_thread->chunk_size_send == 0 )
interchunk_headers--; interchunk_headers--;
if( src_dst != p_thread->rtmp_headers_send[stream_index].src_dst ) if( src_dst != rtmp_send->src_dst )
{ {
p_thread->rtmp_headers_send[stream_index].timestamp = timestamp; rtmp_send->timestamp = timestamp;
p_thread->rtmp_headers_send[stream_index].length_body = body->length_body; rtmp_send->length_body = body->length_body;
p_thread->rtmp_headers_send[stream_index].content_type = content_type; rtmp_send->content_type = content_type;
p_thread->rtmp_headers_send[stream_index].src_dst = src_dst; rtmp_send->src_dst = src_dst;
rtmp_packet->length_header = 12; rtmp_packet->length_header = 12;
} }
else if( content_type != p_thread->rtmp_headers_send[stream_index].content_type else if( content_type != rtmp_send->content_type
|| body->length_body != p_thread->rtmp_headers_send[stream_index].length_body ) || body->length_body != rtmp_send->length_body )
{ {
p_thread->rtmp_headers_send[stream_index].timestamp_relative = rtmp_send->timestamp_relative =
timestamp - p_thread->rtmp_headers_send[stream_index].timestamp; timestamp - rtmp_send->timestamp;
p_thread->rtmp_headers_send[stream_index].timestamp = timestamp; rtmp_send->timestamp = timestamp;
p_thread->rtmp_headers_send[stream_index].length_body = body->length_body; rtmp_send->length_body = body->length_body;
p_thread->rtmp_headers_send[stream_index].content_type = content_type; rtmp_send->content_type = content_type;
rtmp_packet->length_header = 8; rtmp_packet->length_header = 8;
} }
else if( timestamp != p_thread->rtmp_headers_send[stream_index].timestamp ) else if( timestamp != rtmp_send->timestamp )
{ {
p_thread->rtmp_headers_send[stream_index].timestamp_relative = rtmp_send->timestamp_relative =
timestamp - p_thread->rtmp_headers_send[stream_index].timestamp; timestamp - rtmp_send->timestamp;
p_thread->rtmp_headers_send[stream_index].timestamp = timestamp; rtmp_send->timestamp = timestamp;
rtmp_packet->length_header = 4; rtmp_packet->length_header = 4;
} }
...@@ -1516,9 +1415,11 @@ rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index, uint32_t ...@@ -1516,9 +1415,11 @@ rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index, uint32_t
else else
{ {
rtmp_packet->timestamp = timestamp; rtmp_packet->timestamp = timestamp;
rtmp_packet->timestamp_relative = p_thread->rtmp_headers_send[stream_index].timestamp_relative; rtmp_packet->timestamp_relative = rtmp_send->timestamp_relative;
} }
rtmp_packet->length_encoded = rtmp_packet->length_header + body->length_body + interchunk_headers;
rtmp_packet->length_encoded = rtmp_packet->length_header
+ body->length_body + interchunk_headers;
rtmp_packet->length_body = body->length_body; rtmp_packet->length_body = body->length_body;
rtmp_packet->content_type = content_type; rtmp_packet->content_type = content_type;
rtmp_packet->src_dst = src_dst; rtmp_packet->src_dst = src_dst;
...@@ -1572,8 +1473,10 @@ rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length ...@@ -1572,8 +1473,10 @@ rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length
return p_buffer; return p_buffer;
} }
/* call sequence for each packet rtmp_new_packet -> rtmp_encode_packet -> send */ /* Call sequence for each packet:
/* no parallelism allowed because of optimization in header length */ * rtmp_new_packet -> rtmp_encode_packet -> send .
* No parallelism allowed because of optimization in header length. */
uint8_t * uint8_t *
rtmp_encode_packet( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet ) rtmp_encode_packet( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
{ {
...@@ -2068,7 +1971,7 @@ rtmp_body_reset( rtmp_body_t *rtmp_body ) ...@@ -2068,7 +1971,7 @@ rtmp_body_reset( rtmp_body_t *rtmp_body )
static void static void
rtmp_body_append( rtmp_body_t *rtmp_body, uint8_t *buffer, uint32_t length ) rtmp_body_append( rtmp_body_t *rtmp_body, uint8_t *buffer, uint32_t length )
{ {
if( rtmp_body->length_body + length > rtmp_body->length_buffer ) if( (rtmp_body->length_body + length) > rtmp_body->length_buffer )
{ {
uint8_t *tmp; uint8_t *tmp;
rtmp_body->length_buffer = rtmp_body->length_body + length; rtmp_body->length_buffer = rtmp_body->length_body + length;
...@@ -2506,7 +2409,8 @@ flv_build_onMetaData( access_t *p_access, uint64_t duration, uint8_t stereo, uin ...@@ -2506,7 +2409,8 @@ flv_build_onMetaData( access_t *p_access, uint64_t duration, uint8_t stereo, uin
rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT ); rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
free( tmp_buffer ); free( tmp_buffer );
rtmp_packet = rtmp_new_packet( p_access->p_sys->p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE, rtmp_packet = rtmp_new_packet( p_access->p_sys->p_thread,
RTMP_DEFAULT_STREAM_INDEX_INVOKE,
0, RTMP_CONTENT_TYPE_NOTIFY, 0, rtmp_body ); 0, RTMP_CONTENT_TYPE_NOTIFY, 0, rtmp_body );
free( rtmp_body->body ); free( rtmp_body->body );
free( rtmp_body ); free( rtmp_body );
...@@ -2517,21 +2421,22 @@ flv_build_onMetaData( access_t *p_access, uint64_t duration, uint8_t stereo, uin ...@@ -2517,21 +2421,22 @@ flv_build_onMetaData( access_t *p_access, uint64_t duration, uint8_t stereo, uin
block_t * block_t *
flv_get_metadata( access_t *p_access ) flv_get_metadata( access_t *p_access )
{ {
access_sys_t *p_sys = p_access->p_sys; rtmp_control_thread_t *p_thread=p_access->p_sys->p_thread;
rtmp_packet_t *flv_metadata_packet; block_t *p_buf;
block_t *p_buffer;
rtmp_packet_t *p_md = flv_build_onMetaData( p_access, 0,
flv_metadata_packet = flv_build_onMetaData( p_access, 0, p_sys->p_thread->metadata_stereo, p_thread->metadata_stereo,
p_sys->p_thread->metadata_samplesize, p_sys->p_thread->metadata_samplerate, p_thread->metadata_samplesize,
p_sys->p_thread->metadata_audiocodecid, p_sys->p_thread->metadata_videocodecid ); p_thread->metadata_samplerate,
flv_rebuild( p_sys->p_thread, flv_metadata_packet ); p_thread->metadata_audiocodecid,
p_buffer = rtmp_new_block( p_sys->p_thread, flv_metadata_packet->body->body, flv_metadata_packet->body->length_buffer ); p_thread->metadata_videocodecid );
free( flv_metadata_packet->body->body ); flv_rebuild( p_thread, p_md );
free( flv_metadata_packet->body ); p_buf = rtmp_new_block( p_thread,
free( flv_metadata_packet ); p_md->body->body, p_md->body->length_buffer );
return p_buffer; rtmp_packet_free( p_md );
return p_buf;
} }
block_t * block_t *
...@@ -2561,3 +2466,24 @@ flv_insert_header( access_t *p_access, block_t *first_packet ) ...@@ -2561,3 +2466,24 @@ flv_insert_header( access_t *p_access, block_t *first_packet )
return first_packet; return first_packet;
} }
void
rtmp_init_handler( rtmp_handler_t *rtmp_handler )
{
rtmp_handler[RTMP_CONTENT_TYPE_CHUNK_SIZE] = rtmp_handler_chunk_size;
rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_02] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_BYTES_READ] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_PING] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_SERVER_BW] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_CLIENT_BW] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_07] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_AUDIO_DATA] = rtmp_handler_audio_data;
rtmp_handler[RTMP_CONTENT_TYPE_VIDEO_DATA] = rtmp_handler_video_data;
rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_0A_0E] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_FLEX_STREAM] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_FLEX_SHARED_OBJECT] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_MESSAGE] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_NOTIFY] = rtmp_handler_notify;
rtmp_handler[RTMP_CONTENT_TYPE_SHARED_OBJECT] = rtmp_handler_null;
rtmp_handler[RTMP_CONTENT_TYPE_INVOKE] = rtmp_handler_invoke;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment