johan | 7d2ecd3 | 2017-02-28 09:33:09 | [diff] [blame] | 1 | /* |
| 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 | |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 16 | #include "webrtc/api/video_codecs/video_decoder.h" |
nisse | 04ef6b0 | 2017-06-21 08:05:22 | [diff] [blame] | 17 | #include "webrtc/call/rtp_stream_receiver_controller.h" |
johan | 7d2ecd3 | 2017-02-28 09:33:09 | [diff] [blame] | 18 | #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 Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 22 | #include "webrtc/rtc_base/criticalsection.h" |
| 23 | #include "webrtc/rtc_base/event.h" |
Henrik Kjellander | bc32410 | 2017-07-01 14:48:15 | [diff] [blame] | 24 | #include "webrtc/system_wrappers/include/clock.h" |
| 25 | #include "webrtc/test/field_trial.h" |
Edward Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 26 | #include "webrtc/video/call_stats.h" |
| 27 | #include "webrtc/video/video_receive_stream.h" |
johan | 7d2ecd3 | 2017-02-28 09:33:09 | [diff] [blame] | 28 | |
nisse | 04ef6b0 | 2017-06-21 08:05:22 | [diff] [blame] | 29 | namespace webrtc { |
| 30 | namespace { |
| 31 | |
johan | 7d2ecd3 | 2017-02-28 09:33:09 | [diff] [blame] | 32 | using testing::_; |
| 33 | using testing::Invoke; |
| 34 | |
| 35 | constexpr int kDefaultTimeOutMs = 50; |
| 36 | |
johan | 7d2ecd3 | 2017-02-28 09:33:09 | [diff] [blame] | 37 | const char kNewJitterBufferFieldTrialEnabled[] = |
| 38 | "WebRTC-NewVideoJitterBuffer/Enabled/"; |
| 39 | |
| 40 | class 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 | |
| 49 | class 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 | |
| 67 | class 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( |
nisse | 04ef6b0 | 2017-06-21 08:05:22 | [diff] [blame] | 94 | &rtp_stream_receiver_controller_, kDefaultNumCpuCores, |
nisse | 7e3c920 | 2017-04-19 06:38:35 | [diff] [blame] | 95 | &packet_router_, config_.Copy(), process_thread_.get(), &call_stats_)); |
johan | 7d2ecd3 | 2017-02-28 09:33:09 | [diff] [blame] | 96 | } |
| 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_; |
nisse | 04ef6b0 | 2017-06-21 08:05:22 | [diff] [blame] | 108 | RtpStreamReceiverController rtp_stream_receiver_controller_; |
johan | 7d2ecd3 | 2017-02-28 09:33:09 | [diff] [blame] | 109 | std::unique_ptr<webrtc::internal::VideoReceiveStream> video_receive_stream_; |
| 110 | }; |
| 111 | |
| 112 | TEST_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, _, _, _)); |
nisse | ba6f478 | 2017-05-11 15:00:58 | [diff] [blame] | 132 | RtpPacketReceived parsed_packet; |
| 133 | ASSERT_TRUE(parsed_packet.Parse(rtppacket.data(), rtppacket.size())); |
nisse | 04ef6b0 | 2017-06-21 08:05:22 | [diff] [blame] | 134 | rtp_stream_receiver_controller_.OnRtpPacket(parsed_packet); |
johan | 7d2ecd3 | 2017-02-28 09:33:09 | [diff] [blame] | 135 | 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 | } |
nisse | 04ef6b0 | 2017-06-21 08:05:22 | [diff] [blame] | 139 | |
johan | 7d2ecd3 | 2017-02-28 09:33:09 | [diff] [blame] | 140 | } // namespace webrtc |