?? transmit.java
字號:
/*
* Transmit.java
*
* Created on November 19, 2003, 10:38 AM
*/
package gov.nist.applet.phone.media.transmitter;
import java.awt.*;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import javax.media.*;
import java.util.*;
import javax.media.protocol.*;
import javax.media.protocol.DataSource;
import javax.media.format.*;
import javax.media.control.TrackControl;
import javax.media.control.QualityControl;
import javax.media.rtp.*;
import javax.media.rtp.rtcp.*;
import gov.nist.applet.phone.media.util.*;
import gov.nist.applet.phone.media.receiver.*;
import gov.nist.applet.phone.media.protocol.transport.*;
/**
* Class to send RTP using the JMF RTP API.
* @author DERUELLE Jean
*/
public class Transmit {
private Processor processor = null;
private RTPManager rtpMgrs[] = null;
private DataSource dataOutput = null;
private StateListener stateListener=null;
private SessionDescription sessionDescription =null;
private String session=null;
private MediaLocator videoLocator=null;
private MediaLocator audioLocator=null;
private RTPManager rtpManager = null;
private SessionAddress remoteAddress =null;
private Receiver receiver=null;
private Socket socketRTPTransmit=null;
private Socket socketRTCPTransmit=null;
/**
* Constructor for Transmitter
* @param session - the concatened parameters of the session stored in a string
*/
public Transmit(String session) throws IllegalArgumentException{
this.session=session;
stateListener=new StateListener();
//the session Label containing the address, the port and the Time To Live
try {
//create a session label on the session given in argument
// and parse the session address.
sessionDescription =new SessionDescription(session);
sessionDescription.setAudioFormat("6");
sessionDescription.setVideoFormat("h263/rtp");
sessionDescription.setTransportProtocol("udp");
} catch (IllegalArgumentException e) {
System.err.println("Failed to parse the session address given: " + session);
throw e;
}
if(!initialize()){
System.out.println("Bad Session intialization");
//System.exit(0);
}
}
/**
* Constructor for Transmitter
* @param session - the session Description containing the address, the port, the Time To Live
* the video format, the audio format and the transport protocol
*/
public Transmit(SessionDescription session,Receiver receiver) {
this.receiver=receiver;
this.sessionDescription=session;
stateListener=new StateListener();
if(!initialize()){
System.out.println("Bad Session intialization");
//System.exit(0);
}
}
/**
* get the devices for the capture and print their formats
* @return true if all has been initialized correctly
*/
protected boolean initialize() {
CaptureDeviceInfo videoCDI=null;
CaptureDeviceInfo audioCDI=null;
Vector captureDevices=null;
captureDevices= CaptureDeviceManager.getDeviceList(null);
System.out.println("- number of capture devices: "+captureDevices.size() );
CaptureDeviceInfo cdi=null;
for (int i = 0; i < captureDevices.size(); i++) {
cdi = (CaptureDeviceInfo) captureDevices.elementAt(i);
//System.out.println(" - name of the capture device: "+cdi.getName() );
Format[] formatArray=cdi.getFormats();
for (int j = 0; j < formatArray.length; j++) {
Format format=formatArray[j];
if (format instanceof VideoFormat) {
//System.out.println(" - format accepted by this VIDEO device: "+
//format.toString().trim());
if (videoCDI ==null) {
videoCDI=cdi;
}
}
else if (format instanceof AudioFormat) {
//System.out.println(" - format accepted by this AUDIO device: "+
//format.toString().trim());
if (audioCDI == null) {
audioCDI=cdi;
}
}
//else
//System.out.println(" - format of type UNKNOWN");
}
}
if(videoCDI!=null)
videoLocator=videoCDI.getLocator();
if(audioCDI!=null)
audioLocator=audioCDI.getLocator();
return true;
}
/**
* Starts the transmission. Returns null if transmission started ok.
* Otherwise it returns a string with the reason why the setup failed.
*/
public synchronized String start(String localIpAddress) {
String result;
// Create a processor for the specified media locator
// and program it to output JPEG/RTP
result = createProcessor();
if (result != null)
return result;
// Create an RTP session to transmit the output of the
// processor to the specified IP address and port no.
result = createTransmitter(localIpAddress);
if (result != null) {
processor.close();
processor = null;
return result;
}
// Start the transmission
processor.start();
return null;
}
/**
* Stops the transmission if already started
*/
public void stop() {
synchronized (this) {
if (processor != null) {
processor.stop();
processor.close();
processor = null;
}
if(rtpMgrs!=null){
for (int i = 0; i < rtpMgrs.length; i++) {
if(rtpMgrs[i]!=null){
rtpMgrs[i].removeTargets( "Session ended.");
rtpMgrs[i].dispose();
rtpMgrs[i]=null;
}
}
}
}
}
/**
* Creates the processor
*/
private String createProcessor() {
DataSource audioDS=null;
DataSource videoDS=null;
DataSource mergeDS=null;
StateListener stateListener=new StateListener();
//create the DataSource
//it can be a 'video' DataSource, an 'audio' DataSource
//or a combination of audio and video by merging both
if (videoLocator == null && audioLocator == null)
return "Locator is null";
if (audioLocator != null){
try {
//create the 'audio' DataSource
audioDS= javax.media.Manager.createDataSource(audioLocator);
} catch (Exception e) {
System.out.println("-> Couldn't connect to audio capture device");
}
}
if (videoLocator != null){
try {
//create the 'video' DataSource
videoDS = javax.media.Manager.createDataSource(videoLocator);
} catch (Exception e) {
System.out.println("-> Couldn't connect to video capture device");
}
}
if(videoDS!=null && audioDS!=null){
try {
//create the 'audio' and 'video' DataSource
mergeDS = javax.media.Manager.createMergingDataSource(new DataSource [] {audioDS, videoDS});
} catch (Exception e) {
System.out.println("-> Couldn't connect to audio or video capture device");
}
try{
//Create the processor from the merging DataSource
processor = javax.media.Manager.createProcessor(mergeDS);
}
catch (NoProcessorException npe) {
return "Couldn't create processor";
} catch (IOException ioe) {
return "IOException creating processor";
}
}
//if the processor has not been created from the merging DataSource
if(processor==null){
try {
if(audioDS!=null)
//Create the processor from the 'audio' DataSource
processor = javax.media.Manager.createProcessor(audioDS);
else
//Create the processor from the 'video' DataSource
processor = javax.media.Manager.createProcessor(videoDS);
} catch (NoProcessorException npe) {
return "Couldn't create processor";
} catch (IOException ioe) {
return "IOException creating processor";
}
}
// Wait for it to configure
boolean result = stateListener.waitForState(processor, Processor.Configured);
if (result == false)
return "Couldn't configure processor";
// Get the tracks from the processor
TrackControl [] tracks = processor.getTrackControls();
// Do we have atleast one track?
if (tracks == null || tracks.length < 1)
return "Couldn't find tracks in processor";
// Set the output content descriptor to RAW_RTP
// This will limit the supported formats reported from
// Track.getSupportedFormats to only valid RTP formats.
ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);
processor.setContentDescriptor(cd);
Format supported[];
Format chosen=null;
boolean atLeastOneTrack = false;
// Program the tracks.
for (int i = 0; i < tracks.length; i++) {
Format format = tracks[i].getFormat();
if (tracks[i].isEnabled()) {
/*if (tracks[i] instanceof VideoFormat)
System.out.println("Supported Video Formats :");
else
System.out.println("Supported Audio Formats :");*/
supported = tracks[i].getSupportedFormats();
/*System.out.println("track : "+ i);
for(int j=0;j<supported.length;j++)
System.out.println("Supported format : "+supported[j].getEncoding());*/
// We've set the output content to the RAW_RTP.
// So all the supported formats should work with RTP.
if (supported.length > 0) {
for(int j=0;j<supported.length;j++){
//System.out.println("Supported format : "+supported[j].toString().toLowerCase());
if (supported[j] instanceof VideoFormat) {
// For video formats, we should double check the
// sizes since not all formats work in all sizes.
if(sessionDescription.getVideoFormat()!=null)
if(supported[j].toString().toLowerCase().indexOf(
sessionDescription.getVideoFormat().toLowerCase())!=-1)
chosen = checkForVideoSizes(tracks[i].getFormat(),
supported[j]);
} else {
if(sessionDescription.getAudioFormat()!=null)
if(supported[j].toString().toLowerCase().indexOf(
sessionDescription.getAudioFormat().toLowerCase())!=-1)
chosen = supported[j];
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -