• 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 "gstm3u8playlist.h"
25 #include "gsthlselements.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)72 gst_m3u8_playlist_new (guint version, guint window_size)
73 {
74   GstM3U8Playlist *playlist;
75 
76   playlist = g_new0 (GstM3U8Playlist, 1);
77   playlist->version = version;
78   playlist->window_size = window_size;
79   playlist->type = GST_M3U8_PLAYLIST_TYPE_EVENT;
80   playlist->end_list = FALSE;
81   playlist->entries = g_queue_new ();
82 
83   return playlist;
84 }
85 
86 void
gst_m3u8_playlist_free(GstM3U8Playlist * playlist)87 gst_m3u8_playlist_free (GstM3U8Playlist * playlist)
88 {
89   g_return_if_fail (playlist != NULL);
90 
91   g_queue_foreach (playlist->entries, (GFunc) gst_m3u8_entry_free, NULL);
92   g_queue_free (playlist->entries);
93   g_free (playlist);
94 }
95 
96 
97 gboolean
gst_m3u8_playlist_add_entry(GstM3U8Playlist * playlist,const gchar * url,const gchar * title,gfloat duration,guint index,gboolean discontinuous)98 gst_m3u8_playlist_add_entry (GstM3U8Playlist * playlist,
99     const gchar * url, const gchar * title,
100     gfloat duration, guint index, gboolean discontinuous)
101 {
102   GstM3U8Entry *entry;
103 
104   g_return_val_if_fail (playlist != NULL, FALSE);
105   g_return_val_if_fail (url != NULL, FALSE);
106 
107   if (playlist->type == GST_M3U8_PLAYLIST_TYPE_VOD)
108     return FALSE;
109 
110   entry = gst_m3u8_entry_new (url, title, duration, discontinuous);
111 
112   if (playlist->window_size > 0) {
113     /* Delete old entries from the playlist */
114     while (playlist->entries->length >= playlist->window_size) {
115       GstM3U8Entry *old_entry;
116 
117       old_entry = g_queue_pop_head (playlist->entries);
118       gst_m3u8_entry_free (old_entry);
119     }
120   }
121 
122   playlist->sequence_number = index + 1;
123   g_queue_push_tail (playlist->entries, entry);
124 
125   return TRUE;
126 }
127 
128 static guint
gst_m3u8_playlist_target_duration(GstM3U8Playlist * playlist)129 gst_m3u8_playlist_target_duration (GstM3U8Playlist * playlist)
130 {
131   guint64 target_duration = 0;
132   GList *l;
133 
134   for (l = playlist->entries->head; l != NULL; l = l->next) {
135     GstM3U8Entry *entry = l->data;
136 
137     if (entry->duration > target_duration)
138       target_duration = entry->duration;
139   }
140 
141   return (guint) ((target_duration + 500 * GST_MSECOND) / GST_SECOND);
142 }
143 
144 gchar *
gst_m3u8_playlist_render(GstM3U8Playlist * playlist)145 gst_m3u8_playlist_render (GstM3U8Playlist * playlist)
146 {
147   GString *playlist_str;
148   GList *l;
149 
150   g_return_val_if_fail (playlist != NULL, NULL);
151 
152   playlist_str = g_string_new ("#EXTM3U\n");
153 
154   g_string_append_printf (playlist_str, "#EXT-X-VERSION:%d\n",
155       playlist->version);
156 
157   g_string_append_printf (playlist_str, "#EXT-X-MEDIA-SEQUENCE:%d\n",
158       playlist->sequence_number - playlist->entries->length);
159 
160   g_string_append_printf (playlist_str, "#EXT-X-TARGETDURATION:%u\n",
161       gst_m3u8_playlist_target_duration (playlist));
162   g_string_append (playlist_str, "\n");
163 
164   /* Entries */
165   for (l = playlist->entries->head; l != NULL; l = l->next) {
166     gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
167     GstM3U8Entry *entry = l->data;
168 
169     if (entry->discontinuous)
170       g_string_append (playlist_str, "#EXT-X-DISCONTINUITY\n");
171 
172     if (playlist->version < 3) {
173       g_string_append_printf (playlist_str, "#EXTINF:%d,%s\n",
174           (gint) ((entry->duration + 500 * GST_MSECOND) / GST_SECOND),
175           entry->title ? entry->title : "");
176     } else {
177       g_string_append_printf (playlist_str, "#EXTINF:%s,%s\n",
178           g_ascii_dtostr (buf, sizeof (buf), entry->duration / GST_SECOND),
179           entry->title ? entry->title : "");
180     }
181 
182     g_string_append_printf (playlist_str, "%s\n", entry->url);
183   }
184 
185   if (playlist->end_list)
186     g_string_append (playlist_str, "#EXT-X-ENDLIST");
187 
188   return g_string_free (playlist_str, FALSE);
189 }
190