-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioIO.h
170 lines (137 loc) · 5.33 KB
/
AudioIO.h
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
#ifndef _AudioIO_h_
#define _AudioIO_h_
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
}
#include <string>
#include <deque>
#include <utility>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <atomic>
class AudioIO;
class AudioBuffer
{
public:
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);
AudioBuffer(const AudioBuffer & rhs) { *this = rhs; }
~AudioBuffer(void);
void Clear(void);
bool RescanSPDIF(void);
void OwnBuffer(void);
void setEoF(void) { m_EoF.store(true); }
bool isEoF(void) const { return m_EoF.load() == true; }
void PrintState(const std::string & where,
bool force = false) const;
void PrintPointers(const std::string & where,
bool force = false) const;
AudioBuffer& operator=(const AudioBuffer & rhs);
bool operator==(const AudioBuffer & rhs);
int Add(uint8_t* Pframe, int len, int64_t timestamp);
int Read(uint8_t* dest, int len);
AVPacket* ReadSPDIF(void);
int64_t Seek(int64_t offset, int whence);
void set_mark(void);
void return_to_mark(void);
int Id(void) const { return m_id; }
bool Empty(void) const { return m_read == m_write; }
int Size(void) const;
int Tell(void) const { return m_frame_cnt; }
std::string CodecName(void) const { return m_codec_name; }
AVChannelLayout ChannelLayout(void) const { return m_channel_layout; }
bool LPCM(void) const { return m_lpcm; }
int SampleRate(void) const { return m_sample_rate; }
int BytesPerSample(void) const { return m_bytes_per_sample; }
int FrameSize(void) const { return m_frame_size; }
int BlockSize(void) const { return m_block_size; }
bool DetectCodec(void);
private:
bool open_spdif_context(void);
bool open_spdif(void);
int64_t get_timestamp(uint8_t* P) const;
std::atomic<bool> m_EoF {false};
uint32_t m_loop_cnt {0};
uint8_t* m_begin {nullptr};
uint8_t* m_end {nullptr};
uint8_t* m_write {nullptr};
uint8_t* m_read {nullptr};
uint8_t* m_prev_frame {nullptr};
bool m_write_wrapped {false};
AVChannelLayout m_channel_layout;
int64_t* m_timestamps {nullptr};
int m_frame_cnt {0};
bool m_own_buffer {false};
int m_mark;
AVFormatContext* m_spdif_format_context {nullptr};
AVIOContext* m_spdif_avio_context {nullptr};
uint8_t* m_spdif_avio_context_buffer {nullptr};
const AVCodec* m_spdif_codec {nullptr};
AVCodecID m_spdif_codec_id;
bool m_lpcm {true};
std::string m_codec_name;
int m_num_channels {-1};
int m_bytes_per_sample {-1};
int m_frame_size {-1};
int m_samples_per_frame {-1};
int m_sample_rate {-1};
int m_block_size {-1};
AudioIO* m_parent {nullptr};
std::mutex m_write_mutex;
int m_id {-1};
int m_verbose {0};
int m_total {0};
int m_report_next {0};
};
class AudioIO
{
friend AudioBuffer;
public:
AudioIO(int verbose = 0);
~AudioIO(void) { m_running.store(false); }
void Shutdown(void);
bool 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);
bool RescanSPDIF(void);
int Add(uint8_t* Pframe, int len, int64_t timestamp);
int64_t Seek(int64_t offset, int whence);
int Read(uint8_t* dest, int32_t len);
AVPacket* ReadSPDIF(void);
int BufId(void) const;
int LastBufId(void) const { return m_buf_id - 1; }
int Buffers(void) const { return m_buffer_q.size(); }
int Size(void) const;
bool Empty(void) const;
bool BlockReady(void) const;
int64_t TimeStamp(void) const { return m_timestamp; }
std::string CodecName(void) const { return m_codec_name; }
AVChannelLayout ChannelLayout(void) const { return m_channel_layout; }
int SampleRate(void) const { return m_sample_rate; }
int BytesPerSample(void) const { return m_bytes_per_sample; }
bool Bitstream(void) { return !m_lpcm; }
bool CodecChanged(void);
private:
using buffer_que_t = std::deque<AudioBuffer>;
buffer_que_t m_buffer_q;
std::string m_codec_name;
AVChannelLayout m_channel_layout;
int m_sample_rate {-1};
int m_bytes_per_sample {0};
bool m_lpcm {true};
int64_t m_timestamp {0LL};
mutable std::mutex m_buffer_mutex;
bool m_codec_initialized {false};
int m_buf_id {0};
int m_verbose {1};
std::atomic<bool> m_running {true};
};
#endif