927d986c205bb3f0cb21d9ffceb00c30286a1e8c
[WebKit-https.git] / Source / WebCore / platform / mediastream / gstreamer / GStreamerMediaStreamSource.cpp
1 /*
2  * Copyright (C) 2018 Metrological Group B.V.
3  * Author: Thibault Saunier <tsaunier@igalia.com>
4  * Author: Alejandro G. Castro <alex@igalia.com>
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 License
17  * aint with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23
24 #if ENABLE(VIDEO) && ENABLE(MEDIA_STREAM) && USE(LIBWEBRTC) && USE(GSTREAMER)
25 #include "GStreamerMediaStreamSource.h"
26
27 #include "AudioTrackPrivate.h"
28 #include "GStreamerAudioData.h"
29 #include "GStreamerCommon.h"
30 #include "GStreamerVideoCaptureSource.h"
31 #include "MediaSampleGStreamer.h"
32 #include "VideoTrackPrivate.h"
33
34 #include <gst/app/gstappsrc.h>
35 #include <gst/base/gstflowcombiner.h>
36
37 #if GST_CHECK_VERSION(1, 10, 0)
38
39 namespace WebCore {
40
41 static void webkitMediaStreamSrcPushVideoSample(WebKitMediaStreamSrc* self, GstSample* gstsample);
42 static void webkitMediaStreamSrcPushAudioSample(WebKitMediaStreamSrc* self, GstSample* gstsample);
43 static void webkitMediaStreamSrcTrackEnded(WebKitMediaStreamSrc* self, MediaStreamTrackPrivate&);
44 static void webkitMediaStreamSrcRemoveTrackByType(WebKitMediaStreamSrc* self, RealtimeMediaSource::Type trackType);
45
46 static GstStaticPadTemplate videoSrcTemplate = GST_STATIC_PAD_TEMPLATE("video_src",
47     GST_PAD_SRC,
48     GST_PAD_SOMETIMES,
49     GST_STATIC_CAPS("video/x-raw;video/x-h264;video/x-vp8"));
50
51 static GstStaticPadTemplate audioSrcTemplate = GST_STATIC_PAD_TEMPLATE("audio_src",
52     GST_PAD_SRC,
53     GST_PAD_SOMETIMES,
54     GST_STATIC_CAPS("audio/x-raw(ANY);"));
55
56 static GstTagList* mediaStreamTrackPrivateGetTags(MediaStreamTrackPrivate* track)
57 {
58     auto taglist = gst_tag_list_new_empty();
59
60     if (!track->label().isEmpty()) {
61         gst_tag_list_add(taglist, GST_TAG_MERGE_APPEND,
62             GST_TAG_TITLE, track->label().utf8().data(), nullptr);
63     }
64
65     if (track->type() == RealtimeMediaSource::Type::Audio) {
66         gst_tag_list_add(taglist, GST_TAG_MERGE_APPEND, WEBKIT_MEDIA_TRACK_TAG_KIND,
67             static_cast<int>(AudioTrackPrivate::Kind::Main), nullptr);
68     } else if (track->type() == RealtimeMediaSource::Type::Video) {
69         gst_tag_list_add(taglist, GST_TAG_MERGE_APPEND, WEBKIT_MEDIA_TRACK_TAG_KIND,
70             static_cast<int>(VideoTrackPrivate::Kind::Main), nullptr);
71
72         if (track->isCaptureTrack()) {
73             GStreamerVideoCaptureSource& source = static_cast<GStreamerVideoCaptureSource&>(
74                 track->source());
75
76             gst_tag_list_add(taglist, GST_TAG_MERGE_APPEND,
77                 WEBKIT_MEDIA_TRACK_TAG_WIDTH, source.size().width(),
78                 WEBKIT_MEDIA_TRACK_TAG_HEIGHT, source.size().height(), nullptr);
79         }
80     }
81
82     return taglist;
83 }
84
85 GstStream* webkitMediaStreamNew(MediaStreamTrackPrivate* track)
86 {
87     GRefPtr<GstCaps> caps;
88     GstStreamType type;
89
90     if (track->type() == RealtimeMediaSource::Type::Audio) {
91         caps = adoptGRef(gst_static_pad_template_get_caps(&audioSrcTemplate));
92         type = GST_STREAM_TYPE_AUDIO;
93     } else if (track->type() == RealtimeMediaSource::Type::Video) {
94         caps = adoptGRef(gst_static_pad_template_get_caps(&videoSrcTemplate));
95         type = GST_STREAM_TYPE_VIDEO;
96     } else {
97         GST_FIXME("Handle %d type", static_cast<int>(track->type()));
98
99         return nullptr;
100     }
101
102     auto gststream = (GstStream*)gst_stream_new(track->id().utf8().data(),
103         caps.get(), type, GST_STREAM_FLAG_SELECT);
104     auto tags = adoptGRef(mediaStreamTrackPrivateGetTags(track));
105     gst_stream_set_tags(gststream, tags.get());
106
107     return gststream;
108 }
109
110 class WebKitMediaStreamTrackObserver
111     : public MediaStreamTrackPrivate::Observer {
112 public:
113     virtual ~WebKitMediaStreamTrackObserver() { };
114     WebKitMediaStreamTrackObserver(WebKitMediaStreamSrc* src)
115         : m_mediaStreamSrc(src) { }
116     void trackStarted(MediaStreamTrackPrivate&) final { };
117
118     void trackEnded(MediaStreamTrackPrivate& track) final
119     {
120         webkitMediaStreamSrcTrackEnded(m_mediaStreamSrc, track);
121     }
122
123     void trackMutedChanged(MediaStreamTrackPrivate&) final { };
124     void trackSettingsChanged(MediaStreamTrackPrivate&) final { };
125     void trackEnabledChanged(MediaStreamTrackPrivate&) final { };
126     void readyStateChanged(MediaStreamTrackPrivate&) final { };
127
128     void sampleBufferUpdated(MediaStreamTrackPrivate&, MediaSample& sample) final
129     {
130         auto gstsample = static_cast<MediaSampleGStreamer*>(&sample)->platformSample().sample.gstSample;
131
132         webkitMediaStreamSrcPushVideoSample(m_mediaStreamSrc, gstsample);
133     }
134
135     void audioSamplesAvailable(MediaStreamTrackPrivate&, const MediaTime&, const PlatformAudioData& audioData, const AudioStreamDescription&, size_t) final
136     {
137         auto audiodata = static_cast<const GStreamerAudioData&>(audioData);
138
139         webkitMediaStreamSrcPushAudioSample(m_mediaStreamSrc, audiodata.getSample());
140     }
141
142 private:
143     WebKitMediaStreamSrc* m_mediaStreamSrc;
144 };
145
146 class WebKitMediaStreamObserver
147     : public MediaStreamPrivate::Observer {
148 public:
149     virtual ~WebKitMediaStreamObserver() { };
150     WebKitMediaStreamObserver(WebKitMediaStreamSrc* src)
151         : m_mediaStreamSrc(src) { }
152
153     void characteristicsChanged() final { GST_DEBUG_OBJECT(m_mediaStreamSrc.get(), "renegotiation should happen"); }
154     void activeStatusChanged() final { }
155
156     void didAddTrack(MediaStreamTrackPrivate& track) final
157     {
158         webkitMediaStreamSrcAddTrack(m_mediaStreamSrc.get(), &track, false);
159     }
160
161     void didRemoveTrack(MediaStreamTrackPrivate& track) final
162     {
163         webkitMediaStreamSrcRemoveTrackByType(m_mediaStreamSrc.get(), track.type());
164     }
165
166 private:
167     GRefPtr<WebKitMediaStreamSrc> m_mediaStreamSrc;
168 };
169
170 typedef struct _WebKitMediaStreamSrcClass WebKitMediaStreamSrcClass;
171 struct _WebKitMediaStreamSrc {
172     GstBin parent_instance;
173
174     gchar* uri;
175
176     GstElement* audioSrc;
177     GstClockTime firstAudioBufferPts;
178     GstElement* videoSrc;
179     GstClockTime firstFramePts;
180
181     std::unique_ptr<WebKitMediaStreamTrackObserver> mediaStreamTrackObserver;
182     std::unique_ptr<WebKitMediaStreamObserver> mediaStreamObserver;
183     volatile gint npads;
184     RefPtr<MediaStreamPrivate> stream;
185     RefPtr<MediaStreamTrackPrivate> track;
186
187     GstFlowCombiner* flowCombiner;
188     GRefPtr<GstStreamCollection> streamCollection;
189 };
190
191 struct _WebKitMediaStreamSrcClass {
192     GstBinClass parent_class;
193 };
194
195 enum {
196     PROP_0,
197     PROP_IS_LIVE,
198     PROP_LAST
199 };
200
201 static GstURIType webkit_media_stream_src_uri_get_type(GType)
202 {
203     return GST_URI_SRC;
204 }
205
206 static const gchar* const* webkit_media_stream_src_uri_get_protocols(GType)
207 {
208     static const gchar* protocols[] = { "mediastream", nullptr };
209
210     return protocols;
211 }
212
213 static gchar* webkit_media_stream_src_uri_get_uri(GstURIHandler* handler)
214 {
215     WebKitMediaStreamSrc* self = WEBKIT_MEDIA_STREAM_SRC(handler);
216
217     /* FIXME: make thread-safe */
218     return g_strdup(self->uri);
219 }
220
221 static gboolean webkitMediaStreamSrcUriSetUri(GstURIHandler* handler, const gchar* uri,
222     GError**)
223 {
224     WebKitMediaStreamSrc* self = WEBKIT_MEDIA_STREAM_SRC(handler);
225     self->uri = g_strdup(uri);
226
227     return TRUE;
228 }
229
230 static void webkitMediaStreamSrcUriHandlerInit(gpointer g_iface, gpointer)
231 {
232     GstURIHandlerInterface* iface = (GstURIHandlerInterface*)g_iface;
233
234     iface->get_type = webkit_media_stream_src_uri_get_type;
235     iface->get_protocols = webkit_media_stream_src_uri_get_protocols;
236     iface->get_uri = webkit_media_stream_src_uri_get_uri;
237     iface->set_uri = webkitMediaStreamSrcUriSetUri;
238 }
239
240 GST_DEBUG_CATEGORY_STATIC(webkitMediaStreamSrcDebug);
241 #define GST_CAT_DEFAULT webkitMediaStreamSrcDebug
242
243 #define doInit                                                                                                                                                              \
244     G_IMPLEMENT_INTERFACE(GST_TYPE_URI_HANDLER, webkitMediaStreamSrcUriHandlerInit);                                                                                    \
245     GST_DEBUG_CATEGORY_INIT(webkitMediaStreamSrcDebug, "webkitwebmediastreamsrc", 0, "mediastreamsrc element");                                                           \
246     gst_tag_register_static(WEBKIT_MEDIA_TRACK_TAG_WIDTH, GST_TAG_FLAG_META, G_TYPE_INT, "Webkit MediaStream width", "Webkit MediaStream width", gst_tag_merge_use_first);    \
247     gst_tag_register_static(WEBKIT_MEDIA_TRACK_TAG_HEIGHT, GST_TAG_FLAG_META, G_TYPE_INT, "Webkit MediaStream height", "Webkit MediaStream height", gst_tag_merge_use_first); \
248     gst_tag_register_static(WEBKIT_MEDIA_TRACK_TAG_KIND, GST_TAG_FLAG_META, G_TYPE_INT, "Webkit MediaStream Kind", "Webkit MediaStream Kind", gst_tag_merge_use_first);
249
250 G_DEFINE_TYPE_WITH_CODE(WebKitMediaStreamSrc, webkit_media_stream_src, GST_TYPE_BIN, doInit);
251
252 static void webkitMediaStreamSrcSetProperty(GObject* object, guint prop_id,
253     const GValue*, GParamSpec* pspec)
254 {
255     switch (prop_id) {
256     default:
257         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
258         break;
259     }
260 }
261
262 static void webkitMediaStreamSrcGetProperty(GObject* object, guint prop_id, GValue* value,
263     GParamSpec* pspec)
264 {
265     switch (prop_id) {
266     case PROP_IS_LIVE:
267         g_value_set_boolean(value, TRUE);
268         break;
269     default:
270         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
271         break;
272     }
273 }
274
275 static void webkitMediaStreamSrcDispose(GObject* object)
276 {
277     WebKitMediaStreamSrc* self = WEBKIT_MEDIA_STREAM_SRC(object);
278
279     if (self->audioSrc) {
280         gst_bin_remove(GST_BIN(self), self->audioSrc);
281         self->audioSrc = nullptr;
282     }
283
284     if (self->videoSrc) {
285         gst_bin_remove(GST_BIN(self), self->videoSrc);
286         self->videoSrc = nullptr;
287     }
288 }
289
290 static void webkitMediaStreamSrcFinalize(GObject* object)
291 {
292     WebKitMediaStreamSrc* self = WEBKIT_MEDIA_STREAM_SRC(object);
293
294     GST_OBJECT_LOCK(self);
295     if (self->stream) {
296         for (auto& track : self->stream->tracks())
297             track->removeObserver(*self->mediaStreamTrackObserver.get());
298
299         self->stream->removeObserver(*self->mediaStreamObserver);
300         self->stream = nullptr;
301     }
302     GST_OBJECT_UNLOCK(self);
303
304     g_clear_pointer(&self->uri, g_free);
305     gst_flow_combiner_free(self->flowCombiner);
306 }
307
308 static GstStateChangeReturn webkitMediaStreamSrcChangeState(GstElement* element, GstStateChange transition)
309 {
310     GstStateChangeReturn result;
311     auto* self = WEBKIT_MEDIA_STREAM_SRC(element);
312
313     if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
314
315         GST_OBJECT_LOCK(self);
316         if (self->stream) {
317             for (auto& track : self->stream->tracks())
318                 track->removeObserver(*self->mediaStreamTrackObserver.get());
319         } else if (self->track)
320             self->track->removeObserver(*self->mediaStreamTrackObserver.get());
321         GST_OBJECT_UNLOCK(self);
322     }
323
324     result = GST_ELEMENT_CLASS(webkit_media_stream_src_parent_class)->change_state(element, transition);
325
326     if (transition == GST_STATE_CHANGE_READY_TO_PAUSED)
327         result = GST_STATE_CHANGE_NO_PREROLL;
328
329     return result;
330 }
331
332 static void webkit_media_stream_src_class_init(WebKitMediaStreamSrcClass* klass)
333 {
334     GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
335     GstElementClass* gstelement_klass = GST_ELEMENT_CLASS(klass);
336
337     gobject_class->finalize = webkitMediaStreamSrcFinalize;
338     gobject_class->dispose = webkitMediaStreamSrcDispose;
339     gobject_class->get_property = webkitMediaStreamSrcGetProperty;
340     gobject_class->set_property = webkitMediaStreamSrcSetProperty;
341
342     g_object_class_install_property(gobject_class, PROP_IS_LIVE,
343         g_param_spec_boolean("is-live", "Is Live",
344             "Let playbin3 know we are a live source.",
345             TRUE, (GParamFlags)(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
346
347     gstelement_klass->change_state = webkitMediaStreamSrcChangeState;
348     gst_element_class_add_pad_template(gstelement_klass,
349         gst_static_pad_template_get(&videoSrcTemplate));
350     gst_element_class_add_pad_template(gstelement_klass,
351         gst_static_pad_template_get(&audioSrcTemplate));
352 }
353
354 static void webkit_media_stream_src_init(WebKitMediaStreamSrc* self)
355 {
356     self->mediaStreamTrackObserver = std::make_unique<WebKitMediaStreamTrackObserver>(self);
357     self->mediaStreamObserver = std::make_unique<WebKitMediaStreamObserver>(self);
358     self->flowCombiner = gst_flow_combiner_new();
359     self->firstAudioBufferPts = GST_CLOCK_TIME_NONE;
360     self->firstFramePts = GST_CLOCK_TIME_NONE;
361 }
362
363 typedef struct {
364     WebKitMediaStreamSrc* self;
365     RefPtr<MediaStreamTrackPrivate> track;
366     GstStaticPadTemplate* pad_template;
367 } ProbeData;
368
369 static GstFlowReturn webkitMediaStreamSrcChain(GstPad* pad, GstObject* parent, GstBuffer* buffer)
370 {
371     GstFlowReturn result;
372     GRefPtr<WebKitMediaStreamSrc> self = adoptGRef(WEBKIT_MEDIA_STREAM_SRC(gst_object_get_parent(parent)));
373
374     result = gst_flow_combiner_update_pad_flow(self.get()->flowCombiner, pad,
375         gst_proxy_pad_chain_default(pad, GST_OBJECT(self.get()), buffer));
376
377     return result;
378 }
379
380 static void webkitMediaStreamSrcAddPad(WebKitMediaStreamSrc* self, GstPad* target, GstStaticPadTemplate* pad_template)
381 {
382     auto padname = String::format("src_%u", g_atomic_int_add(&(self->npads), 1));
383     auto ghostpad = gst_ghost_pad_new_from_template(padname.utf8().data(), target,
384         gst_static_pad_template_get(pad_template));
385
386     GST_DEBUG_OBJECT(self, "%s Ghosting %" GST_PTR_FORMAT,
387         gst_object_get_path_string(GST_OBJECT_CAST(self)),
388         target);
389
390     auto proxypad = adoptGRef(GST_PAD(gst_proxy_pad_get_internal(GST_PROXY_PAD(ghostpad))));
391     gst_pad_set_active(ghostpad, TRUE);
392     if (!gst_element_add_pad(GST_ELEMENT(self), GST_PAD(ghostpad))) {
393         GST_ERROR_OBJECT(self, "Could not add pad %s:%s", GST_DEBUG_PAD_NAME(ghostpad));
394         ASSERT_NOT_REACHED();
395
396         return;
397     }
398
399     gst_flow_combiner_add_pad(self->flowCombiner, proxypad.get());
400     gst_pad_set_chain_function(proxypad.get(),
401         static_cast<GstPadChainFunction>(webkitMediaStreamSrcChain));
402 }
403
404 static GstPadProbeReturn webkitMediaStreamSrcPadProbeCb(GstPad* pad, GstPadProbeInfo* info, ProbeData* data)
405 {
406     GstEvent* event = GST_PAD_PROBE_INFO_EVENT(info);
407     WebKitMediaStreamSrc* self = data->self;
408
409     switch (GST_EVENT_TYPE(event)) {
410     case GST_EVENT_STREAM_START: {
411         const gchar* stream_id;
412         GRefPtr<GstStream> stream = nullptr;
413
414         gst_event_parse_stream_start(event, &stream_id);
415         if (!g_strcmp0(stream_id, data->track->id().utf8().data())) {
416             GST_INFO_OBJECT(pad, "Event has been sticked already");
417             return GST_PAD_PROBE_OK;
418         }
419
420         auto stream_start = gst_event_new_stream_start(data->track->id().utf8().data());
421         gst_event_set_group_id(stream_start, 1);
422         gst_event_unref(event);
423
424         gst_pad_push_event(pad, stream_start);
425         gst_pad_push_event(pad, gst_event_new_tag(mediaStreamTrackPrivateGetTags(data->track.get())));
426
427         webkitMediaStreamSrcAddPad(self, pad, data->pad_template);
428
429         return GST_PAD_PROBE_HANDLED;
430     }
431     default:
432         break;
433     }
434
435     return GST_PAD_PROBE_OK;
436 }
437
438 static gboolean webkitMediaStreamSrcSetupSrc(WebKitMediaStreamSrc* self,
439     MediaStreamTrackPrivate* track, GstElement* element,
440     GstStaticPadTemplate* pad_template, gboolean observe_track,
441     bool onlyTrack)
442 {
443     auto pad = adoptGRef(gst_element_get_static_pad(element, "src"));
444
445     gst_bin_add(GST_BIN(self), element);
446
447     if (!onlyTrack) {
448         ProbeData* data = new ProbeData;
449         data->self = WEBKIT_MEDIA_STREAM_SRC(self);
450         data->pad_template = pad_template;
451         data->track = track;
452
453         gst_pad_add_probe(pad.get(), (GstPadProbeType)GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
454             (GstPadProbeCallback)webkitMediaStreamSrcPadProbeCb, data,
455             [](gpointer data) {
456                 delete (ProbeData*)data;
457             });
458     } else
459         webkitMediaStreamSrcAddPad(self, pad.get(), pad_template);
460
461     if (observe_track)
462         track->addObserver(*self->mediaStreamTrackObserver.get());
463
464     gst_element_sync_state_with_parent(element);
465     return TRUE;
466 }
467
468 static gboolean webkitMediaStreamSrcSetupAppSrc(WebKitMediaStreamSrc* self,
469     MediaStreamTrackPrivate* track, GstElement** element,
470     GstStaticPadTemplate* pad_template, bool onlyTrack)
471 {
472     *element = gst_element_factory_make("appsrc", nullptr);
473     g_object_set(*element, "is-live", true, "format", GST_FORMAT_TIME, nullptr);
474
475     return webkitMediaStreamSrcSetupSrc(self, track, *element, pad_template, TRUE, onlyTrack);
476 }
477
478 static void webkitMediaStreamSrcPostStreamCollection(WebKitMediaStreamSrc* self, MediaStreamPrivate* stream)
479 {
480     GST_OBJECT_LOCK(self);
481     self->streamCollection = adoptGRef(gst_stream_collection_new(stream->id().utf8().data()));
482     for (auto& track : stream->tracks()) {
483         auto gststream = webkitMediaStreamNew(track.get());
484
485         gst_stream_collection_add_stream(self->streamCollection.get(), gststream);
486     }
487     GST_OBJECT_UNLOCK(self);
488
489     gst_element_post_message(GST_ELEMENT(self),
490         gst_message_new_stream_collection(GST_OBJECT(self), self->streamCollection.get()));
491 }
492
493 bool webkitMediaStreamSrcAddTrack(WebKitMediaStreamSrc* self, MediaStreamTrackPrivate* track, bool onlyTrack)
494 {
495     bool res = false;
496     if (track->type() == RealtimeMediaSource::Type::Audio)
497         res = webkitMediaStreamSrcSetupAppSrc(self, track, &self->audioSrc, &audioSrcTemplate, onlyTrack);
498     else if (track->type() == RealtimeMediaSource::Type::Video)
499         res = webkitMediaStreamSrcSetupAppSrc(self, track, &self->videoSrc, &videoSrcTemplate, onlyTrack);
500     else
501         GST_INFO("Unsupported track type: %d", static_cast<int>(track->type()));
502
503     if (onlyTrack && res)
504         self->track = track;
505
506     return false;
507 }
508
509 static void webkitMediaStreamSrcRemoveTrackByType(WebKitMediaStreamSrc* self, RealtimeMediaSource::Type trackType)
510 {
511     if (trackType == RealtimeMediaSource::Type::Audio) {
512         if (self->audioSrc) {
513             gst_element_set_state(self->audioSrc, GST_STATE_NULL);
514             gst_bin_remove(GST_BIN(self), self->audioSrc);
515             self->audioSrc = nullptr;
516         }
517     } else if (trackType == RealtimeMediaSource::Type::Video) {
518         if (self->videoSrc) {
519             gst_element_set_state(self->videoSrc, GST_STATE_NULL);
520             gst_bin_remove(GST_BIN(self), self->videoSrc);
521             self->videoSrc = nullptr;
522         }
523     } else
524         GST_INFO("Unsupported track type: %d", static_cast<int>(trackType));
525 }
526
527 bool webkitMediaStreamSrcSetStream(WebKitMediaStreamSrc* self, MediaStreamPrivate* stream)
528 {
529     ASSERT(WEBKIT_IS_MEDIA_STREAM_SRC(self));
530
531     webkitMediaStreamSrcRemoveTrackByType(self, RealtimeMediaSource::Type::Audio);
532     webkitMediaStreamSrcRemoveTrackByType(self, RealtimeMediaSource::Type::Video);
533
534     webkitMediaStreamSrcPostStreamCollection(self, stream);
535
536     self->stream = stream;
537     self->stream->addObserver(*self->mediaStreamObserver.get());
538     for (auto& track : stream->tracks())
539         webkitMediaStreamSrcAddTrack(self, track.get(), false);
540
541     return TRUE;
542 }
543
544 static void webkitMediaStreamSrcPushVideoSample(WebKitMediaStreamSrc* self, GstSample* gstsample)
545 {
546     if (self->videoSrc) {
547         if (!GST_CLOCK_TIME_IS_VALID(self->firstFramePts)) {
548             auto buffer = gst_sample_get_buffer(gstsample);
549
550             self->firstFramePts = GST_BUFFER_PTS(buffer);
551             auto pad = adoptGRef(gst_element_get_static_pad(self->videoSrc, "src"));
552             gst_pad_set_offset(pad.get(), -self->firstFramePts);
553         }
554
555         gst_app_src_push_sample(GST_APP_SRC(self->videoSrc), gstsample);
556     }
557 }
558
559 static void webkitMediaStreamSrcPushAudioSample(WebKitMediaStreamSrc* self, GstSample* gstsample)
560 {
561     if (self->audioSrc) {
562         if (!GST_CLOCK_TIME_IS_VALID(self->firstAudioBufferPts)) {
563             auto buffer = gst_sample_get_buffer(gstsample);
564
565             self->firstAudioBufferPts = GST_BUFFER_PTS(buffer);
566             auto pad = adoptGRef(gst_element_get_static_pad(self->audioSrc, "src"));
567             gst_pad_set_offset(pad.get(), -self->firstAudioBufferPts);
568         }
569         gst_app_src_push_sample(GST_APP_SRC(self->audioSrc), gstsample);
570     }
571 }
572
573 static void webkitMediaStreamSrcTrackEnded(WebKitMediaStreamSrc* self,
574     MediaStreamTrackPrivate& track)
575 {
576     GRefPtr<GstPad> pad = nullptr;
577
578     GST_OBJECT_LOCK(self);
579     for (auto tmp = GST_ELEMENT(self)->srcpads; tmp; tmp = tmp->next) {
580         GstPad* tmppad = GST_PAD(tmp->data);
581         const gchar* stream_id;
582
583         GstEvent* stream_start = gst_pad_get_sticky_event(tmppad, GST_EVENT_STREAM_START, 0);
584         if (!stream_start)
585             continue;
586
587         gst_event_parse_stream_start(stream_start, &stream_id);
588         if (String(stream_id) == track.id()) {
589             pad = tmppad;
590             break;
591         }
592     }
593     GST_OBJECT_UNLOCK(self);
594
595     if (!pad) {
596         GST_ERROR_OBJECT(self, "No pad found for %s", track.id().utf8().data());
597
598         return;
599     }
600
601     // Make sure that the video.videoWidth is reset to 0
602     webkitMediaStreamSrcPostStreamCollection(self, self->stream.get());
603     auto tags = mediaStreamTrackPrivateGetTags(&track);
604     gst_pad_push_event(pad.get(), gst_event_new_tag(tags));
605     gst_pad_push_event(pad.get(), gst_event_new_eos());
606 }
607
608 GstElement* webkitMediaStreamSrcNew(void)
609 {
610     return GST_ELEMENT(g_object_new(webkit_media_stream_src_get_type(), nullptr));
611 }
612
613 } // WebCore
614 #endif // GST_CHECK_VERSION(1, 10, 0)
615 #endif // ENABLE(VIDEO) && ENABLE(MEDIA_STREAM) && USE(LIBWEBRTC)