3 #include "AudioTools/CoreAudio/AudioStreams.h"
4 #include "AudioTools/CoreAudio/AudioOutput.h"
5 #include "AudioTools/CoreAudio/Buffers.h"
6 #include "AudioTools/AudioCodecs/AudioEncoded.h"
12 enum class RecordType : uint8_t { Undefined, Begin, Send, Receive, End };
13 enum class AudioType : uint8_t { PCM, MP3, AAC, WAV, ADPC };
14 enum class TransmitRole : uint8_t { Sender, Receiver };
20 RecordType rec = RecordType::Undefined;
24 static uint16_t static_count = 0;
33 AudioType type = AudioType::PCM;
39 rec = RecordType::Send;
66 bool begin(
AudioInfo &info, AudioType type) {
72 int write_len =
sizeof(begin);
73 int len = p_dest->write((
const uint8_t *)&begin, write_len);
74 return len == write_len;
77 size_t write(
const uint8_t *data,
size_t len)
override {
81 while (written_len < len) {
82 int available_to_write = waitForRequest();
83 size_t to_write_len = DEFAULT_BUFFER_SIZE;
84 to_write_len = min(open_len, available_to_write);
86 send.size = to_write_len;
87 p_dest->write((
const uint8_t *)&send,
sizeof(send));
88 int w = p_dest->write(data + written_len, to_write_len);
95 int availableForWrite()
override {
return available_to_write; }
100 p_dest->write((
const uint8_t *)&end,
sizeof(end));
105 int available_to_write = 1024;
110 while (p_dest->available() < size) {
115 int waitForRequest() {
117 size_t rcv_len =
sizeof(rcv);
119 p_dest->readBytes((uint8_t *)&rcv, rcv_len);
135 bool isConfirmer =
true) {
138 is_confirmer = isConfirmer;
143 int header_size =
sizeof(header);
145 readBytes((uint8_t *)&header, header_size);
147 switch (header.rec) {
148 case RecordType::Begin:
151 case RecordType::End:
154 case RecordType::Send:
155 processed = receiveData();
167 size_t available = 0;
168 bool is_started =
false;
191 available = data.size;
195 if (data.seq > last_seq ||
196 (data.seq < max_gap && last_seq >= (32767 - max_gap))) {
197 uint8_t buffer[available];
198 p_in->readBytes((uint8_t *)buffer, available);
199 p_out->write((
const uint8_t *)buffer, available);
211 while (p_in->available() < size) {
218 req.size = p_out->availableForWrite();
220 p_in->write((
const uint8_t *)&req,
sizeof(req));
226 static const int header_size =
sizeof(header);
227 memcpy(data, &header, header_size);
228 int read_size = len - header_size;
230 readBytes((uint8_t *)data + header_size, read_size);