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.mediaserver;
16  
17  import org.fourthline.cling.support.avtransport.lastchange.AVTransportVariable.TransportPlaySpeed;
18  import java.util.EnumMap;
19  import org.fourthline.cling.support.model.dlna.DLNAAttribute;
20  import org.fourthline.cling.support.model.Protocol;
21  import org.fourthline.cling.support.model.ProtocolInfo;
22  import org.fourthline.cling.support.model.dlna.DLNAAttribute.Type;
23  import org.fourthline.cling.support.model.dlna.DLNAConversionIndicator;
24  import org.fourthline.cling.support.model.dlna.DLNAFlags;
25  import org.fourthline.cling.support.model.dlna.DLNAProfiles;
26  import org.fourthline.cling.support.model.dlna.DLNAProtocolInfo;
27  import org.testng.annotations.Test;
28  
29  import java.util.EnumSet;
30  import org.fourthline.cling.support.model.dlna.DLNAConversionIndicatorAttribute;
31  import org.fourthline.cling.support.model.dlna.DLNAFlagsAttribute;
32  import org.fourthline.cling.support.model.dlna.DLNAOperations;
33  import org.fourthline.cling.support.model.dlna.DLNAOperationsAttribute;
34  import org.fourthline.cling.support.model.dlna.DLNAPlaySpeedAttribute;
35  
36  import static org.testng.Assert.assertEquals;
37  
38  /**
39   * @author Mario Franco
40   */
41  public class DLNAProtocolTest {
42  
43      @Test
44      public void convertProtocol() throws Exception {
45  
46          ProtocolInfo p = new ProtocolInfo(
47                  "http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_TN;DLNA.ORG_PS=-1,1/2,4;DLNA.ORG_CI=1;DLNA.ORG_FLAGS=01700000000000000000000000000000"
48          );
49  
50          DLNAProtocolInfo dp = new DLNAProtocolInfo(p);
51  
52          EnumSet<DLNAFlags> dflags = (EnumSet<DLNAFlags>) dp.getAttribute(Type.DLNA_ORG_FLAGS).getValue();
53          TransportPlaySpeed[] speeds = (TransportPlaySpeed[]) dp.getAttribute(Type.DLNA_ORG_PS).getValue();
54          
55          assertEquals(dp.getProtocol(), Protocol.HTTP_GET);
56          assertEquals(dp.getAttribute(Type.DLNA_ORG_PN).getValue(), DLNAProfiles.JPEG_TN);
57          assertEquals(dp.getAttribute(Type.DLNA_ORG_CI).getValue(), DLNAConversionIndicator.TRANSCODED);
58          
59          assertEquals(speeds.length, 3);
60          assertEquals(speeds[0].getValue(),"-1");
61          assertEquals(speeds[1].getValue(),"1/2");
62          assertEquals(speeds[2].getValue(),"4");
63          
64          assertEquals(dflags.size(), 4);
65          assertEquals(dflags.contains(DLNAFlags.DLNA_V15), true);
66          assertEquals(dflags.contains(DLNAFlags.CONNECTION_STALL), true);
67          assertEquals(dflags.contains(DLNAFlags.RTSP_PAUSE), false);
68      }
69  
70      @Test
71      public void createProtocol() throws Exception {
72  
73          DLNAProtocolInfo dp = new DLNAProtocolInfo(DLNAProfiles.JPEG_TN);
74          assertEquals(dp.toString(), "http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_TN");
75  
76          EnumMap<DLNAAttribute.Type, DLNAAttribute> attributes = new EnumMap<>(DLNAAttribute.Type.class);
77          attributes.put(Type.DLNA_ORG_PS, new DLNAPlaySpeedAttribute(new String[] {"1"}));
78          dp = new DLNAProtocolInfo(DLNAProfiles.MATROSKA_MKV, attributes);
79          assertEquals(dp.toString(), "http-get:*:video/x-mkv:DLNA.ORG_PN=MATROSKA");
80          
81          attributes.put(Type.DLNA_ORG_PS, new DLNAPlaySpeedAttribute(new String[] {"1","2/3","-1","4"}));
82          dp = new DLNAProtocolInfo(DLNAProfiles.MATROSKA_MKV, attributes);
83          assertEquals(dp.toString(), "http-get:*:video/x-mkv:DLNA.ORG_PN=MATROSKA;DLNA.ORG_PS=2/3,-1,4");
84          
85          attributes.put(Type.DLNA_ORG_OP, new DLNAOperationsAttribute(DLNAOperations.TIMESEEK, DLNAOperations.RANGE ));
86          attributes.put(Type.DLNA_ORG_FLAGS, new DLNAFlagsAttribute(
87                  DLNAFlags.DLNA_V15, 
88                  DLNAFlags.CONNECTION_STALL, 
89                  DLNAFlags.STREAMING_TRANSFER_MODE,
90                  DLNAFlags.BACKGROUND_TRANSFERT_MODE)
91          );        
92          dp = new DLNAProtocolInfo(DLNAProfiles.MATROSKA_MKV, attributes);
93          assertEquals(dp.toString(), "http-get:*:video/x-mkv:DLNA.ORG_PN=MATROSKA;DLNA.ORG_OP=11;DLNA.ORG_PS=2/3,-1,4;DLNA.ORG_FLAGS=01700000000000000000000000000000");
94          
95          attributes.put(Type.DLNA_ORG_CI, new DLNAConversionIndicatorAttribute(DLNAConversionIndicator.TRANSCODED));
96          dp = new DLNAProtocolInfo(DLNAProfiles.MATROSKA_MKV, attributes);
97          assertEquals(dp.toString(), "http-get:*:video/x-mkv:DLNA.ORG_PN=MATROSKA;DLNA.ORG_OP=11;DLNA.ORG_PS=2/3,-1,4;DLNA.ORG_CI=1;DLNA.ORG_FLAGS=01700000000000000000000000000000");
98      }
99  }