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       }
160     } catch (NumberFormatException e) {
161       throw new IllegalStateException("Bit depth was malformatted: " + e.getMessage());
162     }
163 
164     // channels
165     try {
166       String strChannels = (String) xpath.evaluate("channels/text()", node, XPathConstants.STRING);
167       if (!StringUtils.isBlank(strChannels)) {
168         as.channels = Integer.valueOf(strChannels.trim());
169       }
170     } catch (NumberFormatException e) {
171       throw new IllegalStateException("Number of channels was malformatted: " + e.getMessage());
172     }
173 
174     // sampling rate
175     try {
176       String sr = (String) xpath.evaluate("framerate/text()", node, XPathConstants.STRING);
177       if (!StringUtils.isBlank(sr)) {
178         as.samplingrate = Integer.valueOf(sr.trim());
179       }
180     } catch (NumberFormatException e) {
181       throw new IllegalStateException("Frame rate was malformatted: " + e.getMessage());
182     }
183 
184     // Bit rate
185     try {
186       String br = (String) xpath.evaluate("bitrate/text()", node, XPathConstants.STRING);
187       if (!StringUtils.isBlank(br)) {
188         as.bitrate = Float.valueOf(br.trim());
189       }
190     } catch (NumberFormatException e) {
191       throw new IllegalStateException("Bit rate was malformatted: " + e.getMessage());
192     }
193 
194     // Pk lev dB
195     try {
196       String pkLev = (String) xpath.evaluate("peakleveldb/text()", node, XPathConstants.STRING);
197       if (!StringUtils.isBlank(pkLev)) {
198         as.pkLevDb = Float.valueOf(pkLev.trim());
199       }
200     } catch (NumberFormatException e) {
201       throw new IllegalStateException("Pk lev dB was malformatted: " + e.getMessage());
202     }
203 
204     // RMS lev dB
205     try {
206       String rmsLev = (String) xpath.evaluate("rmsleveldb/text()", node, XPathConstants.STRING);
207       if (!StringUtils.isBlank(rmsLev)) {
208         as.rmsLevDb = Float.valueOf(rmsLev.trim());
209       }
210     } catch (NumberFormatException e) {
211       throw new IllegalStateException("RMS lev dB was malformatted: " + e.getMessage());
212     }
213 
214     // RMS Pk dB
215     try {
216       String rmsPk = (String) xpath.evaluate("rmspeakdb/text()", node, XPathConstants.STRING);
217       if (!StringUtils.isBlank(rmsPk)) {
218         as.rmsPkDb = Float.valueOf(rmsPk.trim());
219       }
220     } catch (NumberFormatException e) {
221       throw new IllegalStateException("RMS Pk dB was malformatted: " + e.getMessage());
222     }
223 
224     return as;
225   }
226 
227   @Override
228   public Integer getBitDepth() {
229     return bitdepth;
230   }
231 
232   @Override
233   public Integer getChannels() {
234     return channels;
235   }
236 
237   @Override
238   public Integer getSamplingRate() {
239     return samplingrate;
240   }
241 
242   @Override
243   public Float getBitRate() {
244     return bitrate;
245   }
246 
247   @Override
248   public Float getPkLevDb() {
249     return pkLevDb;
250   }
251 
252   @Override
253   public Float getRmsLevDb() {
254     return rmsLevDb;
255   }
256 
257   @Override
258   public Float getRmsPkDb() {
259     return rmsPkDb;
260   }
261 
262   // Setter
263 
264   public void setBitDepth(Integer bitdepth) {
265     this.bitdepth = bitdepth;
266   }
267 
268   public void setChannels(Integer channels) {
269     this.channels = channels;
270   }
271 
272   public void setSamplingRate(Integer samplingRate) {
273     this.samplingrate = samplingRate;
274   }
275 
276   public void setBitRate(Float bitRate) {
277     this.bitrate = bitRate;
278   }
279 
280   public void setPkLevDb(Float pkLevDb) {
281     this.pkLevDb = pkLevDb;
282   }
283 
284   public void setRmsLevDb(Float rmsLevDb) {
285     this.rmsLevDb = rmsLevDb;
286   }
287 
288   public void setRmsPkDb(Float rmsPkDb) {
289     this.rmsPkDb = rmsPkDb;
290   }
291 
292   @Override
293   public void setCaptureDevice(String captureDevice) {
294     this.device.type = captureDevice;
295   }
296 
297   @Override
298   public void setCaptureDeviceVersion(String captureDeviceVersion) {
299     this.device.version = captureDeviceVersion;
300   }
301 
302   @Override
303   public void setCaptureDeviceVendor(String captureDeviceVendor) {
304     this.device.vendor = captureDeviceVendor;
305   }
306 
307   @Override
308   public void setFormat(String format) {
309     this.encoder.type = format;
310   }
311 
312   @Override
313   public void setFormatVersion(String formatVersion) {
314     this.encoder.version = formatVersion;
315   }
316 
317   @Override
318   public void setEncoderLibraryVendor(String encoderLibraryVendor) {
319     this.encoder.vendor = encoderLibraryVendor;
320   }
321 
322 }