Skip to content

Commit

Permalink
Reland "Refactor RtpVideoStreamReceiver without RtpReceiver."
Browse files Browse the repository at this point in the history
This is a reland of 0b9e01d

Original change's description:
> Refactor RtpVideoStreamReceiver without RtpReceiver.
> 
> Bug: webrtc:7135
> Change-Id: Iabf3330e579b892efc160683f9f90efbf6ff9a40
> Reviewed-on: https://webrtc-review.googlesource.com/92398
> Commit-Queue: Niels Moller <[email protected]>
> Reviewed-by: Erik Språng <[email protected]>
> Reviewed-by: Magnus Jedvert <[email protected]>
> Reviewed-by: Danil Chapovalov <[email protected]>
> Cr-Commit-Position: refs/heads/master@{#24232}

Bug: webrtc:7135
Change-Id: I707d4c5262e7b428bc7ceac2d886ff34c4a8d76a
Reviewed-on: https://webrtc-review.googlesource.com/93261
Reviewed-by: Erik Språng <[email protected]>
Reviewed-by: Danil Chapovalov <[email protected]>
Commit-Queue: Niels Moller <[email protected]>
Cr-Commit-Position: refs/heads/master@{#24254}
  • Loading branch information
Niels Möller authored and Commit Bot committed Aug 10, 2018
1 parent 45e7281 commit 2ff1f2a
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 51 deletions.
2 changes: 2 additions & 0 deletions call/call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,7 @@ PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
return DELIVERY_OK;
}
} else if (media_type == MediaType::VIDEO) {
parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
if (video_receiver_controller_.OnRtpPacket(parsed_packet)) {
received_bytes_per_second_counter_.Add(length);
received_video_bytes_per_second_counter_.Add(length);
Expand Down Expand Up @@ -1327,6 +1328,7 @@ void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) {
parsed_packet.IdentifyExtensions(it->second.extensions);

// TODO(brandtr): Update here when we support protecting audio packets too.
parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
video_receiver_controller_.OnRtpPacket(parsed_packet);
}

Expand Down
3 changes: 3 additions & 0 deletions call/rtp_video_sender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ RtpVideoSender::RtpVideoSender(
// We add the highest spatial layer first to ensure it'll be prioritized
// when sending padding, with the hope that the packet rate will be smaller,
// and that it's more important to protect than the lower layers.

// TODO(nisse): Consider moving registration with PacketRouter last, after the
// modules are fully configured.
for (auto& rtp_rtcp : rtp_modules_) {
constexpr bool remb_candidate = true;
transport->packet_router()->AddSendRtpModule(rtp_rtcp.get(),
Expand Down
5 changes: 1 addition & 4 deletions modules/rtp_rtcp/source/rtp_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ RtpDepacketizer* RtpDepacketizer::Create(VideoCodecType type) {
return new RtpDepacketizerVp8();
case kVideoCodecVP9:
return new RtpDepacketizerVp9();
case kVideoCodecGeneric:
return new RtpDepacketizerGeneric();
default:
RTC_NOTREACHED();
return new RtpDepacketizerGeneric();
}
return nullptr;
}
} // namespace webrtc
119 changes: 83 additions & 36 deletions video/rtp_video_stream_receiver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
#include <utility>
#include <vector>

#include "absl/memory/memory.h"

#include "common_types.h" // NOLINT(build/include)
#include "media/base/mediaconstants.h"
#include "modules/pacing/packet_router.h"
#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
#include "modules/rtp_rtcp/include/receive_statistics.h"
#include "modules/rtp_rtcp/include/rtp_cvo.h"
#include "modules/rtp_rtcp/include/rtp_receiver.h"
#include "modules/rtp_rtcp/include/rtp_rtcp.h"
#include "modules/rtp_rtcp/include/ulpfec_receiver.h"
#include "modules/rtp_rtcp/source/rtp_format.h"
#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
#include "modules/rtp_rtcp/source/rtp_packet_received.h"
#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
#include "modules/video_coding/frame_object.h"
#include "modules/video_coding/h264_sprop_parameter_sets.h"
#include "modules/video_coding/h264_sps_pps_tracker.h"
Expand Down Expand Up @@ -97,9 +100,6 @@ RtpVideoStreamReceiver::RtpVideoStreamReceiver(
process_thread_(process_thread),
ntp_estimator_(clock_),
rtp_header_extensions_(config_.rtp.extensions),
rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
this,
&rtp_payload_registry_)),
rtp_receive_statistics_(rtp_receive_statistics),
ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc, this)),
receiving_(false),
Expand Down Expand Up @@ -168,26 +168,25 @@ RtpVideoStreamReceiver::~RtpVideoStreamReceiver() {
UpdateHistograms();
}

