public void setMainSource() { setProcessing(false); VideoFormat vidformat = new VideoFormat(VideoFormat.RGB); Vector devices = CaptureDeviceManager.getDeviceList(vidformat); CaptureDeviceInfo di = null; if (devices.size() > 0) di = (CaptureDeviceInfo) devices.elementAt(0); else { JOptionPane.showMessageDialog( parent, "Your camera is not connected", "No webcam found", JOptionPane.WARNING_MESSAGE); return; } try { ml = di.getLocator(); setMainCamSource(Manager.createDataSource(ml)); } catch (Exception e) { JOptionPane.showMessageDialog( parent, "Exception locating media: " + e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE); return; } }
/** * Checks whether the playback and notification configuration share the same device. * * @return are audio out and notifications using the same device. */ public boolean audioOutAndNotificationsShareSameDevice() { AudioSystem audioSystem = getDeviceConfiguration().getAudioSystem(); CaptureDeviceInfo notify = audioSystem.getSelectedDevice(AudioSystem.DataFlow.NOTIFY); CaptureDeviceInfo playback = audioSystem.getSelectedDevice(AudioSystem.DataFlow.PLAYBACK); if (notify == null) return (playback == null); else { if (playback == null) return false; else return notify.getLocator().equals(playback.getLocator()); } }
public Capture_video() { try { // List out available Devices to Capture Video. Vector<CaptureDeviceInfo> list = CaptureDeviceManager.getDeviceList(format); System.out.println(CaptureDeviceManager.getDeviceList(null).toString()); // Iterating list for (CaptureDeviceInfo temp : list) { // Checking whether the current device supports VfW // VfW = Video for Windows if (temp.getName().startsWith("vfw:")) { System.out.println("Found : " + temp.getName().substring(4)); // Selecting the very first device that supports VfW cam = temp; System.out.println("Selected : " + cam.getName().substring(4)); break; } } System.out.println("Put it on work!..."); // Getting the MediaLocator for Selected device. // MediaLocator describes the location of media content locator = cam.getLocator(); if (locator != null) { // Create a Player for Media Located by MediaLocator player = Manager.createRealizedPlayer(locator); if (player != null) { // Starting the player player.start(); // Creating a Frame to display Video f.setTitle("Test Webcam"); f.setLayout(new BorderLayout()); // Adding the Visual Component to display Video captured by Player // from URL provided by MediaLocator f.add(player.getVisualComponent(), BorderLayout.CENTER); f.pack(); f.setAlwaysOnTop(true); f.setVisible(true); } } } catch (Exception e) { e.printStackTrace(); } }
protected void doInitialize() throws Exception { DSCaptureDevice devices[] = DSManager.getInstance().getCaptureDevices(); boolean captureDeviceInfoIsAdded = false; for (int i = 0, count = (devices == null) ? 0 : devices.length; i < count; i++) { long pixelFormat = devices[i].getFormat().getPixelFormat(); int ffmpegPixFmt = (int) DataSource.getFFmpegPixFmt(pixelFormat); Format format = null; if (ffmpegPixFmt != FFmpeg.PIX_FMT_NONE) { format = new AVFrameFormat(ffmpegPixFmt, (int) pixelFormat); } else { logger.warn( "No support for this webcam: " + devices[i].getName() + "(format " + pixelFormat + " not supported)"); continue; } if (logger.isInfoEnabled()) { for (DSFormat f : devices[i].getSupportedFormats()) { if (f.getWidth() != 0 && f.getHeight() != 0) logger.info( "Webcam available resolution for " + devices[i].getName() + ":" + f.getWidth() + "x" + f.getHeight()); } } CaptureDeviceInfo device = new CaptureDeviceInfo( devices[i].getName(), new MediaLocator(LOCATOR_PROTOCOL + ':' + devices[i].getName()), new Format[] {format}); if (logger.isInfoEnabled()) logger.info("Found[" + i + "]: " + device.getName()); CaptureDeviceManager.addDevice(device); captureDeviceInfoIsAdded = true; } if (captureDeviceInfoIsAdded && !MediaServiceImpl.isJmfRegistryDisableLoad()) CaptureDeviceManager.commit(); DSManager.dispose(); }
private void initWebcam() { Vector videoDevices = CaptureDeviceManager.getDeviceList(new VideoFormat(null)); if (videoDevices.size() > 0) { webcam = (CaptureDeviceInfo) videoDevices.get(0); Format[] formats = webcam.getFormats(); Format selectedFormat = null; try { for (Format f : formats) { if (f.toString().contains("640") && f.toString().contains("480")) { selectedFormat = f; break; } } } catch (Exception e) { logger.error( "Failed to get required webcam resolution (640x480). Taking default format: " + e.getMessage()); selectedFormat = formats[0]; } try { webcamPlayer = Manager.createRealizedPlayer(webcam.getLocator()); FormatControl fc = (FormatControl) webcamPlayer.getControl("javax.media.control.FormatControl"); System.out.println("Selected webcam format: " + selectedFormat.toString()); fc.setFormat(selectedFormat); webcamPlayer.start(); webcamAvailable = true; Timer timer = new Timer(); timer.schedule( new TimerTask() { @Override public void run() { frameGrabber = (FrameGrabbingControl) webcamPlayer.getControl("javax.media.control.FrameGrabbingControl"); } }, 2500); } catch (Exception e) { logger.error("Failed to get webcam feed: " + e.getMessage()); } } else { logger.error("No webcam available"); } }
public static synchronized Vector<CaptureDeviceInfo> getDeviceList(Format format) { Vector<CaptureDeviceInfo> result = new Vector<CaptureDeviceInfo>(); for (CaptureDeviceInfo captureDeviceInfo : getDeviceList()) { if (format == null) { result.add(captureDeviceInfo); } else { for (Format aFormat : captureDeviceInfo.getFormats()) { if (format.matches(aFormat)) { result.add(captureDeviceInfo); break; } } } } return result; }
/** Finds a camera and sets it up */ void fetchDeviceDataSource() { CaptureDeviceInfo CapDevice = (CaptureDeviceInfo) camCapDevice.elementAt(formatIdx); Format CapFormat = (Format) camCapFormat.elementAt(formatIdx); MediaLocator loc = CapDevice.getLocator(); try { dataSource = Manager.createDataSource(loc); } catch (Exception e) { } try { // ensures 30 fps or as otherwise preferred FormatControl formCont = ((CaptureDevice) dataSource).getFormatControls()[0]; VideoFormat formatVideoNew = new VideoFormat(null, null, -1, null, (float) cameraFPS); formCont.setFormat(CapFormat.intersects(formatVideoNew)); } catch (Exception e) { } }
public void capture(String device) { CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice(device); System.out.println("-----------------------------------------------------------"); System.out.println("CaptureDevice Name is " + deviceInfo.getName()); System.out.println("-----------------------------------------------------------"); System.out.println("CaptureDevice is " + deviceInfo); System.out.println("-----------------------------------------------------------"); Format[] formatsSupported = deviceInfo.getFormats(); System.out.println("Supports " + formatsSupported.length + " formats"); for (int findex = 0; findex < formatsSupported.length; findex++) { System.out.println("Unique encoding name is " + formatsSupported[findex].getEncoding()); System.out.println("Format attributes are " + formatsSupported[findex]); } System.out.println("-----------------------------------------------------------"); System.out.println("Media Locator is " + deviceInfo.getLocator()); System.out.println("***********************************************************"); try { player = Manager.createPlayer(deviceInfo.getLocator()); } catch (java.io.IOException e) { System.out.println("IOException"); } catch (javax.media.NoPlayerException npe) { System.out.println("NoPlayerException"); } player.addControllerListener(this); System.out.println("About to call start on player"); player.realize(); waitForState(player.Realized); addVisualElements(); setVisible(true); player.start(); System.out.println("Called start on player"); waitForState(player.Started); System.out.println("Player started"); }
public static void main(String[] args) { if (args.length != 2) { System.out.println("Usage: rtpaudio <targetIP> <targetPort>"); System.exit(0); } try { RegistryDefaults.setDefaultFlags(RegistryDefaults.FMJ); // create a clean registry RegistryDefaults.unRegisterAll(RegistryDefaults.ALL); RegistryDefaults.registerAll(RegistryDefaults.FMJ); // remove all capture devices Vector deviceList = (Vector) CaptureDeviceManager.getDeviceList(null).clone(); for (int i = 0; i < deviceList.size(); i++) { CaptureDeviceInfo cdi = (CaptureDeviceInfo) deviceList.elementAt(i); CaptureDeviceManager.removeDevice(cdi); } // update capture device list new net.sf.fmj.media.cdp.javasound.CaptureDevicePlugger().addCaptureDevices(); PlugInManager.commit(); deviceList = (Vector) CaptureDeviceManager.getDeviceList(null).clone(); if ((null == deviceList) || (deviceList.size() == 0)) { System.out.println("### ERROR found no audio capture device"); System.exit(0); } // enumerate all codec Vector codecList = PlugInManager.getPlugInList(null, null, PlugInManager.CODEC); System.out.println("found " + codecList.size() + " codec"); for (int i = 0; i < codecList.size(); i++) { String aCodecClass = (String) codecList.elementAt(i); System.out.println("# " + (i + 1) + " " + aCodecClass); } // fetch first available audio capture device deviceList = (Vector) CaptureDeviceManager.getDeviceList(null).clone(); CaptureDeviceInfo captureDeviceInfo = (CaptureDeviceInfo) deviceList.elementAt(0); System.out.println("### using " + captureDeviceInfo.getName()); System.out.println("### locator " + captureDeviceInfo.getLocator()); javax.media.protocol.DataSource dataSource = javax.media.Manager.createDataSource( new javax.media.MediaLocator(captureDeviceInfo.getLocator().toString())); // javax.media.protocol.DataSource dataSource = // javax.media.Manager.createDataSource(new // javax.media.MediaLocator("javasound://")); System.out.println("### created datasource " + dataSource.getClass().getName()); javax.media.control.FormatControl[] formatControls = ((javax.media.protocol.CaptureDevice) dataSource).getFormatControls(); System.out.println("got format control " + formatControls[0].getClass().getName()); System.out.println("current format is " + formatControls[0].getFormat()); // set audio capture format javax.media.Format[] formats = formatControls[0].getSupportedFormats(); for (int i = 0; i < formats.length; i++) { javax.media.format.AudioFormat af = (javax.media.format.AudioFormat) formats[i]; if ((af.getChannels() == 1) && (af.getSampleSizeInBits() == 16)) { if (af.getSampleRate() == Format.NOT_SPECIFIED) { javax.media.format.AudioFormat newAudioFormat = new javax.media.format.AudioFormat( af.getEncoding(), 8000.0f, javax.media.Format.NOT_SPECIFIED, javax.media.Format.NOT_SPECIFIED); // javax.media.format.AudioFormat newAudioFormat = new // javax.media.format.AudioFormat(af.getEncoding(), // 44100.0f, javax.media.Format.NOT_SPECIFIED, // javax.media.Format.NOT_SPECIFIED); formatControls[0].setFormat(newAudioFormat.intersects(af)); break; } } } System.out.println("current format is now " + formatControls[0].getFormat()); FrameProcessingControl fpc = null; // adujst recording buffer ( to adjust latency ) dataSource.stop(); Object[] controls = dataSource.getControls(); for (int i = 0; i < controls.length; i++) { String className = controls[i].getClass().getName(); if (-1 != className.indexOf("JavaSoundBufferControl")) { javax.media.control.BufferControl bc = (javax.media.control.BufferControl) controls[i]; System.out.println( "### current javasound buffer length is " + bc.getBufferLength() + " ms"); bc.setBufferLength(40); System.out.println( "### current javasound buffer length is " + bc.getBufferLength() + " ms"); } else if (-1 != className.indexOf("JitterBufferControl")) { javax.media.control.BufferControl bc = (javax.media.control.BufferControl) controls[i]; System.out.println("### current jitter buffer length is " + bc.getBufferLength() + " ms"); bc.setBufferLength(80); System.out.println("### current jitter buffer length is " + bc.getBufferLength() + " ms"); } else if (-1 != className.indexOf("FPC")) { fpc = (FrameProcessingControl) controls[i]; System.out.println("### found bitrate control " + fpc.getClass()); } } dataSource.start(); // create processor javax.media.Processor processor = javax.media.Manager.createProcessor(dataSource); System.out.println("### created processor " + processor.getClass().getName()); processor.configure(); for (int idx = 0; idx < 100; idx++) { if (processor.getState() == Processor.Configured) { break; } Thread.sleep(100); } System.out.println("### processor state " + processor.getState()); processor.setContentDescriptor( new javax.media.protocol.ContentDescriptor(ContentDescriptor.RAW_RTP)); javax.media.control.TrackControl[] tracks = processor.getTrackControls(); // /tracks[0].setFormat(new // javax.media.format.AudioFormat(javax.media.format.AudioFormat.ULAW_RTP, // 8000, 8, 1)); tracks[0].setFormat( new javax.media.format.AudioFormat(javax.media.format.AudioFormat.GSM_RTP, 8000, 8, 1)); processor.realize(); for (int idx = 0; idx < 100; idx++) { if (processor.getState() == Controller.Realized) { break; } Thread.sleep(100); } System.out.println("### processor state " + processor.getState()); javax.media.protocol.DataSource dataOutput = processor.getDataOutput(); System.out.println("### processor data output " + dataOutput.getClass().getName()); // BitRateControl BitRateControl bitrateControl = null; Object[] controls2 = dataOutput.getControls(); for (int i = 0; i < controls2.length; i++) { if (controls2[i] instanceof BitRateControl) { bitrateControl = (BitRateControl) controls2[i]; System.out.println("### found bitrate control " + bitrateControl.getClass()); break; } } // PacketSizeControl Object[] controls3 = processor.getControls(); for (int i = 0; i < controls3.length; i++) { if (controls3[i] instanceof PacketSizeControl) { PacketSizeControl psc = (PacketSizeControl) controls3[i]; System.out.println("### current packetsize is " + psc.getPacketSize() + " bytes"); psc.setPacketSize(66); System.out.println("### current packetsize is " + psc.getPacketSize() + " bytes"); break; } } // enumerate all controls of the processor Object[] pcontrols = processor.getControls(); for (int i = 0; i < pcontrols.length; i++) { System.out.println("processor control " + i + " " + pcontrols[i]); } javax.media.rtp.RTPManager rtpManager = javax.media.rtp.RTPManager.newInstance(); javax.media.rtp.SessionAddress local = new javax.media.rtp.SessionAddress( InetAddress.getLocalHost(), Integer.valueOf(args[1]).intValue()); javax.media.rtp.SessionAddress target = new javax.media.rtp.SessionAddress( InetAddress.getByName(args[0]), Integer.valueOf(args[1]).intValue()); rtpManager.initialize(local); rtpManager.addTarget(target); javax.media.rtp.SendStream sendStream = rtpManager.createSendStream(dataOutput, 0); sendStream.start(); processor.start(); Thread.sleep(1000); System.out.println("\n>>>>>> TRANSMITTING ULAW/RTP AUDIO NOW"); while (2 > 1) { Thread.sleep(1000); if (null != bitrateControl) { TransmissionStats stats = sendStream.getSourceTransmissionStats(); System.out.println( "rtp audio send: bitrate=" + bitrateControl.getBitRate() + " (pdu=" + stats.getPDUTransmitted() + " bytes=" + stats.getBytesTransmitted() + " overrun=" + fpc.getFramesDropped() + ")"); } } } catch (Exception ex) { ex.printStackTrace(); } System.exit(0); }
/** Gathers info on a camera */ boolean fetchDeviceFormats() { Vector deviceList = CaptureDeviceManager.getDeviceList(new VideoFormat(null)); CaptureDeviceInfo CapDevice = null; Format CapFormat = null; String type = "N/A"; Codec convcodec = null; CaptureDeviceInfo deviceInfo = null; boolean VideoFormatMatch = false; for (int i = 0; i < deviceList.size(); i++) { // search for video device deviceInfo = (CaptureDeviceInfo) deviceList.elementAt(i); Format deviceFormat[] = deviceInfo.getFormats(); RGBFormat imgFormat = new RGBFormat( null, -1, Format.intArray, -1, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, -1, -1, Format.FALSE, -1); for (int f = 0; f < deviceFormat.length; f++) { // search for all video formats if (deviceFormat[f].matches(imgFormat)) { convcodec = null; // does not need one } else { // find a converter convcodec = fetchCodec(deviceFormat[f], imgFormat); if (convcodec == null) continue; // failed to open one } if (deviceFormat[f] instanceof RGBFormat) type = "RGB"; if (deviceFormat[f] instanceof YUVFormat) type = "YUV"; if (deviceFormat[f] instanceof JPEGFormat) type = "JPG"; Dimension size = ((VideoFormat) deviceFormat[f]).getSize(); camImgSize.addElement(type + " " + size.width + "x" + size.height); CapDevice = deviceInfo; camCapDevice.addElement(CapDevice); CapFormat = (VideoFormat) deviceFormat[f]; camCapFormat.addElement(CapFormat); camConverter.addElement(convcodec); // valid can be null or not VideoFormatMatch = true; // at least one } } if (VideoFormatMatch == false) { if (deviceInfo != null) System.out.println(deviceInfo); System.out.println("Video Format not found"); return false; } return true; }
public WebcamCaptureAndFadePanel(String saveDir, String layout) { System.out.println("Using " + saveDir + " as directory for the images."); saveDirectory = saveDir; getImages(); images_used = new ArrayList<Integer>(); images_lastadded = new ArrayList<Integer>(); images_nevershown = new ArrayList<Integer>(); Vector devices = (Vector) CaptureDeviceManager.getDeviceList(null).clone(); Enumeration enumeration = devices.elements(); System.out.println("- Available cameras -"); ArrayList<String> names = new ArrayList<String>(); while (enumeration.hasMoreElements()) { CaptureDeviceInfo cdi = (CaptureDeviceInfo) enumeration.nextElement(); String name = cdi.getName(); if (name.startsWith("vfw:")) { names.add(name); System.out.println(name); } } // String str1 = "vfw:Logitech USB Video Camera:0"; // String str2 = "vfw:Microsoft WDM Image Capture (Win32):0"; if (names.size() == 0) { JOptionPane.showMessageDialog( null, "Ingen kamera funnet. " + "Du må koble til et kamera for å kjøre programmet.", "Feil", JOptionPane.ERROR_MESSAGE); System.exit(0); } else if (names.size() > 1) { JOptionPane.showMessageDialog( null, "Fant mer enn 1 kamera. " + "Velger da:\n" + names.get(0), "Advarsel", JOptionPane.WARNING_MESSAGE); } String str2 = names.get(0); di = CaptureDeviceManager.getDevice(str2); ml = di.getLocator(); try { player = Manager.createRealizedPlayer(ml); formatControl = (FormatControl) player.getControl("javax.media.control.FormatControl"); /* Format[] formats = formatControl.getSupportedFormats(); for (int i=0; i<formats.length; i++) System.out.println(formats[i].toString()); */ player.start(); } catch (javax.media.NoPlayerException e) { JOptionPane.showMessageDialog( null, "Klarer ikke å starte" + " programmet pga. feil med kamera. Sjekk at det er koblet til.", "IOException", JOptionPane.ERROR_MESSAGE); System.exit(0); } catch (Exception e) { e.printStackTrace(); System.exit(0); } /* * Layout * * Add * - comp * - imagepanels */ if (layout.equals("1024v2")) { layout1024v2(); } else if (layout.equals("1280")) { layout1280(); } else { layout1024(); } // Capture Window if (captureWindow) { cw = new JFrame("Capture from webcam"); cw.setAlwaysOnTop(true); cw.setSize(sizeCaptureWindow_x, sizeCaptureWindow_y); cw.addKeyListener(new captureWindowKeyListner()); cw.setUndecorated(true); // Add webcam if ((comp = player.getVisualComponent()) != null) { cw.add(comp); } // Add panel to window and set location of window cw.setLocation(cwLocation_x, cwLocation_y); } // Text window cwText = new rotatedText(""); /* * Timer for update */ Timer thread = new Timer(); thread.schedule(new frameUpdateTask(), 0, (1000 / fps)); }
public static synchronized CaptureDeviceInfo getDevice(String deviceName) { for (CaptureDeviceInfo captureDeviceInfo : getDeviceList()) { if (captureDeviceInfo.getName().equals(deviceName)) return captureDeviceInfo; } return null; }