package com.github.segator.proxylive.stream;

import com.github.segator.proxylive.config.FFMpegConfiguration;
import com.github.segator.proxylive.config.ProxyLiveConfiguration;
import com.github.segator.proxylive.entity.Channel;

import java.io.IOException;
import java.io.InputStream;


public class FFmpegInputStream extends VideoInputStream {

    private String url;
    private Process process;
    private Thread threadErrorStream;
    private ProxyLiveConfiguration config;
    private InputStream ffmpegInputStream;
    private Channel channel;
    private boolean alive;
    public FFmpegInputStream(String url, Channel channel, ProxyLiveConfiguration config){
        this.url= url;
        this.channel= channel;
        this.config=config;
    }


    @Override
    public boolean isConnected() {
        return process.isAlive();
    }

    @Override
    public boolean connect() throws IOException {
        alive=true;
        String ffmpegCommand =  config.getFfmpeg().getPath() + " -timeout " + config.getSource().getReconnectTimeout() + " -i " +url + " -user-agent \""+config.getUserAgent()+"\" " + (channel.getFfmpegParameters()!=null?channel.getFfmpegParameters():"") + " -codec copy " + config.getFfmpeg().getMpegTS().getParameters() + " -";
        process = Runtime.getRuntime().exec(ffmpegCommand);
        ffmpegInputStream = process.getInputStream();
        threadErrorStream = streamToNull(process.getErrorStream(),process);
        return true;
    }

    @Override
    public int read() throws IOException {
        return ffmpegInputStream.read();
    }

    @Override
    public int read(byte b[]) throws IOException {
        return ffmpegInputStream.read(b);
    }

    public void close() throws IOException {
        if (isConnected()) {
            try {
                if(process.isAlive()) {
                    process.destroy();
                }
            }catch(Exception ex){}
            try{
                if(threadErrorStream.isAlive()) {
                    threadErrorStream.join();
                }
            }catch(Exception ex){}
        } else {
            throw new IOException("The Stream of " + url + " is not connected");
        }
    }

    private Thread  streamToNull(InputStream is,Process proc) {
        Thread t =  new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] buffer = new byte[1024];
                try {
                    while(proc.isAlive()){
                        is.read(buffer);
                        Thread.sleep(10);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        return t;
    }
}

@@ -993,16 +993,19 @@
 () + %22 -
+rw_
 timeout 
@@ -1004,24 +1004,25 @@
 timeout %22 + 
+(
 config.getSo
@@ -1049,16 +1049,22 @@
 imeout()
+*1000)
  + %22 -i 
@@ -1078,49 +1078,8 @@
 + %22 
--user-agent %5C%22%22+config.getUserAgent()+%22%5C%22
  %22 +

Update FFmpegInputStream.java