Blame view

src/com/upc/pbe/upcnews/HLS.java 2.6 KB
Imanol-Mikel Barba Sabariego authored
1
2
package com.upc.pbe.upcnews;
Imanol-Mikel Barba Sabariego authored
3
import java.io.File;
4
import java.io.IOException;
Imanol-Mikel Barba Sabariego authored
5
import java.util.ArrayList;
6
import android.net.TrafficStats;
7
8
import android.util.Log;
Imanol-Mikel Barba Sabariego authored
9
//Gestor del protocol HTTP Live Streaming
10
public class HLS
Imanol-Mikel Barba Sabariego authored
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
Imanol-Mikel Barba Sabariego authored
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]);
Imanol-Mikel Barba Sabariego authored
33
			Log.d(TAG, "Esborrat " + files[i].toString());
Imanol-Mikel Barba Sabariego authored
34
35
			deleteme.delete();
		}
Imanol-Mikel Barba Sabariego authored
36
		currentQuality = 0;
37
		currentVideo = currentQuality = currentSegment = 0;
Imanol-Mikel Barba Sabariego authored
38
		this.videos = parsed;
39
40
41
		endReached = false;
		this.localFolder = localFolder;
		bm = new BandwidthMeasurer();
42
		d = new Descarrega();
Imanol-Mikel Barba Sabariego authored
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;
Imanol-Mikel Barba Sabariego authored
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());
	}
Imanol-Mikel Barba Sabariego authored
93
94
95
96
	public void setDefQuality(int q)
	{
		currentQuality = q;
	}
Imanol-Mikel Barba Sabariego authored
97
}