?? avreceivermais.java
字號:
package net.java.mais.media;
/**
* <p>Title: Netsite TudoMais</p>
* <p>Description:JAIN-SIP Audio/Video phone application</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Organisation: CTBC Telecom / Netsite </p>
* @author Thiago Rocha Camargo (barata7@yahoo.com)
*/
import java.net.InetAddress;
import java.util.ArrayList;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.ControllerClosedEvent;
import javax.media.ControllerErrorEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.Player;
import javax.media.Processor;
import javax.media.RealizeCompleteEvent;
import javax.media.StartEvent;
import javax.media.control.BufferControl;
import javax.media.format.AudioFormat;
import javax.media.protocol.DataSource;
import javax.media.rtp.Participant;
import javax.media.rtp.RTPControl;
import javax.media.rtp.RTPManager;
import javax.media.rtp.ReceiveStream;
import javax.media.rtp.ReceiveStreamListener;
import javax.media.rtp.SendStream;
import javax.media.rtp.SendStreamListener;
import javax.media.rtp.SessionAddress;
import javax.media.rtp.SessionListener;
import javax.media.rtp.event.ActiveReceiveStreamEvent;
import javax.media.rtp.event.ByeEvent;
import javax.media.rtp.event.NewParticipantEvent;
import javax.media.rtp.event.NewReceiveStreamEvent;
import javax.media.rtp.event.ReceiveStreamEvent;
import javax.media.rtp.event.SendStreamEvent;
import javax.media.rtp.event.SessionEvent;
import javax.media.rtp.event.StreamMappedEvent;
import net.java.mais.common.Utils;
class AVReceiverMais
implements ReceiveStreamListener, SessionListener,
ControllerListener, SendStreamListener
{
private DataSource dataSource = null;
net.java.mais.media.MediaManager mediaManager;
ArrayList sessions = null;
RTPManager mgrs[] = null;
RTPManager rtpMan = null;
boolean dataReceived = false;
Object dataSync = new Object();
private int bindRetries = 1;
public static final int DEFAULT_BUFFER_LENGTH = 350;
public AVReceiverMais(ArrayList sessions, RTPManager rtpMan)
{
// JOptionPane.showMessageDialog(null,Thread.currentThread().getName());
this.sessions = sessions;
this.rtpMan = rtpMan;
String retries = null;
if((retries = Utils.getProperty("net.java.mais.media.RECEIVER_BIND_RETRIES")) != null)
try {
bindRetries = Integer.valueOf(retries).intValue();
}
catch (NumberFormatException ex) {
}
}
void setMediaManager(MediaManager mManager)
{
this.mediaManager = mManager;
}
protected boolean initialize()
{
try {
InetAddress ipAddr;
SessionAddress localAddr = new SessionAddress();
SessionAddress destAddr;
SessionLabel session;
// Open the RTP sessions.
for (int i = 0; i < sessions.size(); i++) {
// Parse the session addresses.
try {
session = new SessionLabel((String)sessions.get(i));
}
catch (IllegalArgumentException e) {
return false;
}
rtpMan.addSessionListener(this);
rtpMan.addReceiveStreamListener(this);
rtpMan.addSendStreamListener(this);
ipAddr = InetAddress.getByName(session.addr);
int tries = 0;
while (tries++ < bindRetries)
{
if (ipAddr.isMulticastAddress()) {
// local and remote address pairs are identical:
localAddr = new SessionAddress(ipAddr,
/*AVTransmitter.thePort*/session.port,
session.ttl);
destAddr = new SessionAddress(ipAddr,
/*AVTransmitter.thePort*/session.port,
session.ttl);
}
else {
localAddr = new SessionAddress(mediaManager.
getLocalHost(),
/*AVTransmitter.thePort*/session.port);
destAddr = new SessionAddress(ipAddr, session.port);
//
}
/* try {
mgrs[i].initialize(localAddr);
}
catch (Exception exc) {
if (tries < bindRetries) {
continue;
}
console.error(
"Could not initialize rtp manager!",exc);
return false;
}
*/
// You can try out some other buffer size to see
// if you can get better smoothness.
BufferControl bc = (BufferControl) rtpMan.getControl(
"javax.media.control.BufferControl");
if (bc != null) {
int bl = DEFAULT_BUFFER_LENGTH;
try {
bl = Integer.parseInt(Utils.getProperty(
"net.java.mais.media.MEDIA_BUFFER_LENGTH"));
}
catch (NumberFormatException ex) {
}
bc.setBufferLength(bl);
}
// mgrs[i].removeTarget(destAddr,"NO");
// mgrs[i].addTarget(destAddr);
break; //port retries
} //port retries
}
}
catch (Exception e) {
return false;
}
return true;
}
public boolean isDone()
{
return false;
}
/**
* Close the players and the session managers.
*/
protected void close()
{
try {
// close the RTP session.
for (int i = 0; i < mgrs.length; i++) {
if (mgrs[i] != null) {
mgrs[i].removeTargets("Closing session from AVReceiver");
mgrs[i].dispose();
mgrs[i] = null;
}
}
}
finally {
}
}
/**
* SessionListener.
* @todo I have the imperssion we're missing an event here.
*/
public synchronized void update(SessionEvent evt)
{
try {
if (evt instanceof NewParticipantEvent) {
Participant p = ( (NewParticipantEvent) evt).getParticipant();
}
else {
}
}
finally {
}
}
public synchronized void update(ActiveReceiveStreamEvent evt)
{
try {
}catch(Exception e){
}finally{
}
}
/**
* ReceiveStreamListener
*/
public synchronized void update(ReceiveStreamEvent evt)
{
try {
RTPManager mgr = (RTPManager) evt.getSource();
Participant participant = evt.getParticipant(); // could be null.
ReceiveStream stream = evt.getReceiveStream(); // could be null.
if (evt instanceof NewReceiveStreamEvent) {
try {
stream = ( (NewReceiveStreamEvent) evt).getReceiveStream();
DataSource ds = stream.getDataSource();
// Find out the formats.
RTPControl ctl = (RTPControl) ds.getControl(
"javax.media.rtp.RTPControl");
Player p = Manager.createPlayer(ds);
p.addControllerListener(this);
p.realize();
}
catch (Exception e) {
return;
}
}
else if (evt instanceof StreamMappedEvent) {
if (stream != null && stream.getDataSource() != null) {
DataSource ds = stream.getDataSource();
// Find out the formats.
RTPControl ctl = (RTPControl) ds.getControl(
"javax.media.rtp.RTPControl");
}
}
else if (evt instanceof ByeEvent) {
}else{
}
}
finally {
}
}
/**
* ControllerListener for the Players.
*/
public synchronized void controllerUpdate(ControllerEvent ce)
{
try {
Player p = (Player) ce.getSourceController();
if (p == null) {
return;
}
// Get this when the internal players are realized.
if (ce instanceof RealizeCompleteEvent) {
p.start();
}
if (ce instanceof StartEvent) {
mediaManager.firePlayerStarting(p);
}
if (ce instanceof ControllerErrorEvent) {
}
if (ce instanceof ControllerClosedEvent) {
mediaManager.firePlayerStopped();
}
}
finally {
}
}
/**
* A utility class to parse the session addresses.
*/
class SessionLabel
{
public String addr = null;
public int port;
public int remotePort;
public int ttl = 1;
public String content;
SessionLabel(String session) throws IllegalArgumentException
{
try {
String list[] = new String[5];
list = session.split("/");
for(int i=0;i<5;i++)
if(list[i]==null)
throw new IllegalArgumentException();
addr = list[0];
port = Integer.parseInt(list[1]);
remotePort = Integer.parseInt(list[2]);
ttl = Integer.parseInt(list[3]);
content = list[4];
}
finally {
}
}
}
public void update(SendStreamEvent event)
{
}
/*
private DataSource addDataSource(DataSource newDataSource)
{
if (dataSource == null) {
dataSource = newDataSource;
}
else {
try {
dataSource = Manager.createMergingDataSource(new DataSource[] {
dataSource,
newDataSource
});
}
catch (IncompatibleSourceException ex) {
Console.println(
"[AVReceiver]Failed to create a merging datasource. Old one is ignored!");
dataSource = newDataSource;
}
}
return dataSource;
}
*/
public static void main(String argv[]){
try{
SessionAddress localAddr = new SessionAddress(InetAddress.getByName(argv[3]), Integer.parseInt(argv[1]));
SessionAddress destAddr = new SessionAddress(InetAddress.getByName(argv[4]), Integer.parseInt(argv[2]));
CaptureDeviceInfo videoDevice = (CaptureDeviceInfo) CaptureDeviceManager.getDeviceList(new
AudioFormat(AudioFormat.LINEAR, 44100, 16, 1)).get(0);
DataSource videoDataSource = Manager.createDataSource(videoDevice.getLocator());
videoDataSource.connect();
Processor processor = Manager.createProcessor(videoDataSource);
RTPManager rtpMan = RTPManager.newInstance();
rtpMan.initialize(localAddr);
rtpMan.addTarget(destAddr);
SendStream sendStream= rtpMan.createSendStream(processor.getDataOutput(),0);
sendStream.setBitRate(56000);
sendStream.start();
Thread.currentThread().sleep(2000);
}catch(Exception e){
System.out.println("ERRO: "+e.toString());
}
}
} // end of AVReceive2
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -