gstffmpegmux.c 14.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * 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.
 */

20
#ifdef HAVE_CONFIG_H
21
#include "config.h"
22
23
24
#endif

#include <string.h>
25
26
27
28
29
#ifdef HAVE_FFMPEG_UNINSTALLED
#include <avformat.h>
#else
#include <ffmpeg/avformat.h>
#endif
30
31

#include <gst/gst.h>
32

33
34
#include "gstffmpegcodecmap.h"

35
36
37
typedef struct _GstFFMpegMux GstFFMpegMux;

struct _GstFFMpegMux {
38
  GstElement 		element;
39
40

  /* We need to keep track of our pads, so we do so here. */
41
42
43
44
  GstPad 		*srcpad;

  AVFormatContext 	*context;
  gboolean		opened;
45

46
47
48
49
  GstPad		*sinkpads[MAX_STREAMS];
  gint			videopads, audiopads;
  GstBuffer		*bufferqueue[MAX_STREAMS];
  gboolean		eos[MAX_STREAMS];
50
51
};

52
53
54
55
56
57
58
typedef struct _GstFFMpegMuxClassParams {
  AVOutputFormat 	*in_plugin;
  GstPadTemplate	*srctempl;
  GstPadTemplate	*videosinktempl;
  GstPadTemplate	*audiosinktempl;
} GstFFMpegMuxClassParams;

59
60
61
typedef struct _GstFFMpegMuxClass GstFFMpegMuxClass;

struct _GstFFMpegMuxClass {
62
  GstElementClass	 parent_class;
63

64
65
66
67
  AVOutputFormat 	*in_plugin;
  GstPadTemplate	*srctempl;
  GstPadTemplate	*videosinktempl;
  GstPadTemplate	*audiosinktempl;
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
};

#define GST_TYPE_FFMPEGMUX \
  (gst_ffmpegdec_get_type())
#define GST_FFMPEGMUX(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGMUX,GstFFMpegMux))
#define GST_FFMPEGMUX_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGMUX,GstFFMpegMuxClass))
#define GST_IS_FFMPEGMUX(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGMUX))
#define GST_IS_FFMPEGMUX_CLASS(obj) \
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGMUX))

enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  /* FILL ME */
};

static GHashTable *global_plugins;

/* A number of functon prototypes are given so we can refer to them later. */
static void	gst_ffmpegmux_class_init	(GstFFMpegMuxClass *klass);
static void	gst_ffmpegmux_init		(GstFFMpegMux *ffmpegmux);
96
static void	gst_ffmpegmux_dispose		(GObject *object);
97

98
99
100
101
102
103
104
static GstPadLinkReturn
		gst_ffmpegmux_connect		(GstPad  *pad,
						 GstCaps *caps);
static GstPad *	gst_ffmpegmux_request_new_pad	(GstElement *element,
						 GstPadTemplate *templ,
						 const gchar *name);
static void	gst_ffmpegmux_loop		(GstElement *element);
105

106
107
static GstElementStateReturn
		gst_ffmpegmux_change_state	(GstElement *element);
108
109
110
111
112
113
114
115
116
117

static GstElementClass *parent_class = NULL;

/*static guint gst_ffmpegmux_signals[LAST_SIGNAL] = { 0 }; */

static void
gst_ffmpegmux_class_init (GstFFMpegMuxClass *klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
118
  GstFFMpegMuxClassParams *params;
119
120
121
122
123
124

  gobject_class = (GObjectClass*)klass;
  gstelement_class = (GstElementClass*)klass;

  parent_class = g_type_class_ref(GST_TYPE_ELEMENT);

125
126
  params = g_hash_table_lookup (global_plugins,
		GINT_TO_POINTER (G_OBJECT_CLASS_TYPE (gobject_class)));
127

128
129
130
131
132
133
134
135
  klass->in_plugin = params->in_plugin;
  klass->videosinktempl = params->videosinktempl;
  klass->audiosinktempl = params->audiosinktempl;
  klass->srctempl = params->srctempl;

  gstelement_class->request_new_pad = gst_ffmpegmux_request_new_pad;
  gstelement_class->change_state = gst_ffmpegmux_change_state;
  gobject_class->dispose = gst_ffmpegmux_dispose;
136
137
}

138
139
static void
gst_ffmpegmux_init(GstFFMpegMux *ffmpegmux)
140
141
142
{
  GstFFMpegMuxClass *oclass = (GstFFMpegMuxClass*)(G_OBJECT_GET_CLASS (ffmpegmux));

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  ffmpegmux->srcpad = gst_pad_new_from_template (oclass->srctempl,
						 "src");
  gst_element_add_pad (GST_ELEMENT (ffmpegmux),
		       ffmpegmux->srcpad);
  gst_element_set_loop_function (GST_ELEMENT (ffmpegmux),
				 gst_ffmpegmux_loop);

  ffmpegmux->context = g_new0 (AVFormatContext, 1);
  memset (ffmpegmux->context, 0, sizeof (AVFormatContext));
  ffmpegmux->context->oformat = oclass->in_plugin;
  ffmpegmux->context->nb_streams = 0;
  sprintf (ffmpegmux->context->filename, "gstreamer://%p",
	   ffmpegmux->srcpad);
  ffmpegmux->opened = FALSE;

  ffmpegmux->videopads = 0;
  ffmpegmux->audiopads = 0;
}
161

