|
1
2
|
package com.upc.pbe.upcnews;
|
|
3
|
import java.io.File;
|
|
4
|
import java.io.IOException;
|
|
5
|
import java.util.ArrayList;
|
|
6
|
import android.net.TrafficStats;
|
|
7
8
|
import android.util.Log;
|
|
9
|
//Gestor del protocol HTTP Live Streaming
|
|
10
|
public class HLS
|
|
11
|
{
|
|
12
|
|
|
13
14
|
private static final String TAG = "HLS";
private ArrayList<ParentList> videos;
|
|
15
16
|
private ArrayList<Segment> segments;
private ArrayList<Video> qualities;
|
|
17
18
|
private int currentVideo;
private int currentQuality; // 0 es la mas alta
|
|
19
20
21
|
private int currentSegment;
private boolean endReached;
private String localFolder;
|
|
22
|
private Descarrega d;
|
|
23
|
private BandwidthMeasurer bm;
|
|
24
|
|
|
25
26
27
|
public HLS(ArrayList<ParentList> parsed, String localFolder)
{
// Neteja el directori i inicialitza les variables
|
|
28
29
|
File dir = new File(localFolder);
String[] files = dir.list();
|
|
30
31
|
for (int i = 0; i < files.length; i++)
{
|
|
32
|
File deleteme = new File(dir, files[i]);
|
|
33
|
Log.d(TAG, "Esborrat " + files[i].toString());
|
|
34
35
|
deleteme.delete();
}
|
|
36
|
currentQuality = 0;
|
|
37
|
currentVideo = currentQuality = currentSegment = 0;
|
|
38
|
this.videos = parsed;
|
|
39
40
41
|
endReached = false;
this.localFolder = localFolder;
bm = new BandwidthMeasurer();
|
|
42
|
d = new Descarrega();
|
|
43
|
}
|
|
44
|
|
|
45
46
47
|
public void loadVideo()
{
// Carrega la qualitat i, a partir d'aixo, el seguent segment
|
|
48
49
|
qualities = videos.get(currentVideo++).getLists();
segments = qualities.get(currentQuality).getSegments();
|
|
50
51
|
if (currentVideo == videos.size())
{
|
|
52
|
endReached = true;
|
|
53
54
|
}
}
|
|
55
|
|
|
56
57
58
59
60
61
62
|
public String next() throws IOException
{
// Determina la seguent qualitat a partir de la velocitat
if (currentSegment == segments.size())
{
if (endReached)
{
|
|
63
64
65
66
|
return null;
}
loadVideo();
}
|
|
67
68
69
70
|
Segment seg = segments.get(currentSegment++);
Log.d(TAG, seg.getName() + " " + seg.getURL());
long startTime = System.currentTimeMillis();
long segmentBytes = TrafficStats.getTotalRxBytes();
|
|
71
72
73
74
75
|
d.descarregarguardar(seg.getURL(), localFolder);
double bps = bm.Measure(segmentBytes, startTime);
Log.d(TAG, "Velocitat actual (KB/s): " + (bps / 8e3));
if ((bps < qualities.get(currentQuality).getQuality()) && (bps != -1))
{
|
|
76
77
|
currentQuality++;
}
|
|
78
79
|
return localFolder
+ seg.getURL().substring(seg.getURL().lastIndexOf("/") + 1, seg.getURL().length());
|
|
80
|
}
|
|
81
82
83
84
85
86
87
88
89
90
91
92
|
public String previous() throws IOException
{
Segment seg = null;
if (--currentSegment > 0)
{
--currentSegment;
}
seg = segments.get(currentSegment++);
return localFolder + seg.getURL().substring(seg.getURL().lastIndexOf("/") + 1, seg.getURL().length());
}
|
|
93
94
95
96
|
public void setDefQuality(int q)
{
currentQuality = q;
}
|
|
97
|
}
|