Browse Source

finalize AirPlay sync

philippe44 5 years ago
parent
commit
f547a52c1e
3 changed files with 57 additions and 29 deletions
  1. 1 1
      components/raop/raop_sink.h
  2. 15 12
      components/raop/rtp.c
  3. 41 16
      components/squeezelite/decode_external.c

+ 1 - 1
components/raop/raop_sink.h

@@ -16,7 +16,7 @@
 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_data_cb_t)(const u8_t *data, size_t len);
+typedef void (*raop_data_cb_t)(const u8_t *data, size_t len, u32_t playtime);
 
 /**
  * @brief     init sink mode (need to be provided)

+ 15 - 12
components/raop/rtp.c

@@ -70,8 +70,10 @@
 //#define __RTP_STORE
 
 // default buffer size
-#define BUFFER_FRAMES ( (150 * 88200) / (352 * 100) )
-#define MAX_PACKET    1408
+#define BUFFER_FRAMES 	( (150 * RAOP_SAMPLE_RATE * 2) / (352 * 100) )
+#define MAX_PACKET    	1408
+#define MIN_LATENCY		11025
+#define MAX_LATENCY   	( (120 * RAOP_SAMPLE_RATE * 2) / 100 )
 
 #define RTP_SYNC	(0x01)
 #define NTP_SYNC	(0x02)
@@ -396,13 +398,13 @@ static void buffer_put_packet(rtp_t *ctx, seq_t seqno, unsigned rtptime, bool fi
 
 	if (!ctx->playing) {
 		if ((ctx->flush_seqno == -1 || seq_order(ctx->flush_seqno, seqno)) &&
-		   (ctx->synchro.status & RTP_SYNC && ctx->synchro.status & NTP_SYNC)) {
+		   (ctx->synchro.status & (RTP_SYNC | NTP_SYNC))) {
 			ctx->ab_write = seqno-1;
 			ctx->ab_read = seqno;
 			ctx->flush_seqno = -1;
 			ctx->playing = true;
 			ctx->resent_req = ctx->resent_rec = ctx->silent_frames = ctx->discarded = 0;
-			playtime = ctx->synchro.time + (((s32_t)(rtptime - ctx->synchro.rtp)) * 10) / 441;
+			playtime = ctx->synchro.time + (((s32_t)(rtptime - ctx->synchro.rtp)) * 1000) / RAOP_SAMPLE_RATE;
 			ctx->cmd_cb(RAOP_PLAY, &playtime);
 		} else {
 			pthread_mutex_unlock(&ctx->ab_mutex);
@@ -493,11 +495,11 @@ static void buffer_push_packet(rtp_t *ctx) {
 			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++;
 		} else if (curframe->ready) {
-			ctx->data_cb((const u8_t*) curframe->data, curframe->len);
+			ctx->data_cb((const u8_t*) curframe->data, curframe->len, playtime);
 			curframe->ready = 0;
 		} else if (playtime - now <= hold) {
 			LOG_DEBUG("[%p]: created zero frame (W:%hu R:%hu)", ctx, ctx->ab_write, ctx->ab_read);
-			ctx->data_cb(silence_frame, ctx->frame_size * 4);
+			ctx->data_cb(silence_frame, ctx->frame_size * 4, playtime);
 			ctx->silent_frames++;
 		} else break;
 
@@ -609,11 +611,15 @@ static void *rtp_thread_func(void *arg) {
 				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));
 				u32_t rtp_now = ntohl(*(u32_t*)(pktp+16));
+				u16_t flags = ntohs(*(u16_t*)(pktp+2));
 
 				pthread_mutex_lock(&ctx->ab_mutex);
 
 				// re-align timestamp and expected local playback time (and magic 11025 latency)
-				ctx->latency = rtp_now - rtp_now_latency + 11025;
+				ctx->latency = rtp_now - rtp_now_latency;
+				if (flags == 7 || flags == 4) ctx->latency += 11025;
+				if (ctx->latency < MIN_LATENCY) ctx->latency = MIN_LATENCY;
+				else if (ctx->latency > MAX_LATENCY) ctx->latency = MAX_LATENCY;
 				ctx->synchro.rtp = rtp_now - ctx->latency;
 				ctx->synchro.time = ctx->timing.local + (u32_t) NTP2MS(remote - ctx->timing.remote);
 
@@ -635,6 +641,8 @@ static void *rtp_thread_func(void *arg) {
 					count = 3;
 				}
 
+				if (ctx->synchro.status & (NTP_SYNC | RTP_SYNC)) ctx->cmd_cb(RAOP_TIMING, NULL);
+
 				break;
 			}
 
@@ -661,11 +669,6 @@ static void *rtp_thread_func(void *arg) {
 				ctx->timing.remote = remote;
 				ctx->timing.local = reference;
 
-				if (ctx->synchro.status & NTP_SYNC) {
-					s32_t delta = NTP2MS((s64_t) expected - (s64_t) ctx->timing.remote);
-					ctx->cmd_cb(RAOP_TIMING, &delta);
-				}
-
 				// now we are synced on NTP (mutex not needed)
 				ctx->synchro.status |= NTP_SYNC;
 

+ 41 - 16
components/squeezelite/decode_external.c

@@ -39,8 +39,10 @@ extern log_level loglevel;
 static raop_event_t	raop_state;
 static bool raop_expect_stop = false;
 static struct {
-	s32_t total, count;
-	u32_t start_time, msplayed;
+	bool start;
+	s32_t error;
+	u32_t start_time;
+	u32_t playtime, len;
 } raop_sync;
 
 /****************************************************************************************
@@ -142,6 +144,17 @@ static void bt_sink_cmd_handler(bt_sink_cmd_t cmd, ...)
 	va_end(args);
 }
 
+/****************************************************************************************
+ * raop sink data handler
+ */
+static void raop_sink_data_handler(const uint8_t *data, uint32_t len, u32_t playtime) {
+	
+	raop_sync.playtime = playtime;
+	raop_sync.len = len;
+	
+	sink_data_handler(data, len);
+}	
+
 /****************************************************************************************
  * AirPlay sink command handler
  */
