在Android

时间:2015-06-23 20:48:52

标签: android uri rtsp

我正在创建一个相当简单的Android应用程序,用于将URI插入系统视频播放器,以便用户可以轻松地观看我们从Wowza服务器发送的RTSP流。

一般来说,我基本上只有一个调用按钮:

Uri myURI = Uri.parse("rtsp://ipaddress:1935/path/myStream");    
Intent intent = new Intent(Intent.ACTION_VIEW, myURI);
startActivity(intent);

但我们不会全天候从这个URI流式传输,我想在onCreate期间更改行为,以便在绘制按钮之前验证URI,让我有机会向用户提供流的视觉反馈不存在并保存我们不需要的支持电子邮件。

我查看了Uri和URI类,看看是否有一个方法我可以调用来检查URI,但似乎没有任何东西真的存在。其他与URI相关的问题似乎是指本地文件,因此开发人员可以只检查文件,但我认为这不适用于实时流。任何建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

基本上,您希望打开与RTSP服务器主机名/端口的普通客户端套接字连接,然后发布RTSP OPTIONS请求。 如果服务器以" RTSP / 1.0 200 OK"响应,则表示流服务已启动,否则将关闭。

我写了一个快速而脏的样本RTSP检查并进行了测试。随意根据您的需求进行调整:

处理UI操作的

更新 在您的活动类中,创建以下字段:

private static final int MESSAGE_RTSP_OK = 1;
private static final int MESSAGE_RTSP_ERROR = -1;

private Handler handler;

在你的onCreate方法中,添加:

    handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MESSAGE_RTSP_OK:
                    //show_player(); 
                    // implement ui operation here
                    break;
                case MESSAGE_RTSP_ERROR:
                    //show_error();
                    break;
            }
        }
    };

然后运行此示例代码: 更新了rtsp检查代码

    new Thread() {
        public void run() {
            try {
                Socket client = new Socket("a1352.l1857053128.c18570.g.lq.akamaistream.net", 554);
                OutputStream os = client.getOutputStream();
                os.write("OPTIONS * RTSP/1.0\n".getBytes());
                os.write("CSeq: 1\n\n".getBytes());
                os.flush();

                //NOTE: it's very important to end any rtsp request with \n\n (two new lines). The server will acknowledge that the request ends there and it's time to send the response back.

                BufferedReader br =
                        new BufferedReader(
                                new InputStreamReader(
                                        new BufferedInputStream(client.getInputStream())));

                StringBuilder sb = new StringBuilder();
                String responseLine = null;

                while (null != (responseLine = br.readLine()))
                    sb.append(responseLine);
                String rtspResponse = sb.toString();
                if(rtspResponse.startsWith("RTSP/1.0 200 OK")){
                    // RTSP SERVER IS UP!!
                     handler.obtainMessage(MESSAGE_RTSP_OK).sendToTarget();
                } else {
                    // SOMETHING'S WRONG

                    handler.obtainMessage(MESSAGE_RTSP_ERROR).sendToTarget();
                }
                Log.d("RTSP reply" , rtspResponse);
                client.close();
            } catch (IOException e) {
                // NETWORK ERROR such as Timeout 
                e.printStackTrace();

                handler.obtainMessage(MESSAGE_RTSP_ERROR).sendToTarget();
            }

        }
    }.start();`

在本次测试中,我选择了公共NASA TV rtsp服务器:rtsp://a1352.l1857053128.c18570.g.lq.akamaistream.net:554

代码发送以下rtsp请求:

OPTIONS * RTSP/1.0 CSeq: 1

并收到以下回复:

RTSP/1.0 200 OK Server: QTSS-Akamai/6.0.3 (Build/526.3; Platform/Linux; Release/Darwin; ) Cseq: 1 Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, OPTIONS, ANNOUNCE, RECORD

如果您正在寻找更复杂的检查,请随时深入了解rtsp协议文档并根据自己的需要增强代码:https://www.ietf.org/rfc/rfc2326.txt

如果需要更多信息,请与我们联系。