philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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_VIDEO_STREAM_DECODER_IMPL_H_ |
| 12 | #define VIDEO_VIDEO_STREAM_DECODER_IMPL_H_ |
| 13 | |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <utility> |
| 17 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 19 | #include "api/field_trials_view.h" |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 20 | #include "api/sequence_checker.h" |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame] | 21 | #include "api/transport/field_trial_based_config.h" |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 22 | #include "api/video/video_stream_decoder.h" |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 23 | #include "modules/video_coding/frame_buffer2.h" |
Rasmus Brandt | c4d253c | 2022-05-25 12:03:35 +0200 | [diff] [blame] | 24 | #include "modules/video_coding/timing/timing.h" |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame] | 25 | #include "rtc_base/memory/always_valid_pointer.h" |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 26 | #include "rtc_base/platform_thread.h" |
Markus Handell | 265931e | 2020-07-10 12:23:48 +0200 | [diff] [blame] | 27 | #include "rtc_base/synchronization/mutex.h" |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 28 | #include "rtc_base/task_queue.h" |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 29 | #include "system_wrappers/include/clock.h" |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 30 | |
| 31 | namespace webrtc { |
| 32 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 33 | class VideoStreamDecoderImpl : public VideoStreamDecoderInterface { |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 34 | public: |
| 35 | VideoStreamDecoderImpl( |
Danil Chapovalov | b703db9 | 2019-04-08 16:59:28 +0200 | [diff] [blame] | 36 | VideoStreamDecoderInterface::Callbacks* callbacks, |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 37 | VideoDecoderFactory* decoder_factory, |
Danil Chapovalov | b703db9 | 2019-04-08 16:59:28 +0200 | [diff] [blame] | 38 | TaskQueueFactory* task_queue_factory, |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame] | 39 | std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings, |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 40 | const FieldTrialsView* field_trials); |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 41 | |
| 42 | ~VideoStreamDecoderImpl() override; |
| 43 | |
philipel | ca18809 | 2021-03-23 12:00:49 +0100 | [diff] [blame] | 44 | void OnFrame(std::unique_ptr<EncodedFrame> frame) override; |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 45 | |
philipel | 781653c | 2019-06-04 17:10:37 +0200 | [diff] [blame] | 46 | void SetMinPlayoutDelay(TimeDelta min_delay) override; |
| 47 | void SetMaxPlayoutDelay(TimeDelta max_delay) override; |
| 48 | |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 49 | private: |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 50 | class DecodeCallbacks : public DecodedImageCallback { |
| 51 | public: |
| 52 | explicit DecodeCallbacks(VideoStreamDecoderImpl* video_stream_decoder_impl); |
| 53 | int32_t Decoded(VideoFrame& decodedImage) override; |
| 54 | int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override; |
| 55 | void Decoded(VideoFrame& decodedImage, |
| 56 | absl::optional<int32_t> decode_time_ms, |
| 57 | absl::optional<uint8_t> qp) override; |
| 58 | |
| 59 | private: |
| 60 | VideoStreamDecoderImpl* const video_stream_decoder_impl_; |
| 61 | }; |
| 62 | |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 63 | enum DecodeResult { |
| 64 | kOk, |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 65 | kOkRequestKeyframe, |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 66 | kDecodeFailure, |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 69 | struct FrameInfo { |
| 70 | int64_t timestamp = -1; |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 71 | int64_t decode_start_time_ms; |
| 72 | int64_t render_time_us; |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 73 | VideoContentType content_type; |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
philipel | ca18809 | 2021-03-23 12:00:49 +0100 | [diff] [blame] | 76 | void SaveFrameInfo(const EncodedFrame& frame) RTC_RUN_ON(bookkeeping_queue_); |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 77 | FrameInfo* GetFrameInfo(int64_t timestamp) RTC_RUN_ON(bookkeeping_queue_); |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 78 | void StartNextDecode() RTC_RUN_ON(bookkeeping_queue_); |
Evan Shrubsole | 3d29efd | 2021-12-07 14:11:45 +0100 | [diff] [blame] | 79 | void OnNextFrameCallback(std::unique_ptr<EncodedFrame> frame) |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 80 | RTC_RUN_ON(bookkeeping_queue_); |
| 81 | void OnDecodedFrameCallback(VideoFrame& decodedImage, // NOLINT |
| 82 | absl::optional<int32_t> decode_time_ms, |
| 83 | absl::optional<uint8_t> qp); |
philipel | 79aab3f | 2018-03-26 14:31:23 +0200 | [diff] [blame] | 84 | |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 85 | VideoDecoder* GetDecoder(int payload_type) RTC_RUN_ON(decode_queue_); |
| 86 | VideoStreamDecoderImpl::DecodeResult DecodeFrame( |
philipel | ca18809 | 2021-03-23 12:00:49 +0100 | [diff] [blame] | 87 | std::unique_ptr<EncodedFrame> frame) RTC_RUN_ON(decode_queue_); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 88 | |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 89 | AlwaysValidPointer<const FieldTrialsView, FieldTrialBasedConfig> |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame] | 90 | field_trials_; |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 91 | VCMTiming timing_; |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 92 | DecodeCallbacks decode_callbacks_; |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 93 | |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 94 | // Some decoders are pipelined so it is not sufficient to save frame info |
| 95 | // for the last frame only. |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 96 | static constexpr int kFrameInfoMemory = 8; |
| 97 | std::array<FrameInfo, kFrameInfoMemory> frame_info_ |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 98 | RTC_GUARDED_BY(bookkeeping_queue_); |
philipel | 3dc4780 | 2020-09-10 15:30:02 +0200 | [diff] [blame] | 99 | int next_frame_info_index_ RTC_GUARDED_BY(bookkeeping_queue_); |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 100 | VideoStreamDecoderInterface::Callbacks* const callbacks_ |
| 101 | RTC_PT_GUARDED_BY(bookkeeping_queue_); |
philipel | 9aa9b8d | 2021-02-15 13:31:29 +0100 | [diff] [blame] | 102 | int64_t last_continuous_frame_id_ RTC_GUARDED_BY(bookkeeping_queue_) = -1; |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 103 | bool keyframe_required_ RTC_GUARDED_BY(bookkeeping_queue_); |
| 104 | |
| 105 | absl::optional<int> current_payload_type_ RTC_GUARDED_BY(decode_queue_); |
| 106 | VideoDecoderFactory* const decoder_factory_ RTC_PT_GUARDED_BY(decode_queue_); |
| 107 | std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings_ |
| 108 | RTC_GUARDED_BY(decode_queue_); |
| 109 | |
Artem Titov | ab30d72 | 2021-07-27 16:22:11 +0200 | [diff] [blame] | 110 | // The `bookkeeping_queue_` use the `frame_buffer_` and also posts tasks to |
| 111 | // the `decode_queue_`. The `decode_queue_` in turn use the `decoder_` to |
| 112 | // decode frames. When the `decoder_` is done it will post back to the |
| 113 | // `bookkeeping_queue_` with the decoded frame. During shutdown we start by |
| 114 | // isolating the `bookkeeping_queue_` from the `decode_queue_`, so now it's |
| 115 | // safe for the `decode_queue_` to be destructed. After that the `decoder_` |
| 116 | // can be destructed, and then the `bookkeeping_queue_`. Finally the |
| 117 | // `frame_buffer_` can be destructed. |
Markus Handell | 265931e | 2020-07-10 12:23:48 +0200 | [diff] [blame] | 118 | Mutex shut_down_mutex_; |
| 119 | bool shut_down_ RTC_GUARDED_BY(shut_down_mutex_); |
philipel | 539f9b3 | 2020-01-09 16:12:25 +0100 | [diff] [blame] | 120 | video_coding::FrameBuffer frame_buffer_ RTC_GUARDED_BY(bookkeeping_queue_); |
| 121 | rtc::TaskQueue bookkeeping_queue_; |
| 122 | std::unique_ptr<VideoDecoder> decoder_ RTC_GUARDED_BY(decode_queue_); |
| 123 | rtc::TaskQueue decode_queue_; |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } // namespace webrtc |
| 127 | |
| 128 | #endif // VIDEO_VIDEO_STREAM_DECODER_IMPL_H_ |