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