java开发RtspClient

java开发RtspClient

socket chanel建立

if (selector == null) {
            // 创建新的Selector
            try {
                selector = Selector.open();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
try {
// 打开通道
            socketChannel = SocketChannel.open();
            // 绑定到本地端口
            socketChannel.socket().setSoTimeout(30000);
            socketChannel.configureBlocking(false);
            socketChannel.socket().bind(localAddress);
            if (socketChannel.connect(remoteAddress)) {
                System.out.println("开始建立连接:" + remoteAddress);
            }
            socketChannel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE,
                    this);
            System.out.println("端口打开成功");

        } catch (final IOException e1) {
            e1.printStackTrace();
        }

options 业务实现

    private void doOption() {
        StringBuilder sb = new StringBuilder();
        sb.append("OPTIONS ");
        sb.append(this.address.substring(0, address.lastIndexOf("/")));
        sb.append(VERSION);
        sb.append("\r\n");
        sb.append("CSeq: ");
        sb.append(seq++);
        sb.append("\r\n");
        sb.append("\r\n");
        System.out.println(sb.toString());
        send(sb.toString().getBytes());
    }

交互报文
客户端发出的报文
java开发RtspClient_第1张图片

服务端响应报文

java开发RtspClient_第2张图片

Describe业务

代码实现

    private void doDescribe() {
        StringBuilder sb = new StringBuilder();
        sb.append("DESCRIBE ");
        sb.append(this.address);
        sb.append(VERSION);
        sb.append("\r\n");
        sb.append("CSeq: ");
        sb.append(seq++);
        sb.append("\r\n");
        sb.append("\r\n");
        System.out.println(sb.toString());
        send(sb.toString().getBytes());
    }

客户端发出去的报文

java开发RtspClient_第3张图片

服务响应报文
java开发RtspClient_第4张图片

setup 业务

代码实现
private void doSetup() {
StringBuilder sb = new StringBuilder();
sb.append(“SETUP “);
sb.append(this.address);
sb.append(“/”);
sb.append(trackInfo);
sb.append(VERSION);
sb.append(“CSeq: “);
sb.append(seq++);
sb.append(“\r\n”);
sb.append(“Transport: RTP/AVP;UNICAST;client_port=16264-16265;mode=play”);
sb.append(“\r\n”);
sb.append(“\r\n”);
System.out.println(sb.toString());
send(sb.toString().getBytes());
}
客户端发出报文

java开发RtspClient_第5张图片

服务端响应报文

java开发RtspClient_第6张图片

play 业务

代码 实现

    private void doPlay() {
        StringBuilder sb = new StringBuilder();
        sb.append("PLAY ");
        sb.append(this.address);
        sb.append(VERSION);
        sb.append("Session: ");
        sb.append(sessionid);
        sb.append("CSeq: ");
        sb.append(seq++);
        sb.append("\r\n");
        sb.append("\r\n");
        System.out.println(sb.toString());
        send(sb.toString().getBytes());

    }

客户端发出报文
java开发RtspClient_第7张图片

服务端接收报文
java开发RtspClient_第8张图片

你可能感兴趣的:(协议,协议)