由于数据是第三方接口提供,得到的数据是RTP数据,需要自己分离出H264数据,于是我采用以下代码进行分离,但分离出来的数据无法用ffmpeg解码,不知道是不是分离的代码有问题,请弄过的人帮我解答一下。
class RTPUnPacker
{
public:
RTPUnPacker();
~RTPUnPacker();
bool unpack_rtp_h264(const uint8_t * data, uint32_t data_size);
uint8_t * data() { return _buffer; }
uint32_t data_size() { return _data_size; }
private:
void resize(uint32_t size);
private:
uint8_t * _buffer; /// 00 00 00 01 + NALU + Data
uint32_t _data_size;
uint32_t _buffer_size;
};
RTPUnPacker::RTPUnPacker()
: _buffer(NULL),
_buffer_size(0),
_data_size(0)
{
resize(5);
_buffer[3] = 0x1;
}
RTPUnPacker::~RTPUnPacker()
{
free(_buffer);
}
void RTPUnPacker::resize(uint32_t size)
{
if (_buffer == NULL)
{
_buffer = (uint8_t *)calloc(1, size);
_buffer_size = size;
}
else if (_buffer_size < size)
{
_buffer = (uint8_t *)realloc(_buffer, size);
qDebug() << "buffer address " << (long)_buffer;
_buffer_size = size;
}
}
bool RTPUnPacker::unpack_rtp_h264(const uint8_t * data, uint32_t data_size)
{
if (data_size < 12)
{
return false;
}
if ((data[12] & 0x1F) == 0x1C) /// 分片
{
if ((data[13] & 0x80) == 0x80) /// 开始
{
_buffer[4] = (data[12] & 0x60) | (data[13] & 0x1F);
_data_size = 5;
}
/// 扩容data_size - 14字节
resize(_data_size + data_size - 14);
/// 缓存数据
memcpy(_buffer + _data_size, data + 14, data_size - 14);
_data_size += data_size - 14;
return ((data[13] & 0x40) == 0x40); /// 是否已经结束
}
else /// 未分片
{
_buffer[4] = data[12];
_data_size = 5;
/// 扩容data_size - 13字节
resize(_data_size + data_size - 13);
/// 缓存数据
memcpy(_buffer + _data_size, data + 13, data_size - 13);
_data_size += data_size - 13;
return true;
}
return false;
}
代码有bug,头字节被覆盖了。