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() + " -i \"" +url + "\" " + (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;
    }
}

@@ -43,72 +43,8 @@
 m;%0A%0A
-import com.github.segator.proxylive.config.FFMpegConfiguration;%0A
 impo
@@ -102,24 +102,24 @@
 figuration;%0A
+
 import com.g
@@ -1238,28 +1238,30 @@
 tream = 
-streamToNull
+printErrStream
 (process
@@ -2063,21 +2063,22 @@
 ead 
- streamToNull
+printErrStream
 (Inp
@@ -2089,16 +2089,17 @@
 ream is,
+ 
 Process 
@@ -2344,16 +2344,24 @@
         
+ int r =
  is.read
@@ -2370,16 +2370,151 @@
 uffer);%0A
+                        if(r %3E 0 ) %7B%0A                            System.out.print(new String(buffer, 0, r));%0A                        %7D%0A
         

fix rtmp command