gstmultifilesink.c 25.8 KB
Newer Older
David Schleef's avatar
David Schleef committed
1
2
3
4
5
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *                    2006 Wim Taymans <wim@fluendo.com>
 *                    2006 David A. Schleef <ds@schleef.org>
6
 *                    2011 Collabora Ltd. <tim.muller@collabora.co.uk>
David Schleef's avatar
David Schleef committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *
 * gstmultifilesink.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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/**
 * SECTION:element-multifilesink
 * @see_also: #GstFileSrc
 *
29
 * Write incoming data to a series of sequentially-named files.
30
 *
Stefan Kost's avatar
Stefan Kost committed
31
 * The filename property should contain a string with a \%d placeholder that will
32
33
 * be substituted with the index for each filename.
 *
34
 * If the #GstMultiFileSink:post-messages property is #TRUE, it sends an application
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
 * message named
 * <classname>&quot;GstMultiFileSink&quot;</classname> after writing each
 * buffer.
 *
 * The message's structure contains these fields:
 * <itemizedlist>
 * <listitem>
 *   <para>
 *   #gchar *
 *   <classname>&quot;filename&quot;</classname>:
 *   the filename where the buffer was written.
 *   </para>
 * </listitem>
 * <listitem>
 *   <para>
 *   #gint
 *   <classname>&quot;index&quot;</classname>:
 *   the index of the buffer.
 *   </para>
 * </listitem>
 * <listitem>
 *   <para>
 *   #GstClockTime
 *   <classname>&quot;timestamp&quot;</classname>:
 *   the timestamp of the buffer.
 *   </para>
 * </listitem>
 * <listitem>
 *   <para>
 *   #GstClockTime
 *   <classname>&quot;stream-time&quot;</classname>:
 *   the stream time of the buffer.
 *   </para>
 * </listitem>
 * <listitem>
 *   <para>
 *   #GstClockTime
 *   <classname>&quot;running-time&quot;</classname>:
 *   the running_time of the buffer.
 *   </para>
 * </listitem>
 * <listitem>
 *   <para>
 *   #GstClockTime
 *   <classname>&quot;duration&quot;</classname>:
 *   the duration of the buffer.
 *   </para>
 * </listitem>
 * <listitem>
 *   <para>
 *   #guint64
 *   <classname>&quot;offset&quot;</classname>:
 *   the offset of the buffer that triggered the message.
 *   </para>
 * </listitem>
 * <listitem>
 *   <para>
 *   #guint64
 *   <classname>&quot;offset-end&quot;</classname>:
 *   the offset-end of the buffer that triggered the message.
 *   </para>
 * </listitem>
 * </itemizedlist>
 *
 * <refsect2>
Stefan Kost's avatar
Stefan Kost committed
100
 * <title>Example launch line</title>
101
102
 * |[
 * gst-launch audiotestsrc ! multifilesink
103
 * gst-launch videotestsrc ! multifilesink post-messages=true filename="frame%d"
104
105
106
107
 * ]|
 * </refsect2>
 *
 * Last reviewed on 2009-09-11 (0.10.17)
David Schleef's avatar
David Schleef committed
108
109
110
111
112
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
113
#include <gst/base/gstbasetransform.h>
114
#include <gst/video/video.h>
115
#include <glib/gstdio.h>
116
#include "gstmultifilesink.h"
David Schleef's avatar
David Schleef committed
117
118
119
120
121
122
123
124
125

static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

GST_DEBUG_CATEGORY_STATIC (gst_multi_file_sink_debug);
#define GST_CAT_DEFAULT gst_multi_file_sink_debug

126
127
#define DEFAULT_LOCATION "%05d"
#define DEFAULT_INDEX 0
128
#define DEFAULT_POST_MESSAGES FALSE
129
#define DEFAULT_NEXT_FILE GST_MULTI_FILE_SINK_NEXT_BUFFER
130
#define DEFAULT_MAX_FILES 0
131
#define DEFAULT_MAX_FILE_SIZE G_GUINT64_CONSTANT(2*1024*1024*1024)
132

David Schleef's avatar
David Schleef committed
133
134
enum
{
135
136
137
  PROP_0,
  PROP_LOCATION,
  PROP_INDEX,
138
  PROP_POST_MESSAGES,
139
  PROP_NEXT_FILE,
140
  PROP_MAX_FILES,
141
  PROP_MAX_FILE_SIZE,
142
  PROP_LAST
David Schleef's avatar
David Schleef committed
143
144
};

145
static void gst_multi_file_sink_finalize (GObject * object);
David Schleef's avatar
David Schleef committed
146
147
148
149
150
151

static void gst_multi_file_sink_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_multi_file_sink_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

152
static gboolean gst_multi_file_sink_stop (GstBaseSink * sink);
David Schleef's avatar
David Schleef committed
153
154
static GstFlowReturn gst_multi_file_sink_render (GstBaseSink * sink,
    GstBuffer * buffer);
155
156
static GstFlowReturn gst_multi_file_sink_render_list (GstBaseSink * sink,
    GstBufferList * buffer_list);
157
158
static gboolean gst_multi_file_sink_set_caps (GstBaseSink * sink,
    GstCaps * caps);
159
160
161
162
static gboolean gst_multi_file_sink_open_next_file (GstMultiFileSink *
    multifilesink);
static void gst_multi_file_sink_close_file (GstMultiFileSink * multifilesink,
    GstBuffer * buffer);
163
164
static void gst_multi_file_sink_ensure_max_files (GstMultiFileSink *
    multifilesink);
165
166
static gboolean gst_multi_file_sink_event (GstBaseSink * sink,
    GstEvent * event);
David Schleef's avatar
David Schleef committed
167

168
169
170
171
172
173
174
175
176
#define GST_TYPE_MULTI_FILE_SINK_NEXT (gst_multi_file_sink_next_get_type ())
static GType
gst_multi_file_sink_next_get_type (void)
{
  static GType multi_file_sync_next_type = 0;
  static const GEnumValue next_types[] = {
    {GST_MULTI_FILE_SINK_NEXT_BUFFER, "New file for each buffer", "buffer"},
    {GST_MULTI_FILE_SINK_NEXT_DISCONT, "New file after each discontinuity",
        "discont"},
177
178
    {GST_MULTI_FILE_SINK_NEXT_KEY_FRAME, "New file at each key frame "
          "(Useful for MPEG-TS segmenting)", "key-frame"},
179
180
    {GST_MULTI_FILE_SINK_NEXT_KEY_UNIT_EVENT,
        "New file after a force key unit event", "key-unit-event"},
181
182
183
    {GST_MULTI_FILE_SINK_NEXT_MAX_SIZE, "New file when the configured maximum "
          "file size would be exceeded with the next buffer or buffer list",
        "max-size"},
184
185
186
187
188
189
190
191
192
193
194
    {0, NULL, NULL}
  };

  if (!multi_file_sync_next_type) {
    multi_file_sync_next_type =
        g_enum_register_static ("GstMultiFileSinkNext", next_types);
  }

  return multi_file_sync_next_type;
}

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
195
196
#define gst_multi_file_sink_parent_class parent_class
G_DEFINE_TYPE (GstMultiFileSink, gst_multi_file_sink, GST_TYPE_BASE_SINK);
David Schleef's avatar
David Schleef committed
197
198
199
200
201

static void
gst_multi_file_sink_class_init (GstMultiFileSinkClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
202
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
David Schleef's avatar
David Schleef committed
203
204
205
206
207
  GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);

  gobject_class->set_property = gst_multi_file_sink_set_property;
  gobject_class->get_property = gst_multi_file_sink_get_property;

208
  g_object_class_install_property (gobject_class, PROP_LOCATION,
David Schleef's avatar
David Schleef committed
209
      g_param_spec_string ("location", "File Location",
210
211
          "Location of the file to write", NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212
213

  g_object_class_install_property (gobject_class, PROP_INDEX,
214
215
      g_param_spec_int ("index", "Index",
          "Index to use with location property to create file names.  The "
216
          "index is incremented by one for each buffer written.",
217
218
          0, G_MAXINT, DEFAULT_INDEX,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219
  /**
220
   * GstMultiFileSink:post-messages
221
222
223
224
225
   *
   * Post a message on the GstBus for each file.
   *
   * Since: 0.10.17
   */
