-
Notifications
You must be signed in to change notification settings - Fork 8
/
SDPProcessing.js
129 lines (119 loc) · 4.65 KB
/
SDPProcessing.js
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
/* Copyright 2018 Streampunk Media Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var SDP = require('../model/SDP.js');
var url = require('url');
var http = require('http');
var fs = require('fs');
var makeDynamorseTags = require('./LedgerDiscovery.js').makeDynamorseTags;
var sdpToTags = function(sdp, config) {
if (typeof sdp === 'string') {
sdp = new SDP(sdp);
}
this.setTag('format', sdp, sdp.getMedia, config);
this.setTag('encodingName', sdp, sdp.getEncodingName, config);
this.setTag('clockRate', sdp, sdp.getClockRate, config);
if (this.tags.format[0].endsWith('video')) {
this.setTag('height', sdp, sdp.getHeight, config);
this.setTag('width', sdp, sdp.getWidth, config);
this.setTag('sampling', sdp, sdp.getSampling, config);
this.setTag('depth', sdp, sdp.getDepth, config);
this.setTag('colorimetry', sdp, sdp.getColorimetry, config);
this.setTag('interlace', sdp, sdp.getInterlace, config);
this.tags.packing = [ 'pgroup' ]; // default
this.setTag('packing', sdp, undefined, config);
} else if (this.tags.format[0].endsWith('audio')) {
this.setTag('channels', sdp, sdp.getEncodingParameters, config);
}
// console.log(this.tags);
this.sdpToExt(sdp);
this.sdp = sdp;
return this.tags;
};
var setTag = function (name, sdp, valueFn, config) {
if (!name) return;
var value = (valueFn) ? valueFn.call(sdp, 0) : undefined;
if (value === undefined) {
value = config[name];
if (value === undefined) {
this.warn(`Did not set property ${name} as it is not defined by SDP or config.`);
return;
}
}
if (typeof value === 'number') {
this.tags[name] = [ `${value}` ];
} else if (typeof value === 'string') {
this.tags[name] = [ value ];
} else if (typeof value === 'boolean') {
this.tags[name] = value ? [ '1' ] : [ '0' ];
} else {
this.warn(`Cannot set property ${name} because value is of unsupported type ${typeof value}.`);
}
};
var sdpURLReader = function (config, cb) {
var self = this;
var sdpUrl = config.sdpURL;
if (!sdpUrl || typeof sdpUrl !== 'string' || sdpUrl.length === 0)
return cb(null, self.sdpToTags({}, config));
var sdpDetails = url.parse(sdpUrl);
if (sdpDetails.protocol.startsWith('file')) {
return fs.readFile(sdpDetails.path, 'utf8', (err, data) => {
if (err) return cb(err);
else return cb(null, self.sdpToTags(data, config));
});
} else if (sdpDetails.protocol.startsWith('http:')) {
http.get(sdpDetails.href, (res) => {
var sdpData = '';
if (res.statusCode !== 200) return cb(new Error(
`SDP file request resulted in non-200 response code of ${res.status}.`));
res.setEncoding('utf8');
res.on('data', data => { sdpData += data; });
res.on('end', () => {
console.log('*** Aggregated SDP', sdpData);
cb(null, self.sdpToTags(sdpData, config));
});
});
} else {
cb(new Error('Cannot read an SDP file with protocols other than http or file.'));
}
};
var sdpToExt = function (sdp) {
if (!SDP.isSDP(sdp)) return;
var revExtMap = sdp.getExtMapReverse(0);
this.exts = {}; // Don't overwrite defaults by accident
this.exts.origin_timestamp_id = revExtMap['urn:x-nmos:rtp-hdrext:origin-timestamp'];
this.exts.smpte_tc_id = revExtMap['urn:ietf:params:rtp-hdrext:smpte-tc'];
this.exts.flow_id_id = revExtMap['urn:x-nmos:rtp-hdrext:flow-id'];
this.exts.source_id_id = revExtMap['urn:x-nmos:rtp-hdrext:source-id'];
this.exts.grain_flags_id = revExtMap['urn:x-nmos:rtp-hdrext:grain-flags'];
this.exts.sync_timestamp_id = revExtMap['urn:x-nmos:rtp-hdrext:sync-timestamp'];
this.exts.grain_duration_id = revExtMap['urn:x-nmos:rtp-hdrext:grain-duration'];
this.exts.ts_refclk = sdp.getTimestampReferenceClock(0);
this.exts.smpte_tc_param = sdp.getSMPTETimecodeParameters(0);
return this.exts;
};
var sdpURLReaderDynamorse = function (config, cb) {
sdpURLReader.call(this, config, (err, v) => {
if (err) cb(err);
else {
this.tags = makeDynamorseTags(v);
cb(null, this.tags);
}
});
};
module.exports = {
sdpToTags : sdpToTags,
setTag : setTag,
sdpURLReader : sdpURLReader,
sdpURLReaderDynamorse : sdpURLReaderDynamorse,
sdpToExt : sdpToExt,
SDP: SDP
};