1 package com.inzyme.jmds; 2 3 import java.io.IOException ; 4 5 import javax.media.Buffer; 6 import javax.media.Format; 7 import javax.media.format.VideoFormat; 8 import javax.media.protocol.BufferTransferHandler; 9 import javax.media.protocol.ContentDescriptor; 10 import javax.media.protocol.PushBufferStream; 11 import javax.media.protocol.SourceStream; 12 13 import com.sun.media.vfw.BitMapInfo; 14 15 26 public class DSSourceStream implements PushBufferStream { 27 private DSCapturePin myCapturePin; 28 29 private CaptureRunnable myCaptureRunnable; 30 private Thread myCaptureThread; 31 32 private BufferTransferHandler myTransferHandler; 33 private boolean myConnected; 34 private boolean myStarted; 35 private boolean myStopRequested; 36 37 private Format myFormat; 38 39 private Object myRestartLock = new Object (); 40 41 46 public DSSourceStream(DSCapturePin _capturePin) { 47 myCapturePin = _capturePin; 48 myCaptureRunnable = new CaptureRunnable(); 49 } 50 51 56 public Format getFormat() { 57 BitMapInfo bmi = new BitMapInfo(); 58 fillInBitMapInfo(bmi); 59 float frameRate = getFrameRate(); 60 myFormat = bmi.createVideoFormat(Format.byteArray, frameRate); 61 return myFormat; 62 } 63 64 72 public Format setFormat(int _index) { 73 setFormat0(_index); 74 return getFormat(); 75 } 76 77 82 public void read(Buffer _buffer) throws IOException { 83 Object data = _buffer.getData(); 84 int length = ((VideoFormat) myFormat).getMaxDataLength(); 85 86 if (data == null || !(data instanceof byte[]) || ((byte[]) data).length != length) { 87 data = new byte[length]; 88 _buffer.setData(data); 89 _buffer.setLength(length); 90 } 91 92 int offset = _buffer.getOffset(); 93 _buffer.setFormat(myFormat); 94 95 int pos = 0; 96 while (!myStopRequested && pos < length) { 97 pos += fillBuffer((byte[]) data, offset + pos, length - pos); 98 } 99 100 _buffer.setData(data); 101 _buffer.setOffset(offset); 102 _buffer.setLength(length); 103 _buffer.setTimeStamp(Buffer.TIME_UNKNOWN); 104 } 105 106 public void setTransferHandler(BufferTransferHandler _transferHandler) { 107 myTransferHandler = _transferHandler; 108 } 109 110 public ContentDescriptor getContentDescriptor() { 111 return new ContentDescriptor(ContentDescriptor.RAW); 112 } 113 114 public long getContentLength() { 115 return SourceStream.LENGTH_UNKNOWN; 116 } 117 118 121 public boolean endOfStream() { 122 return false; 123 } 124 125 128 public Object getControl(String _controlClass) { 129 return null; 130 } 131 132 135 public Object [] getControls() { 136 return new Object [0]; 137 } 138 139 146 public void start() { 147 if (!myConnected) { 148 throw new IllegalStateException ("You must connect to this stream prior to starting it."); 149 } 150 151 if (myStarted) { 153 return; 154 } 155 156 synchronized (myRestartLock) { 159 myStarted = true; 160 myStopRequested = false; 161 162 myCaptureThread = new Thread (myCaptureRunnable, "Capture Thread"); 163 myCaptureThread.start(); 164 165 try { 166 myRestartLock.wait(); 167 } 168 catch (InterruptedException e) { 169 e.printStackTrace(); 170 } 171 } 172 } 173 174 178 public void stop() { 179 if (!myStarted) { 180 return; 181 } 182 183 myStopRequested = true; 184 } 185 186 protected void finalize() throws Throwable { 187 stop(); 188 disconnect(); 189 } 190 191 void connect() { 192 if (myConnected) { 193 return; 194 } 195 myConnected = true; 196 connect0(); 197 } 198 199 void disconnect() { 200 if (!myConnected) { 201 return; 202 } 203 disconnect0(); 204 myConnected = false; 205 } 206 207 private native void setFormat0(int _index); 208 209 private native void fillInBitMapInfo(BitMapInfo _bitMapInfo); 210 211 private native float getFrameRate(); 212 213 private native void connect0(); 214 215 private native void start0(); 216 217 private native void stop0(); 218 219 private native void disconnect0(); 220 221 private native int getBufferSize(); 222 223 private native int fillBuffer(byte[] _buffer, int _offset, int _length); 224 225 private class CaptureRunnable implements Runnable { 226 public void run() { 227 start0(); 228 229 synchronized (myRestartLock) { 231 myRestartLock.notifyAll(); 232 } 233 234 while (!myStopRequested && getBufferSize() == 0) { 236 try { 237 Thread.sleep(100); 238 } 239 catch (Throwable t) { 240 t.printStackTrace(); 241 } 242 } 243 244 while (!myStopRequested) { 246 if (myTransferHandler != null) { 247 myTransferHandler.transferData(DSSourceStream.this); 248 } 249 else { 250 try { 252 Thread.sleep(100); 253 } 254 catch (Throwable t) { 255 } 256 } 257 258 try { 262 Thread.sleep(20); 263 } 264 catch (Throwable t) { 265 } 266 } 267 268 stop0(); 269 270 synchronized (myRestartLock) { 272 myStarted = false; 273 myRestartLock.notify(); 274 } 275 } 276 } 277 } 278 | Popular Tags |