在 Android 的 onCreate 期间检查 RTSP URI

Check RTSP URI during onCreate in Android

我正在为工作创建一个相当简单的 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 相关的问题似乎是指本地文件,因此开发人员可以只检查一个文件,但我认为这不适用于直播。任何建议将不胜感激!

基本上你想打开一个到 RTSP 服务器的普通客户端套接字连接 hostname/port 然后 post 一个 RTSP OPTIONS 请求。 如果服务器响应 "RTSP/1.0 200 OK",则表示流媒体服务已启动,否则已关闭。

我写了一个快速而粗略的示例 RTSP 检查并对其进行了测试。请随意根据您的需要进行调整:

更新 处理 UI 操作 在您的 activity class 中,创建这些字段:

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();`

对于此测试,我选择了 public 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

如果需要更多信息,请告诉我。