Skip to content

Commit c173008

Browse files
committed
ContainerOSC: getSequenceNumber()
1 parent 83f478d commit c173008

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/AudioTools/AudioCodecs/ContainerOSC.h

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,23 @@ class OSCContainerEncoder : public AudioEncoder {
4848
// target.begin();
4949
is_active = p_codec->begin();
5050
p_codec->setAudioInfo(audioInfo());
51-
writeAudioInfo();
51+
writeAudioInfo(audioInfo(), p_codec->mime());
5252
return is_active;
5353
}
5454

5555
void setAudioInfo(AudioInfo info) override {
5656
TRACED();
57-
writeAudioInfo();
57+
if (is_active) writeAudioInfo(audioInfo(), p_codec->mime());
5858
AudioWriter::setAudioInfo(info);
5959
}
6060

6161
/// Add data segment. On first write we also add a AudioInfo header
6262
size_t write(const uint8_t *data, size_t len) {
6363
LOGD("OSCContainerEncoder::write: %d", (int)len);
6464
if ((repeat_info > 0) && (packet_count % repeat_info == 0)) {
65-
writeAudioInfo();
65+
writeAudioInfo(audioInfo(), p_codec->mime());
6666
}
67-
writeAudio(data, len);
67+
writeAudio(data, len, packet_count);
6868
packet_count++;
6969
return len;
7070
}
@@ -82,36 +82,39 @@ class OSCContainerEncoder : public AudioEncoder {
8282
this->repeat_info = packet_count;
8383
}
8484

85+
/// Returns the sequence number of the next packet
86+
uint64_t getSequenceNumber() { return packet_count; }
87+
8588
protected:
8689
uint64_t packet_count = 0;
8790
int repeat_info = 0;
8891
bool is_active = false;
8992
AudioEncoder *p_codec = nullptr;
9093
Print *p_out = nullptr;
9194

92-
void writeAudio(const uint8_t *data, size_t len) {
95+
void writeAudio(const uint8_t *data, size_t len, uint64_t packet) {
9396
LOGD("writeAudio: %d", (int)len);
9497
uint8_t osc_data[len + 20]; // 20 is guess to cover address & fmt
9598
OSCData osc{osc_data, sizeof(osc_data)};
9699
osc.setAddress("/audio/data");
97100
osc.setFormat("ttb");
98101
osc.write((uint64_t)millis());
99102
// we use a uint64_t for a sequence number
100-
osc.write(packet_count);
103+
osc.write(packet);
101104
osc.write(data, len);
102105
p_out->write(osc_data, osc.size());
103106
}
104107

105-
void writeAudioInfo() {
108+
void writeAudioInfo(AudioInfo info, const char *mime) {
106109
LOGD("writeAudioInfo");
107110
uint8_t osc_data[100];
108111
OSCData osc{osc_data, sizeof(osc_data)};
109112
osc.setAddress("/audio/info");
110113
osc.setFormat("iiis");
111-
osc.write((int32_t)audioInfo().sample_rate);
112-
osc.write((int32_t)audioInfo().channels);
113-
osc.write((int32_t)audioInfo().bits_per_sample);
114-
osc.write(p_codec->mime());
114+
osc.write((int32_t)info.sample_rate);
115+
osc.write((int32_t)info.channels);
116+
osc.write((int32_t)info.bits_per_sample);
117+
osc.write(mime);
115118
p_out->write(osc_data, osc.size());
116119
}
117120
};
@@ -167,16 +170,19 @@ class OSCContainerDecoder : public ContainerDecoder {
167170
/// Provides the mime type from the encoder
168171
const char *mime() { return mime_str.c_str(); };
169172

170-
/// Replace the write to the decoder with a callback
171-
void setWriteCallback(
172-
bool (*write_callback)(uint64_t time, uint64_t seq, uint8_t *data,
173-
size_t len, void *ref)) {
173+
/// Replace the write to the decoder with a callback:
174+
void setWriteCallback(bool (*write_callback)(uint64_t time, uint64_t seq,
175+
uint8_t *data, size_t len,
176+
void *ref)) {
174177
this->write_callback = write_callback;
175178
}
176179

177180
/// Provide a reference object to the callback
178181
void setReference(void *ref) { this->ref = ref; }
179182

183+
/// Provides the sequence number of the last packet
184+
uint64_t getSequenceNumber() { return seq_no; }
185+
180186
protected:
181187
bool is_active = false;
182188
AudioDecoder *p_codec = nullptr;
@@ -185,17 +191,23 @@ class OSCContainerDecoder : public ContainerDecoder {
185191
Print *p_out = nullptr;
186192
OSCData osc;
187193
Str mime_str;
188-
bool (*write_callback)(uint64_t time, uint64_t seq, uint8_t *data, size_t len, void* ref) = nullptr;
194+
uint64_t seq_no = 0;
195+
/// Return false to complete the processing w/o writing to the decoder
196+
bool (*write_callback)(uint64_t time, uint64_t seq, uint8_t *data, size_t len,
197+
void *ref) = nullptr;
189198
void *ref = nullptr;
190199

191200
static bool parseData(OSCData &osc, void *ref) {
192201
uint64_t time = osc.readTime();
193202
uint64_t seq = osc.readTime();
194203
OSCBinaryData data = osc.readData();
195204
OSCContainerDecoder *self = static_cast<OSCContainerDecoder *>(ref);
205+
// store the actual sequence number
206+
self->seq_no = seq;
196207
// call write callbak if defined
197208
if (self->write_callback != nullptr) {
198-
return self->write_callback(time, seq, data.data, data.len, ref);
209+
bool ok = self->write_callback(time, seq, data.data, data.len, ref);
210+
if (!ok) return true;
199211
}
200212
// output to decoder
201213
if (self->p_codec != nullptr) {

0 commit comments

Comments
 (0)