OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/checks.h" | 11 #include "webrtc/base/checks.h" |
12 #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h" | |
13 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" | 12 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" |
14 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | |
15 #include "webrtc/modules/rtp_rtcp/source/rtp_format.h" | 13 #include "webrtc/modules/rtp_rtcp/source/rtp_format.h" |
16 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" | 14 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" |
17 #include "webrtc/test/layer_filtering_transport.h" | 15 #include "webrtc/test/layer_filtering_transport.h" |
18 | 16 |
19 namespace webrtc { | 17 namespace webrtc { |
20 namespace test { | 18 namespace test { |
21 | 19 |
22 LayerFilteringTransport::LayerFilteringTransport( | 20 LayerFilteringTransport::LayerFilteringTransport( |
23 const FakeNetworkPipe::Config& config, | 21 const FakeNetworkPipe::Config& config, |
24 Call* send_call, | 22 Call* send_call, |
25 uint8_t vp8_video_payload_type, | 23 uint8_t vp8_video_payload_type, |
26 uint8_t vp9_video_payload_type, | 24 uint8_t vp9_video_payload_type, |
27 uint8_t tl_discard_threshold, | 25 int selected_tl, |
28 uint8_t sl_discard_threshold) | 26 int selected_sl) |
29 : test::DirectTransport(config, send_call), | 27 : test::DirectTransport(config, send_call), |
30 vp8_video_payload_type_(vp8_video_payload_type), | 28 vp8_video_payload_type_(vp8_video_payload_type), |
31 vp9_video_payload_type_(vp9_video_payload_type), | 29 vp9_video_payload_type_(vp9_video_payload_type), |
32 tl_discard_threshold_(tl_discard_threshold), | 30 selected_tl_(selected_tl), |
33 sl_discard_threshold_(sl_discard_threshold) {} | 31 selected_sl_(selected_sl), |
| 32 discarded_last_packet_(false) {} |
34 | 33 |
35 uint16_t LayerFilteringTransport::NextSequenceNumber(uint32_t ssrc) { | 34 bool LayerFilteringTransport::DiscardedLastPacket() const { |
36 auto it = current_seq_nums_.find(ssrc); | 35 return discarded_last_packet_; |
37 if (it == current_seq_nums_.end()) | |
38 return current_seq_nums_[ssrc] = 10000; | |
39 return ++it->second; | |
40 } | 36 } |
41 | 37 |
42 bool LayerFilteringTransport::SendRtp(const uint8_t* packet, | 38 bool LayerFilteringTransport::SendRtp(const uint8_t* packet, |
43 size_t length, | 39 size_t length, |
44 const PacketOptions& options) { | 40 const PacketOptions& options) { |
45 if (tl_discard_threshold_ == 0 && sl_discard_threshold_ == 0) { | 41 if (selected_tl_ == -1 && selected_sl_ == -1) { |
46 // Nothing to change, forward the packet immediately. | 42 // Nothing to change, forward the packet immediately. |
47 return test::DirectTransport::SendRtp(packet, length, options); | 43 return test::DirectTransport::SendRtp(packet, length, options); |
48 } | 44 } |
49 | 45 |
50 bool set_marker_bit = false; | 46 bool set_marker_bit = false; |
51 rtc::scoped_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create()); | 47 RtpUtility::RtpHeaderParser parser(packet, length); |
52 RTPHeader header; | 48 RTPHeader header; |
53 parser->Parse(packet, length, &header); | 49 parser.Parse(header); |
| 50 |
| 51 RTC_DCHECK_LE(length, static_cast<size_t>(IP_PACKET_SIZE)); |
| 52 uint8_t temp_buffer[IP_PACKET_SIZE]; |
| 53 memcpy(temp_buffer, packet, length); |
54 | 54 |
55 if (header.payloadType == vp8_video_payload_type_ || | 55 if (header.payloadType == vp8_video_payload_type_ || |
56 header.payloadType == vp9_video_payload_type_) { | 56 header.payloadType == vp9_video_payload_type_) { |
57 const uint8_t* payload = packet + header.headerLength; | 57 const uint8_t* payload = packet + header.headerLength; |
58 RTC_DCHECK_GT(length, header.headerLength); | 58 RTC_DCHECK_GT(length, header.headerLength); |
59 const size_t payload_length = length - header.headerLength; | 59 const size_t payload_length = length - header.headerLength; |
60 RTC_DCHECK_GT(payload_length, header.paddingLength); | 60 RTC_DCHECK_GT(payload_length, header.paddingLength); |
61 const size_t payload_data_length = payload_length - header.paddingLength; | 61 const size_t payload_data_length = payload_length - header.paddingLength; |
62 | 62 |
63 const bool is_vp8 = header.payloadType == vp8_video_payload_type_; | 63 const bool is_vp8 = header.payloadType == vp8_video_payload_type_; |
64 rtc::scoped_ptr<RtpDepacketizer> depacketizer( | 64 rtc::scoped_ptr<RtpDepacketizer> depacketizer( |
65 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9)); | 65 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9)); |
66 RtpDepacketizer::ParsedPayload parsed_payload; | 66 RtpDepacketizer::ParsedPayload parsed_payload; |
67 if (depacketizer->Parse(&parsed_payload, payload, payload_data_length)) { | 67 if (depacketizer->Parse(&parsed_payload, payload, payload_data_length)) { |
68 const uint8_t temporalIdx = | 68 const int temporal_idx = static_cast<int>( |
69 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx | 69 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx |
70 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx; | 70 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx); |
71 const uint8_t spatialIdx = | 71 const int spatial_idx = static_cast<int>( |
72 is_vp8 ? kNoSpatialIdx | 72 is_vp8 ? kNoSpatialIdx |
73 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx; | 73 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx); |
74 if (sl_discard_threshold_ > 0 && | 74 if (selected_sl_ >= 0 && spatial_idx == selected_sl_ && |
75 spatialIdx == sl_discard_threshold_ - 1 && | |
76 parsed_payload.type.Video.codecHeader.VP9.end_of_frame) { | 75 parsed_payload.type.Video.codecHeader.VP9.end_of_frame) { |
77 // This layer is now the last in the superframe. | 76 // This layer is now the last in the superframe. |
78 set_marker_bit = true; | 77 set_marker_bit = true; |
79 } | 78 } else if ((selected_tl_ >= 0 && temporal_idx != kNoTemporalIdx && |
80 if ((tl_discard_threshold_ > 0 && temporalIdx != kNoTemporalIdx && | 79 temporal_idx > selected_tl_) || |
81 temporalIdx >= tl_discard_threshold_) || | 80 (selected_sl_ >= 0 && spatial_idx != kNoSpatialIdx && |
82 (sl_discard_threshold_ > 0 && spatialIdx != kNoSpatialIdx && | 81 spatial_idx > selected_sl_)) { |
83 spatialIdx >= sl_discard_threshold_)) { | 82 // Truncate packet to a padding packet. |
84 return true; // Discard the packet. | 83 length = header.headerLength + 1; |
| 84 temp_buffer[0] |= (1 << 5); // P = 1. |
| 85 temp_buffer[1] &= 0x7F; // M = 0. |
| 86 discarded_last_packet_ = true; |
| 87 temp_buffer[header.headerLength] = 1; // One byte of padding. |
85 } | 88 } |
86 } else { | 89 } else { |
87 RTC_NOTREACHED() << "Parse error"; | 90 RTC_NOTREACHED() << "Parse error"; |
88 } | 91 } |
89 } | 92 } |
90 | 93 |
91 uint8_t temp_buffer[IP_PACKET_SIZE]; | |
92 memcpy(temp_buffer, packet, length); | |
93 | |
94 // We are discarding some of the packets (specifically, whole layers), so | 94 // We are discarding some of the packets (specifically, whole layers), so |
95 // make sure the marker bit is set properly, and that sequence numbers are | 95 // make sure the marker bit is set properly, and that sequence numbers are |
96 // continuous. | 96 // continuous. |
97 if (set_marker_bit) | 97 if (set_marker_bit) |
98 temp_buffer[1] |= kRtpMarkerBitMask; | 98 temp_buffer[1] |= kRtpMarkerBitMask; |
99 | 99 |
100 uint16_t seq_num = NextSequenceNumber(header.ssrc); | |
101 ByteWriter<uint16_t>::WriteBigEndian(&temp_buffer[2], seq_num); | |
102 return test::DirectTransport::SendRtp(temp_buffer, length, options); | 100 return test::DirectTransport::SendRtp(temp_buffer, length, options); |
103 } | 101 } |
104 | 102 |
105 } // namespace test | 103 } // namespace test |
106 } // namespace webrtc | 104 } // namespace webrtc |
OLD | NEW |