226
227
228
  g_object_class_install_property (gobject_class, PROP_POST_MESSAGES,
      g_param_spec_boolean ("post-messages", "Post Messages",
          "Post a message for each file with information of the buffer",
229
          DEFAULT_POST_MESSAGES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230
231
232
233
234
235
236
237
238
239
240
  /**
   * GstMultiFileSink:next-file
   *
   * When to start a new file.
   *
   * Since: 0.10.17
   */
  g_object_class_install_property (gobject_class, PROP_NEXT_FILE,
      g_param_spec_enum ("next-file", "Next File",
          "When to start a new file",
          GST_TYPE_MULTI_FILE_SINK_NEXT, DEFAULT_NEXT_FILE,
241
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_STATIC_STRINGS));
242

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

  /**
   * GstMultiFileSink: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.
   *
   * Since: 0.10.31
   */
  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));

259
260
261
262
263
264
265
266
267
268
269
270
271
  /**
   * GstMultiFileSink:max-file-size
   *
   * Maximum file size before starting a new file in max-size mode.
   *
   * Since: 0.10.31
   */
  g_object_class_install_property (gobject_class, PROP_MAX_FILE_SIZE,
      g_param_spec_uint64 ("max-file-size", "Maximum File Size",
          "Maximum file size before starting a new file in max-size mode",
          0, G_MAXUINT64, DEFAULT_MAX_FILE_SIZE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

272
  gobject_class->finalize = gst_multi_file_sink_finalize;
David Schleef's avatar
David Schleef committed
273
274

  gstbasesink_class->get_times = NULL;
275
  gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_multi_file_sink_stop);
David Schleef's avatar
David Schleef committed
276
  gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_multi_file_sink_render);
277
278
  gstbasesink_class->render_list =
      GST_DEBUG_FUNCPTR (gst_multi_file_sink_render_list);
279
280
  gstbasesink_class->set_caps =
      GST_DEBUG_FUNCPTR (gst_multi_file_sink_set_caps);
281
  gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_multi_file_sink_event);
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
282
283
284
285
286
287
288
289
290
291

  GST_DEBUG_CATEGORY_INIT (gst_multi_file_sink_debug, "multifilesink", 0,
      "multifilesink element");

  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&sinktemplate));
  gst_element_class_set_details_simple (gstelement_class, "Multi-File Sink",
      "Sink/File",
      "Write buffers to a sequentially named set of files",
      "David Schleef <ds@schleef.org>");
David Schleef's avatar
David Schleef committed
292
293
294
}

static void
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
295
gst_multi_file_sink_init (GstMultiFileSink * multifilesink)
David Schleef's avatar
David Schleef committed
296
{
297
298
  multifilesink->filename = g_strdup (DEFAULT_LOCATION);
  multifilesink->index = DEFAULT_INDEX;
299
  multifilesink->post_messages = DEFAULT_POST_MESSAGES;
300
  multifilesink->max_files = DEFAULT_MAX_FILES;
301
  multifilesink->max_file_size = DEFAULT_MAX_FILE_SIZE;
302
303
  multifilesink->files = NULL;
  multifilesink->n_files = 0;
David Schleef's avatar
David Schleef committed
304

305
  gst_base_sink_set_sync (GST_BASE_SINK (multifilesink), FALSE);
306
307

  multifilesink->next_segment = GST_CLOCK_TIME_NONE;
308
  multifilesink->force_key_unit_count = -1;
David Schleef's avatar
David Schleef committed
309
310
311
}

static void
312
gst_multi_file_sink_finalize (GObject * object)
David Schleef's avatar
David Schleef committed
313
314
315
316
{
  GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object);

  g_free (sink->filename);
317
318
  g_slist_foreach (sink->files, (GFunc) g_free, NULL);
  g_slist_free (sink->files);
319

320
  G_OBJECT_CLASS (parent_class)->finalize (object);
David Schleef's avatar
David Schleef committed
321
322
323
324
325
326
327
}

static gboolean
gst_multi_file_sink_set_location (GstMultiFileSink * sink,
    const gchar * location)
{
  g_free (sink->filename);
328
329
  /* FIXME: validate location to have just one %d */
  sink->filename = g_strdup (location);
David Schleef's avatar
David Schleef committed
330
331
332

  return TRUE;
}
333

David Schleef's avatar
David Schleef committed
334
335
336
337
338
339
340
static void
gst_multi_file_sink_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object);

  switch (prop_id) {
341
    case PROP_LOCATION:
David Schleef's avatar
David Schleef committed
342
343
      gst_multi_file_sink_set_location (sink, g_value_get_string (value));
      break;
344
    case PROP_INDEX:
345
346
      sink->index = g_value_get_int (value);
      break;
347
348
    case PROP_POST_MESSAGES:
      sink->post_messages = g_value_get_boolean (value);
349
      break;
350
351
352
    case PROP_NEXT_FILE:
      sink->next_file = g_value_get_enum (value);
      break;
353
354
355
    case PROP_MAX_FILES:
      sink->max_files = g_value_get_uint (value);
      break;
356
357
358
    case PROP_MAX_FILE_SIZE:
      sink->max_file_size = g_value_get_uint64 (value);
      break;
David Schleef's avatar
David Schleef committed
359
360
361
362
363
364
365
366
367
368
369
370
371
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_multi_file_sink_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstMultiFileSink *sink = GST_MULTI_FILE_SINK (object);

  switch (prop_id) {
372
    case PROP_LOCATION:
David Schleef's avatar
David Schleef committed
373
374
      g_value_set_string (value, sink->filename);
      break;
375
    case PROP_INDEX:
376
377
      g_value_set_int (value, sink->index);
      break;
378
379
    case PROP_POST_MESSAGES:
      g_value_set_boolean (value, sink->post_messages);
380
      break;
381
382
383
    case PROP_NEXT_FILE:
      g_value_set_enum (value, sink->next_file);
      break;
384
385
386
    case PROP_MAX_FILES:
      g_value_set_uint (value, sink->max_files);
      break;
387
388
389
    case PROP_MAX_FILE_SIZE:
      g_value_set_uint64 (value, sink->max_file_size);
      break;
David Schleef's avatar
David Schleef committed
390
391
392
393
394
395
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

396
397
static gboolean
gst_multi_file_sink_stop (GstBaseSink * sink)
David Schleef's avatar
David Schleef committed
398
{
399
  GstMultiFileSink *multifilesink;
400
  int i;
David Schleef's avatar
David Schleef committed
401

402
  multifilesink = GST_MULTI_FILE_SINK (sink);
David Schleef's avatar
David Schleef committed
403

404
405
406
407
  if (multifilesink->file != NULL) {
    fclose (multifilesink->file);
    multifilesink->file = NULL;
  }
408

409
410
411
412
413
  if (multifilesink->streamheaders) {
    for (i = 0; i < multifilesink->n_streamheaders; i++) {
      gst_buffer_unref (multifilesink->streamheaders[i]);
    }
    g_free (multifilesink->streamheaders);
414
    multifilesink->streamheaders = NULL;
415
416
  }

417
418
  multifilesink->force_key_unit_count = -1;

419
420
  return TRUE;
}
421

422

423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
static void
gst_multi_file_sink_post_message_full (GstMultiFileSink * multifilesink,
    GstClockTime timestamp, GstClockTime duration, GstClockTime offset,
    GstClockTime offset_end, GstClockTime running_time,
    GstClockTime stream_time, const char *filename)
{
  GstStructure *s;

  if (!multifilesink->post_messages)
    return;

  s = gst_structure_new ("GstMultiFileSink",
      "filename", G_TYPE_STRING, filename,
      "index", G_TYPE_INT, multifilesink->index,
      "timestamp", G_TYPE_UINT64, timestamp,
      "stream-time", G_TYPE_UINT64, stream_time,
      "running-time", G_TYPE_UINT64, running_time,
      "duration", G_TYPE_UINT64, duration,
      "offset", G_TYPE_UINT64, offset,
      "offset-end", G_TYPE_UINT64, offset_end, NULL);

  gst_element_post_message (GST_ELEMENT_CAST (multifilesink),
      gst_message_new_element (GST_OBJECT_CAST (multifilesink), s));
}


449
450
451
452
static void
gst_multi_file_sink_post_message (GstMultiFileSink * multifilesink,
    GstBuffer * buffer, const char *filename)
{
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
  GstClockTime duration, timestamp;
  GstClockTime running_time, stream_time;
  guint64 offset, offset_end;
  GstSegment *segment;
  GstFormat format;

  if (!multifilesink->post_messages)
    return;

  segment = &GST_BASE_SINK (multifilesink)->segment;
  format = segment->format;

  timestamp = GST_BUFFER_TIMESTAMP (buffer);
  duration = GST_BUFFER_DURATION (buffer);
  offset = GST_BUFFER_OFFSET (buffer);
  offset_end = GST_BUFFER_OFFSET_END (buffer);

  running_time = gst_segment_to_running_time (segment, format, timestamp);
  stream_time = gst_segment_to_stream_time (segment, format, timestamp);

  gst_multi_file_sink_post_message_full (multifilesink, timestamp, duration,
      offset, offset_end, running_time, stream_time, filename);
475
}
David Schleef's avatar
David Schleef committed
476

477
478
479
480
481
482
483
484
485
486
487
488
489
static gboolean
gst_multi_file_sink_write_stream_headers (GstMultiFileSink * sink)
{
  int i;

  if (sink->streamheaders == NULL)
    return TRUE;

  /* we want to write these at the beginning */
  g_assert (sink->cur_file_size == 0);

  for (i = 0; i < sink->n_streamheaders; i++) {
    GstBuffer *hdr;
Wim Taymans's avatar
Wim Taymans committed
490
    GstMapInfo map;
491
492
493
    int ret;

    hdr = sink->streamheaders[i];
Wim Taymans's avatar
Wim Taymans committed
494
495
496
    gst_buffer_map (hdr, &map, GST_MAP_READ);
    ret = fwrite (map.data, map.size, 1, sink->file);
    gst_buffer_unmap (hdr, &map);
497
498
499
500

    if (ret != 1)
      return FALSE;

Wim Taymans's avatar
Wim Taymans committed
501
    sink->cur_file_size += map.size;
David Schleef's avatar
David Schleef committed
502
  }
503
504

  return TRUE;
505
}
David Schleef's avatar
David Schleef committed
506

507
508
509
510
static GstFlowReturn
gst_multi_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
{
  GstMultiFileSink *multifilesink;
Wim Taymans's avatar
Wim Taymans committed
511
  GstMapInfo map;
512
513
514
515
  gchar *filename;
  gboolean ret;
  GError *error = NULL;

Wim Taymans's avatar
Wim Taymans committed
516
  gst_buffer_map (buffer, &map, GST_MAP_READ);
517
518
519
520
521

  multifilesink = GST_MULTI_FILE_SINK (sink);

  switch (multifilesink->next_file) {
    case GST_MULTI_FILE_SINK_NEXT_BUFFER:
522
523
      gst_multi_file_sink_ensure_max_files (multifilesink);

524
525
      filename = g_strdup_printf (multifilesink->filename,
          multifilesink->index);
Wim Taymans's avatar
Wim Taymans committed
526
      ret = g_file_set_contents (filename, (char *) map.data, map.size, &error);
527
528
529
      if (!ret)
        goto write_error;

530
531
532
      multifilesink->files = g_slist_append (multifilesink->files, filename);
      multifilesink->n_files += 1;

533
534
535
536
537
538
      gst_multi_file_sink_post_message (multifilesink, buffer, filename);
      multifilesink->index++;

      break;
    case GST_MULTI_FILE_SINK_NEXT_DISCONT:
      if (GST_BUFFER_IS_DISCONT (buffer)) {
539
540
        if (multifilesink->file)
          gst_multi_file_sink_close_file (multifilesink, buffer);
541
542
543
      }

      if (multifilesink->file == NULL) {
544
        if (!gst_multi_file_sink_open_next_file (multifilesink))
545
546
547
          goto stdio_write_error;
      }

Wim Taymans's avatar
Wim Taymans committed
548
      ret = fwrite (map.data, map.size, 1, multifilesink->file);
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
      if (ret != 1)
        goto stdio_write_error;

      break;
    case GST_MULTI_FILE_SINK_NEXT_KEY_FRAME:
      if (multifilesink->next_segment == GST_CLOCK_TIME_NONE) {
        if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
          multifilesink->next_segment = GST_BUFFER_TIMESTAMP (buffer) +
              10 * GST_SECOND;
        }
      }

      if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
          GST_BUFFER_TIMESTAMP (buffer) >= multifilesink->next_segment &&
          !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
564
565
        if (multifilesink->file)
          gst_multi_file_sink_close_file (multifilesink, buffer);
566
567
568
569
570

        multifilesink->next_segment += 10 * GST_SECOND;
      }

      if (multifilesink->file == NULL) {
571
        if (!gst_multi_file_sink_open_next_file (multifilesink))
572
          goto stdio_write_error;
573

574
        gst_multi_file_sink_write_stream_headers (multifilesink);
575
576
      }

Wim Taymans's avatar
Wim Taymans committed
577
      ret = fwrite (map.data, map.size, 1, multifilesink->file);
578
579
580
581
582
583
      if (ret != 1)
        goto stdio_write_error;

      break;
    case GST_MULTI_FILE_SINK_NEXT_KEY_UNIT_EVENT:
      if (multifilesink->file == NULL) {
584
        if (!gst_multi_file_sink_open_next_file (multifilesink))
585
          goto stdio_write_error;
586
      }
587

588
589
590
      if (!gst_multi_file_sink_write_stream_headers (multifilesink))
        goto stdio_write_error;

Wim Taymans's avatar
Wim Taymans committed
591
      ret = fwrite (map.data, map.size, 1, multifilesink->file);
592

593
594
595
596
      if (ret != 1)
        goto stdio_write_error;

      break;
597
598
599
    case GST_MULTI_FILE_SINK_NEXT_MAX_SIZE:{
      guint64 new_size;

Wim Taymans's avatar
Wim Taymans committed
600
      new_size = multifilesink->cur_file_size + map.size;
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
      if (new_size > multifilesink->max_file_size) {

        GST_INFO_OBJECT (multifilesink, "current size: %" G_GUINT64_FORMAT
            ", new_size: %" G_GUINT64_FORMAT ", max. size %" G_GUINT64_FORMAT,
            multifilesink->cur_file_size, new_size,
            multifilesink->max_file_size);

        if (multifilesink->file != NULL)
          gst_multi_file_sink_close_file (multifilesink, NULL);
      }

      if (multifilesink->file == NULL) {
        if (!gst_multi_file_sink_open_next_file (multifilesink))
          goto stdio_write_error;

616
        gst_multi_file_sink_write_stream_headers (multifilesink);
617
618
      }

Wim Taymans's avatar
Wim Taymans committed
619
      ret = fwrite (map.data, map.size, 1, multifilesink->file);
620

621
622
623
      if (ret != 1)
        goto stdio_write_error;

Wim Taymans's avatar
Wim Taymans committed
624
      multifilesink->cur_file_size += map.size;
625
      break;
626
    }
627
628
629
    default:
      g_assert_not_reached ();
  }
630

Wim Taymans's avatar
Wim Taymans committed
631
  gst_buffer_unmap (buffer, &map);
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
  return GST_FLOW_OK;

  /* ERRORS */
write_error:
  {
    switch (error->code) {
      case G_FILE_ERROR_NOSPC:{
        GST_ELEMENT_ERROR (multifilesink, RESOURCE, NO_SPACE_LEFT, (NULL),
            (NULL));
        break;
      }
      default:{
        GST_ELEMENT_ERROR (multifilesink, RESOURCE, WRITE,
            ("Error while writing to file \"%s\".", filename),
            ("%s", g_strerror (errno)));
      }
    }
    g_error_free (error);
    g_free (filename);

Wim Taymans's avatar
Wim Taymans committed
652
    gst_buffer_unmap (buffer, &map);
653
654
    return GST_FLOW_ERROR;
  }
655
stdio_write_error:
656
657
658
659
660
661
662
663
  switch (errno) {
    case ENOSPC:
      GST_ELEMENT_ERROR (multifilesink, RESOURCE, NO_SPACE_LEFT,
          ("Error while writing to file."), ("%s", g_strerror (errno)));
      break;
    default:
      GST_ELEMENT_ERROR (multifilesink, RESOURCE, WRITE,
          ("Error while writing to file."), ("%s", g_strerror (errno)));
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
664
  }
Wim Taymans's avatar
Wim Taymans committed
665
  gst_buffer_unmap (buffer, &map);
666
  return GST_FLOW_ERROR;
David Schleef's avatar
David Schleef committed
667
}
668