162
163
164
165
static void
gst_ffmpegmux_dispose (GObject *object)
{
  GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) object;
166

167
168
169
  if (ffmpegmux->opened) {
    url_fclose (&ffmpegmux->context->pb);
    ffmpegmux->opened = FALSE;
170
171
  }

172
  g_free (ffmpegmux->context);
173
174
}

175
176
177
178
static GstPad *
gst_ffmpegmux_request_new_pad (GstElement *element,
			       GstPadTemplate *templ,
			       const gchar *name)
179
{
180
  GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) element;
181
  GstFFMpegMuxClass *oclass = (GstFFMpegMuxClass*)(G_OBJECT_GET_CLASS (ffmpegmux));
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  gchar *padname;
  GstPad *pad;
  AVStream *st;
  enum CodecType type;
  gint padnum;

  g_return_val_if_fail (templ != NULL, NULL);
  g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
  g_return_val_if_fail (ffmpegmux->opened == FALSE, NULL);

  /* figure out a name that *we* like */
  if (templ == oclass->videosinktempl) {
    padname = g_strdup_printf ("video_%02d",
			       ffmpegmux->videopads++);
    type = CODEC_TYPE_VIDEO;
  } else if (templ == oclass->audiosinktempl) {
    padname = g_strdup_printf ("audio_%02d",
			       ffmpegmux->audiopads++);
    type = CODEC_TYPE_AUDIO;
  } else {
    g_warning("ffmux: unknown pad template!");
    return NULL;
204
205
  }

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  /* create pad */
  pad = gst_pad_new_from_template (templ, padname);
  padnum = ffmpegmux->context->nb_streams++;
  ffmpegmux->sinkpads[padnum] = pad;
  gst_pad_set_link_function (pad, gst_ffmpegmux_connect);
  gst_element_add_pad (element, pad);

  /* AVStream needs to be created */
  st = g_malloc (sizeof (AVStream));
  memset(st, 0, sizeof (AVStream));
  avcodec_get_context_defaults (&st->codec);
  st->codec.codec_type = type;
  st->codec.codec_id = CODEC_ID_NONE; /* this is a check afterwards */
  st->index = padnum;
  st->stream_copy = 1; /* we're not the actual encoder */
  /* we fill in other codec-related info during capsnego */
  ffmpegmux->context->streams[padnum] = st;

  /* we love debug output (c) (tm) (r) */
225
  GST_DEBUG (
226
227
228
229
230
	     "Created %s pad for ffmux_%s element",
	     padname, oclass->in_plugin->name);
  g_free (padname);

  return pad;
231
232
}

233
234
235
static GstPadLinkReturn
gst_ffmpegmux_connect (GstPad  *pad,
		       GstCaps *caps)
236
{
237
238
239
  GstFFMpegMux *ffmpegmux = (GstFFMpegMux *)(gst_pad_get_parent (pad));
  gint i;
  AVStream *st;
240

241
242
  g_return_val_if_fail (ffmpegmux->opened == FALSE,
			GST_PAD_LINK_REFUSED);
243

244
245
  if (!GST_CAPS_IS_FIXED (caps))
    return GST_PAD_LINK_DELAYED;
246

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  for (i = 0; i < ffmpegmux->context->nb_streams; i++) {
    if (pad == ffmpegmux->sinkpads[i]) {
      break;
    }
  }
  if (i == ffmpegmux->context->nb_streams) {
    g_warning ("Unknown pad given during capsnego: %p", pad);
    return GST_PAD_LINK_REFUSED;
  }
  st = ffmpegmux->context->streams[i];

  /* for the format-specific guesses, we'll go to
   * our famous codec mapper */
  for ( ; caps != NULL; caps = caps->next) {
    if (gst_ffmpeg_caps_to_codecid (caps,
262
				    &st->codec) != CODEC_ID_NONE) {
263
264
265
266
267
268
      ffmpegmux->eos[i] = FALSE;
      return GST_PAD_LINK_OK;
    }
  } 

  return GST_PAD_LINK_REFUSED;
269
270
271
}

