summaryrefslogtreecommitdiff
path: root/src/libsink/sink.c
blob: 80f40cd3721f065588575b6612c117119dd42678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
#define AL_LOG_SECTION "sink"
//#define AL_LOG_ENABLE_TRACE
#include <al/log.h>
#include <al/random.h>
#include <nnwt/multiplex.h>

#include "../server/common.h"
#include "../buffer/common.h"
#include "../liana/list.h"

#include "sink.h"
#include "common.h"

//#define CAMU_SINK_ONESHOT

// Requested state of the sinks outputs.
enum {
    SINK_PAUSED = 0,
    SINK_PLAYING
};

enum {
    // Created.
    BUFFER_INIT = 0,
    // Set but not configured.
    BUFFER_QUEUED,
    // Errored buffer was removed, treat it as empty from now on.
    BUFFER_DETACHED,
    // Ready to receive data.
    BUFFER_CONFIGURED,
    // The next call to add can add the buffer.
    BUFFER_SET_OR_BUFFERED,
    // Treat the buffer like it's added, even if it might not be.
    BUFFER_ADDED,
    // Effectively SET_OR_BUFFERED but not addable until after a reset.
    BUFFER_ENDED,
    // Same as BUFFER_ENDED but will be detached on a reset.
    BUFFER_ERRORED
};

// Command queue commands.
enum {
    // Sink operations.
    START = 0,
    STOP,
    ADD_BUFFER,
    REMOVE_BUFFER,
    CLEAR_BUFFERS,
    EJECT_ENTRY,
    CLOSE,
    // List actions.
    SKIP,
    TOGGLE_PAUSE,
    SEEK,
    RESEEK,
    SHUFFLE,
    END
};

// Number of entries to keep buffered at one time.
#define ENTRY_MAX_AGE 4
#define SINK_LRU_MAX UINT16_MAX
AL_STATIC_ASSERT(max_age_lt_lru, ENTRY_MAX_AGE, <, SINK_LRU_MAX);

// Store connection number in the upper 16 bits so IDs don't conflict after a server restart.
// If the sink disconnects but the server didn't restart, this will invalidate IDs that _do_ map
// to the same resource. That could be wasteful. It's also currently (ab)used by reseek().
#define LOCAL_ENTRY_ID(sink, id) (((u64)(sink)->connection_number) << 48 | (u64)(id))
#define REMOTE_ENTRY_ID(id) ((u32)(id & 0x7fffffff))
#define CONNECTION_NUMBER(id) ((id >> 48) & 0xffff)

// Only sink->target can ever be 0xb00b.
#define ENTRY_IS_VALID(entry) ((entry) && (entry) != (struct camu_sink_entry *)0xb00b)

// printf format for entries.
#ifdef AL_DEBUG
#define ENTRY_FMT "#%u(%p)" // @TODO: Put static and ended here at least.
#define ENTRY_ARG(entry) (ENTRY_IS_VALID(entry) ? REMOTE_ENTRY_ID((entry)->id) : 0), ((entry) ? (entry) : 0x0)
#else
#define ENTRY_FMT "#%u"
#define ENTRY_ARG(entry) (ENTRY_IS_VALID(entry) ? REMOTE_ENTRY_ID((entry)->id) : 0)
#endif

#define AUDIO_STATE(entry) ((entry)->audio.state)
#define VIDEO_STATE(entry) ((entry)->video.state)

// If a buffer is still INIT or QUEUED after the entry is configured, it's "empty".
// An ERRORED buffer will be DETACHED after CLIENT_REMOVE_BUFFERS and is then considered empty.
// Empty is a state that cannot change while a buffer could be in use (push()/read()).
#define AUDIO_EMPTY(entry) (AUDIO_STATE(entry) <= BUFFER_DETACHED)
#define VIDEO_EMPTY(entry) (VIDEO_STATE(entry) <= BUFFER_DETACHED)

#define AUDIO_ENDED(entry) (AUDIO_STATE(entry) >= BUFFER_ENDED)
#define VIDEO_ENDED(entry) (VIDEO_STATE(entry) >= BUFFER_ENDED)

#define AUDIO_STREAM(entry) ((al_assert(!AUDIO_EMPTY(entry)), (entry)->audio.buf.stream))
#define VIDEO_STREAM(entry) ((al_assert(!VIDEO_EMPTY(entry)), (entry)->video.buf.stream))
#define VIDEO_IS_SINGLE_FRAME(entry) ((al_assert(!VIDEO_EMPTY(entry)), (entry)->video.buf.single_frame))

#define ENTRY_EVAL_ENDED(entry) \
    ((AUDIO_ENDED(entry) && (VIDEO_ENDED(entry) || VIDEO_EMPTY(entry) || VIDEO_IS_SINGLE_FRAME(entry))) || \
     (AUDIO_EMPTY(entry) && VIDEO_ENDED(entry)))
#define ENTRY_ENDED(entry) (al_assert(entry->ended == ENTRY_EVAL_ENDED(entry)), entry->ended)

// IGNORED = ENDED or EMPTY.
#define AUDIO_ADDED_OR_IGNORED(entry) (AUDIO_STATE(entry) >= BUFFER_ADDED || AUDIO_EMPTY(entry))
#define VIDEO_ADDED_OR_IGNORED(entry) (VIDEO_STATE(entry) >= BUFFER_ADDED || VIDEO_EMPTY(entry))

#define AUDIO_ENDED_OR_EMPTY(entry) (AUDIO_ENDED(entry) || AUDIO_EMPTY(entry))
#define VIDEO_ENDED_OR_EMPTY(entry) (VIDEO_ENDED(entry) || VIDEO_EMPTY(entry))

#if defined CAMU_SCREEN_THREADED && defined CAMU_MIXER_THREADED
#define BLOCKING_SLEEP(sink, delay) ((void)sink, nn_thread_sleep(delay))
#else
#define BLOCKING_SLEEP(sink, delay) nn_event_loop_sleep(sink->loop, delay)
#endif

#define CMD(cmd, ...) ((struct camu_sink_cmd){ .op = cmd, __VA_ARGS__ })

// Functions that might happen on separate threads.
// CAMU_MIXER_THREADED:
//  audio_buffer_callback()
//  mixer_callback()
// CAMU_SCREEN_THREADED:
//  video_buffer_callback()
// CAMU_MIXER_THREADED or CAMU_SCREEN_THREADED:
//  clock_callback()
// client_callback()::LIANA_CLIENT_DATA
// client_callback()::LIANA_CLIENT_EOF/ERRORED

static inline bool entry_audio_buffer_held(struct camu_sink_entry *entry)
{
#ifdef CAMU_MIXER_THREADED
    return atomic_load(bool)(&entry->audio.buf.ref, AL_ATOMIC_RELAXED);
#else
    (void)entry;
    return false;
#endif
}

static inline bool entry_video_buffer_held(struct camu_sink_entry *entry)
{
#ifdef CAMU_SCREEN_THREADED
    return atomic_load(bool)(&entry->video.buf.ref, AL_ATOMIC_RELAXED);
#else
    (void)entry;
    return false;
#endif
}

static void queue_cmds(struct camu_sink *sink, u32 count, ...)
{
    camu_queue_lock(sink->queue);
    va_list cmds;
    va_start(cmds, count);
    for (u32 i = 0; i < count; i++) {
        camu_queue_push(sink->queue, va_arg(cmds, struct camu_sink_cmd));
    }
    camu_queue_unlock(sink->queue);
    nn_signal_send(&sink->queue_signal);
}

static void queue_cmd(struct camu_sink *sink, struct camu_sink_cmd cmd)
{
    queue_cmds(sink, 1, cmd);
}

static void refresh_video_output(struct camu_sink *sink)
{
#ifndef CAMU_SINK_NO_VIDEO
    sink->callback(sink->userdata, CAMU_SINK_REFRESH_VIDEO, 0, NULL);
#else
    (void)sink;
#endif
}

static inline void add_entry_audio_buffer(struct camu_sink_entry *entry)
{
    log_trace("add_entry_audio_buffer("ENTRY_FMT").", ENTRY_ARG(entry));
    struct camu_sink *sink = entry->sink;
#ifdef CAMU_MIXER_THREADED_START_STOP
    sink->callback(sink->userdata, CAMU_SINK_ADD_BUFFER, CAMU_SINK_AUDIO, &entry->audio.buf);
#else
    queue_cmd(sink, CMD(ADD_BUFFER, .v.u = CAMU_SINK_AUDIO, .opaque = entry));
#endif
}

static inline void add_entry_video_buffer(struct camu_sink_entry *entry)
{
#ifndef CAMU_SINK_NO_VIDEO
    log_trace("add_entry_video_buffer("ENTRY_FMT").", ENTRY_ARG(entry));
    struct camu_sink *sink = entry->sink;
    sink->callback(sink->userdata, CAMU_SINK_ADD_BUFFER, CAMU_SINK_VIDEO, &entry->video.buf);
#else
    (void)entry;
#endif
}

static void remove_entry_audio_buffer(struct camu_sink_entry *entry)
{
    al_assert(!AUDIO_ENDED(entry));
    al_assert(AUDIO_STATE(entry) != BUFFER_INIT);
    log_trace("remove_entry_audio_buffer("ENTRY_FMT"), do_remove: %s.", ENTRY_ARG(entry),
        BOOLSTR(AUDIO_STATE(entry) == BUFFER_ADDED));
    switch (AUDIO_STATE(entry)) {
    case BUFFER_ADDED:
        AUDIO_STATE(entry) = BUFFER_SET_OR_BUFFERED;
        struct camu_sink *sink = entry->sink;
#ifdef CAMU_MIXER_THREADED_START_STOP
        sink->callback(sink->userdata, CAMU_SINK_REMOVE_BUFFER, CAMU_SINK_AUDIO, &entry->audio.buf);
#else
        queue_cmd(sink, CMD(REMOVE_BUFFER, .v.u = CAMU_SINK_AUDIO, .opaque = entry));
#endif
        break;
    case BUFFER_SET_OR_BUFFERED:
        AUDIO_STATE(entry) = BUFFER_CONFIGURED;
        break;
    case BUFFER_QUEUED:
        AUDIO_STATE(entry) = BUFFER_INIT;
        break;
    }
}

static void remove_entry_video_buffer(struct camu_sink_entry *entry)
{
    // Don't assert !entry->ended here because of single frame handling.
    al_assert(!VIDEO_ENDED(entry));
    al_assert(VIDEO_STATE(entry) != BUFFER_INIT);
    log_trace("remove_entry_video_buffer("ENTRY_FMT"), do_remove: %s.", ENTRY_ARG(entry),
        BOOLSTR(VIDEO_STATE(entry) == BUFFER_ADDED));
    switch (VIDEO_STATE(entry)) {
    case BUFFER_ADDED:
        VIDEO_STATE(entry) = BUFFER_SET_OR_BUFFERED;
#ifndef CAMU_SINK_NO_VIDEO
        struct camu_sink *sink = entry->sink;
        sink->callback(sink->userdata, CAMU_SINK_REMOVE_BUFFER, CAMU_SINK_VIDEO, &entry->video.buf);
#endif
        break;
    case BUFFER_SET_OR_BUFFERED:
        VIDEO_STATE(entry) = BUFFER_CONFIGURED;
        break;
    case BUFFER_QUEUED:
        // This can be hit when skipping through entries very fast.
        VIDEO_STATE(entry) = BUFFER_INIT;
        break;
    }
}

// It's possible for some of an entry's buffers to be ENDED while others are still ADDED.
// This means entry->ended and BUFFER_ENDED have two distinct considerations.
// entry->ended: Completely ignored and needs special handling in CLIENT_REMOVE_BUFFERS.
// BUFFER_ENDED: No-op'd in remove_entry_buffers(), add_or_queue_entry() and not added by do_add_entry(), otherwise unchanged.
//  Another note: remove_entry_buffers() and add_or_queue_entry() are only ever called by switch_to().
//   switch_to() -> add_or_queue_entry().
//   switch_to() -> maybe_add_to_previous() -> (possibly delayed)remove_entry_buffers().
static void remove_entry_buffers(struct camu_sink_entry *entry)
{
    log_trace("remove_entry_buffers("ENTRY_FMT"), audio_state: %hhu, video_state: %hhu.",
        ENTRY_ARG(entry), AUDIO_STATE(entry), VIDEO_STATE(entry));
    if (!AUDIO_ENDED(entry)) remove_entry_audio_buffer(entry);
    if (!VIDEO_ENDED(entry)) remove_entry_video_buffer(entry);
}

// Disconnecting a packet stream twice before a reconnect is an error.
static void maybe_disconnect_entry(struct camu_sink_entry *entry)
{
    if (!entry->disconnected) {
        // disconnect() could free entry.
        entry->disconnected = true;
        lia_client_disconnect(&entry->client);
    }
}

// sink->current could be NULL.
static inline struct camu_sink_entry *get_entry_for_command(struct camu_sink *sink)
{
    return ENTRY_IS_VALID(sink->target) ? sink->target : sink->current;
}

static inline s32 get_sequence_for_command(struct camu_sink *sink, struct camu_sink_entry *entry)
{
    // If local, using entry->sequence could only lead to feeling like your inputs were eaten.
    if (!sink->local && entry) {
        return entry->sequence;
    }
    // SEQUENCE_ANY resolves order on the server.
    return LIANA_SEQUENCE_ANY;
}

static void handle_sink_cmd(struct camu_sink *sink, struct camu_sink_cmd *cmd)
{
    switch (cmd->op) {
    case START: {
        switch (cmd->v.u) {
        case CAMU_SINK_AUDIO:
            if (sink->audio.state == SINK_PAUSED) {
                sink->callback(sink->userdata, CAMU_SINK_START, CAMU_SINK_AUDIO, NULL);
                sink->audio.state = SINK_PLAYING;
            }
            break;
        case CAMU_SINK_VIDEO:
            if (sink->video.state == SINK_PAUSED) {
#ifndef CAMU_SINK_NO_VIDEO
                sink->callback(sink->userdata, CAMU_SINK_START, CAMU_SINK_VIDEO, NULL);
#endif
                sink->video.state = SINK_PLAYING;
            }
            break;
        }
        break;
    }
    case STOP: {
        switch (cmd->v.u) {
        case CAMU_SINK_AUDIO:
            if (sink->audio.state == SINK_PLAYING) {
                sink->callback(sink->userdata, CAMU_SINK_STOP, CAMU_SINK_AUDIO, NULL);
                sink->audio.state = SINK_PAUSED;
            }
            break;
        case CAMU_SINK_VIDEO:
            if (sink->video.state == SINK_PLAYING) {
#ifndef CAMU_SINK_NO_VIDEO
                sink->callback(sink->userdata, CAMU_SINK_STOP, CAMU_SINK_VIDEO, NULL);
#endif
                sink->video.state = SINK_PAUSED;
            }
            break;
        }
        break;
    }
    case ADD_BUFFER: {
        struct camu_sink_entry *entry = (struct camu_sink_entry *)cmd->opaque;
        switch (cmd->v.u) {
        case CAMU_SINK_AUDIO:
            sink->callback(sink->userdata, CAMU_SINK_ADD_BUFFER, CAMU_SINK_AUDIO, &entry->audio.buf);
            break;
        case CAMU_SINK_VIDEO:
#ifndef CAMU_SINK_NO_VIDEO
            sink->callback(sink->userdata, CAMU_SINK_ADD_BUFFER, CAMU_SINK_VIDEO, &entry->video.buf);
#endif
            break;
        }
        break;
    }
    case REMOVE_BUFFER: {
        struct camu_sink_entry *entry = (struct camu_sink_entry *)cmd->opaque;
        switch (cmd->v.u) {
        case CAMU_SINK_AUDIO:
            sink->callback(sink->userdata, CAMU_SINK_REMOVE_BUFFER, CAMU_SINK_AUDIO, &entry->audio.buf);
            break;
        case CAMU_SINK_VIDEO:
#ifndef CAMU_SINK_NO_VIDEO
            sink->callback(sink->userdata, CAMU_SINK_REMOVE_BUFFER, CAMU_SINK_VIDEO, &entry->video.buf);
#endif
            break;
        }
        break;
    }
    case CLEAR_BUFFERS: {
        switch (cmd->v.u) {
        case CAMU_SINK_AUDIO:
            sink->callback(sink->userdata, CAMU_SINK_CLEAR, CAMU_SINK_AUDIO, NULL);
            break;
        case CAMU_SINK_VIDEO:
#ifndef CAMU_SINK_NO_VIDEO
            sink->callback(sink->userdata, CAMU_SINK_CLEAR, CAMU_SINK_VIDEO, NULL);
#endif
            break;
        }
        break;
    }
    case EJECT_ENTRY: {
        struct camu_sink_entry *entry = (struct camu_sink_entry *)cmd->opaque;
        maybe_disconnect_entry(entry);
        break;
    }
    case CLOSE: {
        nn_signal_stop(&sink->queue_signal);
        sink->callback(sink->userdata, CAMU_SINK_EXIT, 0, NULL);
        return;
    }
    case SKIP: {
        if (!sink->connected) return;
        struct camu_sink_entry *entry = (struct camu_sink_entry *)cmd->opaque;
        struct nn_packet *packet = nn_rpc_get_packet(&sink->client, CAMU_SERVER_LIST_ACTION);
        nn_packet_write_str(packet, &sink->default_list);
        nn_packet_write_u8(packet, CAMU_LIST_SKIP);
        nn_packet_write_s32(packet, get_sequence_for_command(sink, entry));
        nn_packet_write_s32(packet, (s32)cmd->v.i);
        nn_rpc_connection_command(sink->conn, packet, NULL, NULL);
        break;
    }
    case TOGGLE_PAUSE: {
        if (!sink->connected) return;
        struct camu_sink_entry *entry = (struct camu_sink_entry *)cmd->opaque;
        struct nn_packet *packet = nn_rpc_get_packet(&sink->client, CAMU_SERVER_LIST_ACTION);
        nn_packet_write_str(packet, &sink->default_list);
        nn_packet_write_u8(packet, CAMU_LIST_TOGGLE_PAUSE);
        nn_packet_write_s32(packet, get_sequence_for_command(sink, entry));
        nn_packet_write_f64(packet, cmd->v.f);
        nn_rpc_connection_command(sink->conn, packet, NULL, NULL);
        break;
    }
    case SEEK: {
        if (!sink->connected) return;
        struct camu_sink_entry *entry = (struct camu_sink_entry *)cmd->opaque;
        struct nn_packet *packet = nn_rpc_get_packet(&sink->client, CAMU_SERVER_LIST_ACTION);
        nn_packet_write_str(packet, &sink->default_list);
        nn_packet_write_u8(packet, CAMU_LIST_SEEK);
        nn_packet_write_s32(packet, entry->sequence);
        nn_packet_write_u32(packet, REMOTE_ENTRY_ID(entry->id));
        nn_packet_write_u64(packet, cmd->v.u);
        nn_rpc_connection_command(sink->conn, packet, NULL, NULL);
        break;
    }
    case RESEEK: {
        // Crude way to trigger "re-add sink to list".
        if (sink->conn) nn_rpc_conn_disconnect(sink->conn);
        break;
    }
    case SHUFFLE: {
        if (!sink->connected) return;
        struct nn_packet *packet = nn_rpc_get_packet(&sink->client, CAMU_SERVER_LIST_ACTION);
        nn_packet_write_str(packet, &sink->default_list);
        nn_packet_write_u8(packet, CAMU_LIST_SHUFFLE);
        nn_rpc_connection_command(sink->conn, packet, NULL, NULL);
        break;
    }
    case END: {
        if (!sink->connected) return;
        struct camu_sink_entry *entry = (struct camu_sink_entry *)cmd->opaque;
        struct nn_packet *packet = nn_rpc_get_packet(&sink->client, CAMU_SERVER_LIST_ACTION);
        nn_packet_write_str(packet, &sink->default_list);
        nn_packet_write_u8(packet, CAMU_LIST_END);
        nn_packet_write_u32(packet, REMOTE_ENTRY_ID(entry->id));
        nn_packet_write_u32(packet, (u32)cmd->v.u);
        nn_rpc_connection_command(sink->conn, packet, NULL, NULL);
        break;
    }
    }
}

static void queue_signal_callback(void *userdata)
{
    struct camu_sink *sink = (struct camu_sink *)userdata;
    u32 count;
    struct camu_sink_cmd cmd;
    for (;;) {
        camu_queue_try_pop(sink->queue, count, cmd);
        if (count == 0) break;
        handle_sink_cmd(sink, &cmd);
    }
}

static void mixer_callback(void *userdata, u8 op)
{
    struct camu_sink *sink = (struct camu_sink *)userdata;
    if (op == CAMU_MIXER_EMPTY) {
        log_info("Mixer empty.");
        // Regardless of if we are checking an entry's state, we have to sync
        // with do_add_entry() because the order of START/STOPs in the queue matters.
        nn_mutex_lock(&sink->lock);
        // This check is too loose. Still about as good as any naive implementation
        // of SINK_EMPTY, though.
        if (!(sink->current && AUDIO_STATE(sink->current) == BUFFER_ADDED)) {
            queue_cmd(sink, CMD(STOP, .v.u = CAMU_SINK_AUDIO));
        }
        nn_mutex_unlock(&sink->lock);
    }
}

static s32 entry_lru_compare(const void *a, const void *b)
{
    struct camu_sink_entry *aa = *((struct camu_sink_entry **)a);
    struct camu_sink_entry *bb = *((struct camu_sink_entry **)b);
    if (aa->lru > bb->lru) return -1;
    else if (aa->lru < bb->lru) return 1;
    return 0;
}

static void maybe_cleanup_old_entries(struct camu_sink *sink)
{
    al_array_sort(sink->entries, struct camu_sink_entry *, entry_lru_compare);
    // We check size <= MAX_AGE in the loops because sink->lru is not
    // indicative of the amount of entries we have loaded.
    // The most obvious reason being it's incremented when moving back
    // and forth between two entries. As well as for buffer and queue operations.
    // We have to handle sink->lru wrapping in a step before the default case.
    // 0 65532 65533 65534 65535
    // 0 1 65533 65534 65535
    // 0 1 2 65534 65535
    // 0 1 2 3 65535
    // 0 1 2 3 4
    struct camu_sink_entry *entry;
    al_array_foreach_rev(sink->entries, i, entry) {
        if (sink->entries.count <= ENTRY_MAX_AGE) return;
        u16 entry_age = (SINK_LRU_MAX - entry->lru) + sink->lru;
        if (entry->lru > sink->lru && entry_age > ENTRY_MAX_AGE) {
            al_array_remove_at(sink->entries, i);
            maybe_disconnect_entry(entry);
        }
    }
    // Make sure we don't have to consider wrapping in the second loop.
    if (sink->lru < ENTRY_MAX_AGE) return;
    al_array_foreach_rev(sink->entries, i, entry) {
        al_assert(sink->lru >= entry->lru);
        if (sink->entries.count <= ENTRY_MAX_AGE) return;
        u16 entry_age = sink->lru - entry->lru;
        if (entry_age > ENTRY_MAX_AGE) {
            al_array_remove_at(sink->entries, i);
            maybe_disconnect_entry(entry);
        }
    }
}

static void maybe_run_previous(struct camu_sink *sink)
{
    log_trace("maybe_run_previous(), previous_count: %u.", sink->previous.count);
    struct camu_sink_entry *previous;
    al_array_foreach(sink->previous, i, previous) {
        remove_entry_buffers(previous);
        // Cleanup entries from old connections. Especially important to
        // keep reseek() from being overly wasteful.
        if (CONNECTION_NUMBER(previous->id) != sink->connection_number) {
            queue_cmd(sink, CMD(EJECT_ENTRY, .opaque = previous));
        }
    }
    sink->previous.count = 0;
}

// Due to the looseness of the previous queue, we may have to explicitly remove an
// entry if it becomes incorrect to attempt removing it's buffers. The most obvious example
// being at the point it's freed.
static void run_previous_if_contains(struct camu_sink *sink, struct camu_sink_entry *key)
{
    bool removed = false;
    struct camu_sink_entry *previous;
    al_array_foreach(sink->previous, i, previous) {
        if (previous == key) {
            maybe_run_previous(sink);
            removed = true;
            break;
        }
    }
    log_trace("run_previous_if_contains("ENTRY_FMT"), removed: %s.", ENTRY_ARG(key), BOOLSTR(removed));
}

static bool maybe_remove_from_previous(struct camu_sink *sink, struct camu_sink_entry *entry)
{
    bool removed = false;
    struct camu_sink_entry *previous;
    al_array_foreach(sink->previous, i, previous) {
        if (previous == entry) {
            al_array_remove_at(sink->previous, i);
            removed = true;
            break;
        }
    }
    log_trace("maybe_remove_from_previous("ENTRY_FMT"), removed: %s.", ENTRY_ARG(entry), BOOLSTR(removed));
    return removed;
}

// Every call to maybe_add_to_previous() must map to a remove_entry_buffers().
static void maybe_add_to_previous(struct camu_sink *sink, struct camu_sink_entry *previous,
    struct camu_sink_entry *target)
{
    log_trace("maybe_add_to_previous("ENTRY_FMT"), target: "ENTRY_FMT".", ENTRY_ARG(previous), ENTRY_ARG(target));
    al_assert(previous != target);
    bool ignore_target = target == (struct camu_sink_entry *)0xb00b || ENTRY_ENDED(target);
    if (ignore_target || (AUDIO_STATE(previous) != BUFFER_ADDED && VIDEO_STATE(previous) != BUFFER_ADDED)) {
        remove_entry_buffers(previous);
        return;
    }
    al_array_push(sink->previous, previous);
}

static void after_add_entry(struct camu_sink_entry *entry, bool skip_audio, bool skip_video)
{
    struct camu_sink *sink = entry->sink;
    maybe_run_previous(sink);
    queue_cmds(entry->sink, 2,
        CMD((skip_video || entry->paused) ? STOP : START, .v.u = CAMU_SINK_VIDEO),
        CMD((skip_audio || entry->paused) ? STOP : START, .v.u = CAMU_SINK_AUDIO)
    );
    // Clear the screen if skipping from a video to an audio-only entry.
    if (VIDEO_ENDED_OR_EMPTY(entry)) refresh_video_output(sink);
}

// Call this after setting state to ADDED because this entry might be in previous.
static void do_add_entry(struct camu_sink_entry *entry)
{
    bool skip_audio = AUDIO_ENDED_OR_EMPTY(entry);
    // Single frames are unconditionally added in add_video_if_set_and_buffered().
    bool skip_video = VIDEO_ENDED_OR_EMPTY(entry) || VIDEO_IS_SINGLE_FRAME(entry);
    if (!skip_audio && !skip_video) {
        al_assert(VIDEO_STATE(entry) == AUDIO_STATE(entry));
    }
    // Whether to add audio or video first could be a consideration for responsiveness.
    if (!skip_video) add_entry_video_buffer(entry);
    if (!skip_audio) add_entry_audio_buffer(entry);
    after_add_entry(entry, skip_audio, skip_video);
}

static void add_audio_if_set_and_buffered(struct camu_sink_entry *entry)
{
    al_assert(!ENTRY_ENDED(entry));
    al_assert(AUDIO_STATE(entry) != BUFFER_INIT);
    al_assert(AUDIO_STATE(entry) != BUFFER_QUEUED);
    al_assert(AUDIO_STATE(entry) != BUFFER_ADDED);
    al_assert(!AUDIO_ENDED(entry));
    switch (AUDIO_STATE(entry)) {
    case BUFFER_CONFIGURED:
        AUDIO_STATE(entry) = BUFFER_SET_OR_BUFFERED;
        break;
    case BUFFER_SET_OR_BUFFERED:
        AUDIO_STATE(entry) = BUFFER_ADDED;
        if (VIDEO_ADDED_OR_IGNORED(entry) || VIDEO_IS_SINGLE_FRAME(entry)) {
            do_add_entry(entry);
        }
        break;
    }
}

static void add_video_if_set_and_buffered(struct camu_sink_entry *entry)
{
    // Single frame entries will be added/removed with ended set.
    if (ENTRY_ENDED(entry)) al_assert(VIDEO_IS_SINGLE_FRAME(entry));
    al_assert(VIDEO_STATE(entry) != BUFFER_INIT);
    al_assert(VIDEO_STATE(entry) != BUFFER_QUEUED);
    al_assert(VIDEO_STATE(entry) != BUFFER_ADDED);
    al_assert(!VIDEO_ENDED(entry));
    switch (VIDEO_STATE(entry)) {
    case BUFFER_CONFIGURED:
        VIDEO_STATE(entry) = BUFFER_SET_OR_BUFFERED;
        break;
    case BUFFER_SET_OR_BUFFERED:
        VIDEO_STATE(entry) = BUFFER_ADDED;
        if (VIDEO_IS_SINGLE_FRAME(entry)) {
            add_entry_video_buffer(entry);
            bool skip_audio = AUDIO_ENDED_OR_EMPTY(entry);
            if (skip_audio) {
                after_add_entry(entry, skip_audio, true);
            }
        } else if (AUDIO_ADDED_OR_IGNORED(entry)) {
            do_add_entry(entry);
        }
        break;
    }
}

static void add_or_queue_entry(struct camu_sink_entry *entry)
{
    log_trace("add_or_queue_entry("ENTRY_FMT"), audio_state: %hhu, video_state: %hhu.",
        ENTRY_ARG(entry), AUDIO_STATE(entry), VIDEO_STATE(entry));
    al_assert(AUDIO_STATE(entry) != BUFFER_QUEUED);
    al_assert(VIDEO_STATE(entry) != BUFFER_QUEUED);
    // Either buffer could be DETACHED.
    if (AUDIO_STATE(entry) == BUFFER_INIT) {
        AUDIO_STATE(entry) = BUFFER_QUEUED;
    } else if (!AUDIO_ENDED_OR_EMPTY(entry)) {
        add_audio_if_set_and_buffered(entry);
    }
    if (VIDEO_STATE(entry) == BUFFER_INIT) {
        VIDEO_STATE(entry) = BUFFER_QUEUED;
    } else if (!VIDEO_ENDED_OR_EMPTY(entry)) {
        add_video_if_set_and_buffered(entry);
    }
}

static void ensure_single_frame_removed(struct camu_sink_entry *entry)
{
    if (!VIDEO_EMPTY(entry) && VIDEO_IS_SINGLE_FRAME(entry)) {
        remove_entry_video_buffer(entry);
        struct camu_sink *sink = entry->sink;
        while (entry_video_buffer_held(entry)) { BLOCKING_SLEEP(sink, NNWT_TS_FROM_USEC(2000)); }
    }
}

// This is the only function that sets sink->current.
static void switch_to(struct camu_sink *sink, struct camu_sink_entry *target)
{
    struct camu_sink_entry *current = sink->current;

    log_trace("switch_to("ENTRY_FMT"), current: "ENTRY_FMT".", ENTRY_ARG(target), ENTRY_ARG(current));

    if (current) {
        struct camu_sink_entry *suspended = sink->suspended;
        al_assert(current != target);
        if (suspended) {
            al_assert(suspended == current);
            // It should be impossible for a buffer to be QUEUED while it's entry is suspended.
            // Even for a single frame video buffer because we wouldn't even know if it only
            // has a single frame yet.
            al_assert(AUDIO_STATE(suspended) != BUFFER_QUEUED);
            al_assert(VIDEO_STATE(suspended) != BUFFER_QUEUED);
            ensure_single_frame_removed(suspended);
            al_assert(AUDIO_STATE(suspended) != BUFFER_ADDED);
            al_assert(VIDEO_STATE(suspended) != BUFFER_ADDED);
            sink->suspended = NULL;
            log_warn("Unset suspended entry as a substitute for remove.");
        } else {
            maybe_add_to_previous(sink, current, target);
        }
    }

    bool dangling_target = target == (struct camu_sink_entry *)0xb00b;
    if (dangling_target) log_trace("Ignoring dangling target.");
    bool stop_video = dangling_target;
    if (!dangling_target) {
        target->audio.ignore_paused = false;
        if (!sink->local && !target->paused) {
            camu_audio_buffer_resync(&target->audio.buf);
        }
        if (!maybe_remove_from_previous(sink, target)) {
            add_or_queue_entry(target);
        }
        al_assert(AUDIO_STATE(target) != BUFFER_INIT);
        al_assert(VIDEO_STATE(target) != BUFFER_INIT);
        // If target was just created, it's AUDIO/VIDEO_STATE() will be QUEUED.
        // Meaning, at this point, it's video buffer would be considered empty
        // as well as being too early to tell if it's static or not.
        stop_video = VIDEO_ENDED_OR_EMPTY(target) || VIDEO_IS_SINGLE_FRAME(target);
    }

    if (stop_video) {
        queue_cmd(sink, CMD(STOP, .v.u = CAMU_SINK_VIDEO));
        if (dangling_target || VIDEO_ENDED_OR_EMPTY(target)) {
            refresh_video_output(sink);
        }
    }

    sink->current = dangling_target ? NULL : target;
}

static void pause_and_swap_to(struct camu_sink *sink, struct camu_sink_entry *target, u64 at)
{
    struct camu_sink_entry *current = sink->current;
    log_trace("pause_and_swap_to("ENTRY_FMT", %.2f), current: "ENTRY_FMT".",
        ENTRY_ARG(target), at / 1000000.0, ENTRY_ARG(current));
    al_assert(target != current);
    al_assert(!sink->target);
    sink->target = target;
    bool immediate = !current || ENTRY_ENDED(current);
    if (current) {
        current->audio.ignore_paused = true;
        immediate |= camu_clock_pause(&current->clock, at);
    }
    if (immediate) {
        switch_to(sink, sink->target);
        sink->target = NULL;
    }
}

static bool end_entry_and_advance_queue(struct camu_sink *sink, struct camu_sink_entry *entry)
{
    log_debug("Entry ("ENTRY_FMT") ended.", ENTRY_ARG(entry));
    run_previous_if_contains(sink, entry);
#ifdef CAMU_SINK_ONESHOT
    sink->callback(sink->userdata, CAMU_SINK_MOCK_CLOSE, 0, NULL);
    return false;
#endif
    // This entry's buffers cannot be added again until after a reset.
    entry->ended = true;
    al_assert(ENTRY_EVAL_ENDED(entry));
    queue_cmd(sink, CMD(END, .v.u = entry->reset_token, .opaque = entry));
#ifdef LIANA_LIST_SCUFFED_LOOP
    log_info("Looping.");
    return true;
#endif
    if (sink->target) {
        log_info("Buffers swapped on end() (Gapless if queued).");
        switch_to(sink, sink->target);
        sink->target = NULL;
        return true;
    }
    return false;
}

static void audio_buffer_callback(void *userdata, u8 op)
{
    struct camu_sink_entry *entry = (struct camu_sink_entry *)userdata;
    struct camu_sink *sink = entry->sink;
    switch (op) {
    case CAMU_BUFFER_BUFFERED:
        lia_vcr_set_buffered(entry->audio.track);
        nn_mutex_lock(&sink->lock);
        add_audio_if_set_and_buffered(entry);
        nn_mutex_unlock(&sink->lock);
        break;
    case CAMU_BUFFER_CORK:
        lia_vcr_cork(entry->audio.track);
        break;
    case CAMU_BUFFER_UNCORK:
        lia_vcr_uncork(entry->audio.track);
        break;
    case CAMU_BUFFER_PAUSED: // Comes from audio read() thread.
        nn_mutex_lock(&sink->lock);
        // entry->paused could plausibly be false here if the lock was held
        // by pause_command_callback() to resume. This can be simulated by
        // calling list_toggle_pause() twice in server/list_action_callback()
        // for each sink request. Spaced by an nn_event_loop_sleep(~15500us)
        // (no_video, MINIAUDIO_LOW_LATENCY mode).
        if (!entry->audio.ignore_paused && entry->paused) {
            log_info("Audio buffer paused.");
            queue_cmd(sink, CMD(STOP, .v.u = CAMU_SINK_AUDIO));
        }
        nn_mutex_unlock(&sink->lock);
        break;
    case CAMU_BUFFER_EOF:
    case CAMU_BUFFER_ERRORED: {
        bool error = op == CAMU_BUFFER_ERRORED;
        if (error) {
            log_error("Audio buffer errored.");
        } else {
            log_debug("Audio EOF.");
        }
        nn_mutex_lock(&sink->lock);
        // EOF and ERRORED come from the outputs read() thread. So, having threaded
        // outputs means anything could have happened while waiting on the lock above.
        // For example, if we were locked in CLIENT_REMOVE_BUFFERS, state could have
        // dropped all the way to CONFIGURED before we acquired the lock here.
        // This should also maintain a consistent state in the more common case of switch_to()
        // right before a buffer EOF.
        if (AUDIO_STATE(entry) == BUFFER_ADDED) {
            remove_entry_audio_buffer(entry);
        }
        AUDIO_STATE(entry) = error ? BUFFER_ERRORED : BUFFER_ENDED;
        if (VIDEO_ENDED_OR_EMPTY(entry) || VIDEO_IS_SINGLE_FRAME(entry)) {
            end_entry_and_advance_queue(sink, entry);
        }
        nn_mutex_unlock(&sink->lock);
        break;
    }
    }
}

static void video_buffer_callback(void *userdata, u8 op)
{
    struct camu_sink_entry *entry = (struct camu_sink_entry *)userdata;
    struct camu_sink *sink = entry->sink;
    switch (op) {
    case CAMU_BUFFER_BUFFERED:
        lia_vcr_set_buffered(entry->video.track);
        nn_mutex_lock(&sink->lock);
        add_video_if_set_and_buffered(entry);
        nn_mutex_unlock(&sink->lock);
        break;
    case CAMU_BUFFER_CORK:
        lia_vcr_cork(entry->video.track);
        break;
    case CAMU_BUFFER_UNCORK:
        lia_vcr_uncork(entry->video.track);
        break;
    case CAMU_BUFFER_EOF:
    case CAMU_BUFFER_ERRORED: {
        bool error = op == CAMU_BUFFER_ERRORED;
        if (error) {
            log_error("Video buffer errored.");
        } else {
            log_debug("Video EOF.");
        }
        nn_mutex_lock(&sink->lock);
        if (!AUDIO_EMPTY(entry)) {
            camu_audio_buffer_set_no_video(&entry->audio.buf, true);
        }
        if (VIDEO_IS_SINGLE_FRAME(entry) && !error) {
            nn_mutex_unlock(&sink->lock);
            return;
        }
        // Video state could be ADDED, SET_OR_BUFFERED, or CONFIGURED.
        // See note about threaded outputs in audio_buffer_callback(EOF|ERRORED).
        if (VIDEO_STATE(entry) == BUFFER_ADDED) {
            remove_entry_video_buffer(entry);
        }
        VIDEO_STATE(entry) = error ? BUFFER_ERRORED : BUFFER_ENDED;
        bool swapped = false;
        if (AUDIO_ENDED_OR_EMPTY(entry)) {
            swapped = end_entry_and_advance_queue(sink, entry);
        }
        nn_mutex_unlock(&sink->lock);
        if (!swapped) {
            queue_cmd(sink, CMD(STOP, .v.u = CAMU_SINK_VIDEO));
        }
        break;
    }
    }
}

static void clock_callback(void *userdata, u8 op)
{
    struct camu_sink_entry *entry = (struct camu_sink_entry *)userdata;
    struct camu_sink *sink = entry->sink;
    if (op == CAMU_CLOCK_PAUSED) {
        nn_mutex_lock(&sink->lock);
        log_trace("clock_paused("ENTRY_FMT"), target: "ENTRY_FMT".", ENTRY_ARG(entry), ENTRY_ARG(sink->target));
        if (entry == sink->current) {
            if (sink->target) {
                switch_to(sink, sink->target);
                sink->target = NULL;
            } else if (entry->paused) {
                queue_cmd(sink, CMD(STOP, .v.u = CAMU_SINK_VIDEO));
            }
        }
        nn_mutex_unlock(&sink->lock);
    }
}

static void evaluate_and_set_buffer_params(struct camu_sink *sink, struct camu_sink_entry *entry)
{
    bool ignore_video = VIDEO_EMPTY(entry) || VIDEO_IS_SINGLE_FRAME(entry);
    f64 audio = camu_mixer_get_latency(sink->audio.mixer);
    u32 frames = 0;
    f64 video = 0.0;
    if (!ignore_video) {
        struct camu_renderer *renderer = sink->video.renderer;
        if (renderer) {
            f64 avg_frame_duration = entry->video.buf.avg_frame_duration;
            frames = renderer->get_latency(renderer);
            video = frames * avg_frame_duration;
        }
    }
    camu_audio_buffer_set_latency(&entry->audio.buf, audio);
    camu_audio_buffer_set_no_video(&entry->audio.buf, ignore_video);
    camu_video_buffer_set_latency(&entry->video.buf, video);
    log_info("video_latency: %f (%u frames), audio_latency: %f.", video, frames, audio);
    if (sink->local) {
        camu_audio_buffer_set_ignore_desync(&entry->audio.buf, ignore_video);
        // When the video buffer starts the clock, we have to consider the audio
        // buffer is treating the last period of silence sent during a paused clock
        // as part of the stream. This poses an issue for sync because it requires more
        // than a period-length offset to not skip data at the start. Combined with the
        // fact that the timing of a request for the next period doesn't have be uniform.
        // Could make a diagram of this. // @TODO
        camu_clock_offset(&entry->clock, MAX(audio, video));
    }
}

static void run_queue_by_opaque(struct camu_sink *sink, void *opaque)
{
    camu_queue_lock(sink->queue);
    struct camu_sink_cmd *cmd;
    al_array_foreach_ptr(sink->queue.a, i, cmd) {
        if (cmd->opaque == opaque) {
            handle_sink_cmd(sink, cmd);
            al_array_remove_at_iter(sink->queue.a, i);
        }
    }
    camu_queue_unlock(sink->queue);
}

static void remove_from_queue_by_opaque(struct camu_sink *sink, void *opaque)
{
    camu_queue_lock(sink->queue);
    struct camu_sink_cmd *cmd;
    al_array_foreach_ptr_rev(sink->queue.a, i, cmd) {
        if (cmd->opaque == opaque) {
            al_array_remove_at(sink->queue.a, i);
        }
    }
    camu_queue_unlock(sink->queue);
}

