blob: bce4fd3d7b7fbef244c4c1a4cf4902e0a4c1cd2f [file] [log] [blame]
johan7d2ecd32017-02-28 09:33:091/*
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
13#include "webrtc/test/gtest.h"
14#include "webrtc/test/gmock.h"
15
ilnik9b1cf9e2017-04-05 10:02:2016#include "webrtc/api/video_codecs/video_decoder.h"
nisse04ef6b02017-06-21 08:05:2217#include "webrtc/call/rtp_stream_receiver_controller.h"
johan7d2ecd32017-02-28 09:33:0918#include "webrtc/media/base/fakevideorenderer.h"
19#include "webrtc/modules/pacing/packet_router.h"
20#include "webrtc/modules/rtp_rtcp/source/rtp_packet_to_send.h"
21#include "webrtc/modules/utility/include/process_thread.h"
Edward Lemur76de83e2017-07-06 17:44:3422#include "webrtc/rtc_base/criticalsection.h"
23#include "webrtc/rtc_base/event.h"
Henrik Kjellanderbc324102017-07-01 14:48:1524#include "webrtc/system_wrappers/include/clock.h"
25#include "webrtc/test/field_trial.h"
Edward Lemur76de83e2017-07-06 17:44:3426#include "webrtc/video/call_stats.h"
27#include "webrtc/video/video_receive_stream.h"
johan7d2ecd32017-02-28 09:33:0928
nisse04ef6b02017-06-21 08:05:2229namespace webrtc {
30namespace {
31
johan7d2ecd32017-02-28 09:33:0932using testing::_;
33using testing::Invoke;
34
35constexpr int kDefaultTimeOutMs = 50;
36
johan7d2ecd32017-02-28 09:33:0937const 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));
53 MOCK_METHOD5(Decode,
54 int32_t(const EncodedImage& input,
55 bool missing_frames,
56 const RTPFragmentationHeader* fragmentation,
57 const CodecSpecificInfo* codec_specific_info,
58 int64_t render_time_ms));
59 MOCK_METHOD1(RegisterDecodeCompleteCallback,
60 int32_t(DecodedImageCallback* callback));
61 MOCK_METHOD0(Release, int32_t(void));
62 const char* ImplementationName() const { return "MockVideoDecoder"; }
63};
64
65} // namespace
66
67class VideoReceiveStreamTest : public testing::Test {
68 public:
69 VideoReceiveStreamTest()
70 : override_field_trials_(kNewJitterBufferFieldTrialEnabled),
71 config_(&mock_transport_),
72 call_stats_(Clock::GetRealTimeClock()),
73 process_thread_(ProcessThread::Create("TestThread")) {}
74
75 void SetUp() {
76 constexpr int kDefaultNumCpuCores = 2;
77 config_.rtp.remote_ssrc = 1111;
78 config_.rtp.local_ssrc = 2222;
79 config_.renderer = &fake_renderer_;
80 VideoReceiveStream::Decoder h264_decoder;
81 h264_decoder.payload_type = 99;
82 h264_decoder.payload_name = "H264";
83 h264_decoder.codec_params.insert(
84 {"sprop-parameter-sets", "Z0IACpZTBYmI,aMljiA=="});
85 h264_decoder.decoder = &mock_h264_video_decoder_;
86 config_.decoders.push_back(h264_decoder);
87 VideoReceiveStream::Decoder null_decoder;
88 null_decoder.payload_type = 98;
89 null_decoder.payload_name = "null";
90 null_decoder.decoder = &mock_null_video_decoder_;
91 config_.decoders.push_back(null_decoder);
92
93 video_receive_stream_.reset(new webrtc::internal::VideoReceiveStream(
nisse04ef6b02017-06-21 08:05:2294 &rtp_stream_receiver_controller_, kDefaultNumCpuCores,
nisse7e3c9202017-04-19 06:38:3595 &packet_router_, config_.Copy(), process_thread_.get(), &call_stats_));
johan7d2ecd32017-02-28 09:33:0996 }
97
98 protected:
99 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_;
107 std::unique_ptr<ProcessThread> process_thread_;
nisse04ef6b02017-06-21 08:05:22108 RtpStreamReceiverController rtp_stream_receiver_controller_;
johan7d2ecd32017-02-28 09:33:09109 std::unique_ptr<webrtc::internal::VideoReceiveStream> video_receive_stream_;
110};
111
112TEST_F(VideoReceiveStreamTest, CreateFrameFromH264FmtpSpropAndIdr) {
113 constexpr uint8_t idr_nalu[] = {0x05, 0xFF, 0xFF, 0xFF};
114 RtpPacketToSend rtppacket(nullptr);
115 uint8_t* payload = rtppacket.AllocatePayload(sizeof(idr_nalu));
116 memcpy(payload, idr_nalu, sizeof(idr_nalu));
117 rtppacket.SetMarker(true);
118 rtppacket.SetSsrc(1111);
119 rtppacket.SetPayloadType(99);
120 rtppacket.SetSequenceNumber(1);
121 rtppacket.SetTimestamp(0);
122 rtc::Event init_decode_event_(false, false);
123 EXPECT_CALL(mock_h264_video_decoder_, InitDecode(_, _))
124 .WillOnce(Invoke([&init_decode_event_](const VideoCodec* config,
125 int32_t number_of_cores) {
126 init_decode_event_.Set();
127 return 0;
128 }));
129 EXPECT_CALL(mock_h264_video_decoder_, RegisterDecodeCompleteCallback(_));
130 video_receive_stream_->Start();
131 EXPECT_CALL(mock_h264_video_decoder_, Decode(_, false, _, _, _));
nisseba6f4782017-05-11 15:00:58132 RtpPacketReceived parsed_packet;
133 ASSERT_TRUE(parsed_packet.Parse(rtppacket.data(), rtppacket.size()));
nisse04ef6b02017-06-21 08:05:22134 rtp_stream_receiver_controller_.OnRtpPacket(parsed_packet);
johan7d2ecd32017-02-28 09:33:09135 EXPECT_CALL(mock_h264_video_decoder_, Release());
136 // Make sure the decoder thread had a chance to run.
137 init_decode_event_.Wait(kDefaultTimeOutMs);
138}
nisse04ef6b02017-06-21 08:05:22139
johan7d2ecd32017-02-28 09:33:09140} // namespace webrtc