📄 transmit.java
字号:
}
}
if(chosen!=null){
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";
// Realize the processor. This will internally create a flow
// graph and attempt to create an output datasource for JPEG/RTP
// audio frames.
result = stateListener.waitForState(processor, Controller.Realized);
if (result == false)
return "Couldn't realize processor";
// Set the JPEG quality to .5.
setJPEGQuality(processor, 0.25f);
// Get the output data source of the processor
dataOutput = processor.getDataOutput();
return null;
}
/**
* Use the RTPManager API to create sessions for each media
* track of the processor.
*/
private String createTransmitter(String localIpAddress) {
// Cheated. Should have checked the type.
PushBufferDataSource pbds = (PushBufferDataSource)dataOutput;
PushBufferStream pbss[] = pbds.getStreams();
rtpMgrs = new RTPManager[pbss.length];
SessionAddress localAddr, destAddr;
InetAddress ipAddr;
SendStream sendStream;
int localPort;
int destPort;
SourceDescription srcDesList[];
for (int i = 0; i < pbss.length; i++) {
try {
//New instance of RTPManager
//to handle the RTP and RTCP transmission
rtpMgrs[i] = RTPManager.newInstance();
destPort = sessionDescription.getDestinationPort() + 2*i;
ipAddr = InetAddress.getByName(sessionDescription.getAddress());
destAddr = new SessionAddress( ipAddr, destPort);
localPort = sessionDescription.getLocalPort() + 2*i;
localAddr = new SessionAddress(
InetAddress.getByName(localIpAddress),
localPort);
//Establishing the connection with the remote host
//with the chosen underlying protocol to RTP (either UDP or TCP)
if(sessionDescription.getTransportProtocol().toLowerCase().equals("tcp")){
if(receiver==null){
boolean connected=false;
System.out.println("Trying to connect to "+ipAddr+"/"+destPort);
//Trying to connect to the TCP Port of the remote host to establish a RTP connection
while(!connected){
try{
socketRTPTransmit = new Socket(ipAddr,destPort);
System.out.println("Socket connected to "+ipAddr+
" on port "+destPort);
connected=true;
}
catch(IOException ioe){
}
}
connected=false;
int rtcpDestPort=destPort+1;
int rtcpLocalPort=localPort+1;
System.out.println("Trying to connect to "+ipAddr+"/"+rtcpDestPort);
//Trying to connect to the TCP Port of the remote host to establish a RTCP connection
while(!connected){
try{
socketRTCPTransmit = new Socket(ipAddr, rtcpDestPort);
System.out.println("Control Socket connected to "+ipAddr+
" on port "+rtcpDestPort);
connected=true;
}
catch(IOException ioe){
}
}
}
else{
socketRTPTransmit=receiver.getSocketRTPReceiver();
socketRTCPTransmit=receiver.getSocketRTCPReceiver();
}
rtpMgrs[i].initialize( new TCPSendAdapter(socketRTPTransmit,socketRTCPTransmit));
}
else{
SessionAddress localAddress = new SessionAddress(
InetAddress.getByName(localIpAddress),
destPort);
rtpMgrs[i].initialize(localAddress);
SessionAddress destAddress = new SessionAddress(
InetAddress.getByName(sessionDescription.getAddress()),
destPort);
rtpMgrs[i].addTarget(destAddress);
}
System.err.println(
"Created RTP session: " +
sessionDescription.getAddress()+
" dest "+
destPort);
//Start the transmission with the remote host
sendStream = rtpMgrs[i].createSendStream(dataOutput, i);
sendStream.start();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* For JPEG and H263, we know that they only work for particular
* sizes. So we'll perform extra checking here to make sure they
* are of the right sizes.
*/
private 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)) {
// For JPEG, make sure width and height are divisible by 8.
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)) {
// For H.263, we only support some specific sizes.
if (size.width < 128) {
width = 128;
height = 96;
} else if (size.width < 176) {
width = 176;
height = 144;
} else {
width = 352;
height = 288;
}
} else {
// We don't know this particular format. We'll just
// leave it alone then.
return supported;
}
return (new VideoFormat(null,
new Dimension(width, height),
Format.NOT_SPECIFIED,
null,
Format.NOT_SPECIFIED)).intersects(supported);
}
/**
* Setting the encoding quality to the specified value on the JPEG encoder.
* 0.5 is a good default.
*/
void setJPEGQuality(Player p, float val) {
Control cs[] = p.getControls();
QualityControl qc = null;
VideoFormat jpegFmt = new VideoFormat(VideoFormat.JPEG);
// Loop through the controls to find the Quality control for
// the JPEG encoder.
for (int i = 0; i < cs.length; i++) {
if (cs[i] instanceof QualityControl && cs[i] instanceof Owned) {
Object owner = ((Owned)cs[i]).getOwner();
// Check to see if the owner is a Codec.
// Then check for the output format.
if (owner instanceof Codec) {
Format fmts[] = ((Codec)owner).getSupportedOutputFormats(null);
for (int j = 0; j < fmts.length; j++) {
if (fmts[j].matches(jpegFmt)) {
qc = (QualityControl)cs[i];
qc.setQuality(val);
System.err.println("- Setting quality to " +
val + " on " + qc);
break;
}
}
}
if (qc != null)
break;
}
}
}
/**
* Start the RTP transmission
*/
public void transmit(String localIpAddress){
Format fmt = null;
int i = 0;
// Start the transmission
String result = this.start(localIpAddress);
// result will be non-null if there was an error. The return
// value is a String describing the possible error. Print it.
if (result != null) {
System.err.println("Error : " + result);
//System.exit(0);
}
System.err.println("Start transmission... ");
}
public Socket getSocketRTPTransmit(){
return socketRTPTransmit;
}
public Socket getSocketRTCPTransmit(){
return socketRTCPTransmit;
}
/****************************************************************
* Sample Usage for AVTransmit class *
****************************************************************/
public static void main(String [] args) {
if (args.length < 1) {
prUsage();
}
// Create a transmit object with the specified params.
Transmit transmit = new Transmit(args[0]);
// Start the media transmission
String result = transmit.start("127.0.0.1");
// result will be non-null if there was an error. The return
// value is a String describing the possible error. Print it.
if (result != null) {
System.err.println("Error : " + result);
//System.exit(0);
}
System.err.println("Start transmission for 60 seconds...");
// Transmit for 60 seconds and then close the processor
// This is a safeguard when using a capture data source
// so that the capture device will be properly released
// before quitting.
// The right thing to do would be to have a GUI with a
// "Stop" button that would call stop on AVTransmit2
try {
Thread.sleep(60000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
// Stop the transmission
transmit.stop();
System.err.println("...transmission ended.");
}
static void prUsage() {
System.err.println("Usage: AVTransmit <IPAddress>/<LocalPort>/<DestPort>");
//System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -