mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-06 11:36:59 +03:00
fixed airplay
This commit is contained in:
@@ -459,6 +459,9 @@ static bool handle_rtsp(raop_ctx_t *ctx, int sock)
|
|||||||
|
|
||||||
NFREE(p);
|
NFREE(p);
|
||||||
NFREE(padded);
|
NFREE(padded);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((p = strcasestr(body, "fmtp")) != NULL) {
|
||||||
p = strextract(p, ":", "\r\n");
|
p = strextract(p, ":", "\r\n");
|
||||||
ctx->rtsp.fmtp = strdup(p);
|
ctx->rtsp.fmtp = strdup(p);
|
||||||
NFREE(p);
|
NFREE(p);
|
||||||
|
|||||||
@@ -11,7 +11,9 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef enum { RAOP_STREAM, RAOP_PLAY, RAOP_FLUSH, RAOP_PAUSE, RAOP_STOP, RAOP_VOLUME, RAOP_TIMING } raop_event_t ;
|
#define RAOP_SAMPLE_RATE 44100
|
||||||
|
|
||||||
|
typedef enum { RAOP_SETUP, RAOP_STREAM, RAOP_PLAY, RAOP_FLUSH, RAOP_PAUSE, RAOP_STOP, RAOP_VOLUME, RAOP_TIMING } raop_event_t ;
|
||||||
|
|
||||||
typedef void (*raop_cmd_cb_t)(raop_event_t event, void *param);
|
typedef void (*raop_cmd_cb_t)(raop_event_t event, void *param);
|
||||||
typedef void (*raop_data_cb_t)(const u8_t *data, size_t len);
|
typedef void (*raop_data_cb_t)(const u8_t *data, size_t len);
|
||||||
|
|||||||
@@ -70,7 +70,7 @@
|
|||||||
static log_level *loglevel = &raop_loglevel;
|
static log_level *loglevel = &raop_loglevel;
|
||||||
|
|
||||||
//#define __RTP_STORE
|
//#define __RTP_STORE
|
||||||
|
|
||||||
// default buffer size
|
// default buffer size
|
||||||
#define BUFFER_FRAMES ( (150 * 88200) / (352 * 100) )
|
#define BUFFER_FRAMES ( (150 * 88200) / (352 * 100) )
|
||||||
#define MAX_PACKET 1408
|
#define MAX_PACKET 1408
|
||||||
@@ -126,7 +126,6 @@ typedef struct rtp_s {
|
|||||||
u32_t rtptime;
|
u32_t rtptime;
|
||||||
} record;
|
} record;
|
||||||
int latency; // rtp hold depth in samples
|
int latency; // rtp hold depth in samples
|
||||||
u32_t resent_req, resent_rec; // total resent + recovered frames
|
|
||||||
u32_t resent_req, resent_rec; // total resent + recovered frames
|
u32_t resent_req, resent_rec; // total resent + recovered frames
|
||||||
u32_t silent_frames; // total silence frames
|
u32_t silent_frames; // total silence frames
|
||||||
u32_t discarded;
|
u32_t discarded;
|
||||||
@@ -141,7 +140,6 @@ typedef struct rtp_s {
|
|||||||
alac_file *alac_codec;
|
alac_file *alac_codec;
|
||||||
int flush_seqno;
|
int flush_seqno;
|
||||||
bool playing;
|
bool playing;
|
||||||
raop_data_cb_t data_cb;
|
|
||||||
raop_data_cb_t data_cb;
|
raop_data_cb_t data_cb;
|
||||||
raop_cmd_cb_t cmd_cb;
|
raop_cmd_cb_t cmd_cb;
|
||||||
} rtp_t;
|
} rtp_t;
|
||||||
@@ -239,7 +237,7 @@ rtp_resp_t rtp_init(struct in_addr host, int latency, char *aeskey, char *aesiv,
|
|||||||
|
|
||||||
memset(fmtp, 0, sizeof(fmtp));
|
memset(fmtp, 0, sizeof(fmtp));
|
||||||
while ((arg = strsep(&fmtpstr, " \t")) != NULL) fmtp[i++] = atoi(arg);
|
while ((arg = strsep(&fmtpstr, " \t")) != NULL) fmtp[i++] = atoi(arg);
|
||||||
|
|
||||||
ctx->frame_size = fmtp[1];
|
ctx->frame_size = fmtp[1];
|
||||||
ctx->frame_duration = (ctx->frame_size * 1000) / RAOP_SAMPLE_RATE;
|
ctx->frame_duration = (ctx->frame_size * 1000) / RAOP_SAMPLE_RATE;
|
||||||
|
|
||||||
@@ -461,14 +459,14 @@ static void buffer_put_packet(rtp_t *ctx, seq_t seqno, unsigned rtptime, bool fi
|
|||||||
if (abuf) {
|
if (abuf) {
|
||||||
alac_decode(ctx, abuf->data, data, len, &abuf->len);
|
alac_decode(ctx, abuf->data, data, len, &abuf->len);
|
||||||
abuf->ready = 1;
|
abuf->ready = 1;
|
||||||
|
// this is the local rtptime when this frame is expected to play
|
||||||
|
abuf->rtptime = rtptime;
|
||||||
buffer_push_packet(ctx);
|
buffer_push_packet(ctx);
|
||||||
|
|
||||||
#ifdef __RTP_STORE
|
#ifdef __RTP_STORE
|
||||||
fwrite(data, len, 1, ctx->rtpIN);
|
fwrite(data, len, 1, ctx->rtpIN);
|
||||||
fwrite(abuf->data, abuf->len, 1, ctx->rtpOUT);
|
fwrite(abuf->data, abuf->len, 1, ctx->rtpOUT);
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_unlock(&ctx->ab_mutex);
|
pthread_mutex_unlock(&ctx->ab_mutex);
|
||||||
@@ -476,7 +474,7 @@ static void buffer_put_packet(rtp_t *ctx, seq_t seqno, unsigned rtptime, bool fi
|
|||||||
|
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
// push as many frames as possible through callback
|
// push as many frames as possible through callback
|
||||||
static void buffer_push_packet(rtp_t *ctx) {
|
static void buffer_push_packet(rtp_t *ctx) {
|
||||||
abuf_t *curframe = NULL;
|
abuf_t *curframe = NULL;
|
||||||
u32_t now, playtime, hold = max((ctx->latency * 1000) / (8 * RAOP_SAMPLE_RATE), 100);
|
u32_t now, playtime, hold = max((ctx->latency * 1000) / (8 * RAOP_SAMPLE_RATE), 100);
|
||||||
int i;
|
int i;
|
||||||
@@ -489,10 +487,10 @@ static void buffer_push_packet(rtp_t *ctx) {
|
|||||||
|
|
||||||
// there is always at least one frame in the buffer
|
// there is always at least one frame in the buffer
|
||||||
do {
|
do {
|
||||||
|
|
||||||
curframe = ctx->audio_buffer + BUFIDX(ctx->ab_read);
|
curframe = ctx->audio_buffer + BUFIDX(ctx->ab_read);
|
||||||
playtime = ctx->synchro.time + (((s32_t)(curframe->rtptime - ctx->synchro.rtp)) * 1000) / RAOP_SAMPLE_RATE;
|
playtime = ctx->synchro.time + (((s32_t)(curframe->rtptime - ctx->synchro.rtp)) * 1000) / RAOP_SAMPLE_RATE;
|
||||||
|
|
||||||
if (now > playtime) {
|
if (now > playtime) {
|
||||||
LOG_DEBUG("[%p]: discarded frame now:%u missed by:%d (W:%hu R:%hu)", ctx, now, now - playtime, ctx->ab_write, ctx->ab_read);
|
LOG_DEBUG("[%p]: discarded frame now:%u missed by:%d (W:%hu R:%hu)", ctx, now, now - playtime, ctx->ab_write, ctx->ab_read);
|
||||||
ctx->discarded++;
|
ctx->discarded++;
|
||||||
@@ -506,12 +504,13 @@ static void buffer_push_packet(rtp_t *ctx) {
|
|||||||
} else break;
|
} else break;
|
||||||
|
|
||||||
ctx->ab_read++;
|
ctx->ab_read++;
|
||||||
ctx->out_frames++;
|
ctx->out_frames++;
|
||||||
|
|
||||||
// need to be promoted to a signed int *before* addition
|
// need to be promoted to a signed int *before* addition
|
||||||
} while ((s16_t) (ctx->ab_write - ctx->ab_read) + 1 > 0);
|
} while ((s16_t) (ctx->ab_write - ctx->ab_read) + 1 > 0);
|
||||||
|
|
||||||
if (ctx->out_frames > 1000) {
|
if (ctx->out_frames > 1000) {
|
||||||
LOG_INFO("[%p]: drain [level:%hd gap:%d] [W:%hu R:%hu] [R:%u S:%u F:%u D:%u] (head in %u ms) ",
|
LOG_INFO("[%p]: drain [level:%hd head:%d ms] [W:%hu R:%hu] [req:%u sil:%u dis:%u]",
|
||||||
ctx, ctx->ab_write - ctx->ab_read, playtime - now, ctx->ab_write, ctx->ab_read,
|
ctx, ctx->ab_write - ctx->ab_read, playtime - now, ctx->ab_write, ctx->ab_read,
|
||||||
ctx->resent_req, ctx->silent_frames, ctx->discarded);
|
ctx->resent_req, ctx->silent_frames, ctx->discarded);
|
||||||
ctx->out_frames = 0;
|
ctx->out_frames = 0;
|
||||||
@@ -614,7 +613,7 @@ static void *rtp_thread_func(void *arg) {
|
|||||||
u32_t rtp_now_latency = ntohl(*(u32_t*)(pktp+4));
|
u32_t rtp_now_latency = ntohl(*(u32_t*)(pktp+4));
|
||||||
u64_t remote = (((u64_t) ntohl(*(u32_t*)(pktp+8))) << 32) + ntohl(*(u32_t*)(pktp+12));
|
u64_t remote = (((u64_t) ntohl(*(u32_t*)(pktp+8))) << 32) + ntohl(*(u32_t*)(pktp+12));
|
||||||
u32_t rtp_now = ntohl(*(u32_t*)(pktp+16));
|
u32_t rtp_now = ntohl(*(u32_t*)(pktp+16));
|
||||||
|
|
||||||
pthread_mutex_lock(&ctx->ab_mutex);
|
pthread_mutex_lock(&ctx->ab_mutex);
|
||||||
|
|
||||||
// re-align timestamp and expected local playback time (and magic 11025 latency)
|
// re-align timestamp and expected local playback time (and magic 11025 latency)
|
||||||
@@ -628,8 +627,8 @@ static void *rtp_thread_func(void *arg) {
|
|||||||
// 1st sync packet received (signals a restart of playback)
|
// 1st sync packet received (signals a restart of playback)
|
||||||
if (packet[0] & 0x10) {
|
if (packet[0] & 0x10) {
|
||||||
LOG_INFO("[%p]: 1st sync packet received", ctx);
|
LOG_INFO("[%p]: 1st sync packet received", ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_unlock(&ctx->ab_mutex);
|
pthread_mutex_unlock(&ctx->ab_mutex);
|
||||||
|
|
||||||
LOG_DEBUG("[%p]: sync packet latency:%d rtp_latency:%u rtp:%u remote ntp:%llx, local time:%u local rtp:%u (now:%u)",
|
LOG_DEBUG("[%p]: sync packet latency:%d rtp_latency:%u rtp:%u remote ntp:%llx, local time:%u local rtp:%u (now:%u)",
|
||||||
@@ -670,7 +669,7 @@ static void *rtp_thread_func(void *arg) {
|
|||||||
s32_t delta = NTP2MS((s64_t) expected - (s64_t) ctx->timing.remote);
|
s32_t delta = NTP2MS((s64_t) expected - (s64_t) ctx->timing.remote);
|
||||||
ctx->cmd_cb(RAOP_TIMING, &delta);
|
ctx->cmd_cb(RAOP_TIMING, &delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
// now we are synced on NTP (mutex not needed)
|
// now we are synced on NTP (mutex not needed)
|
||||||
ctx->synchro.status |= NTP_SYNC;
|
ctx->synchro.status |= NTP_SYNC;
|
||||||
|
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ void buf_adjust(struct buffer *buf, size_t mod) {
|
|||||||
|
|
||||||
// called with mutex locked to resize, does not retain contents, reverts to original size if fails
|
// called with mutex locked to resize, does not retain contents, reverts to original size if fails
|
||||||
void _buf_resize(struct buffer *buf, size_t size) {
|
void _buf_resize(struct buffer *buf, size_t size) {
|
||||||
|
if (size == buf->size) return;
|
||||||
free(buf->buf);
|
free(buf->buf);
|
||||||
buf->buf = malloc(size);
|
buf->buf = malloc(size);
|
||||||
if (!buf->buf) {
|
if (!buf->buf) {
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ extern struct buffer *outputbuf;
|
|||||||
// this is the only system-wide loglevel variable
|
// this is the only system-wide loglevel variable
|
||||||
extern log_level loglevel;
|
extern log_level loglevel;
|
||||||
|
|
||||||
|
#define RAOP_OUTPUT_SIZE (RAOP_SAMPLE_RATE * 2 * 2 * 2 * 1.2)
|
||||||
|
|
||||||
static raop_event_t raop_state;
|
static raop_event_t raop_state;
|
||||||
static bool raop_expect_stop = false;
|
static bool raop_expect_stop = false;
|
||||||
static struct {
|
static struct {
|
||||||
@@ -165,29 +167,34 @@ void raop_sink_cmd_handler(raop_event_t event, void *param)
|
|||||||
|
|
||||||
raop_sync.total += *(s32_t*) param;
|
raop_sync.total += *(s32_t*) param;
|
||||||
raop_sync.count++;
|
raop_sync.count++;
|
||||||
raop_sync.msplayed = now - output.updated + ((u64_t) (output.frames_played_dmp - output.device_frames) * 1000) / 44100;
|
raop_sync.msplayed = now - output.updated + ((u64_t) (output.frames_played_dmp - output.device_frames) * 1000) / RAOP_SAMPLE_RATE;
|
||||||
error = raop_sync.msplayed - (now - raop_sync.start_time);
|
error = raop_sync.msplayed - (now - raop_sync.start_time);
|
||||||
|
|
||||||
|
LOG_INFO("now:%u updated:%u played_dmp:%u device:%u", now, output.updated, output.frames_played_dmp, output.device_frames);
|
||||||
LOG_INFO("backend played %u, desired %u, (delta:%d raop:%d)", raop_sync.msplayed, now - raop_sync.start_time, error, raop_sync.total / raop_sync.count);
|
LOG_INFO("backend played %u, desired %u, (delta:%d raop:%d)", raop_sync.msplayed, now - raop_sync.start_time, error, raop_sync.total / raop_sync.count);
|
||||||
|
|
||||||
if (error < -10) {
|
if (error < -10) {
|
||||||
output.skip_frames = (abs(error) * 44100) / 1000;
|
output.skip_frames = (abs(error) * RAOP_SAMPLE_RATE) / 1000;
|
||||||
output.state = OUTPUT_SKIP_FRAMES;
|
output.state = OUTPUT_SKIP_FRAMES;
|
||||||
LOG_INFO("skipping %u frames", output.skip_frames);
|
LOG_INFO("skipping %u frames", output.skip_frames);
|
||||||
} else if (error > 10) {
|
} else if (error > 10) {
|
||||||
output.pause_frames = (abs(error) * 44100) / 1000;
|
output.pause_frames = (abs(error) * RAOP_SAMPLE_RATE) / 1000;
|
||||||
output.state = OUTPUT_PAUSE_FRAMES;
|
output.state = OUTPUT_PAUSE_FRAMES;
|
||||||
LOG_INFO("pausing for %u frames", output.pause_frames);
|
LOG_INFO("pausing for %u frames", output.pause_frames);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case RAOP_SETUP:
|
||||||
|
_buf_resize(outputbuf, RAOP_OUTPUT_SIZE);
|
||||||
|
LOG_INFO("resizing buffer %u", outputbuf->size);
|
||||||
|
break;
|
||||||
case RAOP_STREAM:
|
case RAOP_STREAM:
|
||||||
LOG_INFO("Stream", NULL);
|
LOG_INFO("Stream", NULL);
|
||||||
raop_state = event;
|
raop_state = event;
|
||||||
raop_sync.total = raop_sync.count = 0;
|
raop_sync.total = raop_sync.count = 0;
|
||||||
output.external = true;
|
output.external = true;
|
||||||
output.next_sample_rate = 44100;
|
output.next_sample_rate = RAOP_SAMPLE_RATE;
|
||||||
output.state = OUTPUT_STOPPED;
|
output.state = OUTPUT_STOPPED;
|
||||||
break;
|
break;
|
||||||
case RAOP_STOP:
|
case RAOP_STOP:
|
||||||
|
|||||||
@@ -347,6 +347,7 @@ void output_init_common(log_level level, const char *device, unsigned output_buf
|
|||||||
loglevel = level;
|
loglevel = level;
|
||||||
|
|
||||||
output_buf_size = output_buf_size - (output_buf_size % BYTES_PER_FRAME);
|
output_buf_size = output_buf_size - (output_buf_size % BYTES_PER_FRAME);
|
||||||
|
output.init_size = output_buf_size;
|
||||||
LOG_DEBUG("outputbuf size: %u", output_buf_size);
|
LOG_DEBUG("outputbuf size: %u", output_buf_size);
|
||||||
|
|
||||||
buf_init(outputbuf, output_buf_size);
|
buf_init(outputbuf, output_buf_size);
|
||||||
|
|||||||
@@ -371,7 +371,10 @@ static void process_strm(u8_t *pkt, int len) {
|
|||||||
sendSTAT("STMc", 0);
|
sendSTAT("STMc", 0);
|
||||||
sentSTMu = sentSTMo = sentSTMl = false;
|
sentSTMu = sentSTMo = sentSTMl = false;
|
||||||
LOCK_O;
|
LOCK_O;
|
||||||
output.external = false;
|
if (output.external) {
|
||||||
|
output.external = false;
|
||||||
|
_buf_resize(outputbuf, output.init_size);
|
||||||
|
}
|
||||||
output.threshold = strm->output_threshold;
|
output.threshold = strm->output_threshold;
|
||||||
output.next_replay_gain = unpackN(&strm->replay_gain);
|
output.next_replay_gain = unpackN(&strm->replay_gain);
|
||||||
output.fade_mode = strm->transition_type - '0';
|
output.fade_mode = strm->transition_type - '0';
|
||||||
|
|||||||
@@ -656,6 +656,7 @@ struct outputstate {
|
|||||||
output_format format;
|
output_format format;
|
||||||
const char *device;
|
const char *device;
|
||||||
bool external;
|
bool external;
|
||||||
|
u32_t init_size;
|
||||||
#if ALSA
|
#if ALSA
|
||||||
unsigned buffer;
|
unsigned buffer;
|
||||||
unsigned period;
|
unsigned period;
|
||||||
|
|||||||
Reference in New Issue
Block a user