[vlc-devel] [PATCH 29/48] hls: Cosmetics.

Hugo Beauzée-Luyssen beauze.h at gmail.com
Mon Jan 9 16:16:38 CET 2012


---
 modules/stream_filter/httplive.c |  296 +++++++++++++++-----------------------
 1 files changed, 115 insertions(+), 181 deletions(-)

diff --git a/modules/stream_filter/httplive.c b/modules/stream_filter/httplive.c
index ac4b5dc..c40d281 100644
--- a/modules/stream_filter/httplive.c
+++ b/modules/stream_filter/httplive.c
@@ -95,7 +95,7 @@ typedef struct hls_stream_s
     vlc_mutex_t lock;
     bool        b_cache;    /* allow caching */
 
-    char        *psz_current_key_path;		/* URL path of the encrypted key */
+    char        *psz_current_key_path;        /* URL path of the encrypted key */
     uint8_t      psz_AES_IV[AES_BLOCK_SIZE];    /* IV used when decypher the block */
     bool         b_iv_loaded;
 } hls_stream_t;
@@ -221,7 +221,8 @@ static bool isHTTPLiveStreaming(stream_t *s)
 static hls_stream_t *hls_New(vlc_array_t *hls_stream, const int id, const uint64_t bw, const char *uri)
 {
     hls_stream_t *hls = (hls_stream_t *)malloc(sizeof(hls_stream_t));
-    if (hls == NULL) return NULL;
+    if ( unlikely( hls == NULL ) )
+        return NULL;
 
     hls->id = id;
     hls->bandwidth = bw;
@@ -248,39 +249,15 @@ static void hls_Free(hls_stream_t *hls)
         for (int n = 0; n < vlc_array_count(hls->segments); n++)
         {
             segment_t *segment = (segment_t *)vlc_array_item_at_index(hls->segments, n);
-            if (segment) segment_Free(segment);
+            if (segment)
+                segment_Free(segment);
         }
         vlc_array_destroy(hls->segments);
     }
-    if(hls->psz_current_key_path)
-        free(hls->psz_current_key_path);
-    if(hls->uri)
-        free(hls->uri);
+    free(hls->psz_current_key_path);
+    free(hls->uri);
 
     free(hls);
-    hls = NULL;
-}
-
-static hls_stream_t *hls_Copy(hls_stream_t *src, const bool b_cp_segments)
-{
-    assert(src);
-    assert(!b_cp_segments); /* FIXME: copying segments is not implemented */
-
-    hls_stream_t *dst = (hls_stream_t *)malloc(sizeof(hls_stream_t));
-    if (dst == NULL) return NULL;
-
-    dst->id = src->id;
-    dst->bandwidth = src->bandwidth;
-    dst->duration = src->duration;
-    dst->size = src->size;
-    dst->sequence = src->sequence;
-    dst->version = src->version;
-    dst->b_cache = src->b_cache;
-    dst->uri = strdup( src->uri );
-    if (!b_cp_segments)
-        dst->segments = vlc_array_new();
-    vlc_mutex_init(&dst->lock);
-    return dst;
 }
 
 static hls_stream_t *hls_Get(vlc_array_t *hls_stream, const int wanted)
@@ -295,7 +272,7 @@ static hls_stream_t *hls_Get(vlc_array_t *hls_stream, const int wanted)
 
 static inline hls_stream_t *hls_GetFirst(vlc_array_t *hls_stream)
 {
-    return (hls_stream_t*) hls_Get(hls_stream, 0);
+    return hls_Get(hls_stream, 0);
 }
 
 static hls_stream_t *hls_GetLast(vlc_array_t *hls_stream)
@@ -304,24 +281,7 @@ static hls_stream_t *hls_GetLast(vlc_array_t *hls_stream)
     if (count <= 0)
         return NULL;
     count--;
-    return (hls_stream_t *) hls_Get(hls_stream, count);
-}
-
-static hls_stream_t *hls_Find(vlc_array_t *hls_stream, hls_stream_t *hls_new)
-{
-    int count = vlc_array_count(hls_stream);
-    for (int n = 0; n < count; n++)
-    {
-        hls_stream_t *hls = vlc_array_item_at_index(hls_stream, n);
-        if (hls)
-        {
-            /* compare */
-            if ((hls->id == hls_new->id) &&
-                (hls->bandwidth == hls_new->bandwidth))
-                return hls;
-        }
-    }
-    return NULL;
+    return hls_Get(hls_stream, count);
 }
 
 static uint64_t hls_GetStreamSize(hls_stream_t *hls)
