?? localdevicemonitorcontrol.java
字號:
/* * Copyright (c) [2005] [Jeffrey Moore] * * Redistributions in source code form must reproduce the above copyright and * this condition. * * The contents of this file are subject to the Sun Project JXTA License * Version 1.1 (the "License"); you may not use this file except in compliance * with the License. A copy of the License is available at * http://www.jxta.org/jxta_license.html. * *//* * LocalMonitorControl.java * * Created on April 7, 2005, 9:14 AM */package net.jxta.myjxta.plugins.vijxta;import java.awt.Component;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.Collections;import java.util.Hashtable;import java.util.Iterator;import java.util.LinkedList;import javax.imageio.IIOImage;import javax.imageio.ImageIO;import javax.imageio.ImageWriteParam;import javax.imageio.ImageWriter;import javax.imageio.plugins.jpeg.JPEGImageWriteParam;import javax.imageio.stream.ImageOutputStream;import javax.media.CaptureDeviceInfo;import javax.media.CaptureDeviceManager;import javax.media.Format;import javax.media.Manager;import javax.media.MediaLocator;import javax.media.NoDataSourceException;import javax.media.control.FormatControl;import javax.media.control.MonitorControl;import javax.media.format.RGBFormat;import javax.media.format.VideoFormat;import javax.media.format.YUVFormat;import javax.media.protocol.CaptureDevice;import javax.media.protocol.DataSource;import javax.swing.JLabel;import java.util.logging.Level;import net.jxta.logging.Logging;import java.util.logging.Logger;import net.jxta.endpoint.StringMessageElement;import net.jxta.endpoint.ByteArrayMessageElement;import net.jxta.myjxta.dialog.DialogMessage;/** * This class controls the hardware video capturedevice, it's formats and creating * the capture component for use in the UI. This class also dispatches messages * to the remote peer(s). Current implementation is to encode a frame as a jpeg * and send those bytes in a message accross the wire. * @author jamoore * @modified 2005-04-07 jamoore refactored */public class LocalDeviceMonitorControl extends Thread implements ImageCaptureListener, Monitor, DeviceMonitorControl { static final Logger LOG = Logger.getLogger (LocalDeviceMonitorControl.class.getName()); private static final String REFRESH_RATE_UNIT_LABEL = "Frames Per Second"; private LinkedList outgoingBuffer = null; private ViJxtaDialog viJxtaDialog = null; private Hashtable deviceTable = null; private VideoFormat selectedVideoFormat = null; private MonitorCDS monitorDataSource = null; private int refreshRate = this.DEFAULT_FRAMES_PER_SECOND; private long imageCaptureDelay = 0; private static final int DEFAULT_IMAGES_PER_MESSAGE = 1; private int imagesPerMessage = DEFAULT_IMAGES_PER_MESSAGE ; private MonitorControl monitorControl = null; private int imageCompression = Monitor.DEFAULT_IMAGE_COMPRESSION; private CaptureDeviceInfo selectedCaptureDeviceInfo = null; private MyLock captureLock = null; private MyLock monitorPausedLock = null; private MyLock transmitPausedLock = null; private String mimeType = null; private MyLock bufferStarvedLock = null; private JPEGImageWriteParam jpegParams = null; private ImageWriter imageWriter = null; private long imageEncodeTime = 0; private long averageImageEncodeTime = 0; private DialogMessage templateMessage = null; private int bytesSent = 0; private int messagesSent = 0; private int monitorState = STOPPED; private int transmitState = STOPPED; private boolean transmit = false; private Hashtable refreshRateTable = null; private Hashtable refreshRateLabelTable = null; private Thread dispatchThread = null; private Component monitor; public LocalDeviceMonitorControl (ViJxtaCallControl callControl) { LOG.setLevel (Level.INFO); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("LocalMonitorControl Instantiated"); } refreshRateTable = new Hashtable(); refreshRateTable.put(new Integer(1), new Long((long) this.SECOND / 1)); refreshRateTable.put(new Integer(2), new Long((long) this.SECOND / 2)); refreshRateTable.put(new Integer(3), new Long((long) this.SECOND / 2)); refreshRateTable.put(new Integer(4), new Long((long) this.SECOND / 4)); refreshRateTable.put(new Integer(5), new Long((long) this.SECOND / 5)); refreshRateTable.put(new Integer(6), new Long((long) this.SECOND / 6)); refreshRateTable.put(new Integer(7), new Long((long) this.SECOND / 7)); refreshRateTable.put(new Integer(8), new Long((long) this.SECOND / 8)); refreshRateTable.put(new Integer(9), new Long((long) this.SECOND / 9)); refreshRateTable.put(new Integer(10), new Long((long) this.SECOND / 10)); refreshRateTable.put(new Integer(11), new Long((long) this.SECOND / 11)); refreshRateTable.put(new Integer(12), new Long((long) this.SECOND / 12)); refreshRateTable.put(new Integer(13), new Long((long) this.SECOND / 13)); refreshRateTable.put(new Integer(14), new Long((long) this.SECOND / 14)); refreshRateTable.put(new Integer(15), new Long((long) this.SECOND / 15)); refreshRateLabelTable = new Hashtable () ; refreshRateLabelTable.put (new Integer (1), new JLabel ("1")); refreshRateLabelTable.put (new Integer (3), new JLabel ("3")); refreshRateLabelTable.put (new Integer (6), new JLabel ("6")); refreshRateLabelTable.put (new Integer (9), new JLabel ("9")); refreshRateLabelTable.put (new Integer (11), new JLabel ("11")); refreshRateLabelTable.put (new Integer (13), new JLabel ("13")); refreshRateLabelTable.put (new Integer (15), new JLabel ("15")); this.imageCaptureDelay = ((Long)refreshRateTable.get(new Integer(this.DEFAULT_FRAMES_PER_SECOND))).longValue (); this.viJxtaDialog = (ViJxtaDialog) callControl.getDialog (); outgoingBuffer = new LinkedList (); captureLock = new MyLock (); captureLock.setLocked (true); monitorPausedLock = new MyLock (); transmitPausedLock = new MyLock (); bufferStarvedLock = new MyLock (); deviceTable = new Hashtable (); templateMessage = new DialogMessage (callControl.getDialog ().getGroup () .getPeerGroup ().getPeerName (), null, callControl.getDialog ().getGroup () .getPeerGroup ().getPeerGroupID ().toString (), callControl.getDialog () .getGroup ().getPeerGroup ().getPeerGroupName ()); /** consumer thread... takes from the queue and sends message*/ dispatchThread = new Thread() { public void run() { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("Transmit (Consumer) Thread: RUN"); } while (true) { if (getTransmitState() == Monitor.STOPPED) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("run : Stopped"); } // we should send any remaining data in buffer first break; } if (getTransmitState() == Monitor.PAUSED) { synchronized (transmitPausedLock) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("run : tramsmit Paused"); } try { transmitPausedLock.wait(); } catch (InterruptedException ix) { ix.printStackTrace(); } } } if (outgoingBuffer.size() >= getImagesPerMessage()) { dispatchImage(getImagesPerMessage()); } else { try { synchronized (bufferStarvedLock) { bufferStarvedLock.setLocked(true); bufferStarvedLock.wait(); } } catch (InterruptedException ix) { ix.printStackTrace(); } } } if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("Transmit (Consumer) Thread: ENDED"); } } }; } public static boolean isJMFPresent() { LOG.setLevel(Level.INFO); boolean isPresent = true; try { VideoFormat f = new VideoFormat(VideoFormat.RGB); }catch(Exception x ) { x.printStackTrace (); isPresent = false; }catch(Error e) { e.printStackTrace (); isPresent = false; } return isPresent; } /** producer... runs every n (ms)((fps), sets the capture flag*/ public void run () { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("Image Capture (Producer) Thread RUN"); } while(true) { /** module state mechanics*/ if(getMonitorState () == this.PAUSED) { synchronized(monitorPausedLock) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("run : monitor Paused"); } try { monitorPausedLock.wait (); }catch(InterruptedException ix) { ix.printStackTrace (); } } } if(getMonitorState () == this.STOPPED) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("run : Stopped"); } break; } try { Thread.sleep (getImageCaptureDelay ()); }catch(InterruptedException ix) { ix.printStackTrace (); } synchronized(captureLock) { captureLock.setLocked (false); } } if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("Image Capture (Producer) Thread Ended"); } } protected void dispatchImage (int imagesInThisMessage) { if(outgoingBuffer.size () >= imagesInThisMessage) { byte[] imageBytes = null; DialogMessage msg = getNewTemplateMessage (); StringMessageElement commandElement = new StringMessageElement ( ViJxtaCallControl.TAG_SESSION_COMMAND, ViJxtaCallControl.COMMAND_VIJXTA_DATA, null); msg.addMessageElement (ViJxtaCallControl.TAG_SESSION_COMMAND, commandElement); imageBytes = ((ByteArrayOutputStream)outgoingBuffer.getFirst ()).toByteArray (); outgoingBuffer.removeFirst(); ByteArrayMessageElement imageElement = new ByteArrayMessageElement ( ViJxtaCallControl.TAG_IMAGE_DATA, null, imageBytes, 0, null); msg.addMessageElement (ViJxtaCallControl.TAG_IMAGE_DATA, imageElement); this.messagesSent = this.messagesSent + 1; this.bytesSent = this.bytesSent + imageBytes.length; this.viJxtaDialog.dispatch (msg); } } public String getRefreshRateUnitLabel() { return this.REFRESH_RATE_UNIT_LABEL; } public Hashtable getRefreshRateLabelTable() { return this.refreshRateLabelTable; } public Dimension getFormatSize() { return this.getSelectedVideoFormat ().getSize (); } public void resetMonitor() { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("resetMonitor"); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -