1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package example.mediarenderer;
16
17 import org.fourthline.cling.controlpoint.ActionCallback;
18 import org.fourthline.cling.model.action.ActionInvocation;
19 import org.fourthline.cling.model.message.UpnpResponse;
20 import org.fourthline.cling.model.meta.LocalService;
21 import org.fourthline.cling.support.avtransport.callback.GetCurrentTransportActions;
22 import org.fourthline.cling.support.avtransport.callback.GetDeviceCapabilities;
23 import org.fourthline.cling.support.avtransport.callback.GetMediaInfo;
24 import org.fourthline.cling.support.avtransport.callback.GetPositionInfo;
25 import org.fourthline.cling.support.avtransport.callback.GetTransportInfo;
26 import org.fourthline.cling.support.avtransport.callback.Play;
27 import org.fourthline.cling.support.avtransport.callback.SetAVTransportURI;
28 import org.fourthline.cling.support.avtransport.callback.Stop;
29 import org.fourthline.cling.support.avtransport.impl.AVTransportService;
30 import org.fourthline.cling.support.avtransport.lastchange.AVTransportLastChangeParser;
31 import org.fourthline.cling.support.avtransport.lastchange.AVTransportVariable;
32 import org.fourthline.cling.support.lastchange.LastChange;
33 import org.fourthline.cling.support.lastchange.LastChangeAwareServiceManager;
34 import org.fourthline.cling.support.model.DeviceCapabilities;
35 import org.fourthline.cling.support.model.MediaInfo;
36 import org.fourthline.cling.support.model.PositionInfo;
37 import org.fourthline.cling.support.model.TransportAction;
38 import org.fourthline.cling.support.model.TransportInfo;
39 import org.fourthline.cling.support.model.TransportState;
40 import org.testng.annotations.Test;
41
42 import java.beans.PropertyChangeEvent;
43 import java.beans.PropertyChangeListener;
44 import java.beans.PropertyChangeSupport;
45 import java.net.URI;
46 import java.util.Arrays;
47 import java.util.List;
48
49 import static org.testng.Assert.assertEquals;
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 public class AVTransportTest {
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 @Test
159 public void testCustomPlayer() throws Exception {
160
161 LocalService<AVTransportService> service = MediaRendererSampleData.createAVTransportService();
162
163
164 final String[] lcValue = new String[1];
165 PropertyChangeSupport pcs = service.getManager().getPropertyChangeSupport();
166 pcs.addPropertyChangeListener(new PropertyChangeListener() {
167 public void propertyChange(PropertyChangeEvent ev) {
168 if (ev.getPropertyName().equals("LastChange"))
169 lcValue[0] = (String) ev.getNewValue();
170 }
171 });
172
173 final boolean[] assertions = new boolean[5];
174
175 ActionCallback getDeviceCapsAction =
176 new GetDeviceCapabilities(service) {
177 @Override
178 public void received(ActionInvocation actionInvocation, DeviceCapabilities caps) {
179 assertEquals(caps.getPlayMedia()[0].toString(), "NETWORK");
180 assertEquals(caps.getRecMedia()[0].toString(), "NOT_IMPLEMENTED");
181 assertEquals(caps.getRecQualityModes()[0].toString(), "NOT_IMPLEMENTED");
182 assertions[0] = true;
183 }
184
185 @Override
186 public void failure(ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
187
188 }
189 };
190 getDeviceCapsAction.run();
191
192 ActionCallback setAVTransportURIAction =
193 new SetAVTransportURI(service, "http://10.0.0.1/file.mp3", "NO METADATA") {
194 @Override
195 public void failure(ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
196
197 }
198 };
199 setAVTransportURIAction.run();
200
201 ActionCallback getTransportInfo =
202 new GetTransportInfo(service) {
203 @Override
204 public void received(ActionInvocation invocation, TransportInfo transportInfo) {
205 assertEquals(transportInfo.getCurrentTransportState(), TransportState.STOPPED);
206 assertions[1] = true;
207 }
208
209 @Override
210 public void failure(ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
211
212 }
213 };
214 getTransportInfo.run();
215
216 ActionCallback getMediaInfoAction =
217 new GetMediaInfo(service) {
218 @Override
219 public void received(ActionInvocation invocation, MediaInfo mediaInfo) {
220 assertEquals(mediaInfo.getCurrentURI(), "http://10.0.0.1/file.mp3");
221 assertEquals(mediaInfo.getCurrentURIMetaData(), "NO METADATA");
222 assertions[2] = true;
223 }
224
225 @Override
226 public void failure(ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
227
228 }
229 };
230 getMediaInfoAction.run();
231
232 ActionCallback getPositionInfoAction =
233 new GetPositionInfo(service) {
234 @Override
235 public void received(ActionInvocation invocation, PositionInfo positionInfo) {
236 assertEquals(positionInfo.getTrackURI(), "http://10.0.0.1/file.mp3");
237 assertEquals(positionInfo.getTrackMetaData(), "NO METADATA");
238 assertions[3] = true;
239 }
240
241 @Override
242 public void failure(ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
243
244 System.err.println(defaultMsg);
245 }
246 };
247 getPositionInfoAction.run();
248
249 ActionCallback getCurrentTransportActions =
250 new GetCurrentTransportActions(service) {
251 @Override
252 public void received(ActionInvocation invocation, TransportAction[] actions) {
253 List<TransportAction> currentActions = Arrays.asList(actions);
254 assert currentActions.contains(TransportAction.Play);
255 assert currentActions.contains(TransportAction.Stop);
256 assert currentActions.contains(TransportAction.Seek);
257 assertions[4] = true;
258 }
259
260 @Override
261 public void failure(ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
262
263 }
264 };
265 getCurrentTransportActions.run();
266
267 LastChangeAwareServiceManager manager = (LastChangeAwareServiceManager)service.getManager();
268 manager.fireLastChange();
269
270
271 String lastChangeString = lcValue[0];
272 LastChange lastChange = new LastChange(
273 new AVTransportLastChangeParser(),
274 lastChangeString
275 );
276 assertEquals(
277 lastChange.getEventedValue(
278 0,
279 AVTransportVariable.AVTransportURI.class
280 ).getValue(),
281 URI.create("http://10.0.0.1/file.mp3")
282 );
283 assertEquals(
284 lastChange.getEventedValue(
285 0,
286 AVTransportVariable.CurrentTrackURI.class
287 ).getValue(),
288 URI.create("http://10.0.0.1/file.mp3")
289 );
290 assertEquals(
291 lastChange.getEventedValue(
292 0,
293 AVTransportVariable.TransportState.class
294 ).getValue(),
295 TransportState.STOPPED
296 );
297
298 ActionCallback playAction =
299 new Play(service) {
300 @Override
301 public void failure(ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
302
303 }
304 };
305 playAction.run();
306
307 manager.fireLastChange();
308
309 lastChangeString = lcValue[0];
310 lastChange = new LastChange(
311 new AVTransportLastChangeParser(),
312 lastChangeString
313 );
314 assertEquals(
315 lastChange.getEventedValue(
316 0,
317 AVTransportVariable.TransportState.class
318 ).getValue(),
319 TransportState.PLAYING
320 );
321
322 ActionCallback stopAction =
323 new Stop(service) {
324 @Override
325 public void failure(ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
326
327 }
328 };
329 stopAction.run();
330
331 manager.fireLastChange();
332
333 lastChangeString = lcValue[0];
334 lastChange = new LastChange(
335 new AVTransportLastChangeParser(),
336 lastChangeString
337 );
338 assertEquals(
339 lastChange.getEventedValue(
340 0,
341 AVTransportVariable.TransportState.class
342 ).getValue(),
343 TransportState.STOPPED
344 );
345
346 for (boolean assertion : assertions) {
347 assertEquals(assertion, true);
348 }
349
350 }
351
352 }