Check RTSP URI during onCreate in Android

1.4k views Asked by At

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 our Wowza server.

In general, I basically just have a play button that calls:

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

but we don't stream from this URI 24/7 and I would like to change the behavior during onCreate so that it will validate the URI before drawing the button, giving me the opportunity to give the user visual feedback that the stream isn't live and save us unneeded support emails.

I've looked at the Uri and URI classes to see if there was a method that I could call to check the URI but nothing seems to really exist. Other URI related questions seem to refer to local files so the dev can just check a file, but I don't think that would work for a live stream. Any advice would be greatly appreciated!

1

There are 1 answers

9
Andrei Mărcuţ On BEST ANSWER

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.