Qt Audio and Video Development 17-Haikang SDK Decoding

I. Introduction

In the field of video surveillance industry, Haikang deserves to be the boss, and has been on the first throne for many years. Recently, it is necessary to change the video surveillance system to the kernel of Haikang SDK, so I deliberately consulted the SDK user manual and SDK related The documents and files can be downloaded directly from the official website, and they are updated frequently. Currently, the SDK on Windows and Linux is provided. The SDK on Win is still updated quickly and complete, but the sdk on Linux has to be complained. It is very old. Very old version, don’t look at the package file is very new, in fact, the content inside is very old, I don’t know why the official does not also update the sdk on linux, maybe there is not much demand on linux now, but with domestic production With the vigorous advancement of linux, it is estimated that Linux will be gradually improved in the near future. Maybe the official has already had this plan. There is also a personal guess that the development kit on linux is estimated to be new internally used by the official. Yes, you may plan to make various clients on Linux yourself.

The official SDK manual is still very detailed, and it also gave demo codes in languages ​​such as VC/JAVA/C#. This is definitely worthy of praise. A product continues to sell well. There must be deep-seated reasons. In addition to the excellent quality of the product itself, the surrounding services must also be kept up. Since I am accustomed to using Qt for development, I directly compiled the VC demo and ran in one step. The interface is relatively simple and ugly, but most of the functions I want are still available, and many buttons may not be displayed. It’s neat, whatever it is, what we want is the function, demonstrate how to use the corresponding method function, etc. As for the interface beautification, try to give it to professional UI girls. If there is no UI girl, then get some qss stylesheets by yourself. Make do with it, the beautification core of the interface is mainly two points: layout + color.

The official packaged all the SDKs of their commonly used devices. One header file is big, and the header file HCNetSDK.h is 47,451 lines. My deed, it is estimated that it is for the convenience of users to put them in one header file. Inside, in fact, these SDKs not only support cameras, but also NVR/CVR and other devices. They have good compatibility and price. You can use the same SDK. The official is just to unify one category of different categories. There are still some differences between the sdk between hardware products, such as the popular face recognition, infrared temperature measurement, etc., which are all newly added hardware and functions, but the overall interface and use process are generally the same.

Decoding process flow:

  1. Call NET_DVR_Init to initialize the SDK, and only need to initialize once in a project.
  2. Call NET_DVR_SetConnectTime, NET_DVR_SetReconnect to set the connection time and reconnection time.
  3. Fill the NET_DVR_PREVIEWINFO structure data to log in.
  4. Call NET_DVR_Login_V30 to log in to the device.
  5. Call NET_DVR_RealPlay_V40 to play the video.
  6. Call NET_DVR_StopRealPlay to stop playing.
  7. Call NET_DVR_Logout to log out of the device.

2. Features

  1. Supports playing video streams and local MP4 files.
  2. Support two modes: handle and callback.
  3. Multi-threaded display images, not stuck in the main interface.
  4. Reconnect the webcam automatically.
  5. You can set the border size, offset and border color.
  6. You can set whether to draw OSD labels, that is, label text or pictures and label positions.
  7. Two OSD positions and styles can be set.
  8. Can set whether to save to file and file name.
  9. You can directly drag files to haikangwidget control to play.
  10. Support h264/h265 video stream.
  11. Can pause and resume playing.
  12. Support storage of single video files and timing storage of video files.
  13. Customize the top floating bar, send a click signal notification, and set whether to enable it.
  14. You can set the screen stretch fill or equal proportion fill.
  15. You can set the decoding to be speed priority, quality priority, and equalization processing.
  16. You can take screenshots (original pictures) and screenshots (video forms) of videos.
  17. The video files are stored as MP4 files.
  18. Support focus control, pan/tilt control.
  19. Customizable functions.

Three, renderings

Insert picture description here

Four, related sites

  1. Domestic site: https://gitee.com/feiyangqingyun/QWidgetDemo
  2. International site: https://github.com/feiyangqingyun/QWidgetDemo
  3. Personal homepage: https://blog.csdn.net/feiyangqingyun
  4. Zhihu Homepage: https://www.zhihu.com/people/feiyangqingyun/
  5. Experience address: https://blog.csdn.net/feiyangqingyun/article/details/97565652

Five, the core code

bool HaiKangThread::playRtsp()
{
    bool ok = false;
    QString ip, userName, userPwd;
    int port, streamType;
    getInfo(ip, port, streamType, userName, userPwd);

    //登录设备
    NET_DVR_DEVICEINFO_V30 deviceInfo;
    long userid = NET_DVR_Login_V30(ip.toUtf8().data(), port, userName.toUtf8().data(), userPwd.toUtf8().data(), &deviceInfo);
    if (userid >= 0) {
        qDebug() << TIMEMS << "登录海康设备成功" << userid << deviceInfo.sSerialNumber;
        //这里还需要拿到视频流的宽高
        //qDebug() << TIMEMS << url << "videoWidth:" << videoWidth << "videoHeight:" << videoHeight;

        //以下参数具体见对应头文件说明
        NET_DVR_PREVIEWINFO previewInfo;
        previewInfo.lChannel = 1;
        previewInfo.dwStreamType = streamType;
        previewInfo.dwLinkMode = (transport == "tcp" ? 0 : 1);
        previewInfo.bBlocked = 0;
        previewInfo.byProtoType = 1;
        previewInfo.byPreviewMode = 0;
        previewInfo.dwDisplayBufNum = 15;

        //回调则可以拿到音视频数据,否则就直接句柄播放
        if (callback) {
            hand = NET_DVR_RealPlay_V40(userid, &previewInfo, RealDataCallBack, this);
        } else {
            previewInfo.hPlayWnd = (HWND)playWidget->winId();
            hand = NET_DVR_RealPlay_V40(userid, &previewInfo, NULL, NULL);
        }

        if (hand >= 0) {
            ok = true;
            qDebug() << TIMEMS << "打开视频数据成功" << url << "码流" << streamType;
        } else {
            qDebug() << TIMEMS << "打开视频数据失败" << url << NET_DVR_GetLastError();
        }
    } else {
        qDebug() << TIMEMS << "登录海康设备失败" << NET_DVR_GetLastError();
    }

    return ok;
}

bool HaiKangThread::playLocal()
{
    //转码以便支持中文路径
    QTextCodec *codec = QTextCodec::codecForName("gb2312");
    QByteArray data = codec->fromUnicode(url);

    PlayM4_GetPort(&port);
    bool ok = PlayM4_OpenFile(port, data.data());
    if (ok) {
        //设置文件播放完毕回调函数
        PlayM4_SetFileEndCallback(port, FileEndCallback, this);

        //回调则可以拿到音视频数据,否则就直接句柄播放
        if (callback) {
            PlayM4_SetDecCallBackMend(port, DecCallBack, (quser)this);
            PlayM4_Play(port, NULL);
        } else {
            PlayM4_Play(port, (HWND)playWidget->winId());
        }

        //同时播放声音
        PlayM4_PlaySound(port);

        //倒放
        //PlayM4_ReversePlay(port);

        //快进播放,多次调用速度更快
        //PlayM4_Fast(port);
        //PlayM4_Fast(port);

        ok = true;
        qDebug() << TIMEMS << "打开视频文件成功" << url;
    } else {
        qDebug() << TIMEMS << "打开视频文件失败" << url << PlayM4_GetLastError(port);
    }

    return ok;
}

bool HaiKangThread::init()
{
    //判断该摄像机是否能联通
    if (checkConn && isRtsp) {
        if (!checkUrl(url, checkTime)) {
            return false;
        }
    }

    if (playWidget == NULL) {
        return false;
    }

    //视频流和本地文件分别处理
    bool ok = false;
    if (isRtsp) {
        ok = playRtsp();
    } else {
        ok = playLocal();
    }

    if (!ok) {
        return false;
    }

    //设置保存文件
    this->initSave();
    return true;
}

void HaiKangThread::free()
{
    if (isRtsp) {
        //停止播放+登出设备
        NET_DVR_StopRealPlay(hand);
        NET_DVR_Logout(hand);
        hand = -1;
    } else {
        //停止播放+关闭文件+释放端口
        PlayM4_Stop(port);
        PlayM4_StopSound();
        PlayM4_CloseFile(port);
        PlayM4_FreePort(port);
        port = -1;
    }
}

Guess you like

Origin blog.csdn.net/feiyangqingyun/article/details/108213015