forked from lu-zero/bmdtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmdplay.cpp
814 lines (701 loc) · 23.7 KB
/
bmdplay.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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/*
* Blackmagic Devices Decklink capture
* Copyright (c) 2013 Luca Barbato.
*
* This file is part of bmdtools.
*
* bmdtools is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* bmdtools 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with bmdtools; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <libgen.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include "libswscale/swscale.h"
}
#include "compat.h"
#include "Play.h"
#include "modes.h"
pthread_mutex_t sleepMutex;
pthread_cond_t sleepCond;
IDeckLinkConfiguration *deckLinkConfiguration;
AVFormatContext *ic;
AVFrame *avframe;
typedef struct PlayStream {
AVStream *st;
AVCodecContext *codec;
} PlayStream;
PlayStream audio;
PlayStream video;
static enum AVPixelFormat pix_fmt = AV_PIX_FMT_UYVY422;
static BMDPixelFormat pix = bmdFormat8BitYUV;
static int buffer = 2000 * 1000;
static int serial_fd = -1;
const unsigned long kAudioWaterlevel = 48000 / 4; /* small */
typedef struct PacketQueue {
AVPacketList *first_pkt, *last_pkt;
uint64_t nb_packets;
int size;
int abort_request;
pthread_mutex_t mutex;
pthread_cond_t cond;
} PacketQueue;
PacketQueue audioqueue;
PacketQueue videoqueue;
PacketQueue dataqueue;
struct SwsContext *sws;
static void packet_queue_init(PacketQueue *q)
{
memset(q, 0, sizeof(PacketQueue));
pthread_mutex_init(&q->mutex, NULL);
pthread_cond_init(&q->cond, NULL);
}
static void packet_queue_flush(PacketQueue *q)
{
AVPacketList *pkt, *pkt1;
pthread_mutex_lock(&q->mutex);
for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
pkt1 = pkt->next;
av_packet_unref(&pkt->pkt);
av_freep(&pkt);
}
q->last_pkt = NULL;
q->first_pkt = NULL;
q->nb_packets = 0;
q->size = 0;
pthread_mutex_unlock(&q->mutex);
}
static void packet_queue_end(PacketQueue *q)
{
packet_queue_flush(q);
pthread_mutex_lock(&q->mutex);
q->abort_request = -1;
pthread_cond_signal(&q->cond);
pthread_mutex_unlock(&q->mutex);
pthread_mutex_destroy(&q->mutex);
pthread_cond_destroy(&q->cond);
}
static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
{
AVPacketList *pkt1;
pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
if (!pkt1)
return -1;
pkt1->pkt = *pkt;
pkt1->next = NULL;
pthread_mutex_lock(&q->mutex);
if (!q->last_pkt)
q->first_pkt = pkt1;
else
q->last_pkt->next = pkt1;
q->last_pkt = pkt1;
q->nb_packets++;
if (q->nb_packets > 5000)
fprintf(stderr,
"%" PRId64 " storing %p, %s - is the input faster than realtime?\n",
q->nb_packets,
q,
q == &videoqueue ? "videoqueue" : "audioqueue");
q->size += pkt1->pkt.size + sizeof(*pkt1);
pthread_cond_signal(&q->cond);
pthread_mutex_unlock(&q->mutex);
return 0;
}
static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
{
AVPacketList *pkt1;
int ret;
pthread_mutex_lock(&q->mutex);
for (;; ) {
pkt1 = q->first_pkt;
if (pkt1) {
q->first_pkt = pkt1->next;
if (!q->first_pkt)
q->last_pkt = NULL;
q->nb_packets--;
if (q->nb_packets > 5000)
fprintf(stderr, "pulling %" PRId64 " from %p %s\n",
q->nb_packets,
q,
q == &videoqueue ? "videoqueue" : "audioqueue");
q->size -= pkt1->pkt.size + sizeof(*pkt1);
*pkt = pkt1->pkt;
av_free(pkt1);
ret = 1;
break;
} else if (!block) {
ret = 0;
break;
} else {
if (q->abort_request) {
ret = -1;
break;
}
pthread_cond_wait(&q->cond, &q->mutex);
}
}
pthread_mutex_unlock(&q->mutex);
return ret;
}
int64_t first_audio_pts = AV_NOPTS_VALUE;
int64_t first_video_pts = AV_NOPTS_VALUE;
int64_t first_pts = AV_NOPTS_VALUE;
int fill_me = 1;
void *fill_queues(void *unused)
{
AVPacket pkt;
AVStream *st;
int once = 0;
while (fill_me) {
int err = av_read_frame(ic, &pkt);
if (err) {
pthread_cond_signal(&sleepCond);
return NULL;
}
if (videoqueue.nb_packets > 1000) {
if (!once++)
fprintf(stderr, "Queue size %d problems ahead\n",
videoqueue.size);
}
st = ic->streams[pkt.stream_index];
switch (st->codecpar->codec_type) {
case AVMEDIA_TYPE_VIDEO:
if (pkt.pts != AV_NOPTS_VALUE) {
if (first_pts == AV_NOPTS_VALUE) {
first_pts = first_video_pts = pkt.pts;
if (audio.st) {
first_audio_pts =
av_rescale_q(pkt.pts, video.st->time_base,
audio.st->time_base);
}
}
pkt.pts -= first_video_pts;
}
packet_queue_put(&videoqueue, &pkt);
break;
case AVMEDIA_TYPE_AUDIO:
if (pkt.pts != AV_NOPTS_VALUE) {
if (first_pts == AV_NOPTS_VALUE) {
first_pts = first_audio_pts = pkt.pts;
first_video_pts =
av_rescale_q(pkt.pts, audio.st->time_base,
video.st->time_base);
}
pkt.pts -= first_audio_pts;
}
packet_queue_put(&audioqueue, &pkt);
break;
case AVMEDIA_TYPE_DATA:
packet_queue_put(&dataqueue, &pkt);
break;
default:
av_packet_unref(&pkt);
break;
}
}
return NULL;
}
void sigfunc(int signum)
{
pthread_cond_signal(&sleepCond);
}
int usage(int status)
{
HRESULT result;
IDeckLinkIterator *deckLinkIterator;
IDeckLink *deckLink;
int numDevices = 0;
fprintf(stderr,
"Usage: bmdplay -m <mode id> [OPTIONS]\n"
"\n"
" -m <mode id>:\n"
);
// Create an IDeckLinkIterator object to enumerate all DeckLink cards in the system
deckLinkIterator = CreateDeckLinkIteratorInstance();
if (deckLinkIterator == NULL) {
fprintf(
stderr,
"A DeckLink iterator could not be created. The DeckLink drivers may not be installed.\n");
return 1;
}
// Enumerate all cards in this system
while (deckLinkIterator->Next(&deckLink) == S_OK) {
BMDProbeString str;
// Increment the total number of DeckLink cards found
numDevices++;
if (numDevices > 1)
printf("\n\n");
// *** Print the model name of the DeckLink card
result = deckLink->GetModelName(&str);
if (result == S_OK) {
printf("-> %s (-C %d )\n\n",
ToStr(str),
numDevices - 1);
FreeStr(str);
}
print_output_modes(deckLink);
// Release the IDeckLink instance when we've finished with it to prevent leaks
deckLink->Release();
}
deckLinkIterator->Release();
// If no DeckLink cards were found in the system, inform the user
if (numDevices == 0)
printf("No Blackmagic Design devices were found.\n");
printf("\n");
fprintf(
stderr,
" -f <filename> Filename raw video will be written to\n"
" -C <num> Card number to be used\n"
" -b <num> Milliseconds of pre-buffering before playback (default = 2000 ms)\n"
" -p <pixel> PixelFormat Depth (8 or 10 - default is 8)\n"
" -S <port> Serial device (i.e: /dev/ttyS0, /dev/ttyUSB0)\n"
" -O <output> Output connection:\n"
" 1: Composite video + analog audio\n"
" 2: Components video + analog audio\n"
" 3: HDMI video + audio\n"
" 4: SDI video + audio\n\n");
return status;
}
int main(int argc, char *argv[])
{
Player generator;
int ch, ret;
int videomode = 2;
int connection = 0;
int camera = 0;
char *filename = NULL;
while ((ch = getopt(argc, argv, "?hs:f:a:m:n:F:C:O:b:p:S:")) != -1) {
switch (ch) {
case 'p':
switch (atoi(optarg)) {
case 8:
pix = bmdFormat8BitYUV;
pix_fmt = AV_PIX_FMT_UYVY422;
break;
case 10:
pix = bmdFormat10BitYUV;
pix_fmt = AV_PIX_FMT_YUV422P10;
break;
default:
fprintf(
stderr,
"Invalid argument: Pixel Format Depth must be either 8 bits or 10 bits\n");
return usage(1);
}
break;
case 'f':
filename = strdup(optarg);
break;
case 'm':
videomode = atoi(optarg);
break;
case 'O':
connection = atoi(optarg);
break;
case 'C':
camera = atoi(optarg);
break;
case 'b':
buffer = atoi(optarg) * 1000;
break;
case 'S':
serial_fd = open(optarg, O_RDWR | O_NONBLOCK);
break;
case '?':
case 'h':
return usage(0);
}
}
if (!filename)
return usage(1);
av_register_all();
ic = avformat_alloc_context();
avformat_open_input(&ic, filename, NULL, NULL);
avformat_find_stream_info(ic, NULL);
for (int i = 0; i < ic->nb_streams; i++) {
AVStream *st = ic->streams[i];
AVCodecParameters *par = st->codecpar;
AVCodec *codec = avcodec_find_decoder(par->codec_id);
switch (par->codec_type) {
case AVMEDIA_TYPE_AUDIO:
case AVMEDIA_TYPE_VIDEO:
if (codec) {
AVCodecContext *avctx = avcodec_alloc_context3(codec);
if (!avctx) {
av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
exit(1);
}
if (avcodec_parameters_to_context(avctx, par) < 0 ||
avcodec_open2(avctx, codec, NULL) < 0) {
avcodec_free_context(&avctx);
av_log(NULL, AV_LOG_ERROR, "Codec open failed\n");
exit(1);
}
if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
audio.st = st;
audio.codec = avctx;
}
if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
video.st = st;
video.codec = avctx;
}
} else {
fprintf(
stderr, "cannot find codecs for %s\n",
(par->codec_type ==
AVMEDIA_TYPE_AUDIO) ? "Audio" : "Video");
continue;
}
break;
default:
av_log(NULL, AV_LOG_VERBOSE, "Skipping stream %d\n", i);
}
}
if (!audio.st) {
av_log(NULL, AV_LOG_INFO,
"No audio stream found - bmdplay will just play video\n");
}
if (!video.st) {
av_log(NULL, AV_LOG_ERROR,
"No video stream found - bmdplay will close now.\n");
return 1;
}
av_dump_format(ic, 0, filename, 0);
sws = sws_getContext(video.st->codecpar->width,
video.st->codecpar->height,
(AVPixelFormat)video.st->codecpar->format,
video.st->codecpar->width,
video.st->codecpar->height,
pix_fmt,
SWS_BILINEAR, NULL, NULL, NULL);
signal(SIGINT, sigfunc);
pthread_mutex_init(&sleepMutex, NULL);
pthread_cond_init(&sleepCond, NULL);
free(filename);
ret = generator.Init(videomode, connection, camera);
avformat_close_input(&ic);
fprintf(stderr, "video %" PRId64 " audio %" PRId64 "\n",
videoqueue.nb_packets,
audioqueue.nb_packets);
return ret;
}
Player::Player()
{
m_audioSampleRate = bmdAudioSampleRate48kHz;
m_running = false;
m_outputSignal = kOutputSignalDrop;
}
bool Player::Init(int videomode, int connection, int camera)
{
// Initialize the DeckLink API
IDeckLinkIterator *deckLinkIterator = CreateDeckLinkIteratorInstance();
HRESULT result;
int i = 0;
if (!deckLinkIterator) {
fprintf(stderr,
"This application requires the DeckLink drivers installed.\n");
goto bail;
}
if (audio.st) {
m_audioSampleDepth =
av_get_exact_bits_per_sample(audio.codec->codec_id);
switch (audio.codec->channels) {
case 2:
case 8:
case 16:
break;
default:
fprintf(stderr,
"%d channels not supported, please use 2, 8 or 16\n",
audio.codec->channels);
goto bail;
}
switch (m_audioSampleDepth) {
case 16:
case 32:
break;
default:
fprintf(stderr, "%lubit audio not supported use 16bit or 32bit\n",
m_audioSampleDepth);
}
}
do
result = deckLinkIterator->Next(&m_deckLink);
while (i++ < camera);
if (result != S_OK) {
fprintf(stderr, "No DeckLink PCI cards found\n");
goto bail;
}
// Obtain the audio/video output interface (IDeckLinkOutput)
if (m_deckLink->QueryInterface(IID_IDeckLinkOutput,
(void **)&m_deckLinkOutput) != S_OK)
goto bail;
result = m_deckLink->QueryInterface(IID_IDeckLinkConfiguration,
(void **)&deckLinkConfiguration);
if (result != S_OK) {
fprintf(
stderr,
"Could not obtain the IDeckLinkConfiguration interface - result = %08x\n",
result);
goto bail;
}
//XXX make it generic
switch (connection) {
case 1:
DECKLINK_SET_VIDEO_CONNECTION(bmdVideoConnectionComposite);
DECKLINK_SET_AUDIO_CONNECTION(bmdAudioConnectionAnalog);
break;
case 2:
DECKLINK_SET_VIDEO_CONNECTION(bmdVideoConnectionComponent);
DECKLINK_SET_AUDIO_CONNECTION(bmdAudioConnectionAnalog);
break;
case 3:
DECKLINK_SET_VIDEO_CONNECTION(bmdVideoConnectionHDMI);
DECKLINK_SET_AUDIO_CONNECTION(bmdAudioConnectionEmbedded);
break;
case 4:
DECKLINK_SET_VIDEO_CONNECTION(bmdVideoConnectionSDI);
DECKLINK_SET_AUDIO_CONNECTION(bmdAudioConnectionEmbedded);
break;
default:
// do not change it
break;
}
// Provide this class as a delegate to the audio and video output interfaces
m_deckLinkOutput->SetScheduledFrameCompletionCallback(this);
m_deckLinkOutput->SetAudioCallback(this);
avframe = av_frame_alloc();
packet_queue_init(&audioqueue);
packet_queue_init(&videoqueue);
packet_queue_init(&dataqueue);
pthread_t th;
pthread_create(&th, NULL, fill_queues, NULL);
usleep(buffer); // You can add the microseconds you need for pre-buffering before start playing
// Start playing
StartRunning(videomode);
pthread_mutex_lock(&sleepMutex);
pthread_cond_wait(&sleepCond, &sleepMutex);
pthread_mutex_unlock(&sleepMutex);
fill_me = 0;
fprintf(stderr, "Exiting, cleaning up\n");
packet_queue_end(&audioqueue);
packet_queue_end(&videoqueue);
bail:
if (m_running == true) {
StopRunning();
} else {
// Release any resources that were partially allocated
if (m_deckLinkOutput != NULL) {
m_deckLinkOutput->Release();
m_deckLinkOutput = NULL;
}
//
if (m_deckLink != NULL) {
m_deckLink->Release();
m_deckLink = NULL;
}
}
if (deckLinkIterator != NULL)
deckLinkIterator->Release();
return true;
}
IDeckLinkDisplayMode *Player::GetDisplayModeByIndex(int selectedIndex)
{
// Populate the display mode combo with a list of display modes supported by the installed DeckLink card
IDeckLinkDisplayModeIterator *displayModeIterator;
IDeckLinkDisplayMode *deckLinkDisplayMode;
IDeckLinkDisplayMode *selectedMode = NULL;
int index = 0;
if (m_deckLinkOutput->GetDisplayModeIterator(&displayModeIterator) != S_OK)
goto bail;
while (displayModeIterator->Next(&deckLinkDisplayMode) == S_OK) {
BMDProbeString str;
if (deckLinkDisplayMode->GetName(&str) == S_OK) {
if (index == selectedIndex) {
printf("Selected mode: %s\n\n\n", ToStr(str));
selectedMode = deckLinkDisplayMode;
FreeStr(str);
goto bail;
}
}
index++;
}
bail:
displayModeIterator->Release();
return selectedMode;
}
void Player::StartRunning(int videomode)
{
IDeckLinkDisplayMode *videoDisplayMode = NULL;
unsigned long audioSamplesPerFrame;
// Get the display mode for 1080i 59.95
videoDisplayMode = GetDisplayModeByIndex(videomode);
if (!videoDisplayMode)
return;
m_frameWidth = videoDisplayMode->GetWidth();
m_frameHeight = videoDisplayMode->GetHeight();
videoDisplayMode->GetFrameRate(&m_frameDuration, &m_frameTimescale);
// Set the video output mode
if (m_deckLinkOutput->EnableVideoOutput(videoDisplayMode->GetDisplayMode(),
bmdVideoOutputFlagDefault) !=
S_OK) {
fprintf(stderr, "Failed to enable video output\n");
return;
}
// Set the audio output mode
if (audio.st) {
if (m_deckLinkOutput->EnableAudioOutput(bmdAudioSampleRate48kHz,
m_audioSampleDepth,
audio.codec->channels,
bmdAudioOutputStreamTimestamped) !=
S_OK) {
fprintf(stderr, "Failed to enable audio output\n");
return;
}
for (unsigned i = 0; i < 10; i++)
ScheduleNextFrame(true);
// Begin audio preroll. This will begin calling our audio callback, which will start the DeckLink output stream.
// m_audioBufferOffset = 0;
if (m_deckLinkOutput->BeginAudioPreroll() != S_OK) {
fprintf(stderr, "Failed to begin audio preroll\n");
return;
}
} else {
for (unsigned i = 0; i < 10; i++)
ScheduleNextFrame(true);
m_deckLinkOutput->StartScheduledPlayback(0, 100, 1.0);
}
m_running = true;
return;
}
void Player::StopRunning()
{
// Stop the audio and video output streams immediately
m_deckLinkOutput->StopScheduledPlayback(0, NULL, 0);
//
m_deckLinkOutput->DisableAudioOutput();
m_deckLinkOutput->DisableVideoOutput();
}
void Player::ScheduleNextFrame(bool prerolling)
{
AVPacket pkt;
void *frame;
int ret;
if (serial_fd > 0 && packet_queue_get(&dataqueue, &pkt, 0)) {
if (pkt.data[0] != ' '){
fprintf(stderr,"written %.*s \n", pkt.size, pkt.data);
write(serial_fd, pkt.data, pkt.size);
}
av_packet_unref(&pkt);
}
if (packet_queue_get(&videoqueue, &pkt, 0) < 0)
return;
IDeckLinkMutableVideoFrame *videoFrame;
m_deckLinkOutput->CreateVideoFrame(m_frameWidth,
m_frameHeight,
m_frameWidth * 2,
pix,
bmdFrameFlagDefault,
&videoFrame);
videoFrame->GetBytes(&frame);
avcodec_send_packet(video.codec, &pkt);
// TODO: support receiving multiple frames
ret = avcodec_receive_frame(video.codec, avframe);
if (ret >= 0) {
uint8_t *data[4];
int linesize[4];
av_image_fill_arrays(data, linesize, (uint8_t *)frame,
pix_fmt, m_frameWidth, m_frameHeight, 1);
sws_scale(sws, avframe->data, avframe->linesize, 0, avframe->height,
data, linesize);
if (m_deckLinkOutput->ScheduleVideoFrame(videoFrame,
pkt.pts *
video.st->time_base.num,
pkt.duration *
video.st->time_base.num,
video.st->time_base.den) !=
S_OK)
fprintf(stderr, "Error scheduling frame\n");
}
videoFrame->Release();
av_packet_unref(&pkt);
}
void Player::WriteNextAudioSamples()
{
uint32_t samplesWritten = 0;
AVPacket pkt = { 0 };
unsigned int bufferedSamples;
int got_frame = 0;
int i;
int bytes_per_sample =
av_get_bytes_per_sample(audio.codec->sample_fmt) *
audio.codec->channels;
int samples, off = 0;
m_deckLinkOutput->GetBufferedAudioSampleFrameCount(&bufferedSamples);
if (bufferedSamples > kAudioWaterlevel)
return;
if (!packet_queue_get(&audioqueue, &pkt, 0))
return;
samples = pkt.size / bytes_per_sample;
do {
if (m_deckLinkOutput->ScheduleAudioSamples(pkt.data +
off * bytes_per_sample,
samples,
pkt.pts + off,
audio.st->time_base.den / audio.st->time_base.num,
&samplesWritten) != S_OK)
fprintf(stderr, "error writing audio sample\n");
samples -= samplesWritten;
off += samplesWritten;
} while (samples > 0);
av_packet_unref(&pkt);
}
/************************* DeckLink API Delegate Methods *****************************/
HRESULT Player::ScheduledFrameCompleted(IDeckLinkVideoFrame *completedFrame,
BMDOutputFrameCompletionResult result)
{
if (fill_me)
ScheduleNextFrame(false);
return S_OK;
}
HRESULT Player::ScheduledPlaybackHasStopped()
{
return S_OK;
}
HRESULT Player::RenderAudioSamples(bool preroll)
{
if (audio.st) {
// Provide further audio samples to the DeckLink API until our preferred buffer waterlevel is reached
WriteNextAudioSamples();
if (preroll) {
// Start audio and video output
m_deckLinkOutput->StartScheduledPlayback(0, 100, 1.0);
}
}
return S_OK;
}