在Java中解析SDP消息字符串

时间:2017-05-18 12:07:12

标签: java parsing sdp

目前我正在使用SDP消息建立连接的应用程序。我需要做的是为SDP消息的字符串表示创建解析器并创建表示信息的某种结构,也从现有结构创建这样的消息。

RFC 4566的示例:

  v=0
  o=jdoe 2890844526 2890842807 IN IP4 10.47.16.5
  s=SDP Seminar
  i=A Seminar on the session description protocol
  u=http://www.example.com/seminars/sdp.pdf
  e=j.doe@example.com (Jane Doe)
  c=IN IP4 224.2.17.12/127
  t=2873397496 2873404696
  a=recvonly
  m=audio 49170 RTP/AVP 0
  m=video 51372 RTP/AVP 99
  a=rtpmap:99 h263-1998/90000

我的问题是: Java工具中是否有用于解析此类消息的内容?我在github上看到过一些示例,但是由于我是该主题的新手,我无法确定哪种解决方案最适合此类任务。

1 个答案:

答案 0 :(得分:1)

是的,Java有一些名为Jain SIP的内置SIP功能。它在媒体部分非常弱(没有良好的编解码器支持),但是对于信令处理它应该满足您的需求。

示例:

import javax.sdp.*;
import javax.sip.*;

ContentTypeHeader contentType = (ContentTypeHeader) msg.getHeader(ContentTypeHeader.NAME);
ContentLengthHeader contentLen = (ContentLengthHeader) msg.getHeader(ContentLengthHeader.NAME);

if ( contentLen.getContentLength() > 0 && contentType.getContentSubType().equals("sdp") ){
    String charset = null;

    if (contentType != null)
        charset = contentType.getParameter("charset");
    if (charset == null)
        charset = "UTF-8"; // RFC 3261

    //Save the SDP content in a String
    byte[] rawContent = msg.getRawContent();
    String sdpContent = new String(rawContent, charset);

    //Use the static method of SdpFactory to parse the content
    SdpFactory sdpFactory = SdpFactory.getInstance();
    SessionDescription sessionDescription = sdpFactory.createSessionDescription(sdpContent);
    Origin origin = sessionDescription.getOrigin();

    System.out.println("A Session ID is " + origin.getSessionId());
} else {
    System.out.println("It is not a SDP content");
}

如果您不喜欢这样,那么只需使用开源SDP解析器,例如jain sipjsdp

你也可以按照RFC 4566手动完成,因为SDP解析非常简单,可以通过一点字符串操作来完成。