@@ -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
135134static 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
160163static int read_opus_header (void ) {
@@ -219,7 +222,6 @@ static int read_opus_header(void) {
219222
220223static 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
0 commit comments