-
Notifications
You must be signed in to change notification settings - Fork 1
/
streaming-ndn.js
145 lines (116 loc) · 3.92 KB
/
streaming-ndn.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
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict'
/* global Hls Ipfs HlsjsIpfsLoader */
/* eslint-env browser */
//const HLS = require('hls-parser'); // For node
var m3u8 = require('m3u8');
var fs = require('fs');
var exec = require('child_process').exec;
var fs = require('fs');
const date = require('log-timestamp');
var performanceNow = require("performance-now")
var frags = [];
var durations = [];
var received = 0;
//var stream = fs.createWriteStream("video.mp4");
var loadedTime=0,loadedMaxTime=0,bufferedTime=30000;
var loaderContext, loaderConfig, loaderCallbacks;
var lastTime=0,tickTime=100;
var stopped = false;
var loadsuccess = function (name) {
console.log("Load success "+name);
//console.log('stats : %j', response.data.length+" "+performanceNow());
//stream.write(response.data);
if(loadedTime==0)loadedTime=performanceNow();
loadedMaxTime+=durations[received]*1000;
received++;
//doTick();
if(received<frags.length) {
let playTime = performanceNow()-loadedTime;
//console.log("Play2 time "+playTime + " " + loadedMaxTime)
if(loadedMaxTime-playTime<bufferedTime) {
console.log("Load "+frags[received])
getFile(frags[received],loadsuccess)
} else {
stopped = true;
}
} else {
//stream.end();
process.exit();
}
};
function parsePlayList(playlist,callback){
let parser = m3u8.createStream();
//const playlist = HLS.parse(file);
let file2 = fs.createReadStream(playlist);
file2.pipe(parser);
parser.on('item', function(item) {
// emits PlaylistItem, MediaItem, StreamItem, and IframeStreamItem
//console.log(item)
callback(item.properties.uri,item.properties.duration)
});
}
function doTick(){
let playTime = performanceNow()-loadedTime;
console.log("Play time "+playTime+ " "+loadedMaxTime)
if(stopped){
if(received<frags.length) {
console.log("Play time "+playTime + " " + loadedMaxTime)
if(loadedMaxTime-playTime<bufferedTime) {
console.log("Load "+frags[received])
//loaderContext = { url: frags[received], frag: null, responseType: 'arraybuffer', progressData: false };
//loader.load(loaderContext,loaderConfig,loaderCallbacks);
getFile(frags[received],loadsuccess)
stopped=false;
}
} else {
//stream.end();
node.stop(error => {
if (error) {
return console.error('Node failed to stop cleanly!', error)
}
console.log('Node stopped!')
process.exit();
})
}
}
}
function getFile(name,callback)
{
exec("ndncatchunks /video/"+name+" > "+name , function (error, stdout, stderr) {
//sys.print('stdout: ' + stdout);
//sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
callback(name);
console.log("out:"+stdout);
console.log("err:"+stderr);
});
}
function run() {
setInterval(doTick, 1000);
};
getFile("master.m3u8", function(name) {
// use the return value here instead of like a regular (non-evented) return value
parsePlayList(name,function(frag,duration) {
//console.log("Received "+frag+" "+duration)
frags.push(frag)
durations.push(duration)
//console.log("Result " +frags[frags.length-1]+" "+durations[durations.length-1])
if(frags.length==1){
//loaderContext = { url: frags[0], frag: null, responseType: 'arraybuffer', progressData: false };
//loader.load(loaderContext,loaderConfig,loaderCallbacks)
console.log("Get file "+frags[received]);
getFile(frags[received],loadsuccess)
run();
}
});
});
/*while(true)
{
if(performanceNow()>lastTime+tickTime) {
console.log("Tick "+performanceNow())
lastTime = performanceNow();
if(frags.length>0)doTick();
}
}*/