paint-brush
Live stream an ONVIF Camera on your Android app! 📱by@remy
19,141 reads
19,141 reads

Live stream an ONVIF Camera on your Android app! 📱

by Remy VirinMarch 13th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Open Network Video Interface Forum (ONVIF) 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. 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 all of the cameras. To see the live stream of the camera we need to retrieve the different profiles (media profiles, with different configurations available) from the camera.

Company Mentioned

Mention Thumbnail
featured image - Live stream an ONVIF Camera on your Android app! 📱
Remy Virin HackerNoon profile picture

Onvif (stands for: Open Network Video Interface Forum) 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:

How to connect to a camera and visualise its live stream on Android? 👨🏽‍💻

  • 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).

Note: Calling getServices before getting the information is not mandatory but strongly recommended. It retrieves the different paths depending on the web service you’re calling, from one camera to another it can change. For instance getProfiles command will have this path on a Dahua camera /onvif/media2_service whereas it will be /onvif/media2 on a Uniview camera.

If you implement new web services call in your app, you need to parse their path in this call.

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? 🎥

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 read this 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!