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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
/* GStreamer
* Copyright (C) 2011 Alessandro Decina <alessandro.d@gmail.com>
*
* 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gsthlssink.h"
#include <gst/pbutils/pbutils.h>
#include <gst/video/video.h>
#include <gio/gio.h>
#include <glib/gstdio.h>
#include <memory.h>
GST_DEBUG_CATEGORY_STATIC (gst_hls_sink_debug);
#define GST_CAT_DEFAULT gst_hls_sink_debug
#define DEFAULT_LOCATION "segment%05d.ts"
#define DEFAULT_PLAYLIST_LOCATION "playlist.m3u8"
#define DEFAULT_PLAYLIST_ROOT NULL
#define DEFAULT_MAX_FILES 10
#define DEFAULT_TARGET_DURATION 15
enum
{
PROP_0,
PROP_LOCATION,
PROP_PLAYLIST_LOCATION,
PROP_PLAYLIST_ROOT,
PROP_MAX_FILES,
PROP_TARGET_DURATION
};
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
GST_BOILERPLATE (GstHlsSink, gst_hls_sink, GstBin, GST_TYPE_BIN);
static void gst_hls_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * spec);
static void gst_hls_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * spec);
static void gst_hls_sink_handle_message (GstBin * bin, GstMessage * message);
static gboolean gst_hls_sink_ghost_event_probe (GstPad * pad,
GstEvent * event, gpointer data);
static GstStateChangeReturn
gst_hls_sink_change_state (GstElement * element, GstStateChange trans);
static void
gst_hls_sink_dispose (GObject * object)
{
GstHlsSink *sink = GST_HLS_SINK_CAST (object);
if (sink->multifilesink)
g_object_unref (sink->multifilesink);
G_OBJECT_CLASS (parent_class)->dispose ((GObject *) sink);
}
static void
gst_hls_sink_finalize (GObject * object)
{
GstHlsSink *sink = GST_HLS_SINK_CAST (object);
gst_event_replace (&sink->force_key_unit_event, NULL);
g_free (sink->location);
g_free (sink->playlist_location);
g_free (sink->playlist_root);
gst_m3u8_playlist_free (sink->playlist);
G_OBJECT_CLASS (parent_class)->finalize ((GObject *) sink);
}
static void
gst_hls_sink_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sink_template));
gst_element_class_set_metadata (element_class,
"HTTP Live Streaming sink", "Sink", "HTTP Live Streaming sink",
"Alessandro Decina <alessandro.decina@gmail.com>");
}
static void
gst_hls_sink_class_init (GstHlsSinkClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *element_class;
GstBinClass *bin_class;
gobject_class = (GObjectClass *) klass;
element_class = GST_ELEMENT_CLASS (klass);
bin_class = GST_BIN_CLASS (klass);
bin_class->handle_message = gst_hls_sink_handle_message;
element_class->change_state = GST_DEBUG_FUNCPTR (gst_hls_sink_change_state);
gobject_class->dispose = gst_hls_sink_dispose;
gobject_class->finalize = gst_hls_sink_finalize;
gobject_class->set_property = gst_hls_sink_set_property;
gobject_class->get_property = gst_hls_sink_get_property;
g_object_class_install_property (gobject_class, PROP_LOCATION,
g_param_spec_string ("location", "File Location",
"Location of the file to write", DEFAULT_LOCATION,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_PLAYLIST_LOCATION,
g_param_spec_string ("playlist-location", "Playlist Location",
"Location of the playlist to write", DEFAULT_PLAYLIST_LOCATION,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_PLAYLIST_ROOT,
g_param_spec_string ("playlist-root", "Playlist Root",
"Location of the playlist to write", DEFAULT_PLAYLIST_ROOT,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_MAX_FILES,
g_param_spec_uint ("max-files", "Max files",
"Maximum number of files to keep on disk. Once the maximum is reached,"
"old files start to be deleted to make room for new ones.",
0, G_MAXUINT, DEFAULT_MAX_FILES,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_TARGET_DURATION,
g_param_spec_uint ("target-duration", "Target duration",
"The target duration in seconds of a segment/file. "
"(0 - disabled, useful for management of segment duration by the "
"streaming server)",
0, G_MAXUINT, DEFAULT_TARGET_DURATION,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}
static void
gst_hls_sink_init (GstHlsSink * sink, GstHlsSinkClass * sink_class)
{
GstPadTemplate *templ = gst_static_pad_template_get (&sink_template);
sink->ghostpad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
gst_object_unref (templ);
gst_element_add_pad (GST_ELEMENT_CAST (sink), sink->ghostpad);
gst_pad_add_event_probe (sink->ghostpad,
G_CALLBACK (gst_hls_sink_ghost_event_probe), sink);
sink->index = 0;
sink->multifilesink = NULL;
sink->last_stream_time = 0;
sink->location = g_strdup (DEFAULT_LOCATION);
sink->playlist_location = g_strdup (DEFAULT_PLAYLIST_LOCATION);
sink->playlist_root = g_strdup (DEFAULT_PLAYLIST_ROOT);
sink->playlist = gst_m3u8_playlist_new (6, 5, FALSE);
sink->max_files = DEFAULT_MAX_FILES;
sink->target_duration = DEFAULT_TARGET_DURATION;
sink->count = 0;
sink->timeout_id = 0;
}
static gboolean
gst_hls_sink_create_elements (GstHlsSink * sink)
{
GstPad *pad = NULL;
GST_DEBUG_OBJECT (sink, "Creating internal elements");
if (sink->elements_created)
return TRUE;
sink->multifilesink = gst_element_factory_make ("multifilesink", NULL);
if (sink->multifilesink == NULL)
goto missing_element;
g_object_set (sink->multifilesink, "location", sink->location,
"next-file", 3, "post-messages", TRUE, "max-files", sink->max_files,
NULL);
gst_bin_add (GST_BIN_CAST (sink), sink->multifilesink);
pad = gst_element_get_static_pad (sink->multifilesink, "sink");
gst_ghost_pad_set_target (GST_GHOST_PAD (sink->ghostpad), pad);
gst_object_unref (pad);
sink->elements_created = TRUE;
return TRUE;
missing_element:
gst_element_post_message (GST_ELEMENT_CAST (sink),
gst_missing_element_message_new (GST_ELEMENT_CAST (sink),
"multifilesink"));
GST_ELEMENT_ERROR (sink, CORE, MISSING_PLUGIN,
(("Missing element '%s' - check your GStreamer installation."),
"multifilesink"), (NULL));
return FALSE;
}
static void
gst_hls_sink_handle_message (GstBin * bin, GstMessage * message)
{
GstHlsSink *sink = GST_HLS_SINK_CAST (bin);
switch (message->type) {
case GST_MESSAGE_ELEMENT:
{
GFile *file;
const char *filename, *title;
char *playlist_content;
GstClockTime stream_time, duration;
gboolean discont = FALSE;
GError *error = NULL;
gchar *entry_location;
if (strcmp (gst_structure_get_name (message->structure),
"GstMultiFileSink"))
break;
filename = gst_structure_get_string (message->structure, "filename");
gst_structure_get_clock_time (message->structure, "stream-time",
&stream_time);
duration = stream_time - sink->last_stream_time;
sink->last_stream_time = stream_time;
file = g_file_new_for_path (filename);
title = "ciao";
GST_INFO_OBJECT (sink, "COUNT %d", sink->index);
if (sink->playlist_root == NULL)
entry_location = g_strdup (filename);
else {
gchar *name = g_path_get_basename (filename);
entry_location = g_build_filename (sink->playlist_root, name, NULL);
g_free (name);
}
gst_m3u8_playlist_add_entry (sink->playlist, entry_location, file,
title, duration, sink->index, discont);
g_free (entry_location);
playlist_content = gst_m3u8_playlist_render (sink->playlist);
g_file_set_contents (sink->playlist_location,
playlist_content, -1, &error);
g_free (playlist_content);
break;
}
default:
break;
}
GST_BIN_CLASS (parent_class)->handle_message (bin, message);
}
static gboolean
send_force_key_unit_event (gpointer user_data)
{
GstHlsSink *sink = GST_HLS_SINK_CAST (user_data);
GstPad *sinkpad = gst_element_get_static_pad (GST_ELEMENT (sink), "sink");
/* FIXME - try to make segments >= target duration in length by setting the
* time parameter of the force key unit event to last segment cut time +
* target duration */
sink->count++;
if (!gst_pad_push_event (sinkpad,
gst_video_event_new_upstream_force_key_unit (sink->target_duration *
sink->count, TRUE, sink->count))) {
GST_WARNING_OBJECT (sink, "Failed to push upstream force key unit event");
}
gst_object_unref (sinkpad);
return TRUE;
}
static GstStateChangeReturn
gst_hls_sink_change_state (GstElement * element, GstStateChange trans)
{
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
GstHlsSink *sink = GST_HLS_SINK_CAST (element);
switch (trans) {
case GST_STATE_CHANGE_NULL_TO_READY:
if (!gst_hls_sink_create_elements (sink)) {
return GST_STATE_CHANGE_FAILURE;
}
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
if (sink->target_duration) {
sink->timeout_id =
g_timeout_add_seconds (sink->target_duration,
send_force_key_unit_event, sink);
}
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);
switch (trans) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
if (sink->timeout_id && !g_source_remove (sink->timeout_id)) {
GST_WARNING_OBJECT (sink, "Failed to remove target-duration timeout");
}
sink->timeout_id = 0;
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
/* reset the segment/file count */
sink->count = 0;
break;
case GST_STATE_CHANGE_READY_TO_NULL:
break;
default:
break;
}
return ret;
}
static void
gst_hls_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstHlsSink *sink = GST_HLS_SINK_CAST (object);
switch (prop_id) {
case PROP_LOCATION:
g_free (sink->location);
sink->location = g_value_dup_string (value);
if (sink->multifilesink)
g_object_set (sink->multifilesink, "location", sink->location, NULL);
break;
case PROP_PLAYLIST_LOCATION:
g_free (sink->playlist_location);
sink->playlist_location = g_value_dup_string (value);
break;
case PROP_PLAYLIST_ROOT:
g_free (sink->playlist_root);
sink->playlist_root = g_value_dup_string (value);
break;
case PROP_MAX_FILES:
sink->max_files = g_value_get_uint (value);
if (sink->multifilesink) {
g_object_set (sink->multifilesink, "location", sink->location,
"next-file", 3, "post-messages", TRUE, "max-files", sink->max_files,
NULL);
}
break;
case PROP_TARGET_DURATION:
sink->target_duration = g_value_get_uint (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_hls_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstHlsSink *sink = GST_HLS_SINK_CAST (object);
switch (prop_id) {
case PROP_LOCATION:
g_value_set_string (value, sink->location);
break;
case PROP_PLAYLIST_LOCATION:
g_value_set_string (value, sink->playlist_location);
break;
case PROP_PLAYLIST_ROOT:
g_value_set_string (value, sink->playlist_root);
break;
case PROP_MAX_FILES:
g_value_set_uint (value, sink->max_files);
break;
case PROP_TARGET_DURATION:
g_value_set_uint (value, sink->target_duration);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gst_hls_sink_ghost_event_probe (GstPad * pad, GstEvent * event, gpointer data)
{
GstHlsSink *sink = GST_HLS_SINK_CAST (data);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_CUSTOM_DOWNSTREAM:
{
GstClockTime timestamp;
GstClockTime running_time, stream_time;
gboolean all_headers;
guint count;
if (!gst_video_event_is_force_key_unit (event))
break;
gst_event_replace (&sink->force_key_unit_event, event);
gst_video_event_parse_downstream_force_key_unit (event,
×tamp, &stream_time, &running_time, &all_headers, &count);
GST_INFO_OBJECT (sink, "setting index %d", count);
sink->index = count;
break;
}
default:
break;
}
return TRUE;
}
gboolean
gst_hls_sink_plugin_init (GstPlugin * plugin)
{
GST_DEBUG_CATEGORY_INIT (gst_hls_sink_debug, "hlssink", 0, "HlsSink");
return gst_element_register (plugin, "hlssink", GST_RANK_NONE,
gst_hls_sink_get_type ());
}
|