2021-01-07 00:26:08 +08:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
** H264_DeviceSource.cpp
|
|
|
|
**
|
|
|
|
** H264 Live555 source
|
|
|
|
**
|
|
|
|
** -------------------------------------------------------------------------*/
|
|
|
|
|
2021-01-27 23:50:07 +08:00
|
|
|
#include "zm.h"
|
|
|
|
|
|
|
|
#if HAVE_RTSP_SERVER
|
|
|
|
|
2021-01-07 00:26:08 +08:00
|
|
|
#include <sstream>
|
2021-01-21 23:23:30 +08:00
|
|
|
#include <iomanip>
|
2021-01-07 00:26:08 +08:00
|
|
|
|
|
|
|
// live555
|
|
|
|
#include <Base64.hh>
|
|
|
|
|
|
|
|
#include "zm_rtsp_server_h264_device_source.h"
|
|
|
|
|
|
|
|
// ---------------------------------
|
|
|
|
// H264 ZoneMinder FramedSource
|
|
|
|
// ---------------------------------
|
2021-01-21 23:23:30 +08:00
|
|
|
//
|
|
|
|
H264_ZoneMinderDeviceSource::H264_ZoneMinderDeviceSource(
|
|
|
|
UsageEnvironment& env,
|
|
|
|
Monitor *monitor,
|
|
|
|
AVStream *stream,
|
|
|
|
unsigned int queueSize,
|
|
|
|
bool repeatConfig,
|
|
|
|
bool keepMarker)
|
|
|
|
: H26X_ZoneMinderDeviceSource(env, monitor, stream, queueSize, repeatConfig, keepMarker)
|
|
|
|
{
|
|
|
|
// extradata appears to simply be the SPS and PPS NAL's
|
2021-01-23 08:29:01 +08:00
|
|
|
this->splitFrames(m_stream->codecpar->extradata, m_stream->codecpar->extradata_size);
|
2021-01-21 23:23:30 +08:00
|
|
|
}
|
2021-01-07 00:26:08 +08:00
|
|
|
|
|
|
|
// split packet into frames
|
|
|
|
std::list< std::pair<unsigned char*, size_t> > H264_ZoneMinderDeviceSource::splitFrames(unsigned char* frame, unsigned frameSize) {
|
|
|
|
std::list< std::pair<unsigned char*, size_t> > frameList;
|
|
|
|
|
|
|
|
size_t bufSize = frameSize;
|
|
|
|
size_t size = 0;
|
|
|
|
unsigned char* buffer = this->extractFrame(frame, bufSize, size);
|
|
|
|
while ( buffer != nullptr ) {
|
|
|
|
switch ( m_frameType & 0x1F ) {
|
|
|
|
case 7:
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "SPS_Size: %d bufSize %d", size, bufSize);
|
|
|
|
m_sps.assign((char*)buffer, size);
|
2021-01-07 00:26:08 +08:00
|
|
|
break;
|
|
|
|
case 8:
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "PPS_Size: %d bufSize %d", size, bufSize);
|
2021-02-01 00:43:16 +08:00
|
|
|
m_pps.assign((char*)buffer, size);
|
2021-01-07 00:26:08 +08:00
|
|
|
break;
|
|
|
|
case 5:
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "IDR_Size: %d bufSize %d", size, bufSize);
|
2021-01-07 00:26:08 +08:00
|
|
|
if ( m_repeatConfig && !m_sps.empty() && !m_pps.empty() ) {
|
|
|
|
frameList.push_back(std::pair<unsigned char*,size_t>((unsigned char*)m_sps.c_str(), m_sps.size()));
|
|
|
|
frameList.push_back(std::pair<unsigned char*,size_t>((unsigned char*)m_pps.c_str(), m_pps.size()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "Unknown frametype!? %d %d", m_frameType, m_frameType & 0x1F);
|
2021-01-07 00:26:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !m_sps.empty() && !m_pps.empty() ) {
|
|
|
|
u_int32_t profile_level_id = 0;
|
|
|
|
if ( m_sps.size() >= 4 ) profile_level_id = (m_sps[1]<<16)|(m_sps[2]<<8)|m_sps[3];
|
|
|
|
|
|
|
|
char* sps_base64 = base64Encode(m_sps.c_str(), m_sps.size());
|
|
|
|
char* pps_base64 = base64Encode(m_pps.c_str(), m_pps.size());
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
os << "profile-level-id=" << std::hex << std::setw(6) << std::setfill('0') << profile_level_id;
|
2021-01-23 08:29:01 +08:00
|
|
|
os << ";sprop-parameter-sets=" << sps_base64 << "," << pps_base64;
|
2021-01-07 00:26:08 +08:00
|
|
|
m_auxLine.assign(os.str());
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "auxLine: %s", m_auxLine.c_str());
|
2021-01-07 00:26:08 +08:00
|
|
|
|
|
|
|
delete [] sps_base64;
|
|
|
|
delete [] pps_base64;
|
|
|
|
}
|
|
|
|
frameList.push_back(std::pair<unsigned char*,size_t>(buffer, size));
|
|
|
|
|
|
|
|
buffer = this->extractFrame(&buffer[size], bufSize, size);
|
2021-01-21 23:23:30 +08:00
|
|
|
} // end while buffer
|
2021-01-07 00:26:08 +08:00
|
|
|
return frameList;
|
|
|
|
}
|
|
|
|
|
2021-01-21 23:23:30 +08:00
|
|
|
H265_ZoneMinderDeviceSource::H265_ZoneMinderDeviceSource(
|
|
|
|
UsageEnvironment& env,
|
|
|
|
Monitor *monitor,
|
|
|
|
AVStream *stream,
|
|
|
|
unsigned int queueSize,
|
|
|
|
bool repeatConfig,
|
|
|
|
bool keepMarker)
|
|
|
|
: H26X_ZoneMinderDeviceSource(env, monitor, stream, queueSize, repeatConfig, keepMarker)
|
|
|
|
{
|
|
|
|
// extradata appears to simply be the SPS and PPS NAL's
|
2021-01-23 08:29:01 +08:00
|
|
|
this->splitFrames(m_stream->codecpar->extradata, m_stream->codecpar->extradata_size);
|
2021-01-21 23:23:30 +08:00
|
|
|
}
|
|
|
|
|
2021-01-07 00:26:08 +08:00
|
|
|
// split packet in frames
|
2021-01-21 23:23:30 +08:00
|
|
|
std::list< std::pair<unsigned char*,size_t> >
|
|
|
|
H265_ZoneMinderDeviceSource::splitFrames(unsigned char* frame, unsigned frameSize) {
|
2021-01-07 00:26:08 +08:00
|
|
|
std::list< std::pair<unsigned char*,size_t> > frameList;
|
|
|
|
|
|
|
|
size_t bufSize = frameSize;
|
|
|
|
size_t size = 0;
|
|
|
|
unsigned char* buffer = this->extractFrame(frame, bufSize, size);
|
|
|
|
while ( buffer != nullptr ) {
|
|
|
|
switch ((m_frameType&0x7E)>>1) {
|
|
|
|
case 32:
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "VPS_Size: %d bufSize %d", size, bufSize);
|
2021-01-07 00:26:08 +08:00
|
|
|
m_vps.assign((char*)buffer,size);
|
|
|
|
break;
|
|
|
|
case 33:
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "SPS_Size: %d bufSize %d", size, bufSize);
|
2021-01-07 00:26:08 +08:00
|
|
|
m_sps.assign((char*)buffer,size);
|
|
|
|
break;
|
|
|
|
case 34:
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "PPS_Size: %d bufSize %d", size, bufSize);
|
2021-01-07 00:26:08 +08:00
|
|
|
m_pps.assign((char*)buffer,size);
|
|
|
|
break;
|
|
|
|
case 19:
|
|
|
|
case 20:
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "IDR_Size: %d bufSize %d", size, bufSize);
|
2021-01-07 00:26:08 +08:00
|
|
|
if ( m_repeatConfig && !m_vps.empty() && !m_sps.empty() && !m_pps.empty() ) {
|
|
|
|
frameList.push_back(std::pair<unsigned char*,size_t>((unsigned char*)m_vps.c_str(), m_vps.size()));
|
|
|
|
frameList.push_back(std::pair<unsigned char*,size_t>((unsigned char*)m_sps.c_str(), m_sps.size()));
|
|
|
|
frameList.push_back(std::pair<unsigned char*,size_t>((unsigned char*)m_pps.c_str(), m_pps.size()));
|
|
|
|
}
|
2021-01-21 23:23:30 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Debug(1, "Unknown frametype!? %d %d", m_frameType, ((m_frameType & 0x7E) >> 1));
|
|
|
|
break;
|
2021-01-07 00:26:08 +08:00
|
|
|
}
|
|
|
|
|
2021-01-21 23:23:30 +08:00
|
|
|
if ( !m_vps.empty() && !m_sps.empty() && !m_pps.empty() ) {
|
2021-01-07 00:26:08 +08:00
|
|
|
char* vps_base64 = base64Encode(m_vps.c_str(), m_vps.size());
|
|
|
|
char* sps_base64 = base64Encode(m_sps.c_str(), m_sps.size());
|
|
|
|
char* pps_base64 = base64Encode(m_pps.c_str(), m_pps.size());
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
os << "sprop-vps=" << vps_base64;
|
|
|
|
os << ";sprop-sps=" << sps_base64;
|
|
|
|
os << ";sprop-pps=" << pps_base64;
|
|
|
|
m_auxLine.assign(os.str());
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "Assigned auxLine to %s", m_auxLine.c_str());
|
2021-01-07 00:26:08 +08:00
|
|
|
|
|
|
|
delete [] vps_base64;
|
|
|
|
delete [] sps_base64;
|
|
|
|
delete [] pps_base64;
|
|
|
|
}
|
|
|
|
frameList.push_back(std::pair<unsigned char*,size_t>(buffer, size));
|
|
|
|
|
|
|
|
buffer = this->extractFrame(&buffer[size], bufSize, size);
|
2021-01-21 23:23:30 +08:00
|
|
|
} // end while buffer
|
|
|
|
if ( bufSize ) {
|
|
|
|
Debug(1, "%d bytes remaining", bufSize);
|
|
|
|
}
|
2021-01-07 00:26:08 +08:00
|
|
|
return frameList;
|
2021-01-21 23:23:30 +08:00
|
|
|
} // end H265_ZoneMinderDeviceSource::splitFrames(unsigned char* frame, unsigned frameSize)
|
|
|
|
|
|
|
|
unsigned char * H26X_ZoneMinderDeviceSource::findMarker(
|
|
|
|
unsigned char *frame, size_t size, size_t &length
|
|
|
|
) {
|
|
|
|
//Debug(1, "findMarker %p %d", frame, size);
|
|
|
|
unsigned char *start = nullptr;
|
|
|
|
for ( size_t i = 0; i < size-2; i += 1 ) {
|
|
|
|
//Debug(1, "%d: %d %d %d", i, frame[i], frame[i+1], frame[i+2]);
|
|
|
|
if ( (frame[i] == 0) and (frame[i+1]) == 0 and (frame[i+2] == 1) ) {
|
|
|
|
if ( i and (frame[i-1] == 0) ) {
|
|
|
|
start = frame + i - 1;
|
|
|
|
length = sizeof(H264marker);
|
|
|
|
} else {
|
|
|
|
start = frame + i;
|
|
|
|
length = sizeof(H264shortmarker);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return start;
|
2021-01-07 00:26:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// extract a frame
|
|
|
|
unsigned char* H26X_ZoneMinderDeviceSource::extractFrame(unsigned char* frame, size_t& size, size_t& outsize) {
|
2021-01-21 23:23:30 +08:00
|
|
|
unsigned char *outFrame = nullptr;
|
|
|
|
Debug(1, "ExtractFrame: %p %d", frame, size);
|
2021-01-07 00:26:08 +08:00
|
|
|
outsize = 0;
|
2021-01-21 23:23:30 +08:00
|
|
|
size_t markerLength = 0;
|
|
|
|
size_t endMarkerLength = 0;
|
2021-01-07 00:26:08 +08:00
|
|
|
m_frameType = 0;
|
2021-01-21 23:23:30 +08:00
|
|
|
unsigned char *startFrame = nullptr;
|
|
|
|
if ( size >= 3 )
|
|
|
|
startFrame = this->findMarker(frame, size, markerLength);
|
2021-01-07 00:26:08 +08:00
|
|
|
if ( startFrame != nullptr ) {
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "startFrame: %p marker Length %d", startFrame, markerLength);
|
|
|
|
m_frameType = startFrame[markerLength];
|
2021-01-07 00:26:08 +08:00
|
|
|
|
2021-01-21 23:23:30 +08:00
|
|
|
int remainingSize = size-(startFrame-frame+markerLength);
|
|
|
|
unsigned char *endFrame = nullptr;
|
|
|
|
if ( remainingSize > 3 ) {
|
|
|
|
endFrame = this->findMarker(startFrame+markerLength, remainingSize, endMarkerLength);
|
|
|
|
}
|
|
|
|
Debug(1, "endFrame: %p marker Length %d, remaining size %d", endFrame, endMarkerLength, remainingSize);
|
2021-01-07 00:26:08 +08:00
|
|
|
|
|
|
|
if ( m_keepMarker ) {
|
|
|
|
size -= startFrame-frame;
|
|
|
|
outFrame = startFrame;
|
|
|
|
} else {
|
2021-01-21 23:23:30 +08:00
|
|
|
size -= startFrame-frame+markerLength;
|
|
|
|
outFrame = &startFrame[markerLength];
|
2021-01-07 00:26:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( endFrame != nullptr ) {
|
|
|
|
outsize = endFrame - outFrame;
|
|
|
|
} else {
|
|
|
|
outsize = size;
|
|
|
|
}
|
|
|
|
size -= outsize;
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(1, "Have frame type: %d size %d, keepmarker %d", m_frameType, outsize, m_keepMarker);
|
2021-01-07 00:26:08 +08:00
|
|
|
} else if ( size >= sizeof(H264shortmarker) ) {
|
|
|
|
Info("No marker found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return outFrame;
|
|
|
|
}
|
2021-01-27 07:29:02 +08:00
|
|
|
#endif
|