/**
Aceasta clasa creaza un processor pentru dispozitivul de capura care a fost inregistrat in prealabil cu JMF Registry si salveaza cadrele capturate intr-un buffer.
*/
import javax.media.*;
import javax.media.format.YUVFormat;
import javax.media.format.JPEGFormat;
import java.util.*;
import javax.media.protocol.*;
import javax.media.datasink.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.Component;
import javax.media.util.BufferToImage;
import javax.media.format.VideoFormat;
import java.awt.Image;
public class CameraInterface extends Thread implements ControllerListener, DataSinkListener{
/**
clasa CameraImterface returneaza OK daca poate trimite
imaginea in buffer
*/
public static final int OK=0;
/**
clasa CameraImterface returneaza NOT_OK daca nu s-a
initiat inca
*/
public static final int NOT_OK=1;
private boolean state_transition_OK = true;
private int state=NOT_OK;
private boolean Proceesorstart=false;
private Buffer readBuffer=new Buffer();
private DataSource InitDataSource = null;
//aceasta este DataSource initiala pentru Camera
//private Processor processor;
private Player player;
private Object waitSync = new Object();
private DataSource out_data_source=null;
private DataSource rtpDataSource=null;
private DataSource cloneableDataSource=null;
private DataSourceHandler handler=new DataSourceHandler();
private BufferedImage clip_img=null;
/***********************************************************************
* Constructorul
***********************************************************************/
/**
Acesta este singurul constructor pentru CameraInterface.
Detecteaza dispozitivul captura si creeaza processorul pentru el
*/
public CameraInterface(){
//out_data_source = processor.getDataOutput();
try {
handler.setSource(ProcessorSetUp());
} catch (IncompatibleSourceException e) {
System.err.println("Cannot handle the output
DataSource from the processor: " + out_data_source);
}
handler.addDataSinkListener(this);
// Trece procesorul in starea Prefetch
//processor.prefetch();
//waitForState(processor.Prefetched);
System.out.println("Starts the processor");
handler.start();
//porneste procesorul
//processor.start();
player.start();
state=OK;
}
private DataSource ProcessorSetUp(){
CaptureDeviceInfo _cap_info=null;
YUVFormat VF=new
YUVFormat(YUVFormat.YUV_420);
Vector _device_list =
CaptureDeviceManager.getDeviceList(VF);
if (_device_list.size() > 0){
Enumeration _enu=_device_list.elements();
_cap_info = (CaptureDeviceInfo)_enu.nextElement();
_enu=null;
System.out.println(" Capture device info
:"+_cap_info.getName());
}else{
System.out.println("Cannot find a video capture device");
System.exit(0);
}
MediaLocator _mlocator=_cap_info.getLocator();
_cap_info=null;
if (_mlocator == null) {
System.err.println("Cannot build media locator from video
listener " );
System.exit(0);
}
// Creaza o DataSource pentru media locatorul dat
try {
InitDataSource = Manager.createDataSource(_mlocator);
cloneableDataSource=Manager.
createCloneableDataSource(InitDataSource);
}catch (Exception e) {
System.err.println("Cannot create DataSource from: " +
_mlocator);
System.exit(0);
}
_mlocator=null;
System.out.println("create processor for: " +
InitDataSource.getContentType());
//creaza un processor pentru DataSource-ul obtinut de
//la Camera
try {
//processor =
Manager.createProcessor(cloneableDataSource);
player=Manager.createPlayer(((SourceCloneable)
cloneableDataSource).createClone());
}catch (Exception e) {
System.err.println("Failed to create a processor from the
given DataSource: " + e);
System.exit(0);
}
// processor.addControllerListener(this);
player.addControllerListener(this);
// Trece processorul in starea configured
// processor.configure();
// waitForState(processor.Configured);
// primeste iesirea de la processor
// processor.setContentDescriptor(new
ContentDescriptor(ContentDescriptor.RAW));
// pune processorul in starea realize
// processor.realize();
// waitForState(processor.Realized);
return (cloneableDataSource);
}
/**
Aceasta metoda returneaza starea curenta a CameraInterface
*/
public int getStatus(){
return state;
}
public void run() {
}
public synchronized Component getPlayerVisual () {
return (player.getVisualComponent());
}
public synchronized Component getPlayerControl() {
return (player.getControlPanelComponent());
}
public synchronized BufferedImage getBufferedImage(){
BufferedImage bim=null;
VideoFormat vf;
synchronized(readBuffer){
vf=(VideoFormat)readBuffer.getFormat();
BufferToImage bufferToImage = new BufferToImage(vf);
bim= (BufferedImage)bufferToImage.createImage(readBuffer);
if(vf==null){System.out.println("Video format null");};
}
clip_img=bim;
return bim;
}
/**
Urmatoarea metoda returneaza imaginea creata din bufferul
de intrare
*/
public BufferedImage getImage(){
return clip_img;
}
/**
Aceasta metoda termina Camera Interface
*/
public void Delete(){
//processor.close();
handler.close();
InitDataSource.disconnect();
readBuffer=null;
}
public boolean canDelete(){
return true;
}
/**
Aceasta metoda opreste procesarea din buffer
*/
public void Stop(){
handler.Stop();
state=NOT_OK;
}
public void controllerUpdate(ControllerEvent evt) {
if (evt instanceof ConfigureCompleteEvent ||
evt instanceof RealizeCompleteEvent ||
evt instanceof PrefetchCompleteEvent) {
synchronized (waitSync) {
state_transition_OK = true;
waitSync.notifyAll();
}
}else if (evt instanceof ResourceUnavailableEvent) {
synchronized (waitSync) {
state_transition_OK = false;
waitSync.notifyAll();
}
}else if (evt instanceof EndOfMediaEvent) {
//processor.close();
}else if (evt instanceof SizeChangeEvent) { }
}
public void dataSinkUpdate(DataSinkEvent evt) {
if (evt instanceof EndOfStreamEvent) {
System.err.println("All done!");
evt.getSourceDataSink().close();
System.exit(0);
}
}
/**
Aceasta clasa citeste din DataSource si imparte streamul in buffere
*/
private class DataSourceHandler extends Thread implements
DataSink,BufferTransferHandler {
DataSource source;
PushBufferStream pushStrms[] = null;
private Vector listeners = new Vector(1);
//Stocheaza toate streamurile care nu s-au terminat
SourceStream unfinishedStrms[] = null;
/**
Seteaza DataSource pentru DataSourceHandler
*/
public void setSource(DataSource source) throws
IncompatibleSourceException {
if (source instanceof PushBufferDataSource) {
System.err.println("The source is push Buffer Data Source ");
pushStrms = ((PushBufferDataSource)source).getStreams();
unfinishedStrms = new SourceStream[pushStrms.length];
for (int i = 0; i < pushStrms.length; i++) {
pushStrms[i].setTransferHandler(this);
unfinishedStrms[i] = pushStrms[i];
}
}else
throw new IncompatibleSourceException();
this.source = source;
}
public void setOutputLocator(MediaLocator ml) {
}
public MediaLocator getOutputLocator() {
return null;
}
public String getContentType() {
return source.getContentType();
}
public void open() {
}
public void run() {
try {
|