summaryrefslogtreecommitdiff
path: root/src/YalpOutputs/YalpVlcTelnetOutput/YalpOutputPluginImpl.java
blob: f95324e399347cd736dce69ad187a9c325a59958 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Copyright (c) 2008 Manuel Traut and Volker Dahnke
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 *
 * Contributors: Manuel Traut
 */

package YalpOutputs.YalpVlcTelnetOutput;

import YalpInterfaces.*;

import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

import java.util.*;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/*
 * Class YalpOutputPluginImpl
 *
 * <em>Implements VLC Telnet Streamer as YALP Plugin</em>
 *
 * @author Volker Dahnke / Manuel Traut
 * @version 0.7 10-09-2008
 */
public class YalpOutputPluginImpl extends OutputPluginInterfacePOA {

	private ORB orb;
	private static Process vlcPlayer = null;
	private StreamCounter streamCounter= new StreamCounter();
	private LinkedList<Output> currentStreams = new LinkedList<Output>();
	private int startPort;
	private String hostIP;
	private String vlcCommand;
	private ServerControlInterface srvCon;
	private PluginInfo pluginInfo;

	private String log4jFile = "log4j_output_plugin.conf";

	private static Logger logger =
		Logger.getLogger("Yalp.OutputPlugins.VlcTelnetOutput.YalpOutputPluginImpl");

	public YalpOutputPluginImpl()
	{
		PropertyConfigurator.configureAndWatch(log4jFile);
		logger.debug("YalpOutputPluginImpl()");
	}

/*
 * starts a vlc player in a new process
 *
 * @param serverIP
 * 		where vlcStreamer is running
 * @param startport
 * 		first port used for streaming
 * @param vlcCmd
 * 		path and name of vlc player executable
 *
 * @throws RemoteException
 * @throws ClassNotFoundException
 */
	public void setORB(ORB _orb) {

		logger.debug("setORB()");

		orb = _orb;

		pluginInfo = new PluginInfo();
		pluginInfo.name = "VLC Telnet Streamer";
		pluginInfo.description = "creates streams via VLC players telnet interface";
		pluginInfo.type = PluginType.OUTPUT_PLUGIN;
		pluginInfo.access = new AccessInfo();
		pluginInfo.access.name = "stream provided by vlc player";
		pluginInfo.access.description = "streams can be displayed with vlc player";
		pluginInfo.access.executable = "vlc";
		pluginInfo.access.params = "";
		pluginInfo.access.type = AccessType.STREAM;

		/* t.b.d. read vlccmod, startport, hostip from config xml */
		String vlcCmd = "/usr/bin/vlc";
		vlcCommand = vlcCmd + " --reset-config -I telnet";
		startPort = 9000;
		hostIP = "127.0.0.1";

		if(vlcPlayer == null){
			try{
				vlcPlayer = Runtime.getRuntime().exec(this.vlcCommand);
			} catch(IOException e){
				System.out.println("server.VlcStreamer.java: Streamingserver failed");
			}
		}
	}

