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-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-08-12 14:28:00 +00:00
|
|
|
shutting_down_(false),
|
2013-08-12 12:59:04 +00:00
|
|
|
receiver_(NULL) {
|
|
|
|
|
unsigned int thread_id;
|
|
|
|
|
EXPECT_TRUE(thread_->Start(thread_id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirectTransport::~DirectTransport() { StopSending(); }
|
|
|
|
|
|
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-08-12 12:59:04 +00:00
|
|
|
receiver_ = receiver;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DirectTransport::SendRTP(const uint8_t* data, size_t length) {
|
|
|
|
|
QueuePacket(data, length);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DirectTransport::SendRTCP(const uint8_t* data, size_t length) {
|
|
|
|
|
QueuePacket(data, length);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirectTransport::Packet::Packet() : length(0) {}
|
|
|
|
|
|
|
|
|
|
DirectTransport::Packet::Packet(const uint8_t* data, size_t length)
|
|
|
|
|
: length(length) {
|
|
|
|
|
EXPECT_LE(length, sizeof(this->data));
|
|
|
|
|
memcpy(this->data, data, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DirectTransport::QueuePacket(const uint8_t* data, size_t length) {
|
|
|
|
|
CriticalSectionScoped crit(lock_.get());
|
|
|
|
|
EXPECT_TRUE(receiver_ != NULL);
|
|
|
|
|
packet_queue_.push_back(Packet(data, length));
|
|
|
|
|
packet_event_->Set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DirectTransport::NetworkProcess(void* transport) {
|
|
|
|
|
return static_cast<DirectTransport*>(transport)->SendPackets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DirectTransport::SendPackets() {
|
|
|
|
|
while (true) {
|
|
|
|
|
Packet p;
|
|
|
|
|
{
|
2013-08-12 14:28:00 +00:00
|
|
|
CriticalSectionScoped crit(lock_.get());
|
2013-08-12 12:59:04 +00:00
|
|
|
if (packet_queue_.empty())
|
|
|
|
|
break;
|
|
|
|
|
p = packet_queue_.front();
|
|
|
|
|
packet_queue_.pop_front();
|
|
|
|
|
}
|
|
|
|
|
receiver_->DeliverPacket(p.data, p.length);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-12 14:28:00 +00:00
|
|
|
switch (packet_event_->Wait(WEBRTC_EVENT_INFINITE)) {
|
2013-08-12 12:59:04 +00:00
|
|
|
case kEventSignaled:
|
|
|
|
|
packet_event_->Reset();
|
|
|
|
|
break;
|
|
|
|
|
case kEventTimeout:
|
|
|
|
|
break;
|
|
|
|
|
case kEventError:
|
|
|
|
|
// TODO(pbos): Log a warning here?
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
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
|