forked from centricular/gstcefsrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgstcefdemux.cc
286 lines (231 loc) · 8.44 KB
/
gstcefdemux.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <gst/audio/audio.h>
#include "gstcefdemux.h"
#include "gstcefaudiometa.h"
#define CEF_VIDEO_CAPS "video/x-raw, format=BGRA, width=[1, 2147483647], height=[1, 2147483647], framerate=[1/1, 60/1], pixel-aspect-ratio=1/1"
#define CEF_AUDIO_CAPS "audio/x-raw, format=F32LE, rate=[1, 2147483647], channels=[1, 2147483647], layout=interleaved"
G_DEFINE_TYPE (GstCefDemux, gst_cef_demux, GST_TYPE_ELEMENT);
static GstStaticPadTemplate gst_cef_demux_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (CEF_VIDEO_CAPS)
);
static GstStaticPadTemplate gst_cef_demux_video_src_template =
GST_STATIC_PAD_TEMPLATE ("video",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (CEF_VIDEO_CAPS)
);
static GstStaticPadTemplate gst_cef_demux_audio_src_template =
GST_STATIC_PAD_TEMPLATE ("audio",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (CEF_AUDIO_CAPS)
);
static gboolean
gst_cef_demux_push_events (GstCefDemux *demux)
{
GstSegment segment;
GstEvent *event;
if (demux->need_stream_start) {
event = gst_event_new_stream_start ("cefvideo");
gst_pad_push_event (demux->vsrcpad, event);
event = gst_event_new_stream_start ("cefaudio");
gst_pad_push_event (demux->asrcpad, event);
demux->need_stream_start = FALSE;
}
if (demux->need_caps) {
GstCaps *audio_caps;
g_assert (demux->vcaps_event);
gst_pad_push_event (demux->vsrcpad, demux->vcaps_event);
demux->vcaps_event = NULL;
/* Push some dummy caps so that our initial gap events don't
* get refused */
audio_caps = gst_caps_new_simple ("audio/x-raw",
"format", G_TYPE_STRING, "F32LE",
"rate", G_TYPE_INT, 44100,
"channels", G_TYPE_INT, 2,
"layout", G_TYPE_STRING, "interleaved",
NULL);
gst_pad_push_event (demux->asrcpad, gst_event_new_caps (audio_caps));
gst_caps_unref (audio_caps);
demux->need_caps = FALSE;
}
if (demux->need_segment) {
gst_segment_init (&segment, GST_FORMAT_TIME);
event = gst_event_new_segment (&segment);
gst_pad_push_event (demux->vsrcpad, event);
event = gst_event_new_segment (&segment);
gst_pad_push_event (demux->asrcpad, event);
demux->need_segment = FALSE;
}
return TRUE;
}
typedef struct
{
GstCefDemux *demux;
GstFlowCombiner *flow_combiner;
GstFlowReturn combined;
} AudioPushData;
static gboolean
gst_cef_demux_push_audio_buffer (GstBuffer **buffer, guint idx, AudioPushData *push_data)
{
GST_BUFFER_PTS (*buffer) += push_data->demux->ts_offset;
push_data->combined = gst_flow_combiner_update_pad_flow (push_data->flow_combiner, push_data->demux->asrcpad,
gst_pad_push (push_data->demux->asrcpad, *buffer));
push_data->demux->last_audio_time = GST_BUFFER_PTS (*buffer) + GST_BUFFER_DURATION (*buffer);
*buffer = NULL;
return TRUE;
}
static void
gst_cef_demux_update_audio_caps (GstCefDemux *demux, const GstStructure *s)
{
GstCaps *caps;
GstEvent *event;
gint channels, rate;
gst_structure_get_int (s, "channels", &channels);
gst_structure_get_int (s, "rate", &rate);
caps = gst_caps_new_simple ("audio/x-raw",
"format", G_TYPE_STRING, "F32LE",
"rate", G_TYPE_INT, rate,
"channels", G_TYPE_INT, channels,
"channel-mask", GST_TYPE_BITMASK, gst_audio_channel_get_fallback_mask (channels),
"layout", G_TYPE_STRING, "interleaved",
NULL);
event = gst_event_new_caps (caps);
gst_pad_push_event (demux->asrcpad, event);
gst_caps_unref (caps);
}
static GstFlowReturn
gst_cef_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
{
GstCefDemux *demux = (GstCefDemux *) parent;
GstMeta *meta;
gpointer state = NULL;
GList *tmp;
GstFlowReturn ret = GST_FLOW_OK;
gst_cef_demux_push_events (demux);
if (!GST_CLOCK_TIME_IS_VALID (demux->ts_offset)) {
demux->ts_offset = GST_BUFFER_PTS (buffer);
}
for (tmp = demux->cef_audio_stream_start_events; tmp; tmp = tmp->next) {
const GstStructure *s = gst_event_get_structure ((GstEvent *) tmp->data);
gst_cef_demux_update_audio_caps (demux, s);
}
g_list_free_full (demux->cef_audio_stream_start_events, (GDestroyNotify) gst_event_unref);
demux->cef_audio_stream_start_events = NULL;
while ((meta = gst_buffer_iterate_meta_filtered (buffer, &state, GST_CEF_AUDIO_META_API_TYPE)) != NULL) {
AudioPushData push_data;
GstCefAudioMeta *ameta = (GstCefAudioMeta *) meta;
push_data.demux = demux;
push_data.flow_combiner = demux->flow_combiner;
gst_buffer_list_foreach (ameta->buffers, (GstBufferListFunc) gst_cef_demux_push_audio_buffer, &push_data);
if (push_data.combined != GST_FLOW_OK) {
ret = push_data.combined;
goto done;
}
}
ret = gst_flow_combiner_update_pad_flow (demux->flow_combiner, demux->vsrcpad,
gst_pad_push (demux->vsrcpad, buffer));
if (demux->last_audio_time < GST_BUFFER_PTS (buffer)) {
GstEvent *gap;
gap = gst_event_new_gap (demux->last_audio_time, GST_BUFFER_PTS (buffer) - demux->last_audio_time);
gst_pad_push_event (demux->asrcpad, gap);
demux->last_audio_time = GST_BUFFER_PTS (buffer);
}
if (ret != GST_FLOW_OK)
goto done;
done:
return ret;
}
static gboolean
gst_cef_demux_sink_event (GstPad *pad, GstObject *parent, GstEvent *event)
{
GstCefDemux *demux = (GstCefDemux *) parent;
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_CUSTOM_DOWNSTREAM:
{
const GstStructure *s = gst_event_get_structure (event);
if (gst_structure_has_name (s, "cef-audio-stream-start")) {
demux->cef_audio_stream_start_events = g_list_append (demux->cef_audio_stream_start_events, event);
event = NULL;
}
break;
}
case GST_EVENT_CAPS:
demux->vcaps_event = event;
demux->need_caps = TRUE;
event = NULL;
/* We send our own */
case GST_EVENT_SEGMENT:
case GST_EVENT_STREAM_START:
gst_event_replace (&event, NULL);
break;
default:
break;
}
if (event) {
gst_pad_event_default(pad, parent, event);
}
return TRUE;
}
static gboolean
gst_cef_demux_sink_query (GstPad *pad, GstObject *parent, GstQuery *query)
{
GstCefDemux *demux = (GstCefDemux *) parent;
gboolean ret;
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CAPS:
ret = gst_pad_peer_query(demux->vsrcpad, query);
break;
default:
ret = gst_pad_query_default(pad, parent, query);
break;
}
return ret;
}
static void
gst_cef_demux_init (GstCefDemux * demux)
{
GstPad *sinkpad = gst_pad_new_from_static_template (&gst_cef_demux_sink_template, "sink");;
demux->vsrcpad = gst_pad_new_from_static_template (&gst_cef_demux_video_src_template, "video");
demux->asrcpad = gst_pad_new_from_static_template (&gst_cef_demux_audio_src_template, "audio");
gst_pad_set_chain_function (sinkpad, gst_cef_demux_chain);
gst_pad_set_event_function (sinkpad, gst_cef_demux_sink_event);
gst_pad_set_query_function (sinkpad, gst_cef_demux_sink_query);
gst_element_add_pad (GST_ELEMENT (demux), sinkpad);
demux->flow_combiner = gst_flow_combiner_new ();
gst_element_add_pad (GST_ELEMENT (demux), demux->vsrcpad);
gst_flow_combiner_add_pad (demux->flow_combiner, demux->vsrcpad);
gst_element_add_pad (GST_ELEMENT (demux), demux->asrcpad);
gst_flow_combiner_add_pad (demux->flow_combiner, demux->asrcpad);
demux->need_stream_start = TRUE;
demux->need_caps = TRUE;
demux->need_segment = TRUE;
demux->last_audio_time = 0;
demux->ts_offset = GST_CLOCK_TIME_NONE;
}
static void
gst_cef_demux_finalize (GObject *object)
{
GstCefDemux *demux = GST_CEF_DEMUX (object);
g_list_free_full (demux->cef_audio_stream_start_events, (GDestroyNotify) gst_event_unref);
demux->cef_audio_stream_start_events = NULL;
gst_flow_combiner_free (demux->flow_combiner);
}
static void
gst_cef_demux_class_init (GstCefDemuxClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
GstElementClass *gstelement_class = GST_ELEMENT_CLASS(klass);
gobject_class->finalize = gst_cef_demux_finalize;
gst_element_class_set_static_metadata (gstelement_class,
"Chromium Embedded Framework demuxer", "Demuxer/Audio/Video",
"Demuxes audio and video from cefsrc", "Mathieu Duponchelle <[email protected]>");
gst_element_class_add_static_pad_template (gstelement_class,
&gst_cef_demux_sink_template);
gst_element_class_add_static_pad_template (gstelement_class,
&gst_cef_demux_video_src_template);
gst_element_class_add_static_pad_template (gstelement_class,
&gst_cef_demux_audio_src_template);
}