DeviceHK.cpp 8.7 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26 27 28 29
 */
#ifdef ENABLE_HKDEVICE
#include "DeviceHK.h"
#include "Util/TimeTicker.h"
#include "Util/MD5.h"
xiongziliang committed
30
namespace mediakit {
xzl committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

#define HK_APP_NAME  "live"

DeviceHK::DeviceHK() {
	InfoL << endl;
	static onceToken token( []() {
		NET_DVR_Init();
		NET_DVR_SetDVRMessageCallBack_V31([](LONG lCommand,NET_DVR_ALARMER *pAlarmer,char *pAlarmInfo,DWORD dwBufLen,void* pUser){
			WarnL<<pAlarmInfo;
			return TRUE;
		},NULL);
	}, []() {
		NET_DVR_Cleanup();
	});
}

DeviceHK::~DeviceHK() {
	InfoL << endl;
}

void DeviceHK::connectDevice(const connectInfo &info, const connectCB& cb, int iTimeOut) {
	NET_DVR_USER_LOGIN_INFO loginInfo;
	NET_DVR_DEVICEINFO_V40 loginResult;

	//login info
	strcpy(loginInfo.sDeviceAddress, info.strDevIp.c_str());
	loginInfo.wPort = info.ui16DevPort;
	strcpy(loginInfo.sUserName, info.strUserName.c_str());
	strcpy(loginInfo.sPassword, info.strPwd.c_str());

	//callback info
	typedef function< void(LONG lUserID, DWORD dwResult, LPNET_DVR_DEVICEINFO_V30 lpDeviceInfo)> hkLoginCB;
	loginInfo.bUseAsynLogin = TRUE;
	weak_ptr<Device> weakSelf = shared_from_this();
	loginInfo.pUser = new hkLoginCB([weakSelf,cb](LONG lUserID, DWORD dwResult, LPNET_DVR_DEVICEINFO_V30 lpDeviceInfo ) {
						//TraceL<<lUserID<<" "<<dwResult<<" "<<lpDeviceInfo->sSerialNumber;
						connectResult result;
						if(dwResult==TRUE) {
							result.strDevName=(char *)(lpDeviceInfo->sSerialNumber);
							result.ui16ChnStart=lpDeviceInfo->byStartChan;
							result.ui16ChnCount=lpDeviceInfo->byChanNum;
							auto _strongSelf=weakSelf.lock();
							if(_strongSelf) {
								auto strongSelf=dynamic_pointer_cast<DeviceHK>(_strongSelf);
								strongSelf->onConnected(lUserID,lpDeviceInfo);
							}
						} else {
							WarnL<<"connect deviceHK failed:"<<NET_DVR_GetLastError();
						}
						cb(dwResult==TRUE,result);
					});
	loginInfo.cbLoginResult = [](LONG lUserID, DWORD dwResult, LPNET_DVR_DEVICEINFO_V30 lpDeviceInfo , void* pUser) {
				auto *fun=static_cast<hkLoginCB *>(pUser);
				(*fun)(lUserID,dwResult,lpDeviceInfo);
				delete fun;
			};
	NET_DVR_SetConnectTime(iTimeOut * 1000, 3);
	NET_DVR_Login_V40(&loginInfo, &loginResult);
}

void DeviceHK::disconnect(const relustCB& cb) {
	m_mapChannels.clear();
	if (m_i64LoginId >= 0) {
		NET_DVR_Logout(m_i64LoginId);
		m_i64LoginId = -1;
		Device::onDisconnected(true);
	}

}

void DeviceHK::addChannel(int iChn, bool bMainStream) {
	DevChannel::Ptr channel( new DevChannelHK(m_i64LoginId, (char *) m_deviceInfo.sSerialNumber, iChn, bMainStream));
	m_mapChannels[iChn] = channel;
}

void DeviceHK::delChannel(int chn) {
	m_mapChannels.erase(chn);
}

void DeviceHK::onConnected(LONG lUserID, LPNET_DVR_DEVICEINFO_V30 lpDeviceInfo) {
	m_i64LoginId = lUserID;
	m_deviceInfo = *lpDeviceInfo;
	Device::onConnected();
}

void DeviceHK::addAllChannel(bool bMainStream) {
	InfoL << endl;
	for (int i = 0; i < m_deviceInfo.byChanNum; i++) {
		addChannel(m_deviceInfo.byStartChan + i, bMainStream);
	}
}

DevChannelHK::DevChannelHK(int64_t i64LoginId, const char* pcDevName, int iChn, bool bMainStream) :
		DevChannel(HK_APP_NAME,(StrPrinter<<MD5(pcDevName).hexdigest()<<"_"<<iChn<<endl).data()),
		m_i64LoginId(i64LoginId) {
	InfoL << endl;
	NET_DVR_PREVIEWINFO previewInfo;
	previewInfo.lChannel = iChn; //通道号
	previewInfo.dwStreamType = bMainStream ? 0 : 1; // 码流类型,0-主码流,1-子码流,2-码流3,3-码流4 等以此类推
	previewInfo.dwLinkMode = 1; // 0:TCP方式,1:UDP方式,2:多播方式,3 - RTP方式,4-RTP/RTSP,5-RSTP/HTTP
	previewInfo.hPlayWnd = 0; //播放窗口的句柄,为NULL表示不播放图象
	previewInfo.byProtoType = 0; //应用层取流协议,0-私有协议,1-RTSP协议
	previewInfo.dwDisplayBufNum = 1; //播放库播放缓冲区最大缓冲帧数,范围1-50,置0时默认为1
	previewInfo.bBlocked = 0;
	m_i64PreviewHandle = NET_DVR_RealPlay_V40(m_i64LoginId, &previewInfo,
										[](LONG lPlayHandle,DWORD dwDataType,BYTE *pBuffer,DWORD dwBufSize,void* pUser) {
											DevChannelHK *self=reinterpret_cast<DevChannelHK *>(pUser);
											if(self->m_i64PreviewHandle!=(int64_t)lPlayHandle) {
												return;
											}
											self->onPreview(dwDataType,pBuffer,dwBufSize);
										}, this);
	if (m_i64PreviewHandle == -1) {
		throw std::runtime_error( StrPrinter 	<< "设备[" << pcDevName << "/" << iChn << "]开始实时预览失败:"
												<< NET_DVR_GetLastError() << endl);
	}
}

DevChannelHK::~DevChannelHK() {
	InfoL << endl;
	if (m_i64PreviewHandle >= 0) {
		NET_DVR_StopRealPlay(m_i64PreviewHandle);
		m_i64PreviewHandle = -1;
	}
	if (m_iPlayHandle >= 0) {
		PlayM4_StopSoundShare(m_iPlayHandle);
		PlayM4_Stop(m_iPlayHandle);
		m_iPlayHandle = -1;
	}
}

void DevChannelHK::onPreview(DWORD dwDataType, BYTE* pBuffer, DWORD dwBufSize) {
	//TimeTicker1(-1);
	switch (dwDataType) {
	case NET_DVR_SYSHEAD: { //系统头数据
		if (!PlayM4_GetPort(&m_iPlayHandle)) {  //获取播放库未使用的通道号
			WarnL << "PlayM4_GetPort:" << NET_DVR_GetLastError();
			break;
		}
		if (dwBufSize > 0) {
			if (!PlayM4_SetStreamOpenMode(m_iPlayHandle, STREAME_REALTIME)) { //设置实时流播放模式
				WarnL << "PlayM4_SetStreamOpenMode:" << NET_DVR_GetLastError();
				break;
			}
			if (!PlayM4_OpenStream(m_iPlayHandle, pBuffer, dwBufSize,
					1024 * 1024)) {  //打开流接口
				WarnL << "PlayM4_OpenStream:" << NET_DVR_GetLastError();
				break;
			}

			PlayM4_SetDecCallBackMend(m_iPlayHandle,
					[](int nPort,char * pBuf,int nSize,FRAME_INFO * pFrameInfo, void* nUser,int nReserved2) {
						DevChannelHK *chn=reinterpret_cast<DevChannelHK *>(nUser);
						if(chn->m_iPlayHandle!=nPort) {
							return;
						}
						chn->onGetDecData(pBuf,nSize,pFrameInfo);
					}, this);
			if (!PlayM4_Play(m_iPlayHandle, 0)) {  //播放开始
				WarnL << "PlayM4_Play:" << NET_DVR_GetLastError();
				break;
			}
			InfoL << "设置解码器成功!" << endl;
			//打开音频解码, 需要码流是复合流
			if (!PlayM4_PlaySoundShare(m_iPlayHandle)) {
				WarnL << "PlayM4_PlaySound:" << NET_DVR_GetLastError();
				break;
			}
		}
	}
		break;
	case NET_DVR_STREAMDATA: { //流数据(包括复合流或音视频分开的视频流数据)
		if (dwBufSize > 0 && m_iPlayHandle != -1) {
			if (!PlayM4_InputData(m_iPlayHandle, pBuffer, dwBufSize)) {
				WarnL << "PlayM4_InputData:" << NET_DVR_GetLastError();
				break;
			}
		}
	}
		break;
	case NET_DVR_AUDIOSTREAMDATA: { //音频数据
	}
		break;
	case NET_DVR_PRIVATE_DATA: { //私有数据,包括智能信息
	}
		break;
	default:
		break;
	}
}

void DevChannelHK::onGetDecData(char* pBuf, int nSize, FRAME_INFO* pFrameInfo) {
	//InfoL << pFrameInfo->nType;
	switch (pFrameInfo->nType) {
	case T_YV12: {
		if (!m_bVideoSeted) {
			m_bVideoSeted = true;
			VideoInfo video;
			video.iWidth = pFrameInfo->nWidth;
			video.iHeight = pFrameInfo->nHeight;
			video.iFrameRate = pFrameInfo->nFrameRate;
			initVideo(video);
		}
		char *yuv[3];
		int yuv_len[3];
		yuv_len[0] = pFrameInfo->nWidth;
		yuv_len[1] = pFrameInfo->nWidth / 2;
		yuv_len[2] = pFrameInfo->nWidth / 2;
		int dwOffset_Y = pFrameInfo->nWidth * pFrameInfo->nHeight;
		yuv[0] = pBuf;
		yuv[2] = yuv[0] + dwOffset_Y;
		yuv[1] = yuv[2] + dwOffset_Y / 4;
		inputYUV(yuv, yuv_len, pFrameInfo->nStamp);
	}
		break;
	case T_AUDIO16: {
		if (!m_bAudioSeted) {
			m_bAudioSeted = true;
			AudioInfo audio;
			audio.iChannel = pFrameInfo->nWidth;
			audio.iSampleBit = pFrameInfo->nHeight;
			audio.iSampleRate = pFrameInfo->nFrameRate;
			initAudio(audio);
		}
		inputPCM(pBuf, nSize, pFrameInfo->nStamp);
	}
		break;
	default:
		break;
	}
}

xiongziliang committed
263
} /* namespace mediakit */
xzl committed
264 265

#endif //ENABLE_HKDEVICE