Overview

The Camera Reference Code is a simple C/C++ code for integration with IP cameras, NVRs and any other video sources. The code performs the following main operations:

Reference code snippet

#include <CloudSDK.h>
#include <CloudStreamerSDK.h>
#include <Interfaces/ICloudStreamerCallback.h>

std::string ACCESS_TOKEN;

class CStreamerCallback : public ICloudStreamerCallback, public CUnk
{
    const char *TAG = "CStreamerCallback";
    const int LOG_LEVEL = 2; //Log.VERBOSE;
    MLog Log;

public:
    CStreamerCallback() : Log(TAG, LOG_LEVEL) {};
    virtual ~CStreamerCallback() {};

    void onStarted(std::string url_push) 
    {
        // Here we start rtmp push/publishing to the RTMP URL 'url_push'
        // streamPushStart(url_push);
        Log.v("=onStarted rtmp url=%s", url_push.c_str());
    }
    void onStopped()                
    {
        // Here we stop rtmp push/publishing
        // streamPushStop();
        Log.v("=onStopped");
    }
    void onError(int error)
    {
        Log.v("=onError %d", error);
    }
    void onCameraConnected() 
    {
        Log.v("=onCameraConnected");
    }
    void onCommand(std::string cmd)
    {
        Log.v("=onCommand cmd=%s", cmd.c_str());
    }
};

int main(int argc, char **argv)
{
    CloudSDK::setLogEnable(true, "teststreamer.log");
    CloudSDK::setLogLevel(2);
    MLog Log("app", 2);

    Log.v("=>test_cloudstreamer %s\n", CloudSDK::getLibVersion());
    if(argc < 2){
       Log.e("test_cloudstreamer. \n Usage: <access_token> \n 
       Example: test_cloudstreamer tokenstringAABBCCDD\n");
           return -1;
    }

    ACCESS_TOKEN = argv[1];
    Log.v("=test_cloudstreamer args(%s)", ACCESS_TOKEN.c_str());

    CStreamerCallback *streamer_cb = new CStreamerCallback();
    CloudStreamerSDK *streamer = new CloudStreamerSDK(streamer_cb);
    streamer->setSource(ACCESS_TOKEN);
    streamer->Start();

    getchar();
    Log.v("awaiting done\n");

    streamer->Release(); streamer = NULL;
    streamer_cb->Release(); streamer_cb = NULL;
    Log.v("<=test_cloudstreamer %s\n", CloudSDK::getLibVersion());

    getchar();
    return 0;
}

Structure and platforms

The Camera Reference Kit package has the following structure:

Where:

RTMP push/publishing

If the camera supports RTMP push/publishing then it should just use the 'url_push' RTMP publishing URL to start publishing data.

If the camera doesn't support RTMP publishing then we recommend to utilize FFMPEG to perform RTSP to RTMP push remultiplexing. The FFMPEG library will add approximately 1.2MB of a binary code.

Example of how to use FFMPEG is below:

int streamPushStart(std::string url_push) {
    /*Spawn a child to run the program.*/
    pid = fork();
    if (pid == 0) { /* child process */
        std::string cmdline = "ffmpeg -i " + ARG1_CAMERA_URL + " -vcodec copy -acodec copy -f flv " + url_push;

        std::vector<char *> args;
        std::istringstream iss(cmdline);

        std::string token;
        while(iss >> token) {
            char *arg = new char[token.size() + 1];
            copy(token.begin(), token.end(), arg);
            arg[token.size()] = '\0';
            args.push_back(arg);
        }
        args.push_back(0);
        execv("/usr/bin/ffmpeg", &args[0]);

        for(size_t i = 0; i < args.size(); i++){
            delete[] args[i];
        }
    }
    return pid;
}

int streamPushStop() {
    if(pid != -1){
        kill(pid, SIGKILL);
        waitpid(pid,0,0); /* wait for child to exit */
        pid = -1;
    }
    return pid;
}

where:

Full source code

The full source code can be found in this GitHub link: https://github.com/VideoExpertsGroup/Cloud.SDK.Embedded