@@ -160,28 +173,39 @@ void raop_sink_cmd_handler(raop_event_t event, void *param)
 	// this is async, so player might have been deleted
 	switch (event) {
 		case RAOP_TIMING: {
-			u32_t now = gettime_ms();
+			u32_t ms, now = gettime_ms();
 			s32_t error;
 			
 			if (output.state < OUTPUT_RUNNING || output.frames_played_dmp < output.device_frames) break;
 			
-			raop_sync.total += *(s32_t*) param;
-			raop_sync.count++;
-			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);
-			
-			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);
+			// first must make sure we started on time
+			if (raop_sync.start) {
+				// how many ms have we really played
+				ms = now - output.updated + ((u64_t) (output.frames_played_dmp - output.device_frames) * 1000) / RAOP_SAMPLE_RATE;
+				error = ms - (now - raop_sync.start_time); 
+				LOG_INFO("backend played %u, desired %u, (delta:%d)", ms, now - raop_sync.start_time, error);
+				if (abs(error) < 10 && abs(raop_sync.error) < 10) raop_sync.start = false;
+			} else {	
+				// in how many ms will the most recent block play (there is one extra block of FRAME_BLOCK == MAX_SILENCE_FRAMES which is not in queue and not in outputbuf)
+				ms = ((u64_t) ((_buf_used(outputbuf) - raop_sync.len) / BYTES_PER_FRAME + output.device_frames + MAX_SILENCE_FRAMES) * 1000) / RAOP_SAMPLE_RATE - (now - output.updated);
+				error = (raop_sync.playtime - now) - ms;
+				LOG_INFO("head local:%u, remote:%u (delta:%d)", ms, raop_sync.playtime - now, error);
+				//break;
+			}	
 			
-			if (error < -10) {
-				output.skip_frames = (abs(error) * RAOP_SAMPLE_RATE) / 1000;
+			if (error < -10 && raop_sync.error < -10) {
+				output.skip_frames = (abs(error + raop_sync.error) / 2 * RAOP_SAMPLE_RATE) / 1000;
 				output.state = OUTPUT_SKIP_FRAMES;					
+				raop_sync.error = 0;
 				LOG_INFO("skipping %u frames", output.skip_frames);
-			} else if (error > 10) {
-				output.pause_frames = (abs(error) * RAOP_SAMPLE_RATE) / 1000;
+			} else if (error > 10 && raop_sync.error > 10) {
+				output.pause_frames = (abs(error + raop_sync.error) / 2 * RAOP_SAMPLE_RATE) / 1000;
 				output.state = OUTPUT_PAUSE_FRAMES;
+				raop_sync.error = 0;
 				LOG_INFO("pausing for %u frames", output.pause_frames);
 			}
+			
+			raop_sync.error = error;
 						
 			break;
 		}
@@ -192,7 +216,8 @@ void raop_sink_cmd_handler(raop_event_t event, void *param)
 		case RAOP_STREAM:
 			LOG_INFO("Stream", NULL);
 			raop_state = event;
-			raop_sync.total = raop_sync.count = 0;			
+			raop_sync.error = 0;
+			raop_sync.start = true;			
 			output.external = true;
 			output.next_sample_rate = RAOP_SAMPLE_RATE;
 			output.state = OUTPUT_STOPPED;
@@ -253,7 +278,7 @@ void register_other(void) {
 #endif	
 #ifdef CONFIG_AIRPLAY_SINK
 	if (!strcasestr(output.device, "BT ")) {
-		raop_sink_init(raop_sink_cmd_handler, sink_data_handler);
+		raop_sink_init(raop_sink_cmd_handler, raop_sink_data_handler);
 		LOG_INFO("Initializing AirPlay sink");		
 	} else {
 		LOG_WARN("Cannot be an AirPlay sink and BT source");