-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathstreaming.c
More file actions
7902 lines (7263 loc) ยท 301 KB
/
streaming.c
File metadata and controls
7902 lines (7263 loc) ยท 301 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <regex.h>
#include <sys/mman.h>
#include <pthread.h>
#include <sys/stat.h>
#include <errno.h>
#include <setjmp.h>
#include <dict.h>
//#include <ncapi.h>
//#include <trace.h>
#include <microhttpd.h>
#include "common.h"
#include "scx_glru.h"
#include "reqpool.h"
#include "vstring.h"
#include "site.h"
#include "httpd.h"
#include "limitrate.h"
#include "scx_util.h"
#include "smilparser.h"
#include "sessionmgr.h"
#include "status.h"
#include "voxio_def.h"
//#include "solCMAF.h"
//#include "err.h"
#include "scx_list.h"
#include "streaming.h"
#include "streaming_lru.h"
#include "meta_disk_cache.h"
#include "soluri2.h"
#include "scx_timer.h"
#include "md5.h"
#include "soljwt.h"
pthread_mutex_t gscx__media_lock = PTHREAD_MUTEX_INITIALIZER;
int gscx__io_buffer_size = 65536*4; /* zipper ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ io ์์ฒญ์ ์ฌ์ฉํ๋ ๋ฒํผ ํฌ๊ธฐ */
int gscx__list_count = 0; /* media list์ delete list์ ๋ค์ด ์๋ media์ ์ซ์ */
extern regex_t *gscx__url_preg;
extern int gscx__use_aesni;
extern volatile uint64_t gscx__media_context_size;
extern volatile int gscx__media_context_create_interval_cnt;
extern volatile uint64_t gscx__media_context_create_interval_size;
extern int gscx__service_available;
extern int gscx__enable_async;
extern __thread jmp_buf __thread_jmp_buf;
extern __thread int __thread_jmp_working;
typedef struct tag_strm_reg {
regex_t *preg;
int media_type;
int protocol; /* hls, dash, progresive, crossdomain */
int build_type; /* zipper.h ์ ์ ์๋ type */
int count; /* ํจํด๋งค์นญ์ผ๋ก ์ถ์ถํด์ผ ํ๋ ์ธ์ ์ */
char mime_type[32];
char pattern[64];
} strm_reg_t;
/*
* VOD์ฉ ๊ฐ์ ํ์ผ ํจํด
* */
static strm_reg_t gscx__strm_vod_reg[] = {
/* {preg, media_type, protocol, build_type, count, mime_type, pattern}, */
{NULL, MEDIA_TYPE_CROSSDOMAIN, O_PROTOCOL_CORS, BLDTYPE_NONE, 0, "text/xml", "^crossdomain\\.xml$"},
{NULL, MEDIA_TYPE_CLIENTACCESSPOLICY, O_PROTOCOL_CORS, BLDTYPE_NONE, 0, "text/xml", "^clientaccesspolicy\\.xml$"},
{NULL, MEDIA_TYPE_MPEG_DASH_MANIFEST, O_PROTOCOL_DASH, BLDTYPE_MPD, 0, "application/dash+xml", "^manifest\\.mpd$"}, /* BLDTYPE_MPD์ด ๋ง๋์ง ๋์ค์ ํ์ธ ํ์*/
{NULL, MEDIA_TYPE_MPEG_DASH_TS_MANIFEST,O_PROTOCOL_DASH, BLDTYPE_MPD, 0, "application/dash+xml", "^manifest_ts\\.mpd$"},
{NULL, MEDIA_TYPE_MPEG_DASH_YT_MANIFEST,O_PROTOCOL_DASH, BLDTYPE_MPD, 0, "application/dash+xml", "^manifest_yt\\.mpd$"},
{NULL, MEDIA_TYPE_MPEG_DASH_VIDEO_INIT, O_PROTOCOL_DASH, BLDTYPE_FMP4INIT, 1, "video/mp4", "^([0-9]+)_video_init\\.m4s$"}, /* BLDTYPE_FMP4INIT ๋ง๋์ง ํ์ธ ํ์ */
{NULL, MEDIA_TYPE_MPEG_DASH_VIDEO, O_PROTOCOL_DASH, BLDTYPE_FMP4, 2, "video/mp4", "^([0-9]+)_video_segment_([0-9]+)\\.m4s$"},
{NULL, MEDIA_TYPE_MPEG_DASH_AUDIO_INIT, O_PROTOCOL_DASH, BLDTYPE_FMP4INIT, 1, "audio/mp4", "^([0-9]+)_audio_init\\.m4s$"},
{NULL, MEDIA_TYPE_MPEG_DASH_AUDIO, O_PROTOCOL_DASH, BLDTYPE_FMP4, 2, "audio/mp4", "^([0-9]+)_audio_segment_([0-9]+)\\.m4s$"},
{NULL, MEDIA_TYPE_MPEG_DASH_TS_INDEX, O_PROTOCOL_DASH, BLDTYPE_SIDX, 1, "application/octet-stream", "^([0-9]+)_segment_index\\.sidx$"},
{NULL, MEDIA_TYPE_MPEG_DASH_TS, O_PROTOCOL_DASH, BLDTYPE_STS, 2, "video/MP2T", "^([0-9]+)_segment_([0-9]+)\\.ts$"},
{NULL, MEDIA_TYPE_MPEG_DASH_YT_VIDEO, O_PROTOCOL_DASH, BLDTYPE_FMP4SINGLE, 1, "video/mp4", "^([0-9]+)_video_single\\.m4s$"},
{NULL, MEDIA_TYPE_MPEG_DASH_YT_AUDIO, O_PROTOCOL_DASH, BLDTYPE_FMP4SINGLE, 1, "audio/mp4", "^([0-9]+)_audio_single\\.m4s$"},
{NULL, MEDIA_TYPE_MSS_MANIFEST, O_PROTOCOL_MSS, BLDTYPE_IIS, 0, "text/xml", "^Manifest$"},
{NULL, MEDIA_TYPE_MSS_VIDEO, O_PROTOCOL_MSS, BLDTYPE_MOOF, 2, "video/mp4", "^([0-9]+)_video_segment_([0-9]+)\\.ismv$"},
{NULL, MEDIA_TYPE_MSS_AUDIO, O_PROTOCOL_MSS, BLDTYPE_MOOF, 2, "video/mp4", "^([0-9]+)_audio_segment_([0-9]+)\\.isma$"},
{NULL, MEDIA_TYPE_HLS_MAIN_M3U8, O_PROTOCOL_HLS, BLDTYPE_NONE, 0, "application/vnd.apple.mpegurl", "^playlist\\.m3u8$"},
{NULL, MEDIA_TYPE_HLS_SUB_M3U8, O_PROTOCOL_HLS, BLDTYPE_M3U8, 1, "application/vnd.apple.mpegurl", "^content[_]?([0-9]*)\\.m3u8$"},
{NULL, MEDIA_TYPE_HLS_VTT_M3U8, O_PROTOCOL_HLS, BLDTYPE_M3U8, 1, "application/vnd.apple.mpegurl", "^subtitle[_]?([0-9]*)\\.m3u8$"},
{NULL, MEDIA_TYPE_HLS_FMP4_MAIN_M3U8, O_PROTOCOL_HLS, BLDTYPE_FMP4M3U8, 0, "application/vnd.apple.mpegurl", "^playlist_fmp4\\.m3u8$"},
{NULL, MEDIA_TYPE_HLS_FMP4_AUDIO_M3U8, O_PROTOCOL_HLS, BLDTYPE_FMP4SUBM3U8,2, "application/vnd.apple.mpegurl", "^fmp4_audio[_]?([0-9]*)\\.m3u8$"},
{NULL, MEDIA_TYPE_HLS_FMP4_VIDEO_M3U8, O_PROTOCOL_HLS, BLDTYPE_FMP4SUBM3U8,2, "application/vnd.apple.mpegurl", "^fmp4_video[_]?([0-9]*)\\.m3u8$"},
{NULL, MEDIA_TYPE_HLS_TS, O_PROTOCOL_HLS, BLDTYPE_STS, 2, "video/MP2T", "^content[_]?([0-9]*)_([0-9]+)\\.ts$"},
{NULL, MEDIA_TYPE_HLS_VTT, O_PROTOCOL_HLS, BLDTYPE_VTT, 2, "text/vtt", "^subtitle[_]?([0-9]*)_([0-9]+)\\.vtt$"},
{NULL, MEDIA_TYPE_MP4, O_PROTOCOL_PROGRESSIVE, BLDTYPE_MP4, 0, "video/mp4", "^content\\.mp4$"},
{NULL, MEDIA_TYPE_M4A, O_PROTOCOL_PROGRESSIVE, BLDTYPE_MP4, 0, "audio/mp4", "^content\\.m4a$"},
{NULL, MEDIA_TYPE_M4V, O_PROTOCOL_PROGRESSIVE, BLDTYPE_MP4, 0, "video/mp4", "^content\\.m4v$"},
{NULL, MEDIA_TYPE_MP3, O_PROTOCOL_PROGRESSIVE, BLDTYPE_MP3, 0, "audio/mpeg", "^content\\.mp3$"},
{NULL, MEDIA_TYPE_FLV, O_PROTOCOL_PROGRESSIVE, BLDTYPE_FLV, 0, "video/x-flv", "^content\\.flv$"},
{NULL, MEDIA_TYPE_FLAC, O_PROTOCOL_PROGRESSIVE, BLDTYPE_FLAC, 0, "audio/flac", "^content\\.flac$"},
{NULL, MEDIA_TYPE_MP4_DOWN, O_PROTOCOL_PROGRESSIVE, BLDTYPE_NONE, 0, "video/mp4", "^.+\\.[mM][pP]4$"}, /* content.mpX๋ก ๋๋์ง ์๋ mpXํ์ผ์ ์ผ๋ฐ ๋ค์ด๋ก๋์ด๋ค. ์ด๋ถ๋ถ์ด ํญ์ ๋ง์ง๋ง์ ์์ผ ํ๋ค. */
{NULL, MEDIA_TYPE_M4A_DOWN, O_PROTOCOL_PROGRESSIVE, BLDTYPE_NONE, 0, "audio/mp4", "^.+\\.[mM]4[aA]$"},
{NULL, MEDIA_TYPE_M4V_DOWN, O_PROTOCOL_PROGRESSIVE, BLDTYPE_NONE, 0, "video/mp4", "^.+\\.[mM]4[vV]$"},
{NULL, MEDIA_TYPE_MP3_DOWN, O_PROTOCOL_PROGRESSIVE, BLDTYPE_NONE, 0, "audio/mpeg", "^.+\\.[mM][pP]3$"},
{NULL, MEDIA_TYPE_FLAC_DOWN, O_PROTOCOL_PROGRESSIVE, BLDTYPE_NONE, 0, "audio/flac", "^.+\\.[fF][lL][aA][cC]"},
#ifdef ZIPPER
// solproxy์์ ์๋ ๋ถ๋ถ์ด ์์๊ฒฝ์ฐ smil ํ์ผ ์์ฒด๋ก ํผ์ง๊ฐ ์๋๋ ๋ฌธ์ ๋ฐ์
// https://jarvis.solbox.com/redmine/issues/33477
{NULL, MEDIA_TYPE_SMIL_DOWN, O_PROTOCOL_PROGRESSIVE, BLDTYPE_NONE, 0, "application/smil+xml", "^.+\\.[sS][mM][iI][lL]"}, /* ์ด type์ ์บ์ฑ๋ smil ํผ์ง ์ฉ๋๋ก๋ง ์ฌ์ฉ */
{NULL, MEDIA_TYPE_SUBTITLE_DOWN, O_PROTOCOL_PROGRESSIVE, BLDTYPE_NONE, 0, "text/plain", "^.+\\.[sS][rR][tT]"}, /* ์ด type์ ์บ์ฑ๋ srt ํ์ผ ํผ์ง ์ฉ๋๋ก๋ง ์ฌ์ฉ */
{NULL, MEDIA_TYPE_SUBTITLE_DOWN, O_PROTOCOL_PROGRESSIVE, BLDTYPE_NONE, 0, "text/plain", "^.+\\.[vV][tT][tT]"}, /* ์ด type์ ์บ์ฑ๋ vtt ํผ์ง ์ฉ๋๋ก๋ง ์ฌ์ฉ */
#endif
{NULL, MEDIA_TYPE_CMAF_VIDEO_TUNNEL, O_PROTOCOL_TUNNEL, BLDTYPE_NONE, 1, "video/mpeg", "^([0-9]+)_video_tunnel\\.cmaf$"},
{NULL, MEDIA_TYPE_CMAF_AUDIO_TUNNEL, O_PROTOCOL_TUNNEL, BLDTYPE_NONE, 1, "audio/mpeg", "^([0-9]+)_audio_tunnel\\.cmaf$"},
{NULL, MEDIA_TYPE_ENC_KEY, O_PROTOCOL_HLS, BLDTYPE_NONE, 0, "binary/octet-stream", "^decrypt\\.key$"},
{NULL, MEDIA_TYPE_MODIFY_HLS_MANIFEST, O_PROTOCOL_HLS, BLDTYPE_NONE, 0, "application/vnd.apple.mpegurl", "ddddddddddddddd" /* ํจํด ๋งค์นญ์ด ํ์ ์์ด์ ์ด๋ ๊ฒ ํ๊ธฐํจ. */ },
{NULL, MEDIA_TYPE_DOWN, O_PROTOCOL_HLS, BLDTYPE_NONE, 0, "", "ddddddddddddddd" /* ํจํด ๋งค์นญ์ด ํ์ ์์ด์ ์ด๋ ๊ฒ ํ๊ธฐํจ. */ },
{NULL, MEDIA_TYPE_FCS_IDENT, O_PROTOCOL_CORS, BLDTYPE_NONE, 0, "text/html", "^/fcs/$"} /* ํจํด์ ์ฌ์ฉ์ํจ */
};
/* Live ad stitching ์ฉ ๊ฐ์ ํ์ผ ํจํด */
static strm_reg_t gscx__strm_live_reg[] = {
/* {preg, media_type, protocol, build_type, count, mime_type, pattern}, */
{NULL, MEDIA_TYPE_CROSSDOMAIN, O_PROTOCOL_CORS, BLDTYPE_NONE, 0, "text/xml", "^crossdomain\\.xml$"},
{NULL, MEDIA_TYPE_CLIENTACCESSPOLICY, O_PROTOCOL_CORS, BLDTYPE_NONE, 0, "text/xml", "^clientaccesspolicy\\.xml$"},
{NULL, MEDIA_TYPE_HLS_MAIN_M3U8, O_PROTOCOL_HLS, BLDTYPE_NONE, 0, "application/vnd.apple.mpegurl", "^playlist\\.m3u8$"},
{NULL, MEDIA_TYPE_HLS_SUB_M3U8, O_PROTOCOL_HLS, BLDTYPE_NONE, 0, "application/vnd.apple.mpegurl", "^[a-zA-Z0-9._\\-]+\\.m3u8$"},
{NULL, MEDIA_TYPE_HLS_TS, O_PROTOCOL_HLS, BLDTYPE_STS, 0, "video/MP2T", "^[a-zA-Z0-9._\\-]+\\.ts$"},
{NULL, MEDIA_TYPE_FCS_IDENT, O_PROTOCOL_CORS, BLDTYPE_NONE, 0, "text/html", "^/fcs/$"}
};
const int gscx__count_strm_vod_reg = howmany(sizeof(gscx__strm_vod_reg), sizeof(strm_reg_t));
const int gscx__count_strm_live_reg = howmany(sizeof(gscx__strm_live_reg), sizeof(strm_reg_t));
#ifdef ZIPPER
/* '/[application]/[instance]/[protocol]/[lang]/[version]/[content]/[meta]/[content]/[meta]/[virtual_file]?[argument]' */
//const char gscx__strm_url_pattern[] = {"^\\/+([a-zA-Z0-9.\\-_]+)\\/+([a-zA-Z0-9.\\-_]+)\\/+([a-zA-Z0-9.\\-_]+)\\/+([a-zA-Z0-9.\\-_]+)\\/+" /*์ฌ๊ธฐ๊น์ง๊ฐ lang๊น์ง */
//"[/a-zA-Z0-9.\\-_]+(/[a-zA-Z0-9\\-_]+\\.[a-zA-Z0-9]+)(\\?.*)?$"};
const char gscx__strm_url_pattern[] = {
"^/+([a-zA-Z0-9._\\-]+)/+([a-zA-Z0-9._\\-]+)/+([a-zA-Z0-9\\-]+)/+([a-zA-Z]+)/+([a-zA-Z0-9._\\-]+)"
"(/*.*)" /* ์ธ์ฝ๋ฉ๋ ํ๊ธ ๊ฒฝ๋ก ๋ค์ด ๋ค์ด ์ฌ์๋ ์์ด์ ๋ณ๊ฒฝํจ */ //"(/*[/a-zA-Z0-9\\-]+)" /* contents ๊ฒฝ๋ก๋ค */
"/+([^?]+\\.?[a-zA-Z0-9]+)" /* virtual file */
//"/([a-zA-Z0-9_\\-]+)\\.([a-zA-Z0-9]+)" /* virtual file name,format */
"(\\?{1}.*)?$" /* argument parameter */
};
#else
/* '/[content path]/[virtual_file]?[argument]' */
#if 0
const char gscx__strm_url_pattern[] = {
"^(.*\\.[Mm][Pp][34]|.*\\.[mM][oO][vV]|.*\\.[mM][kK][vV]|.*\\.[mM][4][vVaA]|.*\\.[fF][lL][aA][cC]|.*\\.[sS][mM][iI][lL])" /* contents ๊ฒฝ๋ก */
"/+([^?^/]+\\.?[a-zA-Z0-9]+)" /* virtual file */
//"/([a-zA-Z0-9_\\-]+)\\.([a-zA-Z0-9]+)" /* virtual file name,format */
"(\\?{1}.*)?$" /* argument parameter */
};
#else
/*
* ๊ฐ์ ํ์ผ๋ช
์์ด progressive download ์๋น์คํ๋ ๊ฒฝ์ฐ url ํจํด.
* ์ด๋ ๊ฐ์ฅ ๋ฌธ์ ๊ฐ ์ค์ ์๋ณธ์ ๊ฒฝ๋ก๊ฐ /mp4/content.mp4์ ๊ฐ์ ๊ฒฝ์ฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋ฅผ ์ง์ํ ์ ์๋ค.
* https://jarvis.solbox.com/redmine/issues/33050
*/
const char gscx__strm_url_pattern[] = {
"^(.*)" /* contents ๊ฒฝ๋ก */
"/+([^?^/]+\\.?[a-zA-Z0-9]+)+" /* virtual file */
"(\\?{1}.*)?$" /* argument parameter */
};
#endif
#endif
/*
* ์์ฐ์ url ํจํด
* https://jarvis.solbox.com/redmine/issues/33317
*/
const char gscx__wowza_url_pattern[] = {
"^/+([a-zA-Z0-9._\\-]+)/+([a-zA-Z0-9._\\-]+)/" /* application / instance */
"(.*)" /* contents ๊ฒฝ๋ก(์ฒ์์ /๊ฐ ์ ์ธ ๋๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก ์ฒ๋ฆฌ ํด์ค์ผ ํ๋ค.) */
"/+([^?^/]+\\.?[a-zA-Z0-9]+)+" /* virtual file */
"(\\?{1}.*)?$" /* argument parameter */
};
#define MEDIA_FORMAT_MAX_LEN 4096
#define MEDIA_ARGUMENT_MAX_LEN MEDIA_FORMAT_MAX_LEN-32
const char gscx__hls_ts_format[] = {"content_%i.%e"}; //TS ํ์ผ๋ช
์ ๊ท์น format
const char gscx__hls_adaptive_ts_format[] = {"content_%d_%i.%e"}; //adaptive TS ํ์ผ๋ช
์ ๊ท์น format
const char gscx__hls_adaptive_vtt_format[] = {"subtitle_%d_%i.%e"}; //adaptive TS ํ์ผ๋ช
์ ๊ท์น format
const char gscx__hls_extinf_format[] = {"#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1280000\n"}; //m3u8์์ฑ ๊ท์น format
const char gscx__hls_m3u8_format[] = {"content.%e"}; //main m3u8์์ฑ ๊ท์น format
const char gscx__hls_fmp4_audio_m3u8_format[] = {"fmp4_audio_%a.%e"}; // HLS fmp4 audio manifest naming ๊ท์น
const char gscx__hls_fmp4_video_m3u8_format[] = {"fmp4_video_%a.%e"}; // HLS fmp4 video manifest naming ๊ท์น
const char gscx__hls_fmp4_subtitle_m3u8_format[] = {"subtitle_%a.%e"}; // HLS fmp4 subtitle manifest naming ๊ท์น
const char gscx__hls_fmp4_video_init_format[] = {"%a_video_init.m4s"}; //HLS fmp4 video init ํ์ผ naming ๊ท์น
const char gscx__hls_fmp4_audio_init_format[] = {"%a_audio_init.m4s"}; //HLS fmp4 audio init ํ์ผ naming ๊ท์น
#if 0
const char gscx__hls_fmp4_video_seq_format[] = {"%a_video_segment_%t.m4s"}; //HLS fmp4 video segment ํ์ผ naming ๊ท์น
const char gscx__hls_fmp4_audio_seq_format[] = {"%a_audio_segment_%t.m4s"}; //HLS fmp4 audio init ํ์ผ naming ๊ท์น
#else
const char gscx__hls_fmp4_video_seq_format[] = {"%a_video_segment_%i.m4s"}; //HLS fmp4 video segment ํ์ผ naming ๊ท์น
const char gscx__hls_fmp4_audio_seq_format[] = {"%a_audio_segment_%i.m4s"}; //HLS fmp4 audio init ํ์ผ naming ๊ท์น
#endif
const char gscx__hls_adaptive_extinf_format[] = {"#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=%d"}; //m3u8์์ฑ ๊ท์น format
const char gscx__hls_adaptive_m3u8_format[] = {"content_%d.%e"}; //adaptive main m3u8์์ฑ ๊ท์น format
const char gscx__dash_video_init_format[] = {"$RepresentationID$_video_init.m4s"}; //DASH ์์ฑ ๊ท์น format
const char gscx__dash_audio_init_format[] = {"$RepresentationID$_audio_init.m4s"}; //DASH ์์ฑ ๊ท์น format
#if 0
const char gscx__dash_video_seq_format[] = {"$RepresentationID$_video_segment_$Time$.m4s"}; //DASH ์์ฑ ๊ท์น format
const char gscx__dash_audio_seq_format[] = {"$RepresentationID$_audio_segment_$Time$.m4s"}; //DASH ์์ฑ ๊ท์น format
#else
const char gscx__dash_video_seq_format[] = {"$RepresentationID$_video_segment_$Number$.m4s"}; //DASH ์์ฑ ๊ท์น format
const char gscx__dash_audio_seq_format[] = {"$RepresentationID$_audio_segment_$Number$.m4s"}; //DASH ์์ฑ ๊ท์น format
#endif
const char gscx__dash_ts_index_format[] = {"$RepresentationID$_segment_index.sidx"};
#if 0
const char gscx__dash_ts_format[] = {"$RepresentationID$_segment_$Time$.ts"};
#else
const char gscx__dash_ts_format[] = {"$RepresentationID$_segment_$Number$.ts"};
#endif
const char gscx__dash_yt_video_format[] = {"%a_video_single.m4s"}; //DASH ์์ฑ ๊ท์น format
const char gscx__dash_yt_audio_format[] = {"%a_audio_single.m4s"}; //DASH ์์ฑ ๊ท์น format
const char gscx__mss_video_format[] = {"{bitrate}_video_segment_{start time}.ismv"}; //Smooth Streaming ์์ฑ ๊ท์น format
const char gscx__mss_audio_format[] = {"{bitrate}_audio_segment_{start time}.isma"}; //Smooth Streaming ์์ฑ ๊ท์น format
const char gscx__strm_crossdomain_pattern[] = {".*crossdomain\\.xml(\\?{1}.*)?$"};
const char gscx__strm_clientaccesspolicy_pattern[] = {".*clientaccesspolicy\\.xml(\\?{1}.*)?$"};
/* contents ํจํด
* ์) "/2f696e666f2f7072655f373230702e6d7034/0-0/2f61642f666c7962655f373230702ee6d7023/0-0"
* "/2f696e666f2f7072655f373230702e6d7034/0-0/2f61642f666c7962655f373230702ee6d7023/0-0"
*/
const char gscx__strm_content_pattern[] = {"^/*([a-fA-F0-9]+)/+([0-9])-?([0-9]+)?-?([0-9]*)-?([a-zA-Z0-9]*)/*(.*)$"};
regex_t *gscx__strm_url_preg = NULL;
regex_t *gscx__strm_crossdomain_preg = NULL;
regex_t *gscx__strm_clientaccesspolicy_preg = NULL;
regex_t *gscx__strm_content_preg = NULL;
void * strm_cmaf_malloc(size_t size);
void * strm_cmaf_calloc(size_t size, size_t multiply);
void * strm_cmaf_realloc(void *buf, size_t newsize);
void strm_cmaf_free(void *buf);
void * strm_malloc(size_t size, void *param);
void * strm_calloc(size_t size, size_t multiply, void *param);
void * strm_realloc(void *buf, size_t newsize, void *param);
void strm_free(void *buf, void *param);
void *strm_palloc(size_t size, void **pool, void *param);
void strm_pfree(void *pool, void *param);
int strm_strcmp(const void *k1, const void *k2);
unsigned strm_create_hash(const void *p);
void strm_media_key_val_free(void *key, void *datum);
void strm_builder_key_val_free(void *key, void *datum);
ssize_t strm_source_reader(void *context, void *buf, size_t size, off_t offset, void *param);
ssize_t strm_source_size(void *context, void *param);
source_info_t * strm_create_source_info(nc_request_t *req, int type, char *url, zipperCnt context);
source_info_t * strm_find_source_info(streaming_t *param, zipperCnt context);
void strm_destroy_source_info(nc_request_t *req, source_info_t *source);
int strm_set_io_memfunc(zipper_io_handle *ioh);
void strm_destroy_io_handle(zipper_io_handle *ioh);
media_info_t * strm_create_media(nc_request_t *req, int type, char *url);
int strm_set_media(nc_request_t *req, media_info_t *media, int type, char *url, zipperCnt ctx, struct nc_stat *objstat);
int strm_set_objstat(nc_request_t *req);
int strm_write_func(unsigned char *block, size_t size, void *param);
static void strm_make_x_play_durations_header(streaming_t *streaming);
zipperBldr strm_live_create_preroll_bldr_callback(uint32_t dur, void *param);
void strm_live_expire_preroll_bldr_callback(zipperBldr bldr, void *param);
int strm_live_create_context(nc_request_t *req);
void * strm_cmaf_monitor(void *d);
void strm_destroy_builder(builder_info_t * builder);
void strm_expire_medialist(nc_request_t *req);
int strm_regex_compile();
int strm_regex_free();
mode_t strm_protocol_parser(nc_request_t *req, char *protocol);
int strm_content_parser(nc_request_t *req, char *contents, void *mpool);
char *strm_smil_read(nc_request_t *req, adaptive_info_t *adaptive_info, void *tmp_mpool);
int strm_media_type_parser(nc_request_t *req, char *virtualfile);
int strm_get_track_id(nc_request_t *req);
int strm_make_path(nc_request_t *req, int type, const char * url, char *buf, int length);
void *strm_file_open(nc_request_t *req, int type, const char * url, struct nc_stat *objstat, mode_t mode);
void strm_file_close(nc_request_t *req, void * file);
ssize_t strm_file_read(nc_request_t *req, void * file, void *buf, off_t offset, size_t toread);
int strm_file_errno(nc_request_t *req);
int strm_make_enc_key(nc_request_t *req, char *buf);
int strm_destroy_streaming(nc_request_t *req);
int
strm_init()
{
int rv_uint = 0;
// sm_init(); /* module ๊ตฌ์กฐ๋ก ๋ณ๊ฒฝ ๋จ์ ๋ฐ๋ผ ์ฌ๊ธฐ์ ์คํํ ํ์๊ฐ ์๋ค. */
/* dash ํจํด ๋งค์นญ์ฉ compiled form ์์ฑ */
if (strm_regex_compile() == 0) {
return 0;
}
smil_parser_init();
gscx__io_buffer_size = gscx__config->io_buffer_size * 1024;
scx_lru_init();
return 1;
}
void
strm_deinit()
{
// sm_deinit(); /* module ๊ตฌ์กฐ๋ก ๋ณ๊ฒฝ ๋จ์ ๋ฐ๋ผ ์ฌ๊ธฐ์ ์คํํ ํ์๊ฐ ์๋ค. */
/* ํจํด ๋งค์นญ์ฉ compiled form ์ญ์ */
strm_regex_free();
smil_parser_deinit();
scx_lru_destroy();
}
void *
strm_cmaf_malloc(size_t size)
{
return SCX_MALLOC(size);
}
void *
strm_cmaf_calloc(size_t size, size_t multiply)
{
return SCX_CALLOC(size, multiply);
}
void *
strm_cmaf_realloc(void *buf, size_t newsize)
{
return SCX_REALLOC(buf, newsize);
}
void
strm_cmaf_free(void *buf)
{
SCX_FREE(buf);
}
void *
strm_malloc(size_t size, void *param)
{
return SCX_MALLOC(size);
}
void *
strm_calloc(size_t size, size_t multiply, void *param)
{
return SCX_CALLOC(size, multiply);
}
void *
strm_realloc(void *buf, size_t newsize, void *param)
{
return SCX_REALLOC(buf, newsize);
}
void
strm_free(void *buf, void *param)
{
SCX_FREE(buf);
}
void *
strm_palloc(size_t size, void **pool, void *param)
{
mem_pool_t *ptp;
if((*pool) == NULL) {
ptp = mp_create(65536);
ASSERT(ptp);
*pool = (void *)ptp;
}
else {
ptp = (mem_pool_t *)(*pool);
}
return (void *)mp_alloc(ptp, size);
}
void
strm_pfree(void *pool, void *param)
{
if (pool) mp_free(pool);
}
/*
* context๊ฐ NULL์ธ ๊ฒฝ์ฐ๋ zipper_create_media_context์์ readfp์ ์ฝ๋ฐฑ์ผ๋ก ํธ์ถ๋๋ ๊ฒฝ์ฐ์ด๊ณ
* context์ ๊ฐ์ด ๋ค์ด ์๋ ๊ฒฝ์ฐ๋ zipper_build()์์ ์ฝ๋ฐฑ์ผ๋ก ํธ์ถ ๋๋ ๊ฒฝ์ฐ์ด๋ค.
* zipper_create_media_context()์์ ํธ์ถ ๋๋ ๊ฒฝ์ฐ๋ param์ source_info_t *๊ฐ ๋ค์ด ์๊ณ
* zipper_build()์์ ํธ์ถ ๋๋ ๊ฒฝ์ฐ์๋ builder_info_t * ๊ฐ ๋ค์ด ์๋ค
* ์ด ํจ์ ๋ด์์ source info๋ฅผ ์์ฑํ๋ ๋ฐฉํฅ์ผ๋ก ๊ฐ๋ฐ ํด์ผ ํจ.
*/
ssize_t
strm_source_reader(void *context, void *buf, size_t size, off_t offset, void *param)
{
source_info_t *source = NULL;
nc_request_t *req = NULL;
builder_info_t *builder = NULL;
streaming_t *streaming = NULL;
ssize_t copied = 0;
size_t toread;
int using_j_enable = 0;
if (__thread_jmp_working == 1) {
/* zipper library๋ฅผ ๋ฒ์ด๋์ SIGSEGV ๋ฐ์ํ๋ ๊ฒฝ์ฐ๋ ์ฃฝ์ด์ผ ํ๋ฏ๋ก gscx__j_enable์ 0์ผ๋ก ๋ฐ๊พผ๋ค. */
using_j_enable = 1;
__thread_jmp_working = 0;
}
streaming = (streaming_t *) param;
req = (nc_request_t *)streaming->req;
if(context == NULL) {
/*
* context๊ฐ NULL์ธ ๊ฒฝ์ฐ๋ zipper_create_media_context()์์ readfp์ ์ฝ๋ฐฑ์ผ๋ก ํธ์ถ๋๋ ๊ฒฝ์ฐ์ด๋ค.
* ์ด ๊ฒฝ์ฐ๋ source_info_t ๊ฐ ํ๊ฐ๋ง ๋ค์ด ์๊ธฐ ๋๋ฌธ์ ๊ฒ์์ด ํ์ ์๋ค.
*/
source = streaming->source;
//req = (nc_request_t *)source->req;
}
else {
/*
* context์ ๊ฐ์ด ๋ค์ด ์๋ ๊ฒฝ์ฐ๋ zipper_build()์์ ์ฝ๋ฐฑ์ผ๋ก ํธ์ถ ๋๋ ๊ฒฝ์ฐ์ด๋ค.
* ์ด ๊ฒฝ์ฐ๋ param์ builder_info_t * ๊ฐ ๋ค์ด ์๋ค.
* builder_info_t๋ด์์ context์ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋
*/
source = strm_find_source_info(streaming, (zipperCnt)context);
}
if (source == NULL) {
scx_error_log(req, "Failed to find source info.\n");
copied = 0;
goto strm_source_reader_end;
}
if(offset <= source->objstat.st_size) {
//offset + size๊ฐ ํ์ผ์ ํฌ๊ธฐ ๋ณด๋ค ํฐ ๊ฒฝ์ฐ ํ์ผ ํฌ๊ธฐ ๋ด์์๋ง ์ฝ๋๋ค;
toread = min(size, source->objstat.st_size - offset);
copied = strm_file_read(req, source->file, buf, offset, toread);
}
else {
scx_error_log(req, "Invalid request offset. file(%lld),offset(%lld)\n",source->objstat.st_size, offset);
copied = 0;
goto strm_source_reader_end;
}
// printf("strm_source_reader offset = %d, size = %d, calsize = %d, copied = %d, toread = %d\n",
// offset,size, offset+size, copied, toread);
strm_source_reader_end:
if(using_j_enable == 1) {
__thread_jmp_working = 1;
}
return copied;
}
/*
* ์๋ณธ ๋์์์ ํฌ๊ธฐ๋ฅผ ๋ฆฌํดํ๋ค.
*/
ssize_t
strm_source_size(void * context, void *param)
{
source_info_t *source = NULL;
nc_request_t *req = NULL;
builder_info_t *builder = NULL;
streaming_t *streaming = NULL;
ssize_t src_size = 0;
double ts, te;
size_t toread;
int using_j_enable = 0;
if (__thread_jmp_working == 1) {
/* zipper library๋ฅผ ๋ฒ์ด๋์ SIGSEGV ๋ฐ์ํ๋ ๊ฒฝ์ฐ๋ ์ฃฝ์ด์ผ ํ๋ฏ๋ก gscx__j_enable์ 0์ผ๋ก ๋ฐ๊พผ๋ค. */
using_j_enable = 1;
__thread_jmp_working = 0;
}
if(context == NULL) {
/*
* context๊ฐ NULL์ธ ๊ฒฝ์ฐ๋ zipper_create_media_context()์์ readfp์ ์ฝ๋ฐฑ์ผ๋ก ํธ์ถ๋๋ ๊ฒฝ์ฐ์ด๋ค.
* ์ด ๊ฒฝ์ฐ๋ source_info_t ๊ฐ ํ๊ฐ๋ง ๋ค์ด ์๊ธฐ ๋๋ฌธ์ ๊ฒ์์ด ํ์ ์๋ค.
*/
streaming = (streaming_t *) param;
source = streaming->source;
}
else {
streaming = (streaming_t *) param;
source = strm_find_source_info(streaming,(zipperCnt) context);
}
if (source == NULL) {
src_size = 0;
}
else {
src_size = source->objstat.st_size;
}
if(using_j_enable == 1) {
__thread_jmp_working = 1;
}
return src_size;
}
/*
* strm_find_source_info()์์ ํธ์ถ ๋๋ ๊ฒฝ์ฐ์๋ context๊ฐ ๋ค์ด์ค๊ณ
* strm_create_media()์์ ํธ์ถ ๋๋ ๊ฒฝ์ฐ์๋ context๊ฐ NULL์ด๋ค.
*/
source_info_t *
strm_create_source_info(nc_request_t *req, int type, char *url, zipperCnt context)
{
streaming_t *streaming = req->streaming;
source_info_t *source = NULL;
source_info_t *head_source = NULL;
source_info_t *prev = NULL;
service_info_t *service = req->service;
void * file = NULL;
int source_alocated = 0;
mode_t mode = O_RDONLY;
mem_pool_t *mpool = NULL;
if (context == NULL || req->streaming->source == NULL) {
/* strm_find_source_info()์์ ์ฒซ๋ฒ์งธ ํธ์ถ์๋
* strm_create_media()์์ ํธ์ถ ๋๋ ๊ฒฝ์ฐ๋ ์ด๊ณณ๋ง ์คํ๋๋ค */
mpool = mp_create(sizeof(mem_pool_t) + 2048);
ASSERT(mpool);
source = (source_info_t *)mp_alloc(mpool,sizeof(source_info_t));
ASSERT(source);
source->mpool = mpool;
if(context != NULL) {
/* strm_find_source_info()์์๋ ํธ์ถ ๋ ๋๋ source ์ ๋ณด๋ฅผ ๋ฐ๋ก ์ ์ฅํ๋ค. */
req->streaming->source = source;
source_alocated = 1;
}
}
else {
/* strm_find_source_info()์์ ๋๋ฒ์งธ ํธ์ถ ๋ ๋ ๋ถํฐ๋ ์ด๊ณณ์ด ์คํ๋๋ค. */
source = head_source = req->streaming->source;
while (source->next) {
source = source->next;
}
source->next = (source_info_t *)mp_alloc(head_source->mpool,sizeof(source_info_t));
ASSERT(source->next);
source = source->next;
source->mpool = NULL;
}
#if 0
/* strm_file_open()์์ ์ค๋ณต ๊ตฌํ ๋์ด ์๋ ๋ถ๋ถ์ */
if(service->origin_hostname) { /* origin ์์ฒญ์ host ํค๋๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒฝ์ฐ */
kv_replace(streaming->options, MHD_HTTP_HEADER_HOST, vs_data(service->origin_hostname), FALSE);
}
#ifndef ZIPPER
/* streaming์ ๊ฒฝ์ฐ์๋ host๊ฐ ๋๋ฉ์ธ์ด ์๋๋ผ ์ง์ ์ ๋ฌ์ ํ ์ ์๋ค. */
else { /* client์ ์์ฒญ์ ํฌํจ๋ host ํค๋๋ฅผ ์ ๋ฌ */
kv_replace(streaming->options, MHD_HTTP_HEADER_HOST, host, FALSE);
}
#endif
#endif
/*
* hls manifest ์์ ๊ธฐ๋ฅ์ ์ ์ธํ๊ณ ๋ ๋ชจ๋ ์๋ณธ ํ์ผ์ range๋ก ์ฝ๋๋ค.
* dash manifest ์์ ๊ธฐ๋ฅ์ด ๊ฐ๋ฐ๋๋ฉด ๊ทธ ๊ฒฝ์ฐ๋ ์ถ๊ฐํด ์ฃผ์ด์ผ ํ๋ค.
*/
if (req->service->hls_modify_manifest_enable == 1) {
mode = O_RDONLY|O_NCX_NORANDOM;
}
file = strm_file_open(req, type, url, &source->objstat, mode);
if (file == NULL) {
scx_error_log(req, "URL[%s] - open error (%d)\n", url, strm_file_errno(req));
req->p1_error = MHD_HTTP_NOT_FOUND;
goto set_origin_info_error;
}
source->file = file;
source->mcontext = context;
source->next = NULL;
return source;
set_origin_info_error:
if (file) {
strm_file_close(req, file);
}
if (mpool) mp_free(mpool);
if (source_alocated) req->streaming->source = NULL;
return NULL;
}
/*
* ์๋ณธ ํ์ผ์ ์ด์ด์ ํ์ผ์ ์ ๋ณด๋ฅผ objstat์ ์ ์ฅํ๋ค.
*/
int
strm_get_media_attr(nc_request_t *req, int type, const char * url, struct nc_stat *objstat)
{
streaming_t *streaming = req->streaming;
service_info_t *service = req->service;
void *file = NULL;
int ret = 0;
#if 0
/* strm_file_open()์์ ์ค๋ณต ๊ตฌํ ๋์ด ์๋ ๋ถ๋ถ์ */
if(service->origin_hostname) { /* origin ์์ฒญ์ host ํค๋๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒฝ์ฐ */
kv_replace(streaming->options, MHD_HTTP_HEADER_HOST, vs_data(service->origin_hostname), FALSE);
}
#ifndef ZIPPER
/* streaming์ ๊ฒฝ์ฐ์๋ host๊ฐ ๋๋ฉ์ธ์ด ์๋๋ผ ์ง์ ์ ๋ฌ์ ํ ์ ์๋ค. */
else { /* client์ ์์ฒญ์ ํฌํจ๋ host ํค๋๋ฅผ ์ ๋ฌ */
kv_replace(streaming->options, MHD_HTTP_HEADER_HOST, host, FALSE);
}
#endif
#endif
/* ์๋ณธ ํ์ผ์ range๋ก ์ฝ๋๋ค. */
file = strm_file_open(req, type, url, objstat, O_RDONLY);
if (file == NULL) {
scx_error_log(req, "Virtual host(%s), URL[%s] - open error (%d)\n", vs_data(service->name), url, strm_file_errno(req));
req->p1_error = MHD_HTTP_NOT_FOUND;
goto end_get_media_attr;
}
strm_file_close(req, file);
ret = 1;
end_get_media_attr:
return ret;
}
source_info_t *
strm_find_source_info(streaming_t *param, zipperCnt context)
{
builder_info_t *builder = param->builder;
source_info_t *source = param->source;
media_info_t *media = NULL;
nc_request_t *req = (nc_request_t *)param->req;
media_info_list_t * media_list = builder->media_list;
while (source) {
/* source info๊ฐ ๋ง๋ค์ด์ง๊ฒ ์์ผ๋ฉด ํด๋น source info๋ฅผ ๋ฆฌํดํ๋ค. */
if (source->mcontext == context) {
return source;
}
source = source->next;
}
/* source info๊ฐ ์๋ ๊ฒฝ์ฐ ์๋ก ์์ฑํ๋ค. */
/* ๋จผ์ context์ ํด๋นํ๋ media info๋ฅผ ์ฐพ๋๋ค. */
while (media_list) {
if (media_list->media->mcontext == context) {
media = media_list->media;
break;
}
media_list = media_list->next;
}
if (media == NULL)
return NULL;
//ASSERT(media);
source = strm_create_source_info(param->req, media->type, media->url, context);
if (source == NULL) {
return NULL;
}
/* ๋์ค์ ํ์ผ์ด ๋ณ๊ฒฝ ๋๋ ๊ฒฝ์ฐ๊ฐ ์์ด์ source๋ฅผ ๋ง๋ ํ ๋ค์ ํ์ผ ์ ๋ณด๋ฅผ metadata์ ์ ๋ณด์ ๋น๊ตํ๋ค. */
if (media->mtime != source->objstat.st_mtime) {
media->available = 0;
TRACE((T_INFO, "[%llu] media info mtime changed. cache(%ld), changed(%ld), path(%s)\n", req->id, media->mtime, source->objstat.st_mtime, media->url));
scx_error_log(req, "media info mtime changed. cache(%ld), changed(%ld), path(%s)\n", media->mtime, source->objstat.st_mtime, media->url);
return NULL;
}
else if (media->msize != source->objstat.st_size) {
media->available = 0;
TRACE((T_INFO, "[%llu] media info size changed. cache(%lld), changed(%lld), path(%s)\n", req->id, media->msize, source->objstat.st_size, media->url));
scx_error_log(req, "media info size changed. cache(%ld), changed(%ld), path(%s)\n", media->msize, source->objstat.st_size, media->url);
return NULL;
}
else if(strncmp(media->st_devid, source->objstat.st_devid, 128) != 0) {
media->available = 0;
TRACE((T_INFO, "[%llu] media info ETag changed. cache(%s), changed(%s), path(%s)\n", req->id, media->st_devid, source->objstat.st_devid, media->url));
scx_error_log(req, "media info ETag changed. cache(%s), changed(%s), path(%s)\n", media->st_devid, source->objstat.st_devid, media->url);
return NULL;
}
return source;
}
void
strm_destroy_source_info(nc_request_t *req, source_info_t *source)
{
void *file = NULL;
mem_pool_t *mpool = NULL;
source_info_t *cur = source;
while (cur) {
file = cur->file;
if (file) {
strm_file_close(req, file);
cur->file = NULL;
}
cur = cur->next;
}
if (source->mpool) {
mpool = source->mpool;
source->mpool = NULL;
mp_free(mpool);
}
}
/*
* io handle๋ http ์ฐ๊ฒฐ ๋ง๋ค ์๋ก ๋ง๋ค์ด์ผ ํ๋ค.
* zipper_create_media_context()์์ ํธ์ถ ๋๋ ๊ฒฝ์ฐ๋ param์ source_info_t *๊ฐ ๋ค์ด ์๊ณ
* zipper_build()์์ ํธ์ถ ๋๋ ๊ฒฝ์ฐ์๋ builder_info_t * ๊ฐ ๋ค์ด ์๋ค
*/
struct _zipper_io_handle *
strm_set_io_handle(streaming_t *param)
{
zipper_io_handle *ioh = NULL;
ioh = (zipper_io_handle *)SCX_CALLOC(1, sizeof(zipper_io_handle));
ASSERT(ioh);
ioh->reader.sizefp = strm_source_size;
ioh->reader.readfp = strm_source_reader;
ioh->reader.param = param;
if (param != NULL) {
ioh->bufsize = gscx__io_buffer_size;
ioh->readbuf.data = (unsigned char *)SCX_MALLOC(ioh->bufsize);
}
ioh->writer.fp = strm_write_func;
strm_set_io_memfunc(ioh);
return ioh;
}
/*
* zipper_io_handle์ memory ๊ด๋ จ callback function ๋ฑ๋ก
*/
int
strm_set_io_memfunc(zipper_io_handle *ioh)
{
ioh->memfunc.malloc = strm_malloc;
ioh->memfunc.calloc = strm_calloc;
ioh->memfunc.realloc = strm_realloc;
ioh->memfunc.free = strm_free;
ioh->memfunc.pool.alloc = strm_palloc;
ioh->memfunc.pool.free = strm_pfree;
ioh->memfunc.param = NULL;
return 1;
}
void
strm_destroy_io_handle(zipper_io_handle *ioh)
{
streaming_t *streaming = NULL;
if (NULL != ioh->readbuf.data) {
SCX_FREE(ioh->readbuf.data);
}
if (ioh->reader.param) {
streaming = (streaming_t *) ioh->reader.param;
}
SCX_FREE(ioh);
}
/*
* builder๋ฅผ ์์ฑํ๋ ๊ณผ์ ์์ ์ฒ์ media ์ ๋ณด ์ฐธ์กฐํ ๋ ์ฌ์ฉ๋๋ค.
* ์ง์ ๋ host์ url์ด ๋์ผํ media ์ ๋ณด๊ฐ ์๋์ง ํ์ธํ๊ณ
* ์์ผ๋ฉด ์ฐพ์ ์ ๋ณด๋ฅผ ๋ฆฌํดํ๊ณ
* ์๋ ๊ฒฝ์ฐ๋ ์๋ก ์์ฑํํ ๋ฆฌํดํ๋ค.
* ๋ฐฐ์ ์ค๋์ค ํ์ผ์ open ํ๋ ๊ฒฝ์ฐ์๋ zatt์ 1์ ์
๋ ฅํ๋ค.
* type : 0:VOD, 1:๊ด๊ณ , 2:LIVE
*/
media_info_t *
strm_create_media(nc_request_t *req, int type, char *url)
{
/* scx_media_dct๋ก๋ถํฐ ์ ๋ณด๊ฐ ์๋์ง ํ์ธ ํ๊ณ ์์ผ๋ฉด ์๋ก ์์ฑ ํ๋ ๋ถ๋ถ์ด ์์ด์ผ ํจ */
media_info_t *media = NULL;
zipperCnt ctx = NULL;
zipper_io_handle *ioh = NULL;
streaming_t *param = NULL;
int ret = 0;
int gres = 0;
source_info_t *source = NULL;
double ts, te;
struct nc_stat mdc_objstat;
zipperCnt mdc_context = NULL;
int must_update = 0;
// jb = SCX_MALLOC(sizeof(jmp_buf));
/* ์ฌ๊ธฐ์์ key(host+url)๋ฅผ ๋ง๋๋ ๊ณผ์ ํ์ */
while (1) {
printf("strm_create_media() called, url(%s)\n", url);
if (type == O_CONTENT_TYPE_SUBTITLE) {
// ์๋ง์ cache๋ฅผ ํ์ง ์๊ธฐ ๋๋ฌธ์ ์๋ ๋ถ๋ถ๋ค์ skipํ๊ณ source ์์ฑ๋ถํฐ ์งํํ๋ค.
media = strm_allocate_media(req, &gres);
goto next_create_media;
}
/* strm_find_media()์ ํธ์ถํํ ๋ถํฐ commit์ด ์ผ์ด๋ ๋ ๊น์ง๋ ๋์ผ media๋ ์ค์ง ํ ์ธ์
๋ง ์ง์
์ด ๊ฐ๋ฅํ๋ค. */
media = strm_find_media(req, type, url, U_TRUE, U_TRUE, &gres);
if (media == NULL) {
/*
* ์บ์ฑ์ด ๋ ์ํ์์ ์๋ณธ ํ์ผ์ด ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ์ด๊ฑฐ๋
* media cache๊ฐ ๋ชจ๋ ์ฌ์ฉ์ค์ด๋ผ ์๋ก ์ถ๊ฐ๊ฐ ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ
* ๋ฐฐ์ audio ์์ฒญ์ธ ๊ฒฝ์ฐ
* media๋ฅผ glru์์ ํ ๋น ๋ฐ์ง ์๊ณ ์ง์ ์์ฑํ๋ค.
*/
media = strm_allocate_media(req, &gres);
}
if (gres == GLRUR_FOUND ) {
break;
}
/* else if๋ฅผ ์์ด ์ด์ ๋ ์์์ ์บ์ฑ๋์์ง๋ง ์ปจํ
์ธ ์ ๋ฌธ์ ๊ฐ ์์ด strm_reset_media()๊ฐ ํธ์ถ ๋๋ ๊ฒฝ์ฐ๊ฐ ์๊ธฐ ๋๋ฌธ์ */
if (gres == GLRUR_ALLOCATED || gres == GRLUR_RESETTED) {
/* ์ฌ๊ธฐ์ ๋ค์ด์ค๋ ๊ฒฝ์ฐ๋ media๊ฐ ์๋ก ๋ง๋ค์ด ์ก๊ฑฐ๋ ์๋ณธ์ด ๋ณ๊ฒฝ ๋์ด์ ์๋ก ํ ๋น๋ ๊ฒฝ์ฐ์ด๋ค. */
/*
* disk cache๊ฐ ์๋์ง ํ์ธ
* cache๊ฐ ์๋ ๊ฒฝ์ฐ ์ ํจ์ฑ ๊ฒ์ฌํ strm_set_media()๋ง ํธ์ถํ๊ณ mdc_save_cache()๋ ํธ์ถํ๋ฉด ์๋๋ค.
*/
mdc_context = mdc_load_cache(req, &mdc_objstat, url);
if (mdc_context != NULL) {
media_info_t *temp_media = NULL;
zipper_io_handle *temp_ioh = NULL;
time_t timenow = scx_get_cached_time_sec();;
if (mdc_objstat.st_vtime < timenow) {
//temp_media = alloca(sizeof(media_info_t));
temp_media = mp_alloc(req->pool,sizeof(media_info_t));
strncpy(temp_media->st_devid, mdc_objstat.st_devid, 128);
//temp_media->url = (char *)alloca(strlen(url) + 1);
temp_media->url = (char *)mp_alloc(req->pool,strlen(url) + 1);
snprintf(temp_media->url, strlen(url) + 1, "%s", url);
temp_media->vtime = mdc_objstat.st_vtime;
temp_media->mtime = mdc_objstat.st_mtime;
temp_media->msize = mdc_objstat.st_size;
/*
* ์ ํจ ์๊ฐ์ด ์ง๋ ๊ฒฝ์ฐ ์๋ณธ์ ๋ค์ ์ด์ด์ ๊ฐฑ์ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
* ์ด๋ media->st_devid ID๊ฐ ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ๋ ํด๋น media์ available์ 0๋ก ํ์ํ๊ณ
* media๋ฅผ ์ฐพ๋ ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
*/
/* ๋ง๋ฃ ์๊ฐ์ด ์ง๋ ์ปจํ
์ธ ๋ค์ ์๊ฐ์ ๋ค์ ํ์ธ ํ๋ค. */
if (strm_check_media_info(req, type, temp_media) < 0) {
/* disk cache์ ๋ฌธ์ ๊ฐ ์๋ ๊ฒฝ์ฐ zipper contextํด์ ํ ์บ์ ํ์ผ์ ์ง์ด๋ค */
TRACE((T_DAEMON, "[%llu] mdc file check failed(%s).\n", req->id, url));
temp_ioh = strm_set_io_handle(NULL);
if (sigsetjmp(__thread_jmp_buf, 1) == 0) {
__thread_jmp_working = 1;
zipper_free_media_context(temp_ioh,&mdc_context);
__thread_jmp_working = 0;
}
else {
/* zipper ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฌธ์ ๋ก SIGSEGV๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ ์ฌ๊ธฐ๋ ๋์ง ์๊ณ ์ฌ๊ธฐ๊ฐ ํธ์ถ ๋๋ค. */
TRACE((T_ERROR, "zipper_free_media_context() SIGSEGV occured. %s:%d\n", __FILE__, __LINE__));
}
strm_destroy_io_handle(temp_ioh);
mdc_disable_cache_file(req, url);
goto next_create_media;
}
else {
if (temp_media->vtime != mdc_objstat.st_vtime) {
/* TTL์ด ๊ฐฑ์ ๋ ๊ฒฝ์ฐ */
mdc_objstat.st_vtime = temp_media->vtime;
/* disk cache์ TTL์ ์
๋ฐ์ดํธ ํ๋ค. */
must_update = 1;
}
}
}
/* ๋ง์ง๋ง์ strm_set_media()๋ฅผ ํธ์ถํด ์ฃผ์ด์ผ ํจ */
strm_set_media(req, media, type, url, mdc_context, &mdc_objstat);
if (must_update) {
/* TTL์ด ๊ฐฑ์ ๋ ๊ฒฝ์ฐ ๊ธฐ์กด ์บ์ ํ์ผ์ TTL์ ๊ฐฑ์ ํ๋ค. */
mdc_save_cache(req, media, 1);
}
goto create_media_end;
}
next_create_media:
if( gres == GRLUR_RESETTED && (media->etime + req->service->media_negative_ttl) >= scx_get_cached_time_sec() ) {
/*
* ์ปจํ
์ธ ์ ๋ฌธ์ ๊ฐ ์์ด์ GRLUR_RESETTED ๋ ์ํ์์ ์ปจํ
์ธ ์๋ฌ๊ฐ ๋ฐ์ํ ์๊ฐ์ด ํ์ฌ์ ๋น๊ตํด์ 1์ด ์ด์ ์ฐจ์ด๊ฐ ๋์ง ์๋ ๊ฒฝ์ฐ๋ ๋ฏธ๋์ด๋ฅผ ๋ค์ ๊ฒ์ฌ ํ์ง ์๊ณ ์๋ฌ ์ฒ๋ฆฌ ํ๋ค.
* ๋ฌธ์ ๊ฐ ์๋ ์ปจํ
์ธ ์ ๋์์ ์์ฒญ์ด ๋ค์ด์์ ์๋ฒ ์๋ต์ด ์ง์ฐ๋๋ ํ์์ ํํผํ๊ธฐ ์ํด ์ด ๋ฃจํด์ ์ถ๊ฐ
* ๊ด๋ จ ์ผ๊ฐ : https://jarvis.solbox.com/redmine/issues/33500
*/
media->available = 0;
scx_error_log(req, "Request repeated to failed content(%s)\n", url);
break;
}
source = strm_create_source_info(req, type, url, NULL);
if (source == NULL) {
//goto create_media_error;
media->available = 0;
media->etime = scx_get_cached_time_sec(); //๋ง์ง๋ง ์๋ฌ ๋ฐ์์๊ฐ์ ๊ธฐ๋กํ๋ค.
break;
}
if (!source->objstat.st_sizedecled) {
/* ์ด๋ถ๋ถ์ ์๋ฌ ์ฒ๋ฆฌ ํ ์ง๋ ๊ณ ๋ฏผ ํด๋ด์ผ ํจ */
TRACE((T_WARN, "[%llu] '%s' is not rangable\n", req->id, url));
//goto create_media_error;
media->available = 0;
media->etime = scx_get_cached_time_sec(); //๋ง์ง๋ง ์๋ฌ ๋ฐ์์๊ฐ์ ๊ธฐ๋กํ๋ค.
break;
}
/* ์ฌ๊ธฐ์ ์ฌ์ฉ๋๋ param์ streaming_t์ ์ฐ์ง๋ง ํ์ฑ์ ์ํด์ ์ด ํจ์ ๋ด์์๋ง ์ฌ์ฉ ๋๊ธฐ ๋๋ฌธ์
* memory pool ๋ฐฉ์์ ์ฌ์ฉํ์ง ์๊ณ ๋จ์ํ malloc๋งํ๋ค.
* ์ดํจ์๋ฅผ ๋ฒ์ด๋๊ธฐ ์ ์ ๋ฐ๋์ ์ญ์ ๋์ด์ผ ํ๋ค. */
param = (streaming_t *)SCX_CALLOC(1, sizeof(streaming_t));
memset(param, 0, sizeof(streaming_t));
param->req = req;
param->source = source;
ioh = strm_set_io_handle(param);
if (!ioh) {
//goto create_media_error;
media->available = 0;
break;
}
ts = sx_get_time();
if (sigsetjmp(__thread_jmp_buf, 1) == 0) {
__thread_jmp_working = 1;
ret = zipper_create_media_context(ioh,&ctx);
__thread_jmp_working = 0;
}
else {
/* zipper ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฌธ์ ๋ก SIGSEGV๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ ์ฌ๊ธฐ๋ ๋์ง ์๊ณ ์ฌ๊ธฐ๊ฐ ํธ์ถ ๋๋ค. */
ret = zipper_err_internal;
TRACE((T_ERROR, "[%llu] zipper_create_media_context() SIGSEGV occured.(%s), %s:%d\n", req->id, url, __FILE__, __LINE__));
}
#ifdef DEBUG
printf("zipper_create_media_context() ret(%d), url(%s)\n", ret, url);
#endif
te = sx_get_time();
req->t_zipper_build += (te - ts);
if (req->file_errno) {
/*
* zipper_create_media_context() ๊ณผ์ ์์ file_errno๊ฐ ์
ํ
๋๋ ๊ฒฝ์ฐ๋ ์ธ๋ฑ์ค๋ฅผ ์์ฑํ๋ ์ค์ ์ค๋ฆฌ์ง ์ฅ์ ๊ฐ ๋ฐ์ํ๊ฑธ๋ก ๋ณด๊ณ ์ง์ IO error๋ฅผ ์ค์ ํ๋ค.
* zipper_create_media_context() ๋์ ์ค์ strm_source_reader()๊ฐ ํธ์ถ ๋๋๋ฐ strm_source_reader()์์ error๋ฅผ ๋ฆฌํดํด๋
* mp3์ ๊ฒฝ์ฐ๋ media context๊ฐ ์๋ฌ ์์ด ์์ฑ๋๊ธฐ ๋๋ฌธ์ file_errno๋ฅผ ์ด์ฉํด์ ๊ฐ์ ๋ก media๋ฅผ ์๋ฌ๋ก ์ฒ๋ฆฌ ํ๋ค.
*/
ret = zipper_err_io_handle;
}
if (zipper_err_success == ret) {
//์ฑ๊ณต ํ์ ๊ฒฝ์ฐ
//๋ฆฌํด๊ฐ์ media์ ๋ค์ ๋ฃ๋ ์ด์ ๋ ๋ค๋ฅธ ์ธ์
์์ ๋์์ ๊ฐ์ ์ปจํ
์ธ ๋ฅผ ์ธ๋ฑ์ฑ ํ์ ๊ฒฝ์ฐ
//๋์ค์ ์ธ๋ฑ์ฑ ๋ ์ ๋ณด๋ฅผ hls_add_media2list()์์ ์ญ์ ํ๊ณ ์ด์ ์ ์ธ๋ฑ์ฑ ์ ๋ณด์ ํฌ์ธํฐ๋ฅผ ๋ฆฌํดํด ์ฃผ๊ธฐ๋๋ฌธ์ด๋ค.
strm_set_media(req, media, type, url, ctx, &source->objstat);
ATOMIC_ADD(gscx__media_context_create_interval_cnt, 1);
ATOMIC_ADD(gscx__media_context_create_interval_size, media->mcontext_size);
/* ๋์คํฌ ์บ์์ ๊ธฐ๋ก, ์๋ง์ cache์ ๊ธฐ๋กํ์ง ์๋๋ค. */
if (type != O_CONTENT_TYPE_SUBTITLE) {
mdc_save_cache(req, media, 0);
}
}
else {
media->available = 0;
media->etime = scx_get_cached_time_sec(); //๋ง์ง๋ง ์๋ฌ ๋ฐ์์๊ฐ์ ๊ธฐ๋กํ๋ค.
scx_error_log(req, "Failed to create media context.(%s), reason(%s)\n", url, zipper_err_msg(ret));
// if(media->contex) free_mp4tohls_context(&media->context);
//goto create_media_error;
break;
}
}
break;
}
create_media_end:
// ASSERT(media->available); /*media->available๊ฐ 0์ธ ๊ฒฝ์ฐ ์ฌ๊ธฐ๋ก ๋ค์ด์ค๋ฉด ์๋๋ค. */
// strm_use_media(media);
//create_media_error:
if (media != NULL) {
strm_commit_media(req, media);
// if(gres == GLRUR_ALLOCATED) {
// }
if (media->available == 0) { /* ์๋ณธ media์ ๋ฌธ์ ๊ฐ ์๋ ๊ฒฝ์ฐ */
strm_release_media(media);
media = NULL;
scx_error_log(req, "Find media info not available(%s)\n", url);
}
else {
media->etime = 0; // media๊ฐ ์ ์์ธ ๊ฒฝ์ฐ ์ด ์๊ฐ์ reset ํ๋ค.
// strm_setuse_media(media);
}
}