[vlc-devel] [PATCH 4/4] httpd: Start new clients only at keyframes.
Steinar H. Gunderson
steinar+vlc at gunderson.no
Fri Aug 16 16:42:40 CEST 2013
When the mux is capable of marking blocks as containing keyframes
(currently only the avformat mux), start new clients only on such
frames. This makes sure they do not get anything they might not
decode at the beginning of the stream.
This patch completes WebM streaming support.
---
include/vlc_httpd.h | 2 +-
modules/access_output/http.c | 6 ++--
src/network/httpd.c | 71 +++++++++++++++++++++++++++++++++---------
3 files changed, 61 insertions(+), 18 deletions(-)
diff --git a/include/vlc_httpd.h b/include/vlc_httpd.h
index 6100dd0..de00e28 100644
--- a/include/vlc_httpd.h
+++ b/include/vlc_httpd.h
@@ -138,7 +138,7 @@ typedef struct httpd_stream_t httpd_stream_t;
VLC_API httpd_stream_t * httpd_StreamNew( httpd_host_t *, const char *psz_url, const char *psz_mime, const char *psz_user, const char *psz_password ) VLC_USED;
VLC_API void httpd_StreamDelete( httpd_stream_t * );
VLC_API int httpd_StreamHeader( httpd_stream_t *, uint8_t *p_data, int i_data );
-VLC_API int httpd_StreamSend( httpd_stream_t *, uint8_t *p_data, int i_data );
+VLC_API int httpd_StreamSend( httpd_stream_t *, uint8_t *p_data, int i_data, bool b_header, bool b_keyframe );
/* Msg functions facilities */
diff --git a/modules/access_output/http.c b/modules/access_output/http.c
index 364768c..4ca579f 100644
--- a/modules/access_output/http.c
+++ b/modules/access_output/http.c
@@ -263,8 +263,10 @@ static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
while( p_buffer )
{
block_t *p_next;
+ bool b_header_block = p_buffer->i_flags & BLOCK_FLAG_HEADER;
+ bool b_keyframe = p_buffer->i_flags & BLOCK_FLAG_TYPE_I;
- if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
+ if( b_header_block )
{
/* gather header */
if( p_sys->b_header_complete )
@@ -297,7 +299,7 @@ static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
i_len += p_buffer->i_buffer;
/* send data */
i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
- p_buffer->i_buffer );
+ p_buffer->i_buffer, b_header_block, b_keyframe );
p_next = p_buffer->p_next;
block_Release( p_buffer );
diff --git a/src/network/httpd.c b/src/network/httpd.c
index bbfbd18..383933e 100644
--- a/src/network/httpd.c
+++ b/src/network/httpd.c
@@ -66,6 +66,7 @@
#endif
static void httpd_ClientClean( httpd_client_t *cl );
+static void httpd_AppendData( httpd_stream_t *stream, uint8_t *p_data, int i_data );
/* each host run in his own thread */
struct httpd_host_t
@@ -159,6 +160,13 @@ struct httpd_client_t
int i_buffer;
uint8_t *p_buffer;
+ /*
+ * If waiting for a keyframe, this is the position (in bytes) of the
+ * last keyframe the stream saw before this client connected.
+ * Otherwise, -1.
+ */
+ int64_t i_keyframe_wait_to_pass;
+
/* */
httpd_message_t query; /* client -> httpd */
httpd_message_t answer; /* httpd -> client */
@@ -626,6 +634,14 @@ struct httpd_stream_t
uint8_t *p_header;
int i_header;
+ /* Some muxes, in particular the avformat mux, can mark given blocks
+ * as keyframes, to ensure that the stream starts with one.
+ * (This is particularly important for WebM streaming to certain
+ * browsers.) Store if we've ever seen any such keyframe blocks,
+ * and if so, the byte position of the start of the last one. */
+ bool b_has_keyframes;
+ int64_t i_last_keyframe_seen_pos;
+
/* circular buffer */
int i_buffer_size; /* buffer size, can't be reallocated smaller */
uint8_t *p_buffer; /* buffer */
@@ -659,6 +675,16 @@ static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
/* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
return VLC_EGENERIC; /* wait, no data available */
}
+ if( cl->i_keyframe_wait_to_pass >= 0 )
+ {
+ if( stream->i_last_keyframe_seen_pos <= cl->i_keyframe_wait_to_pass )
+ /* still waiting for the next keyframe */
+ return VLC_EGENERIC;
+
+ /* seek to the new keyframe */
+ answer->i_body_offset = stream->i_last_keyframe_seen_pos;
+ cl->i_keyframe_wait_to_pass = -1;
+ }
if( answer->i_body_offset + stream->i_buffer_size <
stream->i_buffer_pos )
{
@@ -717,6 +743,10 @@ static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
memcpy( answer->p_body, stream->p_header, stream->i_header );
}
answer->i_body_offset = stream->i_buffer_last_pos;
+ if( stream->b_has_keyframes )
+ cl->i_keyframe_wait_to_pass = stream->i_last_keyframe_seen_pos;
+ else
+ cl->i_keyframe_wait_to_pass = -1;
vlc_mutex_unlock( &stream->lock );
}
else
@@ -791,6 +821,8 @@ httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
* (this way i_body_offset can never be 0) */
stream->i_buffer_pos = 1;
stream->i_buffer_last_pos = 1;
+ stream->b_has_keyframes = false;
+ stream->i_last_keyframe_seen_pos = 0;
httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
(httpd_callback_sys_t*)stream );
@@ -819,22 +851,10 @@ int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
return VLC_SUCCESS;
}
-int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
+static void httpd_AppendData( httpd_stream_t *stream, uint8_t *p_data, int i_data )
{
- int i_count;
- int i_pos;
-
- if( i_data < 0 || p_data == NULL )
- {
- return VLC_SUCCESS;
- }
- vlc_mutex_lock( &stream->lock );
-
- /* save this pointer (to be used by new connection) */
- stream->i_buffer_last_pos = stream->i_buffer_pos;
-
- i_pos = stream->i_buffer_pos % stream->i_buffer_size;
- i_count = i_data;
+ int i_pos = stream->i_buffer_pos % stream->i_buffer_size;
+ int i_count = i_data;
while( i_count > 0)
{
int i_copy;
@@ -850,6 +870,26 @@ int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
}
stream->i_buffer_pos += i_data;
+}
+
+int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data, bool b_header, bool b_keyframe )
+{
+ if( i_data < 0 || p_data == NULL )
+ {
+ return VLC_SUCCESS;
+ }
+ vlc_mutex_lock( &stream->lock );
+
+ /* save this pointer (to be used by new connection) */
+ stream->i_buffer_last_pos = stream->i_buffer_pos;
+
+ if( b_keyframe )
+ {
+ stream->b_has_keyframes = true;
+ stream->i_last_keyframe_seen_pos = stream->i_buffer_pos;
+ }
+
+ httpd_AppendData( stream, p_data, i_data );
vlc_mutex_unlock( &stream->lock );
return VLC_SUCCESS;
@@ -1273,6 +1313,7 @@ static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
cl->i_buffer_size = HTTPD_CL_BUFSIZE;
cl->i_buffer = 0;
cl->p_buffer = xmalloc( cl->i_buffer_size );
+ cl->i_keyframe_wait_to_pass = -1;
cl->b_stream_mode = false;
httpd_MsgInit( &cl->query );
--
1.7.10.4
More information about the vlc-devel
mailing list