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 org.opencastproject.mediapackage.AudioStream;
26  import org.opencastproject.mediapackage.MediaPackageSerializer;
27  
28  import org.apache.commons.lang3.StringUtils;
29  import org.w3c.dom.Document;
30  import org.w3c.dom.Element;
31  import org.w3c.dom.Node;
32  
33  import java.util.UUID;
34  
35  import javax.xml.bind.annotation.XmlAccessType;
36  import javax.xml.bind.annotation.XmlAccessorType;
37  import javax.xml.bind.annotation.XmlElement;
38  import javax.xml.bind.annotation.XmlType;
39  import javax.xml.xpath.XPath;
40  import javax.xml.xpath.XPathConstants;
41  import javax.xml.xpath.XPathException;
42  
43  /**
44   * Implementation of {@link org.opencastproject.mediapackage.AudioStream}.
45   */
46  @XmlAccessorType(XmlAccessType.NONE)
47  @XmlType(name = "audio", namespace = "http://mediapackage.opencastproject.org")
48  public class AudioStreamImpl extends AbstractStreamImpl implements AudioStream {
49  
50    @XmlElement(name = "bitdepth")
51    protected Integer bitdepth;
52  
53    @XmlElement(name = "channels")
54    protected Integer channels;
55  
56    @XmlElement(name = "samplingrate")
57    protected Integer samplingrate;
58  
59    @XmlElement(name = "bitrate")
60    protected Float bitrate;
61  
62    @XmlElement(name = "peakleveldb")
63    protected Float pkLevDb;
64  
65    @XmlElement(name = "rmsleveldb")
66    protected Float rmsLevDb;
67  
68    @XmlElement(name = "rmspeakdb")
69    protected Float rmsPkDb;
70  
71    public AudioStreamImpl() {
72      this(UUID.randomUUID().toString());
73    }
74  
75    public AudioStreamImpl(String identifier) {
76      super(identifier);
77    }
78  
79    /**
80     * @see org.opencastproject.mediapackage.ManifestContributor#toManifest(org.w3c.dom.Document,
81     *      org.opencastproject.mediapackage.MediaPackageSerializer)
82     */
83    @Override
84    public Node toManifest(Document document, MediaPackageSerializer serializer) {
85      Element node = document.createElement("audio");
86      addCommonManifestElements(node, document, serializer);
87  
88      // Channels
89      if (channels != null) {
90        Element channelsNode = document.createElement("channels");
91        channelsNode.appendChild(document.createTextNode(channels.toString()));
92        node.appendChild(channelsNode);
93      }
94  
95      // Bit depth
96      if (bitdepth != null) {
97        Element bitdepthNode = document.createElement("bitdepth");
98        bitdepthNode.appendChild(document.createTextNode(bitdepth.toString()));
99        node.appendChild(bitdepthNode);
100     }
101 
102     // Bit rate
103     if (bitrate != null) {
104       Element bitratenode = document.createElement("bitrate");
105       bitratenode.appendChild(document.createTextNode(bitrate.toString()));
106       node.appendChild(bitratenode);
107     }
108 
109     // Sampling rate
110     if (samplingrate != null) {
111       Element samplingrateNode = document.createElement("samplingrate");
112       samplingrateNode.appendChild(document.createTextNode(samplingrate.toString()));
113       node.appendChild(samplingrateNode);
114     }
115 
116     // Pk lev dB
117     if (pkLevDb != null) {
118       Element peakleveldbNode = document.createElement("peakleveldb");
119       peakleveldbNode.appendChild(document.createTextNode(pkLevDb.toString()));
120       node.appendChild(peakleveldbNode);
121     }
122     // RMS lev dB
123     if (rmsLevDb != null) {
124       Element rmsleveldbNode = document.createElement("rmsleveldb");
125       rmsleveldbNode.appendChild(document.createTextNode(rmsLevDb.toString()));
126       node.appendChild(rmsleveldbNode);
127     }
128     // RMS Pk dB
129     if (rmsPkDb != null) {
130       Element rmspeakdbNode = document.createElement("rmspeakdb");
131       rmspeakdbNode.appendChild(document.createTextNode(rmsPkDb.toString()));
132       node.appendChild(rmspeakdbNode);
133     }
134     return node;
135   }
136 
137   /**
138    * Create an audio stream from the XML manifest.
139    *
140    * @param streamIdHint
141    *          stream ID that has to be used if the manifest does not provide one. This is the case when reading an old
142    *          manifest.
143    */
144   public static AudioStreamImpl fromManifest(String streamIdHint, Node node, XPath xpath) throws IllegalStateException,
145           XPathException {
146     // Create stream
147     String sid = (String) xpath.evaluate("@id", node, XPathConstants.STRING);
148     if (StringUtils.isEmpty(sid)) {
149       sid = streamIdHint;
150     }
151     AudioStreamImpl as = new AudioStreamImpl(sid);
152     partialFromManifest(as, node, xpath);
153 
154     // bit depth
155     try {
156       String bd = (String) xpath.evaluate("bitdepth/text()", node, XPathConstants.STRING);
157       if (!StringUtils.isBlank(bd))
158         as.bitdepth = Integer.valueOf(bd.trim());
159     } catch (NumberFormatException e) {
160       throw new IllegalStateException("Bit depth was malformatted: " + e.getMessage());
161     }
162 
163     // channels
164     try {
165       String strChannels = (String) xpath.evaluate("channels/text()", node, XPathConstants.STRING);
166       if (!StringUtils.isBlank(strChannels))
167         as.channels = Integer.valueOf(strChannels.trim());
168     } catch (NumberFormatException e) {
169       throw new IllegalStateException("Number of channels was malformatted: " + e.getMessage());
170     }
171 
172     // sampling rate
173     try {
174       String sr = (String) xpath.evaluate("framerate/text()", node, XPathConstants.STRING);
175       if (!StringUtils.isBlank(sr))
176         as.samplingrate = Integer.valueOf(sr.trim());
177     } catch (NumberFormatException e) {
178       throw new IllegalStateException("Frame rate was malformatted: " + e.getMessage());
179     }
180 
181     // Bit rate
182     try {
183       String br = (String) xpath.evaluate("bitrate/text()", node, XPathConstants.STRING);
184       if (!StringUtils.isBlank(br))
185         as.bitrate = Float.valueOf(br.trim());
186     } catch (NumberFormatException e) {
187       throw new IllegalStateException("Bit rate was malformatted: " + e.getMessage());
188     }
189 
190     // Pk lev dB
191     try {
192       String pkLev = (String) xpath.evaluate("peakleveldb/text()", node, XPathConstants.STRING);
193       if (!StringUtils.isBlank(pkLev))
194         as.pkLevDb = Float.valueOf(pkLev.trim());
195     } catch (NumberFormatException e) {
196       throw new IllegalStateException("Pk lev dB was malformatted: " + e.getMessage());
197     }
198 
199     // RMS lev dB
200     try {
201       String rmsLev = (String) xpath.evaluate("rmsleveldb/text()", node, XPathConstants.STRING);
202       if (!StringUtils.isBlank(rmsLev))
203         as.rmsLevDb = Float.valueOf(rmsLev.trim());
204     } catch (NumberFormatException e) {
205       throw new IllegalStateException("RMS lev dB was malformatted: " + e.getMessage());
206     }
207 
208     // RMS Pk dB
209     try {
210       String rmsPk = (String) xpath.evaluate("rmspeakdb/text()", node, XPathConstants.STRING);
211       if (!StringUtils.isBlank(rmsPk))
212         as.rmsPkDb = Float.valueOf(rmsPk.trim());
213     } catch (NumberFormatException e) {
214       throw new IllegalStateException("RMS Pk dB was malformatted: " + e.getMessage());
215     }
216 
217     return as;
218   }
219 
220   @Override
221   public Integer getBitDepth() {
222     return bitdepth;
223   }
224 
225   @Override
226   public Integer getChannels() {
227     return channels;
228   }
229 
230   @Override
231   public Integer getSamplingRate() {
232     return samplingrate;
233   }
234 
235   @Override
236   public Float getBitRate() {
237     return bitrate;
238   }
239 
240   @Override
241   public Float getPkLevDb() {
242     return pkLevDb;
243   }
244 
245   @Override
246   public Float getRmsLevDb() {
247     return rmsLevDb;
248   }
249 
250   @Override
251   public Float getRmsPkDb() {
252     return rmsPkDb;
253   }
254 
255   // Setter
256 
257   public void setBitDepth(Integer bitdepth) {
258     this.bitdepth = bitdepth;
259   }
260 
261   public void setChannels(Integer channels) {
262     this.channels = channels;
263   }
264 
265   public void setSamplingRate(Integer samplingRate) {
266     this.samplingrate = samplingRate;
267   }
268 
269   public void setBitRate(Float bitRate) {
270     this.bitrate = bitRate;
271   }
272 
273   public void setPkLevDb(Float pkLevDb) {
274     this.pkLevDb = pkLevDb;
275   }
276 
277   public void setRmsLevDb(Float rmsLevDb) {
278     this.rmsLevDb = rmsLevDb;
279   }
280 
281   public void setRmsPkDb(Float rmsPkDb) {
282     this.rmsPkDb = rmsPkDb;
283   }
284 
285   @Override
286   public void setCaptureDevice(String captureDevice) {
287     this.device.type = captureDevice;
288   }
289 
290   @Override
291   public void setCaptureDeviceVersion(String captureDeviceVersion) {
292     this.device.version = captureDeviceVersion;
293   }
294 
295   @Override
296   public void setCaptureDeviceVendor(String captureDeviceVendor) {
297     this.device.vendor = captureDeviceVendor;
298   }
299 
300   @Override
301   public void setFormat(String format) {
302     this.encoder.type = format;
303   }
304 
305   @Override
306   public void setFormatVersion(String formatVersion) {
307     this.encoder.version = formatVersion;
308   }
309 
310   @Override
311   public void setEncoderLibraryVendor(String encoderLibraryVendor) {
312     this.encoder.vendor = encoderLibraryVendor;
313   }
314 
315 }