/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package RTP;

import java.awt.Canvas;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JLabel;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface;

/**
 *
 * @author Chilote
 */
public class Cliente {

     private final MediaPlayerFactory mediaPlayerFactory;
     private final EmbeddedMediaPlayer mediaPlayer;
     private final Canvas canvas;
     private final CanvasVideoSurface videoSurface;
     private final JLabel label;
     private final JFrame f;
     Socket s;
     PrintWriter out;
     BufferedReader in;
     int sesion;
     int pause;
     int Cseq = 1; //nueva variable para numero de secuencia rtcp
     String inputLine;
     
     public Cliente() throws IOException{
         canvas = new Canvas();
         canvas.setBackground(Color.black);
         mediaPlayerFactory = new MediaPlayerFactory();
         mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
         videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
         mediaPlayer.setVideoSurface(videoSurface);
         label = new JLabel();
         f = new JFrame("TV");
         f.add(canvas);
         f.setSize(800, 600);
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.setVisible(true);
         sesion= 0;
         pause = 0;
         s = new Socket("localhost",5555);
         out = new PrintWriter(s.getOutputStream(), true);
         in = new BufferedReader(new InputStreamReader(s.getInputStream())); // recibe numero aleatorio
         System.out.println("socket creado");
     }
     
     public void Play() throws IOException{
         System.out.println("enviando PLAY");
         out.println("PLAY bunny.mp4 RTSP/1.0 Cseq:"+ Cseq + " " +"Session:"+ sesion);
         Cseq = Cseq + 1;
         out.flush();
         mediaPlayer.playMedia("rtp://127.0.0.1:5555");
         inputLine = in.readLine();
         System.out.println(inputLine);
     }
     public void Setup() throws IOException{
        System.out.println("enviando SETUP");
        Cseq = 1;
        out.println("SETUP bunny.mp4 RTSP/1.0 Cseq:"+ Cseq + " " + "Transport: RTP/UDP; unicast; client_port= 5555");
        Cseq = Cseq + 1;
        out.flush();
        inputLine = in.readLine();
        System.out.println(inputLine);
        String[] partes = inputLine.split(" ");
        sesion = Integer.parseInt(partes[partes.length-1]);
     }
     public void Pause() throws IOException{
        System.out.println("enviando PAUSE");
        out.println("PAUSE bunny.mp4 RTSP/1.0 Cseq:"+ Cseq + " " +"Session:"+ sesion);
        Cseq = Cseq + 1;
        //out.println("PAUSE");
        out.flush();
        if(pause ==0){
            mediaPlayer.pause();
            pause=1;
        }
        else{
            mediaPlayer.play();
            pause=0;
        }
        inputLine = in.readLine();
        System.out.println(inputLine);
     }
     public void setVol(int vol){
         mediaPlayer.setVolume(vol);
         System.out.println(vol);
     }
     public void Stop() throws IOException{
         System.out.println("enviando TEARDOWN");
         out.println("TEARDOWN bunny.mp4 RTSP/1.0 Cseq:"+ Cseq + " " +"Session:"+ sesion);
         out.flush();
		 inputLine = in.readLine();
         System.out.println(inputLine);
         mediaPlayer.stop();
     }
}