669
670
static gboolean
buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
671
672
{
  guint *p_size = data;
673
  gsize buf_size;
674

675
676
  buf_size = gst_buffer_get_size (*buf);
  GST_TRACE ("buffer %u has size %" G_GSIZE_FORMAT, idx, buf_size);
677
678
  *p_size += buf_size;

679
  return TRUE;
680
681
}

682
683
static gboolean
buffer_list_copy_data (GstBuffer ** buf, guint idx, gpointer data)
684
685
{
  GstBuffer *dest = data;
686
687
688
689
  guint num, i;

  if (idx == 0)
    gst_buffer_copy_into (dest, *buf, GST_BUFFER_COPY_METADATA, 0, -1);
690

691
692
693
  num = gst_buffer_n_memory (*buf);
  for (i = 0; i < num; ++i) {
    GstMemory *mem;
694

Wim Taymans's avatar
Wim Taymans committed
695
696
    mem = gst_buffer_get_memory (*buf, i);
    gst_buffer_take_memory (dest, -1, mem);
697
  }
698

699
  return TRUE;
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
}

/* Our assumption for now is that the buffers in a buffer list should always
 * end up in the same file. If someone wants different behaviour, they'll just
 * have to add a property for that. */
static GstFlowReturn
gst_multi_file_sink_render_list (GstBaseSink * sink, GstBufferList * list)
{
  GstBuffer *buf;
  guint size;

  gst_buffer_list_foreach (list, buffer_list_calc_size, &size);
  GST_LOG_OBJECT (sink, "total size of buffer list %p: %u", list, size);

  /* copy all buffers in the list into one single buffer, so we can use
   * the normal render function (FIXME: optimise to avoid the memcpy) */
716
  buf = gst_buffer_new ();
717
  gst_buffer_list_foreach (list, buffer_list_copy_data, buf);
718
  g_assert (gst_buffer_get_size (buf) == size);
719
720
721
722
723
724
725

  gst_multi_file_sink_render (sink, buf);
  gst_buffer_unref (buf);

  return GST_FLOW_OK;
}

