HLS.java
2.67 KB
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
package com.upc.pbe.upcnews;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import android.util.Log;
//Gestor del protocol HTTP Live Streaming
public class HLS
{
private static final String TAG = "HLS";
private ArrayList<ParentList> videos;
private ArrayList<Segment> segments;
private ArrayList<Video> qualities;
private int currentVideo;
private int currentQuality; // 0 es la mas alta
private int currentSegment;
private boolean endReached;
public HLS(ArrayList<ParentList> parsed, String localFolder)
{
// Neteja el directori i inicialitza les variables
File dir = new File(localFolder);
String[] files = dir.list();
for (int i = 0; i < files.length; i++)
{
File deleteme = new File(dir, files[i]);
Log.d(TAG, "Esborrat " + files[i].toString());
deleteme.delete();
}
currentQuality = 0;
currentVideo = currentQuality = currentSegment = 0;
this.videos = parsed;
endReached = false;
}
public void loadVideo()
{
// Carrega la qualitat i, a partir d'aixo, el seguent segment
qualities = videos.get(currentVideo++).getLists();
segments = qualities.get(currentQuality).getSegments();
if (currentVideo == videos.size())
{
endReached = true;
}
}
public String next() throws IOException
{
// Determina la seguent qualitat a partir de la velocitat
if (currentSegment == segments.size())
{
if (endReached)
{
return null;
}
loadVideo();
}
Segment seg = segments.get(currentSegment++);
Log.d(TAG, seg.getName() + " " + seg.getURL());
return seg.getURL();
}
public long getCurrentSegmentDuration()
{
return (long) segments.get(currentSegment).getDuration();
}
public void updateQuality(long bps)
{
if ((bps <= qualities.get(currentQuality).getQuality()) && (bps != -1))
{
int newQuality;
for(newQuality = 0; newQuality < qualities.size(); newQuality++)
{
if(bps >= qualities.get(newQuality).getQuality())
{
break;
}
}
if(currentQuality != newQuality)
{
currentQuality = newQuality;
currentVideo--; //Corregimos el del loadVideo()
loadVideo(); //Cargamos la nueva calidad
}
}
else if((bps > qualities.get(currentQuality).getQuality()) && (bps != -1))
{
int newQuality;
for(newQuality = qualities.size()-1; newQuality > 0; newQuality--)
{
if(bps <= qualities.get(newQuality).getQuality())
{
newQuality--;
break;
}
}
if(currentQuality != newQuality)
{
currentQuality = newQuality;
currentVideo--; //Corregimos el del loadVideo()
loadVideo(); //Cargamos la nueva calidad
}
}
}
}