8013833dff44fd50e6e6f5f92e90f01978c6ee04
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / video / loopback.cc
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
11 #include <stdio.h>
12
13 #include <map>
14
15 #include "gflags/gflags.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 #include "webrtc/call.h"
19 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
20 #include "webrtc/system_wrappers/interface/clock.h"
21 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
22 #include "webrtc/test/direct_transport.h"
23 #include "webrtc/test/encoder_settings.h"
24 #include "webrtc/test/fake_encoder.h"
25 #include "webrtc/test/field_trial.h"
26 #include "webrtc/test/run_loop.h"
27 #include "webrtc/test/run_test.h"
28 #include "webrtc/test/testsupport/trace_to_stderr.h"
29 #include "webrtc/test/video_capturer.h"
30 #include "webrtc/test/video_renderer.h"
31 #include "webrtc/typedefs.h"
32
33 namespace webrtc {
34
35 static const int kAbsSendTimeExtensionId = 7;
36
37 namespace flags {
38
39 DEFINE_int32(width, 640, "Video width.");
40 size_t Width() { return static_cast<size_t>(FLAGS_width); }
41
42 DEFINE_int32(height, 480, "Video height.");
43 size_t Height() { return static_cast<size_t>(FLAGS_height); }
44
45 DEFINE_int32(fps, 30, "Frames per second.");
46 int Fps() { return static_cast<int>(FLAGS_fps); }
47
48 DEFINE_int32(min_bitrate, 50, "Minimum video bitrate.");
49 size_t MinBitrate() { return static_cast<size_t>(FLAGS_min_bitrate); }
50
51 DEFINE_int32(start_bitrate, 300, "Video starting bitrate.");
52 size_t StartBitrate() { return static_cast<size_t>(FLAGS_start_bitrate); }
53
54 DEFINE_int32(max_bitrate, 800, "Maximum video bitrate.");
55 size_t MaxBitrate() { return static_cast<size_t>(FLAGS_max_bitrate); }
56
57 DEFINE_string(codec, "VP8", "Video codec to use.");
58 std::string Codec() { return static_cast<std::string>(FLAGS_codec); }
59
60 DEFINE_int32(loss_percent, 0, "Percentage of packets randomly lost.");
61 int LossPercent() {
62   return static_cast<int>(FLAGS_loss_percent);
63 }
64
65 DEFINE_int32(link_capacity,
66              0,
67              "Capacity (kbps) of the fake link. 0 means infinite.");
68 int LinkCapacity() {
69   return static_cast<int>(FLAGS_link_capacity);
70 }
71
72 DEFINE_int32(queue_size, 0, "Size of the bottleneck link queue in packets.");
73 int QueueSize() {
74   return static_cast<int>(FLAGS_queue_size);
75 }
76
77 DEFINE_int32(avg_propagation_delay_ms,
78              0,
79              "Average link propagation delay in ms.");
80 int AvgPropagationDelayMs() {
81   return static_cast<int>(FLAGS_avg_propagation_delay_ms);
82 }
83
84 DEFINE_int32(std_propagation_delay_ms,
85              0,
86              "Link propagation delay standard deviation in ms.");
87 int StdPropagationDelayMs() {
88   return static_cast<int>(FLAGS_std_propagation_delay_ms);
89 }
90
91 DEFINE_bool(logs, false, "print logs to stderr");
92
93 DEFINE_string(
94     force_fieldtrials,
95     "",
96     "Field trials control experimental feature code which can be forced. "
97     "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
98     " will assign the group Enable to field trial WebRTC-FooFeature. Multiple "
99     "trials are separated by \"/\"");
100 }  // namespace flags
101
102 static const uint32_t kSendSsrc = 0x654321;
103 static const uint32_t kSendRtxSsrc = 0x654322;
104 static const uint32_t kReceiverLocalSsrc = 0x123456;
105
106 static const uint8_t kRtxPayloadType = 96;
107
108 void Loopback() {
109   scoped_ptr<test::TraceToStderr> trace_to_stderr_;
110   if (webrtc::flags::FLAGS_logs)
111     trace_to_stderr_.reset(new test::TraceToStderr);
112
113   scoped_ptr<test::VideoRenderer> local_preview(test::VideoRenderer::Create(
114       "Local Preview", flags::Width(), flags::Height()));
115   scoped_ptr<test::VideoRenderer> loopback_video(test::VideoRenderer::Create(
116       "Loopback Video", flags::Width(), flags::Height()));
117
118   FakeNetworkPipe::Config pipe_config;
119   pipe_config.loss_percent = flags::LossPercent();
120   pipe_config.link_capacity_kbps = flags::LinkCapacity();
121   pipe_config.queue_length_packets = flags::QueueSize();
122   pipe_config.queue_delay_ms = flags::AvgPropagationDelayMs();
123   pipe_config.delay_standard_deviation_ms = flags::StdPropagationDelayMs();
124   test::DirectTransport transport(pipe_config);
125   Call::Config call_config(&transport);
126   call_config.stream_start_bitrate_bps =
127       static_cast<int>(flags::StartBitrate()) * 1000;
128   scoped_ptr<Call> call(Call::Create(call_config));
129
130   // Loopback, call sends to itself.
131   transport.SetReceiver(call->Receiver());
132
133   VideoSendStream::Config send_config;
134   send_config.rtp.ssrcs.push_back(kSendSsrc);
135   send_config.rtp.rtx.ssrcs.push_back(kSendRtxSsrc);
136   send_config.rtp.rtx.payload_type = kRtxPayloadType;
137   send_config.rtp.nack.rtp_history_ms = 1000;
138   send_config.rtp.extensions.push_back(
139       RtpExtension(RtpExtension::kAbsSendTime, kAbsSendTimeExtensionId));
140
141   send_config.local_renderer = local_preview.get();
142   scoped_ptr<VideoEncoder> encoder;
143   if (flags::Codec() == "VP8") {
144     encoder.reset(VideoEncoder::Create(VideoEncoder::kVp8));
145   } else if (flags::Codec() == "VP9") {
146     encoder.reset(VideoEncoder::Create(VideoEncoder::kVp9));
147   } else {
148     // Codec not supported.
149     assert(false && "Codec not supported!");
150     return;
151   }
152   send_config.encoder_settings.encoder = encoder.get();
153   send_config.encoder_settings.payload_name = flags::Codec();
154   send_config.encoder_settings.payload_type = 124;
155   VideoEncoderConfig encoder_config;
156   encoder_config.streams = test::CreateVideoStreams(1);
157   VideoStream* stream = &encoder_config.streams[0];
158   stream->width = flags::Width();
159   stream->height = flags::Height();
160   stream->min_bitrate_bps = static_cast<int>(flags::MinBitrate()) * 1000;
161   stream->target_bitrate_bps = static_cast<int>(flags::MaxBitrate()) * 1000;
162   stream->max_bitrate_bps = static_cast<int>(flags::MaxBitrate()) * 1000;
163   stream->max_framerate = 30;
164   stream->max_qp = 56;
165
166   VideoSendStream* send_stream =
167       call->CreateVideoSendStream(send_config, encoder_config);
168
169   Clock* test_clock = Clock::GetRealTimeClock();
170
171   scoped_ptr<test::VideoCapturer> camera(
172       test::VideoCapturer::Create(send_stream->Input(),
173                                   flags::Width(),
174                                   flags::Height(),
175                                   flags::Fps(),
176                                   test_clock));
177
178   VideoReceiveStream::Config receive_config;
179   receive_config.rtp.remote_ssrc = send_config.rtp.ssrcs[0];
180   receive_config.rtp.local_ssrc = kReceiverLocalSsrc;
181   receive_config.rtp.nack.rtp_history_ms = 1000;
182   receive_config.rtp.rtx[kRtxPayloadType].ssrc = kSendRtxSsrc;
183   receive_config.rtp.rtx[kRtxPayloadType].payload_type = kRtxPayloadType;
184   receive_config.rtp.extensions.push_back(
185       RtpExtension(RtpExtension::kAbsSendTime, kAbsSendTimeExtensionId));
186   receive_config.renderer = loopback_video.get();
187   VideoReceiveStream::Decoder decoder =
188       test::CreateMatchingDecoder(send_config.encoder_settings);
189   receive_config.decoders.push_back(decoder);
190
191   VideoReceiveStream* receive_stream =
192       call->CreateVideoReceiveStream(receive_config);
193
194   receive_stream->Start();
195   send_stream->Start();
196   camera->Start();
197
198   test::PressEnterToContinue();
199
200   camera->Stop();
201   send_stream->Stop();
202   receive_stream->Stop();
203
204   call->DestroyVideoReceiveStream(receive_stream);
205   call->DestroyVideoSendStream(send_stream);
206
207   delete decoder.decoder;
208
209   transport.StopSending();
210 }
211 }  // namespace webrtc
212
213 int main(int argc, char* argv[]) {
214   ::testing::InitGoogleTest(&argc, argv);
215   google::ParseCommandLineFlags(&argc, &argv, true);
216   webrtc::test::InitFieldTrialsFromString(
217       webrtc::flags::FLAGS_force_fieldtrials);
218   webrtc::test::RunTest(webrtc::Loopback);
219   return 0;
220 }