Biblioteca Java - Rev 23
Subversion Repositories:
(root)/Frameworks and Technologies/RTMPChecker/src/main/java/execprocess/execprocess/StreamChecker.java @ 39
package execprocess.execprocess;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.exec.*;
public class StreamChecker {
public static final boolean DEBUG = true;
public final long timeout = 15000;
public final boolean background = false;
public static void main(String[] args) throws Exception {
StreamChecker tr = new StreamChecker();
/*for(int i=0;i<3;i++){
Thread t = new Thread(new TestRtmp());
t.start();
}*/
if(args.length==0 || args[0] == null)
{
System.err.println("Stream id must be speciffied.");
System.exit(0);
}
System.out.print("Is live ? ");
boolean isLive = tr.checkLiveStream("rtmp://54.172.91.50:1935/05MediaLiveBroadcast/"+args[0]);
if(isLive)
System.out.println("STREAM IS LIVE!");
else
System.out.println("STREAM IS NOT LIVE.");
// System.out.println(isLive);
}
/* public void run(){
try {
checkLiveStream("rtmp://54.172.91.50:1935/05MediaLiveBroadcast/"+args[1]);
} catch (Exception e) {
e.printStackTrace();
}
}*/
public boolean checkLiveStream(String streamUrl) throws Exception {
RtmpDumpHandler printResult;
CollectingLogOutputStream out = new CollectingLogOutputStream();
try {
System.out.println("[main] Start checking ...");
printResult = checkLiveStream(streamUrl, timeout, background,out);
System.out.println("[main] Successfully executed rtmpdump ...");
}
catch (final Exception e) {
e.printStackTrace();
throw e;
}
// come back to check the print result
System.out.println("[main] Wait for rtmpdump to complete...");
printResult.waitFor();
System.out.println("[main] Job completed ...");
for (Iterator iterator = out.getLines().iterator(); iterator.hasNext();) {
String type = (String) iterator.next();
if(DEBUG)System.out.println(type);
if(type.indexOf("Starting Live Stream")!=-1)
return true;
}
return false;
}
private RtmpDumpHandler checkLiveStream(final String streamUrl, final long jobTimeout, boolean backgorund, CollectingLogOutputStream out)
throws IOException {
int exitValue;
ExecuteWatchdog watchdog = null;
RtmpDumpHandler resultHandler;
final CommandLine commandLine = new CommandLine("rtmpdump");
commandLine.addArgument("-B");
commandLine.addArgument("-1");
commandLine.addArgument("-v");
commandLine.addArgument("-r");
commandLine.addArgument(streamUrl);
commandLine.addArgument("-o");
commandLine.addArgument("./out.tmp");
// create the executor and consider the exitValue '1' as success
final Executor executor = new DefaultExecutor();
executor.setExitValue(1);
// create a watchdog if requested
if (jobTimeout > 0) {
watchdog = new ExecuteWatchdog(jobTimeout);
executor.setWatchdog(watchdog);
}
PumpStreamHandler psh = new PumpStreamHandler( out );
executor.setStreamHandler(psh);
// pass a "ExecuteResultHandler" when doing background printing
if (backgorund) {
System.out.println("[rtmp check] Executing non-blocking print job ...");
resultHandler = new RtmpDumpHandler(watchdog);
executor.execute(commandLine, resultHandler);
}
else {
System.out.println("[rtmp check] Executing blocking print job ...");
exitValue = executor.execute(commandLine);
resultHandler = new RtmpDumpHandler(exitValue);
}
return resultHandler;
}
private class RtmpDumpHandler extends DefaultExecuteResultHandler {
private ExecuteWatchdog watchdog;
public RtmpDumpHandler(final ExecuteWatchdog watchdog)
{
this.watchdog = watchdog;
}
public RtmpDumpHandler(final int exitValue) {
super.onProcessComplete(exitValue);
}
@Override
public void onProcessComplete(final int exitValue) {
super.onProcessComplete(exitValue);
System.out.println("[resultHandler] The command has been executed ...");
}
@Override
public void onProcessFailed(final ExecuteException e) {
super.onProcessFailed(e);
if (watchdog != null && watchdog.killedProcess()) {
System.err.println("[resultHandler] Thec command has timedout.");
}
else {
System.err.println("[resultHandler] The command failed with: " + e.getMessage());
}
}
}
}
class CollectingLogOutputStream extends LogOutputStream {
//private final StringBuffer lines = new StringBuffer();
private final List lines = new LinkedList<String>();
@Override protected void processLine(String line, int level) {
//System.err.println(line);
//lines.append(line);
lines.add(line);
}
public List<String> getLines() {
return lines;
}
}
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.exec.*;
public class StreamChecker {
public static final boolean DEBUG = true;
public final long timeout = 15000;
public final boolean background = false;
public static void main(String[] args) throws Exception {
StreamChecker tr = new StreamChecker();
/*for(int i=0;i<3;i++){
Thread t = new Thread(new TestRtmp());
t.start();
}*/
if(args.length==0 || args[0] == null)
{
System.err.println("Stream id must be speciffied.");
System.exit(0);
}
System.out.print("Is live ? ");
boolean isLive = tr.checkLiveStream("rtmp://54.172.91.50:1935/05MediaLiveBroadcast/"+args[0]);
if(isLive)
System.out.println("STREAM IS LIVE!");
else
System.out.println("STREAM IS NOT LIVE.");
// System.out.println(isLive);
}
/* public void run(){
try {
checkLiveStream("rtmp://54.172.91.50:1935/05MediaLiveBroadcast/"+args[1]);
} catch (Exception e) {
e.printStackTrace();
}
}*/
public boolean checkLiveStream(String streamUrl) throws Exception {
RtmpDumpHandler printResult;
CollectingLogOutputStream out = new CollectingLogOutputStream();
try {
System.out.println("[main] Start checking ...");
printResult = checkLiveStream(streamUrl, timeout, background,out);
System.out.println("[main] Successfully executed rtmpdump ...");
}
catch (final Exception e) {
e.printStackTrace();
throw e;
}
// come back to check the print result
System.out.println("[main] Wait for rtmpdump to complete...");
printResult.waitFor();
System.out.println("[main] Job completed ...");
for (Iterator iterator = out.getLines().iterator(); iterator.hasNext();) {
String type = (String) iterator.next();
if(DEBUG)System.out.println(type);
if(type.indexOf("Starting Live Stream")!=-1)
return true;
}
return false;
}
private RtmpDumpHandler checkLiveStream(final String streamUrl, final long jobTimeout, boolean backgorund, CollectingLogOutputStream out)
throws IOException {
int exitValue;
ExecuteWatchdog watchdog = null;
RtmpDumpHandler resultHandler;
final CommandLine commandLine = new CommandLine("rtmpdump");
commandLine.addArgument("-B");
commandLine.addArgument("-1");
commandLine.addArgument("-v");
commandLine.addArgument("-r");
commandLine.addArgument(streamUrl);
commandLine.addArgument("-o");
commandLine.addArgument("./out.tmp");
// create the executor and consider the exitValue '1' as success
final Executor executor = new DefaultExecutor();
executor.setExitValue(1);
// create a watchdog if requested
if (jobTimeout > 0) {
watchdog = new ExecuteWatchdog(jobTimeout);
executor.setWatchdog(watchdog);
}
PumpStreamHandler psh = new PumpStreamHandler( out );
executor.setStreamHandler(psh);
// pass a "ExecuteResultHandler" when doing background printing
if (backgorund) {
System.out.println("[rtmp check] Executing non-blocking print job ...");
resultHandler = new RtmpDumpHandler(watchdog);
executor.execute(commandLine, resultHandler);
}
else {
System.out.println("[rtmp check] Executing blocking print job ...");
exitValue = executor.execute(commandLine);
resultHandler = new RtmpDumpHandler(exitValue);
}
return resultHandler;
}
private class RtmpDumpHandler extends DefaultExecuteResultHandler {
private ExecuteWatchdog watchdog;
public RtmpDumpHandler(final ExecuteWatchdog watchdog)
{
this.watchdog = watchdog;
}
public RtmpDumpHandler(final int exitValue) {
super.onProcessComplete(exitValue);
}
@Override
public void onProcessComplete(final int exitValue) {
super.onProcessComplete(exitValue);
System.out.println("[resultHandler] The command has been executed ...");
}
@Override
public void onProcessFailed(final ExecuteException e) {
super.onProcessFailed(e);
if (watchdog != null && watchdog.killedProcess()) {
System.err.println("[resultHandler] Thec command has timedout.");
}
else {
System.err.println("[resultHandler] The command failed with: " + e.getMessage());
}
}
}
}
class CollectingLogOutputStream extends LogOutputStream {
//private final StringBuffer lines = new StringBuffer();
private final List lines = new LinkedList<String>();
@Override protected void processLine(String line, int level) {
//System.err.println(line);
//lines.append(line);
lines.add(line);
}
public List<String> getLines() {
return lines;
}
}