📄 mediamanager.java
字号:
prepareMediaSession(incomingSdpBody); SessionDescription sessionDescription = null; try { sessionDescription = sdpFactory.createSessionDescription(incomingSdpBody); //Get the remote address where the user agent has to connect to Connection remoteConnection = sessionDescription.getConnection(); remoteAddress = remoteConnection.getAddress(); } catch (SdpParseException spe) { spe.printStackTrace(); } //Constructing the sdp response body SessionDescription responseSessionDescription = null; try { responseSessionDescription = (SessionDescription) sessionDescription.clone(); } catch (CloneNotSupportedException cnse) { cnse.printStackTrace(); } try { //Connection Connection connection = sdpFactory.createConnection( callListener.getConfiguration().contactIPAddress); responseSessionDescription.setConnection(connection); //Owner long sdpSessionId=(long)(Math.random() * 1000000); Origin origin = sdpFactory.createOrigin( callListener.sipMeetingManager.getUserURI().getUser(), sdpSessionId, sdpSessionId+1369, "IN", "IP4", callListener.getConfiguration().contactIPAddress); responseSessionDescription.setOrigin(origin); } catch (SdpException se) { se.printStackTrace(); } //Media Description Vector mediaDescriptions = new Vector(); if (negotiatedAudioCodec != null) { System.out.println( "Negotiated audio codec " + negotiatedAudioCodec + " on Port " + localAudioPort); MediaDescription mediaDescription = sdpFactory.createMediaDescription( "audio", localAudioPort, 1, "RTP/AVP", new String[] { negotiatedAudioCodec }); mediaDescriptions.add(mediaDescription); } else { System.out.println( "No Negotiated audio codec," + "so no audio media descriptions will be added to the sdp body"); } if (negotiatedVideoCodec != null) { System.out.println( "Negotiated video codec " + negotiatedVideoCodec + " on Port " + localVideoPort); MediaDescription mediaDescription = sdpFactory.createMediaDescription( "video", localVideoPort, 1, "RTP/AVP", new String[] { negotiatedVideoCodec }); mediaDescriptions.add(mediaDescription); } else { System.out.println( "No Negotiated video codec," + "so no video media descriptions will be added to the sdp body"); } try { responseSessionDescription.setMediaDescriptions(mediaDescriptions); } catch (SdpException se) { se.printStackTrace(); } return responseSessionDescription; } /** * Extracts all the audio codecs from the description of the media session of the incoming request * @param sessionDescription - the description of the media session of the incoming request * @return List of all the audio codecs from the description of the media session of the incoming request */ public List extractAudioCodecs(SessionDescription sessionDescription) { List audioCodecList = new Vector(); Vector mediaDescriptionList = null; try { mediaDescriptionList = sessionDescription.getMediaDescriptions(true); } catch (SdpException se) { se.printStackTrace(); } try { for (int i = 0; i < mediaDescriptionList.size(); i++) { MediaDescription mediaDescription = (MediaDescription) mediaDescriptionList.elementAt(i); Media media = mediaDescription.getMedia(); if (media.getMediaType().equals("audio")) audioCodecList = media.getMediaFormats(true); } } catch (SdpParseException spe) { spe.printStackTrace(); } return audioCodecList; } /** * Extracts all the video codecs from the description of the media session of the incoming request * @param sessionDescription - the description of the media session of the incoming request * @return List of all the audio codecs from the description of the media session of the incoming request */ public List extractVideoCodecs(SessionDescription sessionDescription) { List videoCodecList = new Vector(); Vector mediaDescriptionList = null; try { mediaDescriptionList = sessionDescription.getMediaDescriptions(true); } catch (SdpException se) { se.printStackTrace(); } try { for (int i = 0; i < mediaDescriptionList.size(); i++) { MediaDescription mediaDescription = (MediaDescription) mediaDescriptionList.elementAt(i); Media media = mediaDescription.getMedia(); if (mediaDescription.getMedia().getMediaType().equals("video")) videoCodecList = media.getMediaFormats(true); } } catch (SdpParseException spe) { spe.printStackTrace(); } return videoCodecList; } /** * Extracts the audio port from the description of the media session of the incoming request * @param sessionDescription - the description of the media session of the incoming request * @return the audio port on which is listening the remote user agent */ public int getAudioPort(SessionDescription sessionDescription) { Vector mediaDescriptionList = null; try { mediaDescriptionList = sessionDescription.getMediaDescriptions(true); } catch (SdpException se) { se.printStackTrace(); } try { for (int i = 0; i < mediaDescriptionList.size(); i++) { MediaDescription mediaDescription = (MediaDescription) mediaDescriptionList.elementAt(i); if (mediaDescription.getMedia().getMediaType().equals("audio")) return mediaDescription.getMedia().getMediaPort(); } } catch (SdpParseException spe) { spe.printStackTrace(); } return -1; } /** * Extracts the video port from the description of the media session of the incoming request * @param sessionDescription - the description of the media session of the incoming request * @return the video port on which is listening the remote user agent */ public int getVideoPort(SessionDescription sessionDescription) { Vector mediaDescriptionList = null; try { mediaDescriptionList = sessionDescription.getMediaDescriptions(true); } catch (SdpException se) { se.printStackTrace(); } try { for (int i = 0; i < mediaDescriptionList.size(); i++) { MediaDescription mediaDescription = (MediaDescription) mediaDescriptionList.elementAt(i); if (mediaDescription.getMedia().getMediaType().equals("video")) return mediaDescription.getMedia().getMediaPort(); } } catch (SdpParseException spe) { spe.printStackTrace(); } return -1; } /** * Find the best codec between our own supported codecs * and the remote supported codecs to initiate the media session * Currently, take the first one to match * @param audioCodecList - the list of the remote audio supported codecs * @return the negotiated audio codec */ public String negotiateAudioCodec(List audioCodecList) { //Find the mapping of the jmf format to the sdp format List audioCodecSupportedSdpFormat = new Vector(); Iterator it = audioCodecSupportedList.iterator(); while (it.hasNext()) { String sdpCodecValue = findCorrespondingSdpFormat(((Format) it.next()).getEncoding()); if (sdpCodecValue != null) audioCodecSupportedSdpFormat.add(sdpCodecValue); } //find the best codec(currently the first one which is in both list) Iterator iteratorSupportedCodec = audioCodecSupportedSdpFormat.iterator(); while (iteratorSupportedCodec.hasNext()) { String supportedCodec = (String) iteratorSupportedCodec.next(); Iterator iteratorRemoteCodec = audioCodecList.iterator(); while (iteratorRemoteCodec.hasNext()) { String remoteCodec = iteratorRemoteCodec.next().toString(); if (remoteCodec.equals(supportedCodec)) return remoteCodec; } } return null; } /** * Find the best codec between our own supported codecs * and the remote supported codecs to initiate the media session * Currently, take the first one to match * @param videoCodecList - the list of the remote video supported codecs * @return the negotiated video codec */ public String negotiateVideoCodec(List videoCodecList) { //Find the mapping of the jmf format to the sdp format List videoCodecSupportedSdpFormat = new Vector(); Iterator it = videoCodecSupportedList.iterator(); while (it.hasNext()) { String sdpCodecValue = findCorrespondingSdpFormat(((Format) it.next()).getEncoding()); if (sdpCodecValue != null) videoCodecSupportedSdpFormat.add(sdpCodecValue); } //find the best codec(currently the first one which is in both list) Iterator iteratorSupportedCodec = videoCodecSupportedSdpFormat.iterator(); while (iteratorSupportedCodec.hasNext()) { String supportedCodec = (String) iteratorSupportedCodec.next(); Iterator iteratorRemoteCodec = videoCodecList.iterator(); while (iteratorRemoteCodec.hasNext()) { String remoteCodec = iteratorRemoteCodec.next().toString(); if (remoteCodec.equals(supportedCodec)) return remoteCodec; } } return null; } /** * Utility method to print the remote codecs */ public void printCodecs(List codecList) { System.out.println("List of codecs: "); Iterator it = codecList.iterator(); while (it.hasNext()) { String att = it.next().toString(); System.out.println(att); } } /** * Utility method to print the supported codecs */ public void printSupportedCodecs() { System.out.println("List of supported audio codecs: "); Iterator it = audioCodecSupportedList.iterator(); while (it.hasNext()) { System.out.println(((Format) it.next()).toString()); } System.out.println("List of supported video codecs: "); it = videoCodecSupportedList.iterator(); while (it.hasNext()) { System.out.println(((Format) it.next()).toString()); } } /** * Utility method to print the supported codecs in parameter * @param codecList - the list of codec to print out */ public void printSupportedCodecs(List codecList) { System.out.println("List of codecs: "); Iterator it = codecList.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } /** * Retrieve the audio port for this media session, * if no audio port has been allowed it will choose one and return it * @return the audio port for this media session */ public int getAudioPort() { if (localAudioPort == -1) { localAudioPort = new Random().nextInt(8885); if (localAudioPort % 2 == 0) localAudioPort += 1024; else localAudioPort += 1025; } return localAudioPort; } /** * Retrieve the video port for this media session, * if no video port has been allowed it will choose one and return it * @return the video port for this media session */ public int getVideoPort() { if (localVideoPort == -1) { localVideoPort = new Random().nextInt(8885); if (localVideoPort % 2 == 0) localVideoPort += 1024; else localVideoPort += 1025; } return localVideoPort; } /** * Map a jmf format to a sdp format * @param jmfFormat - the jmf Format * @return the corresponding sdp format */ public static String findCorrespondingSdpFormat(String jmfFormat) { if (jmfFormat == null) { return null; } else if (jmfFormat.equals(AudioFormat.ULAW_RTP)) { return Integer.toString(SdpConstants.PCMU); } else if (jmfFormat.equals(AudioFormat.GSM_RTP)) { return Integer.toString(SdpConstants.GSM); } else if (jmfFormat.equals(AudioFormat.G723_RTP)) { return Integer.toString(SdpConstants.G723); } else if (jmfFormat.equals(AudioFormat.DVI_RTP)) { return Integer.toString(SdpConstants.DVI4_8000); } else if (jmfFormat.equals(AudioFormat.DVI_RTP)) { return Integer.toString(SdpConstants.DVI4_16000); } else if (jmfFormat.equals(AudioFormat.ALAW)) { return Integer.toString(SdpConstants.PCMA); } else if (jmfFormat.equals(AudioFormat.G728_RTP)) { return Integer.toString(SdpConstants.G728); } else if (jmfFormat.equals(AudioFormat.G729_RTP)) { return Integer.toString(SdpConstants.G729); } else if (jmfFormat.equals(VideoFormat.H263_RTP)) { return Integer.toString(SdpConstants.H263); } else if (jmfFormat.equals(VideoFormat.JPEG_RTP)) { return Integer.toString(SdpConstants.JPEG); } else if (jmfFormat.equals(VideoFormat.H261_RTP)) { return Integer.toString(SdpConstants.H261); } else { return null; } } /** * Map a sdp format to a jmf format * @param sdpFormatStr - the sdp Format * @return the corresponding jmf format */ public static String findCorrespondingJmfFormat(String sdpFormatStr) { int sdpFormat = -1; try { sdpFormat = Integer.parseInt(sdpFormatStr); } catch (NumberFormatException ex) { return null; } switch (sdpFormat) { case SdpConstants.PCMU : return AudioFormat.ULAW_RTP; case SdpConstants.GSM : return AudioFormat.GSM_RTP; case SdpConstants.G723 : return AudioFormat.G723_RTP; case SdpConstants.DVI4_8000 : return AudioFormat.DVI_RTP; case SdpConstants.DVI4_16000 : return AudioFormat.DVI_RTP; case SdpConstants.PCMA : return AudioFormat.ALAW; case SdpConstants.G728 : return AudioFormat.G728_RTP; case SdpConstants.G729 : return AudioFormat.G729_RTP; case SdpConstants.H263 : return VideoFormat.H263_RTP; case SdpConstants.JPEG : return VideoFormat.JPEG_RTP; case SdpConstants.H261 : return VideoFormat.H261_RTP; case 99 : return "mpegaudio/rtp, 48000.0 hz, 16-bit, mono"; default : return null; } } /*PCMU javax.media.format.AudioFormat.ULAW_RTP; 1016 G721 GSM javax.media.format.AudioFormat.GSM_RTP; G723 javax.media.format.AudioFormat.G723_RTP DVI4_8000 javax.media.format.AudioFormat.DVI_RTP; DVI4_16000 javax.media.format.AudioFormat.DVI_RTP; LPC PCMA javax.media.format.AudioFormat.ALAW; G722 javax.media.format.AudioFormat.ALAW; L16_2CH L16_1CH QCELP CN MPA G728 javax.media.format.AudioFormat.G728_RTP; DVI4_11025 DVI4_22050 G729 javax.media.format.AudioFormat.G729_RTP CN_DEPRECATED H263 javax.media.format.VideoFormat.H263_RTP CelB JPEG javax.media.format.VideoFormat.JPEG_RTP nv H261 javax.media.format.VideoFormat.H261_RTP MPV*/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -