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