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