726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
static gboolean
gst_multi_file_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
{
  GstMultiFileSink *multifilesink;
  GstStructure *structure;

  multifilesink = GST_MULTI_FILE_SINK (sink);

  structure = gst_caps_get_structure (caps, 0);
  if (structure) {
    const GValue *value;

    value = gst_structure_get_value (structure, "streamheader");

    if (GST_VALUE_HOLDS_ARRAY (value)) {
      int i;

      if (multifilesink->streamheaders) {
        for (i = 0; i < multifilesink->n_streamheaders; i++) {
          gst_buffer_unref (multifilesink->streamheaders[i]);
        }
        g_free (multifilesink->streamheaders);
      }

      multifilesink->n_streamheaders = gst_value_array_get_size (value);
      multifilesink->streamheaders =
          g_malloc (sizeof (GstBuffer *) * multifilesink->n_streamheaders);

      for (i = 0; i < multifilesink->n_streamheaders; i++) {
        multifilesink->streamheaders[i] =
            gst_buffer_ref (gst_value_get_buffer (gst_value_array_get_value
                (value, i)));
      }
    }
  }

  return TRUE;
}
764

765
766
767
768
769
770
771
772
773
static void
gst_multi_file_sink_ensure_max_files (GstMultiFileSink * multifilesink)
{
  char *filename;

  while (multifilesink->max_files &&
      multifilesink->n_files >= multifilesink->max_files) {
    filename = multifilesink->files->data;
    g_remove (filename);
774
    g_free (filename);
775
776
777
778
779
780
    multifilesink->files = g_slist_delete_link (multifilesink->files,
        multifilesink->files);
    multifilesink->n_files -= 1;
  }
}

781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
static gboolean
gst_multi_file_sink_event (GstBaseSink * sink, GstEvent * event)
{
  GstMultiFileSink *multifilesink;
  gchar *filename;

  multifilesink = GST_MULTI_FILE_SINK (sink);

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_CUSTOM_DOWNSTREAM:
    {
      GstClockTime timestamp, duration;
      GstClockTime running_time, stream_time;
      guint64 offset, offset_end;
      gboolean all_headers;
      guint count;

      if (multifilesink->next_file != GST_MULTI_FILE_SINK_NEXT_KEY_UNIT_EVENT ||
          !gst_video_event_is_force_key_unit (event))
        goto out;

      gst_video_event_parse_downstream_force_key_unit (event, &timestamp,
          &stream_time, &running_time, &all_headers, &count);

      if (multifilesink->force_key_unit_count != -1 &&
          multifilesink->force_key_unit_count == count)
        goto out;

      multifilesink->force_key_unit_count = count;

      if (multifilesink->file) {
        duration = GST_CLOCK_TIME_NONE;
        offset = offset_end = -1;
        filename = g_strdup_printf (multifilesink->filename,
            multifilesink->index);
        gst_multi_file_sink_post_message_full (multifilesink, timestamp,
            duration, offset, offset_end, running_time, stream_time, filename);

        g_free (filename);

        gst_multi_file_sink_close_file (multifilesink, NULL);

      }

      if (multifilesink->file == NULL) {
        if (!gst_multi_file_sink_open_next_file (multifilesink))
          goto stdio_write_error;
      }

      break;
    }
    default:
      break;
  }

