Upstream version 10.39.225.0
[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/run_loop.h"
26 #include "webrtc/test/run_test.h"
27 #include "webrtc/test/video_capturer.h"
28 #include "webrtc/test/video_renderer.h"
29 #include "webrtc/typedefs.h"
30
31 namespace webrtc {
32 namespace flags {
33
34 DEFINE_int32(width, 640, "Video width.");
35 size_t Width() { return static_cast<size_t>(FLAGS_width); }
36
37 DEFINE_int32(height, 480, "Video height.");
38 size_t Height() { return static_cast<size_t>(FLAGS_height); }
39
40 DEFINE_int32(fps, 30, "Frames per second.");
41 int Fps() { return static_cast<int>(FLAGS_fps); }
42
43 DEFINE_int32(min_bitrate, 50, "Minimum video bitrate.");
44 size_t MinBitrate() { return static_cast<size_t>(FLAGS_min_bitrate); }
45
46 DEFINE_int32(start_bitrate, 300, "Video starting bitrate.");
47 size_t StartBitrate() { return static_cast<size_t>(FLAGS_start_bitrate); }
48
49 DEFINE_int32(max_bitrate, 800, "Maximum video bitrate.");
50 size_t MaxBitrate() { return static_cast<size_t>(FLAGS_max_bitrate); }
51
52 DEFINE_string(codec, "VP8", "Video codec to use.");
53 std::string Codec() { return static_cast<std::string>(FLAGS_codec); }
54
55 DEFINE_int32(loss_percent, 0, "Percentage of packets randomly lost.");
56 int LossPercent() {
57   return static_cast<int>(FLAGS_loss_percent);
58 }
59
60 DEFINE_int32(link_capacity,
61              0,
62              "Capacity (kbps) of the fake link. 0 means infinite.");
63 int LinkCapacity() {
64   return static_cast<int>(FLAGS_link_capacity);
65 }
66
67 DEFINE_int32(queue_size, 0, "Size of the bottleneck link queue in packets.");
68 int QueueSize() {
69   return static_cast<int>(FLAGS_queue_size);
70 }
71
72 DEFINE_int32(avg_propagation_delay_ms,
73              0,
74              "Average link propagation delay in ms.");
75 int AvgPropagationDelayMs() {
76   return static_cast<int>(FLAGS_avg_propagation_delay_ms);
77 }
78
79 DEFINE_int32(std_propagation_delay_ms,
80              0,
81              "Link propagation delay standard deviation in ms.");
82 int StdPropagationDelayMs() {
83   return static_cast<int>(FLAGS_std_propagation_delay_ms);
84 }
85 }  // namespace flags
86
87 static const uint32_t kSendSsrc = 0x654321;
88 static const uint32_t kSendRtxSsrc = 0x654322;
89 static const uint32_t kReceiverLocalSsrc = 0x123456;
90
91 static const uint8_t kRtxPayloadType = 96;
92
93 void Loopback() {
94   scoped_ptr<test::VideoRenderer> local_preview(test::VideoRenderer::Create(
95       "Local Preview", flags::Width(), flags::Height()));
96   scoped_ptr<test::VideoRenderer> loopback_video(test::VideoRenderer::Create(
97       "Loopback Video", flags::Width(), flags::Height()));
98
99   FakeNetworkPipe::Config pipe_config;
100   pipe_config.loss_percent = flags::LossPercent();
101   pipe_config.link_capacity_kbps = flags::LinkCapacity();
102   pipe_config.queue_length_packets = flags::QueueSize();
103   pipe_config.queue_delay_ms = flags::AvgPropagationDelayMs();
104   pipe_config.delay_standard_deviation_ms = flags::StdPropagationDelayMs();
105   test::DirectTransport transport(pipe_config);
106   Call::Config call_config(&transport);
107   call_config.start_bitrate_bps =
108       static_cast<int>(flags::StartBitrate()) * 1000;
109   scoped_ptr<Call> call(Call::Create(call_config));
110
111   // Loopback, call sends to itself.
112   transport.SetReceiver(call->Receiver());
113
114   VideoSendStream::Config send_config;
115   send_config.rtp.ssrcs.push_back(kSendSsrc);
116   send_config.rtp.rtx.ssrcs.push_back(kSendRtxSsrc);
117   send_config.rtp.rtx.payload_type = kRtxPayloadType;
118   send_config.rtp.nack.rtp_history_ms = 1000;
119
120   send_config.local_renderer = local_preview.get();
121   scoped_ptr<VideoEncoder> encoder;
122   if (flags::Codec() == "VP8") {
123     encoder.reset(VideoEncoder::Create(VideoEncoder::kVp8));
124   } else {
125     // Codec not supported.
126     assert(false && "Codec not supported!");
127     return;
128   }
129   send_config.encoder_settings.encoder = encoder.get();
130   send_config.encoder_settings.payload_name = flags::Codec();
131   send_config.encoder_settings.payload_type = 124;
132   VideoEncoderConfig encoder_config;
133   encoder_config.streams = test::CreateVideoStreams(1);
134   VideoStream* stream = &encoder_config.streams[0];
135   stream->width = flags::Width();
136   stream->height = flags::Height();
137   stream->min_bitrate_bps = static_cast<int>(flags::MinBitrate()) * 1000;
138   stream->target_bitrate_bps = static_cast<int>(flags::MaxBitrate()) * 1000;
139   stream->max_bitrate_bps = static_cast<int>(flags::MaxBitrate()) * 1000;
140   stream->max_framerate = 30;
141   stream->max_qp = 56;
142
143   VideoSendStream* send_stream =
144       call->CreateVideoSendStream(send_config, encoder_config);
145
146   Clock* test_clock = Clock::GetRealTimeClock();
147
148   scoped_ptr<test::VideoCapturer> camera(
149       test::VideoCapturer::Create(send_stream->Input(),
150                                   flags::Width(),
151                                   flags::Height(),
152                                   flags::Fps(),
153                                   test_clock));
154
155   VideoReceiveStream::Config receive_config;
156   receive_config.rtp.remote_ssrc = send_config.rtp.ssrcs[0];
157   receive_config.rtp.local_ssrc = kReceiverLocalSsrc;
158   receive_config.rtp.nack.rtp_history_ms = 1000;
159   receive_config.rtp.rtx[kRtxPayloadType].ssrc = kSendRtxSsrc;
160   receive_config.rtp.rtx[kRtxPayloadType].payload_type = kRtxPayloadType;
161   receive_config.renderer = loopback_video.get();
162   VideoCodec codec =
163       test::CreateDecoderVideoCodec(send_config.encoder_settings);
164   receive_config.codecs.push_back(codec);
165
166   VideoReceiveStream* receive_stream =
167       call->CreateVideoReceiveStream(receive_config);
168
169   receive_stream->Start();
170   send_stream->Start();
171   camera->Start();
172
173   test::PressEnterToContinue();
174
175   camera->Stop();
176   send_stream->Stop();
177   receive_stream->Stop();
178
179   call->DestroyVideoReceiveStream(receive_stream);
180   call->DestroyVideoSendStream(send_stream);
181
182   transport.StopSending();
183 }
184 }  // namespace webrtc
185
186 int main(int argc, char* argv[]) {
187   ::testing::InitGoogleTest(&argc, argv);
188   google::ParseCommandLineFlags(&argc, &argv, true);
189
190   webrtc::test::RunTest(webrtc::Loopback);
191   return 0;
192 }