1098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey/* 2098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 3098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey * 4098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey * Use of this source code is governed by a BSD-style license 5098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey * that can be found in the LICENSE file in the root of the source 6098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey * tree. An additional intellectual property rights grant can be found 7098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey * in the file PATENTS. All contributing project authors may 8098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey * be found in the AUTHORS file in the root of the source tree. 9098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey */ 10098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 11098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey#include "webrtc/test/fake_encoder.h" 12098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 13098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey#include "testing/gtest/include/gtest/gtest.h" 14098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 15098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey#include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h" 16098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 17098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkeynamespace webrtc { 18098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkeynamespace test { 19098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 2065c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff SharkeyFakeEncoder::FakeEncoder(Clock* clock) 2181f871e2b96125d57b76c07169e868e516443794Jean-Michel Trivi : clock_(clock), 22098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey callback_(NULL), 23098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey target_bitrate_kbps_(0), 24098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey max_target_bitrate_kbps_(-1), 25098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey last_encode_time_ms_(0) { 26098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey // Generate some arbitrary not-all-zero data 27098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) { 28098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded_buffer_[i] = static_cast<uint8_t>(i); 29098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey } 30098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey} 3165c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey 32cd686b5b6d4166b510df8e32138479a9559bc117John SpurlockFakeEncoder::~FakeEncoder() {} 33098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 34098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkeyvoid FakeEncoder::SetMaxBitrate(int max_kbps) { 35098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey assert(max_kbps >= -1); // max_kbps == -1 disables it. 36098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey max_target_bitrate_kbps_ = max_kbps; 37098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey} 38098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 39098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkeyint32_t FakeEncoder::InitEncode(const VideoCodec* config, 40098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey int32_t number_of_cores, 41098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey uint32_t max_payload_size) { 42098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey config_ = *config; 43098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey target_bitrate_kbps_ = config_.startBitrate; 44098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey return 0; 45098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey} 46b6e404a95f31f4681265eb53d2b7969f900151ccJeff Sharkey 47098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkeyint32_t FakeEncoder::Encode( 48098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey const I420VideoFrame& input_image, 49098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey const CodecSpecificInfo* codec_specific_info, 50098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey const std::vector<VideoFrameType>* frame_types) { 51098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey assert(config_.maxFramerate > 0); 52098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey int time_since_last_encode_ms = 1000 / config_.maxFramerate; 53b8bacccfc159fe204e5b09b52de55f8ba853f713John Spurlock int64_t time_now_ms = clock_->TimeInMilliseconds(); 54098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey if (last_encode_time_ms_ > 0) { 55098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey // For all frames but the first we can estimate the display time by looking 56098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey // at the display time of the previous frame. 57098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey time_since_last_encode_ms = time_now_ms - last_encode_time_ms_; 58098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey } 59098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 60098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey int bits_available = target_bitrate_kbps_ * time_since_last_encode_ms; 61098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey int min_bits = 62098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey config_.simulcastStream[0].minBitrate * time_since_last_encode_ms; 63098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey if (bits_available < min_bits) 64cd686b5b6d4166b510df8e32138479a9559bc117John Spurlock bits_available = min_bits; 65098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey int max_bits = max_target_bitrate_kbps_ * time_since_last_encode_ms; 66098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey if (max_bits > 0 && max_bits < bits_available) 67098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey bits_available = max_bits; 68098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey last_encode_time_ms_ = time_now_ms; 69098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 70098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey assert(config_.numberOfSimulcastStreams > 0); 71098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey for (int i = 0; i < config_.numberOfSimulcastStreams; ++i) { 72098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey CodecSpecificInfo specifics; 73098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey memset(&specifics, 0, sizeof(specifics)); 74098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey specifics.codecType = kVideoCodecGeneric; 7581f871e2b96125d57b76c07169e868e516443794Jean-Michel Trivi specifics.codecSpecific.generic.simulcast_idx = i; 76098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey int min_stream_bits = 7765c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey config_.simulcastStream[i].minBitrate * time_since_last_encode_ms; 7865c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey int max_stream_bits = 7981f871e2b96125d57b76c07169e868e516443794Jean-Michel Trivi config_.simulcastStream[i].maxBitrate * time_since_last_encode_ms; 80098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey int stream_bits = (bits_available > max_stream_bits) ? max_stream_bits : 81098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey bits_available; 82098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey int stream_bytes = (stream_bits + 7) / 8; 83098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey if (static_cast<size_t>(stream_bytes) > sizeof(encoded_buffer_)) 84098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey stream_bytes = sizeof(encoded_buffer_); 85cd686b5b6d4166b510df8e32138479a9559bc117John Spurlock 86098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey EncodedImage encoded( 87098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded_buffer_, stream_bytes, sizeof(encoded_buffer_)); 88098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded._timeStamp = input_image.timestamp(); 89098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded.capture_time_ms_ = input_image.render_time_ms(); 90098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded._frameType = (*frame_types)[i]; 91098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey // Always encode something on the first frame. 92098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey if (min_stream_bits > bits_available && i > 0) { 93098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded._length = 0; 94098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded._frameType = kSkipFrame; 9581f871e2b96125d57b76c07169e868e516443794Jean-Michel Trivi } 9665c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey assert(callback_ != NULL); 97cd686b5b6d4166b510df8e32138479a9559bc117John Spurlock if (callback_->Encoded(encoded, &specifics, NULL) != 0) 9865c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey return -1; 9965c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey 100098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey bits_available -= encoded._length * 8; 101098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey } 102098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey return 0; 103098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey} 10465c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey 10581f871e2b96125d57b76c07169e868e516443794Jean-Michel Triviint32_t FakeEncoder::RegisterEncodeCompleteCallback( 106098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey EncodedImageCallback* callback) { 107098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey callback_ = callback; 108098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey return 0; 109098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey} 110098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 111098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkeyint32_t FakeEncoder::Release() { return 0; } 112098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 113098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkeyint32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int rtt) { 114098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey return 0; 115cd686b5b6d4166b510df8e32138479a9559bc117John Spurlock} 116098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 117098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkeyint32_t FakeEncoder::SetRates(uint32_t new_target_bitrate, uint32_t framerate) { 118098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey target_bitrate_kbps_ = new_target_bitrate; 119098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey return 0; 120098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey} 121098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 122098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff SharkeyFakeH264Encoder::FakeH264Encoder(Clock* clock) 123098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey : FakeEncoder(clock), callback_(NULL), idr_counter_(0) { 124098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey FakeEncoder::RegisterEncodeCompleteCallback(this); 125098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey} 126098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 127098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkeyint32_t FakeH264Encoder::RegisterEncodeCompleteCallback( 128cd686b5b6d4166b510df8e32138479a9559bc117John Spurlock EncodedImageCallback* callback) { 129098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey callback_ = callback; 130098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey return 0; 131098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey} 132098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey 133098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkeyint32_t FakeH264Encoder::Encoded(EncodedImage& encoded_image, 134098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey const CodecSpecificInfo* codec_specific_info, 135098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey const RTPFragmentationHeader* fragments) { 136098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey const size_t kSpsSize = 8; 137098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey const size_t kPpsSize = 11; 138098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey const int kIdrFrequency = 10; 139098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey RTPFragmentationHeader fragmentation; 140098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey if (idr_counter_++ % kIdrFrequency == 0 && 14181f871e2b96125d57b76c07169e868e516443794Jean-Michel Trivi encoded_image._length > kSpsSize + kPpsSize + 1) { 142cd686b5b6d4166b510df8e32138479a9559bc117John Spurlock const size_t kNumSlices = 3; 143098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey fragmentation.VerifyAndAllocateFragmentationHeader(kNumSlices); 144098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey fragmentation.fragmentationOffset[0] = 0; 145098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey fragmentation.fragmentationLength[0] = kSpsSize; 14665c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey fragmentation.fragmentationOffset[1] = kSpsSize; 14781f871e2b96125d57b76c07169e868e516443794Jean-Michel Trivi fragmentation.fragmentationLength[1] = kPpsSize; 148098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey fragmentation.fragmentationOffset[2] = kSpsSize + kPpsSize; 149098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey fragmentation.fragmentationLength[2] = 150098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded_image._length - (kSpsSize + kPpsSize); 151098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey const uint8_t kSpsNalHeader = 0x37; 152cd686b5b6d4166b510df8e32138479a9559bc117John Spurlock const uint8_t kPpsNalHeader = 0x38; 153098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey const uint8_t kIdrNalHeader = 0x15; 154098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded_image._buffer[fragmentation.fragmentationOffset[0]] = kSpsNalHeader; 155098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded_image._buffer[fragmentation.fragmentationOffset[1]] = kPpsNalHeader; 156098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded_image._buffer[fragmentation.fragmentationOffset[2]] = kIdrNalHeader; 157098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey } else { 158098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey const size_t kNumSlices = 1; 159098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey fragmentation.VerifyAndAllocateFragmentationHeader(kNumSlices); 16065c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey fragmentation.fragmentationOffset[0] = 0; 16165c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey fragmentation.fragmentationLength[0] = encoded_image._length; 16265c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey const uint8_t kNalHeader = 0x11; 16365c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey encoded_image._buffer[fragmentation.fragmentationOffset[0]] = kNalHeader; 16465c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey } 16565c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey uint8_t value = 0; 16665c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey int fragment_counter = 0; 16765c4a2b26cd8776b0927e9b0e07ecf53bd31b627Jeff Sharkey for (size_t i = 0; i < encoded_image._length; ++i) { 168098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey if (fragment_counter == fragmentation.fragmentationVectorSize || 169098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey i != fragmentation.fragmentationOffset[fragment_counter]) { 170098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey encoded_image._buffer[i] = value++; 171098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey } else { 172098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey ++fragment_counter; 173098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey } 174098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey } 175098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey return callback_->Encoded(encoded_image, NULL, &fragmentation); 176098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey} 177098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey} // namespace test 178098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey} // namespace webrtc 179098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey