@@ -219,8 +219,9 @@ class OggContainerEncoder : public AudioEncoder {
219
219
p_print = &out_stream;
220
220
} else {
221
221
EncodedAudioStream* eas = new EncodedAudioStream ();
222
- eas->begin (&out_stream, p_codec);
223
- p_print = eas;
222
+ eas->begin (&codec_buffer, p_codec);
223
+ p_encoded_audio_stream = eas;
224
+ p_print = &out_stream;
224
225
}
225
226
}
226
227
@@ -239,6 +240,7 @@ class OggContainerEncoder : public AudioEncoder {
239
240
virtual void begin () override {
240
241
LOGD (LOG_METHOD);
241
242
is_open = true ;
243
+ codec_buffer.begin ();
242
244
if (p_oggz == nullptr ) {
243
245
p_oggz = oggz_new (OGGZ_WRITE | OGGZ_NONSTRICT | OGGZ_AUTO);
244
246
serialno = oggz_serialno_new (p_oggz);
@@ -269,22 +271,39 @@ class OggContainerEncoder : public AudioEncoder {
269
271
p_oggz = nullptr ;
270
272
}
271
273
272
- // / Writes Ogg Packet
274
+ // / Writes raw data to be encoded and packaged
273
275
virtual size_t write (const void *in_ptr, size_t in_size) override {
274
276
if (!is_open || p_print == nullptr ) return 0 ;
275
277
LOGD (" write: %d" , (int ) in_size);
276
278
277
- op.packet = (uint8_t *)in_ptr;
278
- op.bytes = in_size;
279
- op.granulepos = granulepos +=
280
- in_size / sizeof (int16_t ) / cfg.channels ; // sample
281
- op.b_o_s = false ;
282
- op.e_o_s = false ;
283
- op.packetno = packetno++;
284
- if (!writePacket (op, OGGZ_FLUSH_AFTER)) {
285
- return 0 ;
286
- }
279
+ if (p_codec!=nullptr ){
280
+ // encode the data
281
+ size_t eff = p_encoded_audio_stream->write ((uint8_t *)in_ptr, in_size);
282
+ if (eff!=in_size){
283
+ LOGE (" Write overflow" );
284
+ }
285
+ // get the result from the buffer
286
+ void *encoded_data = buffer.address ();
287
+ int enoded_size = buffer.available ();
287
288
289
+ op.packet = (uint8_t *)encoded_data;
290
+ op.bytes = enoded_size;
291
+ } else {
292
+ op.packet = (uint8_t *)in_ptr;
293
+ op.bytes = in_size;
294
+ }
295
+ if (op.bytes >0 ){
296
+ buffer.reset ();
297
+ op.granulepos = granulepos +=
298
+ in_size / sizeof (int16_t ) / cfg.channels ; // sample
299
+ op.b_o_s = false ;
300
+ op.e_o_s = false ;
301
+ op.packetno = packetno++;
302
+ is_audio = true ;
303
+ if (!writePacket (op, OGGZ_FLUSH_AFTER)) {
304
+ return 0 ;
305
+ }
306
+ }
288
307
// trigger pysical write
289
308
while ((oggz_write (p_oggz, in_size)) > 0 )
290
309
;
@@ -297,8 +316,11 @@ class OggContainerEncoder : public AudioEncoder {
297
316
bool isOpen () { return is_open; }
298
317
299
318
protected:
300
- AudioEncoder* p_codec = nullptr ;
301
319
Print *p_print = nullptr ;
320
+ Print *p_encoded_audio_stream = nullptr ;
321
+ SingleBuffer<uint8_t > buffer{1024 };
322
+ CallbackBufferedStream<uint8_t > codec_buffer{buffer};
323
+ AudioEncoder* p_codec = nullptr ;
302
324
volatile bool is_open;
303
325
OGGZ *p_oggz = nullptr ;
304
326
ogg_packet op;
@@ -307,6 +329,7 @@ class OggContainerEncoder : public AudioEncoder {
307
329
size_t packetno = 0 ;
308
330
long serialno = -1 ;
309
331
AudioBaseInfo cfg;
332
+ bool is_audio = false ;
310
333
311
334
virtual bool writePacket (ogg_packet &op, int flag = 0 ) {
312
335
LOGD (" writePacket: %d" , (int ) op.bytes );
@@ -326,6 +349,7 @@ class OggContainerEncoder : public AudioEncoder {
326
349
oh.packetno = packetno++;
327
350
oh.b_o_s = true ;
328
351
oh.e_o_s = false ;
352
+ is_audio = false ;
329
353
return writePacket (oh);
330
354
}
331
355
@@ -337,6 +361,7 @@ class OggContainerEncoder : public AudioEncoder {
337
361
op.packetno = packetno++;
338
362
op.b_o_s = false ;
339
363
op.e_o_s = true ;
364
+ is_audio = false ;
340
365
return writePacket (op, OGGZ_FLUSH_AFTER);
341
366
}
342
367
0 commit comments