Check RTSP URI during onCreate in Android

前端 未结 1 805
梦谈多话
梦谈多话 2021-01-15 19:08

I\'m creating a fairly simple Android app for work that will plug a URI into the system video player so that users can easily watch the RTSP stream that we are sending from

相关标签:
1条回答
  • 2021-01-15 19:48

    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.

    0 讨论(0)
提交回复
热议问题