Skip to content

Commit 6d6625b

Browse files
committed
ContainerOSC: missingDataCallback()
1 parent c6c7a65 commit 6d6625b

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

src/AudioTools/AudioCodecs/ContainerOSC.h

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,29 @@ class OSCContainerEncoder : public AudioEncoder {
8585
/// Returns the sequence number of the next packet
8686
uint64_t getSequenceNumber() { return packet_count; }
8787

88-
protected:
89-
uint64_t packet_count = 0;
90-
int repeat_info = 0;
91-
bool is_active = false;
92-
AudioEncoder *p_codec = nullptr;
93-
Print *p_out = nullptr;
94-
95-
void writeAudio(const uint8_t *data, size_t len, uint64_t packet) {
88+
/// Writes the audio with the indicated sequence number to the output.
89+
/// To be called to resend missing data
90+
void writeAudio(const uint8_t *data, size_t len, uint64_t sequenceNumber) {
9691
LOGD("writeAudio: %d", (int)len);
9792
uint8_t osc_data[len + 20]; // 20 is guess to cover address & fmt
9893
OSCData osc{osc_data, sizeof(osc_data)};
9994
osc.setAddress("/audio/data");
10095
osc.setFormat("ttb");
10196
osc.write((uint64_t)millis());
10297
// we use a uint64_t for a sequence number
103-
osc.write(packet);
98+
osc.write(sequenceNumber);
10499
osc.write(data, len);
105100
p_out->write(osc_data, osc.size());
106101
}
107102

103+
protected:
104+
uint64_t packet_count = 0;
105+
int repeat_info = 0;
106+
bool is_active = false;
107+
AudioEncoder *p_codec = nullptr;
108+
Print *p_out = nullptr;
109+
110+
108111
void writeAudioInfo(AudioInfo info, const char *mime) {
109112
LOGD("writeAudioInfo");
110113
uint8_t osc_data[100];
@@ -175,22 +178,27 @@ class OSCContainerDecoder : public ContainerDecoder {
175178

176179
/// Adds an new parser callback for a specific address matching string
177180
void addParserCallback(const char *address,
178-
bool (*callback)(OSCData &data, void *ref),
179-
OSCCompare compare = OSCCompare::Matches) {
181+
bool (*callback)(OSCData &data, void *ref),
182+
OSCCompare compare = OSCCompare::Matches) {
180183
osc.addCallback(address, callback, compare);
181184
}
182185

183-
/// Replace the write to the decoder with a callback:
186+
/// Replace the write to the decoder with a callback:
184187
void setWriteCallback(bool (*write_callback)(uint64_t time, uint64_t seq,
185188
uint8_t *data, size_t len,
186189
void *ref)) {
187190
this->write_callback = write_callback;
188191
}
189192

193+
/// Callback to be called when data is missing
194+
void setMissingDataCallback(void (*missing_data_callback)(uint64_t from_seq,
195+
uint64_t to_seq, void* ref)) {
196+
this->missing_data_callback = missing_data_callback;
197+
}
198+
190199
/// Provide a reference object to the callback
191200
void setReference(void *ref) { this->ref = ref; }
192201

193-
194202
protected:
195203
bool is_active = false;
196204
AudioDecoder *p_codec = nullptr;
@@ -203,13 +211,24 @@ class OSCContainerDecoder : public ContainerDecoder {
203211
/// Return false to complete the processing w/o writing to the decoder
204212
bool (*write_callback)(uint64_t time, uint64_t seq, uint8_t *data, size_t len,
205213
void *ref) = nullptr;
214+
void (*missing_data_callback)(uint64_t from_seq, uint64_t to_seq,
215+
void *ref) = missingDataCallback;
206216
void *ref = nullptr;
207217

218+
/// Default callback for missing data: just log the missing range
219+
static void missingDataCallback(uint64_t from_seq, uint64_t to_seq, void* ref) {
220+
LOGW("Missing sequence numbers %d - %d", from_seq, to_seq);
221+
}
222+
208223
static bool parseData(OSCData &osc, void *ref) {
209224
uint64_t time = osc.readTime();
210225
uint64_t seq = osc.readTime();
211226
OSCBinaryData data = osc.readData();
212227
OSCContainerDecoder *self = static_cast<OSCContainerDecoder *>(ref);
228+
// Check for missing sequence numbers
229+
if (self->seq_no + 1 != seq) {
230+
self->missing_data_callback(self->seq_no + 1, seq - 1, self->ref);
231+
}
213232
// store the actual sequence number
214233
self->seq_no = seq;
215234
// call write callbak if defined
@@ -221,6 +240,7 @@ class OSCContainerDecoder : public ContainerDecoder {
221240
if (self->p_codec != nullptr) {
222241
self->p_codec->write(data.data, data.len);
223242
}
243+
224244
return true;
225245
}
226246

0 commit comments

Comments
 (0)