
/*
 * This file is part of VLCJ.
 *
 * VLCJ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VLCJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VLCJ.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2009, 2010, 2011, 2012, 2013 Caprica Software Limited.
 */

package RTP;

import com.sun.jna.NativeLibrary;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import static java.lang.System.exit;
import java.net.ServerSocket;
import java.net.Socket;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.headless.HeadlessMediaPlayer;


public class StreamRtp {

    public static void main(String[] args) throws Exception {

        NativeLibrary.addSearchPath("libvlc", "C:\\Program Files\\VideoLAN\\VLC");
        String[] partes;
        String options = null;
	String media = null;
        String puerto;
        int pause=0;
        int Cseq = 1; 
        int session = 1;
        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
        HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
        ServerSocket serverSocket = new ServerSocket(5555);
        Socket clientSocket = serverSocket.accept();
        System.out.println("accepted");
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine;
        System.out.println("listening");
        while((inputLine = in.readLine()) != null){
            System.out.println(inputLine);
            partes = inputLine.split(" ");

            if(partes[0].equals("SETUP")){
                Cseq = 1;
                puerto = partes[8];
                media = partes[1];
                partes = clientSocket.getInetAddress().toString().split("/");
                System.out.println("media = " + media);
                System.out.println("ip = " + partes[1]);
                System.out.println("puerto = " + puerto);
                session = 100000 + (int)(Math.random()*200000);
                out.println("RTSP/1.0 200 OK; CSeq:" + Cseq + " Session: " + session);
                Cseq = Cseq + 1;
                options = formatRtpStream(partes[1],Integer.parseInt(puerto));
            }
            
            if(partes[0].equals("PLAY")){
                mediaPlayer.playMedia(media, options); 
                out.println("RTSP/1.0 200 OK; CSeq:" + Cseq + " Session:" + session);
                Cseq = Cseq + 1;
            }
            
            if(partes[0].equals("PAUSE")){
                if(pause==0){
                    mediaPlayer.pause();
                    pause=1;
                }
                else{
                    mediaPlayer.play();
                    pause=0;
                }
               out.println("RTSP/1.0 200 OK; CSeq:" + Cseq + " Session:" + session);
               Cseq = Cseq + 1;
            }
            
            if(partes[0].equals("TEARDOWN")){
                clientSocket.close();
                serverSocket.close();
                exit(0);
            }
                
        }
    }

    private static String formatRtpStream(String serverAddress, int serverPort) {
        StringBuilder sb = new StringBuilder(60);
        sb.append(":sout=#rtp{dst=");
        sb.append(serverAddress);
        sb.append(",port=");
        sb.append(serverPort);
        sb.append(",mux=ts}");
        return sb.toString();
    }

}