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.
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"
29 #include "media/cast/test/linux_output_window.h"
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"
49 static const int kAudioSamplingFrequency
= 48000;
51 const int kVideoWindowWidth
= 1280;
52 const int kVideoWindowHeight
= 720;
54 static const int kFrameTimerMs
= 33;
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();
61 test::InputBuilder
rx_input(
62 "Enter receive port.", DEFAULT_RECEIVE_PORT
, 1, INT_MAX
);
63 *rx_port
= rx_input
.GetIntInput();
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();
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();
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();
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();
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();
97 void GetPayloadtype(AudioReceiverConfig
* audio_config
) {
98 test::InputBuilder
input("Choose audio receiver payload type.",
99 DEFAULT_AUDIO_PAYLOAD_TYPE
,
102 audio_config
->rtp_payload_type
= input
.GetIntInput();
105 AudioReceiverConfig
GetAudioReceiverConfig() {
106 AudioReceiverConfig audio_config
;
108 GetSsrcs(&audio_config
);
109 GetPayloadtype(&audio_config
);
111 audio_config
.rtcp_c_name
= "audio_receiver@a.b.c.d";
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
;
121 void GetPayloadtype(VideoReceiverConfig
* video_config
) {
122 test::InputBuilder
input("Choose video receiver payload type.",
123 DEFAULT_VIDEO_PAYLOAD_TYPE
,
126 video_config
->rtp_payload_type
= input
.GetIntInput();
129 VideoReceiverConfig
GetVideoReceiverConfig() {
130 VideoReceiverConfig video_config
;
132 GetSsrcs(&video_config
);
133 GetPayloadtype(&video_config
);
135 video_config
.rtcp_c_name
= "video_receiver@a.b.c.d";
137 video_config
.use_external_decoder
= false;
139 VLOG(1) << "Using VP8";
140 video_config
.codec
= transport::kVp8
;
144 static void UpdateCastTransportStatus(transport::CastTransportStatus status
) {
145 VLOG(1) << "CastTransportStatus = " << status
;
148 class ReceiveProcess
: public base::RefCountedThreadSafe
<ReceiveProcess
> {
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"),
155 last_playout_time_(),
156 last_render_time_() {
160 GetAudioFrame(base::TimeDelta::FromMilliseconds(kFrameTimerMs
));
165 virtual ~ReceiveProcess() {}
168 friend class base::RefCountedThreadSafe
<ReceiveProcess
>;
170 void DisplayFrame(const scoped_refptr
<media::VideoFrame
>& video_frame
,
171 const base::TimeTicks
& render_time
) {
173 render_
.RenderFrame(video_frame
);
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();
180 last_render_time_
= render_time
;
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();
194 last_playout_time_
= playout_time
;
195 GetAudioFrame(time_diff
);
198 void GetAudioFrame(base::TimeDelta playout_diff
) {
199 int num_10ms_blocks
= playout_diff
.InMilliseconds() / 10;
200 frame_receiver_
->GetRawAudioFrame(
202 kAudioSamplingFrequency
,
203 base::Bind(&ReceiveProcess::ReceiveAudioFrame
, this));
206 void GetVideoFrame() {
207 frame_receiver_
->GetRawVideoFrame(
208 base::Bind(&ReceiveProcess::DisplayFrame
, this));
211 scoped_refptr
<FrameReceiver
> frame_receiver_
;
213 test::LinuxOutputWindow render_
;
215 base::TimeTicks last_playout_time_
;
216 base::TimeTicks last_render_time_
;
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());
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();
234 scoped_ptr
<base::TickClock
> clock(new base::DefaultTickClock());
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(
241 main_message_loop
.message_loop_proxy(),
243 audio_thread
.message_loop_proxy(),
245 video_thread
.message_loop_proxy(),
246 main_message_loop
.message_loop_proxy(),
247 media::cast::GetDefaultCastReceiverLoggingConfig()));
249 media::cast::AudioReceiverConfig audio_config
=
250 media::cast::GetAudioReceiverConfig();
251 media::cast::VideoReceiverConfig video_config
=
252 media::cast::GetVideoReceiverConfig();
254 int remote_port
, local_port
;
255 media::cast::GetPorts(&remote_port
, &local_port
);
257 LOG(ERROR
) << "Invalid local port.";
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
;
266 if (!net::ParseIPLiteralToNumber(remote_ip_address
, &remote_ip_number
)) {
267 LOG(ERROR
) << "Invalid remote IP address.";
271 if (!net::ParseIPLiteralToNumber(local_ip_address
, &local_ip_number
)) {
272 LOG(ERROR
) << "Invalid local IP address.";
276 net::IPEndPoint
remote_end_point(remote_ip_number
, remote_port
);
277 net::IPEndPoint
local_end_point(local_ip_number
, local_port
);
279 scoped_ptr
<media::cast::transport::UdpTransport
> transport(
280 new media::cast::transport::UdpTransport(
281 main_message_loop
.message_loop_proxy(),
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()));
289 // TODO(hubbe): Make the cast receiver do this automatically.
290 transport
->StartReceiving(cast_receiver
->packet_receiver());
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();