blob: f92a33b71efd69acc04f45b0556ff1bb60b32407 [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
11#ifndef VIDEO_FRAME_ENCODE_TIMER_H_
12#define VIDEO_FRAME_ENCODE_TIMER_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 "rtc_base/critical_section.h"
22
23namespace webrtc {
24
25class FrameEncodeTimer {
26 public:
27 explicit FrameEncodeTimer(EncodedImageCallback* frame_drop_callback);
28 ~FrameEncodeTimer();
29
30 void OnEncoderInit(const VideoCodec& codec, bool internal_source);
31 void OnSetRates(const VideoBitrateAllocation& bitrate_allocation,
32 uint32_t framerate_fps);
33
34 void OnEncodeStarted(uint32_t rtp_timestamp, int64_t capture_time_ms);
35
36 void FillTimingInfo(size_t simulcast_svc_idx,
37 EncodedImage* encoded_image,
38 int64_t encode_done_ms);
39 void Reset();
40
41 private:
42 size_t NumSpatialLayers() const RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
43
44 // For non-internal-source encoders, returns encode started time and fixes
45 // capture timestamp for the frame, if corrupted by the encoder.
46 absl::optional<int64_t> ExtractEncodeStartTime(size_t simulcast_svc_idx,
47 EncodedImage* encoded_image)
48 RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
49
50 struct EncodeStartTimeRecord {
51 EncodeStartTimeRecord(uint32_t timestamp,
52 int64_t capture_time,
53 int64_t encode_start_time)
54 : rtp_timestamp(timestamp),
55 capture_time_ms(capture_time),
56 encode_start_time_ms(encode_start_time) {}
57 uint32_t rtp_timestamp;
58 int64_t capture_time_ms;
59 int64_t encode_start_time_ms;
60 };
61 struct TimingFramesLayerInfo {
62 TimingFramesLayerInfo();
63 ~TimingFramesLayerInfo();
64 size_t target_bitrate_bytes_per_sec = 0;
65 std::list<EncodeStartTimeRecord> encode_start_list;
66 };
67
68 rtc::CriticalSection lock_;
69 EncodedImageCallback* const frame_drop_callback_;
70 VideoCodec codec_settings_ RTC_GUARDED_BY(&lock_);
71 bool internal_source_ RTC_GUARDED_BY(&lock_);
72 uint32_t framerate_fps_ RTC_GUARDED_BY(&lock_);
73
74 // Separate instance for each simulcast stream or spatial layer.
75 std::vector<TimingFramesLayerInfo> timing_frames_info_ RTC_GUARDED_BY(&lock_);
76 int64_t last_timing_frame_time_ms_ RTC_GUARDED_BY(&lock_);
77 size_t incorrect_capture_time_logged_messages_ RTC_GUARDED_BY(&lock_);
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
84#endif // VIDEO_FRAME_ENCODE_TIMER_H_