frame_encode_metadata_writer.h 2.9 KB

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