Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / media / cast / video_receiver / video_receiver_unittest.cc
blob31c5bdd6e712b03a574257284358cbbb999f7a04
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.
5 #include "base/bind.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/test/simple_test_tick_clock.h"
9 #include "media/cast/cast_defines.h"
10 #include "media/cast/cast_environment.h"
11 #include "media/cast/pacing/mock_paced_packet_sender.h"
12 #include "media/cast/test/fake_task_runner.h"
13 #include "media/cast/video_receiver/video_receiver.h"
14 #include "testing/gmock/include/gmock/gmock.h"
16 static const int kPacketSize = 1500;
17 static const int64 kStartMillisecond = GG_INT64_C(12345678900000);
19 namespace media {
20 namespace cast {
22 using testing::_;
24 // was thread counted thread safe.
25 class TestVideoReceiverCallback :
26 public base::RefCountedThreadSafe<TestVideoReceiverCallback> {
27 public:
28 TestVideoReceiverCallback()
29 : num_called_(0) {}
31 // TODO(mikhal): Set and check expectations.
32 void DecodeComplete(scoped_ptr<I420VideoFrame> video_frame,
33 const base::TimeTicks& render_time) {
34 ++num_called_;
37 void FrameToDecode(scoped_ptr<EncodedVideoFrame> video_frame,
38 const base::TimeTicks& render_time) {
39 EXPECT_TRUE(video_frame->key_frame);
40 EXPECT_EQ(kVp8, video_frame->codec);
41 ++num_called_;
44 int number_times_called() { return num_called_;}
46 protected:
47 virtual ~TestVideoReceiverCallback() {}
49 private:
50 friend class base::RefCountedThreadSafe<TestVideoReceiverCallback>;
52 int num_called_;
55 class PeerVideoReceiver : public VideoReceiver {
56 public:
57 PeerVideoReceiver(scoped_refptr<CastEnvironment> cast_environment,
58 const VideoReceiverConfig& video_config,
59 PacedPacketSender* const packet_sender)
60 : VideoReceiver(cast_environment, video_config, packet_sender) {
62 using VideoReceiver::IncomingRtpPacket;
66 class VideoReceiverTest : public ::testing::Test {
67 protected:
68 VideoReceiverTest() {
69 // Configure to use vp8 software implementation.
70 config_.codec = kVp8;
71 config_.use_external_decoder = false;
72 task_runner_ = new test::FakeTaskRunner(&testing_clock_);
73 cast_environment_ = new CastEnvironment(&testing_clock_, task_runner_,
74 task_runner_, task_runner_, task_runner_, task_runner_);
75 receiver_.reset(new
76 PeerVideoReceiver(cast_environment_, config_, &mock_transport_));
77 testing_clock_.Advance(
78 base::TimeDelta::FromMilliseconds(kStartMillisecond));
79 video_receiver_callback_ = new TestVideoReceiverCallback();
82 virtual ~VideoReceiverTest() {}
84 virtual void SetUp() {
85 payload_.assign(kPacketSize, 0);
87 // Always start with a key frame.
88 rtp_header_.is_key_frame = true;
89 rtp_header_.frame_id = 0;
90 rtp_header_.packet_id = 0;
91 rtp_header_.max_packet_id = 0;
92 rtp_header_.is_reference = false;
93 rtp_header_.reference_frame_id = 0;
96 MockPacedPacketSender mock_transport_;
97 VideoReceiverConfig config_;
98 scoped_ptr<PeerVideoReceiver> receiver_;
99 std::vector<uint8> payload_;
100 RtpCastHeader rtp_header_;
101 base::SimpleTestTickClock testing_clock_;
103 scoped_refptr<test::FakeTaskRunner> task_runner_;
104 scoped_refptr<CastEnvironment> cast_environment_;
105 scoped_refptr<TestVideoReceiverCallback> video_receiver_callback_;
108 TEST_F(VideoReceiverTest, GetOnePacketEncodedframe) {
109 EXPECT_CALL(mock_transport_, SendRtcpPacket(_)).WillRepeatedly(
110 testing::Return(true));
111 receiver_->IncomingRtpPacket(payload_.data(), payload_.size(), rtp_header_);
113 VideoFrameEncodedCallback frame_to_decode_callback =
114 base::Bind(&TestVideoReceiverCallback::FrameToDecode,
115 video_receiver_callback_);
117 receiver_->GetEncodedVideoFrame(frame_to_decode_callback);
118 task_runner_->RunTasks();
119 EXPECT_EQ(video_receiver_callback_->number_times_called(), 1);
122 TEST_F(VideoReceiverTest, MultiplePackets) {
123 EXPECT_CALL(mock_transport_, SendRtcpPacket(_)).WillRepeatedly(
124 testing::Return(true));
125 rtp_header_.max_packet_id = 2;
126 receiver_->IncomingRtpPacket(payload_.data(), payload_.size(), rtp_header_);
127 ++rtp_header_.packet_id;
128 ++rtp_header_.webrtc.header.sequenceNumber;
129 receiver_->IncomingRtpPacket(payload_.data(), payload_.size(), rtp_header_);
130 ++rtp_header_.packet_id;
131 receiver_->IncomingRtpPacket(payload_.data(), payload_.size(), rtp_header_);
133 VideoFrameEncodedCallback frame_to_decode_callback =
134 base::Bind(&TestVideoReceiverCallback::FrameToDecode,
135 video_receiver_callback_);
137 receiver_->GetEncodedVideoFrame(frame_to_decode_callback);
139 task_runner_->RunTasks();
140 EXPECT_EQ(video_receiver_callback_->number_times_called(), 1);
143 TEST_F(VideoReceiverTest, GetOnePacketRawframe) {
144 EXPECT_CALL(mock_transport_, SendRtcpPacket(_)).WillRepeatedly(
145 testing::Return(true));
146 receiver_->IncomingRtpPacket(payload_.data(), payload_.size(), rtp_header_);
147 // Decode error - requires legal input.
148 VideoFrameDecodedCallback frame_decoded_callback =
149 base::Bind(&TestVideoReceiverCallback::DecodeComplete,
150 video_receiver_callback_);
151 receiver_->GetRawVideoFrame(frame_decoded_callback);
152 task_runner_->RunTasks();
153 EXPECT_EQ(video_receiver_callback_->number_times_called(), 0);
156 // TODO(pwestin): add encoded frames.
158 } // namespace cast
159 } // namespace media