1 |
769 |
jeremybenn |
/*GstAudioFileReaderNativePeer -- GNU Classpath GStreamer AudioFileReader
|
2 |
|
|
native peer class.
|
3 |
|
|
Copyright (C) 2007 Free Software Foundation, Inc.
|
4 |
|
|
|
5 |
|
|
This file is part of GNU Classpath.
|
6 |
|
|
|
7 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
8 |
|
|
it under the terms of the GNU General Public License as published by
|
9 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
10 |
|
|
any later version.
|
11 |
|
|
|
12 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
13 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15 |
|
|
General Public License for more details.
|
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License
|
18 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
19 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
20 |
|
|
02110-1301 USA.
|
21 |
|
|
|
22 |
|
|
Linking this library statically or dynamically with other modules is
|
23 |
|
|
making a combined work based on this library. Thus, the terms and
|
24 |
|
|
conditions of the GNU General Public License cover the whole
|
25 |
|
|
combination.
|
26 |
|
|
|
27 |
|
|
As a special exception, the copyright holders of this library give you
|
28 |
|
|
permission to link this library with independent modules to produce an
|
29 |
|
|
executable, regardless of the license terms of these independent
|
30 |
|
|
modules, and to copy and distribute the resulting executable under
|
31 |
|
|
terms of your choice, provided that you also meet, for each linked
|
32 |
|
|
independent module, the terms and conditions of the license of that
|
33 |
|
|
module. An independent module is a module which is not derived from
|
34 |
|
|
or based on this library. If you modify this library, you may extend
|
35 |
|
|
this exception to your version of the library, but you are not
|
36 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
37 |
|
|
exception statement from your version. */
|
38 |
|
|
|
39 |
|
|
package gnu.javax.sound.sampled.gstreamer.io;
|
40 |
|
|
|
41 |
|
|
import gnu.classpath.Pointer;
|
42 |
|
|
import gnu.javax.sound.sampled.gstreamer.GStreamerMixer;
|
43 |
|
|
|
44 |
|
|
import java.io.BufferedInputStream;
|
45 |
|
|
import java.io.File;
|
46 |
|
|
import java.io.IOException;
|
47 |
|
|
import java.io.InputStream;
|
48 |
|
|
import java.net.URL;
|
49 |
|
|
import java.util.HashMap;
|
50 |
|
|
import java.util.Map;
|
51 |
|
|
|
52 |
|
|
import javax.sound.sampled.AudioFormat;
|
53 |
|
|
import javax.sound.sampled.AudioSystem;
|
54 |
|
|
import javax.sound.sampled.AudioFormat.Encoding;
|
55 |
|
|
|
56 |
|
|
/**
|
57 |
|
|
* GStreamer native peer for GstAudioFileReader.
|
58 |
|
|
*
|
59 |
|
|
* @author Mario Torre <neugens@limasoftware.net>
|
60 |
|
|
*/
|
61 |
|
|
final class GstAudioFileReaderNativePeer
|
62 |
|
|
{
|
63 |
|
|
private static final String GST_ENCODING = "GStreamer Generic Audio Reader";
|
64 |
|
|
|
65 |
|
|
private static class GstHeader
|
66 |
|
|
{
|
67 |
|
|
/*
|
68 |
|
|
* NOTE: these properties are accessed by the native code, be careful
|
69 |
|
|
* if you change them.
|
70 |
|
|
* Not all the fields are necessarily set.
|
71 |
|
|
*
|
72 |
|
|
*/
|
73 |
|
|
public String file = null;
|
74 |
|
|
|
75 |
|
|
public String suffix = null;
|
76 |
|
|
|
77 |
|
|
public String name = null;
|
78 |
|
|
|
79 |
|
|
public String mimetype = null;
|
80 |
|
|
|
81 |
|
|
public String endianness = null;
|
82 |
|
|
|
83 |
|
|
public String channels = null;
|
84 |
|
|
|
85 |
|
|
public String rate = null;
|
86 |
|
|
|
87 |
|
|
public String width = null;
|
88 |
|
|
|
89 |
|
|
public String depth = null;
|
90 |
|
|
|
91 |
|
|
public String isSigned = null;
|
92 |
|
|
|
93 |
|
|
public String layer = null;
|
94 |
|
|
|
95 |
|
|
public String bitrate = null;
|
96 |
|
|
|
97 |
|
|
public String framed = null;
|
98 |
|
|
|
99 |
|
|
public String type = null;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
public static AudioFormat getAudioFormat(File file) throws Exception
|
103 |
|
|
{
|
104 |
|
|
GstHeader header = new GstHeader();
|
105 |
|
|
header.file = file.getAbsolutePath();
|
106 |
|
|
|
107 |
|
|
if (!gstreamer_get_audio_format_file(header))
|
108 |
|
|
return null;
|
109 |
|
|
|
110 |
|
|
return getAudioFormat(header);
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
public static AudioFormat getAudioFormat(InputStream is) throws Exception
|
114 |
|
|
{
|
115 |
|
|
return getAudioFormat(is, new GstHeader());
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
public static AudioFormat getAudioFormat(URL url) throws Exception
|
119 |
|
|
{
|
120 |
|
|
GstHeader header = new GstHeader();
|
121 |
|
|
header.file = url.toExternalForm();
|
122 |
|
|
|
123 |
|
|
return getAudioFormat(url.openStream(), header);
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
private static AudioFormat getAudioFormat(InputStream is, GstHeader header)
|
127 |
|
|
throws Exception
|
128 |
|
|
{
|
129 |
|
|
BufferedInputStream stream = new BufferedInputStream(is);
|
130 |
|
|
if(!stream.markSupported())
|
131 |
|
|
throw new IOException("Stream must support marking.");
|
132 |
|
|
|
133 |
|
|
stream.mark(0);
|
134 |
|
|
|
135 |
|
|
if (!gstreamer_get_audio_format_stream(header, new GstInputStream(stream).
|
136 |
|
|
getNativeClass()))
|
137 |
|
|
return null;
|
138 |
|
|
|
139 |
|
|
return getAudioFormat(header);
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
private static Encoding getEncoding(GstHeader header)
|
143 |
|
|
{
|
144 |
|
|
StringBuilder buffer = new StringBuilder();
|
145 |
|
|
|
146 |
|
|
if (header.name == null)
|
147 |
|
|
{
|
148 |
|
|
buffer.append(GST_ENCODING);
|
149 |
|
|
if (header.mimetype != null)
|
150 |
|
|
{
|
151 |
|
|
buffer.append(" ");
|
152 |
|
|
buffer.append(header.mimetype);
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
header.name = buffer.toString();
|
156 |
|
|
}
|
157 |
|
|
else
|
158 |
|
|
{
|
159 |
|
|
// strip the "decoder" word from the name, if any
|
160 |
|
|
// this is a bit ugly, the alternative would be to still output the
|
161 |
|
|
// full name of the decoder/demuxer
|
162 |
|
|
String lowerCase = header.name.toLowerCase();
|
163 |
|
|
int index = lowerCase.indexOf("decoder");
|
164 |
|
|
if (index == -1)
|
165 |
|
|
{
|
166 |
|
|
index = lowerCase.indexOf("demuxer");
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
if (index == -1)
|
170 |
|
|
index = lowerCase.length();
|
171 |
|
|
|
172 |
|
|
buffer.append(header.name.substring(0, index));
|
173 |
|
|
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
return new Encoding(buffer.toString().trim());
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
private static AudioFormat getAudioFormat(GstHeader header)
|
180 |
|
|
throws Exception
|
181 |
|
|
{
|
182 |
|
|
int na = AudioSystem.NOT_SPECIFIED;
|
183 |
|
|
|
184 |
|
|
/* we use mimetype as an header, but this could have some side effects */
|
185 |
|
|
Encoding encoding = getEncoding(header);
|
186 |
|
|
|
187 |
|
|
float sampleRate = ((header.rate != null) ?
|
188 |
|
|
new Float(header.rate).floatValue() : na);
|
189 |
|
|
|
190 |
|
|
int sampleSizeInBits = ((header.depth != null) ?
|
191 |
|
|
new Integer(header.depth).intValue() : na);
|
192 |
|
|
|
193 |
|
|
int channels = ((header.channels != null) ?
|
194 |
|
|
new Integer(header.channels).intValue() : na);
|
195 |
|
|
|
196 |
|
|
boolean bigEndian = false;
|
197 |
|
|
if (header.endianness != null)
|
198 |
|
|
{
|
199 |
|
|
if (header.endianness.compareTo("4321") == 0)
|
200 |
|
|
bigEndian = true;
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
String ext = null;
|
204 |
|
|
|
205 |
|
|
int frameSize = na;
|
206 |
|
|
float frameRate = na;
|
207 |
|
|
String lowerCase = header.name.toLowerCase();
|
208 |
|
|
|
209 |
|
|
// FIXME: frameRate = sampleRate in these cases under all the tests so far
|
210 |
|
|
// but I'm not sure if this is always correct...
|
211 |
|
|
if (lowerCase.contains("law") || lowerCase.contains("au"))
|
212 |
|
|
{
|
213 |
|
|
frameSize = (sampleSizeInBits >> 3) * channels;
|
214 |
|
|
frameRate = sampleRate;
|
215 |
|
|
ext = "au";
|
216 |
|
|
}
|
217 |
|
|
else if (lowerCase.contains("wav"))
|
218 |
|
|
{
|
219 |
|
|
frameSize = ((sampleSizeInBits + 7) / 8) * channels;
|
220 |
|
|
frameRate = sampleRate;
|
221 |
|
|
ext = "wav";
|
222 |
|
|
}
|
223 |
|
|
else if (lowerCase.contains("iff"))
|
224 |
|
|
{
|
225 |
|
|
frameSize = (sampleSizeInBits * channels) / 8;
|
226 |
|
|
frameRate = sampleRate;
|
227 |
|
|
ext = "aiff";
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
// write all the additional properties we got to identify
|
231 |
|
|
// the gstreamer plugin actually used to deal with this stream
|
232 |
|
|
Map<String, Object> properties = new HashMap<String, Object>();
|
233 |
|
|
properties.put(GStreamerMixer.GST_BACKEND, true);
|
234 |
|
|
properties.put(GStreamerMixer.GST_DECODER, header.name);
|
235 |
|
|
properties.put(GStreamerMixer.GST_TYPE_NAME, encoding.toString());
|
236 |
|
|
if (ext != null)
|
237 |
|
|
properties.put(GStreamerMixer.GST_FILE_EXTENSION, ext);
|
238 |
|
|
|
239 |
|
|
/* now we put in some of the additional properties if we have them */
|
240 |
|
|
if (header.type != null) properties.put("type", header.type);
|
241 |
|
|
if (header.framed != null) properties.put("framed", header.framed);
|
242 |
|
|
if (header.bitrate != null) properties.put("bitrate", header.bitrate);
|
243 |
|
|
if (header.isSigned != null) properties.put("isSigned", header.isSigned);
|
244 |
|
|
if (header.depth != null) properties.put("depth", header.depth);
|
245 |
|
|
if (header.mimetype != null) properties.put("mimetype", header.mimetype);
|
246 |
|
|
|
247 |
|
|
AudioFormat format = new AudioFormat(encoding,
|
248 |
|
|
sampleRate,
|
249 |
|
|
sampleSizeInBits,
|
250 |
|
|
channels,
|
251 |
|
|
frameSize,
|
252 |
|
|
frameRate,
|
253 |
|
|
bigEndian,
|
254 |
|
|
properties);
|
255 |
|
|
return format;
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
/* ***** native methods ***** */
|
259 |
|
|
|
260 |
|
|
/**
|
261 |
|
|
* Retrieve header information about the stream being played.
|
262 |
|
|
*/
|
263 |
|
|
native static final
|
264 |
|
|
protected boolean gstreamer_get_audio_format_stream(GstHeader info,
|
265 |
|
|
Pointer pointer);
|
266 |
|
|
|
267 |
|
|
/**
|
268 |
|
|
* Retrieve header information about the file being played.
|
269 |
|
|
*/
|
270 |
|
|
native static final
|
271 |
|
|
protected boolean gstreamer_get_audio_format_file(GstHeader info);
|
272 |
|
|
|
273 |
|
|
/**
|
274 |
|
|
* Initialize the native peer and enables the object cache.
|
275 |
|
|
* It is meant to be used by the static initializer.
|
276 |
|
|
*/
|
277 |
|
|
native private static final void init_id_cache();
|
278 |
|
|
|
279 |
|
|
static
|
280 |
|
|
{
|
281 |
|
|
System.loadLibrary("gstreamerpeer"); //$NON-NLS-1$
|
282 |
|
|
init_id_cache();
|
283 |
|
|
}
|
284 |
|
|
}
|