📄 mediamanager.java
字号:
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
package net.java.mais.media;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Format;
import javax.media.IncompatibleSourceException;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSourceException;
import javax.media.NoProcessorException;
import javax.media.Player;
import javax.media.Processor;
import javax.media.control.MonitorControl;
import javax.media.control.TrackControl;
import javax.media.format.AudioFormat;
import javax.media.format.VideoFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;
import javax.media.rtp.RTPManager;
import javax.media.rtp.SendStream;
import javax.media.rtp.SessionAddress;
import javax.sdp.Connection;
import javax.sdp.Media;
import javax.sdp.MediaDescription;
import javax.sdp.Origin;
import javax.sdp.SdpConstants;
import javax.sdp.SdpException;
import javax.sdp.SdpFactory;
import javax.sdp.SdpParseException;
import javax.sdp.SessionDescription;
import javax.sdp.SessionName;
import javax.sdp.TimeDescription;
import javax.sdp.Version;
import net.java.mais.common.NetworkAddressManager;
import net.java.mais.common.PropertiesDepot;
import net.java.mais.common.Utils;
import net.java.mais.media.event.MediaErrorEvent;
import net.java.mais.media.event.MediaEvent;
import net.java.mais.media.event.MediaListener;
import net.java.mais.media.jmf.MonitorCDS;
/**
* <p>Title: SIP COMMUNICATOR</p>
* <p>Description:JAIN-SIP Audio/Video phone application</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Organisation: LSIIT laboratory (http://lsiit.u-strasbg.fr) </p>
* <p>Network Research Team (http://www-r2.u-strasbg.fr))</p>
* <p>Louis Pasteur University - Strasbourg - France</p>
* <p>Division Chief: Thomas Noel </p>
* @author Emil Ivov (http://www.emcho.com)
* @author Paulo Pizzarro ( added support for media level connection parameter)
* @version 1.1
**/
public class MediaManager
implements Serializable
{
protected ArrayList listeners = new ArrayList();
protected Vector avTransmitters = new Vector();
protected SdpFactory sdpFactory;
public boolean mute=false;
public boolean video=true;
protected ProcessorUtility procUtility = new ProcessorUtility();
private boolean camera = true;
// Ports
String audioPort = "22224";
String videoPort = "22222";
//media devices
protected CaptureDeviceInfo audioDevice = null;
protected CaptureDeviceInfo videoDevice = null;
//Sdp Codes of all formats supported for
//transmission by the selected datasource
protected ArrayList transmittableVideoFormats = new ArrayList();
protected ArrayList transmittableAudioFormats = new ArrayList();
//Sdp Codes of all formats that we can receive
//i.e. all formats supported by JMF
protected String[] receivableVideoFormats = new String[] {
//sdp format // corresponding JMF Format
Integer.toString(SdpConstants.H263), // javax.media.format.VideoFormat.H263_RTP
Integer.toString(SdpConstants.JPEG), // javax.media.format.VideoFormat.JPEG_RTP
Integer.toString(SdpConstants.H261) // javax.media.format.VideoFormat.H261_RTP
};
protected String[] receivableAudioFormats = new String[] {
//sdp format // corresponding JMF Format
Integer.toString(SdpConstants.G723), // javax.media.format.AudioFormat.G723_RTP
Integer.toString(SdpConstants.PCMU), // javax.media.format.AudioFormat.ULAW_RTP;
Integer.toString(SdpConstants.GSM), // javax.media.format.AudioFormat.GSM_RTP;
Integer.toString(SdpConstants.DVI4_8000), // javax.media.format.AudioFormat.DVI_RTP;
Integer.toString(SdpConstants.DVI4_16000), // javax.media.format.AudioFormat.DVI_RTP;
Integer.toString(SdpConstants.PCMA), // javax.media.format.AudioFormat.ALAW;
Integer.toString(SdpConstants.G728) // javax.media.format.AudioFormat.G728_RTP;
//g729 is not suppported by JMF
// Integer.toString(SdpConstants.G729) // javax.media.format.AudioFormat.G729_RTP
};
/**
* A list of currently active RTPManagers mapped against Local session addresses.
* The list is used by transmitters and receivers so that receiving and transmitting
* from the same port simultaneousl is possible
*/
Hashtable activeRtpManagers = new Hashtable();
protected String mediaSource = null;
//only use these for initialisation and for
// protected DataSource audioDataSource = null;
// protected DataSource videoDataSource = null;
protected DataSource avDataSource = null;
protected Processor processor = null;
protected boolean isStarted = false;
public MediaManager()
{
}
public void setCamera(boolean b){
camera=b;
}
public boolean getCamera(){
return camera;
}
public void start() throws MediaException
{
try {
try {
sdpFactory = SdpFactory.getInstance();
}
catch (SdpException exc) {
throw new MediaException("Failed to create sdpFactory", exc);
}
//init jmf capture devices
setupJMF();
mediaSource = Utils.getProperty(
"net.java.mais.media.MEDIA_SOURCE");
//Init Capture devices
DataSource audioDataSource = null;
DataSource videoDataSource = null;
if (mediaSource == null) {
//audio device
Vector audioDevices = CaptureDeviceManager.getDeviceList(new
AudioFormat(AudioFormat.LINEAR, 44100, 16, 1));
if (audioDevices.size() > 0) {
audioDevice = (CaptureDeviceInfo) audioDevices.get(0);
audioDataSource = createDataSource(audioDevice.getLocator());
}
else
{
}
if(camera){
//video device
Vector videoDevices = CaptureDeviceManager.getDeviceList(new
VideoFormat(VideoFormat.RGB));
if (videoDevices.size() > 0) {
videoDevice = (CaptureDeviceInfo) videoDevices.get(0);
MediaLocator ml = videoDevice.getLocator();
videoDataSource = createDataSource(ml);
}
// no RGB camera found. And what about YUV ?
else
{
videoDevices = CaptureDeviceManager.getDeviceList(new
VideoFormat(VideoFormat.YUV));
if (videoDevices.size() > 0) {
videoDevice = (CaptureDeviceInfo) videoDevices.get(0);
MediaLocator ml = videoDevice.getLocator();
videoDataSource = createDataSource(ml);
}
}
}
//Create the av data source
DataSource[] allDS = new DataSource[] {
audioDataSource,
videoDataSource
};
/* if(videoDataSource!=null){
try{
//DataSource localVideo = Manager.createCloneableDataSource(videoDataSource);
Player player = Manager.createRealizedPlayer(videoDevice.getLocator());
firePlayerStarting(player);
}catch(Exception e){
System.out.println("ERRO WEBCAM: "+e.toString());
}
}
*/
if (audioDataSource != null && videoDataSource != null) {
try {
avDataSource = Manager.createMergingDataSource(allDS);
}
catch (IncompatibleSourceException exc) {
//Shouldn't happen
throw new MediaException(
"Failed to create a media data source!"
+ "Media transmission won't be enabled!", exc);
}
}
else {
if (audioDataSource != null) {
avDataSource = audioDataSource;
}
if (videoDataSource != null) {
avDataSource = videoDataSource;
}
}
//avDataSource may be null (Bug report Vince Fourcade)
if (avDataSource != null) {
initProcessor(avDataSource);
}
}
//A custom media source
else {
MediaLocator locator = new MediaLocator(mediaSource);
avDataSource = createDataSource(locator);
if (avDataSource != null) {
initProcessor(avDataSource);
}
}
isStarted = true;
}
finally {
}
}
protected DataSource createDataSource(MediaLocator locator)
{
try {
DataSource ds = Manager.createDataSource(locator);
//TODO VIEW SELF CAMERA
/* try{
MonitorControl mc = (MonitorControl)ds.getControl("net.java.mais.media.jmf.MonitorStream");
}catch(Exception e){
System.err.println("ERRO: "+e.toString());
}
*/
return ds;
}
catch (NoDataSourceException ex) {
//The failure only concens us
return null;
}
catch (IOException ex) {
//The failure only concens us
return null;
}
finally {
}
}
public void openMediaStreams(String sdpData) throws MediaException
{
try {
checkIfStarted();
SessionDescription sessionDescription = null;
if (sdpData == null) {
throw new MediaException(
"The SDP data was null! Cannot open " +
"a stream withour an SDP Description!");
}
try {
sessionDescription =
sdpFactory.createSessionDescription(sdpData);
}
catch (SdpParseException ex) {
throw new MediaException("Incorrect SDP data!", ex);
}
Vector mediaDescriptions;
try {
mediaDescriptions = sessionDescription.
getMediaDescriptions(true);
}
catch (SdpException ex) {
throw new MediaException(
"Failed to extract media descriptions from provided session description!",
ex);
}
Connection sessionConnection = sessionDescription.getConnection();
String sessionRemoteAddress = null;
if (sessionConnection != null) {
try {
sessionRemoteAddress = sessionConnection.getAddress();
}
catch (SdpParseException ex) {
throw new MediaException(
"Failed to extract the connection address parameter"
+ "from privided session description", ex);
}
}
int mediaPort = -1;
boolean atLeastOneTransmitterStarted = false;
ArrayList mediaTypes = new ArrayList();
ArrayList remoteAddresses = new ArrayList();
//A hashtable that indicates what addresses are different media types
//coming from.
Hashtable remoteTransmisionDetails = new Hashtable();
//by default everything is supposed to be coming from the address
//specified in the session (global) connection parameter so store
//this address for now.
//And only store it if different from null-
//bug report and fix there of: Christian Fritjof.
if(sessionRemoteAddress != null){
remoteTransmisionDetails.put("audio", sessionRemoteAddress);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -