blob: 081bbb566dc89c700406cea954025d58001902dc [file] [log] [blame]
johanf2183ff2017-02-28 01:33:09 -08001/*
2 * Copyright 2017 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 <vector>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "test/gmock.h"
Yves Gerey665174f2018-06-19 15:03:05 +020014#include "test/gtest.h"
johanf2183ff2017-02-28 01:33:09 -080015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/video_codecs/video_decoder.h"
17#include "call/rtp_stream_receiver_controller.h"
18#include "media/base/fakevideorenderer.h"
19#include "modules/pacing/packet_router.h"
20#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
21#include "modules/utility/include/process_thread.h"
22#include "rtc_base/criticalsection.h"
23#include "rtc_base/event.h"
24#include "system_wrappers/include/clock.h"
25#include "test/field_trial.h"
26#include "video/call_stats.h"
27#include "video/video_receive_stream.h"
johanf2183ff2017-02-28 01:33:09 -080028
nisse0f15f922017-06-21 01:05:22 -070029namespace webrtc {
30namespace {
31
johanf2183ff2017-02-28 01:33:09 -080032using testing::_;
33using testing::Invoke;
34
35constexpr int kDefaultTimeOutMs = 50;
36
johanf2183ff2017-02-28 01:33:09 -080037const char kNewJitterBufferFieldTrialEnabled[] =
38 "WebRTC-NewVideoJitterBuffer/Enabled/";
39
40class MockTransport : public Transport {
41 public:
42 MOCK_METHOD3(SendRtp,
43 bool(const uint8_t* packet,
44 size_t length,
45 const PacketOptions& options));
46 MOCK_METHOD2(SendRtcp, bool(const uint8_t* packet, size_t length));
47};
48
49class MockVideoDecoder : public VideoDecoder {
50 public:
51 MOCK_METHOD2(InitDecode,
52 int32_t(const VideoCodec* config, int32_t number_of_cores));
Niels Möller8df3a382018-05-07 16:10:01 +020053 MOCK_METHOD4(Decode,
johanf2183ff2017-02-28 01:33:09 -080054 int32_t(const EncodedImage& input,
55 bool missing_frames,
johanf2183ff2017-02-28 01:33:09 -080056 const CodecSpecificInfo* codec_specific_info,
57 int64_t render_time_ms));
58 MOCK_METHOD1(RegisterDecodeCompleteCallback,
59 int32_t(DecodedImageCallback* callback));
60 MOCK_METHOD0(Release, int32_t(void));
61 const char* ImplementationName() const { return "MockVideoDecoder"; }
62};
63
64} // namespace
65
66class VideoReceiveStreamTest : public testing::Test {
67 public:
68 VideoReceiveStreamTest()
Tommi38c5d932018-03-27 23:11:09 +020069 : process_thread_(ProcessThread::Create("TestThread")),
70 override_field_trials_(kNewJitterBufferFieldTrialEnabled),
johanf2183ff2017-02-28 01:33:09 -080071 config_(&mock_transport_),
Tommi38c5d932018-03-27 23:11:09 +020072 call_stats_(Clock::GetRealTimeClock(), process_thread_.get()) {}
johanf2183ff2017-02-28 01:33:09 -080073
74 void SetUp() {
75 constexpr int kDefaultNumCpuCores = 2;
76 config_.rtp.remote_ssrc = 1111;
77 config_.rtp.local_ssrc = 2222;
78 config_.renderer = &fake_renderer_;
79 VideoReceiveStream::Decoder h264_decoder;
80 h264_decoder.payload_type = 99;
81 h264_decoder.payload_name = "H264";
82 h264_decoder.codec_params.insert(
83 {"sprop-parameter-sets", "Z0IACpZTBYmI,aMljiA=="});
84 h264_decoder.decoder = &mock_h264_video_decoder_;
85 config_.decoders.push_back(h264_decoder);
86 VideoReceiveStream::Decoder null_decoder;
87 null_decoder.payload_type = 98;
88 null_decoder.payload_name = "null";
89 null_decoder.decoder = &mock_null_video_decoder_;
90 config_.decoders.push_back(null_decoder);
91
92 video_receive_stream_.reset(new webrtc::internal::VideoReceiveStream(
Yves Gerey665174f2018-06-19 15:03:05 +020093 &rtp_stream_receiver_controller_, kDefaultNumCpuCores, &packet_router_,
94 config_.Copy(), process_thread_.get(), &call_stats_));
johanf2183ff2017-02-28 01:33:09 -080095 }
96
97 protected:
Tommi38c5d932018-03-27 23:11:09 +020098 std::unique_ptr<ProcessThread> process_thread_;
johanf2183ff2017-02-28 01:33:09 -080099 webrtc::test::ScopedFieldTrials override_field_trials_;
100 VideoReceiveStream::Config config_;
101 CallStats call_stats_;
102 MockVideoDecoder mock_h264_video_decoder_;
103 MockVideoDecoder mock_null_video_decoder_;
104 cricket::FakeVideoRenderer fake_renderer_;
105 MockTransport mock_transport_;
106 PacketRouter packet_router_;
nisse0f15f922017-06-21 01:05:22 -0700107 RtpStreamReceiverController rtp_stream_receiver_controller_;
johanf2183ff2017-02-28 01:33:09 -0800108 std::unique_ptr<webrtc::internal::VideoReceiveStream> video_receive_stream_;
109};
110
111TEST_F(VideoReceiveStreamTest, CreateFrameFromH264FmtpSpropAndIdr) {
112 constexpr uint8_t idr_nalu[] = {0x05, 0xFF, 0xFF, 0xFF};
113 RtpPacketToSend rtppacket(nullptr);
114 uint8_t* payload = rtppacket.AllocatePayload(sizeof(idr_nalu));
115 memcpy(payload, idr_nalu, sizeof(idr_nalu));
116 rtppacket.SetMarker(true);
117 rtppacket.SetSsrc(1111);
118 rtppacket.SetPayloadType(99);
119 rtppacket.SetSequenceNumber(1);
120 rtppacket.SetTimestamp(0);
121 rtc::Event init_decode_event_(false, false);
122 EXPECT_CALL(mock_h264_video_decoder_, InitDecode(_, _))
123 .WillOnce(Invoke([&init_decode_event_](const VideoCodec* config,
124 int32_t number_of_cores) {
125 init_decode_event_.Set();
126 return 0;
127 }));
128 EXPECT_CALL(mock_h264_video_decoder_, RegisterDecodeCompleteCallback(_));
129 video_receive_stream_->Start();
Niels Möller8df3a382018-05-07 16:10:01 +0200130 EXPECT_CALL(mock_h264_video_decoder_, Decode(_, false, _, _));
nissed2ef3142017-05-11 08:00:58 -0700131 RtpPacketReceived parsed_packet;
132 ASSERT_TRUE(parsed_packet.Parse(rtppacket.data(), rtppacket.size()));
nisse0f15f922017-06-21 01:05:22 -0700133 rtp_stream_receiver_controller_.OnRtpPacket(parsed_packet);
johanf2183ff2017-02-28 01:33:09 -0800134 EXPECT_CALL(mock_h264_video_decoder_, Release());
135 // Make sure the decoder thread had a chance to run.
136 init_decode_event_.Wait(kDefaultTimeOutMs);
137}
nisse0f15f922017-06-21 01:05:22 -0700138
johanf2183ff2017-02-28 01:33:09 -0800139} // namespace webrtc