?? rtptransmit.java
字號:
package RTPTransmit;
import java.io.*;
import java.awt.Dimention;
import java.net.InetAddress;
import javax.media.*;
import javax.media.protocol.*;
import javax.media.protocol.DataSource;
import javax.media.format.*;
import javax.media.control.TrackControl;
import javax.media.rtp.*;
public class RTPTransmit
{
private MediaLocator locator;
private String ipAddress;
private int portBase;
private Processor processor = null;
private RTPManager rtpMgrs[];
private DataSource DataOutput = null;
public RTPTransmit(MediaLocator locator,String ipAddress,String pb,Format iformat)
{
this.locator = locator;
this.ipAddress = ipAddress;
Integer integer = Integer.valueOf(pb);
if(integer!=null)
this.portBase = integer.intValue();
}
public synchronized String start()
{
String result;
result = createProcessor();
if(result != null)
return result;
result = createTransmitter();
if(result != null)
{
processor.close();
processor = null;
return result;
}
processor.start();
return null;
}
public String createProcessor()
{
if(locator == null)
return "locator is null";
DataSource ds;
try
{
ds = javax.media.Manager.createDataSource(locator);
}
catch (Exception e)
{
return "Couldn't create DataSource";
}
try
{
processor = javax.media.Manager.createProcessor(ds);
}
catch (NoPrecessorException npe)
{
return "Couldn't create processor";
}
catch (IOException ioe)
{
return "IOException creating processor";
}
boolean result = waitForState(processor,Processor.Configured);
if(result == false)
return "Couldn't configure processor";
TrackControl [] tracks = processor.getTrackControls();
if(tracks == null ||tracks.length<1)
return "Couldn't find tracks in processor";
ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);
processor.setContentDescriptor(cd);
Format supported[];
Format chosen = null;
boolean atLeastOneTrack = false;
for(int i=0;i<tracks.length;i++)
{
Format iformat = tracks[i].getFormat();
if(tracks[i].isEnable())
{
supported = tracks[i].getSupportedFormats();
if(supported.length>0)
{
if(supported[0] instanceof VideoFormat)
{
chosen = checkForVideoSizes(tracks[i].getFormat(),supported[0]);
}
else
chosen = supported[0];
tracks[i].setFormat(chosen);
System.err.println("Track "+i+" is set to transmit as:");
System.err.println(""+chosen);
atLeastOneTrack = true;
}
else
tracks[i].setEnabled(false);
}
else
tracks[i].setEnabled(false);
}
if(!atLeastOneTrack)
return "Couldn't set any of the tracks to a valid RTP format";
result = waitForState(processor,Controller.Realized);
if(result == false)
return "Couldn't realize processor";
DataOutput = precessor.getDataOutput();
return null;
}
private String createTransmitter()
{
PushBufferDataSource pbds = (PushBufferDataSource)DataOutput;
PushBufferStream pbss[] = pbds.getStreams();
rtpMgrs = new RTPManager[pbss.length];
for(int i=0;i<pbss.length;i++)
{
try
{
rtpMgrs[i] = RTPManager.newInstance();
int port = portBase +2*i;
InetAddress ipAddr = InetAddress.getByName(ipAddress);
SessionAddress localAddr = new SessionAddress(InetAddress.getLocalHost(),port);
SessionAddress destAddr = new SessionAddress(ipAddr,port);
rtpMgrs[i].initialize(localAddr);
rtpMgrs[i].addTarget(destAddr);
System.err.println("Created RTP session:"+ipAddress+""+port);
SendStream sendStream = rtpMgrs[i].createSendStream(DataOutput,i);
sendStream.start();
}
catch(Exception e)
{
return e.getMessage();
}
}
return null;
}
Format checkForVideoSizes(Format original,Format supported)
{
int width,height;
Dimension size = ((VideoFormat)original).getSize();
Format jpegFmt = new Format(VideoFormat.JPEG_RTP);
Format H263Fmt = new Format(VideoFormat.H263_RTP);
if(supported.matches(jpegFmt))
{
width = size.width % 8 == 0?size.width:((int)(size.width/8)*8);
height = size.height % 8 == 0?size.height:((int)(size.height/8)*8);
}
else if(supported.matches(h263Fmt))
{
if(size.width<=128){width = 128;height = 96;}
else if(size.width<=176){width = 176;height = 144;}
else{width = 352;height = 288;}
}
else
{
return supported;
}
return (new VideoFormat(null,new Dimension(width,height),Format.NOT_SPECIFIED,null,Format.NOT_SPECIFIED)).intersects(supported);
}
public void stop()
{
synchronized(this)
{
if(precessor != null)
{
precessor.stop();
precessor.close();
precessor = null;
for(int i=0;i<rtpMgrs.length;i++)
{
rtpMgrs[i].removeTargets("Session ended.")
rtpMgrs[i].dispose();
}
}
}
}
/*-------------------以下兩個變量為對處理器狀態改變的處理服務--------------*/
private Integer stateLock = new Integer(0);
private boolean failed = false;
Integer getstateLock()
{
return stateLock;
}
void setFailed()
{
failed = true;
}
private synchronized boolean waitForState(Processor p,int state)
{
p.addControllerListener(new StateListener());
failed = false;
if(state == Processor.Configured)
{
p.configure();
}
else if(state == Processor.Realized)
{
p.realized();
}
while(p.getState() < state&&!failed)
{
synchronized (getstateLock())
{
try
{
getstateLock().wait();
}
catch (InterruptedException ie)
{
return false;
}
}
}
if(failed)
return false;
else
return true;
}
/*-------------------內部監聽類:處理器的狀態監聽器--------------*/
class StateListener implements ControllerListener
{
public void ControllerUpdate(ControllerEvent ce)
{
if(ce instanceof ControllerClosedEvent)
setFailed();
if(ce instanceof ControllerEvent)
{
synchronized (getStateLock())
{
getStateLock().notifyAll();
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -