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 | |
Danil Chapovalov | b703db9 | 2019-04-08 16:59:28 +0200 | [diff] [blame] | 29 | class VideoStreamDecoderImpl : public VideoStreamDecoderInterface, |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 30 | private DecodedImageCallback { |
| 31 | public: |
| 32 | VideoStreamDecoderImpl( |
Danil Chapovalov | b703db9 | 2019-04-08 16:59:28 +0200 | [diff] [blame] | 33 | VideoStreamDecoderInterface::Callbacks* callbacks, |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 34 | VideoDecoderFactory* decoder_factory, |
Danil Chapovalov | b703db9 | 2019-04-08 16:59:28 +0200 | [diff] [blame] | 35 | TaskQueueFactory* task_queue_factory, |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 36 | std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings); |
| 37 | |
| 38 | ~VideoStreamDecoderImpl() override; |
| 39 | |
| 40 | void OnFrame(std::unique_ptr<video_coding::EncodedFrame> frame) override; |
| 41 | |
philipel | 781653c | 2019-06-04 17:10:37 +0200 | [diff] [blame] | 42 | void SetMinPlayoutDelay(TimeDelta min_delay) override; |
| 43 | void SetMaxPlayoutDelay(TimeDelta max_delay) override; |
| 44 | |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 45 | private: |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 46 | enum DecodeResult { |
| 47 | kOk, |
| 48 | kDecodeFailure, |
| 49 | kNoFrame, |
| 50 | kNoDecoder, |
| 51 | kShutdown, |
| 52 | }; |
| 53 | |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 54 | struct FrameTimestamps { |
| 55 | int64_t timestamp; |
| 56 | int64_t decode_start_time_ms; |
| 57 | int64_t render_time_us; |
| 58 | }; |
| 59 | |
philipel | 79aab3f | 2018-03-26 14:31:23 +0200 | [diff] [blame] | 60 | VideoDecoder* GetDecoder(int payload_type); |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 61 | static void DecodeLoop(void* ptr); |
| 62 | DecodeResult DecodeNextFrame(int max_wait_time_ms, bool keyframe_required); |
philipel | 79aab3f | 2018-03-26 14:31:23 +0200 | [diff] [blame] | 63 | |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 64 | FrameTimestamps* GetFrameTimestamps(int64_t timestamp); |
| 65 | |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 66 | // Implements DecodedImageCallback interface |
| 67 | int32_t Decoded(VideoFrame& decodedImage) override; |
| 68 | int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override; |
| 69 | void Decoded(VideoFrame& decodedImage, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 70 | absl::optional<int32_t> decode_time_ms, |
| 71 | absl::optional<uint8_t> qp) override; |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 72 | |
Danil Chapovalov | b703db9 | 2019-04-08 16:59:28 +0200 | [diff] [blame] | 73 | VideoStreamDecoderInterface::Callbacks* const callbacks_ |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 74 | RTC_PT_GUARDED_BY(bookkeeping_queue_); |
| 75 | VideoDecoderFactory* const decoder_factory_; |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 76 | std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings_; |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 77 | |
| 78 | // The |bookkeeping_queue_| is used to: |
| 79 | // - Make |callbacks_|. |
| 80 | // - Insert/extract frames from the |frame_buffer_| |
| 81 | // - Synchronize with whatever thread that makes the Decoded callback. |
| 82 | rtc::TaskQueue bookkeeping_queue_; |
| 83 | |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 84 | rtc::PlatformThread decode_thread_; |
philipel | 9718711 | 2018-03-23 10:43:21 +0100 | [diff] [blame] | 85 | VCMTiming timing_; |
| 86 | video_coding::FrameBuffer frame_buffer_; |
| 87 | video_coding::VideoLayerFrameId last_continuous_id_; |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 88 | absl::optional<int> current_payload_type_; |
philipel | 79aab3f | 2018-03-26 14:31:23 +0200 | [diff] [blame] | 89 | std::unique_ptr<VideoDecoder> decoder_; |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 90 | |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 91 | // Some decoders are pipelined so it is not sufficient to save frame info |
| 92 | // for the last frame only. |
| 93 | static constexpr int kFrameTimestampsMemory = 8; |
| 94 | std::array<FrameTimestamps, kFrameTimestampsMemory> frame_timestamps_ |
philipel | 844876d | 2018-04-05 11:02:54 +0200 | [diff] [blame] | 95 | RTC_GUARDED_BY(bookkeeping_queue_); |
philipel | 6847f9b | 2018-04-20 15:05:37 +0200 | [diff] [blame] | 96 | int next_frame_timestamps_index_ RTC_GUARDED_BY(bookkeeping_queue_); |
philipel | 2fee4d6 | 2018-03-21 16:52:13 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace webrtc |
| 100 | |
| 101 | #endif // VIDEO_VIDEO_STREAM_DECODER_IMPL_H_ |