名流时尚服饰 dior 夏季 男装 男士t恤衬衫卫衣休闲裤牛仔裤英伦
socool 搜酷女包◆任选两款正价包包邮◆5周年店庆◆5折疯抢
紫紫 超人气包邮特价创意家居收纳压缩袋饰品服饰配件包包
socool 搜酷女包◆任选两款正价包包邮◆5周年店庆◆5折疯抢
dior 风格 CF Homme 男装 男士t恤衬衫卫衣休闲裤牛仔裤英伦
非凡瑞丽衣衫 日韩 瑞丽 女装 韩版 2011 欧美 米娜 春夏装 女装
芮诗凯诗国际家居馆\欧美田园家居\复古家居\韩式家居\乡村家居
【徽商联盟】cool-百衣百顺 续写男装 冲双冠两件包邮
美美箱包专卖店 全场19元起,满58免运费,麦包包韩版女包新款。
import javax.media.rtp.*;
import javax.media.rtp.event.*;
import javax.media.rtp.rtcp.*;
import javax.media.protocol.*;
import javax.media.protocol.DataSource;
import javax.media.format.AudioFormat;
import javax.media.format.VideoFormat;
import javax.media.Format;
import javax.media.format.FormatChangeEvent;
import javax.media.control.BufferControl;
* AVReceive3 to receive RTP transmission using the RTPConnector.
public class AVReceive3 implements ReceiveStreamListener, SessionListener,
String sessions[] = null;
RTPManager mgrs[] = null;
Vector playerWindows = null;
boolean dataReceived = false;
Object dataSync = new Object();
public AVReceive3(String sessions[]) {
this.sessions = sessions;
protected boolean initialize() {
mgrs = new RTPManager[sessions.length];
playerWindows = new Vector();
// Open the RTP sessions.
for (int i = 0; i < sessions.length; i++) {
// Parse the session addresses.
session = new SessionLabel(sessions[i]);
} catch (IllegalArgumentException e) {
System.err.println("Failed to parse the session address given: " + sessions[i]);
System.err.println(" - Open RTP session for: addr: " + session.addr + " port: " + session.port + " ttl: " + session.ttl);
mgrs[i] = (RTPManager) RTPManager.newInstance();
mgrs[i].addSessionListener(this);
mgrs[i].addReceiveStreamListener(this);
// Initialize the RTPManager with the RTPSocketAdapter
mgrs[i].initialize(new RTPSocketAdapter(
InetAddress.getByName(session.addr),
session.port, session.ttl));
// You can try out some other buffer size to see
// if you can get better smoothness.
BufferControl bc = (BufferControl)mgrs[i].getControl("javax.media.control.BufferControl");
System.err.println("Cannot create the RTP Session: " + e.getMessage());
// Wait for data to arrive before moving on.
long then = System.currentTimeMillis();
long waitingPeriod = 30000; // wait for a maximum of 30 secs.
synchronized (dataSync) {
System.currentTimeMillis() - then < waitingPeriod) {
System.err.println(" - Waiting for RTP data to arrive");
System.err.println("No RTP data was received.");
public boolean isDone() {
return playerWindows.size() == 0;
* Close the players and the session managers.
for (int i = 0; i < playerWindows.size(); i++) {
((PlayerWindow)playerWindows.elementAt(i)).close();
playerWindows.removeAllElements();
// close the RTP session.
for (int i = 0; i < mgrs.length; i++) {
mgrs[i].removeTargets( "Closing session from AVReceive3");
PlayerWindow find(Player p) {
for (int i = 0; i < playerWindows.size(); i++) {
PlayerWindow pw = (PlayerWindow)playerWindows.elementAt(i);
PlayerWindow find(ReceiveStream strm) {
for (int i = 0; i < playerWindows.size(); i++) {
PlayerWindow pw = (PlayerWindow)playerWindows.elementAt(i);
public synchronized void update(SessionEvent evt) {
if (evt instanceof NewParticipantEvent) {
Participant p = ((NewParticipantEvent)evt).getParticipant();
System.err.println(" - A new participant had just joined: " + p.getCNAME());
public synchronized void update( ReceiveStreamEvent evt) {
RTPManager mgr = (RTPManager)evt.getSource();
Participant participant = evt.getParticipant(); // could be null.
ReceiveStream stream = evt.getReceiveStream(); // could be null.
if (evt instanceof RemotePayloadChangeEvent) {
System.err.println(" - Received an RTP PayloadChangeEvent.");
System.err.println("Sorry, cannot handle payload change.");
else if (evt instanceof NewReceiveStreamEvent) {
stream = ((NewReceiveStreamEvent)evt).getReceiveStream();
DataSource ds = stream.getDataSource();
RTPControl ctl = (RTPControl)ds.getControl("javax.media.rtp.RTPControl");
System.err.println(" - Recevied new RTP stream: " + ctl.getFormat());
System.err.println(" - Recevied new RTP stream");
System.err.println(" The sender of this stream had yet to be identified.");
System.err.println(" The stream comes from: " + participant.getCNAME());
// create a player by passing datasource to the Media Manager
Player p = javax.media.Manager.createPlayer(ds);
p.addControllerListener(this);
PlayerWindow pw = new PlayerWindow(p, stream);
playerWindows.addElement(pw);
// Notify intialize() that a new stream had arrived.
synchronized (dataSync) {
System.err.println("NewReceiveStreamEvent exception " + e.getMessage());
else if (evt instanceof StreamMappedEvent) {
if (stream != null && stream.getDataSource() != null) {
DataSource ds = stream.getDataSource();
RTPControl ctl = (RTPControl)ds.getControl("javax.media.rtp.RTPControl");
System.err.println(" - The previously unidentified stream ");
System.err.println(" " + ctl.getFormat());
System.err.println(" had now been identified as sent by: " + participant.getCNAME());
else if (evt instanceof ByeEvent) {
System.err.println(" - Got \"bye\" from: " + participant.getCNAME());
PlayerWindow pw = find(stream);
playerWindows.removeElement(pw);
* ControllerListener for the Players.
public synchronized void controllerUpdate(ControllerEvent ce) {
Player p = (Player)ce.getSourceController();
// Get this when the internal players are realized.
if (ce instanceof RealizeCompleteEvent) {
PlayerWindow pw = find(p);
// Some strange happened.
System.err.println("Internal error!");
if (ce instanceof ControllerErrorEvent) {
p.removeControllerListener(this);
PlayerWindow pw = find(p);
playerWindows.removeElement(pw);
System.err.println("AVReceive3 internal error: " + ce);
* A utility class to parse the session addresses.
public String addr = null;
SessionLabel(String session) throws IllegalArgumentException {
String portStr = null, ttlStr = null;
if (session != null && session.length() > 0) {
while (session.length() > 1 && session.charAt(0) == '/')
session = session.substring(1);
// Now see if there's a addr specified.
off = session.indexOf('/');
addr = session.substring(0, off);
session = session.substring(off + 1);
// Now see if there's a port specified
off = session.indexOf('/');
portStr = session.substring(0, off);
session = session.substring(off + 1);
// Now see if there's a ttl specified
off = session.indexOf('/');
ttlStr = session.substring(0, off);
throw new IllegalArgumentException();
Integer integer = Integer.valueOf(portStr);
port = integer.intValue();
throw new IllegalArgumentException();
throw new IllegalArgumentException();
Integer integer = Integer.valueOf(ttlStr);
ttl = integer.intValue();
throw new IllegalArgumentException();
* GUI classes for the Player.
class PlayerWindow extends Frame {
PlayerWindow(Player p, ReceiveStream strm) {
public void initialize() {
add(new PlayerPanel(player));
public void addNotify() {
* GUI classes for the Player.
class PlayerPanel extends Panel {
setLayout(new BorderLayout());
if ((vc = p.getVisualComponent()) != null)
if ((cc = p.getControlPanelComponent()) != null)
public Dimension getPreferredSize() {
Dimension size = vc.getPreferredSize();
Dimension size = cc.getPreferredSize();
return new Dimension(w, h);
public static void main(String argv[]) {
AVReceive3 avReceive = new AVReceive3(argv);
if (!avReceive.initialize()) {
System.err.println("Failed to initialize the sessions.");
// Check to see if AVReceive3 is done.
while (!avReceive.isDone())
System.err.println("Exiting AVReceive3");
System.err.println("Usage: AVReceive3 <session> <session> ");
System.err.println(" <session>: <address>/<port>/<ttl>");
import java.net.InetAddress;
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 com.sun.media.rtp.*;
public class AVTransmit3 {
// Can be a file or http or capture source
private MediaLocator locator;
private String ipAddress;
private Processor processor = null;
private RTPManager rtpMgrs[];
private DataSource dataOutput = null;
public AVTransmit3(MediaLocator locator,
this.ipAddress = ipAddress;
Integer integer = Integer.valueOf(pb);
this.portBase = integer.intValue();
* 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() {
// Create a processor for the specified media locator
// and program it to output JPEG/RTP
result = createProcessor();
// Create an RTP session to transmit the output of the
// processor to the specified IP address and port no.
result = createTransmitter();
// Start the transmission
* Stops the transmission if already started
for (int i = 0; i < rtpMgrs.length; i++) {
rtpMgrs[i].removeTargets( "Session ended.");
private String createProcessor() {
return "Locator is null";
ds = javax.media.Manager.createDataSource(locator);
return "Couldn't create DataSource";
// Try to create a processor to handle the input media locator
processor = javax.media.Manager.createProcessor(ds);
} catch (NoProcessorException npe) {
return "Couldn't create processor";
} catch (IOException ioe) {
return "IOException creating processor";
// Wait for it to configure
boolean result = waitForState(processor, Processor.Configured);
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);
boolean atLeastOneTrack = false;
for (int i = 0; i < tracks.length; i++) {
Format format = tracks[i].getFormat();
if (tracks[i].isEnabled()) {
supported = tracks[i].getSupportedFormats();
// We've set the output content to the RAW_RTP.
// So all the supported formats should work with RTP.
// We'll just pick the first one.
if (supported.length > 0) {
if (supported[0] instanceof VideoFormat) {
// For video formats, we should double check the
// sizes since not all formats work in all sizes.
chosen = checkForVideoSizes(tracks[i].getFormat(),
tracks[i].setFormat(chosen);
System.err.println("Track " + i + " is set to transmit as:");
System.err.println(" " + chosen);
tracks[i].setEnabled(false);
tracks[i].setEnabled(false);
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
result = waitForState(processor, Controller.Realized);
return "Couldn't realize processor";
// Set the JPEG quality to .5.
setJPEGQuality(processor, 0.5f);
// Get the output data source of the processor
dataOutput = processor.getDataOutput();
* Use the RTPManager API to create sessions for each media
* track of the processor.
private String createTransmitter() {
// Cheated. Should have checked the type.
PushBufferDataSource pbds = (PushBufferDataSource)dataOutput;
PushBufferStream pbss[] = pbds.getStreams();
rtpMgrs = new RTPManager[pbss.length];
SourceDescription srcDesList[];
for (int i = 0; i < pbss.length; i++) {
rtpMgrs[i] = RTPManager.newInstance();
// Initialize the RTPManager with the RTPSocketAdapter
rtpMgrs[i].initialize(new RTPSocketAdapter(
InetAddress.getByName(ipAddress),
System.err.println( "Created RTP session: " + ipAddress + " " + port);
sendStream = rtpMgrs[i].createSendStream(dataOutput, i);
* 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.
Format checkForVideoSizes(Format original, Format supported) {
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.
} else if (size.width < 176) {
// We don't know this particular format. We'll just
return (new VideoFormat(null,
new Dimension(width, height),
Format.NOT_SPECIFIED)).intersects(supported);
* Setting the encoding quality to the specified value on the JPEG encoder.
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
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];
System.err.println("- Setting quality to " +
/****************************************************************
* Convenience methods to handle processor's state changes.
****************************************************************/
private Integer stateLock = new Integer(0);
private boolean failed = false;
private synchronized boolean waitForState(Processor p, int state) {
p.addControllerListener(new StateListener());
// Call the required method on the processor
if (state == Processor.Configured) {
} else if (state == Processor.Realized) {
// Wait until we get an event that confirms the
// success of the method, or a failure event.
// See StateListener inner class
while (p.getState() < state && !failed) {
synchronized (getStateLock()) {
} catch (InterruptedException ie) {
/****************************************************************
****************************************************************/
class StateListener implements ControllerListener {
public void controllerUpdate(ControllerEvent ce) {
// If there was an error during configure or
// realize, the processor will be closed
if (ce instanceof ControllerClosedEvent)
// All controller events, send a notification
// to the waiting thread in waitForState method.
if (ce instanceof ControllerEvent) {
synchronized (getStateLock()) {
getStateLock().notifyAll();
/****************************************************************
* Sample Usage for AVTransmit3 class
****************************************************************/
public static void main(String [] args) {
// We need three parameters to do the transmission
// java AVTransmit3 file:/C:/media/test.mov 129.130.131.132 42050
// Create a audio transmit object with the specified params.
AVTransmit3 at = new AVTransmit3(new MediaLocator(args[i]),
args[i+1], args[i+2], fmt);
// Start the transmission
String result = at.start();
// result will be non-null if there was an error. The return
// value is a String describing the possible error. Print it.
System.err.println("Error : " + result);
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
// The right thing to do would be to have a GUI with a
// "Stop" button that would call stop on AVTransmit3
Thread.currentThread().sleep(60000);
} catch (InterruptedException ie) {
System.err.println("transmission ended.");
System.err.println("Usage: AVTransmit3 <sourceURL> <destIP> <destPortBase>");
System.err.println(" <sourceURL>: input URL or file name");
System.err.println(" <destIP>: multicast, broadcast or unicast IP address for the transmission");
System.err.println(" <destPortBase>: network port numbers for the transmission.");
System.err.println(" The first track will use the destPortBase.");
System.err.println(" The next track will use destPortBase + 2 and so on.\n");
import java.io.IOException;
import java.net.InetAddress;
import java.net.DatagramSocket;
import java.net.MulticastSocket;
import java.net.DatagramPacket;
import java.net.SocketException;
import javax.media.protocol.DataSource;
import javax.media.protocol.PushSourceStream;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.SourceTransferHandler;
import javax.media.rtp.RTPConnector;
import javax.media.rtp.OutputDataStream;
* An implementation of RTPConnector based on UDP sockets.
public class RTPSocketAdapter implements RTPConnector {
SockInputStream dataInStrm = null, ctrlInStrm = null;
SockOutputStream dataOutStrm = null, ctrlOutStrm = null;
public RTPSocketAdapter(InetAddress addr, int port) throws IOException {
public RTPSocketAdapter(InetAddress addr, int port, int ttl) throws IOException {
if (addr.isMulticastAddress()) {
dataSock = new MulticastSocket(port);
ctrlSock = new MulticastSocket(port+1);
((MulticastSocket)dataSock).joinGroup(addr);
((MulticastSocket)dataSock).setTimeToLive(ttl);
((MulticastSocket)ctrlSock).joinGroup(addr);
((MulticastSocket)ctrlSock).setTimeToLive(ttl);
dataSock = new DatagramSocket(port, InetAddress.getLocalHost());
ctrlSock = new DatagramSocket(port+1, InetAddress.getLocalHost());
} catch (SocketException e) {
throw new IOException(e.getMessage());
* Returns an input stream to receive the RTP data.
public PushSourceStream getDataInputStream() throws IOException {
if (dataInStrm == null) {
dataInStrm = new SockInputStream(dataSock, addr, port);
* Returns an output stream to send the RTP data.
public OutputDataStream getDataOutputStream() throws IOException {
dataOutStrm = new SockOutputStream(dataSock, addr, port);
* Returns an input stream to receive the RTCP data.
public PushSourceStream getControlInputStream() throws IOException {
if (ctrlInStrm == null) {
ctrlInStrm = new SockInputStream(ctrlSock, addr, port+1);
* Returns an output stream to send the RTCP data.
public OutputDataStream getControlOutputStream() throws IOException {
ctrlOutStrm = new SockOutputStream(ctrlSock, addr, port+1);
* Close all the RTP, RTCP streams.
* Set the receive buffer size of the RTP data channel.
* This is only a hint to the implementation. The actual implementation
* may not be able to do anything to this.
public void setReceiveBufferSize( int size) throws IOException {
dataSock.setReceiveBufferSize(size);
* Get the receive buffer size set on the RTP data channel.
* Return -1 if the receive buffer size is not applicable for
public int getReceiveBufferSize() {
return dataSock.getReceiveBufferSize();
* Set the send buffer size of the RTP data channel.
* This is only a hint to the implementation. The actual implementation
* may not be able to do anything to this.
public void setSendBufferSize( int size) throws IOException {
dataSock.setSendBufferSize(size);
* Get the send buffer size set on the RTP data channel.
* Return -1 if the send buffer size is not applicable for
public int getSendBufferSize() {
return dataSock.getSendBufferSize();
* Return the RTCP bandwidth fraction. This value is used to
* initialize the RTPManager. Check RTPManager for more detauls.
* Return -1 to use the default values.
public double getRTCPBandwidthFraction() {
* Return the RTCP sender bandwidth fraction. This value is used to
* initialize the RTPManager. Check RTPManager for more detauls.
* Return -1 to use the default values.
public double getRTCPSenderBandwidthFraction() {
* An inner class to implement an OutputDataStream based on UDP sockets.
class SockOutputStream implements OutputDataStream {
public SockOutputStream(DatagramSocket sock, InetAddress addr, int port) {
public int write(byte data[], int offset, int len) {
sock.send(new DatagramPacket(data, offset, len, addr, port));
* An inner class to implement an PushSourceStream based on UDP sockets.
class SockInputStream extends Thread implements PushSourceStream {
boolean dataRead = false;
SourceTransferHandler sth = null;
public SockInputStream(DatagramSocket sock, InetAddress addr, int port) {
public int read(byte buffer[], int offset, int length) {
DatagramPacket p = new DatagramPacket(buffer, offset, length, addr, port);
} catch (IOException e) {
public synchronized void start() {
public synchronized void kill() {
public int getMinimumTransferSize() {
return 2 * 1024; // twice the MTU size, just to be safe.
public synchronized void setTransferHandler(SourceTransferHandler sth) {
public ContentDescriptor getContentDescriptor() {
public long getContentLength() {
public boolean endOfStream() {
public Object[] getControls() {
public Object getControl(String type) {
* Loop and notify the transfer handler of new data.
while (!dataRead && !done) {
} catch (InterruptedException e) { }
if (sth != null && !done) {
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/09/28/909970.html,如需转载请自行联系原作者