static void client_callback(void *userdata, u8 op, struct camu_codec_stream *stream, void *opaque)
{
    struct camu_sink_entry *entry = (struct camu_sink_entry *)userdata;
    al_assert(ENTRY_IS_VALID(entry));
    struct camu_sink *sink = entry->sink;
    switch (op) {
    case LIANA_CLIENT_CONFIGURE: {
        // No data will be sent until all selected streams are configured.
        switch (stream->type) {
        case CAMU_STREAM_AUDIO:
            entry->audio.track = (struct lia_vcr_track *)opaque;
            if (!camu_audio_buffer_configure(&entry->audio.buf, stream, sink->audio.mixer)) {
                log_error("Audio buffer failed to configure.");
                maybe_disconnect_entry(entry);
                return;
            }
            nn_mutex_lock(&sink->lock);
            al_assert(AUDIO_EMPTY(entry));
            if (AUDIO_STATE(entry) == BUFFER_QUEUED) {
                AUDIO_STATE(entry) = BUFFER_SET_OR_BUFFERED;
            } else if (AUDIO_STATE(entry) == BUFFER_INIT) {
                AUDIO_STATE(entry) = BUFFER_CONFIGURED;
            }
            nn_mutex_unlock(&sink->lock);
            break;
        case CAMU_STREAM_VIDEO:
            entry->video.track = (struct lia_vcr_track *)opaque;
            if (!camu_video_buffer_configure(&entry->video.buf, stream, sink->video.renderer)) {
                log_error("Video buffer failed to configure.");
                maybe_disconnect_entry(entry);
                return;
            }
            nn_mutex_lock(&sink->lock);
            al_assert(VIDEO_EMPTY(entry));
            if (VIDEO_STATE(entry) == BUFFER_QUEUED) {
                VIDEO_STATE(entry) = BUFFER_SET_OR_BUFFERED;
            } else if (VIDEO_STATE(entry) == BUFFER_INIT) {
                VIDEO_STATE(entry) = BUFFER_CONFIGURED;
            }
            nn_mutex_unlock(&sink->lock);
            break;
        case CAMU_STREAM_SUBTITLE:
            if (!camu_video_buffer_configure_subtitles(&entry->video.buf, stream)) {
                log_warn("Video buffer couldn't configure subtitles.");
            }
            break;
        case CAMU_STREAM_ATTACHMENT: {
            struct camu_renderer *renderer = sink->video.renderer;
            if (renderer) renderer->add_font(renderer, stream);
            break;
        }
        }
        break;
    }
    case LIANA_CLIENT_CONFIGURE_COMPLETE: {
        // All present buffers were configured.
        nn_mutex_lock(&sink->lock);
        // This is mainly to assert DETACHED handling.
        al_assert(!VIDEO_ENDED(entry) && !AUDIO_ENDED(entry));
        evaluate_and_set_buffer_params(sink, entry);
        nn_mutex_unlock(&sink->lock);
        break;
    }
    case LIANA_CLIENT_DATA: {
        struct camu_codec_frame *frame = (struct camu_codec_frame *)opaque;
        // Even though !AUDIO/VIDEO_EMPTY() is a value that cannot change at this point, we
        // still have to lock because AUDIO/VIDEO_STATE() is not atomic.
        switch (stream->type) {
        case CAMU_STREAM_AUDIO:
            nn_locked_assert(!AUDIO_EMPTY(entry), &sink->lock);
            camu_audio_buffer_push(&entry->audio.buf, frame);
            break;
        case CAMU_STREAM_VIDEO:
            nn_locked_assert(!VIDEO_EMPTY(entry), &sink->lock);
            camu_video_buffer_push(&entry->video.buf, frame);
            break;
        default:
            camu_codec_frame_discard(frame);
            break;
        }
        break;
    }
    case LIANA_CLIENT_SUBTITLE: {
        switch (stream->type) {
        case CAMU_STREAM_SUBTITLE: {
            camu_video_buffer_push_subtitle(&entry->video.buf, (struct camu_codec_packet *)opaque);
            break;
        }
        }
        break;
    }
    case LIANA_CLIENT_REMOVE_BUFFERS: {
        struct lia_reconnect_info *rec = (struct lia_reconnect_info *)opaque;
        nn_mutex_lock(&sink->lock);
        log_trace("remove_buffers("ENTRY_FMT", %s, %s), entry == current: %s, single_frame: %s.",
            ENTRY_ARG(entry), BOOLSTR(rec->reconnect),
            BOOLSTR(rec->unconfigured), BOOLSTR(entry == sink->current),
            BOOLSTR(!VIDEO_EMPTY(entry) ? VIDEO_IS_SINGLE_FRAME(entry) : false));

        if (entry == sink->current) {
            if (rec->reconnect) {
                // Else, possible failed or aborted reconnect (Empty buffer could be INIT).
                al_assert(AUDIO_STATE(entry) != BUFFER_INIT);
                al_assert(VIDEO_STATE(entry) != BUFFER_INIT);
            }
            if (sink->target) {
                // This is necessary to avoid re-adding an entry with an in-between clock state.
                // See note in clock.c::camu_clock_seek().
                switch_to(sink, sink->target);
                sink->target = NULL;
            } else if (rec->reconnect) {
                // If this entry is still current on CLIENT_RECONNECTED, re-add it's buffers.
                sink->suspended = entry;
            }
        }

        // This entry might be in previous if it was added to previous then,
        //  1. it's being cleaned up after ENTRY_MAX_AGE - 1 entries were added but none buffered.
        //  2. it was seeked.
        run_previous_if_contains(sink, entry);
        // AUDIO/VIDEO_STATE() could be INIT at this point, even if entry = current.

        // We should treat CLIENT_REMOVE_BUFFERS as a function that removes an entry's buffers and
        // resets it's buffered state. For a non-empty entry that means remove_entry_audio/video_buffer()
        // twice and for an empty buffer, knock it down to BUFFER_INIT.
        if (AUDIO_STATE(entry) == BUFFER_QUEUED) AUDIO_STATE(entry) = BUFFER_INIT;
        if (VIDEO_STATE(entry) == BUFFER_QUEUED) VIDEO_STATE(entry) = BUFFER_INIT;

        // Don't consider unconfigured entries past this point.
        if (rec->unconfigured) {
            al_assert(AUDIO_EMPTY(entry) && VIDEO_EMPTY(entry));
            nn_mutex_unlock(&sink->lock);
            return;
        }

        // An empty buffer is guaranteed to not be held. An ended buffer is already
        // removed and will be further handled at the end of this case.
        bool skip_audio = AUDIO_EMPTY(entry);
        if (!skip_audio && !AUDIO_ENDED(entry)) {
            // Remove for re-add in CLIENT_RECONNECTED.
            remove_entry_audio_buffer(entry);
            // Remove again for another add in BUFFER_BUFFERED.
            remove_entry_audio_buffer(entry);
        }
        bool skip_video = VIDEO_EMPTY(entry);
        // Don't remove a single_frame if we are reconnecting, unless it's errored.
        skip_video = skip_video || (rec->reconnect && VIDEO_IS_SINGLE_FRAME(entry) && VIDEO_STATE(entry) != BUFFER_ERRORED);
        if (!skip_video && !VIDEO_ENDED(entry)) {
            remove_entry_video_buffer(entry);
            remove_entry_video_buffer(entry);
        }
        // If MIXER_THREADED_START_STOP is not set, REMOVE_BUFFER is not thread-safe.
        // Meaning it must be run on the event loop. So, resolve any queued REMOVE_BUFFER
        // requests so we can safely block the loop.
        run_queue_by_opaque(sink, entry);

        // Unlock to wait.
        nn_mutex_unlock(&sink->lock);

        while ((!skip_audio && entry_audio_buffer_held(entry)) || (!skip_video && entry_video_buffer_held(entry))) {
            BLOCKING_SLEEP(sink, NNWT_TS_FROM_USEC(2000));
        }
        // At this point we can be sure that the entry's buffers are no longer in use.

        // Re-lock to check an entries ended state. As it could have been set at some point after unlocking to wait.
        nn_mutex_lock(&sink->lock);

        if (rec->reconnect) {
            // Detach errored buffers. If we detach both streams, the entry will be closed.
            if (AUDIO_STATE(entry) == BUFFER_ERRORED) {
                al_array_push(rec->detached, AUDIO_STREAM(entry));
            }
            if (VIDEO_STATE(entry) == BUFFER_ERRORED) {
                al_array_push(rec->detached, VIDEO_STREAM(entry));
            } else if (VIDEO_STATE(entry) == BUFFER_ADDED && VIDEO_IS_SINGLE_FRAME(entry)) {
                // Try to not request a duplicate frame. We can only be sure the frame wasn't
                // dropped by the VCR if it's ADDED.
                rec->mask &= ~(1 << VIDEO_STREAM(entry)->index);
            }
        }

        // Finalize the removal of the buffers by handling "ended" (ENDED or ERRORED) buffers.
        // ENDED: Set to CONFIGURED to emulate the two removes earlier in this case.
        // ERRORED: Set to DETACHED and they will now be considered empty.
        if (AUDIO_STATE(entry) == BUFFER_ENDED) {
            al_assert(!AUDIO_EMPTY(entry));
            AUDIO_STATE(entry) = BUFFER_CONFIGURED;
        } else if (AUDIO_STATE(entry) == BUFFER_ERRORED) {
            AUDIO_STATE(entry) = BUFFER_DETACHED;
        }
        if (VIDEO_STATE(entry) == BUFFER_ENDED) {
            al_assert(!VIDEO_EMPTY(entry));
            VIDEO_STATE(entry) = BUFFER_CONFIGURED;
        } else if (VIDEO_STATE(entry) == BUFFER_ERRORED) {
            VIDEO_STATE(entry) = BUFFER_DETACHED;
        }
        // Unset ended, consistent with the logic in list.
        entry->ended = false;

        nn_mutex_unlock(&sink->lock);

        break;
    }
    case LIANA_CLIENT_RECOVER_TO: {
        struct lia_timing *time = (struct lia_timing *)opaque;
        time->pos = camu_clock_get_last_pts(&entry->clock) * (u64)1000000;
        time->at = 0;
        break;
    }
    case LIANA_CLIENT_RESUME_AT: { // This is called after the client reconnects, before CLIENT_RECONNECTED.
        struct lia_timing *time = (struct lia_timing *)opaque;
        if (sink->local) time->at = 0;
        f64 pos = time->pos / 1000000.0;
        log_trace("resume_at("ENTRY_FMT"), pos: %f, paused_at: %f.", ENTRY_ARG(entry), pos, entry->clock.paused_at);
        nn_mutex_lock(&sink->lock);
        bool ignore_video = VIDEO_EMPTY(entry) || VIDEO_IS_SINGLE_FRAME(entry);
        if (!AUDIO_EMPTY(entry)) {
            camu_audio_buffer_reset(&entry->audio.buf);
            // no_video is set to true in video BUFFER_EOF as a fail-safe. Reset it here.
            camu_audio_buffer_set_no_video(&entry->audio.buf, ignore_video);
        }
        if (!ignore_video) {
            camu_video_buffer_reset(&entry->video.buf, pos);
        }
        camu_clock_seek(&entry->clock, pos, time->at);
        nn_mutex_unlock(&sink->lock);
        break;
    }
    case LIANA_CLIENT_RECONNECTED: {
        struct lia_reconnect_info *rec = (struct lia_reconnect_info *)opaque;
        nn_mutex_lock(&sink->lock);
        log_trace("reconnected("ENTRY_FMT"), suspended: "ENTRY_FMT", audio_state: %hhu, video_state: %hhu.",
            ENTRY_ARG(entry), ENTRY_ARG(sink->suspended), AUDIO_STATE(entry), VIDEO_STATE(entry));
        if (entry == sink->suspended) {
            al_assert(entry == sink->current);
            // Let this be the only other explicit BUFFER_ENDED check, or this will
            // become too complicated.
            al_assert(AUDIO_STATE(entry) != BUFFER_ENDED);
            al_assert(VIDEO_STATE(entry) != BUFFER_ENDED);
            // The value of unconfigured remains consistent from CLIENT_REMOVE_BUFFERS.
            if (rec->unconfigured) {
                al_assert(AUDIO_EMPTY(entry) && VIDEO_EMPTY(entry));
            }
            // An unconfigured entry would have still been queued if it was current.
            if (AUDIO_STATE(entry) == BUFFER_INIT) {
                AUDIO_STATE(entry) = BUFFER_QUEUED;
            } else if (!AUDIO_EMPTY(entry)) {
                add_audio_if_set_and_buffered(entry);
            }
            if (VIDEO_STATE(entry) == BUFFER_INIT) {
                VIDEO_STATE(entry) = BUFFER_QUEUED;
            } else if (!VIDEO_EMPTY(entry) && !VIDEO_IS_SINGLE_FRAME(entry)) {
                add_video_if_set_and_buffered(entry);
            }
            sink->suspended = NULL;
        }
        nn_mutex_unlock(&sink->lock);
        break;
    }
    case LIANA_CLIENT_EOF:
    case LIANA_CLIENT_ERRORED: {
        bool error = op == LIANA_CLIENT_ERRORED;
        switch (stream->type) {
        case CAMU_STREAM_AUDIO: {
            nn_locked_assert(!AUDIO_EMPTY(entry), &sink->lock);
            camu_audio_buffer_flush(&entry->audio.buf, error);
            break;
        }
        case CAMU_STREAM_VIDEO: {
            nn_locked_assert(!VIDEO_EMPTY(entry), &sink->lock);
            // Single frames are immediately flushed inside the buffer.
            if (!VIDEO_IS_SINGLE_FRAME(entry) || error) {
                camu_video_buffer_flush(&entry->video.buf, error);
            }
            break;
        }
        }
        break;
    }
    case LIANA_CLIENT_CLOSED: {
        struct lia_reconnect_info *rec = (struct lia_reconnect_info *)opaque;
        nn_mutex_lock(&sink->lock);
        // We can be assured that CLIENT_REMOVE_BUFFERS has been called on this entry.

        // If a client is closed after a failed reconnect, a single frame
        // video buffer could still be added
        if (rec->reconnect) ensure_single_frame_removed(entry);
        al_assert(AUDIO_STATE(entry) != BUFFER_ADDED);
        al_assert(VIDEO_STATE(entry) != BUFFER_ADDED);

        bool removed = al_array_remove(sink->entries, entry);
        remove_from_queue_by_opaque(sink, entry);

        if (entry == sink->target) {
            sink->target = (struct camu_sink_entry *)0xb00b;
            log_warn("Attempting to handle a disconnected target.");
        } else if (entry == sink->current) {
            if (sink->suspended) {
                al_assert(sink->suspended == sink->current);
                sink->suspended = NULL;
            }
            if (sink->target) {
                switch_to(sink, sink->target);
                sink->target = NULL;
            } else {
                sink->current = NULL;
                if (removed) { // Don't stop video on exit.
                    queue_cmd(sink, CMD(STOP, .v.u = CAMU_SINK_VIDEO));
                }
            }
            // If current was never fully added we need to call this here.
            maybe_run_previous(sink);
        }

        nn_mutex_unlock(&sink->lock);

        lia_client_free(&entry->client);
        camu_audio_buffer_free(&entry->audio.buf);
        camu_video_buffer_free(&entry->video.buf);
        log_warn("Entry ("ENTRY_FMT") closed by %s.", ENTRY_ARG(entry), removed ? "force" : "cleanup");
        al_free(entry);

        break;
    }
    }
}

