• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2018 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 // This file contains tests for |RtpTransceiver|.
12 
13 #include "pc/rtp_transceiver.h"
14 
15 #include <memory>
16 
17 #include "media/base/fake_media_engine.h"
18 #include "pc/test/mock_channel_interface.h"
19 #include "pc/test/mock_rtp_receiver_internal.h"
20 #include "pc/test/mock_rtp_sender_internal.h"
21 #include "test/gmock.h"
22 #include "test/gtest.h"
23 
24 using ::testing::ElementsAre;
25 using ::testing::Eq;
26 using ::testing::Field;
27 using ::testing::Not;
28 using ::testing::Property;
29 using ::testing::Return;
30 using ::testing::ReturnRef;
31 
32 namespace webrtc {
33 
34 // Checks that a channel cannot be set on a stopped |RtpTransceiver|.
TEST(RtpTransceiverTest,CannotSetChannelOnStoppedTransceiver)35 TEST(RtpTransceiverTest, CannotSetChannelOnStoppedTransceiver) {
36   RtpTransceiver transceiver(cricket::MediaType::MEDIA_TYPE_AUDIO);
37   cricket::MockChannelInterface channel1;
38   sigslot::signal1<cricket::ChannelInterface*> signal;
39   EXPECT_CALL(channel1, media_type())
40       .WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_AUDIO));
41   EXPECT_CALL(channel1, SignalFirstPacketReceived())
42       .WillRepeatedly(ReturnRef(signal));
43 
44   transceiver.SetChannel(&channel1);
45   EXPECT_EQ(&channel1, transceiver.channel());
46 
47   // Stop the transceiver.
48   transceiver.Stop();
49   EXPECT_EQ(&channel1, transceiver.channel());
50 
51   cricket::MockChannelInterface channel2;
52   EXPECT_CALL(channel2, media_type())
53       .WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_AUDIO));
54 
55   // Channel can no longer be set, so this call should be a no-op.
56   transceiver.SetChannel(&channel2);
57   EXPECT_EQ(&channel1, transceiver.channel());
58 }
59 
60 // Checks that a channel can be unset on a stopped |RtpTransceiver|
TEST(RtpTransceiverTest,CanUnsetChannelOnStoppedTransceiver)61 TEST(RtpTransceiverTest, CanUnsetChannelOnStoppedTransceiver) {
62   RtpTransceiver transceiver(cricket::MediaType::MEDIA_TYPE_VIDEO);
63   cricket::MockChannelInterface channel;
64   sigslot::signal1<cricket::ChannelInterface*> signal;
65   EXPECT_CALL(channel, media_type())
66       .WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_VIDEO));
67   EXPECT_CALL(channel, SignalFirstPacketReceived())
68       .WillRepeatedly(ReturnRef(signal));
69 
70   transceiver.SetChannel(&channel);
71   EXPECT_EQ(&channel, transceiver.channel());
72 
73   // Stop the transceiver.
74   transceiver.Stop();
75   EXPECT_EQ(&channel, transceiver.channel());
76 
77   // Set the channel to |nullptr|.
78   transceiver.SetChannel(nullptr);
79   EXPECT_EQ(nullptr, transceiver.channel());
80 }
81 
82 class RtpTransceiverTestForHeaderExtensions : public ::testing::Test {
83  public:
RtpTransceiverTestForHeaderExtensions()84   RtpTransceiverTestForHeaderExtensions()
85       : channel_manager_(std::make_unique<cricket::FakeMediaEngine>(),
86                          std::make_unique<cricket::FakeDataEngine>(),
87                          rtc::Thread::Current(),
88                          rtc::Thread::Current()),
89         extensions_(
90             {RtpHeaderExtensionCapability("uri1",
91                                           1,
92                                           RtpTransceiverDirection::kSendOnly),
93              RtpHeaderExtensionCapability("uri2",
94                                           2,
95                                           RtpTransceiverDirection::kRecvOnly),
96              RtpHeaderExtensionCapability(RtpExtension::kMidUri,
97                                           3,
98                                           RtpTransceiverDirection::kSendRecv),
99              RtpHeaderExtensionCapability(RtpExtension::kVideoRotationUri,
100                                           4,
101                                           RtpTransceiverDirection::kSendRecv)}),
102         transceiver_(RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
103                          rtc::Thread::Current(),
104                          new rtc::RefCountedObject<MockRtpSenderInternal>()),
105                      RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
106                          rtc::Thread::Current(),
107                          new rtc::RefCountedObject<MockRtpReceiverInternal>()),
108                      &channel_manager_,
109                      extensions_) {}
110 
111   cricket::ChannelManager channel_manager_;
112   std::vector<RtpHeaderExtensionCapability> extensions_;
113   RtpTransceiver transceiver_;
114 };
115 
TEST_F(RtpTransceiverTestForHeaderExtensions,OffersChannelManagerList)116 TEST_F(RtpTransceiverTestForHeaderExtensions, OffersChannelManagerList) {
117   EXPECT_EQ(transceiver_.HeaderExtensionsToOffer(), extensions_);
118 }
119 
TEST_F(RtpTransceiverTestForHeaderExtensions,ModifiesDirection)120 TEST_F(RtpTransceiverTestForHeaderExtensions, ModifiesDirection) {
121   auto modified_extensions = extensions_;
122   modified_extensions[0].direction = RtpTransceiverDirection::kSendOnly;
123   EXPECT_TRUE(
124       transceiver_.SetOfferedRtpHeaderExtensions(modified_extensions).ok());
125   EXPECT_EQ(transceiver_.HeaderExtensionsToOffer(), modified_extensions);
126   modified_extensions[0].direction = RtpTransceiverDirection::kRecvOnly;
127   EXPECT_TRUE(
128       transceiver_.SetOfferedRtpHeaderExtensions(modified_extensions).ok());
129   EXPECT_EQ(transceiver_.HeaderExtensionsToOffer(), modified_extensions);
130   modified_extensions[0].direction = RtpTransceiverDirection::kSendRecv;
131   EXPECT_TRUE(
132       transceiver_.SetOfferedRtpHeaderExtensions(modified_extensions).ok());
133   EXPECT_EQ(transceiver_.HeaderExtensionsToOffer(), modified_extensions);
134   modified_extensions[0].direction = RtpTransceiverDirection::kInactive;
135   EXPECT_TRUE(
136       transceiver_.SetOfferedRtpHeaderExtensions(modified_extensions).ok());
137   EXPECT_EQ(transceiver_.HeaderExtensionsToOffer(), modified_extensions);
138 }
139 
TEST_F(RtpTransceiverTestForHeaderExtensions,AcceptsStoppedExtension)140 TEST_F(RtpTransceiverTestForHeaderExtensions, AcceptsStoppedExtension) {
141   auto modified_extensions = extensions_;
142   modified_extensions[0].direction = RtpTransceiverDirection::kStopped;
143   EXPECT_TRUE(
144       transceiver_.SetOfferedRtpHeaderExtensions(modified_extensions).ok());
145   EXPECT_EQ(transceiver_.HeaderExtensionsToOffer(), modified_extensions);
146 }
147 
TEST_F(RtpTransceiverTestForHeaderExtensions,RejectsUnsupportedExtension)148 TEST_F(RtpTransceiverTestForHeaderExtensions, RejectsUnsupportedExtension) {
149   std::vector<RtpHeaderExtensionCapability> modified_extensions(
150       {RtpHeaderExtensionCapability("uri3", 1,
151                                     RtpTransceiverDirection::kSendRecv)});
152   EXPECT_THAT(transceiver_.SetOfferedRtpHeaderExtensions(modified_extensions),
153               Property(&RTCError::type, RTCErrorType::INVALID_PARAMETER));
154   EXPECT_EQ(transceiver_.HeaderExtensionsToOffer(), extensions_);
155 }
156 
TEST_F(RtpTransceiverTestForHeaderExtensions,RejectsStoppedMandatoryExtensions)157 TEST_F(RtpTransceiverTestForHeaderExtensions,
158        RejectsStoppedMandatoryExtensions) {
159   std::vector<RtpHeaderExtensionCapability> modified_extensions = extensions_;
160   // Attempting to stop the mandatory MID extension.
161   modified_extensions[2].direction = RtpTransceiverDirection::kStopped;
162   EXPECT_THAT(transceiver_.SetOfferedRtpHeaderExtensions(modified_extensions),
163               Property(&RTCError::type, RTCErrorType::INVALID_MODIFICATION));
164   EXPECT_EQ(transceiver_.HeaderExtensionsToOffer(), extensions_);
165   modified_extensions = extensions_;
166   // Attempting to stop the mandatory video orientation extension.
167   modified_extensions[3].direction = RtpTransceiverDirection::kStopped;
168   EXPECT_THAT(transceiver_.SetOfferedRtpHeaderExtensions(modified_extensions),
169               Property(&RTCError::type, RTCErrorType::INVALID_MODIFICATION));
170   EXPECT_EQ(transceiver_.HeaderExtensionsToOffer(), extensions_);
171 }
172 
173 }  // namespace webrtc
174