View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  
23  package org.opencastproject.mediapackage.track;
24  
25  import static org.apache.commons.lang3.StringUtils.trimToNull;
26  
27  import org.opencastproject.mediapackage.MediaPackageSerializer;
28  import org.opencastproject.mediapackage.Stream;
29  
30  import org.apache.commons.lang3.StringUtils;
31  import org.w3c.dom.Document;
32  import org.w3c.dom.Element;
33  import org.w3c.dom.Node;
34  
35  import javax.xml.bind.annotation.XmlAccessType;
36  import javax.xml.bind.annotation.XmlAccessorType;
37  import javax.xml.bind.annotation.XmlAttribute;
38  import javax.xml.bind.annotation.XmlElement;
39  import javax.xml.bind.annotation.XmlID;
40  import javax.xml.bind.annotation.XmlTransient;
41  import javax.xml.bind.annotation.XmlType;
42  import javax.xml.xpath.XPath;
43  import javax.xml.xpath.XPathConstants;
44  import javax.xml.xpath.XPathException;
45  
46  @XmlTransient
47  @XmlAccessorType(XmlAccessType.NONE)
48  public abstract class AbstractStreamImpl implements Stream {
49  
50    @XmlID
51    @XmlAttribute(name = "id")
52    protected String identifier;
53  
54    @XmlElement(name = "device")
55    protected Device device = new Device();
56  
57    @XmlElement(name = "encoder")
58    protected Encoder encoder = new Encoder();
59  
60    @XmlElement(name = "framecount")
61    protected Long frameCount;
62  
63    @XmlType(name = "device")
64    static class Device {
65      @XmlAttribute(name = "type")
66      protected String type;
67      @XmlAttribute(name = "version")
68      protected String version;
69      @XmlAttribute(name = "vendor")
70      protected String vendor;
71    }
72  
73    @XmlType(name = "encoder")
74    static class Encoder {
75      @XmlAttribute(name = "type")
76      protected String type;
77      @XmlAttribute(name = "version")
78      protected String version;
79      @XmlAttribute(name = "vendor")
80      protected String vendor;
81    }
82  
83    protected AbstractStreamImpl() {
84    }
85  
86    protected AbstractStreamImpl(String identifier) {
87      this.identifier = identifier;
88    }
89  
90    public String getIdentifier() {
91      return identifier;
92    }
93  
94    public void setIdentifier(String identifier) {
95      this.identifier = identifier;
96    }
97  
98    public String getCaptureDevice() {
99      return device.type;
100   }
101 
102   public String getCaptureDeviceVersion() {
103     return device.version;
104   }
105 
106   public String getCaptureDeviceVendor() {
107     return device.vendor;
108   }
109 
110   public Long getFrameCount() {
111     return frameCount;
112   }
113 
114   public String getFormat() {
115     return encoder.type;
116   }
117 
118   public String getFormatVersion() {
119     return encoder.version;
120   }
121 
122   public String getEncoderLibraryVendor() {
123     return encoder.vendor;
124   }
125 
126   public void setCaptureDevice(String capturedevice) {
127     this.device.type = capturedevice;
128   }
129 
130   public void setCaptureDeviceVersion(String capturedeviceVersion) {
131     this.device.version = capturedeviceVersion;
132   }
133 
134   public void setCaptureDeviceVendor(String captureDeviceVendor) {
135     this.device.vendor = captureDeviceVendor;
136   }
137 
138   public void setFrameCount(Long frameCount) {
139     this.frameCount = frameCount;
140   }
141 
142   public void setFormat(String format) {
143     this.encoder.type = format;
144   }
145 
146   public void setFormatVersion(String formatVersion) {
147     this.encoder.version = formatVersion;
148   }
149 
150   public void setEncoderLibraryVendor(String encoderLibraryVendor) {
151     this.encoder.vendor = encoderLibraryVendor;
152   }
153 
154   protected static void partialFromManifest(AbstractStreamImpl stream, Node node, XPath xpath)
155       throws IllegalStateException, XPathException {
156     // Frame count
157     try {
158       String frameCount = (String) xpath.evaluate("framecount/text()", node, XPathConstants.STRING);
159       if (!StringUtils.isBlank(frameCount)) {
160         stream.frameCount = Long.valueOf(frameCount.trim());
161       }
162     } catch (NumberFormatException e) {
163       throw new IllegalStateException("Frame count was malformatted: " + e.getMessage());
164     }
165 
166     // Device
167     Device dev = new Device();
168     dev.type = trimToNull((String) xpath.evaluate("device/@type", node, XPathConstants.STRING));
169     dev.version = trimToNull((String) xpath.evaluate("device/@version", node, XPathConstants.STRING));
170     dev.vendor = trimToNull((String) xpath.evaluate("device/@vendor", node, XPathConstants.STRING));
171     if (dev.type != null || dev.version != null || dev.vendor != null) {
172       stream.device = dev;
173     }
174 
175     // Encoder
176     Encoder enc = new Encoder();
177     enc.type = trimToNull(((String) xpath.evaluate("encoder/@type", node, XPathConstants.STRING)));
178     enc.version = trimToNull(((String) xpath.evaluate("encoder/@version", node, XPathConstants.STRING)));
179     enc.vendor = trimToNull(((String) xpath.evaluate("encoder/@vendor", node, XPathConstants.STRING)));
180     if (enc.type != null || enc.version != null || enc.vendor != null) {
181       stream.encoder = enc;
182     }
183   }
184 
185   protected void addCommonManifestElements(Element node, Document document, MediaPackageSerializer serializer) {
186     // Stream ID
187     node.setAttribute("id", getIdentifier());
188 
189     // Frame count
190     if (frameCount != null) {
191       Element frameCountNode = document.createElement("framecount");
192       frameCountNode.appendChild(document.createTextNode(Long.toString(frameCount)));
193       node.appendChild(frameCountNode);
194     }
195 
196     // Device
197     Element deviceNode = document.createElement("device");
198     boolean addChild = false;
199     if (device.type != null) {
200       deviceNode.setAttribute("type", device.type);
201       addChild = true;
202     }
203     if (device.version != null) {
204       deviceNode.setAttribute("version", device.version);
205       addChild = true;
206     }
207     if (device.vendor != null) {
208       deviceNode.setAttribute("vendor", device.vendor);
209       addChild = true;
210     }
211     if (addChild) {
212       node.appendChild(deviceNode);
213     }
214 
215     // Encoder
216     Element encoderNode = document.createElement("encoder");
217     addChild = false;
218     if (encoder.type != null) {
219       encoderNode.setAttribute("type", encoder.type);
220       addChild = true;
221     }
222     if (encoder.version != null) {
223       encoderNode.setAttribute("version", encoder.version);
224       addChild = true;
225     }
226     if (encoder.vendor != null) {
227       encoderNode.setAttribute("vendor", encoder.vendor);
228       addChild = true;
229     }
230     if (addChild)
231       node.appendChild(encoderNode);
232   }
233 }