Check RTSP URI during onCreate in Android

两盒软妹~` 提交于 2019-12-01 13:11:36

Basically you want to open a plain client socket connection to the RTSP server hostname/port and then post a RTSP OPTIONS request. If the server responds with a "RTSP/1.0 200 OK", it means the streaming service is up, otherwise it's down.

I wrote a quick and dirty sample RTSP check and tested it out. Feel free to adapt it to your needs:

Update handling UI operations In your activity class, create these fields:

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

private Handler handler;

in your onCreate method, add:

    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;
            }
        }
    };

Then run this sample code: Updated rtsp check code

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

For this test I picked the public NASA TV rtsp server: rtsp://a1352.l1857053128.c18570.g.lq.akamaistream.net:554

The code sends the following rtsp request:

OPTIONS * RTSP/1.0 CSeq: 1

And receives the following response:

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

If you are looking for more complex checks, feel free to dive into the rtsp protocol documentation and enhance the code per your own needs: https://www.ietf.org/rfc/rfc2326.txt

Please let me know if more information is needed.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!