Commit c12833f5e31c3f4a3c7302534960f27961e02486

Authored by Imanol-Mikel Barba Sabariego
1 parent 79edf2bf

HLS casi terminado, añadido BandwidthMeasurer

src/com/upc/pbe/upcnews/BandwidthMeasurer.java 0 → 100644
  1 +package com.upc.pbe.upcnews;
  2 +
  3 +import android.net.TrafficStats;
  4 +
  5 +public class BandwidthMeasurer
  6 +{
  7 + public double Measure(long rxBytes, long Time)
  8 + {
  9 + double bps;
  10 + long TotalRxAfterTest = TrafficStats.getTotalRxBytes();
  11 + long AfterTime = System.currentTimeMillis();
  12 +
  13 + double TimeDifference = AfterTime - Time;
  14 +
  15 + double rxDiff = TotalRxAfterTest - rxBytes;
  16 +
  17 + if(rxDiff != 0)
  18 + {
  19 + bps = (rxDiff / (TimeDifference/1000)); // total rx bytes per second.
  20 + }
  21 + else
  22 + {
  23 + bps = -1; //No transmitted data
  24 + }
  25 + return bps;
  26 + }
  27 +}
src/com/upc/pbe/upcnews/Directoris.java
@@ -112,9 +112,8 @@ public class Directoris extends Activity implements OnItemClickListener @@ -112,9 +112,8 @@ public class Directoris extends Activity implements OnItemClickListener
112 { 112 {
113 ArrayList<ParentList> m3u8parsed = p.parseFile(playlist); 113 ArrayList<ParentList> m3u8parsed = p.parseFile(playlist);
114 Log.d(TAG, "parsing completed"); 114 Log.d(TAG, "parsing completed");
115 - HLS h = new HLS(m3u8parsed);  
116 - // h.start();  
117 - 115 + HLS h = new HLS(m3u8parsed,((UpcApp)getApplication()).getLocalPath());
  116 + h.start();
118 } 117 }
119 catch (ErrorException e) 118 catch (ErrorException e)
120 { 119 {
src/com/upc/pbe/upcnews/HLS.java
@@ -2,31 +2,60 @@ package com.upc.pbe.upcnews; @@ -2,31 +2,60 @@ package com.upc.pbe.upcnews;
2 2
3 import java.util.ArrayList; 3 import java.util.ArrayList;
4 4
  5 +import android.util.Log;
  6 +
5 public class HLS { 7 public class HLS {
6 8
7 - static final String TAG = "HLS";  
8 -  
9 - ArrayList<ParentList> videos;  
10 - int video;  
11 - int quality; //0 es la más alta 9 + private static final String TAG = "HLS";
  10 + private ArrayList<ParentList> videos;
  11 + private int currentVideo ;
  12 + private int currentQuality; //0 es la más alta
  13 + private int currentSegment;
  14 + private boolean endReached;
  15 + private String localFolder;
  16 + //private fileDownloader fd;
  17 + private BandwidthMeasurer bm;
12 18
13 - public HLS(ArrayList<ParentList> parsed) 19 + public HLS(ArrayList<ParentList> parsed, String localFolder)
14 { 20 {
  21 + currentVideo = currentQuality = currentSegment = 0;
15 this.videos = parsed; 22 this.videos = parsed;
16 - video = 0;  
17 - quality = 0; 23 + endReached = false;
  24 + this.localFolder = localFolder;
  25 + bm = new BandwidthMeasurer();
18 } 26 }
19 27
20 public void start() 28 public void start()
21 { 29 {
22 - int currentVideo = 0;  
23 - for(video = 0; video < videos.size(); video++) 30 + for(currentVideo = 0; currentVideo < videos.size(); currentVideo++)
24 { 31 {
25 - ArrayList<Video> qualities = videos.get(video).getLists();  
26 - /*while(i > videos.get(quality).getLists()) 32 + ArrayList<Video> qualities = videos.get(currentVideo++).getLists();
  33 + while(!endReached)
27 { 34 {
28 - Segment seg = videos.get(video).getLists().get(quality).getSegments().get(i);  
29 - }*/ 35 + ArrayList<Segment> segments = qualities.get(currentQuality).getSegments();
  36 + while(true)
  37 + {
  38 + Segment seg = segments.get(currentSegment++);
  39 + Log.d(TAG, seg.getName() + " " + seg.getURL());
  40 + long startTime = System.currentTimeMillis();
  41 + //long segmentBytes = fd.Download(seg.getURL(),localFolder);
  42 + long segmentBytes = 0;
  43 + double bps = bm.Measure(segmentBytes,startTime);
  44 + if(bps < qualities.get(currentQuality).getQuality())
  45 + {
  46 + currentQuality++;
  47 + break;
  48 + }
  49 + /*
  50 + * REPRODUCIR SEGMENTO
  51 + */
  52 + if(currentSegment == segments.size())
  53 + {
  54 + endReached = true;
  55 + break;
  56 + }
  57 + }
  58 + }
30 } 59 }
31 } 60 }
32 } 61 }
src/com/upc/pbe/upcnews/MainActivity.java
1 package com.upc.pbe.upcnews; 1 package com.upc.pbe.upcnews;
2 2
3 import android.app.Activity; 3 import android.app.Activity;
4 -import android.content.Context;  
5 import android.content.Intent; 4 import android.content.Intent;
6 -import android.net.wifi.WifiInfo;  
7 -import android.net.wifi.WifiManager;  
8 import android.os.Bundle; 5 import android.os.Bundle;
9 import android.util.Log; 6 import android.util.Log;
10 import android.view.Menu; 7 import android.view.Menu;
@@ -12,55 +9,23 @@ import android.view.MenuItem; @@ -12,55 +9,23 @@ import android.view.MenuItem;
12 import android.view.View; 9 import android.view.View;
13 import android.view.View.OnClickListener; 10 import android.view.View.OnClickListener;
14 import android.widget.ImageButton; 11 import android.widget.ImageButton;
15 -import android.widget.TextView;  
16 import android.widget.Toast; 12 import android.widget.Toast;
17 13
18 public class MainActivity extends Activity implements OnClickListener { 14 public class MainActivity extends Activity implements OnClickListener {
19 - final static String TAG = "Main";  
20 - static String html;  
21 - WifiManager wifi;  
22 -  
23 - Directoris dir;  
24 - ImageButton buttonDescarrega;  
25 - String file = "ejemplo.xml";  
26 - String folder = "Environment.getExternalStorageDirectory.getPath()";  
27 - TextView showText; 15 + private final static String TAG = "Main";
  16 + private static String html;
  17 + private ImageButton buttonDescarrega;
28 18
29 19
30 @Override 20 @Override
31 - public void onCreate(Bundle savedInstanceState) {  
32 - /*  
33 - * FALTA:  
34 - * Boton mayor resolucion  
35 - */ 21 + public void onCreate(Bundle savedInstanceState)
  22 + {
36 super.onCreate(savedInstanceState); 23 super.onCreate(savedInstanceState);
37 setContentView(R.layout.main_activity); 24 setContentView(R.layout.main_activity);
38 Log.d(TAG, "onCreated"); 25 Log.d(TAG, "onCreated");
39 buttonDescarrega = (ImageButton) findViewById(R.id.button); 26 buttonDescarrega = (ImageButton) findViewById(R.id.button);
40 buttonDescarrega.setOnClickListener(this); 27 buttonDescarrega.setOnClickListener(this);
41 - showText = (TextView) findViewById(R.id.textViewXml);  
42 this.getUrl(); 28 this.getUrl();
43 -  
44 - new Thread() {  
45 - public void run() {  
46 - //Wifi Info  
47 - int speed = 0;  
48 - for (int i = 0; i < 5; i++) {  
49 - wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
50 - WifiInfo info = wifi.getConnectionInfo();  
51 - speed = speed + info.getLinkSpeed();  
52 - try {  
53 - Thread.sleep(1000);  
54 - } catch (InterruptedException e) {  
55 - e.printStackTrace();  
56 - }  
57 -  
58 - }  
59 -  
60 - String str = "Speed: " + speed/5;  
61 - Log.d(TAG, str);  
62 - }  
63 - }.start();  
64 } 29 }
65 30
66 public boolean onCreateOptionsMenu(Menu menu) { 31 public boolean onCreateOptionsMenu(Menu menu) {
src/com/upc/pbe/upcnews/Parser.java
@@ -221,7 +221,7 @@ public class Parser @@ -221,7 +221,7 @@ public class Parser
221 } 221 }
222 expectList = true; 222 expectList = true;
223 String programID = ""; 223 String programID = "";
224 - int bandwidth = -1; 224 + double bandwidth = -1;
225 String[] arguments = extTag[1].split(","); 225 String[] arguments = extTag[1].split(",");
226 for (int i = 0; i < arguments.length; i++) 226 for (int i = 0; i < arguments.length; i++)
227 { 227 {
@@ -261,7 +261,7 @@ public class Parser @@ -261,7 +261,7 @@ public class Parser
261 } 261 }
262 else if (argument[0].equals("BANDWIDTH")) 262 else if (argument[0].equals("BANDWIDTH"))
263 { 263 {
264 - bandwidth = Integer.parseInt(argument[1]); 264 + bandwidth = Double.parseDouble(argument[1]);
265 } 265 }
266 } 266 }
267 if (programID.equals("") || bandwidth == -1) 267 if (programID.equals("") || bandwidth == -1)
src/com/upc/pbe/upcnews/UpcApp.java
@@ -3,6 +3,7 @@ package com.upc.pbe.upcnews; @@ -3,6 +3,7 @@ package com.upc.pbe.upcnews;
3 import android.app.Application; 3 import android.app.Application;
4 import android.content.SharedPreferences; 4 import android.content.SharedPreferences;
5 import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 5 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  6 +import android.os.Environment;
6 import android.preference.PreferenceManager; 7 import android.preference.PreferenceManager;
7 import android.util.Log; 8 import android.util.Log;
8 9
@@ -10,10 +11,16 @@ import android.util.Log; @@ -10,10 +11,16 @@ import android.util.Log;
10 public class UpcApp extends Application implements OnSharedPreferenceChangeListener { 11 public class UpcApp extends Application implements OnSharedPreferenceChangeListener {
11 12
12 final static String TAG = "Application"; 13 final static String TAG = "Application";
13 - SharedPreferences prefs;  
14 - String defaultUrl = "imanolbarba.myftp.biz/PBE";  
15 - String url = null;  
16 - String desc; 14 + private SharedPreferences prefs;
  15 + private String defaultUrl = "imanolbarba.myftp.biz/PBE";
  16 + private String url = null;
  17 + private String desc;
  18 + private final static String localPath = Environment.getExternalStorageDirectory().getPath() + "UPC/";
  19 +
  20 + public String getLocalPath()
  21 + {
  22 + return localPath;
  23 + }
17 24
18 public String getUrl() { 25 public String getUrl() {
19 url = "http://" + prefs.getString("server", defaultUrl); 26 url = "http://" + prefs.getString("server", defaultUrl);
src/com/upc/pbe/upcnews/Video.java
@@ -5,12 +5,12 @@ import java.util.ArrayList; @@ -5,12 +5,12 @@ import java.util.ArrayList;
5 public class Video 5 public class Video
6 { 6 {
7 7
8 - private int quality; 8 + private double quality;
9 private int sequenceFirst; 9 private int sequenceFirst;
10 private int maxDuration; 10 private int maxDuration;
11 private ArrayList<Segment> segments; //Lista de SEGMENTOS 11 private ArrayList<Segment> segments; //Lista de SEGMENTOS
12 12
13 - public Video(int q) 13 + public Video(double q)
14 { 14 {
15 quality = q; 15 quality = q;
16 segments = new ArrayList<Segment>(); 16 segments = new ArrayList<Segment>();
@@ -36,12 +36,12 @@ public class Video @@ -36,12 +36,12 @@ public class Video
36 return maxDuration; 36 return maxDuration;
37 } 37 }
38 38
39 - public int getQuality() 39 + public double getQuality()
40 { 40 {
41 return quality; 41 return quality;
42 } 42 }
43 43
44 - public void setQuality(int q) 44 + public void setQuality(double q)
45 { 45 {
46 quality = q; 46 quality = q;
47 } 47 }