Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / cast / test / receiver.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <algorithm>
6 #include <climits>
7 #include <cstdarg>
8 #include <cstdio>
9 #include <string>
10
11 #include "base/at_exit.h"
12 #include "base/command_line.h"
13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/threading/thread.h"
18 #include "base/time/default_tick_clock.h"
19 #include "media/base/video_frame.h"
20 #include "media/cast/cast_config.h"
21 #include "media/cast/cast_environment.h"
22 #include "media/cast/cast_receiver.h"
23 #include "media/cast/logging/logging_defines.h"
24 #include "media/cast/test/utility/input_builder.h"
25 #include "media/cast/transport/transport/udp_transport.h"
26 #include "net/base/net_util.h"
27
28 #if defined(OS_LINUX)
29 #include "media/cast/test/linux_output_window.h"
30 #endif  // OS_LINUX
31
32 namespace media {
33 namespace cast {
34 // Settings chosen to match default sender settings.
35 #define DEFAULT_SEND_PORT "0"
36 #define DEFAULT_RECEIVE_PORT "2344"
37 #define DEFAULT_SEND_IP "0.0.0.0"
38 #define DEFAULT_RESTART "0"
39 #define DEFAULT_AUDIO_FEEDBACK_SSRC "1"
40 #define DEFAULT_AUDIO_INCOMING_SSRC "2"
41 #define DEFAULT_AUDIO_PAYLOAD_TYPE "127"
42 #define DEFAULT_VIDEO_FEEDBACK_SSRC "12"
43 #define DEFAULT_VIDEO_INCOMING_SSRC "11"
44 #define DEFAULT_VIDEO_PAYLOAD_TYPE "96"
45 #define DEFAULT_VIDEO_CODEC_WIDTH "640"
46 #define DEFAULT_VIDEO_CODEC_HEIGHT "480"
47 #define DEFAULT_VIDEO_CODEC_BITRATE "2000"
48
49 static const int kAudioSamplingFrequency = 48000;
50 #if defined(OS_LINUX)
51 const int kVideoWindowWidth = 1280;
52 const int kVideoWindowHeight = 720;
53 #endif  // OS_LINUX
54 static const int kFrameTimerMs = 33;
55
56 void GetPorts(int* tx_port, int* rx_port) {
57   test::InputBuilder tx_input(
58       "Enter send port.", DEFAULT_SEND_PORT, 1, INT_MAX);
59   *tx_port = tx_input.GetIntInput();
60
61   test::InputBuilder rx_input(
62       "Enter receive port.", DEFAULT_RECEIVE_PORT, 1, INT_MAX);
63   *rx_port = rx_input.GetIntInput();
64 }
65
66 std::string GetIpAddress(const std::string display_text) {
67   test::InputBuilder input(display_text, DEFAULT_SEND_IP, INT_MIN, INT_MAX);
68   std::string ip_address = input.GetStringInput();
69   // Ensure IP address is either the default value or in correct form.
70   while (ip_address != DEFAULT_SEND_IP &&
71          std::count(ip_address.begin(), ip_address.end(), '.') != 3) {
72     ip_address = input.GetStringInput();
73   }
74   return ip_address;
75 }
76
77 void GetSsrcs(AudioReceiverConfig* audio_config) {
78   test::InputBuilder input_tx(
79       "Choose audio sender SSRC.", DEFAULT_AUDIO_FEEDBACK_SSRC, 1, INT_MAX);
80   audio_config->feedback_ssrc = input_tx.GetIntInput();
81
82   test::InputBuilder input_rx(
83       "Choose audio receiver SSRC.", DEFAULT_AUDIO_INCOMING_SSRC, 1, INT_MAX);
84   audio_config->incoming_ssrc = input_tx.GetIntInput();
85 }
86
87 void GetSsrcs(VideoReceiverConfig* video_config) {
88   test::InputBuilder input_tx(
89       "Choose video sender SSRC.", DEFAULT_VIDEO_FEEDBACK_SSRC, 1, INT_MAX);
90   video_config->feedback_ssrc = input_tx.GetIntInput();
91
92   test::InputBuilder input_rx(
93       "Choose video receiver SSRC.", DEFAULT_VIDEO_INCOMING_SSRC, 1, INT_MAX);
94   video_config->incoming_ssrc = input_rx.GetIntInput();
95 }
96
97 void GetPayloadtype(AudioReceiverConfig* audio_config) {
98   test::InputBuilder input("Choose audio receiver payload type.",
99                            DEFAULT_AUDIO_PAYLOAD_TYPE,
100                            96,
101                            127);
102   audio_config->rtp_payload_type = input.GetIntInput();
103 }
104
105 AudioReceiverConfig GetAudioReceiverConfig() {
106   AudioReceiverConfig audio_config;
107
108   GetSsrcs(&audio_config);
109   GetPayloadtype(&audio_config);
110
111   audio_config.rtcp_c_name = "audio_receiver@a.b.c.d";
112
113   VLOG(1) << "Using OPUS 48Khz stereo";
114   audio_config.use_external_decoder = false;
115   audio_config.frequency = 48000;
116   audio_config.channels = 2;
117   audio_config.codec = transport::kOpus;
118   return audio_config;
119 }
120
121 void GetPayloadtype(VideoReceiverConfig* video_config) {
122   test::InputBuilder input("Choose video receiver payload type.",
123                            DEFAULT_VIDEO_PAYLOAD_TYPE,
124                            96,
125                            127);
126   video_config->rtp_payload_type = input.GetIntInput();
127 }
128
129 VideoReceiverConfig GetVideoReceiverConfig() {
130   VideoReceiverConfig video_config;
131
132   GetSsrcs(&video_config);
133   GetPayloadtype(&video_config);
134
135   video_config.rtcp_c_name = "video_receiver@a.b.c.d";
136
137   video_config.use_external_decoder = false;
138
139   VLOG(1) << "Using VP8";
140   video_config.codec = transport::kVp8;
141   return video_config;
142 }
143
144 static void UpdateCastTransportStatus(transport::CastTransportStatus status) {
145   VLOG(1) << "CastTransportStatus = " << status;
146 }
147
148 class ReceiveProcess : public base::RefCountedThreadSafe<ReceiveProcess> {
149  public:
150   explicit ReceiveProcess(scoped_refptr<FrameReceiver> frame_receiver)
151       : frame_receiver_(frame_receiver),
152 #if defined(OS_LINUX)
153         render_(0, 0, kVideoWindowWidth, kVideoWindowHeight, "Cast_receiver"),
154 #endif  // OS_LINUX
155         last_playout_time_(),
156         last_render_time_() {
157   }
158
159   void Start() {
160     GetAudioFrame(base::TimeDelta::FromMilliseconds(kFrameTimerMs));
161     GetVideoFrame();
162   }
163
164  protected:
165   virtual ~ReceiveProcess() {}
166
167  private:
168   friend class base::RefCountedThreadSafe<ReceiveProcess>;
169
170   void DisplayFrame(const scoped_refptr<media::VideoFrame>& video_frame,
171                     const base::TimeTicks& render_time) {
172 #ifdef OS_LINUX
173     render_.RenderFrame(video_frame);
174 #endif  // OS_LINUX
175     // Print out the delta between frames.
176     if (!last_render_time_.is_null()) {
177       base::TimeDelta time_diff = render_time - last_render_time_;
178       VLOG(0) << " RenderDelay[mS] =  " << time_diff.InMilliseconds();
179     }
180     last_render_time_ = render_time;
181     GetVideoFrame();
182   }
183
184   void ReceiveAudioFrame(scoped_ptr<PcmAudioFrame> audio_frame,
185                          const base::TimeTicks& playout_time) {
186     // For audio just print the playout delta between audio frames.
187     // Default diff time is kFrameTimerMs.
188     base::TimeDelta time_diff =
189         base::TimeDelta::FromMilliseconds(kFrameTimerMs);
190     if (!last_playout_time_.is_null()) {
191       time_diff = playout_time - last_playout_time_;
192       VLOG(0) << " ***PlayoutDelay[mS] =  " << time_diff.InMilliseconds();
193     }
194     last_playout_time_ = playout_time;
195     GetAudioFrame(time_diff);
196   }
197
198   void GetAudioFrame(base::TimeDelta playout_diff) {
199     int num_10ms_blocks = playout_diff.InMilliseconds() / 10;
200     frame_receiver_->GetRawAudioFrame(
201         num_10ms_blocks,
202         kAudioSamplingFrequency,
203         base::Bind(&ReceiveProcess::ReceiveAudioFrame, this));
204   }
205
206   void GetVideoFrame() {
207     frame_receiver_->GetRawVideoFrame(
208         base::Bind(&ReceiveProcess::DisplayFrame, this));
209   }
210
211   scoped_refptr<FrameReceiver> frame_receiver_;
212 #ifdef OS_LINUX
213   test::LinuxOutputWindow render_;
214 #endif  // OS_LINUX
215   base::TimeTicks last_playout_time_;
216   base::TimeTicks last_render_time_;
217 };
218
219 }  // namespace cast
220 }  // namespace media
221
222 int main(int argc, char** argv) {
223   base::AtExitManager at_exit;
224   base::MessageLoopForIO main_message_loop;
225   CommandLine::Init(argc, argv);
226   InitLogging(logging::LoggingSettings());
227
228   VLOG(1) << "Cast Receiver";
229   base::Thread audio_thread("Cast audio decoder thread");
230   base::Thread video_thread("Cast video decoder thread");
231   audio_thread.Start();
232   video_thread.Start();
233
234   scoped_ptr<base::TickClock> clock(new base::DefaultTickClock());
235
236   // Enable receiver side threads, and disable logging.
237   // Running transport on main thread.
238   scoped_refptr<media::cast::CastEnvironment> cast_environment(
239       new media::cast::CastEnvironment(
240           clock.Pass(),
241           main_message_loop.message_loop_proxy(),
242           NULL,
243           audio_thread.message_loop_proxy(),
244           NULL,
245           video_thread.message_loop_proxy(),
246           main_message_loop.message_loop_proxy(),
247           media::cast::GetDefaultCastReceiverLoggingConfig()));
248
249   media::cast::AudioReceiverConfig audio_config =
250       media::cast::GetAudioReceiverConfig();
251   media::cast::VideoReceiverConfig video_config =
252       media::cast::GetVideoReceiverConfig();
253
254   int remote_port, local_port;
255   media::cast::GetPorts(&remote_port, &local_port);
256   if (!local_port) {
257     LOG(ERROR) << "Invalid local port.";
258     return 1;
259   }
260
261   std::string remote_ip_address = media::cast::GetIpAddress("Enter remote IP.");
262   std::string local_ip_address = media::cast::GetIpAddress("Enter local IP.");
263   net::IPAddressNumber remote_ip_number;
264   net::IPAddressNumber local_ip_number;
265
266   if (!net::ParseIPLiteralToNumber(remote_ip_address, &remote_ip_number)) {
267     LOG(ERROR) << "Invalid remote IP address.";
268     return 1;
269   }
270
271   if (!net::ParseIPLiteralToNumber(local_ip_address, &local_ip_number)) {
272     LOG(ERROR) << "Invalid local IP address.";
273     return 1;
274   }
275
276   net::IPEndPoint remote_end_point(remote_ip_number, remote_port);
277   net::IPEndPoint local_end_point(local_ip_number, local_port);
278
279   scoped_ptr<media::cast::transport::UdpTransport> transport(
280       new media::cast::transport::UdpTransport(
281           main_message_loop.message_loop_proxy(),
282           local_end_point,
283           remote_end_point,
284           base::Bind(&media::cast::UpdateCastTransportStatus)));
285   scoped_ptr<media::cast::CastReceiver> cast_receiver(
286       media::cast::CastReceiver::CreateCastReceiver(
287           cast_environment, audio_config, video_config, transport.get()));
288
289   // TODO(hubbe): Make the cast receiver do this automatically.
290   transport->StartReceiving(cast_receiver->packet_receiver());
291
292   scoped_refptr<media::cast::ReceiveProcess> receive_process(
293       new media::cast::ReceiveProcess(cast_receiver->frame_receiver()));
294   receive_process->Start();
295   main_message_loop.Run();
296   return 0;
297 }