Biblioteca Java - Blame information for rev 23
Subversion Repositories:
(root)/Frameworks and Technologies/RTMPChecker/src/main/java/execprocess/execprocess/StreamChecker.java
Rev | Author | Line No. | Line |
---|---|---|---|
22 | mihai | 1 | package execprocess.execprocess; |
2 | |||
3 | import java.io.IOException; | ||
4 | import java.util.Iterator; | ||
5 | import java.util.LinkedList; | ||
6 | import java.util.List; | ||
7 | |||
8 | import org.apache.commons.exec.*; | ||
9 | |||
23 | mihai | 10 | public class StreamChecker { |
22 | mihai | 11 | |
12 | public static final boolean DEBUG = true; | ||
13 | public final long timeout = 15000; | ||
14 | public final boolean background = false; | ||
15 | |||
16 | public static void main(String[] args) throws Exception { | ||
17 | StreamChecker tr = new StreamChecker(); | ||
18 | /*for(int i=0;i<3;i++){ | ||
19 | Thread t = new Thread(new TestRtmp()); | ||
20 | t.start(); | ||
21 | }*/ | ||
22 | |||
23 | mihai | 23 | if(args.length==0 || args[0] == null) |
24 | { | ||
25 | System.err.println("Stream id must be speciffied."); | ||
26 | System.exit(0); | ||
27 | } | ||
28 | |||
22 | mihai | 29 | System.out.print("Is live ? "); |
23 | mihai | 30 | boolean isLive = tr.checkLiveStream("rtmp://54.172.91.50:1935/05MediaLiveBroadcast/"+args[0]); |
22 | mihai | 31 | |
23 | mihai | 32 | if(isLive) |
33 | System.out.println("STREAM IS LIVE!"); | ||
34 | else | ||
35 | System.out.println("STREAM IS NOT LIVE."); | ||
36 | // System.out.println(isLive); | ||
22 | mihai | 37 | } |
38 | |||
23 | mihai | 39 | /* public void run(){ |
22 | mihai | 40 | try { |
23 | mihai | 41 | checkLiveStream("rtmp://54.172.91.50:1935/05MediaLiveBroadcast/"+args[1]); |
22 | mihai | 42 | } catch (Exception e) { |
43 | e.printStackTrace(); | ||
44 | } | ||
23 | mihai | 45 | }*/ |
22 | mihai | 46 | |
47 | |||
48 | public boolean checkLiveStream(String streamUrl) throws Exception { | ||
49 | |||
50 | RtmpDumpHandler printResult; | ||
51 | CollectingLogOutputStream out = new CollectingLogOutputStream(); | ||
52 | try { | ||
53 | System.out.println("[main] Start checking ..."); | ||
54 | printResult = checkLiveStream(streamUrl, timeout, background,out); | ||
55 | System.out.println("[main] Successfully executed rtmpdump ..."); | ||
56 | } | ||
57 | catch (final Exception e) { | ||
58 | e.printStackTrace(); | ||
59 | |||
60 | throw e; | ||
61 | } | ||
62 | |||
63 | // come back to check the print result | ||
64 | System.out.println("[main] Wait for rtmpdump to complete..."); | ||
65 | printResult.waitFor(); | ||
66 | System.out.println("[main] Job completed ..."); | ||
67 | |||
68 | |||
69 | for (Iterator iterator = out.getLines().iterator(); iterator.hasNext();) { | ||
70 | String type = (String) iterator.next(); | ||
71 | if(DEBUG)System.out.println(type); | ||
72 | if(type.indexOf("Starting Live Stream")!=-1) | ||
73 | return true; | ||
74 | } | ||
75 | return false; | ||
76 | |||
77 | } | ||
78 | |||
79 | |||
80 | private RtmpDumpHandler checkLiveStream(final String streamUrl, final long jobTimeout, boolean backgorund, CollectingLogOutputStream out) | ||
81 | throws IOException { | ||
82 | |||
83 | int exitValue; | ||
84 | ExecuteWatchdog watchdog = null; | ||
85 | RtmpDumpHandler resultHandler; | ||
86 | |||
87 | final CommandLine commandLine = new CommandLine("rtmpdump"); | ||
88 | commandLine.addArgument("-B"); | ||
89 | commandLine.addArgument("-1"); | ||
90 | commandLine.addArgument("-v"); | ||
91 | commandLine.addArgument("-r"); | ||
92 | commandLine.addArgument(streamUrl); | ||
93 | commandLine.addArgument("-o"); | ||
94 | commandLine.addArgument("./out.tmp"); | ||
95 | |||
96 | |||
97 | // create the executor and consider the exitValue '1' as success | ||
98 | final Executor executor = new DefaultExecutor(); | ||
99 | executor.setExitValue(1); | ||
100 | |||
101 | // create a watchdog if requested | ||
102 | if (jobTimeout > 0) { | ||
103 | watchdog = new ExecuteWatchdog(jobTimeout); | ||
104 | executor.setWatchdog(watchdog); | ||
105 | } | ||
106 | |||
107 | PumpStreamHandler psh = new PumpStreamHandler( out ); | ||
108 | executor.setStreamHandler(psh); | ||
109 | |||
110 | // pass a "ExecuteResultHandler" when doing background printing | ||
111 | if (backgorund) { | ||
112 | System.out.println("[rtmp check] Executing non-blocking print job ..."); | ||
113 | resultHandler = new RtmpDumpHandler(watchdog); | ||
114 | executor.execute(commandLine, resultHandler); | ||
115 | } | ||
116 | else { | ||
117 | System.out.println("[rtmp check] Executing blocking print job ..."); | ||
118 | exitValue = executor.execute(commandLine); | ||
119 | resultHandler = new RtmpDumpHandler(exitValue); | ||
120 | } | ||
121 | |||
122 | return resultHandler; | ||
123 | } | ||
124 | |||
125 | |||
126 | private class RtmpDumpHandler extends DefaultExecuteResultHandler { | ||
127 | |||
128 | private ExecuteWatchdog watchdog; | ||
129 | |||
130 | public RtmpDumpHandler(final ExecuteWatchdog watchdog) | ||
131 | { | ||
132 | this.watchdog = watchdog; | ||
133 | } | ||
134 | |||
135 | public RtmpDumpHandler(final int exitValue) { | ||
136 | super.onProcessComplete(exitValue); | ||
137 | } | ||
138 | |||
139 | @Override | ||
140 | public void onProcessComplete(final int exitValue) { | ||
141 | super.onProcessComplete(exitValue); | ||
142 | System.out.println("[resultHandler] The command has been executed ..."); | ||
143 | } | ||
144 | |||
145 | @Override | ||
146 | public void onProcessFailed(final ExecuteException e) { | ||
147 | super.onProcessFailed(e); | ||
148 | if (watchdog != null && watchdog.killedProcess()) { | ||
149 | System.err.println("[resultHandler] Thec command has timedout."); | ||
150 | } | ||
151 | else { | ||
152 | System.err.println("[resultHandler] The command failed with: " + e.getMessage()); | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | } | ||
157 | |||
158 | class CollectingLogOutputStream extends LogOutputStream { | ||
159 | //private final StringBuffer lines = new StringBuffer(); | ||
160 | private final List lines = new LinkedList<String>(); | ||
161 | @Override protected void processLine(String line, int level) { | ||
162 | //System.err.println(line); | ||
163 | //lines.append(line); | ||
164 | lines.add(line); | ||
165 | } | ||
166 | public List<String> getLines() { | ||
167 | return lines; | ||
168 | } | ||
169 | } |