Skip to content
Snippets Groups Projects
Commit f81413fb authored by bgrozev's avatar bgrozev
Browse files

Merge pull request #10 from Haerezis/greyfading

Add a protocol / CaptureDevice that generate RGB frames
parents b33e45db 32f9173c
No related branches found
No related tags found
No related merge requests found
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jitsi.impl.neomedia.jmfext.media.protocol.greyfading;
import javax.media.control.*;
import org.jitsi.impl.neomedia.jmfext.media.protocol.*;
/**
* Implements a <tt>CaptureDevice</tt> which provides a fading animation from
* white to black to white... in form of video.
*
* @author Thomas Kuntz
*/
public class DataSource
extends AbstractVideoPullBufferCaptureDevice
{
/**
* {@inheritDoc}
*
* Implements
* {@link AbstractPushBufferCaptureDevice#createStream(int, FormatControl)}.
*/
protected VideoGreyFadingStream createStream(
int streamIndex,
FormatControl formatControl)
{
return new VideoGreyFadingStream(this, formatControl);
}
}
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jitsi.impl.neomedia.jmfext.media.protocol.greyfading;
import java.awt.Dimension;
import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
import org.jitsi.impl.neomedia.device.*;
import org.jitsi.service.neomedia.*;
/**
* Implements a <tt>MediaDevice</tt> which provides a fading animation from
* white to black to white... in form of video.
*
* @author Thomas Kuntz
*/
public class VideoGreyFadingMediaDevice
extends MediaDeviceImpl
{
/**
* The default framerate the<tt>MediaDevice</tt> will have.
*/
public final static int DEFAULT_FRAMERATE = 10;
/**
* The default dimension the<tt>MediaDevice</tt> will have.
*/
public final static Dimension DEFAULT_DIMENSION = new Dimension(640,480);
/**
* Initializes a new <tt>VideoGreyFadingMediaDevice</tt> with default
* framerate and dimension.
*/
public VideoGreyFadingMediaDevice()
{
this(DEFAULT_FRAMERATE,DEFAULT_DIMENSION);
}
/**
* Initializes a new <tt>VideoGreyFadingMediaDevice</tt> with the given
* framerate and the default dimension.
* @param framerate the framerate of the <tt>CaptureDevice</tt> behind this
* <tt>MediaDevice</tt>.
*/
public VideoGreyFadingMediaDevice(int framerate)
{
this(framerate,DEFAULT_DIMENSION);
}
/**
* Initializes a new <tt>VideoGreyFadingMediaDevice</tt> with the given
* dimension and the default framerate.
* @param dimension the dimension (width & height) of the
* <tt>CaptureDevice</tt> behind this <tt>MediaDevice</tt>.
*/
public VideoGreyFadingMediaDevice(Dimension dimension)
{
this(DEFAULT_FRAMERATE,dimension);
}
/**
* Initializes a new <tt>VideoGreyFadingMediaDevice</tt> with the given
* framerate and dimension.
* @param framerate the framerate of the <tt>CaptureDevice</tt> behind this
* <tt>MediaDevice</tt>.
* @param dimension the dimension (width & height) of the
* <tt>CaptureDevice</tt> behind this <tt>MediaDevice</tt>.
*/
public VideoGreyFadingMediaDevice(int framerate, Dimension dimension)
{
super(new CaptureDeviceInfo(
"GreyFadingVideo",
new MediaLocator("greyfading:"),
new Format[]
{
new RGBFormat(
dimension, // size
Format.NOT_SPECIFIED, // maxDataLength
Format.byteArray, // dataType
framerate, // frameRate
32, // bitsPerPixel
2 /* red */,
3 /* green */,
4 /* blue */)
}),
MediaType.VIDEO);
}
}
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jitsi.impl.neomedia.jmfext.media.protocol.greyfading;
import javax.media.*;
import javax.media.control.*;
import javax.media.format.*;
import org.jitsi.impl.neomedia.codec.*;
import org.jitsi.impl.neomedia.jmfext.media.protocol.*;
import java.awt.Dimension;
import java.io.*;
import java.util.*;
/**
* Implements a <tt>PullBufferStream</tt> which provides a fading animation from
* white to black to white... in form of video.
*
* @author Thomas Kuntz
*/
public class VideoGreyFadingStream
extends AbstractVideoPullBufferStream<DataSource>
{
/**
* The value for the color of the RGB bytes
*/
private int color = 0;
/**
* Indicate if the video is fading toward white (if true) or toward
* black (if false)
*/
private boolean increment = true;
/**
* The timestamp of the last time the <tt>doRead</tt> function returned
* (the timestamp is taken just before the return).
*/
private long timeLastRead = 0;
/**
* Initializes a new <tt>VideoGreyFadingStream</tt> which is to be exposed
* by a specific <tt>VideoGreyFadingCaptureDevice</tt> and which is to have
* its <tt>Format</tt>-related information abstracted by a specific
* <tt>FormatControl</tt>.
*
* @param dataSource the <tt>VideoGreyFadingCaptureDevice</tt> which is
* initializing the new instance and which is to expose it in its array
* of <tt>PushBufferStream</tt>s
* @param formatControl the <tt>FormatControl</tt> which is to abstract
* the <tt>Format</tt>-related information of the new instance
*/
public VideoGreyFadingStream(
DataSource dataSource,
FormatControl formatControl)
{
super(dataSource, formatControl);
}
/**
* Reads available media data from this instance into a specific
* <tt>Buffer</tt>.
*
* @param buffer the <tt>Buffer</tt> to write the available media data
* into
* @throws IOException if an I/O error has prevented the reading of
* available media data from this instance into the specified
* <tt>buffer</tt>
*/
@Override
protected void doRead(Buffer buffer)
throws IOException
{
long millis = 0;
VideoFormat format;
format = (VideoFormat)buffer.getFormat();
if (format == null)
{
format = (VideoFormat)getFormat();
if (format != null)
buffer.setFormat(format);
}
Dimension size = format.getSize();
int frameSizeInBytes
= (int) (
size.getHeight()
* size.getWidth()
* 4);
byte[] data
= AbstractCodec2.validateByteArraySize(
buffer,
frameSizeInBytes,
false);
Arrays.fill(data, 0, frameSizeInBytes, (byte) color);
if(increment)
{
color+=3;
}
else
{
color-=3;
}
if(color >= 255)
{
increment = false;
color=255;
}
else if(color <= 0)
{
increment = true;
color=0;
}
buffer.setData(data);
buffer.setOffset(0);
buffer.setLength(frameSizeInBytes);
buffer.setTimeStamp(System.nanoTime());
//To respect the framerate, we wait for the remaining milliseconds since
//last doRead call.
millis = System.currentTimeMillis() - timeLastRead;
millis = (long)(1000.0 / format.getFrameRate()) - millis;
if(millis > 0)
{
try
{
Thread.sleep(millis);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
timeLastRead=System.currentTimeMillis();
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment