@@ -5,24 +5,24 @@ import { proxyPathBuilder } from '../../shared/utils';
5
5
6
6
interface DASHManifestUtils {
7
7
mergeMap : (
8
- segmentListSize : number ,
9
- configsMap : IndexedCorruptorConfigMap
8
+ segmentListSize : number ,
9
+ configsMap : IndexedCorruptorConfigMap
10
10
) => CorruptorConfigMap ;
11
11
}
12
12
13
13
export interface DASHManifestTools {
14
14
createProxyDASHManifest : (
15
- dashManifestText : string ,
16
- originalUrlQuery : URLSearchParams
15
+ dashManifestText : string ,
16
+ originalUrlQuery : URLSearchParams
17
17
) => Manifest ; // look def again
18
18
utils : DASHManifestUtils ;
19
19
}
20
20
21
21
export default function ( ) : DASHManifestTools {
22
22
const utils = {
23
23
mergeMap (
24
- targetSegmentIndex : number ,
25
- configsMap : IndexedCorruptorConfigMap
24
+ targetSegmentIndex : number ,
25
+ configsMap : IndexedCorruptorConfigMap
26
26
) : CorruptorConfigMap {
27
27
const outputMap = new Map ( ) ;
28
28
const d = configsMap . get ( '*' ) ;
@@ -51,8 +51,8 @@ export default function (): DASHManifestTools {
51
51
return {
52
52
utils,
53
53
createProxyDASHManifest (
54
- dashManifestText : string ,
55
- originalUrlQuery : URLSearchParams
54
+ dashManifestText : string ,
55
+ originalUrlQuery : URLSearchParams
56
56
) : string {
57
57
const parser = new xml2js . Parser ( ) ;
58
58
const builder = new xml2js . Builder ( ) ;
@@ -65,77 +65,30 @@ export default function (): DASHManifestTools {
65
65
let baseUrl ;
66
66
if ( DASH_JSON . MPD . BaseURL ) {
67
67
// There should only ever be one baseurl according to schema
68
- baseUrl = DASH_JSON . MPD . BaseURL [ 0 ] ;
68
+ baseUrl = DASH_JSON . MPD . BaseURL [ 0 ] . match ( / ^ h t t p / )
69
+ ? DASH_JSON . MPD . BaseURL [ 0 ]
70
+ : new URL ( DASH_JSON . MPD . BaseURL [ 0 ] , originalUrlQuery . get ( 'url' ) ) . href ;
69
71
// Remove base url from manifest since we are using relative paths for proxy
70
72
DASH_JSON . MPD . BaseURL = [ ] ;
71
- }
73
+ } else baseUrl = originalUrlQuery . get ( 'url' ) ;
72
74
73
75
DASH_JSON . MPD . Period . map ( ( period ) => {
74
76
period . AdaptationSet . map ( ( adaptationSet ) => {
75
- if ( adaptationSet . SegmentTemplate ) {
76
- // There should only be one segment template with this format
77
- const segmentTemplate = adaptationSet . SegmentTemplate [ 0 ] ;
78
-
79
- // Media attr
80
- const mediaUrl = segmentTemplate . $ . media ;
81
- // Clone params to avoid mutating input argument
82
- const urlQuery = new URLSearchParams ( originalUrlQuery ) ;
83
-
84
- segmentTemplate . $ . media = proxyPathBuilder (
85
- mediaUrl . match ( / ^ h t t p / ) ? mediaUrl : baseUrl + mediaUrl ,
86
- urlQuery ,
87
- 'proxy-segment/segment_$Number$_$RepresentationID$_$Bandwidth$'
77
+ if ( adaptationSet . SegmentTemplate )
78
+ forgeSegment (
79
+ baseUrl ,
80
+ adaptationSet . SegmentTemplate ,
81
+ originalUrlQuery
88
82
) ;
89
- // Initialization attr.
90
- const initUrl = segmentTemplate . $ . initialization ;
91
- if ( ! initUrl . match ( / ^ h t t p / ) ) {
92
- try {
93
- // Use original query url if baseUrl is undefined, combine if relative, or use just baseUrl if its absolute
94
- if ( ! baseUrl ) {
95
- baseUrl = originalUrlQuery . get ( 'url' ) ;
96
- } else if ( ! baseUrl . match ( / ^ h t t p / ) ) {
97
- baseUrl = new URL ( baseUrl , originalUrlQuery . get ( 'url' ) ) . href ;
98
- }
99
- const absoluteInitUrl = new URL ( initUrl , baseUrl ) . href ;
100
- segmentTemplate . $ . initialization = absoluteInitUrl ;
101
- } catch ( e ) {
102
- throw new Error ( e ) ;
103
- }
104
- }
105
- } else {
106
- // Uses segment ids
107
- adaptationSet . Representation . map ( ( representation ) => {
108
- if ( representation . SegmentTemplate ) {
109
- representation . SegmentTemplate . map ( ( segmentTemplate ) => {
110
- // Media attr.
111
- const mediaUrl = segmentTemplate . $ . media ;
112
- // Clone params to avoid mutating input argument
113
- const urlQuery = new URLSearchParams ( originalUrlQuery ) ;
114
- if ( representation . $ . bandwidth ) {
115
- urlQuery . set ( 'bitrate' , representation . $ . bandwidth ) ;
116
- }
117
-
118
- segmentTemplate . $ . media = proxyPathBuilder (
119
- mediaUrl ,
120
- urlQuery ,
121
- 'proxy-segment/segment_$Number$.mp4'
122
- ) ;
123
- // Initialization attr.
124
- const masterDashUrl = originalUrlQuery . get ( 'url' ) ;
125
- const initUrl = segmentTemplate . $ . initialization ;
126
- if ( ! initUrl . match ( / ^ h t t p / ) ) {
127
- try {
128
- const absoluteInitUrl = new URL ( initUrl , masterDashUrl )
129
- . href ;
130
- segmentTemplate . $ . initialization = absoluteInitUrl ;
131
- } catch ( e ) {
132
- throw new Error ( e ) ;
133
- }
134
- }
135
- } ) ;
136
- }
137
- } ) ;
138
- }
83
+ adaptationSet . Representation . map ( ( representation ) => {
84
+ if ( representation . SegmentTemplate )
85
+ forgeSegment (
86
+ baseUrl ,
87
+ representation . SegmentTemplate ,
88
+ originalUrlQuery ,
89
+ representation
90
+ ) ;
91
+ } ) ;
139
92
} ) ;
140
93
} ) ;
141
94
@@ -145,3 +98,34 @@ export default function (): DASHManifestTools {
145
98
}
146
99
} ;
147
100
}
101
+
102
+ function forgeSegment ( baseUrl , segment , originalUrlQuery , representation ?) {
103
+ if ( segment ) {
104
+ segment . map ( ( segmentTemplate ) => {
105
+ // Media attr.
106
+ const mediaUrl = segmentTemplate . $ . media ;
107
+
108
+ // Clone params to avoid mutating input argument
109
+ const urlQuery = new URLSearchParams ( originalUrlQuery ) ;
110
+ if ( representation ?. $ ?. bandwidth )
111
+ urlQuery . set ( 'bitrate' , representation . $ . bandwidth ) ;
112
+
113
+ segmentTemplate . $ . media = decodeURIComponent (
114
+ proxyPathBuilder (
115
+ mediaUrl . match ( / ^ h t t p / ) ? mediaUrl : new URL ( mediaUrl , baseUrl ) . href ,
116
+ urlQuery ,
117
+ representation
118
+ ? 'proxy-segment/segment_$Number$.mp4'
119
+ : 'proxy-segment/segment_$Number$_$RepresentationID$_$Bandwidth$'
120
+ )
121
+ ) ;
122
+
123
+ // Initialization attr.
124
+ const initUrl = segmentTemplate . $ . initialization ;
125
+ if ( ! initUrl ?. match ( / ^ h t t p / ) ) {
126
+ const absoluteInitUrl = new URL ( initUrl , baseUrl ) . href ;
127
+ segmentTemplate . $ . initialization = absoluteInitUrl ;
128
+ }
129
+ } ) ;
130
+ }
131
+ }
0 commit comments