1 /*
2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/modules/include/module_common_types.h"
12 #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
13 #include "webrtc/system_wrappers/include/atomic32.h"
14 #include "webrtc/system_wrappers/include/sleep.h"
15 #include "webrtc/voice_engine/test/auto_test/fixtures/before_streaming_fixture.h"
16 
17 using ::testing::_;
18 using ::testing::AtLeast;
19 using ::testing::Eq;
20 using ::testing::Field;
21 
22 class ExtensionVerifyTransport : public webrtc::Transport {
23  public:
ExtensionVerifyTransport()24   ExtensionVerifyTransport()
25       : parser_(webrtc::RtpHeaderParser::Create()),
26         received_packets_(0),
27         bad_packets_(0),
28         audio_level_id_(-1),
29         absolute_sender_time_id_(-1) {}
30 
SendRtp(const uint8_t * data,size_t len,const webrtc::PacketOptions & options)31   bool SendRtp(const uint8_t* data,
32                size_t len,
33                const webrtc::PacketOptions& options) override {
34     webrtc::RTPHeader header;
35     if (parser_->Parse(reinterpret_cast<const uint8_t*>(data), len, &header)) {
36       bool ok = true;
37       if (audio_level_id_ >= 0 &&
38           !header.extension.hasAudioLevel) {
39         ok = false;
40       }
41       if (absolute_sender_time_id_ >= 0 &&
42           !header.extension.hasAbsoluteSendTime) {
43         ok = false;
44       }
45       if (!ok) {
46         // bad_packets_ count packets we expected to have an extension but
47         // didn't have one.
48         ++bad_packets_;
49       }
50     }
51     // received_packets_ count all packets we receive.
52     ++received_packets_;
53     return true;
54   }
55 
SendRtcp(const uint8_t * data,size_t len)56   bool SendRtcp(const uint8_t* data, size_t len) override {
57     return true;
58   }
59 
SetAudioLevelId(int id)60   void SetAudioLevelId(int id) {
61     audio_level_id_ = id;
62     parser_->RegisterRtpHeaderExtension(webrtc::kRtpExtensionAudioLevel, id);
63   }
64 
SetAbsoluteSenderTimeId(int id)65   void SetAbsoluteSenderTimeId(int id) {
66     absolute_sender_time_id_ = id;
67     parser_->RegisterRtpHeaderExtension(webrtc::kRtpExtensionAbsoluteSendTime,
68                                         id);
69   }
70 
Wait()71   bool Wait() {
72     // Wait until we've received to specified number of packets.
73     while (received_packets_.Value() < kPacketsExpected) {
74       webrtc::SleepMs(kSleepIntervalMs);
75     }
76     // Check whether any were 'bad' (didn't contain an extension when they
77     // where supposed to).
78     return bad_packets_.Value() == 0;
79   }
80 
81  private:
82   enum {
83     kPacketsExpected = 10,
84     kSleepIntervalMs = 10
85   };
86   rtc::scoped_ptr<webrtc::RtpHeaderParser> parser_;
87   webrtc::Atomic32 received_packets_;
88   webrtc::Atomic32 bad_packets_;
89   int audio_level_id_;
90   int absolute_sender_time_id_;
91 };
92 
93 class SendRtpRtcpHeaderExtensionsTest : public BeforeStreamingFixture {
94  protected:
SetUp()95   void SetUp() override {
96     EXPECT_EQ(0, voe_network_->DeRegisterExternalTransport(channel_));
97     EXPECT_EQ(0, voe_network_->RegisterExternalTransport(channel_,
98                                                          verifying_transport_));
99   }
TearDown()100   void TearDown() override { PausePlaying(); }
101 
102   ExtensionVerifyTransport verifying_transport_;
103 };
104 
TEST_F(SendRtpRtcpHeaderExtensionsTest,SentPacketsIncludeNoAudioLevel)105 TEST_F(SendRtpRtcpHeaderExtensionsTest, SentPacketsIncludeNoAudioLevel) {
106   verifying_transport_.SetAudioLevelId(0);
107   ResumePlaying();
108   EXPECT_FALSE(verifying_transport_.Wait());
109 }
110 
TEST_F(SendRtpRtcpHeaderExtensionsTest,SentPacketsIncludeAudioLevel)111 TEST_F(SendRtpRtcpHeaderExtensionsTest, SentPacketsIncludeAudioLevel) {
112   EXPECT_EQ(0, voe_rtp_rtcp_->SetSendAudioLevelIndicationStatus(channel_, true,
113                                                                 9));
114   verifying_transport_.SetAudioLevelId(9);
115   ResumePlaying();
116   EXPECT_TRUE(verifying_transport_.Wait());
117 }
118 
TEST_F(SendRtpRtcpHeaderExtensionsTest,SentPacketsIncludeNoAbsoluteSenderTime)119 TEST_F(SendRtpRtcpHeaderExtensionsTest, SentPacketsIncludeNoAbsoluteSenderTime)
120 {
121   verifying_transport_.SetAbsoluteSenderTimeId(0);
122   ResumePlaying();
123   EXPECT_FALSE(verifying_transport_.Wait());
124 }
125 
TEST_F(SendRtpRtcpHeaderExtensionsTest,SentPacketsIncludeAbsoluteSenderTime)126 TEST_F(SendRtpRtcpHeaderExtensionsTest, SentPacketsIncludeAbsoluteSenderTime) {
127   EXPECT_EQ(0, voe_rtp_rtcp_->SetSendAbsoluteSenderTimeStatus(channel_, true,
128                                                               11));
129   verifying_transport_.SetAbsoluteSenderTimeId(11);
130   ResumePlaying();
131   EXPECT_TRUE(verifying_transport_.Wait());
132 }
133 
TEST_F(SendRtpRtcpHeaderExtensionsTest,SentPacketsIncludeAllExtensions1)134 TEST_F(SendRtpRtcpHeaderExtensionsTest, SentPacketsIncludeAllExtensions1) {
135   EXPECT_EQ(0, voe_rtp_rtcp_->SetSendAudioLevelIndicationStatus(channel_, true,
136                                                                 9));
137   EXPECT_EQ(0, voe_rtp_rtcp_->SetSendAbsoluteSenderTimeStatus(channel_, true,
138                                                               11));
139   verifying_transport_.SetAudioLevelId(9);
140   verifying_transport_.SetAbsoluteSenderTimeId(11);
141   ResumePlaying();
142   EXPECT_TRUE(verifying_transport_.Wait());
143 }
144 
TEST_F(SendRtpRtcpHeaderExtensionsTest,SentPacketsIncludeAllExtensions2)145 TEST_F(SendRtpRtcpHeaderExtensionsTest, SentPacketsIncludeAllExtensions2) {
146   EXPECT_EQ(0, voe_rtp_rtcp_->SetSendAbsoluteSenderTimeStatus(channel_, true,
147                                                               3));
148   EXPECT_EQ(0, voe_rtp_rtcp_->SetSendAudioLevelIndicationStatus(channel_, true,
149                                                                 9));
150   verifying_transport_.SetAbsoluteSenderTimeId(3);
151   // Don't register audio level with header parser - unknown extensions should
152   // be ignored when parsing.
153   ResumePlaying();
154   EXPECT_TRUE(verifying_transport_.Wait());
155 }
156