監控直播方案
越來越多人的開始將普通安防攝像機接入網絡流媒體服務器,進行網絡直播。但是市面上安防攝像機的品牌、型號比較多,想通過SDK方式接入這些各種攝像機不現實。由於安防攝像機基本都是可以輸出rtsp流,因此我們可以用拉取RTSP流的方式來兼容各家的攝像機。LiveNVR就是實現接入普通RTSP/Onvif的安防攝像機,轉成RTMP和HLS流,可以在各平台上進行H5 web直播或者app直播。
直播方案中快照抓取
LiveNVR實現了拉取攝像機RTSP流,實現監控直播。直播的過程只是流媒體數據轉發的過程,不進行解碼,這樣對CPU等硬件資源消耗較小。
但是直播時,可能希望定時抓取一張實時圖片進行封面展示、數據分析等。這種情況就需要定時獲取一幀數據進行解碼,抓成jpg或bmp圖片。
如下是將視頻某一幀數據轉成圖片的代碼:
bool LiveNVRChannel::rawData2Image(char* rawBuf, int bufSize, int codec, int width, int height, const char* jpgpath)
{
decodeParam.codec = codec;
decodeParam.width = width;
decodeParam.height = height;
if (!decoderHelper_)
{
decoderHelper_ = new DecoderHelper;
}
decoderHelper_->SetVideoDecoderParam(width, height, codec);
int ret = decoderHelper_->DecodeVideo(rawBuf, bufSize);
if (ret < 0)
{
return false;
}
decoderHelper_->WriteJPEG(jpgpath);
if (QTSServerInterface::GetServer()->GetThridPartPlatformModule())
{
if (!decodeParam.imageData)
decodeParam.imageData = new char[width * height * 3];
memset(decodeParam.imageData, 0, width * height * 3);
FILE* snapFile = ::fopen(jpgpath, "rb");
if (snapFile)
{
// obtain file size: 獲得文件大小
::fseek(snapFile, 0, SEEK_END); // 指針移到文件末位
decodeParam.imageSize = ftell(snapFile); // 獲得文件長度
::rewind(snapFile); // 函數rewind()把文件指針移到由stream(流)指定的開始處, 同時清除和流相關的錯誤和EOF標記
::fread(decodeParam.imageData, 1, decodeParam.imageSize, snapFile);
::fclose(snapFile);
}
}
return true;
}
在線演示
LiveNVR就是實現將傳統安防RTSP攝像機實現在互聯網直播、錄像、回放,兼容Windows和各移動終端。
大家可以在 http://nvr.liveqing.com:10800