📄 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/>.
*
* Large portions of this software are based upon public domain software
* https://sip-communicator.dev.java.net/
*
*/
package net.sourceforge.gjtapi.raw.sipprovider.media;
import net.sourceforge.gjtapi.raw.sipprovider.media.event.MediaErrorEvent;
import net.sourceforge.gjtapi.raw.sipprovider.media.event.MediaListener;
import net.sourceforge.gjtapi.raw.sipprovider.common.Console;
import net.sourceforge.gjtapi.raw.sipprovider.common.Utils;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
import javax.sdp.*;
import net.sourceforge.gjtapi.raw.sipprovider.media.event.MediaEvent;
import net.sourceforge.gjtapi.raw.sipprovider.common.NetworkAddressManager;
import javax.media.rtp.RTPManager;
import javax.media.rtp.SessionAddress;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.media.*;
import javax.media.control.*;
import javax.media.format.*;
import javax.media.protocol.*;
import javax.sdp.*;
/**
* <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)
* @version 1.1
*
*/
public class MediaManager
implements Serializable
{
protected static Console console = Console.getConsole(MediaManager.class);
protected ArrayList listeners = new ArrayList();
protected Vector avTransmitters = new Vector();
protected AVReceiver avReceiver;
protected SdpFactory sdpFactory;
protected ProcessorUtility procUtility = new ProcessorUtility();
//media devices
protected CaptureDeviceInfo audioDevice = null;
protected CaptureDeviceInfo videoDevice = null;
//Sdp Codes of all formats supported for
//transmission by the selected datasource
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
Integer.toString(SdpConstants.GSM), // javax.media.format.AudioFormat.GSM_RTP;// 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.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
*/
protected Hashtable activeRtpManagers = new Hashtable();
protected Hashtable sessions = new Hashtable();
protected RTPManager rtpManager ;
protected String mediaSource = null;
protected DataSource avDataSource = null;
protected Processor processor = null;
protected boolean isStarted = false;
protected Properties sipProp;
protected String audioPort;
protected Vector transmitters = new Vector();
protected Vector receivers = new Vector();;
protected DataSink sink = null;
private MediaLocator dest;
public MediaManager(Properties sipProp)
{
audioPort=sipProp.getProperty("net.java.sip.communicator.media.AUDIO_PORT");
this.sipProp = new Properties() ;
this.sipProp.putAll(sipProp);
}
public void play(String url) throws MediaException
{
console.logEntry();
MediaLocator locator = new MediaLocator(url);
avDataSource = createDataSource(locator);
if (avDataSource != null)
{
initProcessor(avDataSource);
}
for (int i = avTransmitters.size()-1; i >= 0; i--)
{
try
{
( (AVTransmitter) avTransmitters.elementAt(i)).play(processor);
}
catch (net.sourceforge.gjtapi.raw.sipprovider.media.MediaException ex)
{
console.debug(ex.toString());
}
console.logExit();
}
}
public void stopPlaying()
{
console.logEntry();
for (int i = avTransmitters.size() - 1; i >= 0; i--)
{
try
{
( (AVTransmitter) avTransmitters.elementAt(i)). stopPlaying();
}
catch (Exception ex)
{
console.debug(ex.toString());
}
}
}
public void record(String url)
{
console.logEntry();
DataSource dsTab[] = new DataSource[receivers.size()] ;
DataSource ds =null;
for (int i = receivers.size()-1; i >= 0; i--)
{
try
{
Processor pro = ( (AVReceiver) receivers.elementAt(i)).getProcessor();
ds = pro.getDataOutput();
dsTab[i]=ds;
}
catch (Exception ex)
{
console.debug(ex.toString());
}
}
try
{
DataSource mergeDs = Manager.createMergingDataSource(dsTab);
mergeDs.connect();
mergeDs.start();
// append "file:/" to URL if it is not already there
String fullUrl = (url.indexOf("file:") == 0) ? url : "file:/"+ url;
dest = new MediaLocator(fullUrl);
sink = Manager.createDataSink(ds, dest);
sink.open();
sink.start();
for (int i = receivers.size()-1; i >= 0; i--)
{
try
{
Processor pro = ( (AVReceiver) receivers.elementAt(i)).getProcessor();
pro.start();
}
catch (Exception ex)
{
console.debug(ex.toString());
}
}
}
catch (Exception ex)
{
console.debug(ex.toString());
}
console.logExit();
}
public void stopRecording()
{
try
{
sink.stop();
sink.close();
}
catch(Exception ex)
{
console.debug(ex.toString());
}
}
public void start() throws MediaException
{
try
{
console.logEntry();
try
{
sdpFactory = SdpFactory.getInstance();
}
catch (SdpException exc)
{
console.error("Failed to create sdpFactory", exc);
throw new MediaException("Failed to create sdpFactory", exc);
}
mediaSource = sipProp.getProperty("net.java.sip.communicator.media.MEDIA_SOURCE");
//Init Capture devices
DataSource audioDataSource = null;
isStarted = true;
}
finally
{
console.logExit();
}
}
protected DataSource createDataSource(MediaLocator locator)
{
try
{
console.logEntry();
try
{
if (console.isDebugEnabled())
console.debug("Creating datasource for:"
+ locator != null
? locator.toExternalForm()
: "null");
return Manager.createDataSource(locator);
}
catch (NoDataSourceException ex)
{
//The failure only concens us
if (console.isDebugEnabled())
{
console.debug("Coud not create data source for " +
locator.toExternalForm(), ex);
}
return null;
}
catch (IOException ex)
{
//The failure only concens us
if (console.isDebugEnabled())
{
console.debug("Coud not create data source for " +
locator.toExternalForm(), ex);
}
return null;
}
}
finally
{
console.logExit();
}
}
public void openMediaStreams(String sdpData) throws MediaException
{
try
{
console.logEntry();
if(console.isDebugEnabled())
console.debug("sdpData arg - " + sdpData);
checkIfStarted();
SessionDescription sessionDescription = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -