Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/webrtc/test/fake_encoder.cc @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 4.9 KB
Line 
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
15namespace webrtc {
16namespace test {
17
18FakeEncoder::FakeEncoder(Clock* clock)
19    : clock_(clock),
20      callback_(NULL),
21      target_bitrate_kbps_(0),
22      last_encode_time_ms_(0) {
23  // Generate some arbitrary not-all-zero data
24  for (size_t i = 0; i < sizeof(encoded_buffer_); ++i) {
25    encoded_buffer_[i] = static_cast<uint8_t>(i);
26  }
27}
28
29FakeEncoder::~FakeEncoder() {}
30
31void FakeEncoder::SetCodecSettings(VideoCodec* codec,
32                                   size_t num_streams) {
33  assert(num_streams > 0);
34  assert(num_streams <= kMaxSimulcastStreams);
35
36  static const SimulcastStream stream_settings[] = {
37      {320, 180, 0, 150, 150, 50, codec->qpMax},
38      {640, 360, 0, 500, 500, 150, codec->qpMax},
39      {1280, 720, 0, 1200, 1200, 600, codec->qpMax}};
40  // Add more streams to the settings above with reasonable values if required.
41  assert(num_streams <= sizeof(stream_settings) / sizeof(stream_settings[0]));
42
43  codec->numberOfSimulcastStreams = static_cast<unsigned char>(num_streams);
44
45  unsigned int sum_of_max_bitrates = 0;
46  for (size_t i = 0; i < num_streams; ++i) {
47    codec->simulcastStream[i] = stream_settings[i];
48    sum_of_max_bitrates += stream_settings[i].maxBitrate;
49  }
50
51  size_t last_stream = num_streams - 1;
52  codec->width = stream_settings[last_stream].width;
53  codec->height = stream_settings[last_stream].height;
54  // Start with the average for the middle stream's max/min settings.
55  codec->startBitrate = (stream_settings[last_stream / 2].maxBitrate +
56                         stream_settings[last_stream / 2].minBitrate) /
57                        2;
58  codec->minBitrate = stream_settings[0].minBitrate;
59  codec->maxBitrate = sum_of_max_bitrates;
60
61  codec->codecType = kVideoCodecGeneric;
62  strcpy(codec->plName, "FAKE");
63}
64
65int32_t FakeEncoder::InitEncode(const VideoCodec* config,
66                                int32_t number_of_cores,
67                                uint32_t max_payload_size) {
68  config_ = *config;
69  target_bitrate_kbps_ = config_.startBitrate;
70  return 0;
71}
72
73int32_t FakeEncoder::Encode(
74    const I420VideoFrame& input_image,
75    const CodecSpecificInfo* codec_specific_info,
76    const std::vector<VideoFrameType>* frame_types) {
77  assert(config_.maxFramerate > 0);
78  int delta_since_last_encode = 1000 / config_.maxFramerate;
79  int64_t time_now_ms = clock_->TimeInMilliseconds();
80  if (last_encode_time_ms_ > 0) {
81    // For all frames but the first we can estimate the display time by looking
82    // at the display time of the previous frame.
83    delta_since_last_encode = time_now_ms - last_encode_time_ms_;
84  }
85
86  int bits_available = target_bitrate_kbps_ * delta_since_last_encode;
87  int min_bits =
88      config_.simulcastStream[0].minBitrate * delta_since_last_encode;
89  if (bits_available < min_bits)
90    bits_available = min_bits;
91  last_encode_time_ms_ = time_now_ms;
92
93  for (int i = 0; i < config_.numberOfSimulcastStreams; ++i) {
94    CodecSpecificInfo specifics;
95    memset(&specifics, 0, sizeof(specifics));
96    specifics.codecType = kVideoCodecGeneric;
97    specifics.codecSpecific.generic.simulcast_idx = i;
98    int min_stream_bits = config_.simulcastStream[i].minBitrate *
99        delta_since_last_encode;
100    int max_stream_bits = config_.simulcastStream[i].maxBitrate *
101        delta_since_last_encode;
102    int stream_bits = (bits_available > max_stream_bits) ? max_stream_bits :
103        bits_available;
104    int stream_bytes = (stream_bits + 7) / 8;
105    if (static_cast<size_t>(stream_bytes) > sizeof(encoded_buffer_))
106      stream_bytes = sizeof(encoded_buffer_);
107
108    EncodedImage encoded(
109        encoded_buffer_, stream_bytes, sizeof(encoded_buffer_));
110    encoded._timeStamp = input_image.timestamp();
111    encoded.capture_time_ms_ = input_image.render_time_ms();
112    encoded._frameType = (*frame_types)[i];
113    if (min_stream_bits > bits_available) {
114      encoded._length = 0;
115      encoded._frameType = kSkipFrame;
116    }
117    if (callback_->Encoded(encoded, &specifics, NULL) != 0)
118      return -1;
119
120    bits_available -= encoded._length * 8;
121  }
122  return 0;
123}
124
125int32_t FakeEncoder::RegisterEncodeCompleteCallback(
126    EncodedImageCallback* callback) {
127  callback_ = callback;
128  return 0;
129}
130
131int32_t FakeEncoder::Release() { return 0; }
132
133int32_t FakeEncoder::SetChannelParameters(uint32_t packet_loss, int rtt) {
134  return 0;
135}
136
137int32_t FakeEncoder::SetRates(uint32_t new_target_bitrate, uint32_t framerate) {
138  target_bitrate_kbps_ = new_target_bitrate;
139  return 0;
140}
141}  // namespace test
142}  // namespace webrtc
Note: See TracBrowser for help on using the repository browser.