View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  package example.mediarenderer;
16  
17  import org.fourthline.cling.binding.annotations.AnnotationLocalServiceBinder;
18  import org.fourthline.cling.model.meta.DeviceDetails;
19  import org.fourthline.cling.model.meta.DeviceIdentity;
20  import org.fourthline.cling.model.meta.LocalDevice;
21  import org.fourthline.cling.model.meta.LocalService;
22  import org.fourthline.cling.model.types.UDADeviceType;
23  import org.fourthline.cling.model.types.UDN;
24  import org.fourthline.cling.model.types.UnsignedIntegerFourBytes;
25  import org.fourthline.cling.model.types.UnsignedIntegerTwoBytes;
26  import org.fourthline.cling.support.avtransport.impl.AVTransportService;
27  import org.fourthline.cling.support.avtransport.lastchange.AVTransportLastChangeParser;
28  import org.fourthline.cling.support.lastchange.LastChangeAwareServiceManager;
29  import org.fourthline.cling.support.lastchange.LastChangeParser;
30  import org.fourthline.cling.support.model.Channel;
31  import org.fourthline.cling.support.renderingcontrol.AbstractAudioRenderingControl;
32  import org.fourthline.cling.support.renderingcontrol.RenderingControlException;
33  import org.fourthline.cling.support.renderingcontrol.lastchange.RenderingControlLastChangeParser;
34  
35  /**
36   * @author Christian Bauer
37   */
38  public class MediaRendererSampleData {
39  
40      public static LocalService<AVTransportService> createAVTransportService() throws Exception {
41  
42          LocalService<AVTransportService> service =                                      // DOC:INC1
43                  new AnnotationLocalServiceBinder().read(AVTransportService.class);
44  
45          // Service's which have "logical" instances are very special, they use the
46          // "LastChange" mechanism for eventing. This requires some extra wrappers.
47          LastChangeParser lastChangeParser = new AVTransportLastChangeParser();
48  
49          service.setManager(
50                  new LastChangeAwareServiceManager<AVTransportService>(service, lastChangeParser) {
51                      @Override
52                      protected AVTransportService createServiceInstance() throws Exception {
53                          return new AVTransportService(
54                                  MyRendererStateMachine.class,   // All states
55                                  MyRendererNoMediaPresent.class  // Initial state
56                          );
57                      }
58                  }
59          );                                                                              // DOC:INC1
60          return service;
61      }
62      
63      public static LocalService<AudioRenderingControlService> createRenderingControlService() throws Exception {
64  
65          LocalService<AudioRenderingControlService> service =
66                  new AnnotationLocalServiceBinder().read(AudioRenderingControlService.class);
67  
68          LastChangeParser lastChangeParser = new RenderingControlLastChangeParser();
69  
70          service.setManager(
71                  new LastChangeAwareServiceManager<>(
72                          service,
73                          AudioRenderingControlService.class,
74                          lastChangeParser
75                  )
76          );
77          return service;
78      }
79  
80      public static LocalDevice createDevice() throws Exception {
81          return new LocalDevice(
82                  new DeviceIdentity(new UDN("1111")),
83                  new UDADeviceType("MediaRenderer"),
84                  new DeviceDetails("My MediaRenderer"),
85                  new LocalService[]{
86                          createAVTransportService(),
87                          createRenderingControlService()
88                  }
89          );
90      }
91      
92      public static class AudioRenderingControlService extends AbstractAudioRenderingControl {
93  
94          @Override
95          public boolean getMute(UnsignedIntegerFourBytes instanceId, String channelName) throws RenderingControlException {
96              return false;
97          }
98  
99          @Override
100         public void setMute(UnsignedIntegerFourBytes instanceId, String channelName, boolean desiredMute) throws RenderingControlException {
101 
102         }
103 
104         @Override
105         public UnsignedIntegerTwoBytes getVolume(UnsignedIntegerFourBytes instanceId, String channelName) throws RenderingControlException {
106             return new UnsignedIntegerTwoBytes(50);
107         }
108 
109         @Override
110         public void setVolume(UnsignedIntegerFourBytes instanceId, String channelName, UnsignedIntegerTwoBytes desiredVolume) throws RenderingControlException {
111 
112         }
113 
114         @Override
115         protected Channel[] getCurrentChannels() {
116             return new Channel[] {
117                     Channel.Master
118             };
119         }
120 
121         @Override
122         public UnsignedIntegerFourBytes[] getCurrentInstanceIds() {
123             return new UnsignedIntegerFourBytes[0];
124         }
125     }
126 
127 }