1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/*
* Copyright 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "pc/video_rtp_receiver.h"
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "api/make_ref_counted.h"
#include "api/media_stream_interface.h"
#include "api/scoped_refptr.h"
#include "api/task_queue/task_queue_base.h"
#include "api/video/recordable_encoded_frame.h"
#include "api/video/test/mock_recordable_encoded_frame.h"
#include "api/video/video_sink_interface.h"
#include "media/base/fake_media_engine.h"
#include "media/base/media_channel.h"
#include "rtc_base/task_queue_for_test.h"
#include "rtc_base/thread.h"
#include "test/gmock.h"
#include "test/gtest.h"
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::InSequence;
using ::testing::Mock;
using ::testing::NiceMock;
using ::testing::SaveArg;
using ::testing::StrictMock;
namespace webrtc {
namespace {
class VideoRtpReceiverTest : public testing::Test {
protected:
class MockVideoMediaSendChannel : public FakeVideoMediaSendChannel {
public:
MockVideoMediaSendChannel(const VideoOptions& options,
TaskQueueBase* network_thread = Thread::Current())
: FakeVideoMediaSendChannel(options, network_thread) {}
MOCK_METHOD(void,
GenerateSendKeyFrame,
(uint32_t, const std::vector<std::string>&),
(override));
};
class MockVideoMediaReceiveChannel : public FakeVideoMediaReceiveChannel {
public:
MockVideoMediaReceiveChannel(
const VideoOptions& options,
TaskQueueBase* network_thread = Thread::Current())
: FakeVideoMediaReceiveChannel(options, network_thread) {}
MOCK_METHOD(void,
SetRecordableEncodedFrameCallback,
(uint32_t, std::function<void(const RecordableEncodedFrame&)>),
(override));
MOCK_METHOD(void,
ClearRecordableEncodedFrameCallback,
(uint32_t),
(override));
MOCK_METHOD(void, RequestRecvKeyFrame, (uint32_t), (override));
};
class MockVideoSink : public VideoSinkInterface<RecordableEncodedFrame> {
public:
MOCK_METHOD(void, OnFrame, (const RecordableEncodedFrame&), (override));
};
VideoRtpReceiverTest()
: worker_thread_(Thread::Create()),
channel_(VideoOptions()),
receiver_(make_ref_counted<VideoRtpReceiver>(
worker_thread_.get(),
std::string("receiver"),
std::vector<std::string>({"stream"}))) {
worker_thread_->Start();
SetMediaChannel(&channel_);
}
~VideoRtpReceiverTest() override {
// Clear expectations that tests may have set up before calling
// SetMediaChannel(nullptr).
Mock::VerifyAndClearExpectations(&channel_);
receiver_->Stop();
SetMediaChannel(nullptr);
}
void SetMediaChannel(MediaReceiveChannelInterface* media_channel) {
SendTask(worker_thread_.get(),
[&]() { receiver_->SetMediaChannel(media_channel); });
}
VideoTrackSourceInterface* Source() {
return receiver_->streams()[0]->FindVideoTrack("receiver")->GetSource();
}
AutoThread main_thread_;
std::unique_ptr<Thread> worker_thread_;
NiceMock<MockVideoMediaReceiveChannel> channel_;
scoped_refptr<VideoRtpReceiver> receiver_;
};
TEST_F(VideoRtpReceiverTest, SupportsEncodedOutput) {
EXPECT_TRUE(Source()->SupportsEncodedOutput());
}
TEST_F(VideoRtpReceiverTest, GeneratesKeyFrame) {
EXPECT_CALL(channel_, RequestRecvKeyFrame(0));
Source()->GenerateKeyFrame();
}
TEST_F(VideoRtpReceiverTest,
GenerateKeyFrameOnChannelSwitchUnlessGenerateKeyframeCalled) {
// A channel switch without previous call to GenerateKeyFrame shouldn't
// cause a call to happen on the new channel.
MockVideoMediaReceiveChannel channel2{VideoOptions()};
EXPECT_CALL(channel_, RequestRecvKeyFrame).Times(0);
EXPECT_CALL(channel2, RequestRecvKeyFrame).Times(0);
SetMediaChannel(&channel2);
Mock::VerifyAndClearExpectations(&channel2);
// Generate a key frame. When we switch channel next time, we will have to
// re-generate it as we don't know if it was eventually received
EXPECT_CALL(channel2, RequestRecvKeyFrame).Times(1);
Source()->GenerateKeyFrame();
MockVideoMediaReceiveChannel channel3{VideoOptions()};
EXPECT_CALL(channel3, RequestRecvKeyFrame);
SetMediaChannel(&channel3);
// Switching to a new channel should now not cause calls to GenerateKeyFrame.
StrictMock<MockVideoMediaReceiveChannel> channel4{VideoOptions()};
SetMediaChannel(&channel4);
// We must call SetMediaChannel(nullptr) here since the mock media channels
// live on the stack and `receiver_` still has a pointer to those objects.
SetMediaChannel(nullptr);
}
TEST_F(VideoRtpReceiverTest, EnablesEncodedOutput) {
EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(/*ssrc=*/0, _));
EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback).Times(0);
MockVideoSink sink;
Source()->AddEncodedSink(&sink);
}
TEST_F(VideoRtpReceiverTest, DisablesEncodedOutput) {
EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback(/*ssrc=*/0));
MockVideoSink sink;
Source()->AddEncodedSink(&sink);
Source()->RemoveEncodedSink(&sink);
}
TEST_F(VideoRtpReceiverTest, DisablesEnablesEncodedOutputOnChannelSwitch) {
InSequence s;
EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback);
EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback);
MockVideoSink sink;
Source()->AddEncodedSink(&sink);
MockVideoMediaReceiveChannel channel2{VideoOptions()};
EXPECT_CALL(channel2, SetRecordableEncodedFrameCallback);
SetMediaChannel(&channel2);
Mock::VerifyAndClearExpectations(&channel2);
// When clearing encoded frame buffer function, we need channel switches
// to NOT set the callback again.
EXPECT_CALL(channel2, ClearRecordableEncodedFrameCallback);
Source()->RemoveEncodedSink(&sink);
StrictMock<MockVideoMediaReceiveChannel> channel3{VideoOptions()};
SetMediaChannel(&channel3);
// We must call SetMediaChannel(nullptr) here since the mock media channels
// live on the stack and `receiver_` still has a pointer to those objects.
SetMediaChannel(nullptr);
}
TEST_F(VideoRtpReceiverTest, BroadcastsEncodedFramesWhenEnabled) {
std::function<void(const RecordableEncodedFrame&)> broadcast;
EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(_, _))
.WillRepeatedly(SaveArg<1>(&broadcast));
MockVideoSink sink;
Source()->AddEncodedSink(&sink);
// Make sure SetEncodedFrameBufferFunction completes.
Mock::VerifyAndClearExpectations(&channel_);
// Pass two frames on different contexts.
EXPECT_CALL(sink, OnFrame).Times(2);
MockRecordableEncodedFrame frame;
broadcast(frame);
SendTask(worker_thread_.get(), [&] { broadcast(frame); });
}
TEST_F(VideoRtpReceiverTest, EnablesEncodedOutputOnChannelRestart) {
InSequence s;
MockVideoSink sink;
Source()->AddEncodedSink(&sink);
EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(4711, _));
receiver_->SetupMediaChannel(4711);
EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback(4711));
EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(0, _));
receiver_->SetupUnsignaledMediaChannel();
}
} // namespace
} // namespace webrtc
|