-
Notifications
You must be signed in to change notification settings - Fork 23
/
dash.js
84 lines (72 loc) · 3.14 KB
/
dash.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
'use strict';
const config = require('../../config');
const errors = require('../../components/errors');
const _ = require('lodash');
const path = require('path');
const moment = require('moment');
const xmlBuilder = require('xmlbuilder');
const express = require('express');
const router = express.Router();
const VideoLib = require('node-video-lib');
const Movie = require('../../components/movie');
router.get(/^(.*)\/manifest\.mpd$/, Movie.openMovie, (req, res) => {
let duration = req.fragmentList.relativeDuration();
let xml = xmlBuilder.create('MPD', {encoding: 'UTF-8'})
.att('xmlns', 'urn:mpeg:dash:schema:mpd:2011')
.att('profiles', 'urn:mpeg:dash:profile:full:2011')
.att('minBufferTime', 'PT1.5S');
let period = xml.ele('Period').att('duration', moment.duration(duration, 's').toISOString());
if (req.fragmentList.video !== null) {
let segment = period.ele('AdaptationSet')
.att('mimeType', 'video/mp4')
.ele('Representation')
.att('id', 'video')
.att('bandwidth', duration > 0 ? Math.round(8 * req.fragmentList.video.size / duration) : 0)
.att('codecs', req.fragmentList.video.codec)
.att('width', req.fragmentList.video.width)
.att('height', req.fragmentList.video.height)
.ele('SegmentTemplate')
.att('timescale', req.fragmentList.video.timescale)
.att('media', 'video-$Number$.ts')
.att('initialization', 'video.sidx');
let timestamp = 0;
for (let i = 0, l = req.fragmentList.count(); i < l; i++) {
let duration = req.fragmentList.get(i).duration;
segment.ele('S').att('t', timestamp).att('d', duration);
timestamp += duration;
}
}
if (req.fragmentList.audio !== null) {
let segment = period.ele('AdaptationSet')
.att('mimeType', 'audio/mp4')
.ele('Representation')
.att('id', 'audio')
.att('bandwidth', duration > 0 ? Math.round(8 * req.fragmentList.audio.size / duration) : 0)
.att('codecs', req.fragmentList.audio.codec)
.ele('SegmentTemplate')
.att('timescale', req.fragmentList.audio.timescale)
.att('media', 'audio-$Number$.ts')
.att('initialization', 'audio.sidx');
let timestamp = 0;
for (let i = 0, l = req.fragmentList.count(); i < l; i++) {
let duration = req.fragmentList.get(i).duration;
segment.ele('S').att('t', timestamp).att('d', duration);
timestamp += duration;
}
}
res.header('Content-Type', 'text/xml');
res.send(xml.end({pretty: true}));
});
router.get(/^(.*)\/(audio|video)\.sidx/, Movie.openMovie, (req, res) => {
let type = req.params[1];
req.logger.info('Type:', type);
let buffer = VideoLib.DASHPacketizer[`${type}Index`](req.fragmentList);
res.send(buffer);
});
router.get(/^(.*)\/(audio|video)-(\d+)\.ts$/, Movie.openMovie, (req, res) => {
let type = req.params[1];
let index = parseInt(req.params[2], 10);
req.logger.info('Type:', type, 'Index', index);
res.end();
});
module.exports = router;