Skip to content

Commit

Permalink
fix vorbis as well
Browse files Browse the repository at this point in the history
  • Loading branch information
philippe44 committed Apr 7, 2023
1 parent b4af1e7 commit d2b8edc
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 225 deletions.
4 changes: 2 additions & 2 deletions components/spotify/Shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,11 @@ void cspotPlayer::runTask() {
CSPOT_LOG(info, "disconnecting player %s", name.c_str());
}

// we want to release memory ASAP and fore sure
// we want to release memory ASAP and for sure
centralAudioBuffer.reset();
ctx.reset();
token.clear();

// update volume when we disconnect
cJSON *config = config_alloc_get_cjson("cspot_config");
cJSON_DeleteItemFromObject(config, "volume");
Expand Down
6 changes: 5 additions & 1 deletion components/squeezelite/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,17 @@ void decode_close(void) {
#endif
}

void decode_flush(void) {
void decode_flush(bool close) {
LOG_INFO("decode flush");
LOCK_D;
decode.state = DECODE_STOPPED;
IF_PROCESS(
process_flush();
);
if (close && codec) {
codec->close();
codec = NULL;
}
UNLOCK_D;
}

Expand Down
52 changes: 27 additions & 25 deletions components/squeezelite/opus.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ struct opus {
size_t overframes;
u8_t *overbuf;
int channels;
bool eos;
};

#if !LINKALL
Expand Down Expand Up @@ -101,15 +102,11 @@ extern struct processstate process;
#if PROCESS
#define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
#define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
#define LOCK_O_not_direct if (!decode.direct) mutex_lock(outputbuf->mutex)
#define UNLOCK_O_not_direct if (!decode.direct) mutex_unlock(outputbuf->mutex)
#define IF_DIRECT(x) if (decode.direct) { x }
#define IF_PROCESS(x) if (!decode.direct) { x }
#else
#define LOCK_O_direct mutex_lock(outputbuf->mutex)
#define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
#define LOCK_O_not_direct
#define UNLOCK_O_not_direct
#define IF_DIRECT(x) { x }
#define IF_PROCESS(x)
#endif
Expand Down Expand Up @@ -168,8 +165,6 @@ static int read_opus_header(void) {
size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));

while (bytes && !status) {
bool fetched = false;

// first fetch a page if we need one
if (fetch) {
size_t consumed = min(bytes, 4096);
Expand All @@ -181,7 +176,6 @@ static int read_opus_header(void) {
bytes -= consumed;

if (!OG(&gu, sync_pageseek, &u->sync, &u->page)) continue;
fetched = true;
}

switch (u->status) {
Expand Down Expand Up @@ -211,12 +205,10 @@ static int read_opus_header(void) {
fetch = true;
break;
case OGG_COMMENT_HEADER:
// loop until we have consumed VorbisComment and get ready for a new packet
// skip pakets to consume VorbisComment. With opus, header packets align on pages
status = OG(&gu, page_packets, &u->page);
break;
default:
// just to avoid warning;
fetched = fetched;
break;
}
}
Expand All @@ -228,7 +220,6 @@ static int read_opus_header(void) {
static decode_state opus_decompress(void) {
frames_t frames;
int n;
static int channels;
u8_t *write_buf;

if (decode.new_stream) {
Expand All @@ -249,8 +240,11 @@ static decode_state opus_decompress(void) {
decode.new_stream = false;
UNLOCK_O;

channels = u->channels;

if (u->channels > 2) {
LOG_WARN("too many channels: %d", u->channels);
return DECODE_ERROR;
}

LOG_INFO("setting track_start");
}

Expand All @@ -273,7 +267,7 @@ static decode_state opus_decompress(void) {
u->overframes = 0;
} else if (get_opus_packet() > 0) {
if (frames < MAX_OPUS_FRAMES) {
// don't have enough contiguous space, use the overflow buffer (still works if n < 0)
// don't have enough contiguous space, use the overflow buffer
n = OP(&gu, decode, u->decoder, u->packet.packet, u->packet.bytes, (opus_int16*) u->overbuf, MAX_OPUS_FRAMES, 0);
if (n > 0) {
u->overframes = n - min(n, frames);
Expand All @@ -289,15 +283,15 @@ static decode_state opus_decompress(void) {
} else if (!OG(&go, page_eos, &u->page)) {
UNLOCK_O_direct;
return DECODE_RUNNING;
}
} else u->eos = true;

if (n > 0) {
frames_t count;
s16_t *iptr;
ISAMPLE_T *optr;

frames = n;
count = frames * channels;
count = frames * u->channels;

// work backward to unpack samples (if needed)
iptr = (s16_t *) write_buf + count;
Expand All @@ -308,13 +302,13 @@ static decode_state opus_decompress(void) {
optr = (ISAMPLE_T *) write_buf + frames * 2;
)

if (channels == 2) {
if (u->channels == 2) {
#if BYTES_PER_FRAME == 8
while (count--) {
*--optr = ALIGN(*--iptr);
}
#endif
} else if (channels == 1) {
} else if (u->channels == 1) {
while (count--) {
*--optr = ALIGN(*--iptr);
*--optr = ALIGN(*iptr);
Expand All @@ -332,7 +326,7 @@ static decode_state opus_decompress(void) {

} else if (n == 0) {

if (stream.state <= DISCONNECT) {
if (stream.state <= DISCONNECT && u->eos) {
LOG_INFO("end of decode");
UNLOCK_O_direct;
return DECODE_COMPLETE;
Expand All @@ -348,7 +342,6 @@ static decode_state opus_decompress(void) {
}

UNLOCK_O_direct;

return DECODE_RUNNING;
}

Expand All @@ -358,10 +351,12 @@ static void opus_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {

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

u->eos = false;
u->status = OGG_SYNC;
u->overframes = 0;

OG(&gu, sync_init, &u->sync);
OG(&gu, sync_clear, &u->sync);
OG(&gu, stream_clear, &u->state);
OG(&gu, stream_init, &u->state, -1);
}

Expand All @@ -379,11 +374,17 @@ static void opus_close(void) {
static bool load_opus(void) {
#if !LINKALL
char *err;
void *g_handle = dlopen(LIBOGG, RTLD_NOW);
void *u.handle = dlopen(LIBOPUS, RTLD_NOW);

if (!g_handle || !u_handle) {
LOG_INFO("dlerror: %s", dlerror());
void *u.handle = dlopen(LIBOPUS, RTLD_NOW);
if (!u_handle) {
LOG_INFO("opus dlerror: %s", dlerror());
return false;
}

void *g_handle = dlopen(LIBOGG, RTLD_NOW);
if (!g_handle) {
dlclose(u_handle);
LOG_INFO("ogg dlerror: %s", dlerror());
return false;
}

Expand All @@ -400,6 +401,7 @@ static bool load_opus(void) {
g_handle->ogg_stream_pagein = dlsym(g_handle->handle, "ogg_stream_pagein");
g_handle->ogg_stream_packetout = dlsym(g_handle->handle, "ogg_stream_packetout");
g_handle->ogg_page_packets = dlsym(g_handle->handle, "ogg_page_packets");

u_handle->opus_decoder_create = dlsym(u_handle->handle, "opus_decoder_create");
u_handle->opus_decoder_destroy = dlsym(u_handle->handle, "opus_decoder_destroy");
u_handle->opus_decode = dlsym(u_handle->handle, "opus_decode");
Expand Down
2 changes: 1 addition & 1 deletion components/squeezelite/slimproto.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ static void process_strm(u8_t *pkt, int len) {
break;
case 'f':
case 'q':
decode_flush();
decode_flush(strm->command == 'q');
if (!output.external) output_flush();
status.frames_played = 0;
if (stream_disconnect() && strm->command == 'f') sendSTAT("STMf", 0);
Expand Down
2 changes: 1 addition & 1 deletion components/squeezelite/squeezelite.h
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ struct codec {

void decode_init(log_level level, const char *include_codecs, const char *exclude_codecs);
void decode_close(void);
void decode_flush(void);
void decode_flush(bool close);
unsigned decode_newstream(unsigned sample_rate, unsigned supported_rates[]);
void codec_open(u8_t format, u8_t sample_size, u8_t sample_rate, u8_t channels, u8_t endianness);

Expand Down

0 comments on commit d2b8edc

Please sign in to comment.