	public void shutdown() {
		logger.debug("shutdown()");
		/* t.b.d. */
	}

/*
 * controls Streaming (start, stop, etc)
 *
 * @param howtoStream
 *    specifies streaming options
 */
	public void control(OutputHolder howtoStream, YalpErrorHolder err) {

		logger.debug("control(howtoStream.value.id: "+howtoStream.value.id+")");

		YalpError error = new YalpError();
		error.descr = "";
		error.msg = "";
		error.code = YalpErrorCode.OK;
		error.level = YalpErrorLevel.ERROR_LEVEL_DEBUG;

		err.value = error;

		Output output = new Output();
		output = howtoStream.value;
		howtoStream.value = output;

		switch( howtoStream.value.outputAction.value() ) {

			case Action._START:
				start(howtoStream.value);
				break;

			case Action._STOP:
				stop(howtoStream.value);
				break;

			case Action._PAUSE:
				pause(howtoStream.value);
				break;

			case Action._PLAY:
				play(howtoStream.value);
				break;

			case Action._FORWARD:
				next(howtoStream.value);
				break;

			//add sth. to current playing stream
			case Action._CREATE:
				add(howtoStream.value);
				break;

			default:
				System.out.println("server.VlcStreamer.control: action not supported");
				break;
		}
	}

/*
 * get plugin infos
 *
 * @param PluginInfoHolder (out)
 * @param YalpErrorHolder (out)
 */
	public void getInfo(PluginInfoHolder info, YalpErrorHolder err)
	{
		logger.debug("getInfo()");
		info = new PluginInfoHolder();
		err = new YalpErrorHolder();
		err.value.code = YalpErrorCode.OK;
		info.value = pluginInfo;
	}

/*
 * register Stream at vlcPlayer
 * @param howtoStream
 */
	private void add(Output howtoStream){

		logger.debug("add()");

		String input = "";
		TelnetInterface telnet = new TelnetInterface(hostIP, 4212, "admin");

		input += "new "+howtoStream.info.name+" broadcast enabled";

		for(int i=0; i < howtoStream.playlist.length; i++){
			input += " input file://";
			input += howtoStream.playlist[i].path + "/";
			input += howtoStream.playlist[i].fileName;
		}

		telnet.exec(input);
		telnet.close();
}

/*
 * request Streaming of submitted Output
 *
 * @param stream
 * 		which should be streamed
 * @return Output
 * 		extends Information "how to receive" of submitted Output
 */
	public Output request(Output stream) {

		logger.debug("request()");

		//stream.setIP(hostIP);     for UDP
		stream.info.name = streamName();
		stream.info.params = new Integer(++startPort).toString();
		currentStreams.add(stream);
		return stream;
	}


/*
 * starts streaming of submitted streamInfo
 * @param howtoStream
 */
	private void start(Output howtoStream) {

		logger.debug("start()");

		String newString, setup2;
		streamCounter.actualStreams++;
		streamCounter.allStreams++;

		switch (howtoStream.info.type.value()) {
			case AccessType._STREAM:
				newString  = "control "+howtoStream.info.name+" broadcast enabled";
				break;

			default:
				System.out.println("server.VlcStreamer.start: unsupported Type");
				return;
		}

		String comp = "#transcode{vcodec=DIV3,vb=256,scale=1,";
		comp += "acodec=mpga,ab=192,channels=2}";
		comp += ":duplicate{dst=";

		setup2 = "setup "+howtoStream.info.name+" output "+comp+"std{access=udp,";
		setup2 +="mux=ts,";
		setup2 +="dst=" + howtoStream.destIp + ":9993";// + howtoStream.info.params;
		setup2 += "}";

		String control = "control "+howtoStream.info.name+" play";

		TelnetInterface telnet = new TelnetInterface(hostIP, 4212, "admin");
		telnet.exec(newString);
		telnet.exec(setup2);
		telnet.exec(control);
		telnet.close();
	}

/*
 * plays submitted streamInfo
 * @param howtoStream
 */
	private void play(Output howtoStream){

		logger.debug("play()");

		String control = "control "+howtoStream.info.name+" play";
		// telnet connection
		TelnetInterface telnet = new TelnetInterface(hostIP, 4212, "admin");
		telnet.exec(control);
		// close telnet connection
		telnet.close();
	}

/*
 * pause submitted streamInfo
 * @param howtoStream
 */
	private void pause(Output howtoStream){

		logger.debug("pause()");

		String control = "control "+howtoStream.info.name+" pause";
		logger.debug("client.VlcStreamer: Telnetcmd: " + control);

		TelnetInterface telnet = new TelnetInterface(hostIP, 4212, "admin");
		telnet.exec(control);
		telnet.close();
	}

/*
 * go to next file in playlist
 *
 * @param howtoStream
 */
	private void next(Output howtoStream){

		logger.debug("next()");

		String control = "control "+howtoStream.info.name+" seek 100";
		logger.debug("client.VlcStreamer: Telnetcmd: " + control);
		TelnetInterface telnet = new TelnetInterface(hostIP, 4212, "admin");
		telnet.exec(control);
		telnet.close();
	}

/*
 * stop streaming, delete stream
 *
 * @param howtoStream
 */
	private void stop(Output howtoStream){

		logger.debug("stop()");

		String del = "del "+howtoStream.info.name;
		this.streamCounter.actualStreams--;
		TelnetInterface telnet = new TelnetInterface(hostIP, 4212, "admin");
		telnet.exec(del);
		telnet.close();
		currentStreams.remove(howtoStream);
	}

/*
 * calculates streamName
 * @return String streamName
 */
	private String streamName(){
		logger.debug("streamName()");
		String streamName = "stream";
		streamName += streamCounter.allStreams;
		return streamName;
	}

/*
 *  stops the streaming server
 *
 */
	public void quit(){
		logger.debug("quit()");
		vlcPlayer.destroy();
	}

/*
 * returns the streamcounter
 * @return StreamCounter
 */
	public StreamCounter getStreamCounter(){
		logger.debug("getStreamCounter()");
		return this.streamCounter;
	}

}