static void
272
gst_ffmpegmux_loop (GstElement *element)
273
{
274
275
276
277
278
279
280
281
282
283
284
285
  GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) element;
  GstFFMpegMuxClass *oclass = (GstFFMpegMuxClass*)(G_OBJECT_GET_CLASS (ffmpegmux));
  gint i, bufnum;
  GstBuffer *buf;

  /* we do need all streams to have started capsnego,
   * or things will go horribly wrong */
  for (i = 0; i < ffmpegmux->context->nb_streams; i++) {
    AVStream *st = ffmpegmux->context->streams[i];

    /* check whether the pad has successfully completed capsnego */ 
    if (st->codec.codec_id == CODEC_ID_NONE) {
286
      GST_DEBUG (
287
288
289
290
291
		 "ffmux loop function called without capsnego on pad %d",
		 i);
      return;
    }
  }
292

293
294
295
296
297
  /* open "file" (gstreamer protocol to next element) */
  if (!ffmpegmux->opened) {
    if (url_fopen (&ffmpegmux->context->pb,
		   ffmpegmux->context->filename,
		   URL_WRONLY) < 0) {
298
299
300
      gst_element_gerror(element, GST_ERROR_UNKNOWN,
        g_strdup ("unconverted error, file a bug"),
        g_strdup_printf ("Failed to open stream context in ffmux"));
301
      return;
302
303
    }

304
305
    /* we're now opened */
    ffmpegmux->opened = TRUE;
306

307
308
309
    /* now open the mux format */
    oclass->in_plugin->write_header (ffmpegmux->context);
  }
310

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  /* FIXME: do we support more events? */

  /* start by filling an internal queue of buffers */
  for (i = 0; i < ffmpegmux->context->nb_streams; i++) {
    GstPad *pad = ffmpegmux->sinkpads[i];

    /* check for "pull'ability" */
    while (pad != NULL &&
           GST_PAD_IS_USABLE (pad) &&
           /*GST_PAD_IS_ACTIVE (pad) &&*/
           GST_PAD_IS_LINKED (pad) &&
           ffmpegmux->eos[i] == FALSE &&
           ffmpegmux->bufferqueue[i] == NULL) {
      /* we can pull a buffer! */
      buf = gst_pad_pull (pad);
      if (GST_IS_EVENT (buf)) {
        if (GST_EVENT_TYPE (GST_EVENT (buf)) == GST_EVENT_EOS) {
          /* flag EOS on this stream */
          ffmpegmux->eos[i] = TRUE;
          gst_data_unref (GST_DATA (buf));
        }
      } else {
        ffmpegmux->bufferqueue[i] = buf;
334
      }
335
    }
336
337
  }

338
339
340
341
342
343
344
345
  /* take the one with earliest timestamp,
   * and push it forward */
  bufnum = -1;
  for (i = 0; i < ffmpegmux->context->nb_streams; i++) {
    /* if there's no buffer, just continue */
    if (ffmpegmux->bufferqueue[i] == NULL) {
      continue;
    }
346

347
348
349
350
    /* if we have no buffer yet, just use the first one */
    if (bufnum == -1) {
      bufnum = i;
    }
351

352
353
354
355
356
357
    /* if we do have one, only use this one if it's older */
    if (GST_BUFFER_TIMESTAMP (ffmpegmux->bufferqueue[i]) <
	  GST_BUFFER_TIMESTAMP (ffmpegmux->bufferqueue[bufnum])) {
      bufnum = i;
    }
  }
358

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  /* now handle the buffer, or signal EOS if we have
   * no buffers left */
  if (bufnum >= 0) {
    /* push out current buffer */
    buf = ffmpegmux->bufferqueue[bufnum];
    ffmpegmux->bufferqueue[bufnum] = NULL;
    oclass->in_plugin->write_packet (ffmpegmux->context,
				     bufnum,
				     GST_BUFFER_DATA (buf),
				     GST_BUFFER_SIZE (buf), 0);
    gst_buffer_unref (buf);
  } else {
    /* close down */
    oclass->in_plugin->write_trailer (ffmpegmux->context);
    url_fclose (&ffmpegmux->context->pb);
    ffmpegmux->opened = FALSE;
    gst_element_set_eos (element);
376
377
378
  }
}

379
380
static GstElementStateReturn
gst_ffmpegmux_change_state (GstElement *element)
381
{
382
383
384
385
386
387
388
389
390
  GstFFMpegMux *ffmpegmux = (GstFFMpegMux *)(element);
  gint transition = GST_STATE_TRANSITION (element);

  switch (transition) {
    case GST_STATE_PAUSED_TO_READY:
      if (ffmpegmux->opened) {
        url_fclose (&ffmpegmux->context->pb);
        ffmpegmux->opened = FALSE;
      }
391
392
      break;
  }
393
394
395
396
397

  if (GST_ELEMENT_CLASS (parent_class)->change_state)
    return GST_ELEMENT_CLASS (parent_class)->change_state (element);

  return GST_STATE_SUCCESS;
398
399
}

