3b4ad7b14565f47ef3cf2ab810842ecc9bf2e52a
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / test / rtp_rtcp_observer.h
1 /*
2  *  Copyright (c) 2013 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 #ifndef WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_
11 #define WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_
12
13 #include <map>
14 #include <vector>
15
16 #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
17 #include "webrtc/typedefs.h"
18 #include "webrtc/video_send_stream.h"
19
20 namespace webrtc {
21 namespace test {
22
23 class RtpRtcpObserver {
24  public:
25   virtual ~RtpRtcpObserver() {}
26   newapi::Transport* SendTransport() {
27     return &send_transport_;
28   }
29
30   newapi::Transport* ReceiveTransport() {
31     return &receive_transport_;
32   }
33
34   void SetReceivers(PacketReceiver* send_transport_receiver,
35                     PacketReceiver* receive_transport_receiver) {
36     send_transport_.SetReceiver(send_transport_receiver);
37     receive_transport_.SetReceiver(receive_transport_receiver);
38   }
39
40   void StopSending() {
41     send_transport_.StopSending();
42     receive_transport_.StopSending();
43   }
44
45   virtual EventTypeWrapper Wait() {
46     EventTypeWrapper result = observation_complete_->Wait(timeout_ms_);
47     observation_complete_->Reset();
48     return result;
49   }
50
51  protected:
52   RtpRtcpObserver(unsigned int event_timeout_ms,
53       const FakeNetworkPipe::Config& configuration)
54       : lock_(CriticalSectionWrapper::CreateCriticalSection()),
55         observation_complete_(EventWrapper::Create()),
56         parser_(RtpHeaderParser::Create()),
57         send_transport_(lock_.get(),
58                         this,
59                         &RtpRtcpObserver::OnSendRtp,
60                         &RtpRtcpObserver::OnSendRtcp,
61                         configuration),
62         receive_transport_(lock_.get(),
63                            this,
64                            &RtpRtcpObserver::OnReceiveRtp,
65                            &RtpRtcpObserver::OnReceiveRtcp,
66                            configuration),
67         timeout_ms_(event_timeout_ms) {}
68
69   explicit RtpRtcpObserver(unsigned int event_timeout_ms)
70       : lock_(CriticalSectionWrapper::CreateCriticalSection()),
71         observation_complete_(EventWrapper::Create()),
72         parser_(RtpHeaderParser::Create()),
73         send_transport_(lock_.get(),
74                         this,
75                         &RtpRtcpObserver::OnSendRtp,
76                         &RtpRtcpObserver::OnSendRtcp,
77                         FakeNetworkPipe::Config()),
78         receive_transport_(lock_.get(),
79                            this,
80                            &RtpRtcpObserver::OnReceiveRtp,
81                            &RtpRtcpObserver::OnReceiveRtcp,
82                            FakeNetworkPipe::Config()),
83         timeout_ms_(event_timeout_ms) {}
84
85   enum Action {
86     SEND_PACKET,
87     DROP_PACKET,
88   };
89
90   virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
91     return SEND_PACKET;
92   }
93
94   virtual Action OnSendRtcp(const uint8_t* packet, size_t length) {
95     return SEND_PACKET;
96   }
97
98   virtual Action OnReceiveRtp(const uint8_t* packet, size_t length) {
99     return SEND_PACKET;
100   }
101
102   virtual Action OnReceiveRtcp(const uint8_t* packet, size_t length) {
103     return SEND_PACKET;
104   }
105
106
107  private:
108   class PacketTransport : public test::DirectTransport {
109    public:
110     typedef Action (RtpRtcpObserver::*PacketTransportAction)(const uint8_t*,
111                                                              size_t);
112
113     PacketTransport(CriticalSectionWrapper* lock,
114                     RtpRtcpObserver* observer,
115                     PacketTransportAction on_rtp,
116                     PacketTransportAction on_rtcp,
117                     const FakeNetworkPipe::Config& configuration)
118         : test::DirectTransport(configuration),
119           lock_(lock),
120           observer_(observer),
121           on_rtp_(on_rtp),
122           on_rtcp_(on_rtcp) {}
123
124   private:
125     virtual bool SendRtp(const uint8_t* packet, size_t length) OVERRIDE {
126       Action action;
127       {
128         CriticalSectionScoped crit_(lock_);
129         action = (observer_->*on_rtp_)(packet, length);
130       }
131       switch (action) {
132         case DROP_PACKET:
133           // Drop packet silently.
134           return true;
135         case SEND_PACKET:
136           return test::DirectTransport::SendRtp(packet, length);
137       }
138       return true;  // Will never happen, makes compiler happy.
139     }
140
141     virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
142       Action action;
143       {
144         CriticalSectionScoped crit_(lock_);
145         action = (observer_->*on_rtcp_)(packet, length);
146       }
147       switch (action) {
148         case DROP_PACKET:
149           // Drop packet silently.
150           return true;
151         case SEND_PACKET:
152           return test::DirectTransport::SendRtcp(packet, length);
153       }
154       return true;  // Will never happen, makes compiler happy.
155     }
156
157     // Pointer to shared lock instance protecting on_rtp_/on_rtcp_ calls.
158     CriticalSectionWrapper* lock_;
159
160     RtpRtcpObserver* observer_;
161     PacketTransportAction on_rtp_, on_rtcp_;
162   };
163
164  protected:
165   scoped_ptr<CriticalSectionWrapper> lock_;
166   scoped_ptr<EventWrapper> observation_complete_;
167   scoped_ptr<RtpHeaderParser> parser_;
168
169  private:
170   PacketTransport send_transport_, receive_transport_;
171   unsigned int timeout_ms_;
172 };
173 }  // namespace test
174 }  // namespace webrtc
175
176 #endif  // WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_