Home | History | Annotate | Download | only in test
      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/test/fake_encoder.h"
     12 
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 namespace webrtc {
     16 namespace test {
     17 
     18 FakeEncoder::FakeEncoder(Clock* clock)
     19     : clock_(clock),
     20       callback_(NULL),
     21       target_bitrate_kbps_(0),
     22       max_target_bitrate_kbps_(-1),
     23       last_encode_time_ms_(0) {
     24   // Generate some arbitrary not-all-zero data
     25   for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) {
     26     encoded_buffer_[i] = static_cast<uint8_t>(i);
     27   }
     28 }
     29 
     30 FakeEncoder::~FakeEncoder() {}
     31 
     32 void FakeEncoder::SetMaxBitrate(int max_kbps) {
     33   assert(max_kbps >= -1);  // max_kbps == -1 disables it.
     34   max_target_bitrate_kbps_ = max_kbps;
     35 }
     36 
     37 int32_t FakeEncoder::InitEncode(const VideoCodec* config,
     38                                 int32_t number_of_cores,
     39                                 uint32_t max_payload_size) {
     40   config_ = *config;
     41   target_bitrate_kbps_ = config_.startBitrate;
     42   return 0;
     43 }
     44 
     45 int32_t FakeEncoder::Encode(
     46     const I420VideoFrame& input_image,
     47     const CodecSpecificInfo* codec_specific_info,
     48     const std::vector<VideoFrameType>* frame_types) {
     49   assert(config_.maxFramerate > 0);
     50   int time_since_last_encode_ms = 1000 / config_.maxFramerate;
     51   int64_t time_now_ms = clock_->TimeInMilliseconds();
     52   if (last_encode_time_ms_ > 0) {
     53     // For all frames but the first we can estimate the display time by looking
     54     // at the display time of the previous frame.
     55     time_since_last_encode_ms = time_now_ms - last_encode_time_ms_;
     56   }
     57 
     58   int bits_available = target_bitrate_kbps_ * time_since_last_encode_ms;
     59   int min_bits =
     60       config_.simulcastStream[0].minBitrate * time_since_last_encode_ms;
     61   if (bits_available < min_bits)
     62     bits_available = min_bits;
     63   int max_bits = max_target_bitrate_kbps_ * time_since_last_encode_ms;
     64   if (max_bits > 0 && max_bits < bits_available)
     65     bits_available = max_bits;
     66   last_encode_time_ms_ = time_now_ms;
     67 
     68   assert(config_.numberOfSimulcastStreams > 0);
     69   for (int i = 0; i < config_.numberOfSimulcastStreams; ++i) {
     70     CodecSpecificInfo specifics;
     71     memset(&specifics, 0, sizeof(specifics));
     72     specifics.codecType = kVideoCodecGeneric;
     73     specifics.codecSpecific.generic.simulcast_idx = i;
     74     int min_stream_bits =
     75         config_.simulcastStream[i].minBitrate * time_since_last_encode_ms;
     76     int max_stream_bits =
     77         config_.simulcastStream[i].maxBitrate * time_since_last_encode_ms;
     78     int stream_bits = (bits_available > max_stream_bits) ? max_stream_bits :
     79         bits_available;
     80     int stream_bytes = (stream_bits + 7) / 8;
     81     if (static_cast<size_t>(stream_bytes) > sizeof(encoded_buffer_))
     82       stream_bytes = sizeof(encoded_buffer_);
     83 
     84     EncodedImage encoded(
     85         encoded_buffer_, stream_bytes, sizeof(encoded_buffer_));
     86     encoded._timeStamp = input_image.timestamp();
     87     encoded.capture_time_ms_ = input_image.render_time_ms();
     88     encoded._frameType = (*frame_types)[i];
     89     // Always encode something on the first frame.
     90     if (min_stream_bits > bits_available && i > 0) {
     91       encoded._length = 0;
     92       encoded._frameType = kSkipFrame;
     93     }
     94     assert(callback_ != NULL);
     95     if (callback_->Encoded(encoded, &specifics, NULL) != 0)
     96       return -1;
     97 
     98     bits_available -= encoded._length * 8;
     99   }
    100   return 0;
    101 }
    102 
    103 int32_t FakeEncoder::RegisterEncodeCompleteCallback(
    104     EncodedImageCallback* callback) {
    105   callback_ = callback;
    106   return 0;
    107 }
    108 
    109 int32_t FakeEncoder::Release() { return 0; }
    110 
    111 int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int rtt) {
    112   return 0;
    113 }
    114 
    115 int32_t FakeEncoder::SetRates(uint32_t new_target_bitrate, uint32_t framerate) {
    116   target_bitrate_kbps_ = new_target_bitrate;
    117   return 0;
    118 }
    119 
    120 }  // namespace test
    121 }  // namespace webrtc
    122