blob: 4ee2d7eec73f89cbd1df06fd83bf9ae1ea8b50b2 [file] [log] [blame]
Erik Språng6a7baa72019-02-26 18:31:00 +01001/*
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 Nikolaevskiy2ebf5232019-05-13 16:13:36 +020011#ifndef VIDEO_FRAME_ENCODE_METADATA_WRITER_H_
12#define VIDEO_FRAME_ENCODE_METADATA_WRITER_H_
Erik Språng6a7baa72019-02-26 18:31:00 +010013
14#include <list>
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020015#include <memory>
Erik Språng6a7baa72019-02-26 18:31:00 +010016#include <vector>
17
18#include "absl/types/optional.h"
19#include "api/video/encoded_image.h"
20#include "api/video_codecs/video_codec.h"
21#include "api/video_codecs/video_encoder.h"
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020022#include "modules/video_coding/include/video_codec_interface.h"
Erik Språng6a7baa72019-02-26 18:31:00 +010023#include "rtc_base/critical_section.h"
24
25namespace webrtc {
26
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020027class FrameEncodeMetadataWriter {
Erik Språng6a7baa72019-02-26 18:31:00 +010028 public:
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020029 explicit FrameEncodeMetadataWriter(EncodedImageCallback* frame_drop_callback);
30 ~FrameEncodeMetadataWriter();
Erik Språng6a7baa72019-02-26 18:31:00 +010031
32 void OnEncoderInit(const VideoCodec& codec, bool internal_source);
33 void OnSetRates(const VideoBitrateAllocation& bitrate_allocation,
34 uint32_t framerate_fps);
35
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020036 void OnEncodeStarted(const VideoFrame& frame);
Erik Språng6a7baa72019-02-26 18:31:00 +010037
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020038 void FillTimingInfo(size_t simulcast_svc_idx, EncodedImage* encoded_image);
Mirta Dvornicic28f0eb22019-05-28 16:30:16 +020039
40 std::unique_ptr<RTPFragmentationHeader> UpdateBitstream(
41 const CodecSpecificInfo* codec_specific_info,
42 const RTPFragmentationHeader* fragmentation,
43 EncodedImage* encoded_image);
44
Erik Språng6a7baa72019-02-26 18:31:00 +010045 void Reset();
46
47 private:
48 size_t NumSpatialLayers() const RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
49
50 // For non-internal-source encoders, returns encode started time and fixes
51 // capture timestamp for the frame, if corrupted by the encoder.
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020052 absl::optional<int64_t> ExtractEncodeStartTimeAndFillMetadata(
53 size_t simulcast_svc_idx,
54 EncodedImage* encoded_image) RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
Erik Språng6a7baa72019-02-26 18:31:00 +010055
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020056 struct FrameMetadata {
Erik Språng6a7baa72019-02-26 18:31:00 +010057 uint32_t rtp_timestamp;
Erik Språng6a7baa72019-02-26 18:31:00 +010058 int64_t encode_start_time_ms;
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020059 int64_t ntp_time_ms = 0;
60 int64_t timestamp_us = 0;
61 VideoRotation rotation = kVideoRotation_0;
62 absl::optional<ColorSpace> color_space;
Chen Xingf00bf422019-06-20 10:05:55 +020063 RtpPacketInfos packet_infos;
Erik Språng6a7baa72019-02-26 18:31:00 +010064 };
65 struct TimingFramesLayerInfo {
66 TimingFramesLayerInfo();
67 ~TimingFramesLayerInfo();
68 size_t target_bitrate_bytes_per_sec = 0;
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020069 std::list<FrameMetadata> frames;
Erik Språng6a7baa72019-02-26 18:31:00 +010070 };
71
72 rtc::CriticalSection lock_;
73 EncodedImageCallback* const frame_drop_callback_;
74 VideoCodec codec_settings_ RTC_GUARDED_BY(&lock_);
75 bool internal_source_ RTC_GUARDED_BY(&lock_);
76 uint32_t framerate_fps_ RTC_GUARDED_BY(&lock_);
77
78 // Separate instance for each simulcast stream or spatial layer.
79 std::vector<TimingFramesLayerInfo> timing_frames_info_ RTC_GUARDED_BY(&lock_);
80 int64_t last_timing_frame_time_ms_ RTC_GUARDED_BY(&lock_);
Erik Språng6a7baa72019-02-26 18:31:00 +010081 size_t reordered_frames_logged_messages_ RTC_GUARDED_BY(&lock_);
82 size_t stalled_encoder_logged_messages_ RTC_GUARDED_BY(&lock_);
83};
84
85} // namespace webrtc
86
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020087#endif // VIDEO_FRAME_ENCODE_METADATA_WRITER_H_