@@ -347,7 +307,7 @@ static uint64_t hls_GetStreamSize(hls_stream_t *hls)
 static segment_t *segment_New(hls_stream_t* hls, const int duration, const char *uri)
 {
     segment_t *segment = (segment_t *)malloc(sizeof(segment_t));
-    if (segment == NULL)
+    if ( unlikely( segment == NULL ) )
         return NULL;
 
     segment->duration = duration; /* seconds */
@@ -369,14 +329,11 @@ static void segment_Free(segment_t *segment)
 {
     vlc_mutex_destroy(&segment->lock);
 
-    if (segment->uri)
-        free(segment->uri);
-    if (segment->psz_key_path)
-        free(segment->psz_key_path);
+    free(segment->uri);
+    free(segment->psz_key_path);
     if (segment->data)
         block_Release(segment->data);
     free(segment);
-    segment = NULL;
 }
 
 static segment_t *segment_GetSegment(hls_stream_t *hls, const int wanted)
@@ -409,9 +366,10 @@ static segment_t *segment_Find(hls_stream_t *hls, const int sequence)
 
 static int ChooseSegment(stream_t *s, const int current)
 {
-    stream_sys_t *p_sys = (stream_sys_t *)s->p_sys;
+    stream_sys_t *p_sys = s->p_sys;
     hls_stream_t *hls = hls_Get(p_sys->hls_stream, current);
-    if (hls == NULL) return 0;
+    if (hls == NULL)
+        return 0;
 
     /* Choose a segment to start which is no closer than
      * 3 times the target duration from the end of the playlist.
@@ -445,7 +403,7 @@ static int ChooseSegment(stream_t *s, const int current)
         if (p_sys->b_live)
             i-- ;
         else
-          i++;
+            i++;
     }
 
     msg_Info(s, "Choose segment %d/%d (sequence=%d)", wanted, count, sequence);
@@ -455,9 +413,9 @@ static int ChooseSegment(stream_t *s, const int current)
 /* Parsing */
 static char *parse_Attributes(const char *line, const char *attr)
 {
-    char *p;
-    char *begin = (char *) line;
-    char *end = begin + strlen(line);
+    const char  *p;
+    const char  *begin = line;
+    const char  *end = begin + strlen(line);
 
     /* Find start of attributes */
     if ((p = strchr(begin, ':' )) == NULL)
@@ -512,7 +470,6 @@ static int string_to_IV(const char *string_hexa, uint8_t iv[AES_BLOCK_SIZE])
         c |= hex2int(*p++);
         *d++ = c;
     }
-
     return VLC_SUCCESS;
 }
 
@@ -521,9 +478,9 @@ static int string_to_IV(const char *string_hexa, uint8_t iv[AES_BLOCK_SIZE])
  */
 static char *relative_URI(const char *uri, const char *parent_uri)
 {
-    char *p;
-    char *parent_uri_base;
-    char *new_uri;
+    char  *p;
+    char  *parent_uri_base;
+    char  *new_uri;
 
     /* is address absolute (aka http://xxxx) ? */
     p = strstr(uri, "://");
@@ -764,7 +721,6 @@ static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
 
     if (strncasecmp(attr, "NONE", 4) == 0)
     {
-
         char *uri = parse_Attributes(p_read, "URI");
         if (uri != NULL)
         {
@@ -789,7 +745,7 @@ static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
         char *value, *uri, *iv, *key_path;
         if (s->p_sys->b_aesmsg == false)
         {
-	    msg_Info(s, "playback of AES-128 encrypted HTTP Live media detected.");
+            msg_Info(s, "playback of AES-128 encrypted HTTP Live media detected.");
             s->p_sys->b_aesmsg = true;
         }
         value = uri = parse_Attributes(p_read, "URI");
@@ -804,48 +760,47 @@ static int parse_Key(stream_t *s, hls_stream_t *hls, char *p_read)
         if (*value == '"')
         {
             /* We need to strip the "" from the attribute value */
-	    unsigned int i;
+            unsigned int i;
             uri = value + 1;
-	    for (i=0; i<strlen(uri); i++)
-	     {
-	       if (uri[i] == '"')
-		{
-		  uri[i] = 0;
-		  break;
-		}
-	     }
+            for (i=0; i<strlen(uri); i++)
+            {
+                if (uri[i] == '"')
+                {
+                    uri[i] = 0;
+                    break;
+                }
+            }
         }
         hls->psz_current_key_path = relative_URI(uri, hls->uri);
         free(value);
 
-	value = iv = parse_Attributes(p_read, "IV");
-	if (iv == NULL)
-	{
-             /*
-              * If the EXT-X-KEY tag does not have the IV attribute, implementations
-              * MUST use the sequence number of the media file as the IV when
-              * encrypting or decrypting that media file.  The big-endian binary
-              * representation of the sequence number SHALL be placed in a 16-octet
-              * buffer and padded (on the left) with zeros.
-              */
-             hls->b_iv_loaded = false;
-	}
-	else
-	{
-             /*
-              * If the EXT-X-KEY tag has the IV attribute, implementations MUST use
-              * the attribute value as the IV when encrypting or decrypting with that
-              * key.  The value MUST be interpreted as a 128-bit hexadecimal number
-              * and MUST be prefixed with 0x or 0X.
-              */
-
-             if (string_to_IV(iv, hls->psz_AES_IV) == VLC_EGENERIC)
-                 err = VLC_EGENERIC;
-             else
-                 hls->b_iv_loaded = true;
-
-             free(value);
-	}
+        value = iv = parse_Attributes(p_read, "IV");
+        if (iv == NULL)
+        {
+            /*
+            * If the EXT-X-KEY tag does not have the IV attribute, implementations
+            * MUST use the sequence number of the media file as the IV when
+            * encrypting or decrypting that media file.  The big-endian binary
+            * representation of the sequence number SHALL be placed in a 16-octet
+            * buffer and padded (on the left) with zeros.
+            */
+            hls->b_iv_loaded = false;
+        }
+        else
+        {
+            /*
+            * If the EXT-X-KEY tag has the IV attribute, implementations MUST use
+            * the attribute value as the IV when encrypting or decrypting with that
+            * key.  The value MUST be interpreted as a 128-bit hexadecimal number
+            * and MUST be prefixed with 0x or 0X.
+            */
+
+            if (string_to_IV(iv, hls->psz_AES_IV) == VLC_EGENERIC)
+                err = VLC_EGENERIC;
+            else
+                hls->b_iv_loaded = true;
+            free(value);
+        }
     }
     else
     {
@@ -1150,12 +1105,11 @@ static int hls_DownloadSegmentKey(stream_t *s, segment_t *seg)
 
 static int hls_ManageSegmentKeys(stream_t *s, hls_stream_t *hls)
 {
-    int i, count, i_err;
+    int count;
     segment_t *seg = NULL, *prev_seg;
 
     count = vlc_array_count( hls->segments );
-    i_err = VLC_SUCCESS;
-    for (i=0; i < count; i++)
+    for ( int i = 0; i < count; i++ )
     {
         prev_seg = seg;
         seg = segment_GetSegment(hls, i);
@@ -1167,13 +1121,13 @@ static int hls_ManageSegmentKeys(stream_t *s, hls_stream_t *hls)
         /* if the key has not changed, and already available from previous segment,
          * try to copy it, and don't load the key */
         if (prev_seg && prev_seg->b_key_loaded && strcmp(seg->psz_key_path, prev_seg->psz_key_path) == 0)
-       {
+        {
             memcpy(seg->psz_AES_key, prev_seg->psz_AES_key, AES_BLOCK_SIZE);
             seg->b_key_loaded = true;
             continue;
-       }
+        }
         if (hls_DownloadSegmentKey(s, seg) != VLC_SUCCESS)
-           return VLC_EGENERIC;
+            return VLC_EGENERIC;
 
        seg->b_key_loaded = true;
     }
@@ -1260,8 +1214,8 @@ static int hls_DecodeSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segm
     {
         if (segment->data->p_buffer[segment->data->i_buffer-1-count] != pad)
         {
-                msg_Err(s, "Bad ending buffer, perhaps we failed to decrypt the segment with the correct key");
-                return VLC_EGENERIC;
+            msg_Err(s, "Bad ending buffer, perhaps we failed to decrypt the segment with the correct key");
+            return VLC_EGENERIC;
         }
     }
 
@@ -1271,28 +1225,6 @@ static int hls_DecodeSegmentData(stream_t *s, hls_stream_t *hls, segment_t *segm
     return VLC_SUCCESS;
 }
 
-
-
-
-
-static int get_HTTPLiveMetaPlaylist(stream_t *s, vlc_array_t **streams)
-{
-    stream_sys_t *p_sys = s->p_sys;
-    assert(*streams);
-    int     err = VLC_SUCCESS;
-
-    /* Download new playlist file from server */
-    uint8_t *buffer = NULL;
-    ssize_t len = read_M3U8_from_url(s, p_sys->m3u8_playlist, &buffer);
-    if (len < 0)
-        return VLC_EGENERIC;
-
-    err = parse_M3U8(s, *streams, buffer, len);
-    free(buffer);
-
-    return err;
-}
-
 /* Reload playlist */
 static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t **hls)
 {
@@ -1333,11 +1265,10 @@ static int hls_UpdatePlaylist(stream_t *s, hls_stream_t *hls_new, hls_stream_t *
                 /* We must free the content, because if the key was not downloaded, content can't be decrypted */
                 if (segment->data)
                 {
-                        block_Release(segment->data);
-                        segment->data = NULL;
+                    block_Release(segment->data);
+                    segment->data = NULL;
                 }
-                if (segment->psz_key_path)
-                        free(segment->psz_key_path);
+                free(segment->psz_key_path);
                 segment->psz_key_path = p->psz_key_path?strdup(p->psz_key_path):NULL;
                 vlc_mutex_unlock(&segment->lock);
                 segment_Free(p);
@@ -1445,21 +1376,22 @@ fail:
 
 static void hls_ChooseDefaultStream(stream_sys_t *p_sys)
 {
-        uint64_t current_bw = 0;
-	unsigned int n;
-	unsigned int stream = 0;
+    uint64_t current_bw = 0;
+    unsigned int n;
+    unsigned int stream = 0;
 
-        int count = vlc_array_count(p_sys->hls_stream);
-        for (n = 0; n < count; n++)
+    int count = vlc_array_count(p_sys->hls_stream);
+    for (n = 0; n < count; n++)
+    {
+        hls_stream_t *hls = hls_Get(p_sys->hls_stream, n);
+        if (hls && hls->bandwidth > current_bw)
         {
-                hls_stream_t *hls = hls_Get(p_sys->hls_stream, n);
-                if (hls && hls->bandwidth > current_bw) {
-			stream = n;
-                        current_bw = hls->bandwidth;
-                }
+            stream = n;
+            current_bw = hls->bandwidth;
         }
-	p_sys->playback.stream = stream;
-	p_sys->download.stream = stream;
+    }
+    p_sys->playback.stream = stream;
+    p_sys->download.stream = stream;
 }
 
 
@@ -1478,7 +1410,8 @@ static int BandwidthAdaptation(stream_t *s, int progid, uint64_t *bandwidth)
     {
         /* Select best bandwidth match */
         hls_stream_t *hls = hls_Get(p_sys->hls_stream, n);
-        if (hls == NULL) break;
+        if (hls == NULL)
+            break;
 
         /* only consider streams with the same PROGRAM-ID */
         if (hls->id == progid)
@@ -1600,14 +1533,15 @@ static void* hls_Thread(void *p_this)
         vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
         vlc_mutex_unlock(&p_sys->download.lock_wait);
 
-        if (!vlc_object_alive(s)) break;
-        if (p_sys->b_quit) break;
+        if (!vlc_object_alive(s) || p_sys->b_quit)
+            break;
 
         vlc_mutex_lock(&hls->lock);
         segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
         msg_Dbg(s, "Downloading segment %d from stream %d (aka %s)", p_sys->download.segment, p_sys->download.stream, segment->uri);
         vlc_mutex_unlock(&hls->lock);
 
+
         if (segment == NULL)
         {
             if (p_sys->b_live == false)
@@ -1732,7 +1666,7 @@ again:
     for (int i = 0; i < 2; i++)
     {
         segment_t *segment = segment_GetSegment(hls, p_sys->download.segment);
-        if (segment == NULL )
+        if ( segment == NULL )
             return VLC_EGENERIC;
 
         if (segment->data)
@@ -1775,7 +1709,7 @@ static int hls_Download(stream_t *s, segment_t *segment)
     assert(segment->size > 0);
 
     segment->data = block_Alloc(segment->size);
-    if (segment->data == NULL)
+    if ( unlikely( segment->data == NULL ) )
     {
         stream_Delete(p_ts);
         return VLC_ENOMEM;
@@ -1858,7 +1792,7 @@ static ssize_t read_M3U8_from_stream(stream_t *s, uint8_t **buffer)
     return total_bytes;
 }
 
-static ssize_t read_M3U8_from_url(stream_t *s, const char *url, uint8_t **buffer)
+static ssize_t  read_M3U8_from_url(stream_t *s, const char *url, uint8_t **buffer)
 {
     assert(*buffer == NULL);
 
@@ -1915,10 +1849,10 @@ static int OpenWithPlaylist(stream_t *s, const char *m3u8_playlist)
 
     /* */
     s->p_sys = p_sys = calloc(1, sizeof(*p_sys));
-    if (p_sys == NULL)
+    if ( unlikely( p_sys == NULL ) )
         return VLC_ENOMEM;
 
-    if (m3u8_playlist == NULL)
+    if ( m3u8_playlist == NULL )
     {
         if (asprintf(&p_sys->m3u8_playlist,"%s://%s", s->psz_access, s->psz_path) < 0)
         {
@@ -1941,7 +1875,7 @@ static int OpenWithPlaylist(stream_t *s, const char *m3u8_playlist)
     p_sys->b_dont_change_bitrate = false;
 
     p_sys->hls_stream = vlc_array_new();
-    if (p_sys->hls_stream == NULL)
+    if ( unlikely( p_sys->hls_stream == NULL ) )
     {
         free(p_sys->m3u8_playlist);
         free(p_sys);
@@ -1958,9 +1892,9 @@ static int OpenWithPlaylist(stream_t *s, const char *m3u8_playlist)
     ssize_t len;
 
     if (m3u8_playlist)
-      len = read_M3U8_from_url(s, p_sys->m3u8_playlist, &buffer);
+        len = read_M3U8_from_url(s, p_sys->m3u8_playlist, &buffer);
     else
-      len = read_M3U8_from_stream(s->p_source, &buffer);
+        len = read_M3U8_from_stream(s->p_source, &buffer);
     if (len < 0)
         goto fail;
     if (parse_M3U8(s, p_sys->hls_stream, buffer, len) != VLC_SUCCESS)
@@ -1982,9 +1916,7 @@ static int OpenWithPlaylist(stream_t *s, const char *m3u8_playlist)
         goto fail;
 
     if (p_sys->b_live && (p_sys->playback.segment < 0))
-    {
         msg_Warn(s, "less data than 3 times 'target duration' available for live playback, playback may stall");
-    }
 
 #if 0
     if (Prefetch(s, &current) != VLC_SUCCESS)
@@ -2210,6 +2142,7 @@ again:
                 vlc_mutex_unlock(&p_sys->download.lock_wait);
 #endif
                 msleep(0.1 * 1000000);
+
                 if (!vlc_object_alive(s) || s->b_error || p_sys->b_quit)
                     return 0; /* eof? */
                 goto again;
@@ -2294,7 +2227,7 @@ static int Read(stream_t *s, void *buffer, unsigned int i_read)
         /* caller skips data, get big enough buffer */
         msg_Warn(s, "buffer is NULL (allocate %d)", i_read);
         buffer = calloc(1, i_read);
-        if (buffer == NULL)
+        if ( unlikely( buffer == NULL ) )
             return 0; /* NO MEMORY left*/
     }
 
@@ -2555,23 +2488,24 @@ static int segment_Seek(stream_t *s, const uint64_t pos)
                 (p_sys->download.segment - p_sys->playback.segment < 3)) &&
                 (p_sys->download.segment < (count - 6)))
         {
-             int i;
-             /* Download like Prefetch(), 3 segments in advance */
-             for (i = 0; i < 3; i++) {
-                     segment_t *segment = segment_GetSegment(hls, seek_to_segment + i);
-                     if (segment == NULL)
-                             goto end;
-                     vlc_mutex_lock(&segment->lock);
-                     if (segment->data == NULL)
-                     {
-                             /* The data is not ready, break, to sleep a little */
-                             vlc_mutex_unlock(&segment->lock);
-                             break;
-                     }
-                     vlc_mutex_unlock(&segment->lock);
-             }
-             if (i == 3)
-                     goto end;
+            int i;
+            /* Download like Prefetch(), 3 segments in advance */
+            for (i = 0; i < 3; i++)
+            {
+                segment_t *segment = segment_GetSegment(hls, seek_to_segment + i);
+                if (segment == NULL)
+                    goto end;
+                vlc_mutex_lock(&segment->lock);
+                if (segment->data == NULL)
+                {
+                    /* The data is not ready, break, to sleep a little */
+                    vlc_mutex_unlock(&segment->lock);
+                    break;
+                }
+                vlc_mutex_unlock(&segment->lock);
+            }
+            if (i == 3)
+                goto end;
 
             vlc_cond_wait(&p_sys->download.wait, &p_sys->download.lock_wait);
             if (!vlc_object_alive(s) || s->b_error || p_sys->b_quit)
-- 
1.7.8.3




More information about the vlc-devel mailing list