C++ Camera Example
This guide will show you how to build and run a Nabto WebRTC example device application using the C++ SDK. To use the C++ SDK in your own application see our SDK guide and Application guide.
Obtaining the source
Clone the Nabto WebRTC C++ repository recursively to ensure submodules are initialized properly:
$ git clone --recursive https://github.com/nabto/nabto-webrtc-sdk-cpp.git
Building the Examples
To build the examples requires these tools to be installed:
- CMake
- A C++ 20 compiler
From the root of the repository, go to the examples/libdatachannel
directory and build the examples using the CMake preset:
$ cd sdk/
$ cmake --workflow --preset release
This will use the vcpkg submodule to build and install all other dependencies used by the build as well as build the Nabto WebRTC SDK. After the dependencies are build, it will build the examples.
When build, the resulting binaries can be run from the install directory:
$ ./build/release/install/bin/webrtc_device --help
Preparing the examples
Before running the examples, follow the Console intro to configure a device in the Nabto backend. From this guide you should obtain a Product ID, a Device ID, and a Private Key.
The C++ examples expects the private key to be stored in a local file in PEM format.
Additionally, this guide will use shared secret authentication, so such a secret should be generated:
$ openssl rand -hex 32
d4ff5ecd8f21ed40c889cd350a524f9945a374b5e962dbf0bc1eeec71ee5dc1b
In demonstration scenarios the shared secret may be impractical to copy from the device to the client if using eg. an Android client app. For demonstration purposes only, a simpler shared secret can be used. However, this can compromise the security of the signaling channel.
You are now ready to run the examples. Two variants exist, one using an RTSP server and one using an RTP feed as a video source.
Running the RTSP example
Before running the RTSP device example, you must first have an RTSP server offering a video feed. You can use any RTSP server of your choice, however, the example expects the video to be encoded as H264 constrained-baseline. To ensure a smooth demo, it is recommended to use the Nabto RTSP demo container to setup the feed.
You can also see the simulated video sources guide for more options.
Once the RTSP server is running, the device can be started using the prepared information:
$ ./build/release/install/bin/webrtc_device_rtsp -d <YOUR_DEVICE_ID> -p <YOUR_PRODUCT_ID> \
-k <PATH/TO/PRIVATE_KEY.pem> --secret <YOUR_SHARED_SECRET> -r rtsp://127.0.0.1:8554/video
The device is now ready for clients to connect. To connect a client, either follow the Web guide or the mobile guide.
Running the RTP example
Before running the RTP device example, you must first have an RTP video feed. Again, the example expects the video to be encoded as H264 constrained-baseline. It also expects the feed to be available via UDP port 6000 on the localhost. To ensure a smooth demo, it is recommended to use GStreamer to setup the feed using the following command:
$ gst-launch-1.0 videotestsrc ! clockoverlay ! video/x-raw,width=640,height=480 ! videoconvert ! queue ! \
x264enc tune=zerolatency bitrate=1000 key-int-max=30 ! video/x-h264, profile=constrained-baseline ! \
rtph264pay pt=96 mtu=1200 ! udpsink host=127.0.0.1 port=6000
You can also see the simulated video sources guide for more options.
Once the RTP feed is running, the device can be started using the prepared information:
$ ./build/release/install/bin/webrtc_device -d <YOUR_DEVICE_ID> -p <YOUR_PRODUCT_ID> \
-k <PATH/TO/PRIVATE_KEY.pem> --secret <YOUR_SHARED_SECRET>
The device is now ready for clients to connect. To connect a client, either follow the Web guide or the mobile guide.