Blame view

src/com/upc/pbe/upcnews/HLS.java 2.67 KB
Imanol-Mikel Barba Sabariego authored
1
2
package com.upc.pbe.upcnews;
Imanol-Mikel Barba Sabariego authored
3
4
import java.io.File;
import java.util.ArrayList;
Imanol-Mikel Barba Sabariego authored
5
6
7
import android.util.Log;
Imanol-Mikel Barba Sabariego authored
8
//Gestor del protocol HTTP Live Streaming
9
public class HLS
Imanol-Mikel Barba Sabariego authored
10
{
11
12
13
	private static final String TAG = "HLS";
	private ArrayList<ParentList> videos;
14
15
	private ArrayList<Segment> segments;
	private ArrayList<Video> qualities;
16
17
	private int currentVideo;
	private int currentQuality; // 0 es la mas alta
18
19
	private int currentSegment;
	private boolean endReached;
Imanol-Mikel Barba Sabariego authored
20
21
	public HLS(ArrayList<ParentList> parsed, String localFolder)
22
23
	{
		// Neteja el directori i inicialitza les variables
Imanol-Mikel Barba Sabariego authored
24
25
		File dir = new File(localFolder);
		String[] files = dir.list();
26
27
		for (int i = 0; i < files.length; i++)
		{
28
			File deleteme = new File(dir, files[i]);
Imanol-Mikel Barba Sabariego authored
29
			Log.d(TAG, "Esborrat " + files[i].toString());
Imanol-Mikel Barba Sabariego authored
30
31
			deleteme.delete();
		}
Imanol-Mikel Barba Sabariego authored
32
		currentQuality = 0;
33
		currentVideo = currentQuality = currentSegment = 0;
Imanol-Mikel Barba Sabariego authored
34
		this.videos = parsed;
35
		endReached = false;
36
37
	}
38
39
40
	public void loadVideo()
	{
		// Carrega la qualitat i, a partir d'aixo, el seguent segment
41
42
		qualities = videos.get(currentVideo++).getLists();
		segments = qualities.get(currentQuality).getSegments();
43
44
		if (currentVideo == videos.size())
		{
45
			endReached = true;
Imanol-Mikel Barba Sabariego authored
46
47
		}
	}
48
49
	public String next()
50
51
52
53
54
55
	{
		// Determina la seguent qualitat a partir de la velocitat
		if (currentSegment == segments.size())
		{
			if (endReached)
			{
56
57
				return null;
			}
58
59
			currentQuality = 0;
			currentSegment = 0;
60
61
			loadVideo();
		}
62
		Segment seg = segments.get(currentSegment++);
63
		Log.d(TAG, seg.getName() + " " + seg.getURL());
64
65
66
67
68
69
		return seg.getURL();
	}

	public long getCurrentSegmentDuration()
	{
		return (long) segments.get(currentSegment).getDuration();
70
71
72
73
74
	}

	public void updateQuality(long bps)
	{
		if ((bps <= qualities.get(currentQuality).getQuality()) && (bps != -1))
75
		{
76
77
			int newQuality;
			for(newQuality = 0; newQuality < qualities.size(); newQuality++)
78
			{
79
				if(bps >= qualities.get(newQuality).getQuality())
Imanol-Mikel Barba Sabariego authored
80
				{
81
					break;
82
83
				}
			}
84
			if(currentQuality != newQuality)
85
			{
86
87
88
89
90
91
92
93
94
95
96
				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())
97
				{
98
99
					newQuality--;
					break;
100
101
				}
			}
102
103
104
105
106
107
			if(currentQuality != newQuality)
			{
				currentQuality = newQuality;
				currentVideo--; //Corregimos el del loadVideo()
				loadVideo(); //Cargamos la nueva calidad
			}
108
		}
109
110
	}
111
Imanol-Mikel Barba Sabariego authored
112
}