Skip to content

Commit

Permalink
fix: nalunit analyze.
Browse files Browse the repository at this point in the history
  • Loading branch information
SighingSnow committed Nov 19, 2021
1 parent bc77c84 commit ca268c1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
50 changes: 27 additions & 23 deletions src/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,62 @@ void Encoder::Init()
av_register_all();
avcodec_register_all();
avformat_network_init();

int ret = 0;
in_buf[0] = (uint8_t*)malloc(sizeof(uint8_t)*SCR_HEIGHT*SCR_WIDTH*3);
in_buf[1] = nullptr;

// Output file context

avformat_alloc_output_context2(&ofctx,NULL,NULL,out_filename);
outputFormat = ofctx->oformat;

if(avio_open(&ofctx->pb,out_filename,AVIO_FLAG_READ_WRITE) < 0)
if(avio_open2(&ofctx->pb,out_filename,AVIO_FLAG_WRITE, nullptr,nullptr) < 0)
{
std::cout << "open outfile error" << std::endl;
return;
}
stream = avformat_new_stream(ofctx,NULL);

codec = avcodec_find_encoder(AV_CODEC_ID_H264);
stream = avformat_new_stream(ofctx,codec);
codecCtx = avcodec_alloc_context3(codec);

if(stream == nullptr){
std::cout<<"Create new stream fails"<<std::endl;
return;
}
stream->r_frame_rate.den = 1;
stream->r_frame_rate.num = 40;
// AV_CODEC_ID is declared in common.h
//codec = avcodec_find_encoder((AVCodecID) AV_CODEC_ID);
codecCtx = stream->codec;
codecCtx->codec_id = outputFormat->video_codec;
const AVRational dst_fps = {25,1};
codecCtx->codec_tag = 0;
codecCtx->codec_id = AV_CODEC_ID_H264;
codecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
codecCtx->bit_rate = 400000;
codecCtx->width = SCR_WIDTH;
codecCtx->height = SCR_HEIGHT;
codecCtx->time_base.num = 1;
codecCtx->time_base.den = 40;
codecCtx->gop_size = 40;
//codecCtx->framerate = (AVRational){25,1};
codecCtx->gop_size = 25;
codecCtx->framerate = dst_fps;
codecCtx->time_base = av_inv_q(dst_fps);
//codecCtx->bit_rate = SCR_HEIGHT * SCR_WIDTH * 3;
codecCtx->qmin = 10;
codecCtx->qmax = 51;
codecCtx->max_b_frames = 0;
if(ofctx->oformat->flags & AVFMT_GLOBALHEADER){
codecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
ret = avcodec_parameters_from_context(stream->codecpar,codecCtx);
if(ret < 0){
std::cout<<"Could not initialize stream codec parameters"<<std::endl;
exit(1);
}

AVDictionary* para = nullptr;
if(codecCtx->codec_id == AV_CODEC_ID_H264){
av_dict_set(&para,"preset","slow",0);
av_dict_set(&para,"tune","zerolatency",0);
}

codec = avcodec_find_encoder(codecCtx->codec_id);
if(!codec){
std::cout<<"can not find encoder"<<std::endl;
return;
}
int ret = avcodec_open2(codecCtx,codec,&para);
ret = avcodec_open2(codecCtx,codec,&para);
if(ret < 0){
std::cout<<"avcodec_open2 failed"<<std::endl;
return;
}
stream->codecpar->extradata_size = codecCtx->extradata_size;
stream->codecpar->extradata = codecCtx->extradata;
avformat_write_header(ofctx,NULL);
pkt = av_packet_alloc();
if(!pkt)
Expand Down Expand Up @@ -135,6 +138,7 @@ void Encoder::GenOnePkt(uint8_t* buffer,uint8_t** ret_buf,int& ret_buf_size)
DumpLocalVideo();
}
ret_buf_size = pkt->size;
//printf("%d\n",ret_buf_size);
*ret_buf = (uint8_t*)malloc(ret_buf_size*sizeof(uint8_t));
memcpy(*ret_buf,pkt->data,sizeof(uint8_t)*ret_buf_size);
// TODO: send this pkt data to rtsp out file.
Expand Down
6 changes: 6 additions & 0 deletions src/h264fileparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ void H264FileParser::loadNextSample(std::vector<std::byte>& buffer) {
// uint32_t length = ntohl(*lengthPtr);
// auto naluStartIndex = i + 4;
// auto naluEndIndex = naluStartIndex + length;
// if(naluEndIndex > sample.size()){
// ofstream ofs("xxx.h264");
// std::vector<uint8_t> aloha = *reinterpret_cast<std::vector<uint8_t> *>(&buffer);
// for(int i = 0 ; i < buffer.size();i++)
// ofs<<aloha[i];
// }
// assert(naluEndIndex <= sample.size());
// auto header = reinterpret_cast<rtc::NalUnitHeader *>(sample.data() + naluStartIndex);
// auto type = header->unitType();
Expand Down
4 changes: 2 additions & 2 deletions src/rtc_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ shared_ptr<Stream> RtcPublisher::createStream(const unsigned int fps) {
if (rtpConfig->timestampToSeconds(reportElapsedTimestamp) > 1) {
trackData->sender->setNeedsToReport();
}
cout << "Sending " << streamType << " sample with size: " << to_string(sample.size()) << " to " << client << endl;
cout << "Sending " << streamType << " frame with size: " << to_string(sample.size()) << " to " << client << endl;
bool send = false;
try {
// send sample
Expand Down Expand Up @@ -266,7 +266,7 @@ shared_ptr<ClientTrackData> RtcPublisher::addVideo(const shared_ptr<PeerConnecti
// create RTP configuration
auto rtpConfig = make_shared<RtpPacketizationConfig>(ssrc, cname, payloadType, H264RtpPacketizer::defaultClockRate);
// create packetizer
auto packetizer = make_shared<H264RtpPacketizer>(H264RtpPacketizer::Separator::Length, rtpConfig);
auto packetizer = make_shared<H264RtpPacketizer>(H264RtpPacketizer::Separator::LongStartSequence, rtpConfig);
// create H264 handler
auto h264Handler = make_shared<H264PacketizationHandler>(packetizer);
// add RTCP SR handler
Expand Down
2 changes: 1 addition & 1 deletion src/rtc_publisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RtcPublisher {
RtcPublisher(){
ws = make_shared<WebSocket>();
rtcThread = new DispatchQueue("RtcThread") ;
video = make_shared<H264FileParser>(30,true);
video = make_shared<H264FileParser>(25,true);
}
void setUp();
void publish(uint8_t *buf, int size);
Expand Down

0 comments on commit ca268c1

Please sign in to comment.