1、根据RTSP的Announce请求中的sdp信息和RTP包中的信息初始化音视频编解码器
o_video_stream = avformat_new_stream(o_fmt_ctx, NULL);
{
AVCodecContext *c;
c = o_video_stream->codec;
c->codec_id = AV_CODEC_ID_H264;
c->codec_type = AVMEDIA_TYPE_VIDEO;
c->time_base.num = 1;
c->time_base.den = 180000;
c->pkt_timebase = {1, 90000};
c->channels = 0;
c->width = 480;
c->height = 640;
c->coded_width = 480;
c->coded_height = 640;
c->pix_fmt = AV_PIX_FMT_YUV420P;
if (c->flags & AVFMT_GLOBALHEADER)
c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
o_audio_stream = avformat_new_stream(o_fmt_ctx, NULL);
{
AVCodecContext *c;
c = o_audio_stream->codec;
c->audio_service_type = AV_AUDIO_SERVICE_TYPE_MAIN;
c->sample_rate = 8000;
c->sample_fmt = AV_SAMPLE_FMT_FLTP;
c->codec_id = AV_CODEC_ID_AAC;
c->codec_type = AVMEDIA_TYPE_AUDIO;
c->time_base.num = 1;
c->time_base.den = 8000;
c->channels = 1;
if (c->flags & AVFMT_GLOBALHEADER)
c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
2、将RTP包中的音视频数据提取出来
*pOutLen = 0;
if (InLen < RTP_HEAD_LENGTH) {
return false;
}
unsigned char *src = (unsigned char*)bufferIn + RTP_HEAD_LENGTH;
unsigned char fu_indicator = src[0];
unsigned char fu_header = src[1];
unsigned char nalu_type = fu_indicator & 0x1f;
if (nalu_type == 0x1C) { //FU-A
unsigned char start_flag = fu_header & 0x80;
unsigned char end_flag = fu_header & 0x40;
unsigned char nalu_header = (fu_indicator & 0xe0) | (fu_header & 0x1f);
if (start_flag != 0) {
memcpy(*pBufferOut, src - 3, InLen - RTP_HEAD_LENGTH + 3);
*((int*)(*pBufferOut)) = 0x01000000;
*((char*)(*pBufferOut + 4)) = nalu_header;
*pOutLen = InLen - RTP_HEAD_LENGTH + 3;
}
else if (end_flag != 0) {
memcpy(*pBufferOut, src + 2, InLen - RTP_HEAD_LENGTH - 2);
*pOutLen = InLen - RTP_HEAD_LENGTH - 2;
}
else {
memcpy(*pBufferOut, src + 2, InLen - RTP_HEAD_LENGTH - 2);
*pOutLen = InLen - RTP_HEAD_LENGTH - 2;
}
}
else if (nalu_type == 0x18) { //STAP-A
unsigned int srcOffset = 1 ,bufOffset = 0;
while (((InLen - RTP_HEAD_LENGTH) - srcOffset) > 2)
{
*((int*)(*pBufferOut + bufOffset)) = 0x01000000;
bufOffset += 4;
unsigned int size = 0;
size |= *(src + srcOffset) << 8;
size |= *(src + srcOffset +1);
srcOffset += 2;
memcpy(*pBufferOut + bufOffset, src+srcOffset, size);
bufOffset += size;
srcOffset += size;
}
*pOutLen = bufOffset;
}
else if (nalu_type == 0x1) {
*pOutLen = InLen - RTP_HEAD_LENGTH + 4;
memcpy(*pBufferOut, src - 4, *pOutLen);
*((int *)(*pBufferOut)) = 0x01000000;
}
else {
printf("Unsupport nalu type!\n");
}
bool bFinishFrame;
unsigned char *bufTemp = (unsigned char *)bufferIn;
if (bufTemp[1] & 0x80) {
bFinishFrame = true;
}
else {
bFinishFrame = false;
}
return bFinishFrame;