From 9419e4f97245b66726eaf119f5d0887c9b270f3b Mon Sep 17 00:00:00 2001 From: Equip de Desenvolupadors de PBE Date: Sat, 15 Dec 2012 20:25:22 +0000 Subject: [PATCH] Release Candidate (intentaré implementar reproducción continua, sino, nos quedamos con esto, TODAS LAS MODIFICACIONS SE HARÁN A PARTIR DE AQUÍ --- src/com/upc/pbe/upcnews/BandwidthMeasurer.java | 1 + src/com/upc/pbe/upcnews/HLS.java | 204 +++++++----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- src/com/upc/pbe/upcnews/VideoActivity.java | 219 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- 3 files changed, 204 insertions(+), 220 deletions(-) diff --git a/src/com/upc/pbe/upcnews/BandwidthMeasurer.java b/src/com/upc/pbe/upcnews/BandwidthMeasurer.java index 3912ae5..ea54fb3 100644 --- a/src/com/upc/pbe/upcnews/BandwidthMeasurer.java +++ b/src/com/upc/pbe/upcnews/BandwidthMeasurer.java @@ -18,6 +18,7 @@ public class BandwidthMeasurer Log.d(TAG, "Diferencia de bytes = " + rxDiff); if(rxDiff != 0){ bps = ((rxDiff*8) / (TimeDifference/1000)); // total de rx en bits per second. + Log.d(TAG, "Velocitat actual (KB/s): " + (bps / 8e3)); } else{ bps = -1; //No s'han trasmes dades diff --git a/src/com/upc/pbe/upcnews/HLS.java b/src/com/upc/pbe/upcnews/HLS.java index f914220..de9ba5d 100644 --- a/src/com/upc/pbe/upcnews/HLS.java +++ b/src/com/upc/pbe/upcnews/HLS.java @@ -1,27 +1,15 @@ package com.upc.pbe.upcnews; -import io.vov.vitamio.widget.VideoView; - -import java.io.BufferedInputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; import java.util.ArrayList; -import android.app.AlertDialog; -import android.content.Context; -import android.net.TrafficStats; -import android.os.AsyncTask; import android.util.Log; -import android.view.LayoutInflater; -import android.widget.Toast; //Gestor del protocol HTTP Live Streaming public class HLS { - private static long TRIGGER_PLAY; + private static final String TAG = "HLS"; private ArrayList videos; private ArrayList segments; @@ -30,14 +18,6 @@ public class HLS private int currentQuality; // 0 es la mas alta private int currentSegment; private boolean endReached; - private String localFolder; - private BandwidthMeasurer bm; - private boolean started; - private boolean ended; - private long totalDownloaded; - private VideoView video; - private VideoActivity vActivity; - public HLS(ArrayList parsed, String localFolder) { @@ -54,47 +34,6 @@ public class HLS currentVideo = currentQuality = currentSegment = 0; this.videos = parsed; endReached = false; - this.localFolder = localFolder; - bm = new BandwidthMeasurer(); - started = ended = false; - totalDownloaded = 0; - } - - public void setBufferSize() - { - /* - * Supondremos todos los segmentos de misma duración - */ - if(qualities.get(currentQuality).getQuality() == -1) - { - /* - * No nos dicen la calidad, la pondremos al conocer la longitud del primer segmento - */ - TRIGGER_PLAY = -1; - } - else - { - //15 segundos de cancha para empezar - TRIGGER_PLAY = (long) (15 * qualities.get(currentQuality).getQuality()/8e3); - } - - } - - public void quitPlayer(VideoActivity v) - { - video.stopPlayback(); - v.finish(); - return; - } - - public void setVideoViewer(VideoView v) - { - video = v; - } - - public void setVideoActivity(VideoActivity v) - { - vActivity = v; } public void loadVideo() @@ -106,7 +45,6 @@ public class HLS { endReached = true; } - setBufferSize(); } public String next() throws IOException @@ -121,9 +59,13 @@ public class HLS loadVideo(); } Segment seg = segments.get(currentSegment++); - String path = localFolder+seg.getURL().substring(seg.getURL().lastIndexOf("/") + 1, seg.getURL().length()); Log.d(TAG, seg.getName() + " " + seg.getURL()); - return path; + return seg.getURL(); + } + + public long getCurrentSegmentDuration() + { + return (long) segments.get(currentSegment).getDuration(); } public void updateQuality(long bps) @@ -165,137 +107,5 @@ public class HLS } } - 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()); - } - - public boolean hasEnded() - { - return ended; - } - - public void buffer() - { - video.stopPlayback(); - started = false; - } - - public void downloadSegment(String url) - { - DescarregaSegment ds = new DescarregaSegment(vActivity); - try - { - ds.execute(new URL(url)); - } - catch (MalformedURLException e) - { - e.printStackTrace(); - } - } - public class DescarregaSegment extends AsyncTask - { - final static String TAG = "DescarregaSegment"; - private Context ctx; - private long bps; - AlertDialog alertDialog; - - public DescarregaSegment(Context c) - { - ctx = c; - bps = 0; - AlertDialog.Builder builder = new AlertDialog.Builder(vActivity); - LayoutInflater inflater = vActivity.getLayoutInflater(); - builder.setView(inflater.inflate(R.layout.buffering_dialog,null)); - alertDialog = builder.create(); - } - - - @Override - protected Long doInBackground(URL... urls) - { - if(!started) - { - alertDialog.show(); - } - Long downloaded = Long.valueOf(0); - long startTime = System.currentTimeMillis(); - long segmentBytes = TrafficStats.getTotalRxBytes(); - Log.d(TAG, "Velocitat actual (KB/s): " + (bps / 8e3)); - // Iniciem la connexi� i creem els Streams - try - { - FileOutputStream out = new FileOutputStream(localFolder + "video.ts"); - BufferedInputStream in = new BufferedInputStream(urls[0].openStream()); - Log.d(TAG, "\nDescarregant: \n"); - Log.d(TAG, ">> URL: " + urls[0]); - byte data[] = new byte[102400]; - int count; - while ((count = in.read(data)) != -1) - { - downloaded += count; - out.write(data, 0, count); - } - out.flush(); - out.close(); - in.close(); - bps = (long) bm.Measure(segmentBytes, startTime); - Log.d(TAG, "Descarrega finalitzada"); - } - catch(IOException e) - { - this.cancel(true); - Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show(); - return Long.valueOf(-1); - } - return downloaded; - } - protected void onPostExecute(Long result) - { - totalDownloaded += result; - if(ended = true) - { - quitPlayer((VideoActivity) ctx); - } - if(TRIGGER_PLAY == -1) - { - long Bps = (long) (result/segments.get(currentSegment).getDuration()); - TRIGGER_PLAY = 15*Bps/1000; - } - if(!started && totalDownloaded >= TRIGGER_PLAY) - { - alertDialog.dismiss(); - started = true; - video.start(); - video.requestFocus(); - } - String newSegment = null; - try - { - updateQuality((long) bps); - newSegment = next(); - } - catch(IOException e) - { - Toast.makeText(ctx, "No s'ha trobat el segment", Toast.LENGTH_LONG).show(); - quitPlayer((VideoActivity) ctx); - } - if(newSegment.equals(null)) - { - ended = true; - } - else - { - downloadSegment(newSegment); - } - return; - } - } } \ No newline at end of file diff --git a/src/com/upc/pbe/upcnews/VideoActivity.java b/src/com/upc/pbe/upcnews/VideoActivity.java index c99e3fc..9c1b39f 100644 --- a/src/com/upc/pbe/upcnews/VideoActivity.java +++ b/src/com/upc/pbe/upcnews/VideoActivity.java @@ -1,13 +1,22 @@ package com.upc.pbe.upcnews; import io.vov.vitamio.MediaPlayer; +import io.vov.vitamio.R; import io.vov.vitamio.widget.MediaController; import io.vov.vitamio.widget.VideoView; +import java.io.BufferedInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; import android.app.Activity; +import android.app.ProgressDialog; import android.content.res.Configuration; +import android.net.TrafficStats; +import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; @@ -20,6 +29,13 @@ public class VideoActivity extends Activity { private VideoView video; private HLS h = null; private String filePath; + private BandwidthMeasurer bm; + private ProgressDialog pd; + ArrayList queue; + ArrayList playedQueue; + int currentPos; + boolean ended; + boolean buffering; @Override @@ -33,49 +49,44 @@ public class VideoActivity extends Activity { { return; } - setContentView(R.layout.activity_video); - filePath = ((UpcApp)getApplication()).getLocalPath() + "video.ts"; + currentPos = 0; + queue = new ArrayList(); + playedQueue = new ArrayList(); + buffering = true; + ended = false; + filePath = ((UpcApp)getApplication()).getLocalPath(); video = (VideoView) findViewById(R.id.VideoView1); //Creem un listener associat al fi de l'activitat (el fi de cada ts) io.vov.vitamio.widget.MediaController mc = new MediaController(this); video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { public void onCompletion(MediaPlayer mp) { //Al acabar cada ts, reproduit el seguent - if(h.hasEnded()) - { - finish(); - return; - } - h.buffer(); + playNext(); } }); mc.setOnFFLeftListener(new View.OnClickListener() { public void onClick(View v) { Log.d(TAG, "FFLEFT"); - video.seekTo(-100); + playPrevious(); } }); mc.setOnFFRightListener(new View.OnClickListener() { public void onClick(View v) { Log.d(TAG, "FFRIGHT"); - video.stopPlayback(); playNext(); } }); video.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH); //Creem un gestor HLS, carreguem el video i iniciem la reproduccio h = ((UpcApp)getApplication()).getHLS(); - h.setVideoViewer(video); - h.setVideoActivity(this); h.loadVideo(); - video.stopPlayback(); video.setMediaController(mc); - video.setVideoPath("file://" + filePath); - h.downloadSegment(playNext()); + bm = new BandwidthMeasurer(); + downloadSegment(getNext()); } - public String playNext() { + public String getNext() { try { return h.next(); } @@ -85,16 +96,95 @@ public class VideoActivity extends Activity { return ""; } } - public void playPrevious() { - try { - String prev = h.previous(); - //play(prev); + + public void toggleBuffering(boolean b) + { + if(b) + { + pd = new ProgressDialog(VideoActivity.this); + pd.setCancelable(false); + pd.setMessage(getString(R.string.buffering_text)); + pd.show(); } - catch(IOException e) { - Toast.makeText(this, "No s'ha trobat el segment", Toast.LENGTH_LONG).show(); - super.finish(); + else if(pd != null) + { + pd.dismiss(); + pd = null; + } + } + + public void quitPlayer() + { + video.stopPlayback(); + super.finish(); + return; + } + + public void buffer() + { + buffering = true; + video.suspend(); + } + public void play(String path) + { + toggleBuffering(false); + video.setVideoPath(path); + video.start(); + video.requestFocus(); + } + + public void playNext() + { + if(++currentPos > 0) + { + currentPos = 0; + } + if(currentPos == 0) + { + playedQueue.add(queue.get(0)); + queue.remove(0); + if(ended && queue.isEmpty()) + { + VideoActivity.super.finish(); + return; + } + if(queue.isEmpty()) + { + buffer(); + } + play(queue.get(0)); + } + else if(currentPos < 0) + { + play(playedQueue.get(playedQueue.size() + currentPos)); + } + } + + public void playPrevious() + { + if(playedQueue.isEmpty()) + { + play(queue.get(0)); return; } + else if(playedQueue.size() + (--currentPos) < 0) + { + currentPos++; + } + play(playedQueue.get(playedQueue.size() + currentPos)); + } + + public void downloadSegment(String url) + { + DescarregaSegment ds = new DescarregaSegment(); + try + { + ds.execute(new URL(url)); + } + catch (MalformedURLException e) + { + e.printStackTrace(); + } } @Override @@ -104,4 +194,87 @@ public class VideoActivity extends Activity { super.onConfigurationChanged(newConfig); } + public class DescarregaSegment extends AsyncTask + { + final static String TAG = "DescarregaSegment"; + private long bps; + + public DescarregaSegment() + { + bps = 0; + pd = null; + } + + @Override + protected void onPreExecute() + { + if(queue.isEmpty() && !ended) + { + toggleBuffering(true); + } + } + + @Override + protected Void doInBackground(URL... urls) + { + Long downloaded = Long.valueOf(0); + long startTime = System.currentTimeMillis(); + long segmentBytes = TrafficStats.getTotalRxBytes(); + String fileName = urls[0].toString().substring(urls[0].toString().lastIndexOf("/")); + // Iniciem la connexi� i creem els Streams + try + { + FileOutputStream out = new FileOutputStream(filePath + fileName,true); + BufferedInputStream in = new BufferedInputStream(urls[0].openStream()); + Log.d(TAG, "\nDescarregant: \n"); + Log.d(TAG, ">> URL: " + urls[0]); + byte data[] = new byte[102400]; + int count; + while ((count = in.read(data)) != -1) + { + downloaded += count; + out.write(data, 0, count); + } + out.flush(); + out.close(); + in.close(); + bps = (long) bm.Measure(segmentBytes, startTime); + Log.d(TAG, "Descarrega finalitzada"); + queue.add(filePath + fileName); + } + catch(IOException e) + { + this.cancel(true); + Toast.makeText(VideoActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); + return null; + } + return null; + } + protected void onPostExecute(Void v) + { + if(ended) + { + return; + } + + if(!queue.isEmpty() && buffering) + { + buffering = false; + play(queue.get(0)); + } + String newSegment = null; + h.updateQuality((long) bps); + newSegment = getNext(); + if(newSegment == null) + { + ended = true; + } + else + { + downloadSegment(newSegment); + } + return; + } + } + } \ No newline at end of file -- libgit2 0.22.2