static struct camu_sink_entry *create_entry(struct camu_sink *sink, u64 id)
{
    struct camu_sink_entry *entry = al_alloc_object(struct camu_sink_entry);
    entry->sink = sink;
    entry->id = id;
    entry->disconnected = false;
    entry->ended = false;

    camu_clock_init(&entry->clock, clock_callback, entry);

    entry->client.callback = client_callback;
    entry->client.userdata = entry;
    entry->client.renderer = sink->video.renderer;
    entry->client.prefs = sink->prefs;

    AUDIO_STATE(entry) = BUFFER_INIT;
    entry->audio.ignore_paused = false;
    camu_audio_buffer_init(&entry->audio.buf, &entry->clock);
    entry->audio.buf.callback = audio_buffer_callback;
    entry->audio.buf.userdata = entry;

    VIDEO_STATE(entry) = BUFFER_INIT;
    camu_video_buffer_init(&entry->video.buf, &entry->clock);
    entry->video.buf.callback = video_buffer_callback;
    entry->video.buf.userdata = entry;
    // Two entries in order could share the same pointer if the first
    // entry was just freed. This has to be accounted for in the renderer
    // cache or it won't update on the first frame of the new entry.
    // This is most likely to happen when the sink reconnects to a server.
    union { f64 f; u64 u; } fv = { .u = id };
    entry->video.buf.seek_pts = fv.f;

    al_array_push(sink->entries, entry);

    return entry;
}

static struct camu_sink_entry *get_entry_from_id(struct camu_sink *sink, u64 id)
{
    struct camu_sink_entry *entry;
    al_array_foreach(sink->entries, i, entry) {
        if (entry->id == id) return entry;
    }
    return NULL;
}

static bool set_command_callback(void *userdata, struct nn_rpc_connection *conn,
    struct nn_packet *packet, struct nn_packet *rpacket)
{
    struct camu_sink *sink = (struct camu_sink *)userdata;
    (void)rpacket;

    u8 op = nn_packet_read_u8(packet);
    if (op == LIANA_SINK_UNSET) {
        nn_mutex_lock(&sink->lock);
        struct camu_sink_entry *current = sink->current;
        nn_mutex_unlock(&sink->lock);
        if (current) {
            maybe_disconnect_entry(current);
        }
        nn_mutex_lock(&sink->lock);
        goto out;
    }

    // Liana node info.
    str addr;
    nn_packet_read_str(packet, &addr);
    u16 port = nn_packet_read_u16(packet);
    u32 node_id = nn_packet_read_u32(packet);

    // List entry info.
    u64 id = LOCAL_ENTRY_ID(sink, nn_packet_read_u32(packet));
    s32 sequence = nn_packet_read_s32(packet);
    u64 at = nn_packet_read_u64(packet);
    u64 pos = nn_packet_read_u64(packet);
    u8 pause = nn_packet_read_u8(packet);
    u32 reset_token = nn_packet_read_u32(packet);

    struct camu_sink_entry *entry = get_entry_from_id(sink, id);
    bool create = !entry;
    if (create) entry = create_entry(sink, id);
    entry->sequence = sequence;
    entry->lru = sink->lru;
    sink->lru = al_u16_add_wrap(sink->lru, 1, SINK_LRU_MAX);
    entry->reset_token = reset_token;
    if (create) {
        entry->paused = pause == LIANA_PAUSE_NONE || pause == LIANA_PAUSE_PAUSE;
        camu_clock_set(&entry->clock, pos / 1000000.0);
        lia_client_connect(&entry->client, sink->loop, sink->type, &addr, port, node_id, pos);
    }

    // Don't lock before client_connect() or we could deadlock in CLIENT_CLOSED on a failed socket_connect().
    nn_mutex_lock(&sink->lock);

    struct camu_sink_entry *current = sink->current;

    if (op == LIANA_SINK_BUFFER) {
        log_trace("buffered("ENTRY_FMT"), created: %s.", ENTRY_ARG(entry), BOOLSTR(create));
        goto out;
    } else if (op == LIANA_SINK_BUFFER_AND_QUEUE) {
        log_trace("queued("ENTRY_FMT"), %s, created: %s.", ENTRY_ARG(entry), lia_pause_op_name(pause), BOOLSTR(create));
        goto out;
    }

    if (sink->local) at = 0;

    // For target to be set that must mean current is set, armed to pause, and not ended.
    struct camu_sink_entry *prev_target = sink->target;
    log_trace("set("ENTRY_FMT"), %s, created: %s, target: "ENTRY_FMT".", ENTRY_ARG(entry),
        lia_pause_op_name(pause), BOOLSTR(create), ENTRY_ARG(prev_target));
    switch (pause) {
    case LIANA_PAUSE_NONE:
        if (prev_target) {
            sink->target = NULL;
        } else {
            al_assert(entry != current);
        }
        if (entry != current) {
            switch_to(sink, entry);
        }
        break;
    case LIANA_PAUSE_RESUME:
        if (prev_target) {
            sink->target = NULL;
            if (entry == current) {
                // Switched back to current before pause_and_swap_to() completed.
                camu_audio_buffer_resync(&entry->audio.buf);
            }
        } else {
            al_assert(entry != current);
        }
        if (entry != current) {
            switch_to(sink, entry);
        }
        camu_clock_resume(&entry->clock, at);
        break;
    case LIANA_PAUSE_PAUSE:
        if (prev_target) {
            al_assert(current);
            al_assert(!ENTRY_ENDED(current));
            al_assert(prev_target != entry);
            if (entry == current) {
                // pause_and_swap_to() negated.
                sink->target = NULL;
            } else {
                sink->target = entry;
            }
            if (prev_target != (struct camu_sink_entry *)0xb00b) {
                camu_clock_pause(&prev_target->clock, at);
            }
        } else {
            al_assert(entry != current);
            pause_and_swap_to(sink, entry, at);
        }
        break;
    case LIANA_PAUSE_BOTH:
        if (prev_target) {
            al_assert(current);
            al_assert(!ENTRY_ENDED(current));
            al_assert(prev_target != entry);
            if (entry == current) {
                sink->target = NULL;
                camu_audio_buffer_resync(&entry->audio.buf);
            } else {
                sink->target = entry;
            }
            if (prev_target != (struct camu_sink_entry *)0xb00b) {
                camu_clock_pause(&prev_target->clock, at);
            }
        } else {
            al_assert(entry != current);
            pause_and_swap_to(sink, entry, at);
        }
        camu_clock_resume(&entry->clock, at);
        break;
    }

out:
    nn_mutex_unlock(&sink->lock);
    if (op != LIANA_SINK_BUFFER) {
        maybe_cleanup_old_entries(sink);
    }

    nn_packet_stream_return_packet(conn->stream, packet);

    return false;
}

static bool pause_command_callback(void *userdata, struct nn_rpc_connection *conn,
    struct nn_packet *packet, struct nn_packet *rpacket)
{
    struct camu_sink *sink = (struct camu_sink *)userdata;
    (void)rpacket;

    u64 id = LOCAL_ENTRY_ID(sink, nn_packet_read_u32(packet));
    s32 sequence = nn_packet_read_s32(packet);
    u64 at = nn_packet_read_u64(packet);
    u8 pause = nn_packet_read_u8(packet);

    struct camu_sink_entry *entry = get_entry_from_id(sink, id);
    if (!entry) goto out;
    nn_mutex_lock(&sink->lock);
    // As long as the list discards skips with a non-current sequence, this should hold true.
    al_assert(entry->sequence == sequence);
    log_trace("pause("ENTRY_FMT"), %s, audio_state: %hhu, video_state: %hhu.",
        ENTRY_ARG(entry), lia_pause_op_name(pause), AUDIO_STATE(entry), VIDEO_STATE(entry));
    if (sink->local) at = 0;
    switch (pause) {
    case LIANA_PAUSE_PAUSE: {
        entry->paused = true;
        camu_clock_pause(&entry->clock, at);
        log_info("Clock paused.");
        // Audio will be stopped in a BUFFER_PAUSED callback.
        // Video will be stopped in a CLOCK_PAUSED callback.
        break;
    }
    case LIANA_PAUSE_RESUME: {
        entry->paused = false;
        camu_clock_resume(&entry->clock, at);
        log_info("Clock resumed.");
        if (!VIDEO_ENDED_OR_EMPTY(entry) && !VIDEO_IS_SINGLE_FRAME(entry)) {
            queue_cmd(sink, CMD(START, .v.u = CAMU_SINK_VIDEO));
        }
        if (!AUDIO_ENDED_OR_EMPTY(entry)) {
            if (!sink->local) {
                camu_audio_buffer_resync(&entry->audio.buf);
            }
            queue_cmd(sink, CMD(START, .v.u = CAMU_SINK_AUDIO));
        }
        break;
    }
    }
    nn_mutex_unlock(&sink->lock);

out:
    nn_packet_stream_return_packet(conn->stream, packet);

    return false;
}

static bool seek_command_callback(void *userdata, struct nn_rpc_connection *conn,
    struct nn_packet *packet, struct nn_packet *rpacket)
{
    struct camu_sink *sink = (struct camu_sink *)userdata;
    (void)rpacket;

    u64 id = LOCAL_ENTRY_ID(sink, nn_packet_read_u32(packet));
    s32 sequence = nn_packet_read_s32(packet);
    u64 at = nn_packet_read_u64(packet);
    u64 pos = nn_packet_read_u64(packet);
    u32 reset_token = nn_packet_read_u32(packet);