out:
837
  return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
838

839
  /* ERRORS */
840
stdio_write_error:
841
842
843
844
845
846
  {
    GST_ELEMENT_ERROR (multifilesink, RESOURCE, WRITE,
        ("Error while writing to file."), (NULL));
    gst_event_unref (event);
    return FALSE;
  }
847
848
}

849
850
851
852
853
854
855
static gboolean
gst_multi_file_sink_open_next_file (GstMultiFileSink * multifilesink)
{
  char *filename;

  g_return_val_if_fail (multifilesink->file == NULL, FALSE);

856
  gst_multi_file_sink_ensure_max_files (multifilesink);
857
858
  filename = g_strdup_printf (multifilesink->filename, multifilesink->index);
  multifilesink->file = g_fopen (filename, "wb");
859
860
  if (multifilesink->file == NULL) {
    g_free (filename);
861
    return FALSE;
862
863
  }

864
  GST_INFO_OBJECT (multifilesink, "opening file %s", filename);
865
866
  multifilesink->files = g_slist_append (multifilesink->files, filename);
  multifilesink->n_files += 1;
867

868
  multifilesink->cur_file_size = 0;
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
  return TRUE;
}

static void
gst_multi_file_sink_close_file (GstMultiFileSink * multifilesink,
    GstBuffer * buffer)
{
  char *filename;

  fclose (multifilesink->file);
  multifilesink->file = NULL;

  if (buffer) {
    filename = g_strdup_printf (multifilesink->filename, multifilesink->index);
    gst_multi_file_sink_post_message (multifilesink, buffer, filename);
    g_free (filename);
  }

  multifilesink->index++;
}