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!
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:
in your onCreate method, add:
Then run this sample code: Updated rtsp check code
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.