2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef SRC_VOICE_ENGINE_MAIN_TEST_AUTO_TEST_STANDARD_TEST_BASE_AFTER_INIT_H_
12 #define SRC_VOICE_ENGINE_MAIN_TEST_AUTO_TEST_STANDARD_TEST_BASE_AFTER_INIT_H_
16 #include "webrtc/common_types.h"
17 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
18 #include "webrtc/system_wrappers/interface/event_wrapper.h"
19 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
20 #include "webrtc/system_wrappers/interface/thread_wrapper.h"
21 #include "webrtc/voice_engine/test/auto_test/fixtures/before_initialization_fixture.h"
23 class TestErrorObserver;
25 class LoopBackTransport : public webrtc::Transport {
27 LoopBackTransport(webrtc::VoENetwork* voe_network)
28 : crit_(webrtc::CriticalSectionWrapper::CreateCriticalSection()),
29 packet_event_(webrtc::EventWrapper::Create()),
30 thread_(webrtc::ThreadWrapper::CreateThread(NetworkProcess, this)),
31 voe_network_(voe_network) {
36 ~LoopBackTransport() { thread_->Stop(); }
38 virtual int SendPacket(int channel, const void* data, int len) {
39 StorePacket(Packet::Rtp, channel, data, len);
43 virtual int SendRTCPPacket(int channel, const void* data, int len) {
44 StorePacket(Packet::Rtcp, channel, data, len);
50 enum Type { Rtp, Rtcp, } type;
53 Packet(Type type, int channel, const void* data, int len)
54 : type(type), channel(channel), len(len) {
56 memcpy(this->data, data, static_cast<size_t>(len));
64 void StorePacket(Packet::Type type, int channel, const void* data, int len) {
65 webrtc::CriticalSectionScoped lock(crit_.get());
66 packet_queue_.push_back(Packet(type, channel, data, len));
70 static bool NetworkProcess(void* transport) {
71 return static_cast<LoopBackTransport*>(transport)->SendPackets();
75 switch (packet_event_->Wait(10)) {
76 case webrtc::kEventSignaled:
77 packet_event_->Reset();
79 case webrtc::kEventTimeout:
81 case webrtc::kEventError:
82 // TODO(pbos): Log a warning here?
89 webrtc::CriticalSectionScoped lock(crit_.get());
90 if (packet_queue_.empty())
92 p = packet_queue_.front();
93 packet_queue_.pop_front();
98 voe_network_->ReceivedRTPPacket(p.channel, p.data, p.len,
99 webrtc::PacketTime());
102 voe_network_->ReceivedRTCPPacket(p.channel, p.data, p.len);
109 webrtc::scoped_ptr<webrtc::CriticalSectionWrapper> crit_;
110 webrtc::scoped_ptr<webrtc::EventWrapper> packet_event_;
111 webrtc::scoped_ptr<webrtc::ThreadWrapper> thread_;
112 std::deque<Packet> packet_queue_ GUARDED_BY(crit_.get());
113 webrtc::VoENetwork* const voe_network_;
116 // This fixture initializes the voice engine in addition to the work
117 // done by the before-initialization fixture. It also registers an error
118 // observer which will fail tests on error callbacks. This fixture is
119 // useful to tests that want to run before we have started any form of
120 // streaming through the voice engine.
121 class AfterInitializationFixture : public BeforeInitializationFixture {
123 AfterInitializationFixture();
124 virtual ~AfterInitializationFixture();
127 webrtc::scoped_ptr<TestErrorObserver> error_observer_;
130 #endif // SRC_VOICE_ENGINE_MAIN_TEST_AUTO_TEST_STANDARD_TEST_BASE_AFTER_INIT_H_