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

📄 recorder.java

📁 this is the media streaming project
💻 JAVA
字号:
/****************************************************************************************/
/* 2001-Spring: Java Network-Programming Term-Project					*/
/* Title: Streaming media generation, capture and store.				*/
/* Team Member: Yumin Yuan(yuany@rpi.edu), Rui Mu(mur@rpi.edu), Yining Hu(huyn@rpi.edu) */
/* Recorder.java: This class sets up necessary params to save the streaming media.	*/
/* Complile: javac Recorder.java							*/
/****************************************************************************************/

import java.net.*;
import java.awt.Dimension;
import javax.media.format.*;
import javax.media.*;
import javax.media.protocol.*;
import javax.media.control.TrackControl;
import java.io.File;
import java.io.IOException;
import com.sun.media.format.WavAudioFormat;

//Record RTP video information, change to quicktime(.mov) format)
public class Recorder{
    Processor audioProcessor=null,videoProcessor,mergeProcessor;
    DataSource videoDS=null,audioDS=null,mergeDS=null;
    DataSink filewriter = null;

    String fileURL;

    public Recorder(String _fileURL){
	fileURL=_fileURL;
    }
    public void prepareVideo(DataSource _video){
	try{
	    videoProcessor=Manager.createProcessor(_video);
	    StateHelper sh=new StateHelper(videoProcessor);
	    sh.waitForState(Processor.Configured);
	 
	    ContentDescriptor cd=videoProcessor.setContentDescriptor(
							    new ContentDescriptor( ContentDescriptor.RAW));
	    videoProcessor.setContentDescriptor(cd);
	    TrackControl tracks[] = videoProcessor.getTrackControls();
	    boolean encodingOK = false;
	    for (int i = 0; i < tracks.length; i++) {
		Format format = tracks[i].getFormat();
		if (  tracks[i].isEnabled() &&
		      format instanceof VideoFormat &&
		      !encodingOK) {
		
		// Found a video track. Try to program it to output JPEG
		    Dimension size = ((VideoFormat)format).getSize();
		    float frameRate = ((VideoFormat)format).getFrameRate();
		    VideoFormat jpegFormat = new VideoFormat(VideoFormat.JPEG,
							 size,
							 Format.NOT_SPECIFIED,
							 Format.byteArray,
							 frameRate);
		    tracks[i].setFormat(jpegFormat);
		    encodingOK = true;
		} else
		    tracks[i].setEnabled(false);
	    }

	    
	    if (encodingOK) {
		 boolean result = sh.waitForState(Processor.Realized);
		 if (result == false)
		     videoDS = null;
		 videoDS = videoProcessor.getDataOutput();
	    }
	}
	catch (Exception e){
	    System.out.println(e);
	}
    }

    //start to record file
    public void start(){	
	try{
	    DataSource[] combine={videoDS};
	    DataSource mergeDS=Manager.createMergingDataSource(combine);
	    mergeProcessor=Manager.createProcessor(mergeDS);
	    StateHelper sh=new StateHelper(mergeProcessor);

	    sh.waitForState(Processor.Configured);
	    mergeProcessor.setContentDescriptor(new FileTypeDescriptor(FileTypeDescriptor.QUICKTIME));
	    sh.waitForState(Processor.Realized);
	    DataSource source = mergeProcessor.getDataOutput();

	    // create a File protocol MediaLocator with the location of the
	    // file to which the data is to be written
	    
	    MediaLocator dest = new MediaLocator("file:"+fileURL);
	
	    filewriter = Manager.createDataSink(source, dest);
	    filewriter.open();
	    filewriter.start();
         
	    videoProcessor.start();
	    mergeProcessor.start();
	} catch (NoDataSinkException e) {		   
	    System.out.println(e);
	} catch (java.io.IOException e) {
	    System.exit(-1);
	} catch (SecurityException e) {
	    System.exit(-1);
	}
	catch (IncompatibleSourceException e){
	    System.err.println(e);
	    System.exit(-1);
	}catch (NoProcessorException e){
	     System.err.println(e);
	    System.exit(-1);
	}
     
    }

    public void stop(){
	mergeProcessor.close();
	filewriter.close();
	System.err.println("correctly exit");
	videoProcessor.close();   
    }
}   

⌨️ 快捷键说明

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