You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2018 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. #include "test/fake_vp8_encoder.h"
  11. #include <algorithm>
  12. #include "absl/types/optional.h"
  13. #include "api/video_codecs/video_encoder.h"
  14. #include "api/video_codecs/vp8_temporal_layers.h"
  15. #include "api/video_codecs/vp8_temporal_layers_factory.h"
  16. #include "modules/video_coding/codecs/interface/common_constants.h"
  17. #include "modules/video_coding/include/video_codec_interface.h"
  18. #include "modules/video_coding/include/video_error_codes.h"
  19. #include "modules/video_coding/utility/simulcast_utility.h"
  20. namespace {
  21. // Write width and height to the payload the same way as the real encoder does.
  22. // It requires that |payload| has a size of at least kMinPayLoadHeaderLength.
  23. void WriteFakeVp8(unsigned char* payload,
  24. int width,
  25. int height,
  26. bool key_frame) {
  27. payload[0] = key_frame ? 0 : 0x01;
  28. if (key_frame) {
  29. payload[9] = (height & 0x3F00) >> 8;
  30. payload[8] = (height & 0x00FF);
  31. payload[7] = (width & 0x3F00) >> 8;
  32. payload[6] = (width & 0x00FF);
  33. }
  34. }
  35. } // namespace
  36. namespace webrtc {
  37. namespace test {
  38. FakeVP8Encoder::FakeVP8Encoder(Clock* clock) : FakeEncoder(clock) {
  39. sequence_checker_.Detach();
  40. }
  41. int32_t FakeVP8Encoder::InitEncode(const VideoCodec* config,
  42. const Settings& settings) {
  43. RTC_DCHECK_RUN_ON(&sequence_checker_);
  44. auto result = FakeEncoder::InitEncode(config, settings);
  45. if (result != WEBRTC_VIDEO_CODEC_OK) {
  46. return result;
  47. }
  48. Vp8TemporalLayersFactory factory;
  49. frame_buffer_controller_ =
  50. factory.Create(*config, settings, &fec_controller_override_);
  51. return WEBRTC_VIDEO_CODEC_OK;
  52. }
  53. int32_t FakeVP8Encoder::Release() {
  54. auto result = FakeEncoder::Release();
  55. sequence_checker_.Detach();
  56. return result;
  57. }
  58. void FakeVP8Encoder::PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
  59. size_t size_bytes,
  60. VideoFrameType frame_type,
  61. int stream_idx,
  62. uint32_t timestamp) {
  63. RTC_DCHECK_RUN_ON(&sequence_checker_);
  64. codec_specific->codecType = kVideoCodecVP8;
  65. codec_specific->codecSpecific.VP8.keyIdx = kNoKeyIdx;
  66. codec_specific->codecSpecific.VP8.nonReference = false;
  67. if (size_bytes > 0) {
  68. frame_buffer_controller_->OnEncodeDone(
  69. stream_idx, timestamp, size_bytes,
  70. frame_type == VideoFrameType::kVideoFrameKey, -1, codec_specific);
  71. } else {
  72. frame_buffer_controller_->OnFrameDropped(stream_idx, timestamp);
  73. }
  74. }
  75. std::unique_ptr<RTPFragmentationHeader> FakeVP8Encoder::EncodeHook(
  76. EncodedImage* encoded_image,
  77. CodecSpecificInfo* codec_specific) {
  78. RTC_DCHECK_RUN_ON(&sequence_checker_);
  79. uint8_t stream_idx = encoded_image->SpatialIndex().value_or(0);
  80. frame_buffer_controller_->NextFrameConfig(stream_idx,
  81. encoded_image->Timestamp());
  82. PopulateCodecSpecific(codec_specific, encoded_image->size(),
  83. encoded_image->_frameType, stream_idx,
  84. encoded_image->Timestamp());
  85. // Write width and height to the payload the same way as the real encoder
  86. // does.
  87. WriteFakeVp8(encoded_image->data(), encoded_image->_encodedWidth,
  88. encoded_image->_encodedHeight,
  89. encoded_image->_frameType == VideoFrameType::kVideoFrameKey);
  90. return nullptr;
  91. }
  92. VideoEncoder::EncoderInfo FakeVP8Encoder::GetEncoderInfo() const {
  93. EncoderInfo info;
  94. info.implementation_name = "FakeVp8Encoder";
  95. return info;
  96. }
  97. } // namespace test
  98. } // namespace webrtc