bool RtpVideoStreamReceiver::AddReceiveCodec(
void RtpVideoStreamReceiver::AddReceiveCodec(
const VideoCodec& video_codec,
const std::map<std::string, std::string>& codec_params) {
pt_codec_params_.insert(make_pair(video_codec.plType, codec_params));
return rtp_payload_registry_.RegisterReceivePayload(video_codec) == 0;
pt_codec_type_.emplace(video_codec.plType, video_codec.codecType);
pt_codec_params_.emplace(video_codec.plType, codec_params);
}

absl::optional<Syncable::Info> RtpVideoStreamReceiver::GetSyncInfo() const {
Syncable::Info info;

if (!rtp_receiver_->GetLatestTimestamps(
&info.latest_received_capture_timestamp,
&info.latest_receive_time_ms)) {
if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) {
return absl::nullopt;
}
Syncable::Info info;
if (rtp_rtcp_->RemoteNTP(&info.capture_time_ntp_secs,
&info.capture_time_ntp_frac, nullptr, nullptr,
&info.capture_time_source_clock) != 0) {
return absl::nullopt;
}
info.latest_received_capture_timestamp = *last_received_rtp_timestamp_;
info.latest_receive_time_ms = *last_received_rtp_system_time_ms_;

// Leaves info.current_delay_ms uninitialized.
return info;
Expand Down Expand Up @@ -244,12 +243,20 @@ void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
RtpPacketReceived packet;
if (!packet.Parse(rtp_packet, rtp_packet_length))
return;
if (packet.PayloadType() == config_.rtp.red_payload_type) {
RTC_LOG(LS_WARNING) << "Discarding recovered packet with RED encapsulation";
return;
}

packet.IdentifyExtensions(rtp_header_extensions_);
packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
// TODO(nisse): UlpfecReceiverImpl::ProcessReceivedFec passes both
// original (decapsulated) media packets and recovered packets to
// this callback. We need a way to distinguish, for setting
// packet.recovered() correctly. Ideally, move RED decapsulation out
// of the Ulpfec implementation.

RTPHeader header;
packet.GetHeader(&header);
ReceivePacket(rtp_packet, rtp_packet_length, header);
ReceivePacket(packet);
}

// This method handles both regular RTP packets and packets recovered
Expand All @@ -263,6 +270,9 @@ void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {

if (!packet.recovered()) {
int64_t now_ms = clock_->TimeInMilliseconds();
// TODO(nisse): Exclude out-of-order packets?
last_received_rtp_timestamp_ = packet.Timestamp();
last_received_rtp_system_time_ms_ = now_ms;

// Periodically log the RTP header of incoming packets.
if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
Expand All @@ -285,18 +295,14 @@ void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
}
}

// TODO(nisse): Delete use of GetHeader, but needs refactoring of
// ReceivePacket and IncomingPacket methods below.
RTPHeader header;
packet.GetHeader(&header);

header.payload_type_frequency = kVideoPayloadTypeFrequency;
ReceivePacket(packet);

ReceivePacket(packet.data(), packet.size(), header);
// Update receive statistics after ReceivePacket.
// Receive statistics will be reset if the payload type changes (make sure
// that the first packet is included in the stats).
if (!packet.recovered()) {
RTPHeader header;
packet.GetHeader(&header);
// TODO(nisse): We should pass a recovered flag to stats, to aid
// fixing bug bugs.webrtc.org/6339.
rtp_receive_statistics_->IncomingPacket(header, packet.size(),
Expand Down Expand Up @@ -388,22 +394,63 @@ void RtpVideoStreamReceiver::RemoveSecondarySink(
secondary_sinks_.erase(it);
}

void RtpVideoStreamReceiver::ReceivePacket(const uint8_t* packet,
size_t packet_length,
const RTPHeader& header) {
if (header.payloadType == config_.rtp.red_payload_type) {
ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
void RtpVideoStreamReceiver::ReceivePacket(const RtpPacketReceived& packet) {
if (packet.payload_size() == 0) {
// Keep-alive packet.
// TODO(nisse): Could drop empty packets earlier, but need to figure out how
// they should be counted in stats.
return;
}
if (packet.PayloadType() == config_.rtp.red_payload_type) {
RTPHeader header;
packet.GetHeader(&header);
ParseAndHandleEncapsulatingHeader(packet.data(), packet.size(), header);
return;
}

const auto codec_type_it = pt_codec_type_.find(packet.PayloadType());
if (codec_type_it == pt_codec_type_.end()) {
return;
}
auto depacketizer =
absl::WrapUnique(RtpDepacketizer::Create(codec_type_it->second));

if (!depacketizer) {
RTC_LOG(LS_ERROR) << "Failed to create depacketizer.";
return;
}
const uint8_t* payload = packet + header.headerLength;
assert(packet_length >= header.headerLength);
size_t payload_length = packet_length - header.headerLength;
const auto pl =
rtp_payload_registry_.PayloadTypeToPayload(header.payloadType);
if (pl) {
rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
pl->typeSpecific);
RtpDepacketizer::ParsedPayload parsed_payload;
if (!depacketizer->Parse(&parsed_payload, packet.payload().data(),
packet.payload().size())) {
RTC_LOG(LS_WARNING) << "Failed parsing payload.";
return;
}

WebRtcRTPHeader webrtc_rtp_header = {};
packet.GetHeader(&webrtc_rtp_header.header);

webrtc_rtp_header.frameType = parsed_payload.frame_type;
webrtc_rtp_header.video_header() = parsed_payload.video_header();
webrtc_rtp_header.video_header().rotation = kVideoRotation_0;
webrtc_rtp_header.video_header().content_type = VideoContentType::UNSPECIFIED;
webrtc_rtp_header.video_header().video_timing.flags =
VideoSendTiming::kInvalid;
webrtc_rtp_header.video_header().playout_delay.min_ms = -1;
webrtc_rtp_header.video_header().playout_delay.max_ms = -1;

// Retrieve the video rotation information.
packet.GetExtension<VideoOrientation>(
&webrtc_rtp_header.video_header().rotation);

packet.GetExtension<VideoContentTypeExtension>(
&webrtc_rtp_header.video_header().content_type);
packet.GetExtension<VideoTimingExtension>(
&webrtc_rtp_header.video_header().video_timing);
packet.GetExtension<PlayoutDelayLimits>(
&webrtc_rtp_header.video_header().playout_delay);

OnReceivedPayloadData(parsed_payload.payload, parsed_payload.payload_length,
&webrtc_rtp_header);
}

void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader(
Expand Down Expand Up @@ -456,7 +503,7 @@ bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);

int64_t rtt = 0;
rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
rtp_rtcp_->RTT(config_.rtp.remote_ssrc, &rtt, nullptr, nullptr, nullptr);
if (rtt == 0) {
// Waiting for valid rtt.
return true;
Expand Down
20 changes: 12 additions & 8 deletions video/rtp_video_stream_receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
#include <string>
#include <vector>

#include "absl/types/optional.h"

#include "api/video_codecs/video_codec.h"
#include "call/rtp_packet_sink_interface.h"
#include "call/syncable.h"
#include "call/video_receive_stream.h"
#include "modules/include/module_common_types.h"
#include "modules/rtp_rtcp/include/receive_statistics.h"
#include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
#include "modules/rtp_rtcp/include/rtp_rtcp.h"
#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
#include "modules/video_coding/h264_sps_pps_tracker.h"
Expand Down Expand Up @@ -74,7 +76,7 @@ class RtpVideoStreamReceiver : public RtpData,
video_coding::OnCompleteFrameCallback* complete_frame_callback);
~RtpVideoStreamReceiver();

bool AddReceiveCodec(const VideoCodec& video_codec,
void AddReceiveCodec(const VideoCodec& video_codec,
const std::map<std::string, std::string>& codec_params);

void StartReceive();
Expand Down Expand Up @@ -138,10 +140,10 @@ class RtpVideoStreamReceiver : public RtpData,
void RemoveSecondarySink(const RtpPacketSinkInterface* sink);

private:
void ReceivePacket(const uint8_t* packet,
size_t packet_length,
const RTPHeader& header);
// Parses and handles for instance RTX and RED headers.
// Entry point doing non-stats work for a received packet. Called
// for the same packet both before and after RED decapsulation.
void ReceivePacket(const RtpPacketReceived& packet);
// Parses and handles RED headers.
// This function assumes that it's being called from only one thread.
void ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
size_t packet_length,
Expand All @@ -160,10 +162,8 @@ class RtpVideoStreamReceiver : public RtpData,
ProcessThread* const process_thread_;

RemoteNtpTimeEstimator ntp_estimator_;
RTPPayloadRegistry rtp_payload_registry_;

RtpHeaderExtensionMap rtp_header_extensions_;
const std::unique_ptr<RtpReceiver> rtp_receiver_;
ReceiveStatistics* const rtp_receive_statistics_;
std::unique_ptr<UlpfecReceiver> ulpfec_receiver_;

Expand All @@ -183,6 +183,10 @@ class RtpVideoStreamReceiver : public RtpData,
std::map<int64_t, uint16_t> last_seq_num_for_pic_id_
RTC_GUARDED_BY(last_seq_num_cs_);
video_coding::H264SpsPpsTracker tracker_;

absl::optional<uint32_t> last_received_rtp_timestamp_;
absl::optional<int64_t> last_received_rtp_system_time_ms_;
std::map<uint8_t, VideoCodecType> pt_codec_type_;
// TODO(johan): Remove pt_codec_params_ once
// https://bugs.chromium.org/p/webrtc/issues/detail?id=6883 is resolved.
// Maps a payload type to a map of out-of-band supplied codec parameters.
Expand Down
4 changes: 1 addition & 3 deletions video/video_receive_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "common_video/h264/profile_level_id.h"
#include "common_video/include/incoming_video_stream.h"
#include "common_video/libyuv/include/webrtc_libyuv.h"
#include "modules/rtp_rtcp/include/rtp_receiver.h"
#include "modules/rtp_rtcp/include/rtp_rtcp.h"
#include "modules/utility/include/process_thread.h"
#include "modules/video_coding/frame_object.h"
Expand Down Expand Up @@ -203,8 +202,7 @@ void VideoReceiveStream::Start() {
video_receiver_.RegisterExternalDecoder(decoder.decoder,
decoder.payload_type);
VideoCodec codec = CreateDecoderVideoCodec(decoder);
RTC_CHECK(rtp_video_stream_receiver_.AddReceiveCodec(codec,
decoder.codec_params));
rtp_video_stream_receiver_.AddReceiveCodec(codec, decoder.codec_params);
RTC_CHECK_EQ(VCM_OK, video_receiver_.RegisterReceiveCodec(
&codec, num_cpu_cores_, false));
}
Expand Down

0 comments on commit 2ff1f2a

Please sign in to comment.