OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_TEST_FAKE_ENCODER_H_ | 11 #ifndef WEBRTC_TEST_FAKE_ENCODER_H_ |
12 #define WEBRTC_TEST_FAKE_ENCODER_H_ | 12 #define WEBRTC_TEST_FAKE_ENCODER_H_ |
13 | 13 |
14 #include <vector> | 14 #include <vector> |
| 15 #include <memory> |
15 | 16 |
16 #include "webrtc/base/criticalsection.h" | 17 #include "webrtc/base/criticalsection.h" |
| 18 #include "webrtc/base/sequenced_task_checker.h" |
17 #include "webrtc/base/task_queue.h" | 19 #include "webrtc/base/task_queue.h" |
18 #include "webrtc/common_types.h" | 20 #include "webrtc/common_types.h" |
19 #include "webrtc/system_wrappers/include/clock.h" | 21 #include "webrtc/system_wrappers/include/clock.h" |
20 #include "webrtc/video_encoder.h" | 22 #include "webrtc/video_encoder.h" |
21 | 23 |
22 namespace webrtc { | 24 namespace webrtc { |
23 namespace test { | 25 namespace test { |
24 | 26 |
25 class FakeEncoder : public VideoEncoder { | 27 class FakeEncoder : public VideoEncoder { |
26 public: | 28 public: |
27 explicit FakeEncoder(Clock* clock); | 29 explicit FakeEncoder(Clock* clock); |
28 virtual ~FakeEncoder(); | 30 virtual ~FakeEncoder() = default; |
29 | 31 |
30 // Sets max bitrate. Not thread-safe, call before registering the encoder. | 32 // Sets max bitrate. Not thread-safe, call before registering the encoder. |
31 void SetMaxBitrate(int max_kbps); | 33 void SetMaxBitrate(int max_kbps); |
32 | 34 |
33 int32_t InitEncode(const VideoCodec* config, | 35 int32_t InitEncode(const VideoCodec* config, |
34 int32_t number_of_cores, | 36 int32_t number_of_cores, |
35 size_t max_payload_size) override; | 37 size_t max_payload_size) override; |
36 int32_t Encode(const VideoFrame& input_image, | 38 int32_t Encode(const VideoFrame& input_image, |
37 const CodecSpecificInfo* codec_specific_info, | 39 const CodecSpecificInfo* codec_specific_info, |
38 const std::vector<FrameType>* frame_types) override; | 40 const std::vector<FrameType>* frame_types) override; |
39 int32_t RegisterEncodeCompleteCallback( | 41 int32_t RegisterEncodeCompleteCallback( |
40 EncodedImageCallback* callback) override; | 42 EncodedImageCallback* callback) override; |
41 int32_t Release() override; | 43 int32_t Release() override; |
42 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; | 44 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; |
43 int32_t SetRateAllocation(const BitrateAllocation& rate_allocation, | 45 int32_t SetRateAllocation(const BitrateAllocation& rate_allocation, |
44 uint32_t framerate) override; | 46 uint32_t framerate) override; |
45 const char* ImplementationName() const override; | 47 const char* ImplementationName() const override; |
46 | 48 |
47 static const char* kImplementationName; | 49 static const char* kImplementationName; |
48 | 50 |
49 protected: | 51 protected: |
50 rtc::CriticalSection crit_sect_; | |
51 Clock* const clock_; | 52 Clock* const clock_; |
52 VideoCodec config_ GUARDED_BY(crit_sect_); | 53 VideoCodec config_ GUARDED_BY(crit_sect_); |
53 EncodedImageCallback* callback_ GUARDED_BY(crit_sect_); | 54 EncodedImageCallback* callback_ GUARDED_BY(crit_sect_); |
54 BitrateAllocation target_bitrate_ GUARDED_BY(crit_sect_); | 55 BitrateAllocation target_bitrate_ GUARDED_BY(crit_sect_); |
55 int max_target_bitrate_kbps_ GUARDED_BY(crit_sect_); | 56 int max_target_bitrate_kbps_ GUARDED_BY(crit_sect_); |
56 int64_t last_encode_time_ms_ GUARDED_BY(crit_sect_); | 57 int64_t last_encode_time_ms_ GUARDED_BY(crit_sect_); |
| 58 rtc::CriticalSection crit_sect_; |
| 59 |
57 uint8_t encoded_buffer_[100000]; | 60 uint8_t encoded_buffer_[100000]; |
58 }; | 61 }; |
59 | 62 |
60 class FakeH264Encoder : public FakeEncoder, public EncodedImageCallback { | 63 class FakeH264Encoder : public FakeEncoder, public EncodedImageCallback { |
61 public: | 64 public: |
62 explicit FakeH264Encoder(Clock* clock); | 65 explicit FakeH264Encoder(Clock* clock); |
63 virtual ~FakeH264Encoder() {} | 66 virtual ~FakeH264Encoder() = default; |
64 | 67 |
65 int32_t RegisterEncodeCompleteCallback( | 68 int32_t RegisterEncodeCompleteCallback( |
66 EncodedImageCallback* callback) override; | 69 EncodedImageCallback* callback) override; |
67 | 70 |
68 Result OnEncodedImage(const EncodedImage& encodedImage, | 71 Result OnEncodedImage(const EncodedImage& encodedImage, |
69 const CodecSpecificInfo* codecSpecificInfo, | 72 const CodecSpecificInfo* codecSpecificInfo, |
70 const RTPFragmentationHeader* fragments) override; | 73 const RTPFragmentationHeader* fragments) override; |
71 | 74 |
72 private: | 75 private: |
73 rtc::CriticalSection local_crit_sect_; | |
74 EncodedImageCallback* callback_ GUARDED_BY(local_crit_sect_); | 76 EncodedImageCallback* callback_ GUARDED_BY(local_crit_sect_); |
75 int idr_counter_ GUARDED_BY(local_crit_sect_); | 77 int idr_counter_ GUARDED_BY(local_crit_sect_); |
| 78 rtc::CriticalSection local_crit_sect_; |
76 }; | 79 }; |
77 | 80 |
78 class DelayedEncoder : public test::FakeEncoder { | 81 class DelayedEncoder : public test::FakeEncoder { |
79 public: | 82 public: |
80 DelayedEncoder(Clock* clock, int delay_ms); | 83 DelayedEncoder(Clock* clock, int delay_ms); |
81 virtual ~DelayedEncoder() {} | 84 virtual ~DelayedEncoder() = default; |
82 | 85 |
83 void SetDelay(int delay_ms); | 86 void SetDelay(int delay_ms); |
84 int32_t Encode(const VideoFrame& input_image, | 87 int32_t Encode(const VideoFrame& input_image, |
85 const CodecSpecificInfo* codec_specific_info, | 88 const CodecSpecificInfo* codec_specific_info, |
86 const std::vector<FrameType>* frame_types) override; | 89 const std::vector<FrameType>* frame_types) override; |
87 | 90 |
88 private: | 91 private: |
89 rtc::CriticalSection local_crit_sect_; | 92 int delay_ms_ ACCESS_ON(sequence_checker_); |
90 int delay_ms_ GUARDED_BY(&local_crit_sect_); | 93 rtc::SequencedTaskChecker sequence_checker_; |
91 }; | 94 }; |
92 | 95 |
93 // This class implements a multi-threaded fake encoder by posting | 96 // This class implements a multi-threaded fake encoder by posting |
94 // FakeH264Encoder::Encode(.) tasks to |queue1_| and |queue2_|, in an | 97 // FakeH264Encoder::Encode(.) tasks to |queue1_| and |queue2_|, in an |
95 // alternating fashion. | 98 // alternating fashion. The class itself does not need to be thread safe, |
| 99 // as it is called from the task queue in ViEEncoder. |
96 class MultithreadedFakeH264Encoder : public test::FakeH264Encoder { | 100 class MultithreadedFakeH264Encoder : public test::FakeH264Encoder { |
97 public: | 101 public: |
98 explicit MultithreadedFakeH264Encoder(Clock* clock); | 102 explicit MultithreadedFakeH264Encoder(Clock* clock); |
99 virtual ~MultithreadedFakeH264Encoder() override; | 103 virtual ~MultithreadedFakeH264Encoder() = default; |
| 104 |
| 105 int32_t InitEncode(const VideoCodec* config, |
| 106 int32_t number_of_cores, |
| 107 size_t max_payload_size) override; |
100 | 108 |
101 int32_t Encode(const VideoFrame& input_image, | 109 int32_t Encode(const VideoFrame& input_image, |
102 const CodecSpecificInfo* codec_specific_info, | 110 const CodecSpecificInfo* codec_specific_info, |
103 const std::vector<FrameType>* frame_types) override; | 111 const std::vector<FrameType>* frame_types) override; |
104 | 112 |
105 int32_t EncodeCallback(const VideoFrame& input_image, | 113 int32_t EncodeCallback(const VideoFrame& input_image, |
106 const CodecSpecificInfo* codec_specific_info, | 114 const CodecSpecificInfo* codec_specific_info, |
107 const std::vector<FrameType>* frame_types); | 115 const std::vector<FrameType>* frame_types); |
108 | 116 |
| 117 int32_t Release() override; |
| 118 |
109 protected: | 119 protected: |
110 class EncodeTask; | 120 class EncodeTask; |
111 | 121 |
112 int current_queue_; | 122 int current_queue_ ACCESS_ON(sequence_checker_); |
113 rtc::TaskQueue queue1_; | 123 std::unique_ptr<rtc::TaskQueue> queue1_ ACCESS_ON(sequence_checker_); |
114 rtc::TaskQueue queue2_; | 124 std::unique_ptr<rtc::TaskQueue> queue2_ ACCESS_ON(sequence_checker_); |
| 125 rtc::SequencedTaskChecker sequence_checker_; |
115 }; | 126 }; |
116 | 127 |
117 } // namespace test | 128 } // namespace test |
118 } // namespace webrtc | 129 } // namespace webrtc |
119 | 130 |
120 #endif // WEBRTC_TEST_FAKE_ENCODER_H_ | 131 #endif // WEBRTC_TEST_FAKE_ENCODER_H_ |
OLD | NEW |