summaryrefslogtreecommitdiff
path: root/gst/hls/gstm3u8playlist.c
blob: 1f9192066fc4f3ec796475e148f52316481fa10c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* GStreamer
 * Copyright (C) 2011 Andoni Morales Alastruey <ylatuya@gmail.com>
 *
 * gstm3u8playlist.c:
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <glib.h>

#include "gstfragmented.h"
#include "gstm3u8playlist.h"

#define GST_CAT_DEFAULT fragmented_debug

#define M3U8_HEADER_TAG "#EXTM3U\n"
#define M3U8_VERSION_TAG "#EXT-X-VERSION:%d\n"
#define M3U8_TARGETDURATION_TAG "#EXT-X-TARGETDURATION:%d\n"
#define M3U8_MEDIA_SEQUENCE_TAG "#EXT-X-MEDIA-SEQUENCE:%d\n"
#define M3U8_DISCONTINUITY_TAG "#EXT-X-DISCONTINUITY\n"
#define M3U8_INT_INF_TAG "#EXTINF:%d,%s\n%s\n"
#define M3U8_FLOAT_INF_TAG "#EXTINF:%.2f,%s\n%s\n"
#define M3U8_ENDLIST_TAG "#EXT-X-ENDLIST"

enum
{
  GST_M3U8_PLAYLIST_TYPE_EVENT,
  GST_M3U8_PLAYLIST_TYPE_VOD,
};

static GstM3U8Entry *
gst_m3u8_entry_new (const gchar * url, GFile * file, const gchar * title,
    gfloat duration, gboolean discontinuous)
{
  GstM3U8Entry *entry;

  g_return_val_if_fail (url != NULL, NULL);
  g_return_val_if_fail (title != NULL, NULL);

  entry = g_new0 (GstM3U8Entry, 1);
  entry->url = g_strdup (url);
  entry->title = g_strdup (title);
  entry->duration = duration;
  entry->file = file;
  entry->discontinuous = discontinuous;
  return entry;
}

static void
gst_m3u8_entry_free (GstM3U8Entry * entry)
{
  g_return_if_fail (entry != NULL);

  g_free (entry->url);
  g_free (entry->title);
  if (entry->file != NULL)
    g_object_unref (entry->file);
  g_free (entry);
}

static gchar *
gst_m3u8_entry_render (GstM3U8Entry * entry, guint version)
{
  g_return_val_if_fail (entry != NULL, NULL);

  /* FIXME: Ensure the radix is always a '.' and not a ',' when printing
   * floating point number, but for now only use integers*/
  /* if (version < 3) */
  if (TRUE)
    return g_strdup_printf ("%s" M3U8_INT_INF_TAG,
        entry->discontinuous ? M3U8_DISCONTINUITY_TAG : "",
        (gint) (entry->duration / GST_SECOND), entry->title, entry->url);

  return g_strdup_printf ("%s" M3U8_FLOAT_INF_TAG,
      entry->discontinuous ? M3U8_DISCONTINUITY_TAG : "",
      (entry->duration / GST_SECOND), entry->title, entry->url);
}

GstM3U8Playlist *
gst_m3u8_playlist_new (guint version, guint window_size, gboolean allow_cache)
{
  GstM3U8Playlist *playlist;

  playlist = g_new0 (GstM3U8Playlist, 1);
  playlist->version = version;
  playlist->window_size = window_size;
  playlist->allow_cache = allow_cache;
  playlist->type = GST_M3U8_PLAYLIST_TYPE_EVENT;
  playlist->end_list = FALSE;
  playlist->entries = g_queue_new ();

  return playlist;
}

void
gst_m3u8_playlist_free (GstM3U8Playlist * playlist)
{
  g_return_if_fail (playlist != NULL);

  g_queue_foreach (playlist->entries, (GFunc) gst_m3u8_entry_free, NULL);
  g_queue_free (playlist->entries);
  g_free (playlist);
}


GList *
gst_m3u8_playlist_add_entry (GstM3U8Playlist * playlist,
    const gchar * url, GFile * file, const gchar * title,
    gfloat duration, guint index, gboolean discontinuous)
{
  GstM3U8Entry *entry;
  GList *old_files = NULL;

  g_return_val_if_fail (playlist != NULL, FALSE);
  g_return_val_if_fail (url != NULL, FALSE);
  g_return_val_if_fail (title != NULL, FALSE);

  if (playlist->type == GST_M3U8_PLAYLIST_TYPE_VOD)
    return FALSE;

  entry = gst_m3u8_entry_new (url, file, title, duration, discontinuous);

  if (playlist->window_size != -1) {
    /* Delete old entries from the playlist */
    while (playlist->entries->length >= playlist->window_size) {
      GstM3U8Entry *old_entry;

      old_entry = g_queue_pop_head (playlist->entries);
      g_object_ref (old_entry->file);
      old_files = g_list_prepend (old_files, old_entry->file);
      gst_m3u8_entry_free (old_entry);
    }
  }

  playlist->sequence_number = index + 1;
  g_queue_push_tail (playlist->entries, entry);

  return old_files;
}

static guint
gst_m3u8_playlist_target_duration (GstM3U8Playlist * playlist)
{
  gint i;
  GstM3U8Entry *entry;
  guint64 target_duration = 0;

  for (i = 0; i < playlist->entries->length; i++) {
    entry = (GstM3U8Entry *) g_queue_peek_nth (playlist->entries, i);
    if (entry->duration > target_duration)
      target_duration = entry->duration;
  }

  return (guint) (target_duration / GST_SECOND);
}

static void
render_entry (GstM3U8Entry * entry, GstM3U8Playlist * playlist)
{
  gchar *entry_str;

  entry_str = gst_m3u8_entry_render (entry, playlist->version);
  g_string_append_printf (playlist->playlist_str, "%s", entry_str);
  g_free (entry_str);
}

gchar *
gst_m3u8_playlist_render (GstM3U8Playlist * playlist)
{
  gchar *pl;

  g_return_val_if_fail (playlist != NULL, NULL);

  playlist->playlist_str = g_string_new ("");

  /* #EXTM3U */
  g_string_append_printf (playlist->playlist_str, M3U8_HEADER_TAG);
  /* #EXT-X-VERSION */
//  g_string_append_printf (playlist->playlist_str, M3U8_VERSION_TAG,
//      playlist->version);
  /* #EXT-X-MEDIA-SEQUENCE */
  g_string_append_printf (playlist->playlist_str, M3U8_MEDIA_SEQUENCE_TAG,
      playlist->sequence_number - playlist->entries->length);
  /* #EXT-X-TARGETDURATION */
  g_string_append_printf (playlist->playlist_str, M3U8_TARGETDURATION_TAG,
      gst_m3u8_playlist_target_duration (playlist));
  g_string_append_printf (playlist->playlist_str, "\n");

  /* Entries */
  g_queue_foreach (playlist->entries, (GFunc) render_entry, playlist);

  if (playlist->end_list)
    g_string_append_printf (playlist->playlist_str, M3U8_ENDLIST_TAG);

  pl = playlist->playlist_str->str;
  g_string_free (playlist->playlist_str, FALSE);
  return pl;
}

void
gst_m3u8_playlist_clear (GstM3U8Playlist * playlist)
{
  g_return_if_fail (playlist != NULL);

  g_queue_foreach (playlist->entries, (GFunc) gst_m3u8_entry_free, NULL);
  g_queue_clear (playlist->entries);
}

guint
gst_m3u8_playlist_n_entries (GstM3U8Playlist * playlist)
{
  g_return_val_if_fail (playlist != NULL, 0);

  return playlist->entries->length;
}