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 #include "webrtc/modules/rtp_rtcp/test/BWEStandAlone/TestSenderReceiver.h"
16 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
17 #include "webrtc/modules/rtp_rtcp/test/BWEStandAlone/TestLoadGenerator.h"
18 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
19 #include "webrtc/system_wrappers/interface/event_wrapper.h"
20 #include "webrtc/system_wrappers/interface/thread_wrapper.h"
21 #include "webrtc/system_wrappers/interface/tick_util.h"
22 #include "webrtc/test/channel_transport/udp_transport.h"
24 #define NR_OF_SOCKET_BUFFERS 500
27 bool ProcThreadFunction(void *obj)
33 TestSenderReceiver *theObj = static_cast<TestSenderReceiver *>(obj);
35 return theObj->ProcLoop();
39 TestSenderReceiver::TestSenderReceiver (void)
41 _critSect(CriticalSectionWrapper::CreateCriticalSection()),
50 _lastBytesReceived(0),
54 _rtp = RtpRtcp::CreateRtpRtcp(0, false);
57 throw "Could not create RTP/RTCP module";
61 if (_rtp->InitReceiver() != 0)
63 throw "_rtp->InitReceiver()";
67 if (_rtp->InitSender() != 0)
69 throw "_rtp->InitSender()";
73 // SocketTransport module
74 uint8_t numberOfThreads = 1;
75 _transport = UdpTransport::Create(0, numberOfThreads);
78 throw "Could not create transport module";
83 TestSenderReceiver::~TestSenderReceiver (void)
86 Stop(); // N.B. without critSect
92 RtpRtcp::DestroyRtpRtcp(_rtp);
98 UdpTransport::Destroy(_transport);
107 int32_t TestSenderReceiver::InitReceiver (const uint16_t rtpPort,
108 const uint16_t rtcpPort,
109 const int8_t payloadType /*= 127*/)
111 CriticalSectionScoped cs(_critSect);
114 if (_transport->InitializeReceiveSockets(this, rtpPort/*, 0, NULL, 0, true*/) != 0)
116 throw "_transport->InitializeReceiveSockets";
120 if (_rtp->RegisterIncomingRTPCallback(this) != 0)
122 throw "_rtp->RegisterIncomingRTPCallback";
126 if (_rtp->RegisterIncomingDataCallback(this) != 0)
128 throw "_rtp->RegisterIncomingRTPCallback";
132 if (_rtp->SetRTCPStatus(kRtcpNonCompound) != 0)
134 throw "_rtp->SetRTCPStatus";
138 if (_rtp->SetTMMBRStatus(true) != 0)
140 throw "_rtp->SetTMMBRStatus";
144 if (_rtp->RegisterReceivePayload("I420", payloadType, 90000) != 0)
146 throw "_rtp->RegisterReceivePayload";
156 int32_t TestSenderReceiver::Start()
158 CriticalSectionScoped cs(_critSect);
160 _eventPtr = EventWrapper::Create();
162 if (_rtp->SetSendingStatus(true) != 0)
164 throw "_rtp->SetSendingStatus";
168 _procThread = ThreadWrapper::CreateThread(ProcThreadFunction, this, kRealtimePriority, "TestSenderReceiver");
169 if (_procThread == NULL)
171 throw "Unable to create process thread";
179 if (_transport->StartReceiving(NR_OF_SOCKET_BUFFERS) != 0)
181 throw "_transport->StartReceiving";
187 _procThread->Start(tid);
194 int32_t TestSenderReceiver::Stop ()
196 CriticalSectionScoped cs(_critSect);
198 _transport->StopReceiving();
202 _procThread->SetNotAlive();
206 while (!_procThread->Stop())
222 bool TestSenderReceiver::ProcLoop(void)
225 // process RTP/RTCP module
228 // process SocketTransport module
229 _transport->Process();
234 // ask RTP/RTCP module for wait time
235 int32_t rtpWait = _rtp->TimeUntilNextProcess();
237 // ask SocketTransport module for wait time
238 int32_t tpWait = _transport->TimeUntilNextProcess();
240 int32_t minWait = (rtpWait < tpWait) ? rtpWait: tpWait;
241 minWait = (minWait > 0) ? minWait : 0;
243 _eventPtr->Wait(minWait);
245 // process RTP/RTCP module
248 // process SocketTransport module
249 _transport->Process();
257 int32_t TestSenderReceiver::ReceiveBitrateKbps ()
260 uint32_t packetsSent;
261 uint32_t bytesReceived;
262 uint32_t packetsReceived;
264 if (_rtp->DataCountersRTP(&bytesSent, &packetsSent, &bytesReceived, &packetsReceived) == 0)
266 int64_t now = TickTime::MillisecondTimestamp();
272 // 8 * bytes / ms = kbps
273 kbps = static_cast<int32_t>(
274 (8 * (bytesReceived - _lastBytesReceived)) / (now - _lastTime));
277 _lastBytesReceived = bytesReceived;
286 int32_t TestSenderReceiver::SetPacketTimeout(const uint32_t timeoutMS)
288 return (_rtp->SetPacketTimeout(timeoutMS, 0 /* RTCP timeout */));
292 int32_t TestSenderReceiver::OnReceivedPayloadData(const uint8_t* payloadData,
293 const uint16_t payloadSize,
294 const webrtc::WebRtcRTPHeader* rtpHeader)
296 //printf("OnReceivedPayloadData\n");
301 void TestSenderReceiver::IncomingRTPPacket(const int8_t* incomingRtpPacket,
302 const int32_t rtpPacketLength,
303 const int8_t* fromIP,
304 const uint16_t fromPort)
306 _rtp->IncomingPacket((uint8_t *) incomingRtpPacket, static_cast<uint16_t>(rtpPacketLength));
311 void TestSenderReceiver::IncomingRTCPPacket(const int8_t* incomingRtcpPacket,
312 const int32_t rtcpPacketLength,
313 const int8_t* fromIP,
314 const uint16_t fromPort)
316 _rtp->IncomingPacket((uint8_t *) incomingRtcpPacket, static_cast<uint16_t>(rtcpPacketLength));
326 int32_t TestSenderReceiver::InitSender (const uint32_t startBitrateKbps,
327 const int8_t* ipAddr,
328 const uint16_t rtpPort,
329 const uint16_t rtcpPort /*= 0*/,
330 const int8_t payloadType /*= 127*/)
332 CriticalSectionScoped cs(_critSect);
334 _payloadType = payloadType;
336 // check load generator valid
339 _loadGenerator->SetBitrate(startBitrateKbps);
342 if (_rtp->RegisterSendTransport(_transport) != 0)
344 throw "_rtp->RegisterSendTransport";
347 if (_rtp->RegisterSendPayload("I420", _payloadType, 90000) != 0)
349 throw "_rtp->RegisterSendPayload";
353 if (_rtp->RegisterIncomingVideoCallback(this) != 0)
355 throw "_rtp->RegisterIncomingVideoCallback";
359 if (_rtp->SetRTCPStatus(kRtcpNonCompound) != 0)
361 throw "_rtp->SetRTCPStatus";
365 if (_rtp->SetSendBitrate(startBitrateKbps*1000, 0, MAX_BITRATE_KBPS) != 0)
367 throw "_rtp->SetSendBitrate";
373 if (_transport->InitializeSendSockets(ipAddr, rtpPort, rtcpPort))
375 throw "_transport->InitializeSendSockets";
387 TestSenderReceiver::SendOutgoingData(const uint32_t timeStamp,
388 const uint8_t* payloadData,
389 const uint32_t payloadSize,
390 const webrtc::FrameType frameType /*= webrtc::kVideoFrameDelta*/)
392 return (_rtp->SendOutgoingData(frameType, _payloadType, timeStamp, payloadData, payloadSize));
396 int32_t TestSenderReceiver::SetLoadGenerator(TestLoadGenerator *generator)
398 CriticalSectionScoped cs(_critSect);
400 _loadGenerator = generator;
405 void TestSenderReceiver::OnNetworkChanged(const int32_t id,
406 const uint32_t minBitrateBps,
407 const uint32_t maxBitrateBps,
408 const uint8_t fractionLost,
409 const uint16_t roundTripTimeMs,
410 const uint16_t bwEstimateKbitMin,
411 const uint16_t bwEstimateKbitMax)
415 _loadGenerator->SetBitrate(maxBitrateBps/1000);
420 _sendRecCB->OnOnNetworkChanged(maxBitrateBps,