Data Transfer using DataSink from DataSource to MediaLocator not working
I wrote this pretty straightforward program to transfer from a DataSource to a MediaLocator and when i run it nothing happens. I waited for around 10 minutes and still nothing happened. I manually closed the program to see if any data has been transferred to the destination file but the destination file timpu.mp3 is empty. Can someone tell me why it isn't working?
import javax.swing.*;
import javax.media.*;
import java.net.*;
import java.io.*;
import javax.media.datasink.*;
import javax.media.protocol.*;
class Abcimplements DataSinkListener
{
DataSource ds;
DataSink dsk;
publicvoid transfer()throws Exception
{
ds=Manager.createDataSource(new MediaLocator(new File("G:/java files1/jmf/aa.mp3").toURL()));
MediaLocator mc=new MediaLocator(new File("G:/java files1/jmf/timpu.mp3").toURL());
dsk=Manager.createDataSink(ds,mc);
System.out.println(ds.getContentType()+"\n"+dsk.getOutputLocator().toString());
dsk.open();
dsk.start();
dsk.addDataSinkListener(this);
}
publicvoid dataSinkUpdate(DataSinkEvent event)
{
if(eventinstanceof EndOfStreamEvent)
{
try
{
System.out.println("EndOfStreamEvent");
dsk.stop();
dsk.close();
System.exit(1);
}
catch(Exception e)
{
}
}
}
}
publicclass JMFCapture5
{
publicstaticvoid main(String args[])throws Exception
{
Abc a=new Abc();
a.transfer();
}
}
Message was edited by:
qUesT_foR_knOwLeDge
# 1
qUesTtake a look at http://forum.java.sun.com/thread.jspa?threadID=5143241&tstart=0I think this is similar to what you're after
# 2
> qUesT
>
> take a look at
> http://forum.java.sun.com/thread.jspa?threadID=5143241
> &tstart=0
>
> I think this is similar to what you're after
Thanks Stefan for the response. I had a look at the link and the example you gave was helpful. That code from the OP is from the tutorial. There when we close the processor the datasink stops transferring. But in my case i am directly transferring from a data source like this
ds=Manager.createDataSource(new MediaLocator(new File("G:/java files1/jmf/aa.mp3").toURL()));
MediaLocator mc=new MediaLocator(new File("G:/java files1/jmf/timpu.mp3").toURL());
dsk=Manager.createDataSink(ds,mc);
As you see i haven't used any internediate processor. It's direct transfer from a data source to the destination medialocator. So i would have to wait for the EndOfStreamEvent which never occurs. Your help would be greatly appreciated.
Message was edited by:
qUesT_foR_knOwLeDge
# 3
qUesT
I'm not sure if JMF allows you to create a DataSink in the way you have done
everything in the JMF user guide
http://java.sun.com/products/java-media/jmf/2.1.1/guide/JMFProcessing.html#96157
suggests that the DataSource for your DataSink should be obtained by
proceesor.getDataOutput();
I have messed around with your code & still come back to something like this
public class ABC implements DataSinkListener, ControllerListener{
DataSource ds;
DataSink dsk;
public ABC(){
}
/**
*ControllerListener
*/
public void controllerUpdate(ControllerEvent e)
{
Processor p = (Processor)e.getSourceController();
if(e instanceof ConfigureCompleteEvent){
System.out.println("ConfigureCompleteEvent-" + System.currentTimeMillis());
p.setContentDescriptor(new FileTypeDescriptor(FileTypeDescriptor.MPEG_AUDIO));
//p.setContentDescriptor(null);
p.realize();
}
else if(e instanceof RealizeCompleteEvent){
System.out.println("RealizeCompleteEvent-" + System.currentTimeMillis());
try{
MediaLocator mc = new MediaLocator("file:C:/Workspace/ABCTest.mpg");
dsk = Manager.createDataSink(p.getDataOutput(), mc);
dsk.addDataSinkListener(this);
System.out.println(ds.getContentType() + "\n"+dsk.getOutputLocator().toString());
dsk.open();
dsk.start();
p.start();
}catch(Exception eX){
eX.printStackTrace();
}
}
else if(e instanceof EndOfMediaEvent){
System.out.println("EndOfMediaEvent-" + System.currentTimeMillis());
p.stop();
}
else if(e instanceof StopEvent){
System.out.println ("StopEvent-" + System.currentTimeMillis());
p.close();
}
}
public void transfer() throws Exception{
//ds=Manager.createDataSource(new MediaLocator(new File("G:/java files1/jmf/aa.mp3").toURL()));
//MediaLocator mc=new MediaLocator(new File("G:/java files1/jmf/timpu.mp3").toURL());
//dsk=Manager.createDataSink(ds,mc);
ds = Manager.createDataSource(new MediaLocator("file:C:/WINDOWS/Media/Windows XP Startup.wav"));
Processor p = Manager.createProcessor(ds);
p.addControllerListener(this);
p.configure();
}
public void dataSinkUpdate(DataSinkEvent event)
{
if(event instanceof EndOfStreamEvent){
try{
System.out.println("EndOfStreamEvent-" + System.currentTimeMillis());
dsk.stop();
dsk.close();
System.exit(1);
}catch(Exception e){
}
}
}
public static void main(String args[]) throws Exception
{
ABC a=new ABC();
a.transfer();
}
}
# 4
Thanks stefan. I tried out the above code but it doesn't work. I am not getting playback.
I am having a lot of ttrouble with Processor. Actually my main aim is to capture video from the webcam and save it to a file.
And one more thing. My pc gets stuck a lot of time when i come to p.getDataOutput( ). As i was just saying i am having a lot of trouble with Processor. Thanks for your help Stefan. I would look forward for your help in the future. ( And is there any way i can get in touch with you, email-id or so).
# 5
Have thrown this together - so it's not pretty, but should give you an idea
public class ABC extends JFrame implements DataSinkListener, ControllerListener, ActionListener{
private Container cont;
private JButton jBRecord;
private boolean bRecording = false;
private Processor recordingProcessor;
private DataSource recordingDataSource;
private DataSink recordingDataSink;
private Processor mainProcessor;
private DataSource mainDataSource;
public ABC(){
super("Basic WebCam Handler");
cont = getContentPane();
cont.setLayout(new BorderLayout());
//control panel buttons
jBRecord = new JButton("R+");
jBRecord.setToolTipText("Record On / Off");
cont.add(jBRecord, BorderLayout.NORTH);
//& Listeners
jBRecord.addActionListener(this);
addMyCamera();
pack();
setVisible(true);
}
private void addMyCamera()
{
Vector vCDs = CaptureDeviceManager.getDeviceList(null); //get Devices supported
Iterator iTCams = vCDs.iterator();
while(iTCams.hasNext())
{
CaptureDeviceInfo cDI = (CaptureDeviceInfo)iTCams.next();
if(cDI.getName().startsWith("vfw:"))
{
try{
MediaLocator mL = cDI.getLocator();
mainDataSource = Manager.createCloneableDataSource(Manager.createDataSource(mL));
mainProcessor = Manager.createProcessor(mainDataSource);
mainProcessor.addControllerListener(this);
mainProcessor.configure();
break;
}catch(Exception eX){
eX.printStackTrace();
}
}
}
}
private void startRecording(){
if(!bRecording){
try{
System.out.println("startRecording");
recordingDataSource = ((SourceCloneable)mainDataSource).createClone();
recordingProcessor = Manager.createProcessor(recordingDataSource);
recordingProcessor.addControllerListener(this);
recordingProcessor.configure();
bRecording = true;
}catch(Exception eX){
eX.printStackTrace();
}
}
}
private void stopRecording(){
if(bRecording){
System.out.println("stopRecording");
bRecording = false;
try{
recordingProcessor.close();
recordingDataSink.stop();
recordingDataSink.close();
}catch(Exception eX){
eX.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
if(obj == jBRecord)
{
if(jBRecord.getText().equals("R+"))
{
jBRecord.setText("R-");
startRecording();
}
else
{
jBRecord.setText("R+");
stopRecording();
}
}
}
/**
*ControllerListener
*/
public void controllerUpdate(ControllerEvent e)
{
Processor p = (Processor)e.getSourceController();
if(e instanceof ConfigureCompleteEvent){
System.out.println("ConfigureCompleteEvent-" + System.currentTimeMillis());
if(p == recordingProcessor){
try{
VideoFormat vfmt = new VideoFormat(VideoFormat.CINEPAK);
TrackControl [] tC = p.getTrackControls();
tC[0].setFormat(vfmt);
tC[0].setEnabled(true);
p.setContentDescriptor(new FileTypeDescriptor(FileTypeDescriptor.QUICKTIME));
Control control = p.getControl("javax.media.control.FrameRateControl");
if(control != null && control instanceof FrameRateControl){
FrameRateControl fRC = (FrameRateControl)control;
fRC.setFrameRate(30.0f);
}
}
catch(Exception eX)
{
eX.printStackTrace();
}
}
else{
p.setContentDescriptor(null);
}
p.realize();
}
else if(e instanceof RealizeCompleteEvent){
System.out.println("RealizeCompleteEvent-" + System.currentTimeMillis());
try{
if(p == mainProcessor){
Component c = p.getVisualComponent();
if(c != null){
cont.add(c);
}
p.start();
validate();
}
else if(p == recordingProcessor){
GregorianCalendar gC = new GregorianCalendar();
File f = new File("C:/Workspace/" + gC.get(Calendar.YEAR) + gC.get(Calendar.MONTH) +
gC.get(Calendar.DAY_OF_MONTH) + gC.get(Calendar.HOUR_OF_DAY) +
gC.get(Calendar.MINUTE) + gC.get(Calendar.SECOND) + ".mov");
MediaLocator mL = new MediaLocator(f.toURL());
recordingDataSink = Manager.createDataSink(p.getDataOutput(), mL);
p.start();
recordingDataSink.open();
recordingDataSink.start();
}
}catch(Exception eX){
eX.printStackTrace();
}
}
else if(e instanceof EndOfMediaEvent){
System.out.println("EndOfMediaEvent-" + System.currentTimeMillis());
p.stop();
}
else if(e instanceof StopEvent){
System.out.println ("StopEvent-" + System.currentTimeMillis());
p.close();
}
}
public void dataSinkUpdate(DataSinkEvent event)
{
if(event instanceof EndOfStreamEvent){
try{
System.out.println("EndOfStreamEvent-" + System.currentTimeMillis());
recordingDataSink.stop();
recordingDataSink.close();
}catch(Exception e){
}
}
}
public static void main(String args[]) throws Exception
{
ABC a=new ABC();
//a.transfer();
}
}
# 6
Thanks, will give this a look. Hoping to see you around for sometime in the forum.
# 7
Stefan, I made some changes and i wrote this code to copy a .mpg file but the file isn't getting copied.Is there anything wrong with this code?
import javax.swing.*;
import javax.media.*;
import java.net.*;
import java.io.*;
import javax.media.datasink.*;
import javax.media.protocol.*;
import javax.media.format.*;
import javax.media.control.*;
class Abc implements ControllerListener
{
DataSource ds;
DataSink sink;
Processor pr;
public void maam() throws Exception
{
pr=Manager.createProcessor(new URL("file:G:/java files1/jmf/love.mpg"));
pr.addControllerListener(this);
pr.configure();
}
public void controllerUpdate(ControllerEvent e)
{
if(e instanceof ConfigureCompleteEvent)
{
System.out.println ("ConfigureCompleteEvent");
Processor p = (Processor)e.getSourceController();
System.out.println ("ConfigureCompleteEvent");
processConfigured(p);
}
if(e instanceof RealizeCompleteEvent)
{
System.out.println ("RealizeCompleteEvent");
Processor p = (Processor)e.getSourceController();
processRealized(p);
}
if(e instanceof ControllerClosedEvent)
{
System.out.println ("ControllerClosedEvent");
}
if(e instanceof EndOfMediaEvent)
{
System.out.println ("EndOfMediaEvent");
Processor p = (Processor)e.getSourceController();
p.stop();
}
if(e instanceof StopEvent)
{
System.out.println ("StopEvent");
Processor p = (Processor)e.getSourceController();
p.close();
}
}
public void processConfigured(Processor p)
{
System.out.println("Entered processConfigured");
p.setContentDescriptor (new FileTypeDescriptor (FileTypeDescriptor.MPEG));
TrackControl track[] = p.getTrackControls ();
boolean encodingPossible = false;
for (int i = 0; i < track.length; i++)
{
try
{
track[i].setFormat (new VideoFormat (VideoFormat.MPEG));
encodingPossible = true;
}
catch (Exception e)
{
track[i].setEnabled (false);
}
}
p.realize();
}
public void processRealized(Processor p)
{
pr=p;
System.out.println("Entered processRealized");
try
{
MediaLocator dest = new MediaLocator("file:C:/Workspace/ring1.mpg");
sink = Manager.createDataSink(p.getDataOutput(), dest);
sink.addDataSinkListener(new DataSinkListener()
{
public void dataSinkUpdate(DataSinkEvent e)
{
if(e instanceof EndOfStreamEvent)
{
System.out.println ("EndOfStreamEvent");
sink.close();
}
}
});
sink.open();
sink.start();
}
catch(Exception eX)
{
}
System.out.println("Just before start");
p.start();
}
/*public void dataSinkUpdate(DataSinkEvent event)
{
if(event instanceof EndOfStreamEvent)
{
try
{
System.out.println("EndOfStreamEvent");
dsk.stop();
dsk.close();
System.exit(1);
}
catch(Exception e)
{
}
}
}*/
}
public class JMFCapture5
{
public static void main(String args[]) throws Exception
{
Abc a=new Abc();
a.maam();
}
}
# 8
I think you'll find that you've fallen foul of the lack of maintenace from SunJMF can read in & play and MPEG file but cannot encode it - so it can't write mpeg filesSee http://java.sun.com/products/java-media/jmf/2.1.1/formats.html
# 9
> I think you'll find that you've fallen foul of the
> lack of maintenace from Sun
Yes. There is very little backing for JMF from Sun ( IBM shows some interest though ). I really had a hard time and even the tutorials i obtained for JMF is for the version 2.0.0
> JMF can read in & play and MPEG file but cannot
> encode it - so it can't write mpeg files
Drat. I have been meddling with video conversion for .mpg content type from the past three days.
> See
> http://java.sun.com/products/java-media/jmf/2.1.1/form
> ats.html
Thank you
I awarded 10 dukes to you. If it was possible for me to award more dukes in a single thread i would have given you all the dukes i had. I really appreciate your help and the time you take to read my posts.
And in case you find my thread here in this forum please do reply. You would be amongst the ones i would look forward first for help.
One more question
I searched the net ( googled a hell lot ) for the definition of
1.) Media containers
2.) Content Descriptors
3.) Formats
4. ) Content Type.
These are the definitions i got so far
1.) Media containers - I still haven't got a clear definition
2.) Content Descriptors - I till haven't got a clear definition. The API says that it is a media container but since i haven't understood what a media container is it's of no use
3.) Formats - It is the wau data is structured within a file ( like h.263)
4. ) Content Type - It describes the file type ( like .mp3 )
Can you please provide some explaination for these four.
# 10
> I awarded 10 dukes to you
Thanks - but there was no need for that
The forums are here for people to help each other out
> One more question
> These are the definitions i got so far
They are all related in that you should choose the combintion you need as the output of your Processor
Also NOTE - a Processor is capable of supporting many combinations of these - enables MPEG in to MOV out type functionality
> 3.) Formats - It is the way data is structured within
> a file ( like h.263)
as you say, but remember that there are variations of a Format for a particular Content type (mono/stereo/bit rate for audio, then frame rate etc for video)
> 4. ) Content Type is available from > 2.) Content Descriptors
Together they identify the stream type
FileTypeDescriptor then extends this to describe the file type ( like mp3 )
From the - JavaTM Media Framework API Guide
Content Type
The format in which the media data is stored is referred to as its content type. QuickTime, MPEG, and WAV are all examples of content types. Content type is essentially synonymous with file type--content type is used because media data is often acquired from sources other than local files.
> 1.) Media containers - I still haven't got a clear
> definition. The API says that it is a media container
Agreed - I think that's as far as it goes
# 11
> > I awarded 10 dukes to you
> Thanks - but there was no need for that
> The forums are here for people to help each other
> out
Agreed.
I have 2 questions.
1.) Basically in a media streams there are many tracks. Say audio tracks and video tracks. So before i can set the format for the TrackControl for a particular track i can always consult the TrackControl.getSupportedFormats( ) method and then set the format. Am i right ?( because i doubt the getSupportedFormats( ) method works correctly and the reason i don't trust is given in the next question)
2.) I have set a media locator for a .mpg video file. When i retrieve the supported content descriptors using getSupportedContentDescriptors() from a configured processor which points to that .mpg video file this is the list i obtained-
RAW
RAW/RTP
GSM
MPEG_AUDIO
WAV
AIFF
BASIC_AUDIO
AVI
QuickTime
Now my question is how can it return a MPEG_AUDIO, WAV, BASIC_AUDIO as a supported output content descriptors for a video stream? I even tried to set these descriptors as the output descriptors but it gave me a process engine cannot realize error.
# 12
I forgot to add this question -
3.) I have observed that till there is EndOfStreamEvent from a DataSink and till we call DataSink.close( ), the file to which we are writing using the data sink won't get created and a EndOfStreamEvent will be called only when the corresponding Processor calls a stop( )
Now my question is that in case of a Capture Device like a webcam, there is a non stop stream of media supplied by it to the Processor. If thats the case how can the Processor ever recieve a EnfOfMediaEvent so that i can call Processor.stop( ) and then DataSinkc.close( ) so that the file containing the recorded data gets saved. Is there any way i can make the Processor receive a EndOfMediaEvent or will calling the stop from the processor call a EndOfMediaEvent?
# 13
From the earlier ABC.java code
private void startRecording(){
if(!bRecording){
try{
System.out.println("startRecording");
recordingDataSource = ((SourceCloneable)mainDataSource).createClone();
recordingProcessor = Manager.createProcessor(recordingDataSource);
recordingProcessor.addControllerListener(this);
recordingProcessor.configure();
bRecording = true;
}catch(Exception eX){
eX.printStackTrace();
}
}
}
private void stopRecording(){
if(bRecording){
System.out.println("stopRecording");
bRecording = false;
try{
recordingProcessor.close();
recordingDataSink.stop();
recordingDataSink.close();
}catch(Exception eX){
eX.printStackTrace();
}
}
}
For a webcam type app
The GUI wil have the original webcam stream as a cloneable datasource
This is always playing until you close the app
When the user 'starts recording' a clone of the original webcam datasource is established and used to create a recordingProcessor/DataSource/DataSink that remains active until 'stop Recording' is actioned
At this point the recordingProcessor/DataSource/DataSink cna be closed down
So there is no need for an EndOfMediaEvent in this scenario
# 14
Got it.
I tried to run ABC.java and it gave some problem. When i tried to stop the recording it all of a sudden gave errors.
So i wrote this code. I haven't displayed the visual component. The recording takes place right from the beginning. You would just have to press stop to stop recording.
import javax.swing.*;
import javax.media.*;
import java.net.*;
import java.io.*;
import javax.media.datasink.*;
import javax.media.protocol.*;
import javax.media.format.*;
import javax.media.control.*;
import javax.swing.*;
import javax.media.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;
class Abc implements ControllerListener, ActionListener
{
DataSource ds;
DataSink sink;
Processor pr;
MediaLocator mc;
JFrame jframe=new JFrame();
JButton jbutton=new JButton("Stop");
JPanel jpanel=new JPanel();
CaptureDeviceManager cdm=new CaptureDeviceManager();
CaptureDeviceInfo cdi;
MediaLocator cammc;
public void maam() throws Exception
{
jframe.setSize(300,300);
jpanel.setLayout(new BoxLayout(jpanel,BoxLayout.Y_AXIS));
jpanel.add(jbutton);
jframe.add(jpanel);
jbutton.addActionListener(this);
mc=new MediaLocator("file:C:/Workspace/aaaaa.mpg");
cdi= (CaptureDeviceInfo) cdm.getDeviceList(new VideoFormat(VideoFormat.RGB)).elementAt(0);
MediaLocator cammc=cdi.getLocator();
pr=Manager.createProcessor(cammc);
pr.addControllerListener(this);
pr.configure();
}
public void actionPerformed(ActionEvent e)
{
try
{
pr.stop();
sink.stop();
sink.close();
}
catch(Exception ep)
{
}
}
public void controllerUpdate(ControllerEvent e)
{
if(e instanceof ConfigureCompleteEvent)
{
System.out.println ("ConfigureCompleteEvent");
Processor p = (Processor)e.getSourceController();
System.out.println ("ConfigureCompleteEvent");
processConfigured(p);
}
if(e instanceof RealizeCompleteEvent)
{
try
{
Processor p = (Processor)e.getSourceController();
System.out.println ("RealizeCompleteEvent");
MediaLocator dest = new MediaLocator("file:C:/Workspace/ring3.mpg");
sink = Manager.createDataSink(p.getDataOutput(), dest);
processRealized(p);
}
catch(Exception ep)
{
}
}
if(e instanceof ControllerClosedEvent)
{
System.out.println ("ControllerClosedEvent");
}
if(e instanceof EndOfMediaEvent)
{
System.out.println ("EndOfMediaEvent");
Processor p = (Processor)e.getSourceController();
p.stop();
}
if(e instanceof StopEvent)
{
System.out.println ("StopEvent");
Processor p = (Processor)e.getSourceController();
p.close();
try
{
sink.stop();
sink.close();
}
catch(Exception ee)
{
}
}
}
public void processConfigured(Processor p)
{
System.out.println("Entered processConfigured");
p.setContentDescriptor (new FileTypeDescriptor (FileTypeDescriptor.MSVIDEO ));
p.realize();
}
public void processRealized(Processor p)
{
jframe.setVisible(true);
pr=p;
System.out.println("Entered processRealized");
try
{
sink.addDataSinkListener(new DataSinkListener()
{
public void dataSinkUpdate(DataSinkEvent e)
{
if(e instanceof EndOfStreamEvent)
{
System.out.println ("EndOfStreamEvent");
sink.close();
}
}
});
sink.open();
sink.start();
}
catch(Exception eX)
{
}
System.out.println("Just before start");
p.start();
}
}
public class Camera2
{
public static void main(String args[]) throws Exception
{
Abc a=new Abc();
a.maam();
}
}
( I can have a peaceful sleep tonight )
