-
Notifications
You must be signed in to change notification settings - Fork 0
/
gstajasrcdemux.cpp
292 lines (239 loc) · 10.4 KB
/
gstajasrcdemux.cpp
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
287
288
289
290
291
292
/* GStreamer
* Copyright (C) 2021 Sebastian Dröge <[email protected]>
*
* 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., 51 Franklin Street, Suite 500,
* Boston, MA 02110-1335, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/audio/audio.h>
#include "gstajacommon.h"
#include "gstajasrcdemux.h"
GST_DEBUG_CATEGORY_STATIC(gst_aja_src_demux_debug);
#define GST_CAT_DEFAULT gst_aja_src_demux_debug
static GstStaticPadTemplate video_src_template = GST_STATIC_PAD_TEMPLATE(
"video", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS("video/x-raw"));
static GstStaticPadTemplate audio_src_template = GST_STATIC_PAD_TEMPLATE(
"audio", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS("audio/x-raw"));
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE(
"sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS("video/x-raw"));
static GstFlowReturn gst_aja_src_demux_sink_chain(GstPad *pad,
GstObject *parent,
GstBuffer *buffer);
static gboolean gst_aja_src_demux_sink_event(GstPad *pad, GstObject *parent,
GstEvent *event);
static gboolean gst_aja_src_demux_audio_src_query(GstPad *pad,
GstObject *parent,
GstQuery *query);
static gboolean gst_aja_src_demux_video_src_query(GstPad *pad,
GstObject *parent,
GstQuery *query);
#define parent_class gst_aja_src_demux_parent_class
G_DEFINE_TYPE(GstAjaSrcDemux, gst_aja_src_demux, GST_TYPE_ELEMENT);
static void gst_aja_src_demux_class_init(GstAjaSrcDemuxClass *klass) {
GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
gst_element_class_add_static_pad_template(element_class, &sink_template);
gst_element_class_add_static_pad_template(element_class, &video_src_template);
gst_element_class_add_static_pad_template(element_class, &audio_src_template);
gst_element_class_set_static_metadata(
element_class, "AJA audio/video source demuxer", "Audio/Video/Demux",
"Demuxes audio/video from video buffers",
"Sebastian Dröge <[email protected]>");
GST_DEBUG_CATEGORY_INIT(gst_aja_src_demux_debug, "ajasrcdemux", 0,
"AJA source demuxer");
}
static void gst_aja_src_demux_init(GstAjaSrcDemux *self) {
self->sink = gst_pad_new_from_static_template(&sink_template, "sink");
gst_pad_set_chain_function(self->sink,
GST_DEBUG_FUNCPTR(gst_aja_src_demux_sink_chain));
gst_pad_set_event_function(self->sink,
GST_DEBUG_FUNCPTR(gst_aja_src_demux_sink_event));
gst_element_add_pad(GST_ELEMENT(self), self->sink);
self->audio_src =
gst_pad_new_from_static_template(&audio_src_template, "audio");
gst_pad_set_query_function(
self->audio_src, GST_DEBUG_FUNCPTR(gst_aja_src_demux_audio_src_query));
gst_element_add_pad(GST_ELEMENT(self), self->audio_src);
self->video_src =
gst_pad_new_from_static_template(&video_src_template, "video");
gst_pad_set_query_function(
self->video_src, GST_DEBUG_FUNCPTR(gst_aja_src_demux_video_src_query));
gst_element_add_pad(GST_ELEMENT(self), self->video_src);
}
static GstFlowReturn gst_aja_src_demux_sink_chain(GstPad *pad,
GstObject *parent,
GstBuffer *buffer) {
GstAjaSrcDemux *self = GST_AJA_SRC_DEMUX(parent);
GstAjaAudioMeta *meta = gst_buffer_get_aja_audio_meta(buffer);
GstFlowReturn audio_flow_ret = GST_FLOW_OK;
GstFlowReturn video_flow_ret = GST_FLOW_OK;
if (meta) {
GstBuffer *audio_buffer;
buffer = gst_buffer_make_writable(buffer);
meta = gst_buffer_get_aja_audio_meta(buffer);
audio_buffer = gst_buffer_ref(meta->buffer);
gst_buffer_remove_meta(buffer, GST_META_CAST(meta));
audio_flow_ret = gst_pad_push(self->audio_src, audio_buffer);
} else {
GstEvent *event =
gst_event_new_gap(GST_BUFFER_PTS(buffer), GST_BUFFER_DURATION(buffer));
gst_pad_push_event(self->audio_src, event);
}
video_flow_ret = gst_pad_push(self->video_src, buffer);
// Combine flows the way it makes sense
if (video_flow_ret == GST_FLOW_NOT_LINKED &&
audio_flow_ret == GST_FLOW_NOT_LINKED)
return GST_FLOW_NOT_LINKED;
if (video_flow_ret == GST_FLOW_EOS && audio_flow_ret == GST_FLOW_EOS)
return GST_FLOW_EOS;
if (video_flow_ret == GST_FLOW_FLUSHING ||
video_flow_ret <= GST_FLOW_NOT_NEGOTIATED)
return video_flow_ret;
if (audio_flow_ret == GST_FLOW_FLUSHING ||
audio_flow_ret <= GST_FLOW_NOT_NEGOTIATED)
return audio_flow_ret;
return GST_FLOW_OK;
}
static gboolean gst_aja_src_demux_sink_event(GstPad *pad, GstObject *parent,
GstEvent *event) {
GstAjaSrcDemux *self = GST_AJA_SRC_DEMUX(parent);
switch (GST_EVENT_TYPE(event)) {
case GST_EVENT_CAPS: {
GstCaps *caps;
GstStructure *s;
GstAudioInfo audio_info;
gint audio_channels = 0;
gst_event_parse_caps(event, &caps);
s = gst_caps_get_structure(caps, 0);
gst_structure_get_int(s, "audio-channels", &audio_channels);
GstCaps *audio_caps, *video_caps;
gst_audio_info_init(&audio_info);
gst_audio_info_set_format(&audio_info, GST_AUDIO_FORMAT_S32LE, 48000,
audio_channels ? audio_channels : 1, NULL);
audio_caps = gst_audio_info_to_caps(&audio_info);
gst_pad_set_caps(self->audio_src, audio_caps);
gst_caps_unref(audio_caps);
video_caps = gst_caps_ref(caps);
gst_event_unref(event);
video_caps = gst_caps_make_writable(video_caps);
s = gst_caps_get_structure(video_caps, 0);
gst_structure_remove_field(s, "audio-channels");
gst_pad_set_caps(self->video_src, video_caps);
gst_caps_unref(video_caps);
return TRUE;
}
default:
return gst_pad_event_default(pad, parent, event);
}
}
static gboolean gst_aja_src_demux_audio_src_query(GstPad *pad,
GstObject *parent,
GstQuery *query) {
GstAjaSrcDemux *self = GST_AJA_SRC_DEMUX(parent);
switch (GST_QUERY_TYPE(query)) {
case GST_QUERY_CAPS: {
GstCaps *filter, *caps;
gst_query_parse_caps(query, &filter);
if ((caps = gst_pad_get_current_caps(pad))) {
GST_DEBUG_OBJECT(
pad, "Returning currently negotiated caps %" GST_PTR_FORMAT, caps);
} else if ((caps = gst_pad_peer_query_caps(self->sink, NULL))) {
guint n;
GstAudioInfo audio_info;
gint audio_channels = 0;
GstCaps *tmp;
GST_DEBUG_OBJECT(pad, "Got upstream caps %" GST_PTR_FORMAT, caps);
n = gst_caps_get_size(caps);
for (guint i = 0; i < n; i++) {
GstStructure *s = gst_caps_get_structure(caps, i);
gint tmp;
if (!gst_structure_get_int(s, "audio-channels", &tmp)) {
tmp = 0;
}
// No audio channels in all caps
if (tmp == 0 || (audio_channels != 0 && audio_channels != tmp)) {
audio_channels = 0;
break;
}
audio_channels = tmp;
}
gst_audio_info_init(&audio_info);
gst_audio_info_set_format(&audio_info, GST_AUDIO_FORMAT_S32LE, 48000,
audio_channels ? audio_channels : 1, NULL);
tmp = gst_audio_info_to_caps(&audio_info);
gst_caps_unref(caps);
caps = tmp;
if (!audio_channels) {
gst_caps_set_simple(caps, "channels", GST_TYPE_INT_RANGE, 1, G_MAXINT,
NULL);
}
GST_DEBUG_OBJECT(pad, "Returning caps %" GST_PTR_FORMAT, caps);
} else {
caps = gst_pad_get_pad_template_caps(pad);
GST_DEBUG_OBJECT(pad, "Returning template caps %" GST_PTR_FORMAT, caps);
}
if (filter) {
GstCaps *tmp =
gst_caps_intersect_full(filter, caps, GST_CAPS_INTERSECT_FIRST);
gst_caps_unref(caps);
caps = tmp;
}
gst_query_set_caps_result(query, caps);
gst_caps_unref(caps);
return TRUE;
}
default:
return gst_pad_query_default(pad, parent, query);
}
}
static gboolean gst_aja_src_demux_video_src_query(GstPad *pad,
GstObject *parent,
GstQuery *query) {
GstAjaSrcDemux *self = GST_AJA_SRC_DEMUX(parent);
switch (GST_QUERY_TYPE(query)) {
case GST_QUERY_CAPS: {
GstCaps *filter, *caps;
gst_query_parse_caps(query, &filter);
if ((caps = gst_pad_get_current_caps(pad))) {
GST_DEBUG_OBJECT(
pad, "Returning currently negotiated caps %" GST_PTR_FORMAT, caps);
} else if ((caps = gst_pad_peer_query_caps(self->sink, NULL))) {
guint n;
GST_DEBUG_OBJECT(pad, "Returning upstream caps %" GST_PTR_FORMAT, caps);
caps = gst_caps_make_writable(caps);
n = gst_caps_get_size(caps);
for (guint i = 0; i < n; i++) {
GstStructure *s = gst_caps_get_structure(caps, i);
gst_structure_remove_field(s, "audio-channels");
}
} else {
caps = gst_pad_get_pad_template_caps(pad);
GST_DEBUG_OBJECT(pad, "Returning template caps %" GST_PTR_FORMAT, caps);
}
if (filter) {
GstCaps *tmp =
gst_caps_intersect_full(filter, caps, GST_CAPS_INTERSECT_FIRST);
gst_caps_unref(caps);
caps = tmp;
}
gst_query_set_caps_result(query, caps);
gst_caps_unref(caps);
return TRUE;
}
default:
return gst_pad_query_default(pad, parent, query);
}
}