400

401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
gboolean
gst_ffmpegmux_register (GstPlugin *plugin)
{
  GstElementFactory *factory;
  GTypeInfo typeinfo = {
    sizeof(GstFFMpegMuxClass),      
    NULL,
    NULL,
    (GClassInitFunc)gst_ffmpegmux_class_init,
    NULL,
    NULL,
    sizeof(GstFFMpegMux),
    0,
    (GInstanceInitFunc)gst_ffmpegmux_init,
  };
  GType type;
  GstElementDetails *details;
418
419
420
  AVOutputFormat *in_plugin;
  GstFFMpegMuxClassParams *params;
  AVCodec *in_codec;
421
  
422
  in_plugin = first_oformat;
423
424
425
426
427

  global_plugins = g_hash_table_new (NULL, NULL);

  while (in_plugin) {
    gchar *type_name;
428
429
    gchar *p;
    GstCaps *srccaps, *audiosinkcaps, *videosinkcaps;
430

431
432
433
    /* Try to find the caps that belongs here */
    srccaps = gst_ffmpeg_formatid_to_caps (in_plugin->name);
    if (!srccaps) {
434
435
      goto next;
    }
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
    /* This is a bit ugly, but we just take all formats
     * for the pad template. We'll get an exact match
     * when we open the stream */
    audiosinkcaps = NULL;
    videosinkcaps = NULL;
    for (in_codec = first_avcodec; in_codec != NULL;
	 in_codec = in_codec->next) {
      GstCaps *temp = gst_ffmpeg_codecid_to_caps (in_codec->id, NULL);
      if (!temp) {
        continue;
      }
      switch (in_codec->type) {
        case CODEC_TYPE_VIDEO:
          videosinkcaps = gst_caps_append (videosinkcaps, temp);
          break;
        case CODEC_TYPE_AUDIO:
          audiosinkcaps = gst_caps_append (audiosinkcaps, temp);
          break;
        default:
          gst_caps_unref (temp);
          break;
      }
    }

460
    /* construct the type */
461
462
463
464
465
466
467
468
    type_name = g_strdup_printf("ffmux_%s", in_plugin->name);

    p = type_name;

    while (*p) {
      if (*p == '.') *p = '_';
      p++;
    }
469
470
471
472
473
474
475

    /* if it's already registered, drop it */
    if (g_type_from_name(type_name)) {
      g_free(type_name);
      goto next;
    }

476
    /* create the type now */
477
478
479
    type = g_type_register_static(GST_TYPE_ELEMENT, type_name , &typeinfo, 0);

    /* construct the element details struct */
480
481
482
483
484
485
486
    details = g_new0 (GstElementDetails, 1);
    details->longname = g_strdup (in_plugin->long_name);
    details->klass = g_strdup ("Codec/Muxer");
    details->license = g_strdup ("LGPL");
    details->description = g_strdup_printf ("FFMPEG %s Muxer",
					    in_plugin->name);
    details->version = g_strdup (VERSION);
487
488
    details->author = g_strdup ("The FFMPEG crew, "
				"Wim Taymans <wim.taymans@chello.be>, "
489
490
				"Ronald Bultje <rbultje@ronald.bitfreak.net>");
    details->copyright = g_strdup ("(c) 2002-2003");
491
492
493
494
495

    /* register the plugin with gstreamer */
    factory = gst_element_factory_new(type_name,type,details);
    g_return_val_if_fail(factory != NULL, FALSE);

496
497
498
499
500
501
502
503
    /* create a cache for these properties */
    params = g_new0 (GstFFMpegMuxClassParams, 1);
    params->in_plugin = in_plugin;
    params->srctempl = gst_pad_template_new ("sink", GST_PAD_SRC,
					     GST_PAD_ALWAYS,
					     srccaps, NULL);
    gst_element_factory_add_pad_template (factory,
					  params->srctempl);
504
    params->audiosinktempl = gst_pad_template_new ("audio_%d",
505
506
507
508
509
						   GST_PAD_SINK,
						   GST_PAD_REQUEST,
						   audiosinkcaps, NULL);
    gst_element_factory_add_pad_template (factory,
					  params->audiosinktempl);
510
    params->videosinktempl = gst_pad_template_new ("video_%d",
511
512
513
514
515
						   GST_PAD_SINK,
						   GST_PAD_REQUEST,
						   videosinkcaps, NULL);
    gst_element_factory_add_pad_template (factory,
					  params->videosinktempl);
516

517
518
519
    g_hash_table_insert (global_plugins, 
		         GINT_TO_POINTER (type), 
			 (gpointer) params);
520
521
522
523
524
525
526
527
528
529

    /* The very last thing is to register the elementfactory with the plugin. */
    gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));

next:
    in_plugin = in_plugin->next;
  }

  return TRUE;
}