Skip to content

Commit e0e7e71

Browse files
committed
fix vorbis & opus never ending - release
1 parent f0527f7 commit e0e7e71

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

components/squeezelite/opus.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ struct opus {
5454
size_t overframes;
5555
u8_t *overbuf;
5656
int channels;
57-
bool eos;
5857
};
5958

6059
#if !LINKALL
@@ -133,7 +132,7 @@ static opus_uint32 parse_uint32(const unsigned char* _data) {
133132
}
134133

135134
static int get_opus_packet(void) {
136-
int status = 0;
135+
int status, packet = -1;
137136

138137
LOCK_S;
139138
size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
@@ -152,9 +151,13 @@ static int get_opus_packet(void) {
152151
// if we have a new page, put it in
153152
if (status) OG(&go, stream_pagein, &u->state, &u->page);
154153
}
154+
155+
// only return a negative value when end of streaming is reached
156+
if (status > 0) packet = status;
157+
else if (stream.state > DISCONNECT) packet = 0;
155158

156159
UNLOCK_S;
157-
return status;
160+
return packet;
158161
}
159162

160163
static int read_opus_header(void) {
@@ -219,7 +222,6 @@ static int read_opus_header(void) {
219222

220223
static decode_state opus_decompress(void) {
221224
frames_t frames;
222-
int n;
223225
u8_t *write_buf;
224226

225227
if (decode.new_stream) {
@@ -257,6 +259,8 @@ static decode_state opus_decompress(void) {
257259
frames = process.max_in_frames;
258260
write_buf = process.inbuf;
259261
);
262+
263+
int packet, n = 0;
260264

261265
// get some packets and decode them, or use the leftover from previous pass
262266
if (u->overframes) {
@@ -265,7 +269,7 @@ static decode_state opus_decompress(void) {
265269
memcpy(write_buf, u->overbuf, u->overframes * BYTES_PER_FRAME);
266270
n = u->overframes;
267271
u->overframes = 0;
268-
} else if (get_opus_packet() > 0) {
272+
} else if ((packet = get_opus_packet()) > 0) {
269273
if (frames < MAX_OPUS_FRAMES) {
270274
// don't have enough contiguous space, use the overflow buffer
271275
n = OP(&gu, decode, u->decoder, u->packet.packet, u->packet.bytes, (opus_int16*) u->overbuf, MAX_OPUS_FRAMES, 0);
@@ -280,10 +284,10 @@ static decode_state opus_decompress(void) {
280284
* outputbuf and streambuf for maybe a long time while we process it all, so don't do that */
281285
n = OP(&gu, decode, u->decoder, u->packet.packet, u->packet.bytes, (opus_int16*) write_buf, frames, 0);
282286
}
283-
} else if (!OG(&go, page_eos, &u->page)) {
287+
} else if (!packet && !OG(&go, page_eos, &u->page)) {
284288
UNLOCK_O_direct;
285289
return DECODE_RUNNING;
286-
} else u->eos = true;
290+
}
287291

288292
if (n > 0) {
289293
frames_t count;
@@ -326,7 +330,7 @@ static decode_state opus_decompress(void) {
326330

327331
} else if (n == 0) {
328332

329-
if (stream.state <= DISCONNECT && u->eos) {
333+
if (packet < 0) {
330334
LOG_INFO("end of decode");
331335
UNLOCK_O_direct;
332336
return DECODE_COMPLETE;
@@ -351,7 +355,6 @@ static void opus_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
351355

352356
if (!u->overbuf) u->overbuf = malloc(MAX_OPUS_FRAMES * BYTES_PER_FRAME);
353357

354-
u->eos = false;
355358
u->status = OGG_SYNC;
356359
u->overframes = 0;
357360

components/squeezelite/vorbis.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ struct vorbis {
6565
};
6666
int rate, channels;
6767
uint32_t overflow;
68-
bool eos;
6968
};
7069

7170
#if !LINKALL
@@ -133,7 +132,7 @@ extern struct processstate process;
133132
#endif
134133

135134
static int get_ogg_packet(void) {
136-
int status = 0;
135+
int status, packet = -1;
137136

138137
LOCK_S;
139138
size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
@@ -152,9 +151,13 @@ static int get_ogg_packet(void) {
152151
// if we have a new page, put it in
153152
if (status) OG(&go, stream_pagein, &v->state, &v->page);
154153
}
154+
155+
// only return a negative value when end of streaming is reached
156+
if (status > 0) packet = status;
157+
else if (stream.state > DISCONNECT) packet = 0;
155158

156159
UNLOCK_S;
157-
return status;
160+
return packet;
158161
}
159162

160163
static int read_vorbis_header(void) {
@@ -261,10 +264,8 @@ inline int pcm_out(vorbis_dsp_state* decoder, void*** pcm) {
261264

262265
static decode_state vorbis_decode(void) {
263266
frames_t frames;
264-
int n = 0;
265267
u8_t *write_buf;
266-
void** pcm = NULL;
267-
268+
268269
if (decode.new_stream) {
269270
int status = read_vorbis_header();
270271

@@ -300,19 +301,22 @@ static decode_state vorbis_decode(void) {
300301
frames = process.max_in_frames;
301302
write_buf = process.inbuf;
302303
);
304+
305+
void** pcm = NULL;
306+
int packet, n = 0;
303307

304308
if (v->overflow) {
305309
n = pcm_out(&v->decoder, &pcm);
306310
v->overflow = n - min(n, frames);
307-
} else if (get_ogg_packet() > 0) {
311+
} else if ((packet = get_ogg_packet()) > 0) {
308312
n = OV(&gv, synthesis, &v->block, &v->packet);
309313
if (n == 0) n = OV(&gv, synthesis_blockin, &v->decoder, &v->block);
310314
if (n == 0) n = pcm_out(&v->decoder, &pcm);
311315
v->overflow = n - min(n, frames);
312-
} else if (!OG(&go, page_eos, &v->page)) {
316+
} else if (!packet && !OG(&go, page_eos, &v->page)) {
313317
UNLOCK_O_direct;
314318
return DECODE_RUNNING;
315-
} else v->eos = true;
319+
}
316320

317321
if (n > 0) {
318322
ISAMPLE_T *optr = (ISAMPLE_T*) write_buf;
@@ -370,7 +374,7 @@ static decode_state vorbis_decode(void) {
370374

371375
} else if (n == 0) {
372376

373-
if (stream.state <= DISCONNECT && v->eos) {
377+
if (packet < 0) {
374378
LOG_INFO("end of decode");
375379
UNLOCK_O_direct;
376380
return DECODE_COMPLETE;
@@ -397,7 +401,6 @@ static void vorbis_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
397401
OV(&go, dsp_clear, &v->decoder);
398402
}
399403

400-
v->eos = false;
401404
v->opened = false;
402405
v->status = OGG_SYNC;
403406
v->overflow = 0;

0 commit comments

Comments
 (0)