Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [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 16:13:36 +0200 | [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 18:31:00 +0100 | [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" |
| 21 | #include "rtc_base/critical_section.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 25 | class FrameEncodeMetadataWriter { |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 26 | public: |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 27 | explicit FrameEncodeMetadataWriter(EncodedImageCallback* frame_drop_callback); |
| 28 | ~FrameEncodeMetadataWriter(); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 29 | |
| 30 | void OnEncoderInit(const VideoCodec& codec, bool internal_source); |
| 31 | void OnSetRates(const VideoBitrateAllocation& bitrate_allocation, |
| 32 | uint32_t framerate_fps); |
| 33 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 34 | void OnEncodeStarted(const VideoFrame& frame); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 35 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 36 | void FillTimingInfo(size_t simulcast_svc_idx, EncodedImage* encoded_image); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 37 | void Reset(); |
| 38 | |
| 39 | private: |
| 40 | size_t NumSpatialLayers() const RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 41 | |
| 42 | // For non-internal-source encoders, returns encode started time and fixes |
| 43 | // capture timestamp for the frame, if corrupted by the encoder. |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 44 | absl::optional<int64_t> ExtractEncodeStartTimeAndFillMetadata( |
| 45 | size_t simulcast_svc_idx, |
| 46 | EncodedImage* encoded_image) RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 47 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 48 | struct FrameMetadata { |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 49 | uint32_t rtp_timestamp; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 50 | int64_t encode_start_time_ms; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 51 | int64_t ntp_time_ms = 0; |
| 52 | int64_t timestamp_us = 0; |
| 53 | VideoRotation rotation = kVideoRotation_0; |
| 54 | absl::optional<ColorSpace> color_space; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 55 | }; |
| 56 | struct TimingFramesLayerInfo { |
| 57 | TimingFramesLayerInfo(); |
| 58 | ~TimingFramesLayerInfo(); |
| 59 | size_t target_bitrate_bytes_per_sec = 0; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 60 | std::list<FrameMetadata> frames; |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | rtc::CriticalSection lock_; |
| 64 | EncodedImageCallback* const frame_drop_callback_; |
| 65 | VideoCodec codec_settings_ RTC_GUARDED_BY(&lock_); |
| 66 | bool internal_source_ RTC_GUARDED_BY(&lock_); |
| 67 | uint32_t framerate_fps_ RTC_GUARDED_BY(&lock_); |
| 68 | |
| 69 | // Separate instance for each simulcast stream or spatial layer. |
| 70 | std::vector<TimingFramesLayerInfo> timing_frames_info_ RTC_GUARDED_BY(&lock_); |
| 71 | int64_t last_timing_frame_time_ms_ RTC_GUARDED_BY(&lock_); |
Erik Språng | 6a7baa7 | 2019-02-26 18:31:00 +0100 | [diff] [blame] | 72 | size_t reordered_frames_logged_messages_ RTC_GUARDED_BY(&lock_); |
| 73 | size_t stalled_encoder_logged_messages_ RTC_GUARDED_BY(&lock_); |
| 74 | }; |
| 75 | |
| 76 | } // namespace webrtc |
| 77 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame^] | 78 | #endif // VIDEO_FRAME_ENCODE_METADATA_WRITER_H_ |