Implementation for jitter

This commit is contained in:
dijunkun
2023-09-08 17:45:01 +08:00
parent dc11f50d82
commit 79c838629a
9 changed files with 177 additions and 34 deletions

View File

@@ -6,16 +6,33 @@
RtpVideoReceiver::RtpVideoReceiver() {}
RtpVideoReceiver::~RtpVideoReceiver() {}
RtpVideoReceiver::~RtpVideoReceiver() {
if (jitter_thread_ && jitter_thread_->joinable()) {
jitter_thread_->join();
delete jitter_thread_;
jitter_thread_ = nullptr;
}
}
void RtpVideoReceiver::InsertRtpPacket(RtpPacket& rtp_packet) {
if (!jitter_thread_) {
jitter_thread_ = new std::thread(&RtpVideoReceiver::Process, this);
}
if (NAL_UNIT_TYPE::NALU == rtp_packet.NalUnitType()) {
// compelete_video_frame_queue_.push(
// VideoFrame(rtp_packet.Payload(), rtp_packet.Size()));
if (on_receive_complete_frame_) {
on_receive_complete_frame_(
VideoFrame(rtp_packet.Payload(), rtp_packet.Size()));
}
compelete_video_frame_queue_.push(
VideoFrame(rtp_packet.Payload(), rtp_packet.Size()));
// if (on_receive_complete_frame_) {
// auto now_complete_frame_ts =
// std::chrono::high_resolution_clock::now().time_since_epoch().count()
// / 1000000;
// uint32_t duration = now_complete_frame_ts - last_complete_frame_ts_;
// LOG_ERROR("Duration {}", 1000 / duration);
// last_complete_frame_ts_ = now_complete_frame_ts;
// on_receive_complete_frame_(
// VideoFrame(rtp_packet.Payload(), rtp_packet.Size()));
// }
} else if (NAL_UNIT_TYPE::FU_A == rtp_packet.NalUnitType()) {
incomplete_frame_list_[rtp_packet.SequenceNumber()] = rtp_packet;
bool complete = CheckIsFrameCompleted(rtp_packet);
@@ -53,16 +70,22 @@ bool RtpVideoReceiver::CheckIsFrameCompleted(RtpPacket& rtp_packet) {
incomplete_frame_list_.erase(start);
}
// compelete_video_frame_queue_.push(
// VideoFrame(nv12_data_, complete_frame_size));
compelete_video_frame_queue_.push(
VideoFrame(nv12_data_, complete_frame_size));
// LOG_ERROR("Size of compelete_video_frame_queue_ [{}]",
// compelete_video_frame_queue_.size());
// if (on_receive_complete_frame_) {
// auto now_complete_frame_ts =
// std::chrono::high_resolution_clock::now()
// .time_since_epoch()
// .count() /
// 1000000;
// uint32_t duration = now_complete_frame_ts -
// last_complete_frame_ts_; LOG_ERROR("Duration {}", 1000 / duration);
// last_complete_frame_ts_ = now_complete_frame_ts;
if (on_receive_complete_frame_) {
on_receive_complete_frame_(
VideoFrame(nv12_data_, complete_frame_size));
}
// on_receive_complete_frame_(
// VideoFrame(nv12_data_, complete_frame_size));
// }
return true;
} else {
LOG_WARN("What happened?")
@@ -73,4 +96,25 @@ bool RtpVideoReceiver::CheckIsFrameCompleted(RtpPacket& rtp_packet) {
return true;
}
return false;
}
void RtpVideoReceiver::Process() {
while (1) {
if (!compelete_video_frame_queue_.isEmpty()) {
VideoFrame video_frame;
compelete_video_frame_queue_.pop(video_frame);
if (on_receive_complete_frame_) {
auto now_complete_frame_ts = std::chrono::high_resolution_clock::now()
.time_since_epoch()
.count() /
1000000;
uint32_t duration = now_complete_frame_ts - last_complete_frame_ts_;
LOG_ERROR("Duration {}", 1000 / duration);
last_complete_frame_ts_ = now_complete_frame_ts;
on_receive_complete_frame_(video_frame);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(13));
}
}