    struct camu_sink_entry *entry = get_entry_from_id(sink, id);
    if (!entry) goto out;
    nn_mutex_lock(&sink->lock);
    entry->sequence = sequence;
    log_trace("seek("ENTRY_FMT", %.2f), reset_token: %u.", ENTRY_ARG(entry), pos / 1000000.0, reset_token);
    entry->reset_token = reset_token;
    nn_mutex_unlock(&sink->lock);
    // The rest of the seek is handled in CLIENT_REMOVE_BUFFERS/RESUME_AT/RECONNECTED.
    lia_client_seek(&entry->client, pos, at);

out:
    nn_packet_stream_return_packet(conn->stream, packet);

    return false;
}

static struct nn_rpc_command commands[] = {
    { .op = CAMU_SINK_SET, .callback = set_command_callback, .userdata = NULL },
    { .op = CAMU_SINK_PAUSE, .callback = pause_command_callback, .userdata = NULL },
    { .op = CAMU_SINK_SEEK, .callback = seek_command_callback, .userdata = NULL }
};

static void identify_callback(void *userdata, struct nn_rpc_connection *conn, struct nn_packet *packet)
{
    struct camu_sink *sink = (struct camu_sink *)userdata;
#ifndef CAMU_DIRECT_MODE
    if (sink->type == NNWT_SOCKET_UNIX) {
        log_info("Sink connected to %.*s.", al_str_x(&sink->addr));
    } else {
        log_info("Sink connected to %.*s:%hu.", al_str_x(&sink->addr), sink->port);
    }
#else
    (void)sink;
    log_info("Sink directly bridged to server.");
#endif
    nn_packet_stream_return_packet(conn->stream, packet);
}

static void identify_on_connection(struct camu_sink *sink)
{
    struct nn_packet *packet = nn_rpc_get_packet(&sink->client, CAMU_SERVER_IDENTIFY);
    nn_packet_write_u8(packet, CAMU_SINK);
    nn_packet_write_str(packet, &sink->name);
    nn_rpc_connection_command(sink->conn, packet, identify_callback, sink);
}

static void connection_callback(void *userdata, struct nn_rpc_connection *conn)
{
    struct camu_sink *sink = (struct camu_sink *)userdata;
    nn_timer_stop(&sink->reconnect_timer);
    if (sink->conn) al_assert(sink->conn == conn);
    sink->conn = conn;
    sink->connected = true;
    sink->connection_number = al_u16_inc_wrap(sink->connection_number);
    if (sink->connection_number == 0) sink->connection_number = 1;
    identify_on_connection(sink);
}

static void reconnect_timer_callback(void *userdata, struct nn_timer *timer)
{
    struct camu_sink *sink = (struct camu_sink *)userdata;
    (void)timer;
    if (sink->conn) {
        // If the client was connecting, reconnect() will force a disconnect before reconnecting.
        log_warn("Forcing reconnect due to timeout.");
    }
    sink->conn = nn_rpc_reconnect(&sink->client, &sink->addr, sink->port);
}

static void connection_closed_callback(void *userdata, struct nn_rpc_connection *conn)
{
    struct camu_sink *sink = (struct camu_sink *)userdata;
    bool reconnect = sink->conn != NULL;
    bool disconnected = sink->conn && sink->connection_number > 0;
    if (sink->conn) {
        al_assert(sink->conn == conn);
        sink->conn = NULL;
        sink->connected = false;
    } else {
        al_assert(!reconnect || !sink->connected);
    }
    if (reconnect) {
        if (disconnected) {
            log_warn("Connection to server closed, attempting reconnect...");
            nn_rpc_reconnect(&sink->client, &sink->addr, sink->port);
        } else {
            log_warn("Failed to connect to server, trying again...");
            nn_timer_again(&sink->reconnect_timer);
        }
    }
}

bool camu_sink_init(struct camu_sink *sink, struct nn_event_loop *loop,
    struct camu_mixer *mixer, struct camu_renderer *renderer)
{
    sink->loop = loop;
    nn_rpc_init(&sink->client, sink->loop, connection_callback, connection_closed_callback, sink);
    sink->conn = NULL;
    sink->connection_number = 0;
    nn_mutex_init(&sink->lock);
    nn_timer_init(&sink->reconnect_timer, sink->loop, reconnect_timer_callback, sink);
    // This is also effectively a timeout for attempted reconnects.
    nn_timer_set_repeat(&sink->reconnect_timer, NNWT_TS_FROM_USEC(1000000));
    nn_signal_init(&sink->queue_signal, sink->loop, queue_signal_callback, sink);
    nn_signal_start(&sink->queue_signal);
    camu_queue_init(sink->queue);
    sink->current = NULL;
    sink->target = NULL;
    sink->suspended = NULL;
    al_array_init(sink->previous);
    al_array_init(sink->entries);
    // Start high to exercise the wrapping path.
    sink->lru = SINK_LRU_MAX - al_random_int(0, ENTRY_MAX_AGE);
    mixer->callback = mixer_callback;
    mixer->userdata = sink;
    sink->audio.state = SINK_PAUSED;
    sink->audio.mixer = mixer;
    sink->video.state = SINK_PAUSED;
    sink->video.renderer = renderer;
    return true;
}

bool camu_sink_connect(struct camu_sink *sink, str *name, u8 type, str *addr, u16 port)
{
    al_str_clone(&sink->name, name);
    sink->type = type;
    al_str_clone(&sink->addr, addr);
    sink->port = port;
    sink->conn = nn_rpc_prepare_client(&sink->client);
    al_assert(sink->callback);
    for (u32 i = 0; i < ARRAY_SIZE(commands); i++) {
        commands[i].userdata = sink;
        nn_rpc_add_command(&sink->client, &commands[i]);
    }
#ifdef CAMU_DIRECT_MODE
    // Note that direct_connect() runs connection_callback() directly.
    nn_multiplex_direct_connect(sink->conn->stream, CAMU_MULTIPLEX_RPC);
#else
    nn_rpc_connect(&sink->client, CAMU_MULTIPLEX_RPC, sink->type, &sink->addr, sink->port);
#endif
    return true;
}

struct camu_sink_entry *camu_sink_get_current(struct camu_sink *sink)
{
    nn_mutex_lock(&sink->lock);
    return sink->current;
}

void camu_sink_return_current(struct camu_sink *sink)
{
    nn_mutex_unlock(&sink->lock);
}

void camu_sink_skip(struct camu_sink *sink, s32 n)
{
    nn_mutex_lock(&sink->lock);
    struct camu_sink_entry *current = get_entry_for_command(sink);
    nn_mutex_unlock(&sink->lock);
    queue_cmd(sink, CMD(SKIP, .v.i = n, .opaque = current));
}

void camu_sink_toggle_pause(struct camu_sink *sink)
{
    nn_mutex_lock(&sink->lock);
    struct camu_sink_entry *current = get_entry_for_command(sink);
    nn_mutex_unlock(&sink->lock);
    if (!current) return;
    bool armed_for_pause = false;
    camu_clock_external_pause(&current->clock);
    f64 pts = camu_clock_get_pts(&current->clock, 0.0, false, &armed_for_pause);
    queue_cmd(sink, CMD(TOGGLE_PAUSE, .v.f = pts, .opaque = current));
}

void camu_sink_seek(struct camu_sink *sink, void *value, u8 mode)
{
    nn_mutex_lock(&sink->lock);
    struct camu_sink_entry *current = get_entry_for_command(sink);
    u64 duration = 0;
    f64 pts = 0.0;
    if (current) {
        duration = current->client.duration;
        pts = camu_clock_get_last_pts(&current->clock);
    }
    nn_mutex_unlock(&sink->lock);
    if (!current || duration == 0) return;
    struct camu_sink_cmd cmd = {
        .op = SEEK,
        .opaque = current
    };
    switch (mode) {
    case CAMU_SEEK_POS: {
        u64 pos = *(u64 *)value;
        cmd.v.u = pos;
        break;
    }
    case CAMU_SEEK_RELATIVE: {
        f64 offset = *(f64 *)value;
        pts = MAX(pts + offset, 0.0);
        cmd.v.u = (u64)(pts * 1000000);
        break;
    }
    case CAMU_SEEK_PERCENT: {
        // There's probably a way to lose less precision here.
        f64 percent = *(f64 *)value;
        cmd.v.u = (u64)(duration * percent);
        break;
    }
    }
    queue_cmd(sink, cmd);
}

void camu_sink_reseek(struct camu_sink *sink)
{
    queue_cmd(sink, CMD(RESEEK));
}

void camu_sink_shuffle(struct camu_sink *sink)
{
    queue_cmd(sink, CMD(SHUFFLE));
}

void camu_sink_stop(struct camu_sink *sink)
{
    queue_cmds(sink, 3,
        CMD(STOP,          .v.u = CAMU_SINK_AUDIO),
        CMD(CLEAR_BUFFERS, .v.u = CAMU_SINK_AUDIO),
        CMD(CLOSE)
    );
}

void camu_sink_close(struct camu_sink *sink)
{
    nn_timer_stop(&sink->reconnect_timer);
    if (sink->conn) {
        struct nn_rpc_connection *conn = sink->conn;
        sink->conn = NULL; // Signal to connection_closed_callback() we're done.
        nn_rpc_conn_disconnect(conn);
    }
    struct camu_sink_entry *entry;
    al_array_foreach_rev(sink->entries, i, entry) {
        al_array_remove_at(sink->entries, i);
        maybe_disconnect_entry(entry);
    }
}

void camu_sink_free(struct camu_sink *sink)
{
    al_assert(!sink->entries.count);
    al_array_free(sink->entries);
    nn_rpc_free(&sink->client);
    camu_queue_free(sink->queue);
    nn_mutex_destroy(&sink->lock);
    al_str_free(&sink->name);
    al_str_free(&sink->addr);
}