Parent: [a7a9ff] (diff)

Download this file

RemoteControlledMediaPlayer.java    275 lines (242 with data), 9.5 kB

  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
/*
* Copyright 2013-2014 TECO - Karlsruhe Institute of Technology.
*
* This file is part of TACET.
*
* TACET is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TACET is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TACET. If not, see <http://www.gnu.org/licenses/>.
*/
package squirrel.model.video;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import squirrel.controller.video.RemoteControlled;
import squirrel.controller.video.RemoteController;
import squirrel.view.video.UpdateView;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.videosurface.VideoSurfaceAdapter;
import uk.co.caprica.vlcj.player.embedded.videosurface.linux.LinuxVideoSurfaceAdapter;
import uk.co.caprica.vlcj.player.embedded.videosurface.mac.MacVideoSurfaceAdapter;
import uk.co.caprica.vlcj.player.embedded.videosurface.windows.WindowsVideoSurfaceAdapter;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import uk.co.caprica.vlcj.version.LibVlcVersion;
/**
* The Class RemoteControlledMediaPlayer.
*/
public class RemoteControlledMediaPlayer extends MyMediaPlayer implements
RemoteControlled {
private float defaultSpeed = 1;
private long maxTime;
private long defaultStartTime = 0;
private long minDeltaTime = 200;
private final ScheduledExecutorService executorService = Executors
.newSingleThreadScheduledExecutor();
private static RemoteController controller;
private UpdateView update;
/**
* Instantiates a new remote controlled media player which plays the media
* on a canvas placed in a different JVM.
*
* @param canvasId the id of the canvas in the other JVM
*/
public RemoteControlledMediaPlayer(long canvasId) {
super();
configureMP(canvasId);
update = new UpdateView(mediaPlayer, controller);
executorService.scheduleAtFixedRate(update, 0L, 1L, TimeUnit.SECONDS);
}
private void configureMP(long canvasId) {
VideoSurfaceAdapter videoSurfaceAdapter = null;
if (RuntimeUtil.isNix()) {
videoSurfaceAdapter = new LinuxVideoSurfaceAdapter();
} else if (RuntimeUtil.isWindows()) {
videoSurfaceAdapter = new WindowsVideoSurfaceAdapter();
} else if (RuntimeUtil.isMac()) {
videoSurfaceAdapter = new MacVideoSurfaceAdapter();
}
videoSurfaceAdapter.attach(LibVlc.INSTANCE, mediaPlayer, canvasId);
}
/**
* The main method. Creates the connection between the JVMs, and creates the
* mediaplayer. canvasID is the ID of the canvas to be painted to,
* mediaPalyerID is the unique identifier of the mediaplayer, solo shows
* whether the mediaplayer is controlled by a MediaPlayerControlsController
* (1) or a MainController(0).
*
* @param args [int mediaplayerID, boolean solo, long canvasID, ]
*/
public static void main(String args[]) {
String controllerName;
String name;
new NativeDiscovery().discover();
try {
LibVlcVersion.getVersion();
} catch (Exception e) {
e.printStackTrace();
}
if (args[1].equals("1")) {
controllerName = "Controller" + args[0];
name = "MediaPlayer" + args[0];
} else {
controllerName = "Controller";
name = "MediaPlayer0" + args[0];
}
try {
Registry registry = LocateRegistry.getRegistry(1099);
controller = (RemoteController) registry.lookup(controllerName);
controller.printMsg(name + " Client to " + controllerName + " Server connected");
} catch (Exception ex) {
System.err
.println("MediaPlayer Client exception: " + ex.toString());
ex.printStackTrace();
}
RemoteControlledMediaPlayer mp = null;
if(args.length == 3) {
mp = new RemoteControlledMediaPlayer(Long.parseLong(args[2]));
try {
controller.printMsg(args[2]);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
mp = new RemoteControlledMediaPlayer(controller.getCanvasID());
} catch (RemoteException e) {
e.printStackTrace();
}
}
try {
RemoteControlled stub = (RemoteControlled) UnicastRemoteObject
.exportObject(mp, 0);
Registry registry = LocateRegistry.getRegistry(1099);
registry.bind(name, stub);
controller.printMsg(name + " Server Ready");
} catch (Exception ex) {
System.err
.println("MediaPlayer Server exception: " + ex.toString());
ex.printStackTrace();
}
try {
if (args[1].equals("1")) {
controller.startConnection();
} else {
int playerNum = Integer.parseInt(args[0]);
controller.startConnection(playerNum);
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void setDefaultSpeed(float rate) throws RemoteException {
this.defaultSpeed = rate;
mediaPlayer.setRate(defaultSpeed);
}
@Override
public void setDefaultStartTime(long timeStamp) throws RemoteException {
this.defaultStartTime = timeStamp;
controller.printMsg(Long.toString(timeStamp));
}
@Override
public void setTime(long timeStamp) throws IOException {
long calculatedTime = Math.round((timeStamp - defaultStartTime) * defaultSpeed);
if (calculatedTime > getLength() || timeStamp < 0 || timeStamp > maxTime || calculatedTime < 0) {
mediaPlayer.stop();
} else {
mediaPlayer.setTime(calculatedTime);
}
}
@Override
public void setPosition(float position) throws RemoteException {
mediaPlayer.setPosition(position);
}
@Override
public float getPosition() throws RemoteException {
return mediaPlayer.getPosition();
}
@Override
public void killProcess() throws RemoteException {
update.setKill(true);
}
@Override
public void nextTS() throws IOException {
long canlculatedTime =Math.round(mediaPlayer.getTime()/defaultSpeed) + defaultStartTime;
setTime(canlculatedTime + minDeltaTime - 15);
mediaPlayer.pause();
}
@Override
public void prevTS() throws IOException {
long canlculatedTime =Math.round(mediaPlayer.getTime()/defaultSpeed) + defaultStartTime;
setTime(canlculatedTime - this.minDeltaTime - 15);
mediaPlayer.pause();
}
@Override
public void setMinDeltaTime(long deltaTime) throws RemoteException {
this.minDeltaTime = deltaTime;
controller.printMsg(mediaPath);
}
@Override
public void nextFrame() throws RemoteException {
float fps = mediaPlayer.getFps();
long newTime = Math.round(mediaPlayer.getTime() + (1000 / fps) - 15);
mediaPlayer.setTime(newTime);
mediaPlayer.pause();
}
@Override
public void previousFrame() throws RemoteException {
float fps = mediaPlayer.getFps();
long newTime = Math.round(mediaPlayer.getTime() - (1000 / fps) - 15);
mediaPlayer.setTime(newTime);
mediaPlayer.pause();
}
@Override
public boolean isPlaying() throws RemoteException {
return mediaPlayer.isPlaying();
}
@Override
public void setMaxTime(long maxTime) throws RemoteException {
this.maxTime = maxTime;
}
@Override
public void restoreMediaPlayer(long canvasID, boolean isPlaying, long timeStamp) throws IOException {
mediaPlayer.stop();
mediaPlayer = mpf.newEmbeddedMediaPlayer();
configureMP(canvasID);
update = new UpdateView(mediaPlayer, controller);
executorService.scheduleAtFixedRate(update, 0L, 1L, TimeUnit.SECONDS);
mediaPlayer.prepareMedia(mediaPath);
if(isPlaying) {
mediaPlayer.play();
setTime(timeStamp);
// } else {
// mediaPlayer.play();
// setTime(timeStamp);
// try {
// Thread.sleep(50);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// mediaPlayer.pause();
}
}
}