• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2011 Andoni Morales Alastruey <ylatuya@gmail.com>
3  *
4  * gstm3u8playlist.c:
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include <glib.h>
23 
24 #include "gsthls.h"
25 #include "gstm3u8playlist.h"
26 
27 #define GST_CAT_DEFAULT hls_debug
28 
29 enum
30 {
31   GST_M3U8_PLAYLIST_TYPE_EVENT,
32   GST_M3U8_PLAYLIST_TYPE_VOD,
33 };
34 
35 typedef struct _GstM3U8Entry GstM3U8Entry;
36 
37 struct _GstM3U8Entry
38 {
39   gfloat duration;
40   gchar *title;
41   gchar *url;
42   gboolean discontinuous;
43 };
44 
45 static GstM3U8Entry *
gst_m3u8_entry_new(const gchar * url,const gchar * title,gfloat duration,gboolean discontinuous)46 gst_m3u8_entry_new (const gchar * url, const gchar * title,
47     gfloat duration, gboolean discontinuous)
48 {
49   GstM3U8Entry *entry;
50 
51   g_return_val_if_fail (url != NULL, NULL);
52 
53   entry = g_new0 (GstM3U8Entry, 1);
54   entry->url = g_strdup (url);
55   entry->title = g_strdup (title);
56   entry->duration = duration;
57   entry->discontinuous = discontinuous;
58   return entry;
59 }
60 
61 static void
gst_m3u8_entry_free(GstM3U8Entry * entry)62 gst_m3u8_entry_free (GstM3U8Entry * entry)
63 {
64   g_return_if_fail (entry != NULL);
65 
66   g_free (entry->url);
67   g_free (entry->title);
68   g_free (entry);
69 }
70 
71 GstM3U8Playlist *
gst_m3u8_playlist_new(guint version,guint window_size,gboolean allow_cache)72 gst_m3u8_playlist_new (guint version, guint window_size, gboolean allow_cache)
73 {
74   GstM3U8Playlist *playlist;
75 
76   playlist = g_new0 (GstM3U8Playlist, 1);
77   playlist->version = version;
78   playlist->window_size = window_size;
79   playlist->allow_cache = allow_cache;
80   playlist->type = GST_M3U8_PLAYLIST_TYPE_EVENT;
81   playlist->end_list = FALSE;
82   playlist->entries = g_queue_new ();
83 
84   return playlist;
85 }
86 
87 void
gst_m3u8_playlist_free(GstM3U8Playlist * playlist)88 gst_m3u8_playlist_free (GstM3U8Playlist * playlist)
89 {
90   g_return_if_fail (playlist != NULL);
91 
92   g_queue_foreach (playlist->entries, (GFunc) gst_m3u8_entry_free, NULL);
93   g_queue_free (playlist->entries);
94   g_free (playlist);
95 }
96 
97 
98 gboolean
gst_m3u8_playlist_add_entry(GstM3U8Playlist * playlist,const gchar * url,const gchar * title,gfloat duration,guint index,gboolean discontinuous)99 gst_m3u8_playlist_add_entry (GstM3U8Playlist * playlist,
100     const gchar * url, const gchar * title,
101     gfloat duration, guint index, gboolean discontinuous)
102 {
103   GstM3U8Entry *entry;
104 
105   g_return_val_if_fail (playlist != NULL, FALSE);
106   g_return_val_if_fail (url != NULL, FALSE);
107 
108   if (playlist->type == GST_M3U8_PLAYLIST_TYPE_VOD)
109     return FALSE;
110 
111   entry = gst_m3u8_entry_new (url, title, duration, discontinuous);
112 
113   if (playlist->window_size > 0) {
114     /* Delete old entries from the playlist */
115     while (playlist->entries->length >= playlist->window_size) {
116       GstM3U8Entry *old_entry;
117 
118       old_entry = g_queue_pop_head (playlist->entries);
119       gst_m3u8_entry_free (old_entry);
120     }
121   }
122 
123   playlist->sequence_number = index + 1;
124   g_queue_push_tail (playlist->entries, entry);
125 
126   return TRUE;
127 }
128 
129 static guint
gst_m3u8_playlist_target_duration(GstM3U8Playlist * playlist)130 gst_m3u8_playlist_target_duration (GstM3U8Playlist * playlist)
131 {
132   guint64 target_duration = 0;
133   GList *l;
134 
135   for (l = playlist->entries->head; l != NULL; l = l->next) {
136     GstM3U8Entry *entry = l->data;
137 
138     if (entry->duration > target_duration)
139       target_duration = entry->duration;
140   }
141 
142   return (guint) ((target_duration + 500 * GST_MSECOND) / GST_SECOND);
143 }
144 
145 gchar *
gst_m3u8_playlist_render(GstM3U8Playlist * playlist)146 gst_m3u8_playlist_render (GstM3U8Playlist * playlist)
147 {
148   GString *playlist_str;
149   GList *l;
150 
151   g_return_val_if_fail (playlist != NULL, NULL);
152 
153   playlist_str = g_string_new ("#EXTM3U\n");
154 
155   g_string_append_printf (playlist_str, "#EXT-X-VERSION:%d\n",
156       playlist->version);
157 
158   g_string_append_printf (playlist_str, "#EXT-X-ALLOW-CACHE:%s\n",
159       playlist->allow_cache ? "YES" : "NO");
160 
161   g_string_append_printf (playlist_str, "#EXT-X-MEDIA-SEQUENCE:%d\n",
162       playlist->sequence_number - playlist->entries->length);
163 
164   g_string_append_printf (playlist_str, "#EXT-X-TARGETDURATION:%u\n",
165       gst_m3u8_playlist_target_duration (playlist));
166   g_string_append (playlist_str, "\n");
167 
168   /* Entries */
169   for (l = playlist->entries->head; l != NULL; l = l->next) {
170     gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
171     GstM3U8Entry *entry = l->data;
172 
173     if (entry->discontinuous)
174       g_string_append (playlist_str, "#EXT-X-DISCONTINUITY\n");
175 
176     if (playlist->version < 3) {
177       g_string_append_printf (playlist_str, "#EXTINF:%d,%s\n",
178           (gint) ((entry->duration + 500 * GST_MSECOND) / GST_SECOND),
179           entry->title ? entry->title : "");
180     } else {
181       g_string_append_printf (playlist_str, "#EXTINF:%s,%s\n",
182           g_ascii_dtostr (buf, sizeof (buf), entry->duration / GST_SECOND),
183           entry->title ? entry->title : "");
184     }
185 
186     g_string_append_printf (playlist_str, "%s\n", entry->url);
187   }
188 
189   if (playlist->end_list)
190     g_string_append (playlist_str, "#EXT-X-ENDLIST");
191 
192   return g_string_free (playlist_str, FALSE);
193 }
194