⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datasource.java

📁 FMJ(freedom media for java)是java视频开发的新选择
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			if (started.getValue())			{				// TODO: should always be the same as outputVideoFormat:				final VideoFormat format = convertCivilFormat(image.getFormat());				final Buffer buffer = new Buffer();				buffer.setData(image.getBytes());				buffer.setOffset(0);				if (firstImageTimestamp < 0)					firstImageTimestamp = image.getTimestamp();				// if we don't set the timestamp, JMF is not happy.				if (image.getTimestamp() >= 0)					buffer.setTimeStamp((image.getTimestamp() - firstImageTimestamp) * 1000000L);	// timestamps will start with zero				//logger.fine("      Timestamp=" + image.getTimestamp());				buffer.setLength(image.getBytes().length);				buffer.setFormat(format);				// this is what causes us to drop frames if we can't keep up:				try 				{					synchronized (bufferQueue)					{//						if (!bufferQueueEnabled)//						{	// if we are not buffering multiple items, clear out all old ones://							while (!bufferQueue.isEmpty())//								bufferQueue.get();//						}						if (bufferQueue.isFull())							bufferQueue.get();	// drop a frame.						bufferQueue.put(buffer);					}										if (pushBufferStream != null)						pushBufferStream.notifyTransferHandlerAsync();									} 				catch (InterruptedException e) 				{					return;				}								if (TRACE_FPS)				{					fpsCounter.nextFrame();					if (fpsCounter.getNumFrames() >= 50)					{	System.out.println(fpsCounter);						fpsCounter.reset();					}				}			}		}	}	        public static VideoFormat convertCivilFormat(com.lti.civil.VideoFormat civilVideoFormat)    {    	final int bitsPerPixel;    	if (civilVideoFormat.getFormatType() == com.lti.civil.VideoFormat.RGB24)    		bitsPerPixel = 24;    	else if (civilVideoFormat.getFormatType() == com.lti.civil.VideoFormat.RGB32)    		bitsPerPixel = 32;    	else    		throw new IllegalArgumentException();    	final int red, green, blue;    	red = 3;    	green = 2;    	blue = 1;    	    	final float fps = civilVideoFormat.getFPS();    	final float frameRate;    	if (fps < 0)    		frameRate = Format.NOT_SPECIFIED;    	else    		frameRate = fps;    	    	return new RGBFormat(new Dimension(civilVideoFormat.getWidth(), civilVideoFormat.getHeight()), -1, byte[].class, frameRate, bitsPerPixel, red, green, blue);    	    }    		private class MyPushBufferStream implements PushBufferStream	{		public boolean endOfStream()		{			return false;		}		public ContentDescriptor getContentDescriptor()		{			return new ContentDescriptor(ContentDescriptor.RAW);	// It confuses me that we provide both this, and the correct format below (getFormat)		}		public long getContentLength()		{			return LENGTH_UNKNOWN;		}		public Object getControl(String controlType)		{			return null;		}		public Object[] getControls()		{			return new Object[0];		}		public Format getFormat()		{			if (outputVideoFormat == null )				logger.warning("outputVideoFormat == null, video format unknown.");			return outputVideoFormat;		}		public void read(Buffer buffer) throws IOException		{			Buffer nextBuffer = null;			try			{				synchronized (bufferQueue)				{//					if (!bufferQueueEnabled)//					{	//						// now that we are reading, start queuing.//						bufferQueueEnabled = true;//					}					nextBuffer = (Buffer) bufferQueue.get();				}			} catch (InterruptedException e)			{				throw new InterruptedIOException("" + e);			}									buffer.copy(nextBuffer);								}		private final SynchronizedObjectHolder transferHandlerHolder = new SynchronizedObjectHolder();		public void setTransferHandler(BufferTransferHandler transferHandler)		{	transferHandlerHolder.setObject(transferHandler);		}				void notifyTransferHandlerSync()		{			final BufferTransferHandler handler = (BufferTransferHandler) transferHandlerHolder.getObject();			if (handler != null)				handler.transferData(this);		}				void notifyTransferHandlerAsync() throws InterruptedException		{			if (notifyTransferHandlerThread == null)			{	notifyTransferHandlerThread = new NotifyTransferHandlerThread(ThreadGroupMgr.getDefaultThreadGroup(), "NotifyTransferHandlerThread for " + DataSource.this);				notifyTransferHandlerThread.start();			}						notifyTransferHandlerThread.notifyTransferHandlerAsync();		}				void dispose()		{			if (notifyTransferHandlerThread != null)			{				notifyTransferHandlerThread.close();				try 				{					notifyTransferHandlerThread.waitUntilClosed();				} catch (InterruptedException e) 				{					logger.log(Level.WARNING, "" + e, e);				}				finally				{					notifyTransferHandlerThread = null;				}			}		}				private NotifyTransferHandlerThread notifyTransferHandlerThread;			// doing the transfer notifications in a different thread keeps the 		// capture thread from being tied up.  Seems to avoid some deadlocks when		// JMF is ahead in the classpath as well.		class NotifyTransferHandlerThread extends CloseableThread		{						public NotifyTransferHandlerThread(ThreadGroup group, String threadName) 			{				super(group, threadName);			}			private final ProducerConsumerQueue q = new ProducerConsumerQueue(); 			public void notifyTransferHandlerAsync() throws InterruptedException			{				q.put(Boolean.TRUE);			}						@Override			public void run()			{				try				{					while (!isClosing())					{						if (q.get() == null)							break;												notifyTransferHandlerSync();					}				}				catch (InterruptedException e)				{				}				finally				{					setClosed();				}			}		}			}		// implementation of CaptureDevice:	// From Andrew Rowley:	// 1) I am using JMF to perform processing, and using the civil library for the capture devices.  If I set the video resolution too high (say 640x480 or more), sometimes it looks like the capturing is taking place, but nothing happens.  When I try to stop the sending, I get an error (Error: Unable to prefetch com.sun.media.ProcessEngine@1ea0252).  This does not happen if I use a vfw device at any resolution, so I think it is something to do with civil, possibly to do with an assumption about the maximum video size?  I will look into this myself to make sure that it is not something in JMF, but it would be good to know that there is not something going wrong in civil too.	// It turns out that if you make the civil DataSource (net.sf.fmj.media.protocol.civil.DataSource) implement javax.media.protocol.CaptureDevice, then the problem goes away as JMF does not then do prefetching		    public CaptureDeviceInfo getCaptureDeviceInfo()	{    	// TODO: use name instead of device id:    	// TODO: since we can query output formats from CIVIL, we should offer them all here:		return new CaptureDeviceInfo(deviceId, getLocator(), new Format[] {outputVideoFormat});	}	public FormatControl[] getFormatControls()	{		return new FormatControl[] {new CivilFormatControl()};	}		private boolean enabled = true;		private class CivilFormatControl implements FormatControl	{		public Component getControlComponent()		{			return null;		}		public Format getFormat()		{			return outputVideoFormat;		}		public Format[] getSupportedFormats()		{			// TODO: since we can query output formats from CIVIL, we should offer them all here:			return new Format[] {outputVideoFormat};			}		public boolean isEnabled()		{			return enabled;		}		public void setEnabled(boolean enabled)		{	DataSource.this.enabled = enabled;		}		public Format setFormat(Format format)		{			// TODO: we shouldn't just accept anything that comes in...			outputVideoFormat = (VideoFormat) format;			// TODO: return specific format if passed in format is partially unspecified			return outputVideoFormat;		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -