Live stream an ONVIF Camera on your Android app!

Onvif (stands for: O pen N etwork V ideo I nterface F orum) is a non-profit with the goal of facilitating the development and use of a global open standard for the interface of physical IP-based security products — Wikipedia

Being able to control your house, open doors, view in real time your living room, control the lights is a childhood dream! I was really delighted to develop an app and a dependency to ease the development of ONVIF Android apps.

ONVIF goal is to standardise how IP products (video surveillance cameras, alarms, doors, audio recorders…) communicate with each others. They created some specifications manufacturers have to conform to to be compliant with ONVIF. The goal behind these specifications is to standardise how to connect to these products, for instance if you develop an app for streaming video from an ONVIF camera, it should work with every ONVIF camera.

Try it!

If you just want to try the demo project, you can download the sample project on Github . Open it with Android Studio and run it, that’s it!

You will be able to login on every ONVIF camera and view its live stream:

Streaming from an ONVIF camera

How to connect to a camera and visualise its live stream on Android? :man|type_4:‍:computer:

  • Create a new project on Android Studio
  • Add implementation `com.rvirin.onvif:onvifcamera:1.1.6` and implementation `com.squareup.okhttp3:okhttp:3.10.0` to your build.gradle (Module: app)

Once it’s done, you can add the following code to connect to the camera and retrieve its information:

To instantiate an ONVIF camera, we use the class OnvifDevice , this class takes an OnvifListener which defines the requestPerformed() method. This method is called once the camera returns a call we made (getDeviceInformation, getProfiles, getStreamUri).

To be able to see the live stream of the camera we need to retrieve the different profiles (media profiles, with different configurations available) from the camera, select the one we want to play and retrieve the corresponding stream URI.

Here is how we retrieve the profiles and the stream URI:

Depending on your camera, your URI will look like this one: rtsp://IP_ADDRESS:PORT/cam/realmonitor?channel=1 etc…

The URI does not follow the http protocol, but the rtsp protocol, which is normal because RTSP (Real Time Streaming Protocol) is aiming to make streaming simpler. The problem is, VideoView doesn’t handle RTSP well (it can work but not with every codec, which is a problem to stream from an ONVIF camera). Fortunately, VLC comes to the rescue!

How to read a RTSP stream on Android? :movie_camera:

Add these lines to your build.gradle:

Here is how you play the video with libVLC once you have the rtsp URI:

VLCListener implements two methods ( onComplete() , and onError() ). They are called by VlcVideoLibrary to tell us if the video loads without any problem.

Tip: You can also insert the login and password in the rtsp uri like this: rtsp://login:password @ IP_ADDRESS:PORT/cam/realmonitor?channel=1 etc… If you use the ONVIFCamera dependency, the URI saved in OnvifDevice.rtspUri contains the login and password.

If you want to learn how to create the same project on iOS, you can readthis article.

Creating an ONVIF library was a exciting opportunity, as you can see in my previous posts , I really enjoy having an interaction between my apps and hardware devices, whether it’s a printer for iPhone, a camera, or even a door! I’m really looking forward to test new ONVIF devices and develop apps that can be useful for everyone that has ONVIF devices.

The code of this project is open source, you can download it on Github .

If you have any question, I’ll be happy to read them in the comments!