001: /*
002: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: */
032:
033: package com.db.layers.underlay;
034:
035: import java.util.Vector;
036: import javax.media.*;
037: import javax.media.control.FormatControl;
038: import javax.media.format.AudioFormat;
039: import javax.media.format.VideoFormat;
040: import javax.media.protocol.*;
041: import javax.media.protocol.CaptureDevice;
042: import javax.media.bean.playerbean.MediaPlayer;
043: import java.io.IOException;
044: import java.lang.reflect.Constructor;
045:
046: import java.awt.*;
047: import java.awt.event.*;
048:
049: public class Capturer extends Object {
050:
051: private Vector vectorDevices;
052: private Vector vectorAudioDevices;
053: private Vector vectorVideoDevices;
054:
055: public Capturer() {
056:
057: super ();
058:
059: vectorDevices = null;
060: try {
061: vectorDevices = CaptureDeviceManager.getDeviceList(null);
062: } catch (Exception exception) {
063: exception.printStackTrace();
064: }
065:
066: vectorAudioDevices = getAudioDevices();
067: vectorVideoDevices = getVideoDevices();
068:
069: }
070:
071: public Vector getAudioDevices() {
072:
073: int l = vectorDevices.size();
074: vectorAudioDevices = new Vector();
075: for (int i = 0; i < l; i++) {
076: CaptureDeviceInfo capturedeviceinfo = (CaptureDeviceInfo) vectorDevices
077: .elementAt(i);
078: javax.media.Format aformat[] = capturedeviceinfo
079: .getFormats();
080: for (int k = 0; k < aformat.length; k++) {
081: if (!(aformat[k] instanceof AudioFormat))
082: continue;
083: vectorAudioDevices.addElement(capturedeviceinfo);
084: break;
085: }
086: }
087:
088: return vectorAudioDevices;
089:
090: }
091:
092: public Vector getVideoDevices() {
093:
094: int l = vectorDevices.size();
095: vectorVideoDevices = new Vector();
096: for (int i = 0; i < l; i++) {
097: CaptureDeviceInfo capturedeviceinfo = (CaptureDeviceInfo) vectorDevices
098: .elementAt(i);
099: javax.media.Format aformat[] = capturedeviceinfo
100: .getFormats();
101: for (int k = 0; k < aformat.length; k++) {
102: if (!(aformat[k] instanceof VideoFormat))
103: continue;
104: vectorVideoDevices.addElement(capturedeviceinfo);
105: break;
106: }
107: }
108:
109: return vectorVideoDevices;
110:
111: }
112:
113: public Format getMatchingAudioFormat(Format[] aFormat,
114: AudioFormat audioFormat) {
115:
116: int i;
117: boolean found;
118: Format result;
119:
120: i = 0;
121: found = false;
122: result = null;
123: while (i < aFormat.length && !found) {
124: found = audioFormat.matches(aFormat[i]);
125: }
126:
127: if (found)
128: result = audioFormat;
129:
130: return result;
131:
132: }
133:
134: public Format getMatchingVideoFormat(Format[] vFormat,
135: VideoFormat videoFormat) {
136:
137: int i;
138: boolean found;
139: Format result;
140:
141: i = 0;
142: found = false;
143: result = null;
144: while (i < vFormat.length && !found) {
145: found = videoFormat.matches(vFormat[i]);
146: }
147:
148: if (found)
149: result = videoFormat;
150:
151: return result;
152:
153: }
154:
155: public void captureMedia(CaptureDeviceInfo audioCapturedeviceinfo,
156: AudioFormat audioFormat,
157: CaptureDeviceInfo videoCapturedeviceinfo,
158: VideoFormat videoFormat) {
159:
160: Object obj;
161:
162: obj = createCaptureDataSource(audioCapturedeviceinfo,
163: audioFormat, videoCapturedeviceinfo, videoFormat);
164: if (obj != null) {
165: if ((obj instanceof CaptureDevice)
166: && (obj instanceof PushBufferDataSource)) {
167: CDSWrapper cdswrapper = new CDSWrapper(
168: (PushBufferDataSource) obj);
169: obj = cdswrapper;
170: try {
171: cdswrapper.connect();
172: } catch (IOException _ex) {
173: obj = null;
174: System.out
175: .println("Couldn't initialize the Capture Device.");
176: }
177: }
178: open(((DataSource) (obj)));
179: } else {
180: System.out
181: .println("Couldn't initialize the Capture Device.");
182: }
183:
184: }
185:
186: public DataSource createCaptureDataSource(
187: CaptureDeviceInfo audioCapturedeviceinfo,
188: AudioFormat audioFormat,
189: CaptureDeviceInfo videoCapturedeviceinfo,
190: VideoFormat videoFormat) {
191:
192: Format sourceAudioFormat;
193: Format sourceVideoFormat;
194: String s;
195: String s1;
196:
197: s = null;
198: s1 = null;
199: if (audioCapturedeviceinfo != null)
200: s = audioCapturedeviceinfo.getName();
201: if (videoCapturedeviceinfo != null)
202: s1 = videoCapturedeviceinfo.getName();
203: sourceAudioFormat = null;
204: sourceVideoFormat = null;
205: if (vectorAudioDevices.contains(audioCapturedeviceinfo)
206: && vectorVideoDevices.contains(videoCapturedeviceinfo)) {
207: sourceAudioFormat = getMatchingAudioFormat(
208: audioCapturedeviceinfo.getFormats(), audioFormat);
209: sourceVideoFormat = getMatchingVideoFormat(
210: videoCapturedeviceinfo.getFormats(), videoFormat);
211: }
212:
213: return createCaptureDataSource(s, sourceAudioFormat, s1,
214: sourceVideoFormat);
215:
216: }
217:
218: public static DataSource createCaptureDataSource(String s,
219: Format format, String s1, Format format1) {
220:
221: DataSource datasource = null;
222: Object obj = null;
223: DataSource datasource2 = null;
224: if (s == null && s1 == null)
225: return null;
226: if (s == null || s1 == null) {
227: if (s != null)
228: datasource = initializeCaptureDataSource(null, s,
229: format);
230: else
231: datasource = initializeCaptureDataSource(null, s1,
232: format1);
233: } else if (!s.equals(s1)) {
234: datasource = initializeCaptureDataSource(null, s, format);
235: if (datasource == null)
236: return null;
237: DataSource datasource1 = initializeCaptureDataSource(null,
238: s1, format1);
239: if (datasource1 == null)
240: return null;
241: DataSource adatasource[] = { datasource, datasource1 };
242: try {
243: datasource2 = Manager
244: .createMergingDataSource(adatasource);
245: } catch (IncompatibleSourceException _ex) {
246: }
247: datasource = datasource2;
248: } else {
249: datasource = initializeCaptureDataSource(null, s, format);
250: if (datasource == null)
251: return null;
252: datasource = initializeCaptureDataSource(datasource, s1,
253: format1);
254: }
255: if (datasource == null)
256: return null;
257: try {
258: datasource.connect();
259: } catch (IOException _ex) {
260: return null;
261: }
262: return datasource;
263:
264: }
265:
266: public static DataSource initializeCaptureDataSource(
267: DataSource datasource, String s, Format format) {
268:
269: DataSource datasource1 = datasource;
270: if (datasource == null) {
271: CaptureDeviceInfo capturedeviceinfo = CaptureDeviceManager
272: .getDevice(s);
273: if (capturedeviceinfo == null)
274: return null;
275: MediaLocator medialocator = capturedeviceinfo.getLocator();
276: try {
277: datasource1 = Manager.createDataSource(medialocator);
278: if (datasource1 == null)
279: return null;
280: } catch (NoDataSourceException _ex) {
281: return null;
282: } catch (IOException _ex) {
283: return null;
284: }
285: }
286: if (format == null)
287: return datasource1;
288: if (format != null && !(datasource1 instanceof CaptureDevice))
289: return null;
290: FormatControl aformatcontrol[] = ((CaptureDevice) datasource1)
291: .getFormatControls();
292: if (aformatcontrol == null || aformatcontrol.length == 0)
293: return null;
294: for (int i = 0; i < aformatcontrol.length; i++)
295: if (aformatcontrol[i] != null) {
296: Format aformat[] = aformatcontrol[i]
297: .getSupportedFormats();
298: if (aformat == null)
299: return null;
300: if (matches(format, aformat) != null) {
301: aformatcontrol[i].setFormat(format);
302: return datasource1;
303: }
304: }
305:
306: return null;
307:
308: }
309:
310: private static Format matches(Format format, Format aformat[]) {
311:
312: if (aformat == null)
313: return null;
314: for (int i = 0; i < aformat.length; i++)
315: if (aformat[i].matches(format))
316: return aformat[i];
317:
318: return null;
319:
320: }
321:
322: public static DataSource createMergingDataSource(
323: DataSource adatasource[])
324: throws IncompatibleSourceException {
325:
326: if (adatasource.length == 0)
327: throw new IncompatibleSourceException("No sources");
328: if (adatasource[0] instanceof PullDataSource) {
329: for (int i = 1; i < adatasource.length; i++)
330: if (!(adatasource[i] instanceof PullDataSource))
331: throw new IncompatibleSourceException(
332: "One of the sources isn't matching the others");
333:
334: PullDataSource apulldatasource[] = new PullDataSource[adatasource.length];
335: for (int i1 = 0; i1 < apulldatasource.length; i1++)
336: apulldatasource[i1] = (PullDataSource) adatasource[i1];
337:
338: return reflectMDS(
339: "com.ibm.media.protocol.MergingPullDataSource",
340: apulldatasource);
341: }
342: if (adatasource[0] instanceof PushDataSource) {
343: for (int j = 1; j < adatasource.length; j++)
344: if (!(adatasource[j] instanceof PushDataSource))
345: throw new IncompatibleSourceException(
346: "One of the sources isn't matching the others");
347:
348: PushDataSource apushdatasource[] = new PushDataSource[adatasource.length];
349: for (int j1 = 0; j1 < apushdatasource.length; j1++)
350: apushdatasource[j1] = (PushDataSource) adatasource[j1];
351:
352: return reflectMDS(
353: "com.ibm.media.protocol.MergingPushDataSource",
354: apushdatasource);
355: }
356: if (adatasource[0] instanceof PullBufferDataSource) {
357: for (int k = 1; k < adatasource.length; k++)
358: if (!(adatasource[k] instanceof PullBufferDataSource))
359: throw new IncompatibleSourceException(
360: "One of the sources isn't matching the others");
361:
362: PullBufferDataSource apullbufferdatasource[] = new PullBufferDataSource[adatasource.length];
363: for (int k1 = 0; k1 < apullbufferdatasource.length; k1++)
364: apullbufferdatasource[k1] = (PullBufferDataSource) adatasource[k1];
365:
366: return reflectMDS(
367: "com.ibm.media.protocol.MergingPullBufferDataSource",
368: apullbufferdatasource);
369: }
370: if (adatasource[0] instanceof PushBufferDataSource) {
371: boolean flag = false;
372: for (int l = 1; l < adatasource.length; l++) {
373: if (!(adatasource[l] instanceof PushBufferDataSource))
374: throw new IncompatibleSourceException(
375: "One of the sources isn't matching the others");
376: if (adatasource[l] instanceof CaptureDevice)
377: flag = true;
378: }
379:
380: PushBufferDataSource apushbufferdatasource[] = new PushBufferDataSource[adatasource.length];
381: for (int l1 = 0; l1 < apushbufferdatasource.length; l1++)
382: apushbufferdatasource[l1] = (PushBufferDataSource) adatasource[l1];
383:
384: if (flag)
385: return reflectMDS(
386: "com.ibm.media.protocol.MergingCDPushBDS",
387: apushbufferdatasource);
388: else
389: return reflectMDS(
390: "com.ibm.media.protocol.MergingPushBufferDataSource",
391: apushbufferdatasource);
392: } else {
393: return null;
394: }
395:
396: }
397:
398: private static DataSource reflectMDS(String s, Object obj) {
399:
400: Class aclass[] = new Class[1];
401: Object aobj[] = new Object[1];
402:
403: try {
404: Class class1 = Class.forName(s);
405: aclass[0] = obj.getClass();
406: Constructor constructor = class1.getConstructor(aclass);
407: if (s.indexOf("PullDataSource") >= 0)
408: aobj[0] = (PullDataSource[]) obj;
409: else if (s.indexOf("PushDataSource") >= 0)
410: aobj[0] = (PushDataSource[]) obj;
411: else if (s.indexOf("PullBufferDataSource") >= 0)
412: aobj[0] = (PullBufferDataSource[]) obj;
413: else if (s.indexOf("PushBufferDataSource") >= 0)
414: aobj[0] = (PushBufferDataSource[]) obj;
415: else if (s.indexOf("CDPushBDS") >= 0)
416: aobj[0] = (PushBufferDataSource[]) obj;
417: return (DataSource) constructor.newInstance(aobj);
418: } catch (Exception _ex) {
419: return null;
420: }
421:
422: }
423:
424: public void open(DataSource datasource) {
425:
426: MediaPlayer mediaplayer = createMediaPlayer(datasource);
427: mediaplayer.setPopupActive(false);
428: mediaplayer.setControlPanelVisible(false);
429: //mediaplayer.addControllerListener(this);
430: mediaplayer.realize();
431:
432: }
433:
434: public static MediaPlayer createMediaPlayer(Player player) {
435:
436: MediaPlayer mediaplayer = null;
437: if (player == null) {
438: System.out.println("Error creating player " + player);
439: return null;
440: }
441: mediaplayer = new MediaPlayer();
442: mediaplayer.setPlayer(player);
443: if (mediaplayer.getPlayer() == null) {
444: System.out.println("Error creating player " + player);
445: return null;
446: } else {
447: return mediaplayer;
448: }
449:
450: }
451:
452: public static MediaPlayer createMediaPlayer(DataSource datasource) {
453:
454: MediaPlayer mediaplayer = null;
455: if (datasource == null) {
456: System.out.println("Error creating player " + datasource);
457: return null;
458: }
459: mediaplayer = new MediaPlayer();
460: mediaplayer.setDataSource(datasource);
461: if (mediaplayer.getPlayer() == null) {
462: System.out.println("Error creating player " + datasource);
463: return null;
464: } else {
465: return mediaplayer;
466: }
467:
468: }
469:
470: }
|