forked from ISIA-DASHR/DASHR-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BufferManager.js
245 lines (196 loc) · 6.97 KB
/
BufferManager.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
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
"use strict";
define(['./VirtualBufferedManager'], function(VirtualBufferedManager){
var BufferManager = function(video,emergencyMarge,bufferMarge)
{
this.video = video;
this.emergencyMarge = emergencyMarge; // specifie a quel moment on considere qu'on n'a plus le droit de lire
this.bufferMarge = bufferMarge; // si bufferMarge < t(finbuffer) - t(lecture) demande la piece suivante
this.bufs = new Array();
this.STREAMING = 0;
this.PREBUFFERING_MAX = 1;
this.STOP_BUFFERING = 3;
this.BUFFER_LOCAL_PIECE = 4;
this.mode = this.STOP_BUFFERING;
this.is_ready = false;
this.is_initiated = false;
this.storedBufferInfos = new Array();
this.bufferize();
}
BufferManager.prototype.set_onBufferReady = function(cb)
{
this.cb_bufferready = cb;
}
BufferManager.prototype.set_onInitiated = function(cb)
{
this.cb_initiated = cb;
}
BufferManager.prototype.set_onQoS= function(cb)
{
this.cb_qos = cb;
}
BufferManager.prototype.set_buffering_mode = function (mode)
{
this.old_mode = this.mode;
this.mode = mode;
}
BufferManager.prototype.getVideoBufferedTimeRanges = function()
{
//il faudrait plutot faire l'intersection des times range de chaque sourcebuffer
// mais pour simplifier ou prend celui qui finit le plus proche de la position de lecture.
//c linfos la plus importante
if(!this.storedBufferInfos.length)
{
return new VirtualBufferedManager();
}
var bt = new Array();
for(var i=0; i<this.storedBufferInfos.length; i++)
{
bt.push(this.getMaxBufferedTimeFromCurrentTime(this.storedBufferInfos[i]));
}
return this.storedBufferInfos[bt.indexOf(Math.min.apply(null, bt))].buffered;
}
BufferManager.prototype.getMaxBufferedTimeFromCurrentTime = function(buf)
{
buf = buf || this.video; // pas prendre en compte parce que le buffer video comprend potentiellement dautre representation que celle ds laquelle on est
for(var i=0; i<buf.buffered.length; i++)
{
if(buf.buffered.start(i)<=this.video.currentTime && this.video.currentTime<=buf.buffered.end(i))
{
return buf.buffered.end(i);
}
}
return this.video.currentTime;
}
BufferManager.prototype.appendData = function(id_buffer,result)
{
if(result && result.response)
{
// appel de la gestion du QoS
this.video.ms.sourceBuffers[id_buffer].append(new Uint8Array(result.response));
// update virtual buffered
this.storedBufferInfos[id_buffer].buffered.addPiece(result.pptes.piece);
if(this.cb_qos)
{
this.cb_qos({type : 'bufferInfos'});
}
}
}
BufferManager.prototype.bufferize = function (id_buffer,result)
{
if(this.video.ms.duration >0 && this.video.currentTime + 0.1 >= this.video.ms.duration && this.video.event_handlers.onFinish)
{
this.video.event_handlers.onFinish();
}
var emergency = false;
if((this.mode != this.STOP_BUFFERING) && this.is_initiated)
{
for(var i=0;i<this.video.ms.sourceBuffers.length; i++)
{
var buf = this.video.ms.sourceBuffers[i];
//var mbt2= (buf.buffered.length && this.getCurrentBufferedPart(buf)!==false) ? buf.buffered.end(this.getCurrentBufferedPart(buf)) : this.video.currentTime;
//utilisation du buffered virtuel
var maxbufferedtime = this.getMaxBufferedTimeFromCurrentTime(this.storedBufferInfos[i]);
//console.log(maxbufferedtime + ' et '+mbt2);
var d = maxbufferedtime - this.video.currentTime;
if( (d < this.bufferMarge) || (this.mode == this.PREBUFFERING_MAX))
{
if(d<=0) // cas d'un seek par ex
{
maxbufferedtime = this.video.currentTime;
}
var piece = this.video.mpd.getPartForTime(this.currentPeriod, maxbufferedtime+0.1,this.storedBufferInfos[i].id_aset,this.storedBufferInfos[i].id_rep);
}
if(d<=this.emergencyMarge)
{
emergency = true;
}
if(piece)
{
this.video.dlm.getPart(piece,this.appendData.bind(this,i));
if(this.mode == this.BUFFER_LOCAL_PIECE)
{
this.set_buffering_mode(this.old_mode);
}
}
}
//on fait le test global sur la video pour voir si on est pret à etre en lecture ou pas
if((!emergency) && !this.is_ready)
{
this.is_ready = true;
if(this.cb_bufferready)
{
this.cb_bufferready();
}
}
else if((emergency && this.is_ready) && (this.video.ms.duration - this.video.currentTime > this.emergencyMarge + 1) ) // desactivation de l'emergency sur la toute fin
{
console.log('emergency');
this.is_ready = false;
if(this.cb_qos)
{
this.cb_qos({type : 'emergency'});
}
}
////////////////////
}
window.setTimeout(this.bufferize.bind(this),100);
}
BufferManager.prototype.initBuffer = function(period, tobuffer)
{
if(this.video.ms.readyState == "closed")
{
window.setTimeout(this.initBuffer.bind(this,period,tobuffer),100);
return;
}
console.log(tobuffer);
//buffers == array de {id_aset, id_rep} // c'est dash.js qui s'occupe de faire attention a envoyer des reps compatibles
this.currentPeriod = period;
var infos = this.video.mpd.getInit(period, tobuffer);
console.log(infos);
console.log(this.video.ms.readyState);
this.video.ms.duration = infos.duration;
this.storedBufferInfos = new Array();
for(var i = 0; i< infos.inits.length; i++)
{
this.video.ms.addSourceBuffer(infos.inits[i].mimecodec);
this.storedBufferInfos[i] = {}
}
for(var i = 0; i< this.video.ms.sourceBuffers.length; i++) // il faut d'abord ajouter tous les buffers avant d'ajouter l'initialisation
{
this.video.ms.sourceBuffers[i].append(infos.inits[i].v);
this.storedBufferInfos[i].id_aset = tobuffer[i].id_aset; //pas possible de stocker directement sur le sourcebuffer car chrome l'efface au changement de fenetre
this.storedBufferInfos[i].id_rep = tobuffer[i].id_rep;
this.storedBufferInfos[i].buffered = new VirtualBufferedManager();
}
if(this.cb_initiated)
{
this.cb_initiated();
}
this.is_initiated = true;
}
BufferManager.prototype.setAsetBufferFromTo= function(id_aset_from, id_aset_to, id_rep_to, force)
{
for(var i =0; i<this.storedBufferInfos.length; i++)
{
if(this.storedBufferInfos[i].id_aset == id_aset_from)
{
var init = this.video.mpd.getInit(this.currentPeriod, [{id_aset : id_aset_to,id_rep : id_rep_to}]);
this.video.ms.sourceBuffers[i].abort();
this.video.ms.sourceBuffers[i].append(init.inits[0].v);
console.log('switch de '+this.storedBufferInfos[i].id_rep+' a '+id_rep_to);
this.storedBufferInfos[i].id_rep = id_rep_to;
this.storedBufferInfos[i].id_aset = id_aset_to;
if(force) // on clear le buffered si on force car ça veut dire qu'on veut toute la video ds la rep choisie
{
this.storedBufferInfos[i].buffered.clear();
}
if(this.cb_initiated) //on doit le lancer car c la dedans entre autre que le p2p est notifié du chgt de fenetre
// et que c lance ds le cas force donc autant etre coherent
{
this.cb_initiated();
}
}
}
}
return (BufferManager);
});