-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmoq.c
7058 lines (6890 loc) · 310 KB
/
moq.c
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
/*! \file moq.c
* \author Lorenzo Miniero <[email protected]>
* \copyright MIT License
* \brief Media Over QUIC (MoQ) stack
* \details Implementation of the Media Over QUIC (MoQ) stack as part
* of the library itself. At the time of writing, this implements (most
* of) versions -03 and -04 of the protocol.
*
* \note This is the internal implementation of MoQ in the library. You're
* still free to only use imquic as the underlying QUIC/WebTransport library,
* and take care of the MoQ implementation on your own instead: in order
* to do that, use the generic imquic client/server creation utilities,
* rather than the MoQ specific ones.
*
* \ingroup MoQ Core
*/
#include <arpa/inet.h>
#include "internal/moq.h"
#include "internal/connection.h"
#include "imquic/debug.h"
/* Logging */
#define IMQUIC_MOQ_LOG_VERB IMQUIC_LOG_HUGE
#define IMQUIC_MOQ_LOG_HUGE IMQUIC_LOG_VERB
//~ #define IMQUIC_MOQ_LOG_VERB IMQUIC_LOG_INFO
//~ #define IMQUIC_MOQ_LOG_HUGE IMQUIC_LOG_INFO
/* Collection of sessions */
static GHashTable *moq_sessions = NULL;
static imquic_mutex moq_mutex = IMQUIC_MUTEX_INITIALIZER;
/* Initialization */
static void imquic_moq_context_destroy(imquic_moq_context *moq);
static void imquic_moq_context_free(const imquic_refcount *moq_ref);
void imquic_moq_init(void) {
moq_sessions = g_hash_table_new_full(NULL, NULL, NULL, (GDestroyNotify)imquic_moq_context_destroy);
}
void imquic_moq_deinit(void) {
imquic_mutex_lock(&moq_mutex);
if(moq_sessions != NULL)
g_hash_table_unref(moq_sessions);
moq_sessions = NULL;
imquic_mutex_unlock(&moq_mutex);
}
/* Callbacks */
void imquic_moq_new_connection(imquic_connection *conn, void *user_data) {
/* Got new connection */
imquic_refcount_increase(&conn->ref);
IMQUIC_LOG(IMQUIC_LOG_VERB, "[%s][MoQ] New connection %p\n", imquic_get_connection_name(conn), conn);
imquic_moq_context *moq = g_malloc0(sizeof(imquic_moq_context));
moq->conn = conn;
moq->is_server = conn->is_server;
moq->version = IMQUIC_MOQ_VERSION_ANY;
moq->streams = g_hash_table_new_full(g_int64_hash, g_int64_equal,
(GDestroyNotify)g_free, (GDestroyNotify)imquic_moq_stream_destroy);
moq->subscriptions = g_hash_table_new_full(g_int64_hash, g_int64_equal,
(GDestroyNotify)g_free, NULL);
moq->subscriptions_by_id = g_hash_table_new_full(g_int64_hash, g_int64_equal,
(GDestroyNotify)g_free, (GDestroyNotify)imquic_moq_subscription_destroy);
moq->buffer = g_malloc0(sizeof(imquic_moq_buffer));
imquic_mutex_init(&moq->mutex);
imquic_refcount_init(&moq->ref, imquic_moq_context_free);
imquic_mutex_lock(&moq_mutex);
g_hash_table_insert(moq_sessions, conn, moq);
imquic_mutex_unlock(&moq_mutex);
/* If we're a client, let's create a control stream */
if(!moq->is_server) {
uint64_t stream_id = 0;
imquic_connection_new_stream_id(conn, TRUE, &stream_id);
moq->control_stream_id = stream_id;
moq->has_control_stream = TRUE;
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->moq)
imquic_moq_qlog_stream_type_set(moq->conn->qlog, TRUE, moq->control_stream_id, "control");
#endif
}
/* Notify the application: for clients, we'll need it to set role and version */
if(conn->socket && conn->socket->callbacks.moq.new_connection)
conn->socket->callbacks.moq.new_connection(conn, user_data);
/* After the function returns, check if we can do something */
if(!moq->is_server) {
if(!moq->role_set) {
IMQUIC_LOG(IMQUIC_LOG_ERR, "[%s][MoQ] No role set by the MoQ client, unable to send CLIENT_SETUP...\n",
imquic_get_connection_name(conn));
return;
}
moq->version_set = TRUE;
/* Generate a CLIENT_SETUP */
imquic_moq_setup_parameters parameters = { 0 };
if(moq->version < IMQUIC_MOQ_VERSION_08) {
parameters.role_set = TRUE;
parameters.role = moq->type;
}
if(((moq->version >= IMQUIC_MOQ_VERSION_06 && moq->version <= IMQUIC_MOQ_VERSION_MAX) || moq->version == IMQUIC_MOQ_VERSION_ANY) &&
moq->local_max_subscribe_id > 0) {
parameters.max_subscribe_id_set = TRUE;
parameters.max_subscribe_id = moq->local_max_subscribe_id;
}
GList *versions = NULL;
if(moq->version == IMQUIC_MOQ_VERSION_ANY) {
/* Offer all newer supported versions */
versions = g_list_append(versions, GUINT_TO_POINTER(IMQUIC_MOQ_VERSION_10));
versions = g_list_append(versions, GUINT_TO_POINTER(IMQUIC_MOQ_VERSION_09));
versions = g_list_append(versions, GUINT_TO_POINTER(IMQUIC_MOQ_VERSION_08));
versions = g_list_append(versions, GUINT_TO_POINTER(IMQUIC_MOQ_VERSION_07));
versions = g_list_append(versions, GUINT_TO_POINTER(IMQUIC_MOQ_VERSION_06));
} else if(moq->version == IMQUIC_MOQ_VERSION_ANY_LEGACY) {
/* Offer all supported versions before -06 */
versions = g_list_append(versions, GUINT_TO_POINTER(IMQUIC_MOQ_VERSION_05));
versions = g_list_append(versions, GUINT_TO_POINTER(IMQUIC_MOQ_VERSION_04));
versions = g_list_append(versions, GUINT_TO_POINTER(IMQUIC_MOQ_VERSION_03));
} else {
/* Offer a specific version */
versions = g_list_append(versions, GUINT_TO_POINTER(moq->version));
}
uint8_t buffer[200];
size_t blen = sizeof(buffer), poffset = 5, start = 0;
size_t cs_len = imquic_moq_add_client_setup(moq, &buffer[poffset], blen-poffset, versions, ¶meters);
cs_len = imquic_moq_add_control_message(moq, IMQUIC_MOQ_CLIENT_SETUP, buffer, blen, poffset, cs_len, &start);
g_list_free(versions);
imquic_connection_send_on_stream(moq->conn, moq->control_stream_id,
&buffer[start], moq->control_stream_offset, cs_len, FALSE);
moq->control_stream_offset += cs_len;
imquic_connection_flush_stream(moq->conn, moq->control_stream_id);
}
}
void imquic_moq_stream_incoming(imquic_connection *conn, uint64_t stream_id,
uint8_t *bytes, uint64_t offset, uint64_t length, gboolean complete) {
/* Got incoming data via STREAM */
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ] [STREAM-%"SCNu64"] Got data: %"SCNu64"--%"SCNu64" (%s)\n",
imquic_get_connection_name(conn),
stream_id, offset, offset+length, (complete ? "complete" : "not complete"));
imquic_mutex_lock(&moq_mutex);
imquic_moq_context *moq = g_hash_table_lookup(moq_sessions, conn);
imquic_mutex_unlock(&moq_mutex);
if(moq == NULL)
return;
if(offset == 0 && !moq->has_control_stream) {
uint64_t actual_id = 0;
gboolean client_initiated = FALSE, bidirectional = FALSE;
imquic_parse_stream_id(stream_id, &actual_id, &client_initiated, &bidirectional);
if(!bidirectional) {
/* First stream we get is not a bidirectional control stream */
IMQUIC_LOG(IMQUIC_LOG_ERR, "[%s][MoQ] Not a bidirectional MoQ control stream\n",
imquic_get_connection_name(conn));
return;
}
moq->has_control_stream = TRUE;
moq->control_stream_id = stream_id;
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->moq)
imquic_moq_qlog_stream_type_set(moq->conn->qlog, FALSE, moq->control_stream_id, "control");
#endif
}
imquic_moq_parse_message(moq, stream_id, bytes, length, complete, FALSE);
}
void imquic_moq_datagram_incoming(imquic_connection *conn, uint8_t *bytes, uint64_t length) {
/* Got incoming data via DATAGRAM */
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ][%p] [DATAGRAM] Got data: %"SCNu64"\n",
imquic_get_connection_name(conn), conn, length);
imquic_mutex_lock(&moq_mutex);
imquic_moq_context *moq = g_hash_table_lookup(moq_sessions, conn);
imquic_mutex_unlock(&moq_mutex);
if(moq == NULL)
return;
imquic_moq_parse_message(moq, 0, bytes, length, FALSE, TRUE);
}
void imquic_moq_connection_gone(imquic_connection *conn) {
/* Connection was closed */
imquic_mutex_lock(&moq_mutex);
gboolean removed = g_hash_table_remove(moq_sessions, conn);
imquic_mutex_unlock(&moq_mutex);
if(conn->socket && conn->socket->callbacks.moq.connection_gone)
conn->socket->callbacks.moq.connection_gone(conn);
if(removed) {
IMQUIC_LOG(IMQUIC_LOG_VERB, "[%s][MoQ] Connection gone\n",
imquic_get_connection_name(conn));
imquic_refcount_decrease(&conn->ref);
}
}
/* Cleanup */
static void imquic_moq_context_destroy(imquic_moq_context *moq) {
if(moq && g_atomic_int_compare_and_exchange(&moq->destroyed, 0, 1))
imquic_refcount_decrease(&moq->ref);
}
static void imquic_moq_context_free(const imquic_refcount *moq_ref) {
imquic_moq_context *moq = imquic_refcount_containerof(moq_ref, imquic_moq_context, ref);
g_list_free(moq->supported_versions);
if(moq->streams)
g_hash_table_unref(moq->streams);
if(moq->subscriptions)
g_hash_table_unref(moq->subscriptions);
if(moq->subscriptions_by_id)
g_hash_table_unref(moq->subscriptions_by_id);
imquic_moq_buffer_destroy(moq->buffer);
g_free(moq);
}
static void imquic_moq_object_extension_free(imquic_moq_object_extension *extension) {
if(extension != NULL) {
if(extension->value.data.buffer != NULL)
g_free(extension->value.data.buffer);
g_free(extension);
}
}
/* MoQ stringifiers */
const char *imquic_moq_error_code_str(imquic_moq_error_code code) {
switch(code) {
case IMQUIC_MOQ_NO_ERROR:
return "No Error";
case IMQUIC_MOQ_INTERNAL_ERROR:
return "Internal Error";
case IMQUIC_MOQ_UNAUTHORIZED:
return "Unauthorized";
case IMQUIC_MOQ_PROTOCOL_VIOLATION:
return "Protocol Violation";
case IMQUIC_MOQ_DUPLICATE_TRACK_ALIAS:
return "Duplicate Track Alias";
case IMQUIC_MOQ_PARAMETER_LENGTH_MISMATCH:
return "Parameter Length Mismatch";
case IMQUIC_MOQ_TOO_MANY_SUBSCRIBES:
return "Too Many Subscribes";
case IMQUIC_MOQ_GOAWAY_TIMEOUT:
return "GOAWAY Timeout";
case IMQUIC_MOQ_CONTROL_MESSAGE_TIMEOUT:
return "Control Message Timeout";
case IMQUIC_MOQ_DATA_STREAM_TIMEOUT:
return "Data Stream Timeout";
default: break;
}
return NULL;
}
const char *imquic_moq_announce_error_code_str(imquic_moq_announce_error_code code) {
switch(code) {
case IMQUIC_MOQ_ANNCERR_INTERNAL_ERROR:
return "Internal Error";
case IMQUIC_MOQ_ANNCERR_UNAUTHORIZED:
return "Unauthorized";
case IMQUIC_MOQ_ANNCERR_TIMEOUT:
return "Timeout";
case IMQUIC_MOQ_ANNCERR_NOT_SUPPORTED:
return "Not Supported";
case IMQUIC_MOQ_ANNCERR_UNINTERESTED:
return "Uninterested";
default: break;
}
return NULL;
}
const char *imquic_moq_sub_error_code_str(imquic_moq_sub_error_code code) {
switch(code) {
case IMQUIC_MOQ_SUBERR_INTERNAL_ERROR:
return "Internal Error";
case IMQUIC_MOQ_SUBERR_UNAUTHORIZED:
return "Unauthorized";
case IMQUIC_MOQ_SUBERR_TIMEOUT:
return "Timeout";
case IMQUIC_MOQ_SUBERR_NOT_SUPPORTED:
return "Not Supported";
case IMQUIC_MOQ_SUBERR_TRACK_DOES_NOT_EXIST:
return "Track Does Not Exist";
case IMQUIC_MOQ_SUBERR_INVALID_RANGE:
return "Invalid Range";
case IMQUIC_MOQ_SUBERR_RETRY_TRACK_ALIAS:
return "Retry Track Alias";
default: break;
}
return NULL;
}
const char *imquic_moq_subannc_error_code_str(imquic_moq_subannc_error_code code) {
switch(code) {
case IMQUIC_MOQ_SUBANNCERR_INTERNAL_ERROR:
return "Internal Error";
case IMQUIC_MOQ_SUBANNCERR_UNAUTHORIZED:
return "Unauthorized";
case IMQUIC_MOQ_SUBANNCERR_TIMEOUT:
return "Timeout";
case IMQUIC_MOQ_SUBANNCERR_NOT_SUPPORTED:
return "Not Supported";
case IMQUIC_MOQ_SUBANNCERR_NAMESPACE_PREFIX_UNKNOWN:
return "Namespace Prefix Unknown";
default: break;
}
return NULL;
}
const char *imquic_moq_fetch_error_code_str(imquic_moq_fetch_error_code code) {
switch(code) {
case IMQUIC_MOQ_FETCHERR_INTERNAL_ERROR:
return "Internal Error";
case IMQUIC_MOQ_FETCHERR_UNAUTHORIZED:
return "Unauthorized";
case IMQUIC_MOQ_FETCHERR_TIMEOUT:
return "Timeout";
case IMQUIC_MOQ_FETCHERR_NOT_SUPPORTED:
return "Not Supported";
case IMQUIC_MOQ_FETCHERR_TRACK_DOES_NOT_EXIST:
return "Track Does Not Exist";
case IMQUIC_MOQ_FETCHERR_INVALID_RANGE:
return "Invalid Range";
default: break;
}
return NULL;
}
const char *imquic_moq_sub_done_code_str(imquic_moq_sub_done_code code) {
switch(code) {
case IMQUIC_MOQ_SUBDONE_INTERNAL_ERROR:
return "Internal Error";
case IMQUIC_MOQ_SUBDONE_UNAUTHORIZED:
return "Unauthorized";
case IMQUIC_MOQ_SUBDONE_TRACK_ENDED:
return "Track Ended";
case IMQUIC_MOQ_SUBDONE_SUBSCRIPTION_ENDED:
return "Subscription Ended";
case IMQUIC_MOQ_SUBDONE_GOING_AWAY:
return "Going Away";
case IMQUIC_MOQ_SUBDONE_EXPIRED:
return "Expired";
case IMQUIC_MOQ_SUBDONE_TOO_FAR_BEHIND:
return "Too Far Behind";
default: break;
}
return NULL;
}
const char *imquic_moq_message_type_str(imquic_moq_message_type type) {
switch(type) {
case IMQUIC_MOQ_SUBSCRIBE:
return "SUBSCRIBE";
case IMQUIC_MOQ_SUBSCRIBE_OK:
return "SUBSCRIBE_OK";
case IMQUIC_MOQ_SUBSCRIBE_ERROR:
return "SUBSCRIBE_ERROR";
case IMQUIC_MOQ_ANNOUNCE:
return "ANNOUNCE";
case IMQUIC_MOQ_ANNOUNCE_OK:
return "ANNOUNCE_OK";
case IMQUIC_MOQ_ANNOUNCE_ERROR:
return "ANNOUNCE_ERROR";
case IMQUIC_MOQ_UNANNOUNCE:
return "UNANNOUNCE";
case IMQUIC_MOQ_UNSUBSCRIBE:
return "UNSUBSCRIBE";
case IMQUIC_MOQ_SUBSCRIBE_DONE:
return "SUBSCRIBE_DONE";
case IMQUIC_MOQ_ANNOUNCE_CANCEL:
return "ANNOUNCE_CANCEL";
case IMQUIC_MOQ_TRACK_STATUS_REQUEST:
return "TRACK_STATUS_REQUEST";
case IMQUIC_MOQ_TRACK_STATUS:
return "TRACK_STATUS";
case IMQUIC_MOQ_GOAWAY:
return "GOAWAY";
case IMQUIC_MOQ_SUBSCRIBE_ANNOUNCES:
return "SUBSCRIBE_ANNOUNCES";
case IMQUIC_MOQ_SUBSCRIBE_ANNOUNCES_OK:
return "SUBSCRIBE_ANNOUNCES_OK";
case IMQUIC_MOQ_SUBSCRIBE_ANNOUNCES_ERROR:
return "SUBSCRIBE_ANNOUNCES_ERROR";
case IMQUIC_MOQ_UNSUBSCRIBE_ANNOUNCES:
return "SUBSCRIBE_UNNAMESPACE";
case IMQUIC_MOQ_MAX_SUBSCRIBE_ID:
return "MAX_SUBSCRIBE_ID";
case IMQUIC_MOQ_SUBSCRIBES_BLOCKED:
return "SUBSCRIBES_BLOCKED";
case IMQUIC_MOQ_FETCH:
return "FETCH";
case IMQUIC_MOQ_FETCH_CANCEL:
return "FETCH_CANCEL";
case IMQUIC_MOQ_FETCH_OK:
return "FETCH_OK";
case IMQUIC_MOQ_FETCH_ERROR:
return "FETCH_ERROR";
case IMQUIC_MOQ_CLIENT_SETUP:
return "CLIENT_SETUP";
case IMQUIC_MOQ_SERVER_SETUP:
return "SERVER_SETUP";
default: break;
}
return NULL;
}
const char *imquic_moq_data_message_type_str(imquic_moq_data_message_type type, imquic_moq_version version) {
if(version == IMQUIC_MOQ_VERSION_06 && type == IMQUIC_MOQ_STREAM_HEADER_TRACK_V06)
return "STREAM_HEADER_TRACK";
switch(type) {
case IMQUIC_MOQ_OBJECT_STREAM:
return "OBJECT_STREAM";
case IMQUIC_MOQ_OBJECT_DATAGRAM:
return "OBJECT_DATAGRAM";
case IMQUIC_MOQ_OBJECT_DATAGRAM_STATUS:
return "OBJECT_DATAGRAM_STATUS";
case IMQUIC_MOQ_STREAM_HEADER_TRACK:
return "STREAM_HEADER_TRACK";
case IMQUIC_MOQ_STREAM_HEADER_GROUP:
return "STREAM_HEADER_GROUP";
case IMQUIC_MOQ_SUBGROUP_HEADER:
return "SUBGROUP_HEADER";
case IMQUIC_MOQ_FETCH_HEADER:
return "FETCH_HEADER";
default: break;
}
return NULL;
}
imquic_moq_delivery imquic_moq_data_message_type_to_delivery(imquic_moq_data_message_type type, imquic_moq_version version) {
if(version == IMQUIC_MOQ_VERSION_06 && type == IMQUIC_MOQ_STREAM_HEADER_TRACK_V06)
return IMQUIC_MOQ_USE_TRACK;
switch(type) {
case IMQUIC_MOQ_OBJECT_STREAM:
return IMQUIC_MOQ_USE_STREAM;
case IMQUIC_MOQ_OBJECT_DATAGRAM:
return IMQUIC_MOQ_USE_DATAGRAM;
case IMQUIC_MOQ_STREAM_HEADER_TRACK:
return IMQUIC_MOQ_USE_TRACK;
case IMQUIC_MOQ_STREAM_HEADER_GROUP:
return IMQUIC_MOQ_USE_GROUP;
case IMQUIC_MOQ_SUBGROUP_HEADER:
return IMQUIC_MOQ_USE_SUBGROUP;
case IMQUIC_MOQ_FETCH_HEADER:
return IMQUIC_MOQ_USE_FETCH;
default: break;
}
return -1;
}
const char *imquic_moq_setup_parameter_type_str(imquic_moq_setup_parameter_type type) {
switch(type) {
case IMQUIC_MOQ_PARAM_ROLE:
return "ROLE";
case IMQUIC_MOQ_PARAM_PATH:
return "PATH";
case IMQUIC_MOQ_PARAM_MAX_SUBSCRIBE_ID:
return "MAX_SUBSCRIBE_ID";
default: break;
}
return NULL;
}
const char *imquic_moq_subscribe_parameter_type_str(imquic_moq_subscribe_parameter_type type) {
switch(type) {
case IMQUIC_MOQ_PARAM_AUTHORIZATION_INFO:
return "AUTHORIZATION_INFO";
case IMQUIC_MOQ_PARAM_DELIVERY_TIMEOUT:
return "DELIVERY_TIMEOUT";
case IMQUIC_MOQ_PARAM_MAX_CACHE_DURATION:
return "MAX_CACHE_DURATION";
default: break;
}
return NULL;
}
const char *imquic_moq_role_type_str(imquic_moq_role_type type) {
switch(type) {
case IMQUIC_MOQ_ROLE_ENDPOINT:
return "Endpoint";
case IMQUIC_MOQ_ROLE_PUBLISHER:
return "Publisher";
case IMQUIC_MOQ_ROLE_SUBSCRIBER:
return "Subscriber";
case IMQUIC_MOQ_ROLE_PUBSUB:
return "PubSub";
default: break;
}
return NULL;
}
const char *imquic_moq_location_mode_str(imquic_moq_location_mode mode) {
switch(mode) {
case IMQUIC_MOQ_LOCATION_NONE:
return "None";
case IMQUIC_MOQ_LOCATION_ABSOLUTE:
return "Absolute";
case IMQUIC_MOQ_LOCATION_RELATIVEPREVIOUS:
return "RelativePrevious";
case IMQUIC_MOQ_LOCATION_RELATIVENEXT:
return "RelativeNext";
default: break;
}
return NULL;
}
const char *imquic_moq_filter_type_str(imquic_moq_filter_type type) {
switch(type) {
case IMQUIC_MOQ_FILTER_LATEST_GROUP:
return "LatestGroup";
case IMQUIC_MOQ_FILTER_LATEST_OBJECT:
return "LatestObject";
case IMQUIC_MOQ_FILTER_ABSOLUTE_START:
return "AbsoluteStart";
case IMQUIC_MOQ_FILTER_ABSOLUTE_RANGE:
return "AbsoluteRange";
default: break;
}
return NULL;
}
const char *imquic_moq_group_order_str(imquic_moq_group_order type) {
switch(type) {
case IMQUIC_MOQ_ORDERING_ORIGINAL:
return "Original";
case IMQUIC_MOQ_ORDERING_ASCENDING:
return "Ascending";
case IMQUIC_MOQ_ORDERING_DESCENDING:
return "Descending";
default: break;
}
return NULL;
}
const char *imquic_moq_fetch_type_str(imquic_moq_fetch_type type) {
switch(type) {
case IMQUIC_MOQ_FETCH_STANDALONE:
return "Standalone Fetch";
case IMQUIC_MOQ_FETCH_JOINING:
return "Joining Fetch";
default: break;
}
return NULL;
}
/* MoQ parameters */
size_t imquic_moq_setup_parameters_serialize(imquic_moq_context *moq,
imquic_moq_setup_parameters *parameters,
uint8_t *bytes, size_t blen, uint8_t *params_num) {
*params_num = 0;
if(bytes == NULL || blen == 0)
return 0;
size_t offset = 0;
if(parameters == NULL) {
/* No parameters */
offset += imquic_write_varint(0, &bytes[offset], blen-offset);
} else {
if(parameters->role_set)
*params_num = *params_num + 1;
if(parameters->path_set)
*params_num = *params_num + 1;
if(parameters->max_subscribe_id_set)
*params_num = *params_num + 1;
offset += imquic_write_varint(*params_num, &bytes[offset], blen-offset);
if(*params_num > 0) {
if(parameters->role_set) {
offset += imquic_moq_parameter_add_int(moq, &bytes[offset], blen-offset,
IMQUIC_MOQ_PARAM_ROLE, parameters->role);
}
if(parameters->path_set) {
offset += imquic_moq_parameter_add_data(moq, &bytes[offset], blen-offset,
IMQUIC_MOQ_PARAM_PATH, (uint8_t *)parameters->path, strlen(parameters->path));
}
if(parameters->max_subscribe_id_set) {
offset += imquic_moq_parameter_add_int(moq, &bytes[offset], blen-offset,
IMQUIC_MOQ_PARAM_MAX_SUBSCRIBE_ID, parameters->max_subscribe_id);
}
}
}
return offset;
}
size_t imquic_moq_subscribe_parameters_serialize(imquic_moq_context *moq,
imquic_moq_subscribe_parameters *parameters,
uint8_t *bytes, size_t blen, uint8_t *params_num) {
if(bytes == NULL || blen == 0)
return 0;
size_t offset = 0;
if(parameters == NULL) {
/* No parameters */
offset += imquic_write_varint(0, &bytes[offset], blen-offset);
} else {
if(parameters->auth_info_set)
*params_num = *params_num + 1;
if(parameters->delivery_timeout_set)
*params_num = *params_num + 1;
if(parameters->max_cache_duration_set)
*params_num = *params_num + 1;
offset += imquic_write_varint(*params_num, &bytes[offset], blen-offset);
if(*params_num > 0) {
if(parameters->auth_info_set) {
offset += imquic_moq_parameter_add_data(moq, &bytes[offset], blen-offset,
IMQUIC_MOQ_PARAM_AUTHORIZATION_INFO, (uint8_t *)parameters->auth_info, strlen(parameters->auth_info));
}
if(parameters->delivery_timeout_set) {
offset += imquic_moq_parameter_add_int(moq, &bytes[offset], blen-offset,
IMQUIC_MOQ_PARAM_DELIVERY_TIMEOUT, parameters->delivery_timeout);
}
if(parameters->max_cache_duration_set) {
offset += imquic_moq_parameter_add_int(moq, &bytes[offset], blen-offset,
IMQUIC_MOQ_PARAM_MAX_CACHE_DURATION, parameters->max_cache_duration);
}
}
}
return offset;
}
/* MoQ Buffer */
gboolean imquic_moq_buffer_resize(imquic_moq_buffer *buffer, uint64_t new_size) {
if(buffer == NULL || buffer->size >= new_size)
return FALSE;
if(buffer->bytes == NULL)
buffer->bytes = g_malloc(new_size);
else
buffer->bytes = g_realloc(buffer->bytes, new_size);
buffer->size = new_size;
return TRUE;
}
void imquic_moq_buffer_append(imquic_moq_buffer *buffer, uint8_t *bytes, uint64_t length) {
if(buffer == NULL || bytes == NULL || length == 0)
return;
if(buffer->size < buffer->length + length) {
if(!imquic_moq_buffer_resize(buffer, buffer->length + length)) {
IMQUIC_LOG(IMQUIC_LOG_WARN, "Couldn't resize MoQ buffer\n");
return;
}
}
memcpy(buffer->bytes + buffer->length, bytes, length);
buffer->length += length;
}
void imquic_moq_buffer_shift(imquic_moq_buffer *buffer, uint64_t length) {
if(buffer == NULL || buffer->bytes == NULL || length == 0)
return;
if(length >= buffer->length) {
buffer->length = 0;
} else {
memmove(buffer->bytes, buffer->bytes + length, buffer->length - length);
buffer->length -= length;
}
}
void imquic_moq_buffer_destroy(imquic_moq_buffer *buffer) {
if(buffer != NULL) {
g_free(buffer->bytes);
g_free(buffer);
}
}
imquic_moq_subscription *imquic_moq_subscription_create(uint64_t subscribe_id, uint64_t track_alias) {
imquic_moq_subscription *moq_sub = g_malloc(sizeof(imquic_moq_subscription));
moq_sub->subscribe_id = subscribe_id;
moq_sub->track_alias = track_alias;
moq_sub->stream = NULL;
moq_sub->streams_by_group = g_hash_table_new_full(g_int64_hash, g_int64_equal,
(GDestroyNotify)g_free, (GDestroyNotify)imquic_moq_stream_destroy);
moq_sub->streams_by_subgroup = g_hash_table_new_full(g_int64_hash, g_int64_equal,
(GDestroyNotify)g_free, (GDestroyNotify)imquic_moq_stream_destroy);
return moq_sub;
}
void imquic_moq_subscription_destroy(imquic_moq_subscription *moq_sub) {
if(moq_sub != NULL) {
if(moq_sub->stream != NULL)
imquic_moq_stream_destroy(moq_sub->stream);
g_hash_table_unref(moq_sub->streams_by_group);
g_hash_table_unref(moq_sub->streams_by_subgroup);
g_free(moq_sub);
}
}
void imquic_moq_stream_destroy(imquic_moq_stream *moq_stream) {
if(moq_stream != NULL) {
imquic_moq_buffer_destroy(moq_stream->buffer);
g_free(moq_stream);
}
}
/* FIXME Message parsing */
#define IMQUIC_MOQ_CHECK_ERR(err, res, reason) \
if(err) { \
IMQUIC_LOG(IMQUIC_LOG_ERR, "%s\n", reason); \
return res; \
}
int imquic_moq_parse_message(imquic_moq_context *moq, uint64_t stream_id, uint8_t *bytes, size_t blen, gboolean complete, gboolean datagram) {
size_t offset = 0, parsed = 0, parsed_prev = 0;
uint8_t tlen = 0, error = 0;
/* If this is a datagram, it can only be OBJECT_DATAGRAM or OBJECT_DATAGRAM_STATUS */
if(datagram) {
imquic_moq_data_message_type dtype = imquic_read_varint(&bytes[offset], blen-offset, &tlen);
offset += tlen;
if(dtype == IMQUIC_MOQ_OBJECT_DATAGRAM) {
/* Parse this OBJECT_DATAGRAM message */
parsed = imquic_moq_parse_object_datagram(moq, &bytes[offset], blen-offset, &error);
IMQUIC_MOQ_CHECK_ERR(error, -1, "Broken MoQ Message");
} else if(moq->version >= IMQUIC_MOQ_VERSION_08 && dtype == IMQUIC_MOQ_OBJECT_DATAGRAM_STATUS) {
/* Parse this OBJECT_DATAGRAM_STATUS message */
parsed = imquic_moq_parse_object_datagram_status(moq, &bytes[offset], blen-offset, &error);
IMQUIC_MOQ_CHECK_ERR(error, -1, "Broken MoQ Message");
} else {
/* TODO Handle failure */
IMQUIC_LOG(IMQUIC_LOG_ERR, "[%s][MoQ] MoQ message '%s' (%02x) is not allowed on datagrams\n",
imquic_get_connection_name(moq->conn), imquic_moq_data_message_type_str(dtype, moq->version), dtype);
return -1;
}
/* Done */
return 0;
}
/* Check if this is a media stream */
imquic_moq_stream *moq_stream = g_hash_table_lookup(moq->streams, &stream_id);
if(stream_id == moq->control_stream_id) {
imquic_moq_buffer_append(moq->buffer, bytes, blen);
bytes = moq->buffer->bytes;
blen = moq->buffer->length;
}
/* Iterate on all frames */
while(moq_stream == NULL && blen-offset > 0) {
/* If we're here, we're either on the control stream, or on a media stream waiting to know what it will be like */
imquic_moq_message_type type = imquic_read_varint(&bytes[offset], blen-offset, &tlen);
IMQUIC_LOG(IMQUIC_MOQ_LOG_VERB, "[%s][MoQ][%zu] >> %s (%02x, %u)\n",
imquic_get_connection_name(moq->conn), offset, imquic_moq_message_type_str(type), type, tlen);
if(stream_id != moq->control_stream_id) {
/* Not the control stream, make sure it's a supported message */
imquic_moq_data_message_type dtype = (imquic_moq_data_message_type)type;
if(dtype == IMQUIC_MOQ_STREAM_HEADER_TRACK ||
(moq->version == IMQUIC_MOQ_VERSION_06 && dtype == IMQUIC_MOQ_STREAM_HEADER_TRACK_V06) ||
dtype == IMQUIC_MOQ_STREAM_HEADER_GROUP || dtype == IMQUIC_MOQ_SUBGROUP_HEADER ||
dtype == IMQUIC_MOQ_OBJECT_STREAM || dtype == IMQUIC_MOQ_FETCH_HEADER) {
/* Create a new MoQ stream and track it */
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ] -- Stream %"SCNu64" will be used for %s\n",
imquic_get_connection_name(moq->conn), stream_id, imquic_moq_data_message_type_str(dtype, moq->version));
moq_stream = g_malloc0(sizeof(imquic_moq_stream));
moq_stream->stream_id = stream_id;
moq_stream->type = dtype;
g_hash_table_insert(moq->streams, imquic_dup_uint64(stream_id), moq_stream);
} else {
/* TODO Handle failure */
IMQUIC_LOG(IMQUIC_LOG_ERR, "[%s][MoQ] MoQ message '%s' (%02x) is not allowed on media streams\n",
imquic_get_connection_name(moq->conn), imquic_moq_message_type_str(type), type);
return -1;
}
}
parsed_prev = parsed;
offset += tlen;
if(stream_id == moq->control_stream_id) {
/* Control message */
size_t plen = blen-offset;
if((moq->version >= IMQUIC_MOQ_VERSION_06 && moq->version <= IMQUIC_MOQ_VERSION_MAX) || moq->version == IMQUIC_MOQ_VERSION_ANY) {
/* Versions later than 06 require a payload length before the payload */
plen = imquic_read_varint(&bytes[offset], blen-offset, &tlen);
IMQUIC_MOQ_CHECK_ERR(tlen == 0, -1, "Broken MoQ Message");
offset += tlen;
if(plen > blen-offset) {
/* Try again later */
IMQUIC_LOG(IMQUIC_MOQ_LOG_VERB, "[%s][MoQ] Not enough bytes available to parse this message (%zu > %zu), waiting for more data\n",
imquic_get_connection_name(moq->conn), plen, blen-offset);
return 0;
}
}
if(type == IMQUIC_MOQ_CLIENT_SETUP) {
/* Parse this CLIENT_SETUP message */
parsed = imquic_moq_parse_client_setup(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_SERVER_SETUP) {
/* Parse this SERVER_SETUP message */
parsed = imquic_moq_parse_server_setup(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_MAX_SUBSCRIBE_ID) {
/* Parse this MAX_SUBSCRIBE_ID message */
parsed = imquic_moq_parse_max_subscribe_id(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_SUBSCRIBES_BLOCKED) {
/* Parse this SUBSCRIBES_BLOCKED message */
parsed = imquic_moq_parse_subscribes_blocked(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_ANNOUNCE) {
/* Parse this ANNOUNCE message */
parsed = imquic_moq_parse_announce(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_ANNOUNCE_OK) {
/* Parse this ANNOUNCE_OK message */
parsed = imquic_moq_parse_announce_ok(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_ANNOUNCE_ERROR) {
/* Parse this ANNOUNCE_ERROR message */
parsed = imquic_moq_parse_announce_error(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_UNANNOUNCE) {
/* Parse this UNANNOUNCE message */
parsed = imquic_moq_parse_unannounce(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_ANNOUNCE_CANCEL) {
/* Parse this ANNOUNCE_CANCEL message */
parsed = imquic_moq_parse_announce_cancel(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_SUBSCRIBE) {
/* Parse this SUBSCRIBE message */
parsed = imquic_moq_parse_subscribe(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_SUBSCRIBE_UPDATE) {
/* Parse this SUBSCRIBE_UPDATE message */
parsed = imquic_moq_parse_subscribe_update(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_SUBSCRIBE_OK) {
/* Parse this SUBSCRIBE_OK message */
parsed = imquic_moq_parse_subscribe_ok(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_SUBSCRIBE_ERROR) {
/* Parse this SUBSCRIBE_ERROR message */
parsed = imquic_moq_parse_subscribe_error(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_UNSUBSCRIBE) {
/* Parse this UNSUBSCRIBE message */
parsed = imquic_moq_parse_unsubscribe(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_SUBSCRIBE_DONE) {
/* Parse this SUBSCRIBE_DONE message */
parsed = imquic_moq_parse_subscribe_done(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_SUBSCRIBE_ANNOUNCES) {
/* Parse this SUBSCRIBE_ANNOUNCES message */
parsed = imquic_moq_parse_subscribe_announces(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_SUBSCRIBE_ANNOUNCES_OK) {
/* Parse this SUBSCRIBE_ANNOUNCES_OK message */
parsed = imquic_moq_parse_subscribe_announces_ok(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_SUBSCRIBE_ANNOUNCES_ERROR) {
/* Parse this SUBSCRIBE_ANNOUNCES_ERROR message */
parsed = imquic_moq_parse_subscribe_announces_error(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_UNSUBSCRIBE_ANNOUNCES) {
/* Parse this UNSUBSCRIBE_ANNOUNCES message */
parsed = imquic_moq_parse_unsubscribe_announces(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_FETCH) {
/* Parse this FETCH message */
parsed = imquic_moq_parse_fetch(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_FETCH_CANCEL) {
/* Parse this FETCH_CANCEL message */
parsed = imquic_moq_parse_fetch_cancel(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_FETCH_OK) {
/* Parse this FETCH_OK message */
parsed = imquic_moq_parse_fetch_ok(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_FETCH_ERROR) {
/* Parse this FETCH_ERROR message */
parsed = imquic_moq_parse_fetch_error(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_TRACK_STATUS_REQUEST) {
/* Parse this TRACK_STATUS_REQUEST message */
parsed = imquic_moq_parse_track_status_request(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_TRACK_STATUS) {
/* Parse this TRACK_STATUS message */
parsed = imquic_moq_parse_track_status_request(moq, &bytes[offset], plen, &error);
} else if(type == IMQUIC_MOQ_GOAWAY) {
/* Parse this GOAWAY message */
parsed = imquic_moq_parse_goaway(moq, &bytes[offset], plen, &error);
} else {
IMQUIC_LOG(IMQUIC_LOG_WARN, "[%s][MoQ] Unsupported message '%02x'\n",
imquic_get_connection_name(moq->conn), type);
imquic_moq_buffer_shift(moq->buffer, plen);
return -1;
}
if(error) {
IMQUIC_LOG(IMQUIC_LOG_ERR, "[%s][MoQ] Broken MoQ message %s\n",
imquic_get_connection_name(moq->conn), imquic_moq_message_type_str(type));
imquic_moq_buffer_shift(moq->buffer, plen);
return -1;
}
/* Move to the next message */
offset += parsed;
imquic_moq_buffer_shift(moq->buffer, offset);
bytes = moq->buffer->bytes;
blen = moq->buffer->length;
offset = 0;
} else {
/* Data message */
if((imquic_moq_data_message_type)type == IMQUIC_MOQ_OBJECT_STREAM) {
/* Parse this OBJECT_STREAM message */
parsed = imquic_moq_parse_object_stream(moq, moq_stream, &bytes[offset], blen-offset, &error);
IMQUIC_MOQ_CHECK_ERR(error, -1, "Broken MoQ Message");
offset += parsed;
} else if((imquic_moq_data_message_type)type == IMQUIC_MOQ_STREAM_HEADER_TRACK ||
(moq->version == IMQUIC_MOQ_VERSION_06 && (imquic_moq_data_message_type)type == IMQUIC_MOQ_STREAM_HEADER_TRACK_V06)) {
/* Parse this STREAM_HEADER_TRACK message */
parsed = imquic_moq_parse_stream_header_track(moq, moq_stream, &bytes[offset], blen-offset, &error);
IMQUIC_MOQ_CHECK_ERR(error, -1, "Broken MoQ Message");
offset += parsed;
} else if((imquic_moq_data_message_type)type == IMQUIC_MOQ_STREAM_HEADER_GROUP) {
/* Parse this STREAM_HEADER_GROUP message */
parsed = imquic_moq_parse_stream_header_group(moq, moq_stream, &bytes[offset], blen-offset, &error);
IMQUIC_MOQ_CHECK_ERR(error, -1, "Broken MoQ Message");
offset += parsed;
} else if((imquic_moq_data_message_type)type == IMQUIC_MOQ_SUBGROUP_HEADER) {
/* Parse this SUBGROUP_HEADER message */
parsed = imquic_moq_parse_subgroup_header(moq, moq_stream, &bytes[offset], blen-offset, &error);
IMQUIC_MOQ_CHECK_ERR(error, -1, "Broken MoQ Message");
offset += parsed;
} else if((imquic_moq_data_message_type)type == IMQUIC_MOQ_FETCH_HEADER) {
/* Parse this FETCH_HEADER message */
parsed = imquic_moq_parse_fetch_header(moq, moq_stream, &bytes[offset], blen-offset, &error);
IMQUIC_MOQ_CHECK_ERR(error, -1, "Broken MoQ Message");
offset += parsed;
} else {
IMQUIC_LOG(IMQUIC_LOG_WARN, "[%s][MoQ] Unsupported data message '%02x'\n",
imquic_get_connection_name(moq->conn), type);
return -1;
}
}
if(parsed == parsed_prev) {
IMQUIC_LOG(IMQUIC_LOG_WARN, "[%s][MoQ] Broken MoQ message (didn't advance from offset %zu/%zu)\n",
imquic_get_connection_name(moq->conn), parsed, blen);
return -1;
}
}
/* Check if we have a media stream to process */
if(moq_stream != NULL && blen-offset > 0) {
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ] MoQ media stream %"SCNu64" (%zu bytes)\n",
imquic_get_connection_name(moq->conn), stream_id, blen - offset);
/* Copy the incoming data to the buffer, as we'll use that for parsing */
imquic_moq_buffer_append(moq_stream->buffer, bytes + offset, blen - offset);
while(moq_stream->buffer && moq_stream->buffer->length > 0) {
/* Parse the object we're receiving on that stream */
if(moq_stream->type == IMQUIC_MOQ_STREAM_HEADER_TRACK ||
(moq->version == IMQUIC_MOQ_VERSION_06 && moq_stream->type == IMQUIC_MOQ_STREAM_HEADER_TRACK_V06)) {
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ][%zu] >> %s object\n",
imquic_get_connection_name(moq->conn), offset, imquic_moq_data_message_type_str(moq_stream->type, moq->version));
if(imquic_moq_parse_stream_header_track_object(moq, moq_stream, complete) < 0) {
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ] -- Not enough data, trying again later\n",
imquic_get_connection_name(moq->conn));
break;
}
} else if(moq_stream->type == IMQUIC_MOQ_STREAM_HEADER_GROUP) {
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ][%zu] >> %s object\n",
imquic_get_connection_name(moq->conn), offset, imquic_moq_data_message_type_str(moq_stream->type, moq->version));
if(imquic_moq_parse_stream_header_group_object(moq, moq_stream, complete) < 0) {
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ] -- Not enough data, trying again later\n",
imquic_get_connection_name(moq->conn));
break;
}
} else if(moq_stream->type == IMQUIC_MOQ_SUBGROUP_HEADER) {
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ][%zu] >> %s object\n",
imquic_get_connection_name(moq->conn), offset, imquic_moq_data_message_type_str(moq_stream->type, moq->version));
if(imquic_moq_parse_subgroup_header_object(moq, moq_stream, complete) < 0) {
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ] -- Not enough data, trying again later\n",
imquic_get_connection_name(moq->conn));
break;
}
} else if(moq_stream->type == IMQUIC_MOQ_FETCH_HEADER) {
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ][%zu] >> %s object\n",
imquic_get_connection_name(moq->conn), offset, imquic_moq_data_message_type_str(moq_stream->type, moq->version));
if(imquic_moq_parse_fetch_header_object(moq, moq_stream, complete) < 0) {
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ] -- Not enough data, trying again later\n",
imquic_get_connection_name(moq->conn));
break;
}
} else if(moq_stream->type == IMQUIC_MOQ_OBJECT_STREAM) {
/* Nothing to do, just keep on adding to the buffer until the stream is complete */
break;
} else {
/* FIXME Shouldn't happen */
IMQUIC_LOG(IMQUIC_LOG_ERR, "[%s][MoQ] Invalid MoQ stream type '%s' (%02x)\n",
imquic_get_connection_name(moq->conn), imquic_moq_data_message_type_str(moq_stream->type, moq->version), moq_stream->type);
return -1;
}
}
}
if(moq_stream != NULL && complete) {
IMQUIC_LOG(IMQUIC_MOQ_LOG_HUGE, "[%s][MoQ] Media stream %"SCNu64" is complete\n",
imquic_get_connection_name(moq->conn), stream_id);
if(moq_stream->type == IMQUIC_MOQ_OBJECT_STREAM && moq_stream->buffer != NULL) {
/* Notify the payload at the application layer */
imquic_moq_object object = {
.subscribe_id = moq_stream->subscribe_id,
.track_alias = moq_stream->track_alias,
.group_id = moq_stream->group_id,
.subgroup_id = 0,
.object_id = moq_stream->object_id,
.object_status = moq_stream->object_status,
.object_send_order = moq_stream->object_send_order,
.priority = moq_stream->priority,
.payload = moq_stream->buffer->bytes,
.payload_len = moq_stream->buffer->length,
.delivery = IMQUIC_MOQ_USE_STREAM,
.end_of_stream = TRUE
};
if(moq->conn->socket && moq->conn->socket->callbacks.moq.incoming_object)
moq->conn->socket->callbacks.moq.incoming_object(moq->conn, &object);
} else if(!moq_stream->closed && (moq_stream->type == IMQUIC_MOQ_STREAM_HEADER_GROUP ||
moq_stream->type == IMQUIC_MOQ_SUBGROUP_HEADER ||
moq_stream->type == IMQUIC_MOQ_FETCH_HEADER ||
moq_stream->type == IMQUIC_MOQ_STREAM_HEADER_TRACK ||
(moq->version == IMQUIC_MOQ_VERSION_06 && moq_stream->type == IMQUIC_MOQ_STREAM_HEADER_TRACK_V06))) {
/* FIXME Notify an empty payload to signal the end of the stream */
imquic_moq_object object = {
.subscribe_id = moq_stream->subscribe_id,
.track_alias = 0,
.group_id = moq_stream->group_id,
.subgroup_id = moq_stream->subgroup_id,
.object_id = IMQUIC_MOQ_NORMAL_OBJECT,
.object_send_order = 0,
.priority = 0,
.payload = NULL,
.payload_len = 0,
.delivery = imquic_moq_data_message_type_to_delivery(moq_stream->type, moq->version),
.end_of_stream = TRUE
};
if(moq->conn->socket && moq->conn->socket->callbacks.moq.incoming_object)
moq->conn->socket->callbacks.moq.incoming_object(moq->conn, &object);
}
g_hash_table_remove(moq->streams, &stream_id);
}
/* Done */
return 0;
}
size_t imquic_moq_parse_client_setup(imquic_moq_context *moq, uint8_t *bytes, size_t blen, uint8_t *error) {
if(error)
*error = 1;