Danil Chapovalov | c0fd5f9 | 2017-11-16 14:35:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 "modules/rtp_rtcp/source/rtcp_transceiver.h" |
| 12 | |
| 13 | #include "rtc_base/event.h" |
| 14 | #include "rtc_base/ptr_util.h" |
| 15 | #include "test/gmock.h" |
| 16 | #include "test/gtest.h" |
| 17 | #include "test/mock_transport.h" |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | using ::testing::AtLeast; |
| 22 | using ::testing::InvokeWithoutArgs; |
| 23 | using ::testing::NiceMock; |
| 24 | using ::testing::_; |
| 25 | using ::webrtc::MockTransport; |
| 26 | using ::webrtc::RtcpTransceiver; |
| 27 | using ::webrtc::RtcpTransceiverConfig; |
| 28 | |
| 29 | void WaitPostedTasks(rtc::TaskQueue* queue) { |
| 30 | rtc::Event done(false, false); |
| 31 | queue->PostTask([&done] { done.Set(); }); |
| 32 | ASSERT_TRUE(done.Wait(1000)); |
| 33 | } |
| 34 | |
| 35 | TEST(RtcpTransceiverTest, SendsRtcpOnTaskQueueWhenCreatedOffTaskQueue) { |
| 36 | rtc::TaskQueue queue("rtcp"); |
| 37 | MockTransport outgoing_transport; |
| 38 | RtcpTransceiverConfig config; |
| 39 | config.outgoing_transport = &outgoing_transport; |
| 40 | config.task_queue = &queue; |
| 41 | EXPECT_CALL(outgoing_transport, SendRtcp(_, _)) |
| 42 | .WillRepeatedly(InvokeWithoutArgs([&] { |
| 43 | EXPECT_TRUE(queue.IsCurrent()); |
| 44 | return true; |
| 45 | })); |
| 46 | |
| 47 | RtcpTransceiver rtcp_transceiver(config); |
| 48 | rtcp_transceiver.SendCompoundPacket(); |
| 49 | WaitPostedTasks(&queue); |
| 50 | } |
| 51 | |
| 52 | TEST(RtcpTransceiverTest, SendsRtcpOnTaskQueueWhenCreatedOnTaskQueue) { |
| 53 | rtc::TaskQueue queue("rtcp"); |
| 54 | MockTransport outgoing_transport; |
| 55 | RtcpTransceiverConfig config; |
| 56 | config.outgoing_transport = &outgoing_transport; |
| 57 | config.task_queue = &queue; |
| 58 | EXPECT_CALL(outgoing_transport, SendRtcp(_, _)) |
| 59 | .WillRepeatedly(InvokeWithoutArgs([&] { |
| 60 | EXPECT_TRUE(queue.IsCurrent()); |
| 61 | return true; |
| 62 | })); |
| 63 | |
| 64 | std::unique_ptr<RtcpTransceiver> rtcp_transceiver; |
| 65 | queue.PostTask([&] { |
| 66 | rtcp_transceiver = rtc::MakeUnique<RtcpTransceiver>(config); |
| 67 | rtcp_transceiver->SendCompoundPacket(); |
| 68 | }); |
| 69 | WaitPostedTasks(&queue); |
| 70 | } |
| 71 | |
| 72 | TEST(RtcpTransceiverTest, CanBeDestoryedOnTaskQueue) { |
| 73 | rtc::TaskQueue queue("rtcp"); |
| 74 | NiceMock<MockTransport> outgoing_transport; |
| 75 | RtcpTransceiverConfig config; |
| 76 | config.outgoing_transport = &outgoing_transport; |
| 77 | config.task_queue = &queue; |
| 78 | auto rtcp_transceiver = rtc::MakeUnique<RtcpTransceiver>(config); |
| 79 | |
| 80 | queue.PostTask([&] { rtcp_transceiver.reset(); }); |
| 81 | WaitPostedTasks(&queue); |
| 82 | } |
| 83 | |
| 84 | TEST(RtcpTransceiverTest, CanCallSendCompoundPacketFromAnyThread) { |
| 85 | MockTransport outgoing_transport; |
| 86 | rtc::TaskQueue queue("rtcp"); |
| 87 | RtcpTransceiverConfig config; |
| 88 | config.outgoing_transport = &outgoing_transport; |
| 89 | config.task_queue = &queue; |
| 90 | |
| 91 | EXPECT_CALL(outgoing_transport, SendRtcp(_, _)) |
| 92 | // If test is slow, a periodic task may send an extra packet. |
| 93 | .Times(AtLeast(3)) |
| 94 | .WillRepeatedly(InvokeWithoutArgs([&] { |
| 95 | EXPECT_TRUE(queue.IsCurrent()); |
| 96 | return true; |
| 97 | })); |
| 98 | |
| 99 | RtcpTransceiver rtcp_transceiver(config); |
| 100 | |
| 101 | // Call from the construction thread. |
| 102 | rtcp_transceiver.SendCompoundPacket(); |
| 103 | // Call from the same queue transceiver use for processing. |
| 104 | queue.PostTask([&] { rtcp_transceiver.SendCompoundPacket(); }); |
| 105 | // Call from unrelated task queue. |
| 106 | rtc::TaskQueue queue_send("send_packet"); |
| 107 | queue_send.PostTask([&] { rtcp_transceiver.SendCompoundPacket(); }); |
| 108 | |
| 109 | WaitPostedTasks(&queue_send); |
| 110 | WaitPostedTasks(&queue); |
| 111 | } |
| 112 | |
| 113 | TEST(RtcpTransceiverTest, DoesntSendPacketsAfterDestruction) { |
| 114 | MockTransport outgoing_transport; |
| 115 | rtc::TaskQueue queue("rtcp"); |
| 116 | RtcpTransceiverConfig config; |
| 117 | config.outgoing_transport = &outgoing_transport; |
| 118 | config.task_queue = &queue; |
| 119 | config.schedule_periodic_compound_packets = false; |
| 120 | |
| 121 | EXPECT_CALL(outgoing_transport, SendRtcp(_, _)).Times(0); |
| 122 | |
| 123 | auto rtcp_transceiver = rtc::MakeUnique<RtcpTransceiver>(config); |
| 124 | rtc::Event pause(false, false); |
| 125 | queue.PostTask([&] { |
| 126 | pause.Wait(rtc::Event::kForever); |
| 127 | rtcp_transceiver.reset(); |
| 128 | }); |
| 129 | rtcp_transceiver->SendCompoundPacket(); |
| 130 | pause.Set(); |
| 131 | WaitPostedTasks(&queue); |
| 132 | EXPECT_FALSE(rtcp_transceiver); |
| 133 | } |
| 134 | |
| 135 | } // namespace |