Commit 13c43e11eeab434e5677189fa6664cd494d2f43c

Authored by Imanol-Mikel Barba Sabariego
1 parent 29086b71

Reproductor funcionando 100%

src/com/upc/pbe/upcnews/BandwidthMeasurer.java
1 1 package com.upc.pbe.upcnews;
2 2  
3 3 import android.net.TrafficStats;
  4 +import android.util.Log;
4 5  
5 6 //Mesura la velocitat (Bandwidth) d'Internet
6 7 public class BandwidthMeasurer
7 8 {
8   - public double Measure(long rxBytes, long Time){
  9 + private final static String TAG = "Measurer";
  10 + public double Measure(long rxBytes, long Time)
  11 + {
9 12 long AfterTime = System.currentTimeMillis();
10 13 double bps;
11 14 long TotalRxAfterTest = TrafficStats.getTotalRxBytes();
12 15 double TimeDifference = AfterTime - Time;
13 16 double rxDiff = TotalRxAfterTest - rxBytes;
14   -
  17 + Log.d(TAG,"Time difference = " + TimeDifference/1000);
  18 + Log.d(TAG, "Bytes difference = " + rxDiff);
15 19 if(rxDiff != 0){
16 20 bps = ((rxDiff*8) / (TimeDifference/1000)); // total rx bits per second.
17 21 }
... ...
src/com/upc/pbe/upcnews/Descarrega.java
... ... @@ -7,7 +7,6 @@ import java.io.IOException;
7 7 import java.io.InputStream;
8 8 import java.io.InputStreamReader;
9 9 import java.io.OutputStream;
10   -import java.net.MalformedURLException;
11 10 import java.net.URL;
12 11 import java.net.URLConnection;
13 12  
... ... @@ -15,72 +14,60 @@ import android.os.AsyncTask;
15 14 import android.util.Log;
16 15  
17 16 //Descarrega un arxiu i el guarda o retorna en String
18   -public class Descarrega extends AsyncTask<Object, Object, Object> {
19   -
  17 +public class Descarrega extends AsyncTask<Object, Object, Object>
  18 +{
  19 +
20 20 final static String TAG = "Descarrega";
21   - private String url, carpeta, nomarxiu, html;
22   - private File arxiu;
  21 + private String html;
23 22  
24   - public Descarrega() {
25   - nomarxiu = "ejemplo.xml";
26   - carpeta = "Environment.getExternalStorageDirectory.getPath()";
27   - this.carpeta = null;
28   - this.html = "";
29   - this.arxiu = null;
  23 + public Descarrega()
  24 + {
  25 + html = "";
30 26 }
31 27  
32   - public void descarregarguardar(String u, String nom, String car) {
33   - //Descarrega un arxiu i el guarda
34   - this.nomarxiu = nom;
35   - this.carpeta = car;
36   - this.url = u;
37   - //Creem el directori
38   - File dir = new File(carpeta);
39   - if (!dir.exists()) {
40   - if (!dir.mkdir()) {
41   - return; // No s'ha pogut crear (ja existeix)
42   - }
43   - }
44   - //Creem l'arxiu
45   - arxiu = new File(carpeta + nomarxiu);
46   - //Iniciem la descarrega
47   - try {
48   - URLConnection conn = new URL(url).openConnection();
49   - conn.connect();
50   - Log.d(TAG, "\nDescarregant: \n");
51   - Log.d(TAG, " >> URL: " + url + " >> Nom: " + nomarxiu
52   - + " >> Tamany: " + conn.getContentLength() + " bytes");
53   - //Llegeix cada byte i l'escriu en un arxiu fins que arriba a -1
54   - InputStream in = conn.getInputStream();
55   - OutputStream out = new FileOutputStream(arxiu);
56   - int b = 0;
57   - while (b != -1) {
58   - b = in.read();
59   - if (b != -1) {
60   - out.write(b);
61   - }
  28 + public long descarregarguardar(String url, String path) throws IOException
  29 + {
  30 + // Descarrega un arxiu i el guarda
  31 + // Creem l'arxiu
  32 + File file = new File(path + url.substring(url.lastIndexOf("/")+1, url.length()));
  33 + // Iniciem la descarrega
  34 +
  35 + URLConnection conn = new URL(url).openConnection();
  36 + conn.connect();
  37 + Log.d(TAG, "\nDescarregant: \n");
  38 + Log.d(TAG, " >> URL: " + url + " >> Desti: " + path + " >> Tamany: "
  39 + + conn.getContentLength() + " bytes");
  40 + // Llegeix cada byte i l'escriu en un arxiu fins que arriba a -1
  41 + InputStream in = conn.getInputStream();
  42 + OutputStream out = new FileOutputStream(file);
  43 + int b = 0;
  44 + while (b != -1)
  45 + {
  46 + b = in.read();
  47 + if (b != -1)
  48 + {
  49 + out.write(b);
62 50 }
63   - out.close();
64   - in.close();
65   - } catch (MalformedURLException e) {
66   - e.printStackTrace();
67   - } catch (IOException e) {
68   - e.printStackTrace();
69 51 }
  52 + out.close();
  53 + in.close();
  54 + return conn.getContentLength();
70 55 }
71 56  
72   - protected String doInBackground(String u) throws IOException {
73   - //Descarrega un arxiu i el retorna en un String
74   - this.url = u;
  57 + protected String doInBackground(String url) throws IOException
  58 + {
  59 + // Descarrega un arxiu i el retorna en un String
75 60 html = "";
76   - //Iniciem la connexió i creem els Streams
  61 + // Iniciem la connexi� i creem els Streams
77 62 URLConnection conn = new URL(url).openConnection();
78 63 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
79   - //Iniciem la descarrega i anem guardant al String
  64 + // Iniciem la descarrega i anem guardant al String
80 65 Log.d(TAG, "\nDescarregant: \n");
81   - Log.d(TAG, ">> URL: " + url + " >> Tamany: " + conn.getContentLength()+ " bytes");
  66 + Log.d(TAG, ">> URL: " + url + " >> Tamany: " + conn.getContentLength()
  67 + + " bytes");
82 68 String inputLine;
83   - while ((inputLine = in.readLine()) != null) {
  69 + while ((inputLine = in.readLine()) != null)
  70 + {
84 71 html = html + "\n" + inputLine;
85 72 }
86 73 Log.d(TAG, "Descarrega finalitzada");
... ... @@ -89,8 +76,9 @@ public class Descarrega extends AsyncTask&lt;Object, Object, Object&gt; {
89 76 }
90 77  
91 78 @Override
92   - protected Object doInBackground(Object... arg0) {
93   - //Metode Overrided, no te utilitat
  79 + protected Object doInBackground(Object... arg0)
  80 + {
  81 + // Metode Overrided, no te utilitat
94 82 return null;
95 83 }
96 84 }
... ...
src/com/upc/pbe/upcnews/HLS.java
1 1 package com.upc.pbe.upcnews;
2 2  
3   -
4 3 import java.io.File;
  4 +import java.io.IOException;
5 5 import java.util.ArrayList;
  6 +
  7 +import android.net.TrafficStats;
6 8 import android.util.Log;
7 9  
8 10 //Gestor del protocol HTTP Live Streaming
9   -public class HLS
  11 +public class HLS
10 12 {
11 13 private static final String TAG = "HLS";
12 14 private ArrayList<ParentList> videos;
13 15 private ArrayList<Segment> segments;
14 16 private ArrayList<Video> qualities;
15   - private int currentVideo ;
16   - private int currentQuality; //0 es la mas alta
  17 + private int currentVideo;
  18 + private int currentQuality; // 0 es la mas alta
17 19 private int currentSegment;
18 20 private boolean endReached;
19 21 private String localFolder;
20   - //private FileDownloader fd;
  22 + private Descarrega d;
21 23 private BandwidthMeasurer bm;
22   -
  24 +
23 25 public HLS(ArrayList<ParentList> parsed, String localFolder)
24 26 {
25 27 /*
... ... @@ -27,10 +29,10 @@ public class HLS
27 29 */
28 30 File dir = new File(localFolder);
29 31 String[] files = dir.list();
30   - for(int i = 0; i < files.length; i++)
  32 + for (int i = 0; i < files.length; i++)
31 33 {
32   - File deleteme = new File(dir,files[i]);
33   - Log.d(TAG,"Deleted " + files[i].toString());
  34 + File deleteme = new File(dir, files[i]);
  35 + Log.d(TAG, "Deleted " + files[i].toString());
34 36 deleteme.delete();
35 37 }
36 38 currentVideo = currentQuality = currentSegment = 0;
... ... @@ -38,31 +40,21 @@ public class HLS
38 40 endReached = false;
39 41 this.localFolder = localFolder;
40 42 bm = new BandwidthMeasurer();
41   - //fd = new FileDownloader();
  43 + d = new Descarrega();
42 44 }
43   -
  45 +
44 46 public void loadVideo()
45 47 {
46 48 qualities = videos.get(currentVideo++).getLists();
47 49 segments = qualities.get(currentQuality).getSegments();
48   - if(currentVideo == videos.size())
  50 + if (currentVideo == videos.size())
49 51 {
50 52 endReached = true;
51 53 }
52 54 }
53   -
54   - public String next()
  55 +
  56 + public String next() throws IOException
55 57 {
56   - Segment seg = segments.get(currentSegment++);
57   - Log.d(TAG, seg.getName() + " " + seg.getURL());
58   - long startTime = System.currentTimeMillis();
59   - //long segmentBytes = fd.Download(seg.getURL(),localFolder);
60   - long segmentBytes = 0;
61   - double bps = bm.Measure(segmentBytes,startTime);
62   - if((bps < qualities.get(currentQuality).getQuality()) && (bps != -1))
63   - {
64   - currentQuality++;
65   - }
66 58 if(currentSegment == segments.size())
67 59 {
68 60 if(endReached)
... ... @@ -71,7 +63,17 @@ public class HLS
71 63 }
72 64 loadVideo();
73 65 }
74   -
  66 + Segment seg = segments.get(currentSegment++);
  67 + Log.d(TAG, seg.getName() + " " + seg.getURL());
  68 + long startTime = System.currentTimeMillis();
  69 + long segmentBytes = TrafficStats.getTotalRxBytes();
  70 + d.descarregarguardar(seg.getURL(),localFolder);
  71 + double bps = bm.Measure(segmentBytes,startTime);
  72 + Log.d(TAG, "Current KB/s: " + (bps/8e3));
  73 + if((bps < qualities.get(currentQuality).getQuality()) && (bps != -1))
  74 + {
  75 + currentQuality++;
  76 + }
75 77 return localFolder + seg.getURL().substring(seg.getURL().lastIndexOf("/")+1, seg.getURL().length());
76 78 }
77 79 }
78 80 \ No newline at end of file
... ...
src/com/upc/pbe/upcnews/MainActivity.java
... ... @@ -107,7 +107,7 @@ public class MainActivity extends Activity implements OnClickListener {
107 107 /*
108 108 * COSAS POR HACER
109 109 * Qué coño pasa con los codecs -- Yo
110   - * Descargador de archivos binario -- Marc
  110 + * CRASH AL DARLE A LA TECLA MENU DENTRO DE LAS PREFERENCES -- Marc
111 111 * Poner la defaultURL al server de PBE
112 112 * Unificar la lengua de todo (WERT STYLE FUCK YES) y que cada uno documente minimamente su codigo
113 113 * Borrar esta puta mierda
... ...
src/com/upc/pbe/upcnews/VideoActivity.java
1 1 package com.upc.pbe.upcnews;
2 2  
  3 +import java.io.IOException;
  4 +
3 5 import android.app.Activity;
4 6 import android.media.MediaPlayer;
5 7 import android.net.Uri;
... ... @@ -7,6 +9,7 @@ import android.os.Bundle;
7 9 import android.util.Log;
8 10 import android.view.Menu;
9 11 import android.widget.MediaController;
  12 +import android.widget.Toast;
10 13 import android.widget.VideoView;
11 14  
12 15 public class VideoActivity extends Activity
... ... @@ -24,8 +27,7 @@ public class VideoActivity extends Activity
24 27 {
25 28 public void onCompletion(MediaPlayer mp)
26 29 {
27   - play(null);
28   - //playNext();
  30 + playNext();
29 31 }
30 32 });
31 33 h = ((UpcApp)getApplication()).getHLS();
... ... @@ -40,6 +42,7 @@ public class VideoActivity extends Activity
40 42 super.finish();
41 43 return;
42 44 }
  45 + Log.d(TAG,url);
43 46 video.setVideoURI(Uri.parse(url));
44 47 video.setMediaController(new MediaController(this));
45 48 video.start();
... ... @@ -48,10 +51,15 @@ public class VideoActivity extends Activity
48 51  
49 52 public void playNext()
50 53 {
51   - String next = h.next();
52   - Log.d(TAG,next);
53   - //play(next);
54   - play("/sdcard/a.ts");
  54 + try
  55 + {
  56 + String next = h.next();
  57 + play(next);
  58 + }
  59 + catch(IOException e)
  60 + {
  61 + Toast.makeText(this, "Can't find segment", Toast.LENGTH_LONG).show();
  62 + }
55 63 }
56 64  
57 65 @Override
... ...