forked from mapmapteam/mapmap
/
MediaImpl.cpp
770 lines (672 loc) · 21.9 KB
/
MediaImpl.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
* MediaImpl.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
* (c) 2012 Jean-Sebastien Senecal
* (c) 2004 Mathieu Guindon, Julien Keable
* Based on code from Drone http://github.com/sofian/drone
* Based on code from the GStreamer Tutorials http://docs.gstreamer.com/display/GstSDK/Tutorials
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MediaImpl.h"
#include <cstring>
#include <iostream>
// -------- private implementation of VideoImpl -------
bool MediaImpl::hasVideoSupport()
{
static bool did_print_gst_version = false;
if (! did_print_gst_version)
{
qDebug() << "Using GStreamer version " <<
GST_VERSION_MAJOR << "." <<
GST_VERSION_MINOR << "." <<
GST_VERSION_MICRO;
did_print_gst_version = true;
}
// TODO: actually check if we have it
return true;
}
int MediaImpl::getWidth() const
{
return _width;
}
int MediaImpl::getHeight() const
{
return _height;
}
const uchar* MediaImpl::getBits() const
{
return _data;
}
void MediaImpl::build()
{
qDebug() << "Building video impl";
if (!loadMovie(_uri))
{
qDebug() << "Cannot load movie " << _currentMovie << ".";
}
}
MediaImpl::~MediaImpl()
{
freeResources();
/* _data points to gstreamer-allocated data, we don't manage it ourselves */
//if (_data)
// free(_data);
}
bool MediaImpl::_videoPull()
{
// qDebug() << "video pull" << endl;
GstSample *sample = NULL;
GstStructure *structure = NULL;
GstCaps* caps = NULL;
GstBuffer *buffer = NULL;
// Retrieve the sample
sample = queue_input_buf.get();
if (sample == NULL)
{
// Either means we are not playing or we have reached EOS.
return false;
}
else
{
caps = gst_sample_get_caps(sample);
structure = gst_caps_get_structure(caps, 0);
buffer = gst_sample_get_buffer(sample);
int width = 640;
int height = 480;
int bpp = 32;
int depth = 32;
gst_structure_get_int(structure, "width", &width);
gst_structure_get_int(structure, "height", &height);
// TODO: use gst_video_info_from_caps if we want to support many different formats
// otherwise, since we set the caps ourselves, we can assume bpp is 32 and depth too.
_width = width;
_height = height;
int size = _width * _height;
// video->resize(width, height);
// qDebug() << gst_structure_to_string(capsStruct) << endl;
// qDebug() << width << "x" << height << "=" << width*height << "(" << width*height*4 << "," << width*height*3 << ")" << endl;
// qDebug() << "bpp: " << bpp << " depth: " << depth << endl;
// qDebug() << "Buffer size: " << GST_BUFFER_SIZE(buffer) << endl;
GstMapInfo map;
if (gst_buffer_map(buffer, &map, GST_MAP_READ))
{
// For debugging:
//gst_util_dump_mem(map.data, map.size)
_data = map.data;
gst_buffer_unmap(buffer, &map);
if(this->_frame != NULL)
queue_output_buf.put(this->_frame);
_frame = sample;
}
return true;
}
}
bool MediaImpl::_eos() const
{
if (_movieReady)
{
Q_ASSERT( _videoSink );
// Q_ASSERT( _audioSink );
gboolean videoEos;
// gboolean audioEos;
g_object_get (G_OBJECT (_videoSink), "eos", &videoEos, NULL);
// g_object_get (G_OBJECT (_audioSink), "eos", &audioEos, NULL);
return (bool) (videoEos /*|| audioEos*/);
}
else
return false;
}
//void VideoImpl::_init()
//{
// _audioHasNewBuffer = false;
// _videoHasNewBuffer = false;
//
// _terminate = false;
// _seekEnabled = false;
//
// _movieReady=true;
//
// // Stop sleeping the video output.
// _VIDEO_OUT->sleeping(false);
// _AUDIO_OUT->sleeping(false);
//}
GstFlowReturn MediaImpl::gstNewSampleCallback(GstElement*, MediaImpl *p)
{
GstSample *sample;
sample = gst_app_sink_pull_sample(GST_APP_SINK(p->_videoSink));
//g_signal_emit_by_name (p->_videoSink, "pull-sample", &sample);
p->get_queue_input_buf()->put(sample);
if (p->get_queue_output_buf()->size() > 1) {
sample = p->get_queue_output_buf()->get();
gst_sample_unref(sample);
}
return GST_FLOW_OK;
}
MediaImpl::MediaImpl(const QString uri) :
_currentMovie(""),
_bus(NULL),
_pipeline(NULL),
_source(NULL),
//_audioQueue(NULL),
//_audioConvert(NULL),
//_audioResample(NULL),
_videoQueue(NULL),
_videoConvert(NULL),
_videoColorSpace(NULL),
_audioSink(NULL),
_videoSink(NULL),
_frame(NULL),
_width(640),
_height(480),
_data(NULL),
//_audioBufferAdapter(NULL),
_seekEnabled(false),
//_audioNewBufferCounter(0),
_movieReady(false),
_uri(uri)
{
if (uri != "")
loadMovie(uri);
// addPlug(_VIDEO_OUT = new PlugOut<VideoRGBAType>(this, "ImgOut", false));
// addPlug(_AUDIO_OUT = new PlugOut<SignalType>(this, "AudioOut", false));
//
// addPlug(_FINISH_OUT = new PlugOut<ValueType>(this, "FinishOut", false));
//
// QList<AbstractPlug*> atLeastOneOfThem;
// atLeastOneOfThem.push_back(_VIDEO_OUT);
// atLeastOneOfThem.push_back(_AUDIO_OUT);
// setPlugAtLeastOneNeeded(atLeastOneOfThem);
//
// addPlug(_RESET_IN = new PlugIn<ValueType>(this, "Reset", false, new ValueType(0, 0, 1)));
// addPlug(_MOVIE_IN = new PlugIn<StringType>(this, "Movie", false));
//
// //_settings.add(Property::FILENAME, SETTING_FILENAME)->valueStr("");
//
// _VIDEO_OUT->sleeping(true);
// _AUDIO_OUT->sleeping(true);
//
// // Crease audio buffer handler.
// _audioBufferAdapter = gst_adapter_new();
}
void MediaImpl::unloadMovie()
{
// Free allocated resources.
freeResources();
// Reset flags.
// _audioNewBufferCounter = 0;
_terminate = false;
_seekEnabled = false;
_setReady(false);
// Unsynch.
// NOTE: I commented this out, it was in Drone, most probably useless but who knows.
// unSynch(); // XXX: I'm not sure why we are doing this...
}
void MediaImpl::freeResources()
{
// Free resources.
if (_bus)
{
gst_object_unref (_bus);
_bus = NULL;
}
if (_pipeline)
{
gst_element_set_state (_pipeline, GST_STATE_NULL);
gst_object_unref (_pipeline);
_pipeline = NULL;
}
_source = NULL;
// _audioQueue = NULL;
// _audioConvert = NULL;
// _audioResample = NULL;
_videoQueue = NULL;
_videoConvert = NULL;
_videoColorSpace = NULL;
_audioSink = NULL;
_videoSink = NULL;
_frame = NULL;
_padHandlerData = GstPadHandlerData();
// Flush buffers in adapter.
// gst_adapter_clear(_audioBufferAdapter);
}
void MediaImpl::resetMovie()
{
// TODO: Check if we can still seek when we reach EOS. It seems like it's then impossible and we
// have to reload but it seems weird so we should check.
if (!_eos() && _seekEnabled)
{
qDebug() << "Seeking at position 0.";
gst_element_seek_simple (_pipeline, GST_FORMAT_TIME,
(GstSeekFlags) (GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT), 0);
this->_frame = NULL;
_setReady(true);
}
else
{
// Just reload movie.
qDebug() << "Reloading the movie" << _seekEnabled;
_currentMovie = "";
loadMovie(_uri);
}
}
bool MediaImpl::loadMovie(QString filename)
{
_uri = filename;
qDebug() << "Opening movie: " << filename << ".";
this->_frame = NULL;
// Free previously allocated structures
unloadMovie();
//_firstFrameTime=_formatContext->start_time;
// Initialize GStreamer.
gst_init (NULL, NULL);
GstElement *capsFilter = NULL;
GstElement *videoScale = NULL;
// Create the elements.
_source = gst_element_factory_make ("uridecodebin", "source");
// _audioQueue = gst_element_factory_make ("queue", "aqueue");
// _audioConvert = gst_element_factory_make ("audioconvert", "aconvert");
// _audioResample = gst_element_factory_make ("audioresample", "aresample");
// _audioSink = gst_element_factory_make ("appsink", "asink");
//
_videoQueue = gst_element_factory_make ("queue", "vqueue");
_videoColorSpace = gst_element_factory_make ("videoconvert", "vcolorspace");
videoScale = gst_element_factory_make ("videoscale", "videoscale0");
capsFilter = gst_element_factory_make ("capsfilter", "capsfilter0");
_videoSink = gst_element_factory_make ("appsink", "vsink");
// Prepare handler data.
// _padHandlerData.audioToConnect = _audioQueue;
_padHandlerData.videoToConnect = _videoQueue;
_padHandlerData.videoSink = _videoSink;
//_padHandlerData.audioIsConnected = false;
_padHandlerData.videoIsConnected = false;
// _newAudioBufferHandlerData.audioSink = _audioSink;
// _newAudioBufferHandlerData.audioBufferAdapter = _audioBufferAdapter;
// Create the empty pipeline.
_pipeline = gst_pipeline_new ( "video-source-pipeline" );
if (!_pipeline || !_source ||
// !_audioQueue || !_audioConvert || !_audioResample || !_audioSink ||
!_videoQueue || !_videoColorSpace || ! videoScale || ! capsFilter || ! _videoSink)
{
g_printerr ("Not all elements could be created.\n");
unloadMovie();
return -1;
}
// Build the pipeline. Note that we are NOT linking the source at this
// point. We will do it later.
gst_bin_add_many (GST_BIN (_pipeline), _source,
// _audioQueue, _audioConvert, _audioResample, _audioSink,
_videoQueue, _videoColorSpace, videoScale, capsFilter, _videoSink, NULL);
// if (!gst_element_link_many(_audioQueue, _audioConvert, _audioResample, _audioSink, NULL)) {
// g_printerr ("Audio elements could not be linked.\n");
// unloadMovie();
// return false;
// }
if (!gst_element_link_many (_videoQueue, _videoColorSpace, capsFilter, videoScale, _videoSink, NULL)) {
g_printerr ("Video elements could not be linked.\n");
unloadMovie();
return false;
}
// Process URI.
gchar* uri = (gchar*) filename.toUtf8().constData();
if (!gst_uri_is_valid(uri))
{
// Try to convert filename to URI.
GError* error = NULL;
uri = gst_filename_to_uri(uri, &error);
if (error) {
qDebug() << "Filename to URI error: " << error->message;
g_error_free(error);
gst_object_unref (uri);
freeResources();
return false;
}
}
// Set URI to be played.
qDebug() << "URI for uridecodebin: " << uri;
// FIXME: sometimes it's just the path to the directory that is given, not the file itself.
g_object_set (_source, "uri", uri, NULL);
// Connect to the pad-added signal
g_signal_connect (_source, "pad-added", G_CALLBACK (MediaImpl::gstPadAddedCallback), &_padHandlerData);
// Configure audio appsink.
// TODO: change from mono to stereo
// gchar* audioCapsText = g_strdup_printf ("audio/x-raw-float,channels=1,rate=%d,signed=(boolean)true,width=%d,depth=%d,endianness=BYTE_ORDER",
// Engine::signalInfo().sampleRate(), (int)(sizeof(Signal_T)*8), (int)(sizeof(Signal_T)*8) );
// GstCaps* audioCaps = gst_caps_from_string (audioCapsText);
// g_object_set (_audioSink, "emit-signals", TRUE,
// "caps", audioCaps,
//// "max-buffers", 1, // only one buffer (the last) is maintained in the queue
//// "drop", TRUE, // ... other buffers are dropped
// NULL);
// g_signal_connect (_audioSink, "new-buffer", G_CALLBACK (VideoImpl::gstNewAudioBufferCallback), &_newAudioBufferHandlerData);
// gst_caps_unref (audioCaps);
// g_free (audioCapsText);
// Configure video appsink.
// GstCaps *videoCaps = gst_caps_from_string ("video/x-raw-rgb");
GstCaps *videoCaps = gst_caps_from_string ("video/x-raw,format=RGBA");
g_object_set (capsFilter, "caps", videoCaps, NULL);
g_object_set (_videoSink, "emit-signals", TRUE,
"max-buffers", 1, // only one buffer (the last) is maintained in the queue
"drop", TRUE, // ... other buffers are dropped
NULL);
g_signal_connect (_videoSink, "new-sample", G_CALLBACK (MediaImpl::gstNewSampleCallback), this);
gst_caps_unref (videoCaps);
// Listen to the bus.
_bus = gst_element_get_bus (_pipeline);
// Start playing.
if (!setPlayState(true))
return false;
qDebug() << "Pipeline started.";
//_movieReady = true;
return true;
}
bool MediaImpl::runVideo() {
// if (!_VIDEO_OUT->connected())
// return;
if (!_preRun())
return false;
bool bitsChanged = false;
if (queue_input_buf.size() > 0) {
// Pull video.
if (!_videoPull())
{
_setFinished(true);
// _FINISH_OUT->type()->setValue(1.0f);
// _VIDEO_OUT->sleeping(true);
}
else
{
bitsChanged = true;
// _VIDEO_OUT->sleeping(false);
}
//std::cout << "VideoImpl::runVideo: read frame #" << _videoNewBufferCounter << std::endl;
}
/* TODO: This causes the texture to be loaded always in Mapper.cpp . The
* problem if this is not set is: When we have more than one shape, a
* shape that has a new buffer coming in will overdraw the old buffer of the
* shape on top. This implementation seems to be fast enough that
* _videoNewBufferCounter is often 1 or 0. If bitsChanged is often switching
* between true and false (as in the case described above), than the shape
* textures will appear to be flickering/alternating. Maybe a better solution is
* needed (in the GL layer or here?)*/
else
bitsChanged = true;
_postRun();
return bitsChanged;
}
bool MediaImpl::setPlayState(bool play)
{
if (_pipeline == NULL)
return false;
GstStateChangeReturn ret = gst_element_set_state (_pipeline, (play ? GST_STATE_PLAYING : GST_STATE_PAUSED));
if (ret == GST_STATE_CHANGE_FAILURE)
{
qDebug() << "Unable to set the pipeline to the playing state.";
unloadMovie();
return false;
}
else
{
_setReady(play);
return true;
}
}
//void VideoImpl::runAudio() {
//
// if (!_AUDIO_OUT->connected())
// return;
//
// if (!_preRun())
// return;
//
// unsigned int blockByteSize = Engine::signalInfo().blockSize()*sizeof(Signal_T);
// if (gst_adapter_available(_audioBufferAdapter) >= blockByteSize )
// {
// // Copy block of data to audio output.
// gst_adapter_copy(_audioBufferAdapter, (guint8*)_AUDIO_OUT->type()->data(), 0, blockByteSize);
// gst_adapter_flush (_audioBufferAdapter, blockByteSize);
//
// _AUDIO_OUT->sleeping(false);
// }
// else
// {
// _FINISH_OUT->type()->setValue(1.0f);
// _AUDIO_OUT->sleeping(true);
// }
//
// _postRun();
//}
bool MediaImpl::_preRun()
{
// Check for end-of-stream or terminate.
if (_eos() || _terminate)
{
_setFinished(true);
resetMovie();
// _FINISH_OUT->type()->setValue(1.0f);
// _VIDEO_OUT->sleeping(true);
// _AUDIO_OUT->sleeping(true);
//
// if (_audioBufferAdapter != NULL)
// gst_adapter_clear(_audioBufferAdapter);
}
else
_setFinished(false);
// _FINISH_OUT->type()->setValue(0.0f);
// if (_RESET_IN->type()->boolValue())
// resetMovie();
if (!_movieReady ||
!_padHandlerData.isConnected())
return false;
return true;
}
void MediaImpl::_postRun()
{
// Parse message.
if (_bus != NULL)
{
GstMessage *msg = gst_bus_timed_pop_filtered(
_bus, 0,
(GstMessageType) (GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
if (msg != NULL) {
GError *err;
gchar *debug_info;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error(msg, &err, &debug_info);
g_printerr("Error received from element %s: %s\n",
GST_OBJECT_NAME (msg->src), err->message);
g_printerr("Debugging information: %s\n",
debug_info ? debug_info : "none");
g_clear_error(&err);
g_free(debug_info);
_terminate = true;
// _finish();
break;
case GST_MESSAGE_EOS:
g_print("End-Of-Stream reached.\n");
// _terminate = true;
// _finish();
break;
case GST_MESSAGE_STATE_CHANGED:
// We are only interested in state-changed messages from the pipeline.
if (GST_MESSAGE_SRC (msg) == GST_OBJECT (_pipeline))
{
GstState oldState, newState, pendingState;
gst_message_parse_state_changed(msg, &oldState, &newState,
&pendingState);
g_print("Pipeline state for movie %s changed from %s to %s:\n",
_currentMovie.toUtf8().constData(),
gst_element_state_get_name(oldState),
gst_element_state_get_name(newState));
// if (oldState == GST_STATE_PAUSED && newState == GST_STATE_READY)
// gst_adapter_clear(_audioBufferAdapter);
if (newState == GST_STATE_PLAYING)
{
// Check if seeking is allowed.
gint64 start, end;
GstQuery *query = gst_query_new_seeking (GST_FORMAT_TIME);
if (gst_element_query (_pipeline, query))
{
gst_query_parse_seeking (query, NULL, (gboolean*)&_seekEnabled, &start, &end);
if (_seekEnabled)
{
g_print ("Seeking is ENABLED from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT "\n",
GST_TIME_ARGS (start), GST_TIME_ARGS (end));
}
else
{
g_print ("Seeking is DISABLED for this stream.\n");
}
}
else
{
g_printerr ("Seeking query failed.");
}
gst_query_unref (query);
}
}
break;
default:
// We should not reach here.
g_printerr("Unexpected message received.\n");
break;
}
gst_message_unref(msg);
}
}
}
void MediaImpl::_setReady(bool ready)
{
_movieReady = ready;
// _VIDEO_OUT->sleeping(!ready);
// _AUDIO_OUT->sleeping(!ready);
}
void MediaImpl::_setFinished(bool finished) {
// qDebug() << "Clip " << (finished ? "finished" : "not finished");
}
void MediaImpl::gstPadAddedCallback(GstElement *src, GstPad *newPad, MediaImpl::GstPadHandlerData* data) {
g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (newPad), GST_ELEMENT_NAME (src));
bool isAudio = false;
GstPad *sinkPad = NULL;
// Check the new pad's type.
GstCaps *newPadCaps = gst_pad_query_caps (newPad, NULL);
GstStructure *newPadStruct = gst_caps_get_structure (newPadCaps, 0);
const gchar *newPadType = gst_structure_get_name (newPadStruct);
g_print("Structure is %s\n", gst_structure_to_string(newPadStruct));
if (g_str_has_prefix (newPadType, "audio/x-raw"))
{
sinkPad = gst_element_get_static_pad (data->audioToConnect, "sink");
isAudio = true;
}
else if (g_str_has_prefix (newPadType, "video/x-raw"))
{
sinkPad = gst_element_get_static_pad (data->videoToConnect, "sink");
isAudio = false;
}
else
{
g_print (" It has type '%s' which is not raw audio/video. Ignoring.\n", newPadType);
goto exit;
}
// If our converter is already linked, we have nothing to do here.
if (gst_pad_is_linked (sinkPad))
{
// Best prefixes.
if (g_str_has_prefix (newPadType, "audio/x-raw-float") ||
g_str_has_prefix (newPadType, "video/x-raw-int") )
{
g_print (" Found a better pad.\n");
GstPad* oldPad = gst_pad_get_peer(sinkPad);
gst_pad_unlink(oldPad, sinkPad);
g_object_unref(oldPad);
}
else
{
g_print (" We are already linked. Ignoring.\n");
goto exit;
}
}
// Attempt the link
if (GST_PAD_LINK_FAILED (gst_pad_link (newPad, sinkPad))) {
g_print (" Type is '%s' but link failed.\n", newPadType);
goto exit;
} else {
g_print (" Link succeeded (type '%s').\n", newPadType);
if (isAudio)
{
//data->audioIsConnected = true;
}
else
{
data->videoIsConnected = true;
}
}
exit:
// Unreference the new pad's caps, if we got them.
if (newPadCaps != NULL)
gst_caps_unref (newPadCaps);
// Unreference the sink pad.
if (sinkPad != NULL)
gst_object_unref (sinkPad);
}
//void VideoImpl::gstNewAudioBufferCallback(GstElement *sink, GstNewAudioBufferHandlerData *data) {
// GstBuffer *buffer = NULL;
//
// // Retrieve the buffer.
// // TODO: we should pull ALL buffers and add them to the adapter
// g_signal_emit_by_name (data->audioSink, "pull-buffer", &buffer);
//
// if (buffer)
// {
// ASSERT_WARNING_MESSAGE( ! GST_BUFFER_IS_DISCONT(buffer), "Discontinuity detected in audio buffer." );
//
//// int blockSize = 2;
//// int sampleRate = 1;
//// int channels = 0;
//// int width = 0;
//// GstCaps* caps = GST_BUFFER_CAPS(buffer);
//// GstStructure *capsStruct = gst_caps_get_structure (caps, 0);
////
//// gst_structure_get_int(capsStruct, "rate", &sampleRate);
//// gst_structure_get_int(capsStruct, "channels", &channels);
//// gst_structure_get_int(capsStruct, "width", &width);
//
//// qDebug() << "rate = " << sampleRate << " channels = " << channels << " width = " << width << endl;
//// unsigned int blockByteSize = Engine::signalInfo().blockSize() * sizeof(Signal_T);
//
//// qDebug() << "bufsize: "<< GST_BUFFER_SIZE(buffer) <<
//// " / adaptersize: " << gst_adapter_available(data->audioBufferAdapter) << endl;
//
// // Add buffer to the adapter.
// gst_adapter_push(data->audioBufferAdapter, buffer);
// // qDebug() << " .. after push = : "<< gst_adapter_available(_audioBufferAdapter);
//
// // NOTE: no need to unref the buffer here because the buffer was given away with the
// // call to gst_adapter_push()
// //gst_buffer_unref (buffer);
// }
//}
void MediaImpl::internalPrePlay()
{
// Start/resume playback.
setPlayState(true);
}
void MediaImpl::internalPostPlay()
{
// Pause playback.
setPlayState(false);
}