-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteering_server.go
More file actions
422 lines (344 loc) · 14 KB
/
Copy pathsteering_server.go
File metadata and controls
422 lines (344 loc) · 14 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
package main
import (
"fmt"
"os"
"net/http"
"encoding/xml"
"encoding/json"
"strconv"
"io/ioutil"
"strings"
"github.com/google/uuid"
)
type dashPeriodXml struct {
XMLName xml.Name `xml:"Period"`
Value string `xml:",innerxml"`
}
type ContentSteeringXml struct {
XMLName xml.Name `xml:"ContentSteering"`
Value string `xml:",innerxml"`
DefaultServiceLocation string `xml:"defaultServiceLocation,attr,omitempty"`
QueryBeforeStart string `xml:"queryBeforeStart,attr,omitempty"`
ProxyServerURL string `xml:"proxyServerURL,attr,omitempty"`
}
type BaseURLXml struct {
XMLName xml.Name `xml:"BaseURL"`
Value string `xml:",innerxml"`
ServiceLocation string `xml:"serviceLocation,attr"`
}
type mpdXml struct {
XMLName xml.Name `xml:"MPD"`
MediaPresentationDuration string `xml:"mediaPresentationDuration,attr"`
MinBufferTime string `xml:"minBufferTime,attr"`
Profiles string `xml:"profiles,attr"`
Mpd_type string `xml:"type,attr"`
Xmlns string `xml:"xmlns,attr,omitempty"`
Xmlns_xsi string `xml:"xmlns:xsi,attr,omitempty"`
Xsi_schemaLocation string `xml:"xsi:schemaLocation,attr,omitempty"`
BaseURLs []BaseURLXml `xml:"BaseURL"`
ContentSteering ContentSteeringXml `xml:"ContentSteering"`
Periods []dashPeriodXml `xml:"Period"`
}
// MPEG-DASH Content Steering spec: https://dashif.org/docs/DASH-IF-CTS-00XX-Content-Steering-Community-Review.pdf
// The following definition can be found in Table 6.3.1 in the spec.
type dcsmSpec struct {
VERSION int `json:"VERSION"`
TTL int `json:"TTL"`
RELOAD_URI string `json:"RELOAD-URI"`
SERVICE_LOCATION_PRIORITY []string `json:"SERVICE-LOCATION-PRIORITY"`
}
type serviceLocationEntrySpec struct {
ServiceLocationId string `json:"serviceLocationId"`
ServiceLocationUri string `json:"serviceLocationUri"`
}
type contentSteeringConfigSpec struct {
TTL int `json:"TTL"`
ReloadUri string `json:"RELOAD_URI"`
ServiceLocations []serviceLocationEntrySpec `json:"serviceLocations"`
}
var dashMpdFileExtension = ".mpd"
var dashContentSteeringManifestFileExtension = ".dcsm"
var content_steering_config_endpoint = "content_steering_config"
var remoteBaseUrl = "https://example.com/"
var server_ip = "localhost"
var server_port = "2210"
var server_addr = server_ip + ":" + server_port
const session_id_query_param = "sessionId"
const dash_pathway_query_param = "_DASH_pathway"
const dash_throughput_query_param = "_DASH_throughput"
var dcsm_ttl = 10
const DCSM_VERSION = 1
var content_steering_server_url = "http://" + server_addr + "/dash.dcsm"
var serviceLocationMap []serviceLocationEntrySpec
func main() {
if len(os.Args) > 1 {
remoteBaseUrl = os.Args[1]
}
fmt.Println("remoteBaseUrl: ", remoteBaseUrl)
http.HandleFunc("/", main_steering_server_handler)
fmt.Println("Steering server listening on: ", server_addr)
http.ListenAndServe(server_addr, nil)
}
func resolveRemoteUrl(objUrl string) string {
return (remoteBaseUrl + objUrl)
}
func downloadRemoteMpd(remoteMpdUrl string) []byte {
resp, err := http.Get(remoteMpdUrl)
if err != nil {
fmt.Println("Error: Failed to download: ", remoteMpdUrl)
return nil
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error: Failed to read response body")
return nil
}
//responseBodyString := string(bodyBytes)
return bodyBytes
}
func getBaseUrls(base_urls *[]serviceLocationEntrySpec) {
*base_urls = serviceLocationMap
}
func addContentSteeringInfo(contentSteering *ContentSteeringXml,
steering_server_url string,
defaultServiceLocation string) {
contentSteering.Value = steering_server_url
contentSteering.DefaultServiceLocation = defaultServiceLocation
contentSteering.QueryBeforeStart = "true"
}
func addContentSteeringInfoToMpd(mpdBytes []byte) []byte {
mpd_xml := mpdXml{}
err := xml.Unmarshal(mpdBytes, &mpd_xml)
if err != nil {
fmt.Println("Error: Failed to parse MPD")
return nil
}
//fmt.Println("minBufferTime: ", mpd_xml.MinBufferTime)
var base_urls []serviceLocationEntrySpec
getBaseUrls(&base_urls)
if len(base_urls) == 0 {
fmt.Println("Error: Failed to generate MPD. Error: no BaseUrl found")
return nil
}
// TODO: Need to query cdn switcher for default service location
default_service_loc := base_urls[0].ServiceLocationId
for _, sl_entry := range base_urls {
newBaseUrl := BaseURLXml{ServiceLocation: sl_entry.ServiceLocationId, Value: sl_entry.ServiceLocationUri}
mpd_xml.BaseURLs = append(mpd_xml.BaseURLs, newBaseUrl)
}
// Add ContentSteering element
addContentSteeringInfo(&mpd_xml.ContentSteering,
content_steering_server_url,
default_service_loc)
output, err := xml.MarshalIndent(mpd_xml, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Println("New MPD: .............................\n")
fmt.Printf("%s\n\n", output)
return output
}
// TODO: we need to make use of the query params contained in the
// DCSM request url (e.g., sessionId, _DASH_pathway and _DASH_throughput)
// when determining the service location priority.
func generateDcsm(sessionId_query_param string,
pathway_query_param string,
throughput_query_param string,
r *http.Request) []byte {
reloadUri := content_steering_server_url + "?" + session_id_query_param + "=" + sessionId_query_param
fmt.Println("pathway_query_param: " + pathway_query_param)
fmt.Println("throughput_query_param: " + throughput_query_param)
dcsm := dcsmSpec{
VERSION: DCSM_VERSION,
TTL: dcsm_ttl,
RELOAD_URI: reloadUri,
SERVICE_LOCATION_PRIORITY: []string{},
}
var base_urls []serviceLocationEntrySpec
getBaseUrls(&base_urls)
if len(base_urls) == 0 {
return nil
}
// TODO: Need to query cdn switcher for the service location priority
for _, sl_entry := range base_urls {
dcsm.SERVICE_LOCATION_PRIORITY = append(dcsm.SERVICE_LOCATION_PRIORITY, sl_entry.ServiceLocationId)
}
dcsmBytes, err := json.Marshal(dcsm)
if err != nil {
fmt.Println("Failed to marshal DCSM JSON object:", err)
}
fmt.Println("DCSM response: .............................\n")
fmt.Printf("%s\n\n", dcsmBytes)
return dcsmBytes
}
func updateDefaultServiceLocationPriority(slm contentSteeringConfigSpec) string {
serviceLocationMap_old := serviceLocationMap
serviceLocationMap = nil
for _, sl := range slm.ServiceLocations {
if sl.ServiceLocationId == "" {
fmt.Println("Error: Empty ServiceLocationId")
serviceLocationMap = serviceLocationMap_old
return "Empty ServiceLocation"
}
if sl.ServiceLocationUri == "" {
fmt.Println("Error: Empty ServiceLocationUri")
serviceLocationMap = serviceLocationMap_old
return "Empty ServiceLocationUri"
}
new_sl := sl
serviceLocationMap = append(serviceLocationMap, new_sl)
}
serviceLocationMap_old = nil
return ""
}
func updateDefaultContentSteeringConfig(csc contentSteeringConfigSpec) string {
if len(csc.ReloadUri) == 0 {
return "Empty RELOAD-URI config"
}
if csc.TTL <= 0 {
return "Non-positive TTL value"
}
content_steering_server_url = csc.ReloadUri
dcsm_ttl = csc.TTL
return updateDefaultServiceLocationPriority(csc)
}
func respondMpd(responseBytes []byte, w http.ResponseWriter) int {
enableCors(w)
FileContentType := "application/dash+xml"
w.Header().Set("Content-Type", FileContentType)
w.Header().Set("Content-Length", strconv.FormatInt(int64(len(responseBytes)), 10))
w.Write(responseBytes)
return 0
}
func respondDcsm(responseBytes []byte, w http.ResponseWriter) int {
enableCors(w)
FileContentType := "application/json"
w.Header().Set("Content-Type", FileContentType)
w.Header().Set("Content-Length", strconv.FormatInt(int64(len(responseBytes)), 10))
w.Write(responseBytes)
return 0
}
func enableCors(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
}
func main_steering_server_handler(w http.ResponseWriter, r *http.Request) {
fmt.Println("----------------------------------------")
fmt.Println("Received new request:")
fmt.Println(r.Method, r.URL.Path)
posLastSingleSlash := strings.LastIndex(r.URL.Path, "/")
UrlLastPart := r.URL.Path[posLastSingleSlash + 1 :]
// Remove trailing "/" if any
if len(UrlLastPart) == 0 {
path_without_trailing_slash := r.URL.Path[0 : posLastSingleSlash]
posLastSingleSlash = strings.LastIndex(path_without_trailing_slash, "/")
UrlLastPart = path_without_trailing_slash[posLastSingleSlash + 1 :]
}
if !(UrlLastPart == content_steering_config_endpoint || strings.Contains(UrlLastPart, dashContentSteeringManifestFileExtension) || strings.Contains(UrlLastPart, dashMpdFileExtension)) {
err := "Endpoint = " + UrlLastPart + " is not supported."
fmt.Println(err)
http.Error(w, "403 forbidden\n Error: " + err, http.StatusForbidden)
return
}
// Handle content steering configuration requests
if UrlLastPart == content_steering_config_endpoint {
// TODO: We should support "GET" content steering config.
// Currently, we only handle "POST" requests to this endpoint
if r.Method != "POST" {
err := "Method = " + r.Method + " is not allowed to " + r.URL.Path
fmt.Println(err)
http.Error(w, "405 method not allowed\n Error: " + err, http.StatusMethodNotAllowed)
return
}
var csc contentSteeringConfigSpec
err := json.NewDecoder(r.Body).Decode(&csc)
if err != nil {
res := "Failed to decode the received content steering configuration. Is it valid JSON?"
fmt.Println("Error happened in JSON marshal. Err: %s", err)
http.Error(w, "400 bad request\n Error: " + res, http.StatusBadRequest)
return
}
res := updateDefaultContentSteeringConfig(csc)
if res == "" {
w.WriteHeader(http.StatusAccepted)
w.Header().Set("Content-Type", "application/json")
jsonResp, err := json.MarshalIndent(csc, " ", " ")
if err != nil {
fmt.Println("Error happened in JSON marshal. Err: %s", err)
http.Error(w, "500 Internal server error", http.StatusInternalServerError)
return
}
w.Write(jsonResp)
return
} else if res != "" {
fmt.Println("Error: ", res)
http.Error(w, "400 bad request.\n Error: " + res, http.StatusBadRequest)
return
}
return
}
posLastDotIn_UrlLastPart := strings.LastIndex(UrlLastPart, ".")
UrlLastPart_fileExtension := UrlLastPart[posLastDotIn_UrlLastPart :]
if strings.Contains(UrlLastPart_fileExtension, dashMpdFileExtension) {
if r.Method == "OPTIONS" {
w.Header().Add("Access-Control-Allow-Methods", "GET")
w.WriteHeader(http.StatusOK)
return
} else if r.Method == "GET" {
remoteMpdUrl := resolveRemoteUrl(UrlLastPart)
mpdBytes := downloadRemoteMpd(remoteMpdUrl)
// Add the BaseUrl elements and the ContentSteering element to MPD
newMpdBytes := addContentSteeringInfoToMpd(mpdBytes)
if newMpdBytes != nil {
respondMpd(newMpdBytes, w)
} else {
fmt.Println("Error: Failed to generate MPD")
http.Error(w, "500 Internal server error\n Error: Failed to generate MPD\nHave you created content steering config first?\ne.g. POST /content_steering_config", http.StatusInternalServerError)
return
}
return
} else {
err := "Error: method = " + r.Method + " is not allowed to " + r.URL.Path
fmt.Println()
http.Error(w, "405 method not allowed.\n Error: " + err, http.StatusMethodNotAllowed)
return
}
} else if strings.Contains(UrlLastPart_fileExtension, dashContentSteeringManifestFileExtension) {
var sid string
var pathway string
var throughput string
// According to the spec, https://dashif.org/docs/DASH-IF-CTS-00XX-Content-Steering-Community-Review.pdf
// the very first DCSM request in a session does not contain query params
if len(r.URL.Query()) == 0 {
sid = uuid.New().String()
fmt.Println("Start of a new session. Generating a new session ID: ", sid)
} else {
sid = r.URL.Query().Get(session_id_query_param)
pathway = r.URL.Query().Get(dash_pathway_query_param)
throughput = r.URL.Query().Get(dash_throughput_query_param)
}
if r.Method == "OPTIONS" {
w.Header().Add("Access-Control-Allow-Methods", "GET")
w.WriteHeader(http.StatusOK)
return
} else if r.Method == "GET" {
// Generate DASH Content Steering Manifest (DCSM) for the requesting client
dcsmBytes := generateDcsm(sid, pathway, throughput, r)
if dcsmBytes == nil {
err := "No DCSM configuration found. Please create DCSM configuration first"
fmt.Println(err)
http.Error(w, "404 not found\n Error: " + err, http.StatusBadRequest)
return
}
respondDcsm(dcsmBytes, w)
return
} else {
err := "Error: method = " + r.Method + " is not allowed to " + r.URL.Path
fmt.Println(err)
http.Error(w, "405 method not allowed.\n Error: " + err, http.StatusMethodNotAllowed)
return
}
}
http.Error(w, "400 bad request", http.StatusBadRequest)
return
}