-
Notifications
You must be signed in to change notification settings - Fork 1
/
streaming-ipfs.js
176 lines (146 loc) · 5.52 KB
/
streaming-ipfs.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
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
'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 requested=0,received = 0;
//var stream = fs.createWriteStream("video.mp4");
var startTime=0,loadedMaxTime=0,bufferSize=30000,bufferingTime=0,playTime=0;
var loaderContext, loaderConfig, loaderCallbacks;
var stopped = false;
var time = 0,loadedTime=0;
var lastTime=0,tickTime=1000,maxOnFly=5;
const hash=process.argv[2]
var loadsuccess = function (name,size) {
var latency = performanceNow()-time;
var throughput = (size*8*1000)/latency
console.log("Load success "+name+" "+latency+" "+size+" "+throughput+" kbps");
//console.log('stats : %j', response.data.length+" "+performanceNow());
//stream.write(response.data);
//console.log(response.url+" chunks: "+stats.chunk+" bytes: "+response.data.byteLength+" hashTime: "+stats.hashtime+" resolTime: "+stats.resoltime+" dTime: "+stats.dtime+" Totaltime: "+stats.totaltime+" totaltput: "+parseFloat(response.data.byteLength*8/(stats.totaltime)).toFixed(2)+" Kbps dloadtput: "+parseFloat(response.data.byteLength*8/(stats.dtime)).toFixed(2)+" Kbps");
if(loadedTime==0)loadedTime=performanceNow();
loadedMaxTime+=durations[received]*1000;
if(startTime==0&&loadedMaxTime>=bufferSize)startTime=performanceNow();
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();
}*/
if(received>=frags.length){
//stream.end();
console.log("Bufferingtime: "+bufferingTime)
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();
})
}
}*/
if(received>0&&loadedMaxTime>playTime)playTime+=tickTime;
else bufferingTime+=tickTime;
//console.log("Play time "+playTime+ " "+loadedMaxTime)
if(requested<frags.length) {
//console.log("Play time "+playTime + " " + loadedMaxTime+" "+requested+" "+received+" "+maxOnFly)
if((playTime>0&&loadedMaxTime-playTime<bufferSize&&requested-received==0)||(playTime==0&&(requested-received)<maxOnFly)) {
//console.log("Load "+frags[requested])
//loaderContext = { url: frags[requested], frag: null, responseType: 'arraybuffer', progressData: false };
time = performanceNow();
/*requested++
loader.load(loaderContext,loaderConfig,loaderCallbacks);*/
requested++
getFile(frags[received],loadsuccess)
}
}
}
function getFile(name,callback)
{
exec("ipfs get "+hash+"/"+name+" >/dev/null" , function (error, stdout, stderr) {
//sys.print('stdout: ' + stdout);
//sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
//console.log(stderr)
var arr = stderr.split(" ")
//console.log(arr[4]);
callback(name,arr[4]);
//console.log("out:"+stdout);
//console.log(stderr);
});
}
function run() {
setInterval(doTick, tickTime);
};
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)
requested++
run();
}
});
});
/*while(true)
{
if(performanceNow()>lastTime+tickTime) {
console.log("Tick "+performanceNow())
lastTime = performanceNow();
if(frags.length>0)doTick();
}
}*/