forked from LostmanMing/gs-video-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoAudioCapture.cpp
471 lines (357 loc) · 14.1 KB
/
VideoAudioCapture.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include <sstream>
#include "VideoAudioCapture.h"
#include "gstBufferManager.h"
bool gstreamerInit() {
if (gstreamer_initialized) {
return true;
}
int argc = 0;
//char* argv[] = { "none" };
if (!gst_init_check(&argc, nullptr, nullptr)) {
spdlog::error("failed to initialize gstreamer library with gst_init()");
return false;
}
gstreamer_initialized = true;
uint32_t ver[] = {0, 0, 0, 0};
gst_version(&ver[0], &ver[1], &ver[2], &ver[3]);
spdlog::info("initialized gstreamer, version {}.{}.{}.{}", ver[0], ver[1], ver[2], ver[3]);
// debugging
gst_debug_remove_log_function(gst_debug_log_default);
return true;
}
static const char *gst_stream_status_string(GstStreamStatusType status) {
switch (status) {
case GST_STREAM_STATUS_TYPE_CREATE:
return "CREATE";
case GST_STREAM_STATUS_TYPE_ENTER:
return "ENTER";
case GST_STREAM_STATUS_TYPE_LEAVE:
return "LEAVE";
case GST_STREAM_STATUS_TYPE_DESTROY:
return "DESTROY";
case GST_STREAM_STATUS_TYPE_START:
return "START";
case GST_STREAM_STATUS_TYPE_PAUSE:
return "PAUSE";
case GST_STREAM_STATUS_TYPE_STOP:
return "STOP";
default:
return "UNKNOWN";
}
}
// gst_message_print
gboolean gst_message_print(GstBus *bus, GstMessage *message, gpointer user_data) {
switch (GST_MESSAGE_TYPE (message)) {
case GST_MESSAGE_ERROR: {
GError *err = nullptr;
gchar *dbg_info = nullptr;
gst_message_parse_error(message, &err, &dbg_info);
spdlog::info("gstreamer {} ERROR {}", GST_OBJECT_NAME (message->src), err->message);
spdlog::info("gstreamer Debugging info: {}", (dbg_info) ? dbg_info : "none");
g_error_free(err);
g_free(dbg_info);
//g_main_loop_quit (app->loop);
break;
}
case GST_MESSAGE_EOS: {
spdlog::info("gstreamer {} recieved EOS signal...", GST_OBJECT_NAME(message->src));
//g_main_loop_quit (app->loop); // TODO trigger plugin Close() upon error
break;
}
case GST_MESSAGE_STATE_CHANGED: {
GstState old_state, new_state;
gst_message_parse_state_changed(message, &old_state, &new_state, nullptr);
spdlog::info("gstreamer {} changed state from {} to {}", GST_OBJECT_NAME(message->src),
gst_element_state_get_name(old_state),
gst_element_state_get_name(new_state));
break;
}
case GST_MESSAGE_STREAM_STATUS: {
GstStreamStatusType streamStatus;
gst_message_parse_stream_status(message, &streamStatus, nullptr);
spdlog::info("gstreamer {} stream status {} ==> {}", GST_OBJECT_NAME(message->src),
gst_stream_status_string(streamStatus),
GST_OBJECT_NAME(message->src));
break;
}
case GST_MESSAGE_TAG: {
GstTagList *tags = nullptr;
gst_message_parse_tag(message, &tags);
gchar *txt = gst_tag_list_to_string(tags);
if (txt != nullptr) {
spdlog::info("gstreamer {} tag {}", GST_OBJECT_NAME(message->src), txt);
g_free(txt);
}
//gst_tag_list_foreach(tags, gst_print_one_tag, NULL);
if (tags != nullptr)
gst_tag_list_free(tags);
break;
}
default: {
spdlog::info("gstreamer {} message {} ==> {}", GST_OBJECT_NAME(message->src),
gst_message_type_get_name(GST_MESSAGE_TYPE(message)), GST_OBJECT_NAME(message->src));
break;
}
}
return TRUE;
}
bool VideoAudioCapture::initPipeline() {
if (!gstreamerInit()) {
spdlog::error("gst init error");
return false;
}
GError *err = nullptr;
buildLaunchStr();
mPipeline = gst_parse_launch(mLaunchStr.c_str(), &err);
if (err != nullptr) {
spdlog::error("gstDecoder -- failed to create pipeline");
spdlog::error(" ({})", err->message);
g_error_free(err);
return false;
}
GstPipeline *pipeline = GST_PIPELINE(mPipeline);
if (!pipeline) {
spdlog::error("gstDecoder -- failed to cast GstElement into GstPipeline");
return false;
}
// retrieve pipeline bus
mBus = gst_pipeline_get_bus(pipeline);
if (!mBus) {
spdlog::error("gstDecoder -- failed to retrieve GstBus from pipeline");
return false;
}
// add watch for messages (disabled when we poll the bus ourselves, instead of gmainloop)
//gst_bus_add_watch(mBus, (GstBusFunc)gst_message_print, NULL);
// get the appsrc
GstElement *appvideosinkElement = gst_bin_get_by_name(GST_BIN(pipeline), "video_sink");
GstElement *appaudiosinkElement = gst_bin_get_by_name(GST_BIN(pipeline),"audio_sink");
GstAppSink *appvideosink = GST_APP_SINK(appvideosinkElement);
GstAppSink *appaudiosink = GST_APP_SINK(appaudiosinkElement);
if (!appvideosinkElement || !appvideosink || !appaudiosinkElement || !appaudiosink) {
spdlog::error("gstDecoder -- failed to retrieve AppSink element from pipeline");
return false;
}
mVideoAppSink = appvideosink;
mAudioAppSink = appaudiosink;
// setup callbacks
GstAppSinkCallbacks video_cb;
GstAppSinkCallbacks audio_cb;
memset(&video_cb, 0, sizeof(GstAppSinkCallbacks));
memset(&audio_cb, 0, sizeof(GstAppSinkCallbacks));
video_cb.eos = onEOS_video;
video_cb.new_preroll = onPreroll_video; // disabled b/c preroll sometimes occurs during Close() and crashes
video_cb.new_sample = onBuffer_video;
audio_cb.eos = onEOS_audio;
audio_cb.new_preroll = onPreroll_audio;
audio_cb.new_sample = onBuffer_audio;
gst_app_sink_set_callbacks(mVideoAppSink, &video_cb, (void *) this, nullptr);
gst_app_sink_set_callbacks(mAudioAppSink, &audio_cb, (void *) this, nullptr);
return true;
}
bool VideoAudioCapture::buildLaunchStr() {
std::ostringstream ss;
//ss<< "filesrc location=/home/topeet/gst-workspace/samples/test.mp4 ! qtdemux name=demux demux.video_0 ! queue ! decodebin ! videoconvert ! video/x-raw ,format=(string)RGBA ! appsink name=mysink sync=false";
ss<<"filesrc location=/home/topeet/gst-workspace/samples/test.mp4 ! decodebin name=decoder decoder. ! queue ! videoconvert ! video/x-raw ,format=(string)RGBA ! appsink name=video_sink sync=true decoder. ! queue ! audioconvert ! appsink name=audio_sink sync=true";
mLaunchStr = ss.str();
return true;
}
#define release_return { gst_sample_unref(gstSample); return; }
void VideoAudioCapture::checkVideoBuffer() {
if (!mVideoAppSink) {
return;
}
// block waiting for the sample
GstSample *gstSample = gst_app_sink_pull_sample(mVideoAppSink);
if (!gstSample) {
spdlog::error("gstDecoder -- app_sink_pull_sample() returned NULL...");
return;
}
// retrieve sample caps
GstCaps *gstCaps = gst_sample_get_caps(gstSample);
if (!gstCaps) {
spdlog::error("gstDecoder -- gst_sample had NULL caps...");
release_return;
}
// retrieve the buffer from the sample
GstBuffer *gstBuffer = gst_sample_get_buffer(gstSample);
if (!gstBuffer) {
spdlog::error("gstDecoder -- gst_sample had NULL buffer...");
release_return;
}
const uint32_t width = mOptions.width;
const uint32_t height = mOptions.height;
// enqueue the buffer for color conversion
if( !mBufferManager->videoEnqueue(gstBuffer, gstCaps) )
{
spdlog::error("gstDecoder -- failed to enqueue buffer for color conversion");
release_return;
}
if ((width > 0 && width != mOptions.width) || (height > 0 && height != mOptions.height)) {
spdlog::warn("gstDecoder -- resolution changing from ({0}x{1}) to ({2}x{3})", width, height, mOptions.width,
mOptions.height);
// nvbufsurface: NvBufSurfaceCopy: buffer param mismatch
GstElement *vidconv = gst_bin_get_by_name(GST_BIN(mPipeline), "vidconv");
if (vidconv != nullptr) {
gst_element_set_state(vidconv, GST_STATE_NULL);
gst_element_set_state(vidconv, GST_STATE_PLAYING);
gst_object_unref(vidconv);
}
}
//mOptions.frameCount++;
release_return;
}
void VideoAudioCapture::checkAudioBuffer() {
if (!mAudioAppSink) {
return;
}
// block waiting for the sample
GstSample *gstSample = gst_app_sink_pull_sample(mAudioAppSink);
if (!gstSample) {
spdlog::error("gstDecoder -- app_sink_pull_sample() returned NULL...");
return;
}
// retrieve sample caps
GstCaps *gstCaps = gst_sample_get_caps(gstSample);
if (!gstCaps) {
spdlog::error("gstDecoder -- gst_sample had NULL caps...");
release_return;
}
// retrieve the buffer from the sample
GstBuffer *gstBuffer = gst_sample_get_buffer(gstSample);
if (!gstBuffer) {
spdlog::error("gstDecoder -- gst_sample had NULL buffer...");
release_return;
}
const uint32_t sample_rate = mOptions.Sampling_rate;
const uint32_t channels = mOptions.channels;
// enqueue the buffer for color conversion
if( !mBufferManager->audioEnqueue(gstBuffer, gstCaps) )
{
spdlog::error("gstDecoder -- failed to enqueue buffer for color conversion");
release_return;
}
if ((sample_rate > 0 && sample_rate != mOptions.Sampling_rate) || (channels > 0 && channels != mOptions.channels)) {
spdlog::warn("gstDecoder -- audio resolution changing from ({0}x{1}) to ({2}x{3})", sample_rate, channels, mOptions.Sampling_rate,
mOptions.channels);
}
//mOptions.frameCount++;
release_return;
}
void VideoAudioCapture::checkMsgBus() {
while (true) {
GstMessage *msg = gst_bus_pop(mBus);
if (!msg) {
break;
}
gst_message_print(mBus, msg, this);
gst_message_unref(msg);
}
}
GstFlowReturn VideoAudioCapture::onBuffer_video(_GstAppSink *sink, void *user_data) {
//printf(LOG_GSTREAMER "gstDecoder -- onBuffer_video()\n");
spdlog::info("gstDecoder -- onBuffer_video()");
if (!user_data){
return GST_FLOW_OK;
}
auto *dec = (VideoAudioCapture *) user_data;
dec->checkVideoBuffer();
dec->checkMsgBus();
return GST_FLOW_OK;
}
GstFlowReturn VideoAudioCapture::onBuffer_audio(_GstAppSink *sink, void *user_data) {
//printf(LOG_GSTREAMER "gstDecoder -- onBuffer_video()\n");
spdlog::info("gstDecoder -- onBuffer_audio");
if (!user_data){
return GST_FLOW_OK;
}
auto *dec = (VideoAudioCapture *) user_data;
dec->checkAudioBuffer();
dec->checkMsgBus();
return GST_FLOW_OK;
}
GstFlowReturn VideoAudioCapture::onPreroll_video(_GstAppSink *sink, void *user_data) {
spdlog::debug("gstDecoder -- onPreroll_video");
if (!user_data)
return GST_FLOW_OK;
auto *dec = (VideoAudioCapture *) user_data;
// onPreroll_video gets called sometimes, just pull and free the buffer
// otherwise the pipeline may hang during shutdown
GstSample *gstSample = gst_app_sink_pull_preroll(dec->mVideoAppSink);
if (!gstSample) {
spdlog::error("gstDecoder -- app_sink_pull_preroll() returned NULL...");
return GST_FLOW_OK;
}
gst_sample_unref(gstSample);
dec->checkMsgBus();
return GST_FLOW_OK;
}
GstFlowReturn VideoAudioCapture::onPreroll_audio(_GstAppSink *sink, void *user_data) {
spdlog::debug("gstDecoder -- onPreroll_audio()\n");
if (!user_data)
return GST_FLOW_OK;
auto *dec = (VideoAudioCapture *) user_data;
// onPreroll_video gets called sometimes, just pull and free the buffer
// otherwise the pipeline may hang during shutdown
GstSample *gstSample = gst_app_sink_pull_preroll(dec->mAudioAppSink);
if (!gstSample) {
spdlog::error("gstDecoder -- app_sink_pull_preroll() returned NULL...");
return GST_FLOW_OK;
}
gst_sample_unref(gstSample);
dec->checkMsgBus();
return GST_FLOW_OK;
}
bool VideoAudioCapture::isOpen() {
return mStreaming;
}
void VideoAudioCapture::onEOS_video(_GstAppSink *sink, void *user_data) {
spdlog::info("VideoAudioCapture -- onEOS_video\n");
}
void VideoAudioCapture::onEOS_audio(_GstAppSink *sink, void *user_data) {
spdlog::info("VideoAudioCapture -- onEOS_audio\n");
}
std::shared_ptr<std::vector<uint8_t>> VideoAudioCapture::Capture(imageFormat format, uint64_t timeout, int *status) {
// confirm the stream is open
if (!mStreaming || mEOS) {
spdlog::error("the stream is not open or EOS has been reached");
return nullptr;
}
// wait until a new frame is recieved
auto result = mBufferManager->Dequeue(format, timeout);
if (result == nullptr) {
spdlog::error("gstDecoder -- failed to dequeue buffer for capture");
}
return result;
}
bool VideoAudioCapture::Open() {
if (mStreaming) {
return true;
}
spdlog::info("opening gs_push for streaming, transitioning pipeline to GST_STATE_PLAYING");
const GstStateChangeReturn result = gst_element_set_state(mPipeline, GST_STATE_PLAYING);
if (result == GST_STATE_CHANGE_ASYNC) {
spdlog::debug("GST_STATE_CHANGE_ASYNC");
} else if (result != GST_STATE_CHANGE_SUCCESS) {
spdlog::error("gstCamera failed to set pipeline state to PLAYING ");
return false;
}
checkMsgBus();
usleep(100 * 1000);
checkMsgBus();
mStreaming = true;
mEOS= false;
return true;
}
#if 0
static void queryPipelineState( GstElement* pipeline )
{
GstState state = GST_STATE_VOID_PENDING;
GstState pending = GST_STATE_VOID_PENDING;
GstStateChangeReturn result = gst_element_get_state (pipeline,
&state, &pending, GST_CLOCK_TIME_NONE);
if( result == GST_STATE_CHANGE_FAILURE )
printf("GST_STATE_CHANGE_FAILURE\n");
printf("state - %s\n", gst_element_state_get_name(state));
printf("pending - %s\n", gst_element_state_get_name(pending));
}
#endif