Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 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 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 14:13:36 | [diff] [blame] | 11 | #ifndef VIDEO_FRAME_ENCODE_METADATA_WRITER_H_ |
| 12 | #define VIDEO_FRAME_ENCODE_METADATA_WRITER_H_ |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 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" |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 14:30:16 | [diff] [blame] | 21 | #include "modules/video_coding/include/video_codec_interface.h" |
Markus Handell | a376518 | 2020-07-08 11:13:32 | [diff] [blame] | 22 | #include "rtc_base/synchronization/mutex.h" |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 14:13:36 | [diff] [blame] | 26 | class FrameEncodeMetadataWriter { |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 27 | public: |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 14:13:36 | [diff] [blame] | 28 | explicit FrameEncodeMetadataWriter(EncodedImageCallback* frame_drop_callback); |
| 29 | ~FrameEncodeMetadataWriter(); |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 30 | |
Niels Möller | 13d1636 | 2021-11-16 10:39:27 | [diff] [blame] | 31 | void OnEncoderInit(const VideoCodec& codec); |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 32 | void OnSetRates(const VideoBitrateAllocation& bitrate_allocation, |
| 33 | uint32_t framerate_fps); |
| 34 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 14:13:36 | [diff] [blame] | 35 | void OnEncodeStarted(const VideoFrame& frame); |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 36 | |
Johannes Kron | 5297075 | 2024-06-26 16:16:02 | [diff] [blame] | 37 | void FillMetadataAndTimingInfo(size_t simulcast_svc_idx, |
| 38 | EncodedImage* encoded_image); |
| 39 | |
Danil Chapovalov | 6d008a8 | 2020-07-22 17:49:36 | [diff] [blame] | 40 | void UpdateBitstream(const CodecSpecificInfo* codec_specific_info, |
| 41 | EncodedImage* encoded_image); |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 14:30:16 | [diff] [blame] | 42 | |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 43 | void Reset(); |
| 44 | |
| 45 | private: |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 46 | // For non-internal-source encoders, returns encode started time and fixes |
| 47 | // capture timestamp for the frame, if corrupted by the encoder. |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 14:13:36 | [diff] [blame] | 48 | absl::optional<int64_t> ExtractEncodeStartTimeAndFillMetadata( |
| 49 | size_t simulcast_svc_idx, |
| 50 | EncodedImage* encoded_image) RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 51 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 14:13:36 | [diff] [blame] | 52 | struct FrameMetadata { |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 53 | uint32_t rtp_timestamp; |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 54 | int64_t encode_start_time_ms; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 14:13:36 | [diff] [blame] | 55 | int64_t ntp_time_ms = 0; |
| 56 | int64_t timestamp_us = 0; |
| 57 | VideoRotation rotation = kVideoRotation_0; |
| 58 | absl::optional<ColorSpace> color_space; |
Johannes Kron | 5297075 | 2024-06-26 16:16:02 | [diff] [blame] | 59 | bool is_steady_state_refresh_frame = false; |
Chen Xing | f00bf42 | 2019-06-20 08:05:55 | [diff] [blame] | 60 | RtpPacketInfos packet_infos; |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 61 | }; |
| 62 | struct TimingFramesLayerInfo { |
| 63 | TimingFramesLayerInfo(); |
| 64 | ~TimingFramesLayerInfo(); |
| 65 | size_t target_bitrate_bytes_per_sec = 0; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 14:13:36 | [diff] [blame] | 66 | std::list<FrameMetadata> frames; |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 67 | }; |
| 68 | |
Markus Handell | a376518 | 2020-07-08 11:13:32 | [diff] [blame] | 69 | Mutex lock_; |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 70 | EncodedImageCallback* const frame_drop_callback_; |
| 71 | VideoCodec codec_settings_ RTC_GUARDED_BY(&lock_); |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 72 | uint32_t framerate_fps_ RTC_GUARDED_BY(&lock_); |
| 73 | |
Zhaoliang Ma | 528e489 | 2021-11-12 05:05:07 | [diff] [blame] | 74 | size_t num_spatial_layers_ RTC_GUARDED_BY(&lock_); |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 75 | // Separate instance for each simulcast stream or spatial layer. |
| 76 | std::vector<TimingFramesLayerInfo> timing_frames_info_ RTC_GUARDED_BY(&lock_); |
| 77 | int64_t last_timing_frame_time_ms_ RTC_GUARDED_BY(&lock_); |
Erik Språng | 6a7baa7 | 2019-02-26 17:31:00 | [diff] [blame] | 78 | size_t reordered_frames_logged_messages_ RTC_GUARDED_BY(&lock_); |
| 79 | size_t stalled_encoder_logged_messages_ RTC_GUARDED_BY(&lock_); |
| 80 | }; |
| 81 | |
| 82 | } // namespace webrtc |
| 83 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 14:13:36 | [diff] [blame] | 84 | #endif // VIDEO_FRAME_ENCODE_METADATA_WRITER_H_ |