frame_encode_metadata_writer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2019 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. #ifndef VIDEO_FRAME_ENCODE_METADATA_WRITER_H_
  11. #define VIDEO_FRAME_ENCODE_METADATA_WRITER_H_
  12. #include <list>
  13. #include <memory>
  14. #include <vector>
  15. #include "absl/types/optional.h"
  16. #include "api/video/encoded_image.h"
  17. #include "api/video_codecs/video_codec.h"
  18. #include "api/video_codecs/video_encoder.h"
  19. #include "modules/video_coding/include/video_codec_interface.h"
  20. #include "rtc_base/critical_section.h"
  21. namespace webrtc {
  22. class FrameEncodeMetadataWriter {
  23. public:
  24. explicit FrameEncodeMetadataWriter(EncodedImageCallback* frame_drop_callback);
  25. ~FrameEncodeMetadataWriter();
  26. void OnEncoderInit(const VideoCodec& codec, bool internal_source);
  27. void OnSetRates(const VideoBitrateAllocation& bitrate_allocation,
  28. uint32_t framerate_fps);
  29. void OnEncodeStarted(const VideoFrame& frame);
  30. void FillTimingInfo(size_t simulcast_svc_idx, EncodedImage* encoded_image);
  31. std::unique_ptr<RTPFragmentationHeader> UpdateBitstream(
  32. const CodecSpecificInfo* codec_specific_info,
  33. const RTPFragmentationHeader* fragmentation,
  34. EncodedImage* encoded_image);
  35. void Reset();
  36. private:
  37. size_t NumSpatialLayers() const RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
  38. // For non-internal-source encoders, returns encode started time and fixes
  39. // capture timestamp for the frame, if corrupted by the encoder.
  40. absl::optional<int64_t> ExtractEncodeStartTimeAndFillMetadata(
  41. size_t simulcast_svc_idx,
  42. EncodedImage* encoded_image) RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
  43. struct FrameMetadata {
  44. uint32_t rtp_timestamp;
  45. int64_t encode_start_time_ms;
  46. int64_t ntp_time_ms = 0;
  47. int64_t timestamp_us = 0;
  48. VideoRotation rotation = kVideoRotation_0;
  49. absl::optional<ColorSpace> color_space;
  50. RtpPacketInfos packet_infos;
  51. };
  52. struct TimingFramesLayerInfo {
  53. TimingFramesLayerInfo();
  54. ~TimingFramesLayerInfo();
  55. size_t target_bitrate_bytes_per_sec = 0;
  56. std::list<FrameMetadata> frames;
  57. };
  58. rtc::CriticalSection lock_;
  59. EncodedImageCallback* const frame_drop_callback_;
  60. VideoCodec codec_settings_ RTC_GUARDED_BY(&lock_);
  61. bool internal_source_ RTC_GUARDED_BY(&lock_);
  62. uint32_t framerate_fps_ RTC_GUARDED_BY(&lock_);
  63. // Separate instance for each simulcast stream or spatial layer.
  64. std::vector<TimingFramesLayerInfo> timing_frames_info_ RTC_GUARDED_BY(&lock_);
  65. int64_t last_timing_frame_time_ms_ RTC_GUARDED_BY(&lock_);
  66. size_t reordered_frames_logged_messages_ RTC_GUARDED_BY(&lock_);
  67. size_t stalled_encoder_logged_messages_ RTC_GUARDED_BY(&lock_);
  68. };
  69. } // namespace webrtc
  70. #endif // VIDEO_FRAME_ENCODE_METADATA_WRITER_H_