OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 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 #include "webrtc/test/direct_transport.h" | 10 #include "webrtc/test/direct_transport.h" |
11 | 11 |
12 #include "webrtc/call/call.h" | 12 #include "webrtc/call/call.h" |
| 13 #include "webrtc/rtc_base/ptr_util.h" |
13 #include "webrtc/system_wrappers/include/clock.h" | 14 #include "webrtc/system_wrappers/include/clock.h" |
| 15 #include "webrtc/test/single_threaded_task_queue.h" |
14 | 16 |
15 namespace webrtc { | 17 namespace webrtc { |
16 namespace test { | 18 namespace test { |
17 | 19 |
18 DirectTransport::DirectTransport( | 20 DirectTransport::DirectTransport( |
19 Call* send_call, | 21 Call* send_call, |
20 const std::map<uint8_t, MediaType>& payload_type_map) | 22 const std::map<uint8_t, MediaType>& payload_type_map) |
21 : DirectTransport(FakeNetworkPipe::Config(), send_call, payload_type_map) {} | 23 : DirectTransport(FakeNetworkPipe::Config(), send_call, payload_type_map) {} |
22 | 24 |
23 DirectTransport::DirectTransport( | 25 DirectTransport::DirectTransport( |
24 const FakeNetworkPipe::Config& config, | 26 const FakeNetworkPipe::Config& config, |
25 Call* send_call, | 27 Call* send_call, |
26 const std::map<uint8_t, MediaType>& payload_type_map) | 28 const std::map<uint8_t, MediaType>& payload_type_map) |
27 : DirectTransport( | 29 : DirectTransport( |
28 config, | 30 config, |
29 send_call, | 31 send_call, |
30 std::unique_ptr<Demuxer>(new DemuxerImpl(payload_type_map))) {} | 32 std::unique_ptr<Demuxer>(new DemuxerImpl(payload_type_map))) {} |
31 | 33 |
32 DirectTransport::DirectTransport(const FakeNetworkPipe::Config& config, | 34 DirectTransport::DirectTransport(const FakeNetworkPipe::Config& config, |
33 Call* send_call, | 35 Call* send_call, |
34 std::unique_ptr<Demuxer> demuxer) | 36 std::unique_ptr<Demuxer> demuxer) |
| 37 : DirectTransport(nullptr, config, send_call, std::move(demuxer)) {} |
| 38 |
| 39 DirectTransport::DirectTransport( |
| 40 SingleThreadedTaskQueueForTesting* task_queue, |
| 41 Call* send_call, |
| 42 const std::map<uint8_t, MediaType>& payload_type_map) |
| 43 : DirectTransport(task_queue, |
| 44 FakeNetworkPipe::Config(), |
| 45 send_call, |
| 46 payload_type_map) { |
| 47 } |
| 48 |
| 49 DirectTransport::DirectTransport( |
| 50 SingleThreadedTaskQueueForTesting* task_queue, |
| 51 const FakeNetworkPipe::Config& config, |
| 52 Call* send_call, |
| 53 const std::map<uint8_t, MediaType>& payload_type_map) |
| 54 : DirectTransport( |
| 55 task_queue, |
| 56 config, |
| 57 send_call, |
| 58 std::unique_ptr<Demuxer>(new DemuxerImpl(payload_type_map))) { |
| 59 } |
| 60 |
| 61 DirectTransport::DirectTransport(SingleThreadedTaskQueueForTesting* task_queue, |
| 62 const FakeNetworkPipe::Config& config, |
| 63 Call* send_call, |
| 64 std::unique_ptr<Demuxer> demuxer) |
35 : send_call_(send_call), | 65 : send_call_(send_call), |
36 packet_event_(false, false), | |
37 thread_(NetworkProcess, this, "NetworkProcess"), | |
38 clock_(Clock::GetRealTimeClock()), | 66 clock_(Clock::GetRealTimeClock()), |
39 shutting_down_(false), | 67 task_queue_(task_queue), |
40 fake_network_(clock_, config, std::move(demuxer)) { | 68 fake_network_(clock_, config, std::move(demuxer)) { |
41 thread_.Start(); | 69 // TODO(eladalon): When the deprecated ctors are removed, this check |
| 70 // can be restored. https://bugs.chromium.org/p/webrtc/issues/detail?id=8125 |
| 71 // RTC_DCHECK(task_queue); |
| 72 if (!task_queue) { |
| 73 deprecated_task_queue_ = |
| 74 rtc::MakeUnique<SingleThreadedTaskQueueForTesting>("deprecated_queue"); |
| 75 task_queue_ = deprecated_task_queue_.get(); |
| 76 } |
| 77 |
42 if (send_call_) { | 78 if (send_call_) { |
43 send_call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp); | 79 send_call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp); |
44 send_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp); | 80 send_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp); |
45 } | 81 } |
| 82 SendPackets(); |
46 } | 83 } |
47 | 84 |
48 DirectTransport::~DirectTransport() { StopSending(); } | 85 DirectTransport::~DirectTransport() { |
| 86 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 87 // Constructor updates |next_scheduled_task_|, so it's guaranteed to |
| 88 // be initialized. |
| 89 task_queue_->CancelTask(next_scheduled_task_); |
| 90 } |
49 | 91 |
50 void DirectTransport::SetConfig(const FakeNetworkPipe::Config& config) { | 92 void DirectTransport::SetConfig(const FakeNetworkPipe::Config& config) { |
51 fake_network_.SetConfig(config); | 93 fake_network_.SetConfig(config); |
52 } | 94 } |
53 | 95 |
54 void DirectTransport::StopSending() { | 96 void DirectTransport::StopSending() { |
55 { | 97 task_queue_->CancelTask(next_scheduled_task_); |
56 rtc::CritScope crit(&lock_); | |
57 shutting_down_ = true; | |
58 } | |
59 | |
60 packet_event_.Set(); | |
61 thread_.Stop(); | |
62 } | 98 } |
63 | 99 |
64 void DirectTransport::SetReceiver(PacketReceiver* receiver) { | 100 void DirectTransport::SetReceiver(PacketReceiver* receiver) { |
| 101 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
65 fake_network_.SetReceiver(receiver); | 102 fake_network_.SetReceiver(receiver); |
66 } | 103 } |
67 | 104 |
68 bool DirectTransport::SendRtp(const uint8_t* data, | 105 bool DirectTransport::SendRtp(const uint8_t* data, |
69 size_t length, | 106 size_t length, |
70 const PacketOptions& options) { | 107 const PacketOptions& options) { |
71 if (send_call_) { | 108 if (send_call_) { |
72 rtc::SentPacket sent_packet(options.packet_id, | 109 rtc::SentPacket sent_packet(options.packet_id, |
73 clock_->TimeInMilliseconds()); | 110 clock_->TimeInMilliseconds()); |
74 send_call_->OnSentPacket(sent_packet); | 111 send_call_->OnSentPacket(sent_packet); |
75 } | 112 } |
76 fake_network_.SendPacket(data, length); | 113 fake_network_.SendPacket(data, length); |
77 packet_event_.Set(); | |
78 return true; | 114 return true; |
79 } | 115 } |
80 | 116 |
81 bool DirectTransport::SendRtcp(const uint8_t* data, size_t length) { | 117 bool DirectTransport::SendRtcp(const uint8_t* data, size_t length) { |
82 fake_network_.SendPacket(data, length); | 118 fake_network_.SendPacket(data, length); |
83 packet_event_.Set(); | |
84 return true; | 119 return true; |
85 } | 120 } |
86 | 121 |
87 int DirectTransport::GetAverageDelayMs() { | 122 int DirectTransport::GetAverageDelayMs() { |
88 return fake_network_.AverageDelay(); | 123 return fake_network_.AverageDelay(); |
89 } | 124 } |
90 | 125 |
91 DirectTransport::ForceDemuxer::ForceDemuxer(MediaType media_type) | 126 DirectTransport::ForceDemuxer::ForceDemuxer(MediaType media_type) |
92 : media_type_(media_type) {} | 127 : media_type_(media_type) {} |
93 | 128 |
94 void DirectTransport::ForceDemuxer::SetReceiver(PacketReceiver* receiver) { | 129 void DirectTransport::ForceDemuxer::SetReceiver(PacketReceiver* receiver) { |
95 packet_receiver_ = receiver; | 130 packet_receiver_ = receiver; |
96 } | 131 } |
97 | 132 |
98 void DirectTransport::ForceDemuxer::DeliverPacket( | 133 void DirectTransport::ForceDemuxer::DeliverPacket( |
99 const NetworkPacket* packet, | 134 const NetworkPacket* packet, |
100 const PacketTime& packet_time) { | 135 const PacketTime& packet_time) { |
101 if (!packet_receiver_) | 136 if (!packet_receiver_) |
102 return; | 137 return; |
103 packet_receiver_->DeliverPacket(media_type_, packet->data(), | 138 packet_receiver_->DeliverPacket(media_type_, packet->data(), |
104 packet->data_length(), packet_time); | 139 packet->data_length(), packet_time); |
105 } | 140 } |
106 | 141 |
107 bool DirectTransport::NetworkProcess(void* transport) { | 142 void DirectTransport::SendPackets() { |
108 return static_cast<DirectTransport*>(transport)->SendPackets(); | 143 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
109 } | |
110 | 144 |
111 bool DirectTransport::SendPackets() { | |
112 fake_network_.Process(); | 145 fake_network_.Process(); |
113 int64_t wait_time_ms = fake_network_.TimeUntilNextProcess(); | 146 |
114 if (wait_time_ms > 0) { | 147 int64_t delay_ms = fake_network_.TimeUntilNextProcess(); |
115 packet_event_.Wait(static_cast<int>(wait_time_ms)); | 148 next_scheduled_task_ = task_queue_->PostDelayedTask([this]() { |
116 } | 149 SendPackets(); |
117 rtc::CritScope crit(&lock_); | 150 }, delay_ms); |
118 return shutting_down_ ? false : true; | |
119 } | 151 } |
120 } // namespace test | 152 } // namespace test |
121 } // namespace webrtc | 153 } // namespace webrtc |
OLD | NEW |