2013-08-12 12:59:04 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
2013-10-28 16:32:01 +00:00
|
|
|
#include "webrtc/test/direct_transport.h"
|
2013-08-12 12:59:04 +00:00
|
|
|
|
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
2013-10-28 16:32:01 +00:00
|
|
|
|
|
|
|
|
#include "webrtc/call.h"
|
2013-11-18 11:45:11 +00:00
|
|
|
#include "webrtc/system_wrappers/interface/clock.h"
|
2013-08-12 12:59:04 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
DirectTransport::DirectTransport()
|
|
|
|
|
: lock_(CriticalSectionWrapper::CreateCriticalSection()),
|
|
|
|
|
packet_event_(EventWrapper::Create()),
|
|
|
|
|
thread_(ThreadWrapper::CreateThread(NetworkProcess, this)),
|
2013-11-18 11:45:11 +00:00
|
|
|
clock_(Clock::GetRealTimeClock()),
|
2013-08-12 14:28:00 +00:00
|
|
|
shutting_down_(false),
|
2013-12-18 20:28:25 +00:00
|
|
|
fake_network_(FakeNetworkPipe::Config()) {
|
2013-11-18 11:45:11 +00:00
|
|
|
unsigned int thread_id;
|
|
|
|
|
EXPECT_TRUE(thread_->Start(thread_id));
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-18 20:28:25 +00:00
|
|
|
DirectTransport::DirectTransport(
|
|
|
|
|
const FakeNetworkPipe::Config& config)
|
2013-11-18 11:45:11 +00:00
|
|
|
: lock_(CriticalSectionWrapper::CreateCriticalSection()),
|
|
|
|
|
packet_event_(EventWrapper::Create()),
|
|
|
|
|
thread_(ThreadWrapper::CreateThread(NetworkProcess, this)),
|
|
|
|
|
clock_(Clock::GetRealTimeClock()),
|
|
|
|
|
shutting_down_(false),
|
2013-12-18 20:28:25 +00:00
|
|
|
fake_network_(config) {
|
2013-08-12 12:59:04 +00:00
|
|
|
unsigned int thread_id;
|
|
|
|
|
EXPECT_TRUE(thread_->Start(thread_id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirectTransport::~DirectTransport() { StopSending(); }
|
|
|
|
|
|
2014-02-26 13:34:52 +00:00
|
|
|
void DirectTransport::SetConfig(const FakeNetworkPipe::Config& config) {
|
|
|
|
|
fake_network_.SetConfig(config);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-12 14:28:00 +00:00
|
|
|
void DirectTransport::StopSending() {
|
|
|
|
|
{
|
|
|
|
|
CriticalSectionScoped crit_(lock_.get());
|
|
|
|
|
shutting_down_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
packet_event_->Set();
|
|
|
|
|
EXPECT_TRUE(thread_->Stop());
|
|
|
|
|
}
|
2013-08-12 12:59:04 +00:00
|
|
|
|
2013-08-23 09:19:30 +00:00
|
|
|
void DirectTransport::SetReceiver(PacketReceiver* receiver) {
|
2013-12-18 20:28:25 +00:00
|
|
|
fake_network_.SetReceiver(receiver);
|
2013-08-12 12:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-20 12:17:04 +00:00
|
|
|
bool DirectTransport::SendRtp(const uint8_t* data, size_t length) {
|
2013-12-18 20:28:25 +00:00
|
|
|
fake_network_.SendPacket(data, length);
|
|
|
|
|
packet_event_->Set();
|
2013-08-12 12:59:04 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-20 12:17:04 +00:00
|
|
|
bool DirectTransport::SendRtcp(const uint8_t* data, size_t length) {
|
2013-12-18 20:28:25 +00:00
|
|
|
fake_network_.SendPacket(data, length);
|
2013-08-12 12:59:04 +00:00
|
|
|
packet_event_->Set();
|
2013-12-18 20:28:25 +00:00
|
|
|
return true;
|
2013-08-12 12:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DirectTransport::NetworkProcess(void* transport) {
|
|
|
|
|
return static_cast<DirectTransport*>(transport)->SendPackets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DirectTransport::SendPackets() {
|
2013-12-18 20:28:25 +00:00
|
|
|
fake_network_.Process();
|
|
|
|
|
int wait_time_ms = fake_network_.TimeUntilNextProcess();
|
|
|
|
|
if (wait_time_ms > 0) {
|
|
|
|
|
switch (packet_event_->Wait(wait_time_ms)) {
|
|
|
|
|
case kEventSignaled:
|
|
|
|
|
packet_event_->Reset();
|
2013-08-12 12:59:04 +00:00
|
|
|
break;
|
2013-12-18 20:28:25 +00:00
|
|
|
case kEventTimeout:
|
2013-11-18 11:45:11 +00:00
|
|
|
break;
|
2013-12-18 20:28:25 +00:00
|
|
|
case kEventError:
|
|
|
|
|
// TODO(pbos): Log a warning here?
|
|
|
|
|
return true;
|
2013-11-18 11:45:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-08-12 14:28:00 +00:00
|
|
|
CriticalSectionScoped crit(lock_.get());
|
|
|
|
|
return shutting_down_ ? false : true;
|
2013-08-12 12:59:04 +00:00
|
|
|
}
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace webrtc
|