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