after_initialization_fixture.h revision 064b32acbb9ae0152918d6770ff98d2ba60d20e4
1/* 2 * Copyright (c) 2011 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#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_ 13 14#include <deque> 15 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" 22 23class TestErrorObserver; 24 25class LoopBackTransport : public webrtc::Transport { 26 public: 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) { 32 unsigned int id; 33 thread_->Start(id); 34 } 35 36 ~LoopBackTransport() { thread_->Stop(); } 37 38 virtual int SendPacket(int channel, const void* data, int len) { 39 StorePacket(Packet::Rtp, channel, data, len); 40 return len; 41 } 42 43 virtual int SendRTCPPacket(int channel, const void* data, int len) { 44 StorePacket(Packet::Rtcp, channel, data, len); 45 return len; 46 } 47 48 private: 49 struct Packet { 50 enum Type { Rtp, Rtcp, } type; 51 52 Packet() : len(0) {} 53 Packet(Type type, int channel, const void* data, int len) 54 : type(type), channel(channel), len(len) { 55 assert(len <= 1500); 56 memcpy(this->data, data, static_cast<size_t>(len)); 57 } 58 59 int channel; 60 uint8_t data[1500]; 61 int len; 62 }; 63 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)); 67 packet_event_->Set(); 68 } 69 70 static bool NetworkProcess(void* transport) { 71 return static_cast<LoopBackTransport*>(transport)->SendPackets(); 72 } 73 74 bool SendPackets() { 75 switch (packet_event_->Wait(10)) { 76 case webrtc::kEventSignaled: 77 packet_event_->Reset(); 78 break; 79 case webrtc::kEventTimeout: 80 break; 81 case webrtc::kEventError: 82 // TODO(pbos): Log a warning here? 83 return true; 84 } 85 86 while (true) { 87 Packet p; 88 { 89 webrtc::CriticalSectionScoped lock(crit_.get()); 90 if (packet_queue_.empty()) 91 break; 92 p = packet_queue_.front(); 93 packet_queue_.pop_front(); 94 } 95 96 switch (p.type) { 97 case Packet::Rtp: 98 voe_network_->ReceivedRTPPacket(p.channel, p.data, p.len); 99 break; 100 case Packet::Rtcp: 101 voe_network_->ReceivedRTCPPacket(p.channel, p.data, p.len); 102 break; 103 } 104 } 105 return true; 106 } 107 108 webrtc::scoped_ptr<webrtc::CriticalSectionWrapper> crit_; 109 webrtc::scoped_ptr<webrtc::EventWrapper> packet_event_; 110 webrtc::scoped_ptr<webrtc::ThreadWrapper> thread_; 111 std::deque<Packet> packet_queue_ GUARDED_BY(crit_.get()); 112 webrtc::VoENetwork* const voe_network_; 113}; 114 115// This fixture initializes the voice engine in addition to the work 116// done by the before-initialization fixture. It also registers an error 117// observer which will fail tests on error callbacks. This fixture is 118// useful to tests that want to run before we have started any form of 119// streaming through the voice engine. 120class AfterInitializationFixture : public BeforeInitializationFixture { 121 public: 122 AfterInitializationFixture(); 123 virtual ~AfterInitializationFixture(); 124 125 protected: 126 webrtc::scoped_ptr<TestErrorObserver> error_observer_; 127}; 128 129#endif // SRC_VOICE_ENGINE_MAIN_TEST_AUTO_TEST_STANDARD_TEST_BASE_AFTER_INIT_H_ 130