Biblioteca Java - Blame information for rev 22
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 | |||
10 | public class StreamChecker implements Runnable{ | ||
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 | System.out.print("Is live ? "); | ||
24 | boolean isLive = tr.checkLiveStream("rtmp://XYZYXYXYXYYX"); | ||
25 | |||
26 | System.out.println(isLive); | ||
27 | } | ||
28 | |||
29 | public void run(){ | ||
30 | try { | ||
31 | checkLiveStream("rtmp://XYZYXYXYXYYX"); | ||
32 | } catch (Exception e) { | ||
33 | e.printStackTrace(); | ||
34 | } | ||
35 | } | ||
36 | |||
37 | |||
38 | public boolean checkLiveStream(String streamUrl) throws Exception { | ||
39 | |||
40 | RtmpDumpHandler printResult; | ||
41 | CollectingLogOutputStream out = new CollectingLogOutputStream(); | ||
42 | try { | ||
43 | System.out.println("[main] Start checking ..."); | ||
44 | printResult = checkLiveStream(streamUrl, timeout, background,out); | ||
45 | System.out.println("[main] Successfully executed rtmpdump ..."); | ||
46 | } | ||
47 | catch (final Exception e) { | ||
48 | e.printStackTrace(); | ||
49 | |||
50 | throw e; | ||
51 | } | ||
52 | |||
53 | // come back to check the print result | ||
54 | System.out.println("[main] Wait for rtmpdump to complete..."); | ||
55 | printResult.waitFor(); | ||
56 | System.out.println("[main] Job completed ..."); | ||
57 | |||
58 | |||
59 | for (Iterator iterator = out.getLines().iterator(); iterator.hasNext();) { | ||
60 | String type = (String) iterator.next(); | ||
61 | if(DEBUG)System.out.println(type); | ||
62 | if(type.indexOf("Starting Live Stream")!=-1) | ||
63 | return true; | ||
64 | } | ||
65 | return false; | ||
66 | |||
67 | } | ||
68 | |||
69 | |||
70 | private RtmpDumpHandler checkLiveStream(final String streamUrl, final long jobTimeout, boolean backgorund, CollectingLogOutputStream out) | ||
71 | throws IOException { | ||
72 | |||
73 | int exitValue; | ||
74 | ExecuteWatchdog watchdog = null; | ||
75 | RtmpDumpHandler resultHandler; | ||
76 | |||
77 | final CommandLine commandLine = new CommandLine("rtmpdump"); | ||
78 | commandLine.addArgument("-B"); | ||
79 | commandLine.addArgument("-1"); | ||
80 | commandLine.addArgument("-v"); | ||
81 | commandLine.addArgument("-r"); | ||
82 | commandLine.addArgument(streamUrl); | ||
83 | commandLine.addArgument("-o"); | ||
84 | commandLine.addArgument("./out.tmp"); | ||
85 | |||
86 | |||
87 | // create the executor and consider the exitValue '1' as success | ||
88 | final Executor executor = new DefaultExecutor(); | ||
89 | executor.setExitValue(1); | ||
90 | |||
91 | // create a watchdog if requested | ||
92 | if (jobTimeout > 0) { | ||
93 | watchdog = new ExecuteWatchdog(jobTimeout); | ||
94 | executor.setWatchdog(watchdog); | ||
95 | } | ||
96 | |||
97 | PumpStreamHandler psh = new PumpStreamHandler( out ); | ||
98 | executor.setStreamHandler(psh); | ||
99 | |||
100 | // pass a "ExecuteResultHandler" when doing background printing | ||
101 | if (backgorund) { | ||
102 | System.out.println("[rtmp check] Executing non-blocking print job ..."); | ||
103 | resultHandler = new RtmpDumpHandler(watchdog); | ||
104 | executor.execute(commandLine, resultHandler); | ||
105 | } | ||
106 | else { | ||
107 | System.out.println("[rtmp check] Executing blocking print job ..."); | ||
108 | exitValue = executor.execute(commandLine); | ||
109 | resultHandler = new RtmpDumpHandler(exitValue); | ||
110 | } | ||
111 | |||
112 | return resultHandler; | ||
113 | } | ||
114 | |||
115 | |||
116 | private class RtmpDumpHandler extends DefaultExecuteResultHandler { | ||
117 | |||
118 | private ExecuteWatchdog watchdog; | ||
119 | |||
120 | public RtmpDumpHandler(final ExecuteWatchdog watchdog) | ||
121 | { | ||
122 | this.watchdog = watchdog; | ||
123 | } | ||
124 | |||
125 | public RtmpDumpHandler(final int exitValue) { | ||
126 | super.onProcessComplete(exitValue); | ||
127 | } | ||
128 | |||
129 | @Override | ||
130 | public void onProcessComplete(final int exitValue) { | ||
131 | super.onProcessComplete(exitValue); | ||
132 | System.out.println("[resultHandler] The command has been executed ..."); | ||
133 | } | ||
134 | |||
135 | @Override | ||
136 | public void onProcessFailed(final ExecuteException e) { | ||
137 | super.onProcessFailed(e); | ||
138 | if (watchdog != null && watchdog.killedProcess()) { | ||
139 | System.err.println("[resultHandler] Thec command has timedout."); | ||
140 | } | ||
141 | else { | ||
142 | System.err.println("[resultHandler] The command failed with: " + e.getMessage()); | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | } | ||
147 | |||
148 | class CollectingLogOutputStream extends LogOutputStream { | ||
149 | //private final StringBuffer lines = new StringBuffer(); | ||
150 | private final List lines = new LinkedList<String>(); | ||
151 | @Override protected void processLine(String line, int level) { | ||
152 | //System.err.println(line); | ||
153 | //lines.append(line); | ||
154 | lines.add(line); | ||
155 | } | ||
156 | public List<String> getLines() { | ||
157 | return lines; | ||
158 | } | ||
159 | } |