-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioIO.cpp
881 lines (750 loc) · 22.2 KB
/
AudioIO.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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
#include "AudioIO.h"
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <csignal>
using namespace std;
static std::string AVerr2str(int code)
{
char astr[AV_ERROR_MAX_STRING_SIZE] = { 0 };
av_make_error_string(astr, AV_ERROR_MAX_STRING_SIZE, code);
return string(astr);
}
AudioBuffer& AudioBuffer::operator=(const AudioBuffer & rhs)
{
if (this == &rhs)
return *this;
m_loop_cnt = rhs.m_loop_cnt;
m_begin = rhs.m_begin;
m_end = rhs.m_end;
m_write = rhs.m_write;
m_read = rhs.m_read;
m_prev_frame = rhs.m_prev_frame;
m_lpcm = rhs.m_lpcm;
m_write_wrapped = rhs.m_write_wrapped;
m_timestamps = rhs.m_timestamps;
m_frame_cnt = rhs.m_frame_cnt;
m_own_buffer = false;
m_spdif_format_context = rhs.m_spdif_format_context;
m_spdif_avio_context = rhs.m_spdif_avio_context;
m_spdif_avio_context_buffer = rhs.m_spdif_avio_context_buffer;
m_spdif_codec = rhs.m_spdif_codec;
m_spdif_codec_id = rhs.m_spdif_codec_id;
m_codec_name = rhs.m_codec_name;
m_num_channels = rhs.m_num_channels;
m_bytes_per_sample = rhs.m_bytes_per_sample;
m_frame_size = rhs.m_frame_size;
m_samples_per_frame = rhs.m_samples_per_frame;
m_sample_rate = rhs.m_sample_rate;
m_block_size = rhs.m_block_size;
m_parent = rhs.m_parent;
m_id = rhs.m_id;
m_verbose = rhs.m_verbose;
m_report_next = rhs.m_report_next;
return *this;
}
bool AudioBuffer::operator==(const AudioBuffer & rhs)
{
if (this == &rhs)
return true;
return m_begin == rhs.m_begin;
}
int64_t AudioBuffer::get_timestamp(uint8_t* P) const
{
int idx = static_cast<uint32_t>(P - m_begin) / m_frame_size;
return m_timestamps[idx];
}
AudioBuffer::AudioBuffer(uint8_t* Pbegin, uint8_t* Pend,
int num_channels, bool is_lpcm,
int bytes_per_sample, int sample_rate,
int samples_per_frame, int frame_size,
int64_t* timestamps,
AudioIO* parent, int verbose, int id)
: m_begin(Pbegin)
, m_end(Pend)
, m_write(Pbegin)
, m_read(Pbegin)
, m_timestamps(timestamps)
, m_lpcm(is_lpcm)
, m_num_channels(num_channels)
, m_bytes_per_sample(bytes_per_sample)
, m_frame_size(frame_size)
, m_samples_per_frame(samples_per_frame)
, m_sample_rate(sample_rate)
, m_parent(parent)
, m_id(id)
, m_verbose(verbose)
{
m_block_size = 8 * m_bytes_per_sample * m_samples_per_frame * 8;
}
AudioBuffer::~AudioBuffer(void)
{
if (m_own_buffer)
{
m_EoF.store(true);
delete[] m_begin;
delete[] m_timestamps;
}
}
void AudioBuffer::Clear(void)
{
const std::unique_lock<std::mutex> lock(m_write_mutex);
m_read = m_write;
m_write_wrapped = false;
if (m_verbose > 1)
cerr << "[" << m_id << "] audio buffer cleared.\n";
}
bool AudioBuffer::RescanSPDIF(void)
{
return DetectCodec();
}
void AudioBuffer::OwnBuffer(void)
{
m_own_buffer = true;
m_EoF.store(false);
if (m_lpcm)
{
m_codec_name = "ac3";
m_channel_layout = AV_CHANNEL_LAYOUT_STEREO;
}
PrintPointers("OwnBuffer");
PrintState("OwnBuffer");
}
void AudioBuffer::PrintState(const string & where, bool force) const
{
if (force || m_verbose > 0)
{
string loc = "[" + std::to_string(m_id) + "] " + where + " ";
cerr << loc
<< (m_lpcm ? "LPCM" : "Bitstream")
<< " Codec: " << m_codec_name
<< ", Channels: " << m_num_channels
<< ", BytesPerSample: " << m_bytes_per_sample
<< ", FrameSize: " << m_frame_size
<< "\n" << string(loc.size(), ' ')
<< "SamplesPerFrame: " << m_samples_per_frame
<< ", SampleRate: " << m_sample_rate
<< ", BlockSize: " << m_block_size
<< ", TotalBytes: " << m_total
<< endl;
}
}
void AudioBuffer::PrintPointers(const string & where, bool force) const
{
if (force || m_verbose > 4)
{
string loc = "[" + std::to_string(m_id) + "] " + where + " ";
cerr << loc
<< "begin: " << (uint64_t)(m_begin)
<< ", end : " << (int)(m_end - m_begin)
<< ", write : " << std::setw(6) << (int)(m_write - m_begin)
<< ", read : " << std::setw(6) << (int)(m_read - m_begin)
<< ", frame sz: " << m_frame_size
<< ", wrapped: " << (m_write_wrapped ? "Yes, " : "No, ")
<< (m_lpcm ? " LPCM" : " bistream")
<< " codec: " << m_codec_name
// << ", timestamp: " << m_timestamp
<< ", size " << Size()
<< endl;
}
}
int AudioBuffer::Add(uint8_t* Pframe, int len, int64_t timestamp)
{
const std::unique_lock<std::mutex> lock(m_write_mutex);
m_total += len;
PrintPointers(" Add");
m_write = Pframe + len;
if (m_write > m_end)
{
cerr << "WARNING: [" << m_id << "] Add audio to " << (uint64_t)Pframe
<< " - " << m_write
<< " which is greater than the end " << (uint64_t)m_end
<< endl;
}
if (m_prev_frame == m_end)
{
++m_loop_cnt;
m_write_wrapped = true;
}
else if (Pframe < m_prev_frame)
{
cerr << "WARNING: [" << m_id << "] Add audio to " << (uint64_t)Pframe
<< " which is less than " << (uint64_t)m_prev_frame
<< " but not the begining " << (uint64_t)m_begin
<< endl;
}
m_prev_frame = m_write;
if (m_write_wrapped && m_read < m_write)
{
if (m_verbose > 1 && m_read != m_begin)
{
cerr << "INFO: [" << m_id
<< "] Overwrote buffer begin, moving read\n";
PrintPointers(" Add", true);
}
m_read = m_write;
}
return 0;
}
int AudioBuffer::Read(uint8_t* dest, int32_t len)
{
uint8_t* Pend;
int sz;
static int empty_cnt = 0;
if (Empty())
{
if (m_EoF.load() == true)
{
if (m_verbose > 3)
cerr << "[" << m_id << "] AudioIO::Read: EOF\n";
return AVERROR_EOF;
}
++empty_cnt;
return 0;
}
if (empty_cnt)
{
if (m_verbose > 4)
cerr << "INFO: [" << m_id
<< "] AudioBuffer::Read: contiguously called " << empty_cnt
<< " times with no data available.\n";
empty_cnt = 0;
}
const std::unique_lock<std::mutex> lock(m_write_mutex);
if (m_write_wrapped)
{
if (m_read == m_end)
{
Pend = m_write;
m_read = m_begin;
m_write_wrapped = false;
}
else if (m_read > m_end)
{
cerr << "[" << m_id << "] Read has passed the end!\n";
cerr << "[" << m_id << "] Audio write: "
<< (int)(m_write - m_begin)
<< " read: " << (int)(m_read - m_begin)
<< " end: " << (int)(m_end - m_begin)
<< endl;
exit(-1);
}
else
Pend = m_end;
}
else
{
if (m_read > m_write)
{
cerr << "WARNING: [" << m_id << "] Read has passed Write!\n";
PrintPointers(" Read", true);
exit(-1);
}
Pend = m_write;
}
if (Pend - m_read < len)
{
sz = Pend - m_read;
if (m_verbose > 4)
{
cerr << "[" << m_id << "] AudioIO::Read: Requested " << len
<< " bytes, but only " << sz << " bytes available\n";
m_report_next = 10;
}
}
else
sz = len;
if (dest != nullptr)
memcpy(dest, m_read, sz);
m_parent->m_timestamp = get_timestamp(m_read);
m_read += sz;
PrintPointers(" Read", m_report_next);
if (m_report_next > 0)
--m_report_next;
#if 0
cerr << "\nTS: " << m_parent->m_timestamp
<< " sz " << sz << " Frames " << sz / m_frame_size << endl;
#endif
m_frame_cnt += sz;
return sz;
}
AVPacket* AudioBuffer::ReadSPDIF(void)
{
if (Size() < m_frame_size)
{
if (m_verbose > 1)
cerr << "[" << m_id << "] ReadSPDIF: Only "
<< Size() << " bytes available. "
<< m_frame_size << " desired.\n";
return nullptr;
}
AVPacket* pkt = av_packet_alloc();
if (!pkt)
{
cerr << "WARNING: [" << m_id
<< "] Could not allocate pkt for spdif input.\n";
return nullptr;
}
double ret = av_read_frame(m_spdif_format_context, pkt);
#if 0
cerr << "ReadSPDIF [" << pkt->stream_index << "] pts: " << pkt->pts
<< " dts: " << AV_ts2str(pkt->dts)
<< " duration: " << AV_ts2str(pkt->duration)
<< " size: " << pkt->size
<< endl;
#endif
if (0 > ret)
{
av_packet_free(&pkt);
if (ret != AVERROR_EOF && m_verbose > 0)
cerr << "WARNING: [" << m_id
<< "] Failed to read spdif frame: (" << ret << ") "
<< AVerr2str(ret) << endl;
return nullptr;
}
return pkt;
}
int64_t AudioBuffer::Seek(int64_t offset, int whence)
{
if (m_read == m_write)
return 0;
int force = whence & AVSEEK_FORCE;
whence &= ~AVSEEK_FORCE;
#if 0
int size = whence & AVSEEK_SIZE;
#endif
whence &= ~AVSEEK_SIZE;
if (force)
return -1;
string whence_str;
switch (whence)
{
case SEEK_END:
whence_str = "END";
break;
case SEEK_SET:
whence_str = "SET";
break;
case SEEK_CUR:
whence_str = "CURRENT";
break;
default:
whence_str = "UNHANDLED";
cerr << "[" << m_id << "] whence = " << whence << endl;
break;
}
if (m_verbose > 2)
cerr << "[" << m_id << "] Seeking from " << whence_str
<< " to " << offset << endl;
int64_t desired = offset;
if (whence == SEEK_END)
{
m_read = m_write + m_frame_size;
if (desired > 0)
return -1;
}
else if (whence == SEEK_SET)
{
// Basically rewind as much as we can and seek from there.
if (m_loop_cnt)
m_read = m_write;
else
m_read = m_begin;
desired = std::max(desired, static_cast<int64_t>(0));
}
else if (whence != SEEK_CUR)
return -1;
if (desired < 0)
{
// Backwards
desired = -desired; // To keep my head from exploading
if (m_write_wrapped)
{
if (m_read - desired < m_write)
m_read = m_write;
else
m_read -= desired;
}
else
{
if (m_read - desired < m_begin)
{
if (m_loop_cnt)
{
// Wrapped
m_read = m_end - (m_begin - (m_read - desired));
if (m_read < m_write)
m_read = m_write;
}
else
m_read = m_begin;
}
else
m_read -= desired;
}
m_parent->m_timestamp = get_timestamp(m_read - m_frame_size);
}
else if (desired > 0)
{
int len = Read(nullptr, desired);
if (len < desired)
return -1;
return 0;
}
return 0;
}
void AudioBuffer::set_mark(void)
{
m_mark = m_frame_cnt;
}
void AudioBuffer::return_to_mark(void)
{
Seek(m_mark - m_frame_cnt, SEEK_CUR);
}
static int read_packet(void* opaque, uint8_t* buf, int buf_size)
{
AudioBuffer* q = reinterpret_cast<AudioBuffer* >(opaque);
return q->Read(buf, buf_size);
}
static int64_t seek_packet(void* opaque, int64_t offset, int whence)
{
AudioBuffer* q = reinterpret_cast<AudioBuffer* >(opaque);
return q->Seek(offset, whence);
}
bool AudioBuffer::open_spdif_context(void)
{
if (m_spdif_format_context)
{
avformat_close_input(&m_spdif_format_context);
m_spdif_format_context = nullptr;
avio_context_free(&m_spdif_avio_context);
m_spdif_avio_context = nullptr;
#if 0
av_free(m_spdif_avio_context_buffer);
m_spdif_avio_context_buffer = nullptr;
#endif
avformat_free_context(m_spdif_format_context);
m_spdif_format_context = nullptr;
}
if (!(m_spdif_format_context = avformat_alloc_context()))
{
cerr << "WARNING: [" << m_id
<< "] Unable to allocate spdif format context.\n";
return false;
}
m_spdif_avio_context_buffer =
reinterpret_cast<uint8_t* >(av_malloc(m_frame_size));
if (!m_spdif_avio_context_buffer)
{
cerr << "WARNING: [" << m_id
<< "] Unable to allocate spdif avio context buffer.\n";
return false;
}
m_spdif_avio_context = avio_alloc_context(m_spdif_avio_context_buffer,
m_frame_size,
0,
reinterpret_cast<void* >(this),
read_packet,
nullptr,
seek_packet);
if (!m_spdif_avio_context)
{
cerr << "WARNING: [" << m_id
<< "] Unable to allocate audio input avio context.\n";
return false;
}
m_spdif_format_context->pb = m_spdif_avio_context;
const AVInputFormat* spdif_fmt = av_find_input_format("spdif");
if (0 > avformat_open_input(&m_spdif_format_context, NULL,
spdif_fmt, NULL))
{
cerr << "WARNING: [" << m_id << "] Could not open spdif input.\n";
return false;
}
return true;
}
bool AudioBuffer::open_spdif(void)
{
/* retrieve stream information */
const AVInputFormat* fmt = nullptr;
int ret;
int idx;
if (m_verbose > 1)
cerr << "[" << m_id << "] Scanning S/PDIF\n";
set_mark();
if (m_spdif_format_context == nullptr)
open_spdif_context();
int try_cnt = 3;
for (idx = 0; idx < try_cnt; ++idx)
{
if (m_EoF.load())
{
cerr << "WARNING: [" << m_id << "] Abort S/PDIF scan due EoF.\n";
return false;
}
if ((ret = av_probe_input_buffer(m_spdif_avio_context,
&fmt, "", nullptr, 0,
m_block_size * 20)) != 0)
{
if (!m_codec_name.empty())
{
return_to_mark();
cerr << "Re-initilized S/PDIF. Skipping rest of bitstream scan.\n";
return true;
}
cerr << "WARNING: [" << m_id << "] Failed to probe spdif input: "
<< AVerr2str(ret) << endl;
continue;
}
if (m_verbose > 1)
{
cerr << "[" << m_id << "] --> Detected fmt '" << fmt->name
<< "' '" << fmt->long_name << "'\n";
}
if (0 > avformat_find_stream_info(m_spdif_format_context, NULL))
{
cerr << "WARNING: [" << m_id
<< "] Could not find stream information\n";
continue;
}
if (m_spdif_format_context->nb_streams < 1)
{
cerr << "WARNING: [" << m_id << "] No streams found in SPDIF.\n";
continue;
}
int audio_stream_idx = 0;
AVStream* audio_stream =
m_spdif_format_context->streams[audio_stream_idx];
if (m_verbose > 0)
{
/* dump input information to stderr */
av_dump_format(m_spdif_format_context, 0, "pipe:0", 0);
}
if (!audio_stream)
{
cerr << "WARNING: [" << m_id
<< "] Could not find audio stream in spdif input.\n";
continue;
}
m_spdif_codec_id = audio_stream->codecpar->codec_id;
break;
}
if (idx >= try_cnt)
return false;
/* Find a decoder for the audio stream. */
if (!(m_spdif_codec = avcodec_find_decoder(m_spdif_codec_id)))
{
cerr << "WARNING: [" << m_id << "] Could not find input audio codec "
<< m_spdif_codec_id << "\n";
m_codec_name = "Unknown";
return false;
}
m_codec_name = m_spdif_codec->name;
m_channel_layout = AV_CHANNEL_LAYOUT_5POINT1;
/* The AVIO buffer is not timestamp aware, so discard it
*/
avio_flush(m_spdif_avio_context);
avformat_flush(m_spdif_format_context);
return_to_mark();
return true;
}
bool AudioBuffer::DetectCodec(void)
{
int idx = 0;
while (m_EoF.load() == false)
{
if (m_verbose > 0)
cerr << "\n[" << m_id << "] Detect codec (try " << ++idx << ")\n";
if (open_spdif())
{
PrintState("SPDIF");
return true;
}
if (idx > 11)
std::raise(SIGHUP);
}
setEoF();
return false;
}
int AudioBuffer::Size(void) const
{
if (m_write_wrapped)
return (m_end - m_read) +
(m_write - m_begin);
else
return m_write - m_read;
}
/************************************************
* AudioIO
************************************************/
AudioIO::AudioIO(int verbose)
: m_verbose(verbose)
{
}
void AudioIO::Shutdown(void)
{
m_running.store(false);
const std::unique_lock<std::mutex> lock(m_buffer_mutex);
buffer_que_t::iterator Ibuf;
for (Ibuf = m_buffer_q.begin(); Ibuf != m_buffer_q.end(); ++Ibuf)
(*Ibuf).setEoF();
}
bool AudioIO::AddBuffer(uint8_t* Pbegin, uint8_t* Pend,
int num_channels, bool is_lpcm,
int bytes_per_sample, int sample_rate,
int samples_per_frame, int frame_size,
int64_t* timestamps)
{
const std::unique_lock<std::mutex> lock(m_buffer_mutex);
buffer_que_t::iterator Ibuf;
if (m_running.load() == false)
return false;
if (!m_buffer_q.empty())
{
Ibuf = m_buffer_q.end() - 1;
(*Ibuf).setEoF();
}
m_buffer_q.push_back(AudioBuffer(Pbegin, Pend,
num_channels, is_lpcm,
bytes_per_sample, sample_rate,
samples_per_frame, frame_size,
timestamps,
this, m_verbose, m_buf_id++));
m_codec_initialized = false;
Ibuf = m_buffer_q.end() - 1;
(*Ibuf).OwnBuffer();
return true;
}
bool AudioIO::RescanSPDIF(void)
{
if (!m_buffer_q.empty())
return m_buffer_q.begin()->RescanSPDIF();
return false;
}
int AudioIO::BufId(void) const
{
const std::unique_lock<std::mutex> lock(m_buffer_mutex);
if (m_buffer_q.empty())
return 0;
buffer_que_t::const_iterator Ibuf = m_buffer_q.begin();
return (*Ibuf).Id();
}
int AudioIO::Size(void) const
{
const std::unique_lock<std::mutex> lock(m_buffer_mutex);
if (m_buffer_q.empty())
return 0;
int sz = 0;
buffer_que_t::const_iterator Ibuf;
for (Ibuf = m_buffer_q.begin(); Ibuf != m_buffer_q.end(); ++Ibuf)
{
sz += (*Ibuf).Size();
}
return sz;
}
bool AudioIO::Empty(void) const
{
const std::unique_lock<std::mutex> lock(m_buffer_mutex);
if (m_buffer_q.empty())
return true;
buffer_que_t::const_iterator Ibuf;
for (Ibuf = m_buffer_q.begin(); Ibuf != m_buffer_q.end(); ++Ibuf)
{
if (!(*Ibuf).Empty())
return false;
}
return true;
}
bool AudioIO::BlockReady(void) const
{
const std::unique_lock<std::mutex> lock(m_buffer_mutex);
if (m_buffer_q.empty())
{
cerr << "q empty\n";
return false;
}
buffer_que_t::const_iterator Ibuf = m_buffer_q.begin();
return (*Ibuf).Size() > (*Ibuf).BlockSize();
}
int AudioIO::Add(uint8_t* Pframe, int len, int64_t timestamp)
{
// const std::unique_lock<std::mutex> lock(m_buffer_mutex);
if (m_buffer_q.empty())
{
cerr << "WARNING: No audio buffers to Add to\n";
return 0;
}
buffer_que_t::iterator Ibuf = m_buffer_q.end() - 1;
return (*Ibuf).Add(Pframe, len, timestamp);
}
int64_t AudioIO::Seek(int64_t offset, int whence)
{
const std::unique_lock<std::mutex> lock(m_buffer_mutex);
if (m_buffer_q.empty())
{
cerr << "WARNING: No audio buffers to Seek in\n";
return 0;
}
buffer_que_t::iterator Ibuf = m_buffer_q.begin();
return (*Ibuf).Seek(offset, whence);
}
int AudioIO::Read(uint8_t* dest, int len)
{
if (m_buffer_q.empty())
{
cerr << "WARNING: No audio buffers to Read from\n";
return 0;
}
buffer_que_t::iterator Ibuf = m_buffer_q.begin();
return (*Ibuf).Read(dest, len);
}
AVPacket* AudioIO::ReadSPDIF(void)
{
if (m_buffer_q.empty())
{
cerr << "WARNING: No audio buffers to Read from\n";
return 0;
}
buffer_que_t::iterator Ibuf = m_buffer_q.begin();
return (*Ibuf).ReadSPDIF();
}
bool AudioIO::CodecChanged(void)
{
{
const std::unique_lock<std::mutex> lock(m_buffer_mutex);
while (!m_buffer_q.empty() && m_buffer_q.front().isEoF())
m_buffer_q.pop_front();
if (m_buffer_q.empty())
{
m_codec_name.clear();
return true;
}
}
string codec = m_codec_name;
int bytes_per_sample = m_bytes_per_sample;
buffer_que_t::iterator Ibuf = m_buffer_q.begin();
if ((*Ibuf).CodecName().empty())
{
if (!(*Ibuf).DetectCodec())
{
#if 0
cerr << "Failed to detect S/PDIF\n";
#endif
m_codec_name.clear();
return true;
}
}
if (m_codec_initialized == false)
{
m_codec_name = (*Ibuf).CodecName();
m_channel_layout = (*Ibuf).ChannelLayout();
m_sample_rate = (*Ibuf).SampleRate();
m_bytes_per_sample = (*Ibuf).BytesPerSample();
m_lpcm = (*Ibuf).LPCM();
m_codec_initialized = true;
}
if (codec == m_codec_name && bytes_per_sample == m_bytes_per_sample)
return false;
return true;
}