Qt audio and video development 20-Haikang sdk local playback

I. Introduction

Hikvision SDK contains MP4 decoding and playback library. The corresponding API functions all start with PlayM4. As the name suggests, it plays MP4. Hikvision’s video can be saved as MP4 file by default and can be played with a general player. That’s why. The various core video storages mentioned in many articles have been changed to MP4 for storage, such as vlc, mpv, ffmpeg, etc., and the final video storage is converted to MP4 files. It seems that big companies and manufacturers also save them as MP4 files. There is his reason, as for how much reason there is, just follow his scholars and he can handle it like this. You can just start writing the code without asking. Some things need to be studied in depth, and some things need not be too detailed. It’s a dead end. It’s like knowing that 1+1=2. As for why=2, just leave it to a dedicated researcher to study.

The MP4 decoding library of Haikang SDK supports its own MP4 files as well as regular MP4 files, and the latest version also supports AVI file format. I just tried the local MP4 files and they are all supported. If you need to get the audio For video data, you only need to call PlayM4_SetDecCallBackMend to set the decoding callback function, which is much simpler than the callback process of the video stream. It does not have a lot of processing to set the callback processing function, and only needs to be decoded and converted into QImage. The local file also needs to add a file end callback, so that you can know when the file has been played, and send the corresponding signal for processing. You can call PlayM4_SetFileEndCallback to set the file playback complete callback function. In the FileEndCallback callback function, execute QMetaObject::invokeMethod asynchronously Stop the function to notify the interface.

Please note the following points about local files:

  1. In file playback mode, PlayM4_CloseStream cannot be called.
  2. Under streaming playback, the PlayM4_CloseFile interface cannot be called.
  3. The stream switch interface and the file switch interface cannot be used interchangeably.

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::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;
}

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/108274916