stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2013 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 "webrtc/video_engine/test/common/fake_encoder.h" |
| 12 | |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | namespace webrtc { |
| 16 | |
| 17 | FakeEncoder::FakeEncoder(Clock* clock) |
| 18 | : clock_(clock), |
| 19 | callback_(NULL), |
| 20 | target_bitrate_kbps_(0), |
| 21 | last_encode_time_ms_(0) { |
| 22 | memset(encoded_buffer_, 0, sizeof(encoded_buffer_)); |
| 23 | } |
| 24 | |
| 25 | FakeEncoder::~FakeEncoder() {} |
| 26 | |
| 27 | int32_t FakeEncoder::InitEncode(const VideoCodec* config, |
| 28 | int32_t number_of_cores, |
| 29 | uint32_t max_payload_size) { |
| 30 | config_ = *config; |
| 31 | target_bitrate_kbps_ = config_.startBitrate; |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | int32_t FakeEncoder::Encode( |
| 36 | const I420VideoFrame& input_image, |
| 37 | const CodecSpecificInfo* codec_specific_info, |
| 38 | const std::vector<VideoFrameType>* frame_types) { |
| 39 | assert(config_.maxFramerate > 0); |
| 40 | int delta_since_last_encode = 1000 / config_.maxFramerate; |
| 41 | int64_t time_now_ms = clock_->TimeInMilliseconds(); |
| 42 | if (last_encode_time_ms_ > 0) { |
| 43 | // For all frames but the first we can estimate the display time by looking |
| 44 | // at the display time of the previous frame. |
| 45 | delta_since_last_encode = time_now_ms - last_encode_time_ms_; |
| 46 | } |
| 47 | |
| 48 | int bits_available = target_bitrate_kbps_ * delta_since_last_encode; |
| 49 | last_encode_time_ms_ = time_now_ms; |
| 50 | |
| 51 | for (int i = 0; i < config_.numberOfSimulcastStreams; ++i) { |
| 52 | CodecSpecificInfo specifics; |
| 53 | memset(&specifics, 0, sizeof(specifics)); |
| 54 | specifics.codecType = kVideoCodecVP8; |
| 55 | specifics.codecSpecific.VP8.simulcastIdx = i; |
| 56 | int min_stream_bits = config_.simulcastStream[i].minBitrate * |
| 57 | delta_since_last_encode; |
| 58 | int max_stream_bits = config_.simulcastStream[i].maxBitrate * |
| 59 | delta_since_last_encode; |
| 60 | int stream_bits = (bits_available > max_stream_bits) ? max_stream_bits : |
| 61 | bits_available; |
| 62 | int stream_bytes = (stream_bits + 7) / 8; |
| 63 | EXPECT_LT(stream_bytes, kMaxFrameSizeBytes); |
| 64 | if (stream_bytes > kMaxFrameSizeBytes) |
| 65 | return -1; |
| 66 | |
| 67 | EncodedImage encoded(encoded_buffer_, stream_bytes, kMaxFrameSizeBytes); |
| 68 | encoded._timeStamp = input_image.timestamp(); |
| 69 | encoded.capture_time_ms_ = input_image.render_time_ms(); |
| 70 | if (min_stream_bits > bits_available) { |
| 71 | encoded._length = 0; |
| 72 | encoded._frameType = kSkipFrame; |
| 73 | } |
| 74 | if (callback_->Encoded(encoded, &specifics, NULL) != 0) |
| 75 | return -1; |
| 76 | |
| 77 | bits_available -= encoded._length * 8; |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | int32_t FakeEncoder::RegisterEncodeCompleteCallback( |
| 83 | EncodedImageCallback* callback) { |
| 84 | callback_ = callback; |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int32_t FakeEncoder::Release() { return 0; } |
| 89 | |
| 90 | int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int rtt) { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int32_t FakeEncoder::SetRates(uint32_t new_target_bitrate, uint32_t framerate) { |
| 95 | target_bitrate_kbps_ = new_target_bitrate; |
| 96 | return 0; |
| 97 | } |
| 98 | } // namespace webrtc |