-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathserver.c
More file actions
1275 lines (1065 loc) · 36.5 KB
/
Copy pathserver.c
File metadata and controls
1275 lines (1065 loc) · 36.5 KB
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
/*
* A simple RTSP server implementation using libevent [1].
*
* To obtain `audio.g711a` and `video.h264`:
*
* $ ffmpeg -i http://docs.evostream.com/sample_content/assets/bun33s.mp4 \
* -acodec pcm_mulaw -f mulaw -ar 8000 -ac 1 audio.g711a \
* -vcodec h264 -x264opts aud=1 video.h264
*
* [1] https://libevent.org/
*/
#include <smolrtsp.h>
#include <smolrtsp-libevent.h>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/event.h>
#include <event2/listener.h>
#include <event2/util.h>
// G.711 A-Law, 8k sample rate, mono channel.
#include "media/audio.g711a.h"
// H.264 video with AUDs, 25 FPS.
#include "media/video.h264.h"
#ifdef ENABLE_JPEGXS
/* A single 640x480 YUV422 8-bit JPEG XS codestream at 3 bpp, encoded by
* SVT-JPEG-XS <https://github.com/OpenVisualCloud/SVT-JPEG-XS> from
* `ffmpeg -f lavfi -i smptebars=size=640x480 -frames:v 1 -pix_fmt
* yuv422p input.yuv` then `SvtJpegxsEncApp -i input.yuv -w 640 -h 480
* --colour-format yuv422 --input-depth 8 --bpp 3 -n 1 -b
* media/video.jxs`. */
#include "media/video.jxs.h"
#endif
#ifdef ENABLE_AV1
/* A short AV1 raw-OBU stream (320x240, 10 fps, 5 frames) produced by
* `ffmpeg -f lavfi -i smptebars=size=320x240:rate=10 -frames:v 5 \
* -c:v libaom-av1 -cpu-used 8 -b:v 200k -f obu media/video.av1`.
* Each TU starts with a Temporal Delimiter OBU per the AV1 spec. */
#include "media/video.av1.h"
#endif
#ifdef ENABLE_VVC
/* A short H.266 / VVC Annex-B byte stream (320x240, 10 fps, 5 frames)
* produced by Fraunhofer vvenc <https://github.com/fraunhoferhhi/vvenc>:
* ffmpeg -f lavfi -i smptebars=size=320x240:rate=10 -frames:v 5 \
* -pix_fmt yuv420p -f rawvideo input.yuv
* vvencapp -i input.yuv -s 320x240 -c yuv420 --fps 10 -f 5 \
* --preset faster -aud 1 -o media/video.h266
* Each AU starts with an AUD_NUT, which the example uses to detect AU
* boundaries for RTP timestamp / marker bit handling. */
#include "media/video.h266.h"
#endif
// Comment one of these lines if you do not need audio or video.
#define ENABLE_AUDIO
#define ENABLE_VIDEO
/* ENABLE_JPEGXS / ENABLE_AV1 are set from the build system
* (examples/CMakeLists.txt has opt-in options). When set, the server
* advertises additional media streams alongside H.264 -- "/jpegxs" for
* RFC 9134 JPEG XS and "/av1" for the AOMedia AV1 RTP format. */
#define SERVER_PORT SMOLRTSP_DEFAULT_PORT
#define AUDIO_PCMU_PAYLOAD_TYPE 0
#define AUDIO_SAMPLE_RATE 8000
#define AUDIO_SAMPLES_PER_PACKET 160
#define AUDIO_PACKETIZATION_TIME_US \
(1e6 / (AUDIO_SAMPLE_RATE / AUDIO_SAMPLES_PER_PACKET))
#define VIDEO_PAYLOAD_TYPE 96 // dynamic PT
#define VIDEO_SAMPLE_RATE 90000
#define VIDEO_FPS 25
#define JPEGXS_PAYLOAD_TYPE 97 // dynamic PT
#define JPEGXS_SAMPLE_RATE 90000 // RFC 9134 §4.2: 90 kHz
#define JPEGXS_FPS 25
#define AV1_PAYLOAD_TYPE 98 // dynamic PT
#define AV1_SAMPLE_RATE 90000 // AOMedia AV1 RTP §7.2.1: 90 kHz
#define AV1_FPS 10
#define VVC_PAYLOAD_TYPE 99 // dynamic PT
#define VVC_SAMPLE_RATE 90000 // RFC 9328 (H.266): 90 kHz
#define VVC_FPS 10
#define AUDIO_STREAM_ID 0
#define VIDEO_STREAM_ID 1
#define JPEGXS_STREAM_ID 2
#define AV1_STREAM_ID 3
#define VVC_STREAM_ID 4
#define MAX_STREAMS 5
typedef struct {
uint64_t session_id;
SmolRTSP_RtpTransport *transport;
struct event *ev;
SmolRTSP_Droppable ctx;
} Stream;
typedef struct {
struct event_base *base;
struct bufferevent *bev;
struct sockaddr_storage addr;
size_t addr_len;
Stream streams[MAX_STREAMS];
int streams_playing;
} Client;
declImpl(SmolRTSP_Controller, Client);
static void listener_cb(
struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *sa,
int socklen, void *ctx);
static void on_event_cb(struct bufferevent *bev, short events, void *ctx);
static void on_sigint_cb(evutil_socket_t sig, short events, void *ctx);
static int setup_transport(
Client *self, SmolRTSP_Context *ctx, const SmolRTSP_Request *req,
SmolRTSP_Transport *t);
static int setup_tcp(
SmolRTSP_Context *ctx, SmolRTSP_Transport *t,
SmolRTSP_TransportConfig config);
static int setup_udp(
const struct sockaddr *addr, SmolRTSP_Context *ctx, SmolRTSP_Transport *t,
SmolRTSP_TransportConfig config);
typedef struct {
SmolRTSP_RtpTransport *transport;
size_t i;
struct event *ev;
struct bufferevent *bev;
int *streams_playing;
} AudioCtx;
static SmolRTSP_Droppable play_audio(
struct event_base *base, struct bufferevent *bev, SmolRTSP_RtpTransport *t,
struct event **ev, int *streams_playing);
static void send_audio_packet_cb(evutil_socket_t fd, short events, void *arg);
typedef struct {
SmolRTSP_NalTransport *transport;
SmolRTSP_NalStartCodeTester start_code_tester;
uint32_t timestamp;
U8Slice99 video;
uint8_t *nalu_start;
struct event *ev;
struct bufferevent *bev;
int *streams_playing;
} VideoCtx;
static SmolRTSP_Droppable play_video(
struct event_base *base, struct bufferevent *bev, SmolRTSP_RtpTransport *t,
struct event **ev, int *streams_playing);
static void send_video_packet_cb(evutil_socket_t fd, short events, void *arg);
static bool send_nalu(VideoCtx *ctx);
#ifdef ENABLE_JPEGXS
typedef struct {
SmolRTSP_JpegXsTransport *transport;
uint32_t timestamp;
struct event *ev;
struct bufferevent *bev;
int *streams_playing;
} JpegXsCtx;
static SmolRTSP_Droppable play_jpegxs(
struct event_base *base, struct bufferevent *bev, SmolRTSP_RtpTransport *t,
struct event **ev, int *streams_playing);
static void send_jpegxs_packet_cb(evutil_socket_t fd, short events, void *arg);
#endif
#ifdef ENABLE_AV1
/* The raw OBU stream gets pre-split into temporal units (TU) at TD OBU
* boundaries on first play; the callback then walks the list and loops. */
#define AV1_MAX_TUS 64
typedef struct {
size_t offset;
size_t length;
} Av1TuRange;
typedef struct {
SmolRTSP_Av1Transport *transport;
uint32_t timestamp;
Av1TuRange tus[AV1_MAX_TUS];
size_t tu_count;
size_t tu_index;
struct event *ev;
struct bufferevent *bev;
int *streams_playing;
} Av1Ctx;
static SmolRTSP_Droppable play_av1(
struct event_base *base, struct bufferevent *bev, SmolRTSP_RtpTransport *t,
struct event **ev, int *streams_playing);
static void send_av1_packet_cb(evutil_socket_t fd, short events, void *arg);
static size_t split_av1_temporal_units(
const uint8_t *stream, size_t stream_len, Av1TuRange *tus, size_t max_tus);
#endif
#ifdef ENABLE_VVC
/* vvenc emits a mix of 3-byte and 4-byte NAL start codes per Annex-B,
* so the simple single-tester scheme used by the H.264 demo doesn't
* work. Recognise either pattern at the current position. */
static size_t vvc_test_start_code(U8Slice99 data) {
const size_t four = smolrtsp_test_start_code_4b(data);
if (four != 0) {
return four;
}
return smolrtsp_test_start_code_3b(data);
}
typedef struct {
SmolRTSP_NalTransport *transport;
uint32_t timestamp;
U8Slice99 video;
uint8_t *nalu_start;
struct event *ev;
struct bufferevent *bev;
int *streams_playing;
} VvcCtx;
static SmolRTSP_Droppable play_vvc(
struct event_base *base, struct bufferevent *bev, SmolRTSP_RtpTransport *t,
struct event **ev, int *streams_playing);
static void send_vvc_packet_cb(evutil_socket_t fd, short events, void *arg);
static bool send_vvc_nalu(VvcCtx *ctx);
#endif
int main(void) {
srand(time(NULL));
int rc = EXIT_FAILURE;
struct event_base *base = NULL;
struct evconnlistener *listener = NULL;
struct event *sigint_handler = NULL;
if ((base = event_base_new()) == NULL) {
perror("event_base_new");
goto out;
}
struct sockaddr_in sin = {
.sin_family = AF_INET,
.sin_port = htons(SERVER_PORT),
};
if ((listener = evconnlistener_new_bind(
base, listener_cb, (void *)base,
LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, -1,
(struct sockaddr *)&sin, sizeof sin)) == NULL) {
const int bind_errno = errno;
perror("evconnlistener_new_bind");
if (bind_errno == EACCES) {
fprintf(
stderr,
"Hint: port %d is privileged; rerun with sudo, "
"grant CAP_NET_BIND_SERVICE, or change SERVER_PORT.\n",
SERVER_PORT);
}
goto out;
}
if ((sigint_handler =
evsignal_new(base, SIGINT, on_sigint_cb, (void *)base)) == NULL) {
perror("evsignal_new");
goto out;
}
if (event_add(sigint_handler, NULL) < 0) {
perror("event_add");
goto out;
}
printf("Server started on port %d.\n", SERVER_PORT);
event_base_dispatch(base);
puts("Done.");
rc = EXIT_SUCCESS;
out:
if (sigint_handler != NULL) {
event_free(sigint_handler);
}
if (listener != NULL) {
evconnlistener_free(listener);
}
if (base != NULL) {
event_base_free(base);
}
return rc;
}
static void listener_cb(
struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *sa,
int socklen, void *arg) {
(void)listener;
(void)fd;
(void)socklen;
struct event_base *base = arg;
struct bufferevent *bev;
if ((bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE)) ==
NULL) {
fputs("bufferevent_socket_new failed.\n", stderr);
event_base_loopbreak(base);
return;
}
Client *client = calloc(1, sizeof *client);
assert(client);
client->base = base;
client->bev = bev;
memcpy(&client->addr, sa, socklen);
client->addr_len = socklen;
SmolRTSP_Controller controller = DYN(Client, SmolRTSP_Controller, client);
void *ctx = smolrtsp_libevent_ctx(controller);
bufferevent_setcb(bev, smolrtsp_libevent_cb, NULL, on_event_cb, ctx);
bufferevent_enable(bev, EV_READ | EV_WRITE);
}
static void on_event_cb(struct bufferevent *bev, short events, void *ctx) {
if (events & BEV_EVENT_EOF) {
puts("Connection closed.");
} else if (events & BEV_EVENT_ERROR) {
perror("Got an error on the connection");
}
bufferevent_free(bev);
smolrtsp_libevent_ctx_free(ctx);
}
static void on_sigint_cb(evutil_socket_t sig, short events, void *ctx) {
(void)sig;
(void)events;
struct event_base *base = ctx;
puts("Caught an interrupt signal; exiting cleanly in two seconds.");
struct timeval delay = {2, 0};
event_base_loopexit(base, &delay);
}
static void Client_drop(VSelf) {
VSELF(Client);
for (size_t i = 0; i < MAX_STREAMS; i++) {
if (self->streams[i].ctx.vptr != NULL) {
VCALL(self->streams[i].ctx, drop);
}
}
free(self);
}
impl(SmolRTSP_Droppable, Client);
static void
Client_options(VSelf, SmolRTSP_Context *ctx, const SmolRTSP_Request *req) {
VSELF(Client);
(void)self;
(void)req;
smolrtsp_header(
ctx, SMOLRTSP_HEADER_PUBLIC, "DESCRIBE, SETUP, TEARDOWN, PLAY");
smolrtsp_respond_ok(ctx);
}
static void
Client_describe(VSelf, SmolRTSP_Context *ctx, const SmolRTSP_Request *req) {
VSELF(Client);
(void)self;
(void)req;
char sdp_buf[1024] = {0};
SmolRTSP_Writer sdp = smolrtsp_string_writer(sdp_buf);
ssize_t ret = 0;
// clang-format off
SMOLRTSP_SDP_DESCRIBE(
ret, sdp,
(SMOLRTSP_SDP_VERSION, "0"),
(SMOLRTSP_SDP_ORIGIN, "SmolRTSP 3855320066 3855320129 IN IP4 0.0.0.0"),
(SMOLRTSP_SDP_SESSION_NAME, "SmolRTSP example"),
(SMOLRTSP_SDP_CONNECTION, "IN IP4 0.0.0.0"),
(SMOLRTSP_SDP_TIME, "0 0"));
#ifdef ENABLE_AUDIO
SMOLRTSP_SDP_DESCRIBE(
ret, sdp,
(SMOLRTSP_SDP_MEDIA, "audio 0 RTP/AVP %d", AUDIO_PCMU_PAYLOAD_TYPE),
(SMOLRTSP_SDP_ATTR, "control:audio"));
#endif
#ifdef ENABLE_VIDEO
SMOLRTSP_SDP_DESCRIBE(
ret, sdp,
(SMOLRTSP_SDP_MEDIA, "video 0 RTP/AVP %d", VIDEO_PAYLOAD_TYPE),
(SMOLRTSP_SDP_ATTR, "control:video"),
(SMOLRTSP_SDP_ATTR, "rtpmap:%d H264/%" PRIu32, VIDEO_PAYLOAD_TYPE, VIDEO_SAMPLE_RATE),
(SMOLRTSP_SDP_ATTR, "fmtp:%d packetization-mode=1", VIDEO_PAYLOAD_TYPE),
(SMOLRTSP_SDP_ATTR, "framerate:%d", VIDEO_FPS));
#endif
#ifdef ENABLE_JPEGXS
SMOLRTSP_SDP_DESCRIBE(
ret, sdp,
(SMOLRTSP_SDP_MEDIA, "video 0 RTP/AVP %d", JPEGXS_PAYLOAD_TYPE),
(SMOLRTSP_SDP_ATTR, "control:jpegxs"),
(SMOLRTSP_SDP_ATTR, "rtpmap:%d jxsv/%d", JPEGXS_PAYLOAD_TYPE, JPEGXS_SAMPLE_RATE),
(SMOLRTSP_SDP_ATTR,
"fmtp:%d packetmode=0;transmode=1;sampling=YCbCr-4:2:2;"
"width=640;height=480;depth=8;colorimetry=BT709;TCS=SDR",
JPEGXS_PAYLOAD_TYPE),
(SMOLRTSP_SDP_ATTR, "framerate:%d", JPEGXS_FPS));
#endif
#ifdef ENABLE_AV1
SMOLRTSP_SDP_DESCRIBE(
ret, sdp,
(SMOLRTSP_SDP_MEDIA, "video 0 RTP/AVP %d", AV1_PAYLOAD_TYPE),
(SMOLRTSP_SDP_ATTR, "control:av1"),
(SMOLRTSP_SDP_ATTR, "rtpmap:%d AV1/%d", AV1_PAYLOAD_TYPE, AV1_SAMPLE_RATE),
(SMOLRTSP_SDP_ATTR, "fmtp:%d profile=0;level-idx=5;tier=0", AV1_PAYLOAD_TYPE),
(SMOLRTSP_SDP_ATTR, "framerate:%d", AV1_FPS));
#endif
#ifdef ENABLE_VVC
SMOLRTSP_SDP_DESCRIBE(
ret, sdp,
(SMOLRTSP_SDP_MEDIA, "video 0 RTP/AVP %d", VVC_PAYLOAD_TYPE),
(SMOLRTSP_SDP_ATTR, "control:vvc"),
(SMOLRTSP_SDP_ATTR, "rtpmap:%d H266/%d", VVC_PAYLOAD_TYPE, VVC_SAMPLE_RATE),
(SMOLRTSP_SDP_ATTR, "fmtp:%d profile-id=1;tier-flag=0;level-id=51", VVC_PAYLOAD_TYPE),
(SMOLRTSP_SDP_ATTR, "framerate:%d", VVC_FPS));
#endif
// clang-format on
assert(ret > 0);
smolrtsp_header(ctx, SMOLRTSP_HEADER_CONTENT_TYPE, "application/sdp");
smolrtsp_body(ctx, CharSlice99_from_str(sdp_buf));
smolrtsp_respond_ok(ctx);
}
static void
Client_setup(VSelf, SmolRTSP_Context *ctx, const SmolRTSP_Request *req) {
VSELF(Client);
SmolRTSP_Transport transport;
if (setup_transport(self, ctx, req, &transport) == -1) {
return;
}
size_t stream_id;
if (CharSlice99_primitive_ends_with(
req->start_line.uri, CharSlice99_from_str("/audio"))) {
stream_id = AUDIO_STREAM_ID;
}
#ifdef ENABLE_JPEGXS
else if (CharSlice99_primitive_ends_with(
req->start_line.uri, CharSlice99_from_str("/jpegxs"))) {
stream_id = JPEGXS_STREAM_ID;
}
#endif
#ifdef ENABLE_AV1
else if (CharSlice99_primitive_ends_with(
req->start_line.uri, CharSlice99_from_str("/av1"))) {
stream_id = AV1_STREAM_ID;
}
#endif
#ifdef ENABLE_VVC
else if (CharSlice99_primitive_ends_with(
req->start_line.uri, CharSlice99_from_str("/vvc"))) {
stream_id = VVC_STREAM_ID;
}
#endif
else {
stream_id = VIDEO_STREAM_ID;
}
Stream *stream = &self->streams[stream_id];
const bool aggregate_control_requested = SmolRTSP_HeaderMap_contains_key(
&req->header_map, SMOLRTSP_HEADER_SESSION);
if (aggregate_control_requested) {
uint64_t session_id;
if (smolrtsp_scanf_header(
&req->header_map, SMOLRTSP_HEADER_SESSION, "%" SCNu64,
&session_id) != 1) {
smolrtsp_respond(
ctx, SMOLRTSP_STATUS_BAD_REQUEST, "Malformed `Session'");
return;
}
stream->session_id = session_id;
} else {
stream->session_id = (uint64_t)rand();
}
if (AUDIO_STREAM_ID == stream_id) {
stream->transport = SmolRTSP_RtpTransport_new(
transport, AUDIO_PCMU_PAYLOAD_TYPE, AUDIO_SAMPLE_RATE);
}
#ifdef ENABLE_JPEGXS
else if (JPEGXS_STREAM_ID == stream_id) {
stream->transport = SmolRTSP_RtpTransport_new(
transport, JPEGXS_PAYLOAD_TYPE, JPEGXS_SAMPLE_RATE);
}
#endif
#ifdef ENABLE_AV1
else if (AV1_STREAM_ID == stream_id) {
stream->transport = SmolRTSP_RtpTransport_new(
transport, AV1_PAYLOAD_TYPE, AV1_SAMPLE_RATE);
}
#endif
#ifdef ENABLE_VVC
else if (VVC_STREAM_ID == stream_id) {
stream->transport = SmolRTSP_RtpTransport_new(
transport, VVC_PAYLOAD_TYPE, VVC_SAMPLE_RATE);
}
#endif
else {
stream->transport = SmolRTSP_RtpTransport_new(
transport, VIDEO_PAYLOAD_TYPE, VIDEO_SAMPLE_RATE);
}
smolrtsp_header(
ctx, SMOLRTSP_HEADER_SESSION, "%" PRIu64, stream->session_id);
smolrtsp_respond_ok(ctx);
}
static void
Client_play(VSelf, SmolRTSP_Context *ctx, const SmolRTSP_Request *req) {
VSELF(Client);
uint64_t session_id;
if (smolrtsp_scanf_header(
&req->header_map, SMOLRTSP_HEADER_SESSION, "%" SCNu64,
&session_id) != 1) {
smolrtsp_respond(
ctx, SMOLRTSP_STATUS_BAD_REQUEST, "Malformed `Session'");
return;
}
bool played = false;
for (size_t i = 0; i < MAX_STREAMS; i++) {
if (self->streams[i].session_id == session_id) {
if (AUDIO_STREAM_ID == i) {
self->streams[i].ctx = play_audio(
self->base, self->bev, self->streams[i].transport,
&self->streams[i].ev, &self->streams_playing);
}
#ifdef ENABLE_JPEGXS
else if (JPEGXS_STREAM_ID == i) {
self->streams[i].ctx = play_jpegxs(
self->base, self->bev, self->streams[i].transport,
&self->streams[i].ev, &self->streams_playing);
}
#endif
#ifdef ENABLE_AV1
else if (AV1_STREAM_ID == i) {
self->streams[i].ctx = play_av1(
self->base, self->bev, self->streams[i].transport,
&self->streams[i].ev, &self->streams_playing);
}
#endif
#ifdef ENABLE_VVC
else if (VVC_STREAM_ID == i) {
self->streams[i].ctx = play_vvc(
self->base, self->bev, self->streams[i].transport,
&self->streams[i].ev, &self->streams_playing);
}
#endif
else {
self->streams[i].ctx = play_video(
self->base, self->bev, self->streams[i].transport,
&self->streams[i].ev, &self->streams_playing);
}
played = true;
}
}
if (!played) {
smolrtsp_respond(
ctx, SMOLRTSP_STATUS_SESSION_NOT_FOUND, "Invalid Session ID");
return;
}
smolrtsp_header(ctx, SMOLRTSP_HEADER_RANGE, "npt=now-");
smolrtsp_respond_ok(ctx);
}
static void
Client_teardown(VSelf, SmolRTSP_Context *ctx, const SmolRTSP_Request *req) {
VSELF(Client);
uint64_t session_id;
if (smolrtsp_scanf_header(
&req->header_map, SMOLRTSP_HEADER_SESSION, "%" SCNu64,
&session_id) != 1) {
smolrtsp_respond(
ctx, SMOLRTSP_STATUS_BAD_REQUEST, "Malformed `Session'");
return;
}
bool teardowned = false;
for (size_t i = 0; i < MAX_STREAMS; i++) {
if (self->streams[i].session_id == session_id) {
event_del(self->streams[i].ev);
teardowned = true;
}
}
if (!teardowned) {
smolrtsp_respond(
ctx, SMOLRTSP_STATUS_SESSION_NOT_FOUND, "Invalid Session ID");
return;
}
smolrtsp_respond_ok(ctx);
}
static void
Client_unknown(VSelf, SmolRTSP_Context *ctx, const SmolRTSP_Request *req) {
VSELF(Client);
(void)self;
(void)req;
smolrtsp_respond(ctx, SMOLRTSP_STATUS_METHOD_NOT_ALLOWED, "Unknown method");
}
static SmolRTSP_ControlFlow
Client_before(VSelf, SmolRTSP_Context *ctx, const SmolRTSP_Request *req) {
VSELF(Client);
(void)self;
(void)ctx;
printf(
"%.*s %.*s CSeq=%" PRIu32 ".\n", (int)req->start_line.method.len,
req->start_line.method.ptr, (int)req->start_line.uri.len,
req->start_line.uri.ptr, req->cseq);
return SmolRTSP_ControlFlow_Continue;
}
static void Client_after(
VSelf, ssize_t ret, SmolRTSP_Context *ctx, const SmolRTSP_Request *req) {
VSELF(Client);
(void)self;
(void)ctx;
(void)req;
if (ret < 0) {
perror("Failed to respond");
}
}
impl(SmolRTSP_Controller, Client);
static int setup_transport(
Client *self, SmolRTSP_Context *ctx, const SmolRTSP_Request *req,
SmolRTSP_Transport *t) {
CharSlice99 transport_val;
const bool transport_found = SmolRTSP_HeaderMap_find(
&req->header_map, SMOLRTSP_HEADER_TRANSPORT, &transport_val);
if (!transport_found) {
smolrtsp_respond(
ctx, SMOLRTSP_STATUS_BAD_REQUEST, "`Transport' not present");
return -1;
}
SmolRTSP_TransportConfig config;
if (smolrtsp_parse_transport(&config, transport_val) == -1) {
smolrtsp_respond(
ctx, SMOLRTSP_STATUS_BAD_REQUEST, "Malformed `Transport'");
return -1;
}
switch (config.lower) {
case SmolRTSP_LowerTransport_TCP:
if (setup_tcp(ctx, t, config) == -1) {
smolrtsp_respond_internal_error(ctx);
return -1;
}
break;
case SmolRTSP_LowerTransport_UDP:
if (setup_udp((const struct sockaddr *)&self->addr, ctx, t, config) ==
-1) {
smolrtsp_respond_internal_error(ctx);
return -1;
}
break;
}
return 0;
}
static int setup_tcp(
SmolRTSP_Context *ctx, SmolRTSP_Transport *t,
SmolRTSP_TransportConfig config) {
ifLet(config.interleaved, SmolRTSP_ChannelPair_Some, interleaved) {
*t = smolrtsp_transport_tcp(
SmolRTSP_Context_get_writer(ctx), interleaved->rtp_channel, 0);
smolrtsp_header(
ctx, SMOLRTSP_HEADER_TRANSPORT,
"RTP/AVP/TCP;unicast;interleaved=%" PRIu8 "-%" PRIu8,
interleaved->rtp_channel, interleaved->rtcp_channel);
return 0;
}
smolrtsp_respond(
ctx, SMOLRTSP_STATUS_BAD_REQUEST, "`interleaved' not found");
return -1;
}
static int setup_udp(
const struct sockaddr *addr, SmolRTSP_Context *ctx, SmolRTSP_Transport *t,
SmolRTSP_TransportConfig config) {
ifLet(config.client_port, SmolRTSP_PortPair_Some, client_port) {
int fd;
if ((fd = smolrtsp_dgram_socket(
addr->sa_family, smolrtsp_sockaddr_ip(addr),
client_port->rtp_port)) == -1) {
return -1;
}
*t = smolrtsp_transport_udp(fd);
smolrtsp_header(
ctx, SMOLRTSP_HEADER_TRANSPORT,
"RTP/AVP/UDP;unicast;client_port=%" PRIu16 "-%" PRIu16,
client_port->rtp_port, client_port->rtcp_port);
return 0;
}
smolrtsp_respond(
ctx, SMOLRTSP_STATUS_BAD_REQUEST, "`client_port' not found");
return -1;
}
static void AudioCtx_drop(VSelf) {
VSELF(AudioCtx);
event_free(self->ev);
VTABLE(SmolRTSP_RtpTransport, SmolRTSP_Droppable).drop(self->transport);
free(self);
}
impl(SmolRTSP_Droppable, AudioCtx);
static SmolRTSP_Droppable play_audio(
struct event_base *base, struct bufferevent *bev, SmolRTSP_RtpTransport *t,
struct event **ev, int *streams_playing) {
AudioCtx *ctx = malloc(sizeof *ctx);
assert(ctx);
*ctx = (AudioCtx){
.transport = t,
.i = 0,
.ev = NULL,
.streams_playing = streams_playing,
.bev = bev,
};
ctx->ev = event_new(
base, -1, EV_PERSIST | EV_TIMEOUT, send_audio_packet_cb, (void *)ctx);
assert(ctx->ev);
event_add(
ctx->ev, &(const struct timeval){
.tv_sec = 0,
.tv_usec = AUDIO_PACKETIZATION_TIME_US,
});
*ev = ctx->ev;
(*streams_playing)++;
return DYN(AudioCtx, SmolRTSP_Droppable, ctx);
}
static void send_audio_packet_cb(evutil_socket_t fd, short events, void *arg) {
(void)fd;
(void)events;
AudioCtx *ctx = arg;
if (ctx->i * AUDIO_SAMPLES_PER_PACKET >= ___media_audio_g711a_len) {
event_del(ctx->ev);
(*ctx->streams_playing)--;
if (0 == *ctx->streams_playing) {
bufferevent_trigger_event(ctx->bev, BEV_EVENT_EOF, 0);
}
return;
}
const SmolRTSP_RtpTimestamp ts =
SmolRTSP_RtpTimestamp_Raw(ctx->i * AUDIO_SAMPLES_PER_PACKET);
const bool marker = false;
const size_t samples_count =
___media_audio_g711a_len <
ctx->i * AUDIO_SAMPLES_PER_PACKET + AUDIO_SAMPLES_PER_PACKET
? ___media_audio_g711a_len % AUDIO_SAMPLES_PER_PACKET
: AUDIO_SAMPLES_PER_PACKET;
const U8Slice99 header = U8Slice99_empty(),
payload = U8Slice99_new(
___media_audio_g711a +
ctx->i * AUDIO_SAMPLES_PER_PACKET,
samples_count);
if (SmolRTSP_RtpTransport_send_packet(
ctx->transport, ts, marker, header, payload) == -1) {
perror("Failed to send RTP/PCMU");
}
ctx->i++;
}
static void VideoCtx_drop(VSelf) {
VSELF(VideoCtx);
event_free(self->ev);
VTABLE(SmolRTSP_NalTransport, SmolRTSP_Droppable).drop(self->transport);
free(self);
}
impl(SmolRTSP_Droppable, VideoCtx);
static SmolRTSP_Droppable play_video(
struct event_base *base, struct bufferevent *bev, SmolRTSP_RtpTransport *t,
struct event **ev, int *streams_playing) {
U8Slice99 video = Slice99_typed_from_array(___media_video_h264);
SmolRTSP_NalStartCodeTester start_code_tester;
if ((start_code_tester = smolrtsp_determine_start_code(video)) == NULL) {
fputs("Invalid video file.\n", stderr);
abort();
}
VideoCtx *ctx = malloc(sizeof *ctx);
assert(ctx);
*ctx = (VideoCtx){
.transport = SmolRTSP_NalTransport_new(t),
.start_code_tester = start_code_tester,
.timestamp = 0,
.video = video,
.nalu_start = NULL,
.ev = NULL,
.bev = bev,
.streams_playing = streams_playing,
};
ctx->ev = event_new(
base, -1, EV_PERSIST | EV_TIMEOUT, send_video_packet_cb, (void *)ctx);
assert(ctx->ev);
event_add(
ctx->ev, &(const struct timeval){
.tv_sec = 0,
.tv_usec = 1e6 / VIDEO_FPS,
});
*ev = ctx->ev;
(*streams_playing)++;
return DYN(VideoCtx, SmolRTSP_Droppable, ctx);
}
static void send_video_packet_cb(evutil_socket_t fd, short events, void *arg) {
(void)fd;
(void)events;
VideoCtx *ctx = arg;
again:
if (U8Slice99_is_empty(ctx->video)) {
send_nalu(ctx);
event_del(ctx->ev);
(*ctx->streams_playing)--;
if (0 == *ctx->streams_playing) {
bufferevent_trigger_event(ctx->bev, BEV_EVENT_EOF, 0);
}
return;
}
const size_t start_code_len = ctx->start_code_tester(ctx->video);
if (0 == start_code_len) {
ctx->video = U8Slice99_advance(ctx->video, 1);
goto again;
}
bool au_found = false;
if (NULL != ctx->nalu_start) {
au_found = send_nalu(ctx);
}
ctx->video = U8Slice99_advance(ctx->video, start_code_len);
ctx->nalu_start = ctx->video.ptr;
if (!au_found) {
goto again;
}
}
static bool send_nalu(VideoCtx *ctx) {
const SmolRTSP_NalUnit nalu = {
.header = SmolRTSP_NalHeader_H264(
SmolRTSP_H264NalHeader_parse(ctx->nalu_start[0])),
.payload = U8Slice99_from_ptrdiff(ctx->nalu_start + 1, ctx->video.ptr),
};
bool au_found = false;
if (SmolRTSP_NalHeader_unit_type(nalu.header) ==
SMOLRTSP_H264_NAL_UNIT_AUD) {
ctx->timestamp += VIDEO_SAMPLE_RATE / VIDEO_FPS;
au_found = true;
}
if (SmolRTSP_NalTransport_send_packet(
ctx->transport, SmolRTSP_RtpTimestamp_Raw(ctx->timestamp), true,
nalu) == -1) {
perror("Failed to send RTP/NAL");
}
return au_found;
}
#ifdef ENABLE_JPEGXS
static void JpegXsCtx_drop(VSelf) {
VSELF(JpegXsCtx);
event_free(self->ev);
VTABLE(SmolRTSP_JpegXsTransport, SmolRTSP_Droppable).drop(self->transport);
free(self);
}
impl(SmolRTSP_Droppable, JpegXsCtx);
static SmolRTSP_Droppable play_jpegxs(
struct event_base *base, struct bufferevent *bev, SmolRTSP_RtpTransport *t,
struct event **ev, int *streams_playing) {
JpegXsCtx *ctx = malloc(sizeof *ctx);
assert(ctx);
*ctx = (JpegXsCtx){
.transport = SmolRTSP_JpegXsTransport_new(t),
.timestamp = 0,
.ev = NULL,
.bev = bev,
.streams_playing = streams_playing,
};
ctx->ev = event_new(
base, -1, EV_PERSIST | EV_TIMEOUT, send_jpegxs_packet_cb, (void *)ctx);
assert(ctx->ev);
event_add(
ctx->ev, &(const struct timeval){
.tv_sec = 0,
.tv_usec = 1e6 / JPEGXS_FPS,
});
*ev = ctx->ev;
(*streams_playing)++;
return DYN(JpegXsCtx, SmolRTSP_Droppable, ctx);
}
static void
send_jpegxs_packet_cb(evutil_socket_t fd, short events, void *arg) {