blob: 2cb65135742129e6398498c6b2b067940a529542 [file] [log] [blame]
philipel2fee4d62018-03-21 15:52:131/*
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
philipel2fee4d62018-03-21 15:52:1314#include <map>
15#include <memory>
16#include <utility>
17
Danil Chapovalovb9b146c2018-06-15 10:28:0718#include "absl/types/optional.h"
Artem Titovd15a5752021-02-10 13:31:2419#include "api/sequence_checker.h"
philipel2fee4d62018-03-21 15:52:1320#include "api/video/video_stream_decoder.h"
philipel97187112018-03-23 09:43:2121#include "modules/video_coding/frame_buffer2.h"
philipel97187112018-03-23 09:43:2122#include "modules/video_coding/timing.h"
philipel844876d2018-04-05 09:02:5423#include "rtc_base/platform_thread.h"
Markus Handell265931e2020-07-10 10:23:4824#include "rtc_base/synchronization/mutex.h"
philipel97187112018-03-23 09:43:2125#include "rtc_base/task_queue.h"
philipel97187112018-03-23 09:43:2126#include "system_wrappers/include/clock.h"
philipel2fee4d62018-03-21 15:52:1327
28namespace webrtc {
29
philipel539f9b32020-01-09 15:12:2530class VideoStreamDecoderImpl : public VideoStreamDecoderInterface {
philipel2fee4d62018-03-21 15:52:1331 public:
32 VideoStreamDecoderImpl(
Danil Chapovalovb703db92019-04-08 14:59:2833 VideoStreamDecoderInterface::Callbacks* callbacks,
philipel2fee4d62018-03-21 15:52:1334 VideoDecoderFactory* decoder_factory,
Danil Chapovalovb703db92019-04-08 14:59:2835 TaskQueueFactory* task_queue_factory,
philipel2fee4d62018-03-21 15:52:1336 std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings);
37
38 ~VideoStreamDecoderImpl() override;
39
philipelca188092021-03-23 11:00:4940 void OnFrame(std::unique_ptr<EncodedFrame> frame) override;
philipel2fee4d62018-03-21 15:52:1341
philipel781653c2019-06-04 15:10:3742 void SetMinPlayoutDelay(TimeDelta min_delay) override;
43 void SetMaxPlayoutDelay(TimeDelta max_delay) override;
44
philipel2fee4d62018-03-21 15:52:1345 private:
philipel539f9b32020-01-09 15:12:2546 class DecodeCallbacks : public DecodedImageCallback {
47 public:
48 explicit DecodeCallbacks(VideoStreamDecoderImpl* video_stream_decoder_impl);
49 int32_t Decoded(VideoFrame& decodedImage) override;
50 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
51 void Decoded(VideoFrame& decodedImage,
52 absl::optional<int32_t> decode_time_ms,
53 absl::optional<uint8_t> qp) override;
54
55 private:
56 VideoStreamDecoderImpl* const video_stream_decoder_impl_;
57 };
58
philipel844876d2018-04-05 09:02:5459 enum DecodeResult {
60 kOk,
philipel539f9b32020-01-09 15:12:2561 kOkRequestKeyframe,
philipel844876d2018-04-05 09:02:5462 kDecodeFailure,
philipel844876d2018-04-05 09:02:5463 };
64
philipel3dc47802020-09-10 13:30:0265 struct FrameInfo {
66 int64_t timestamp = -1;
philipel6847f9b2018-04-20 13:05:3767 int64_t decode_start_time_ms;
68 int64_t render_time_us;
philipel3dc47802020-09-10 13:30:0269 VideoContentType content_type;
philipel6847f9b2018-04-20 13:05:3770 };
71
philipelca188092021-03-23 11:00:4972 void SaveFrameInfo(const EncodedFrame& frame) RTC_RUN_ON(bookkeeping_queue_);
philipel3dc47802020-09-10 13:30:0273 FrameInfo* GetFrameInfo(int64_t timestamp) RTC_RUN_ON(bookkeeping_queue_);
philipel539f9b32020-01-09 15:12:2574 void StartNextDecode() RTC_RUN_ON(bookkeeping_queue_);
philipelca188092021-03-23 11:00:4975 void OnNextFrameCallback(std::unique_ptr<EncodedFrame> frame,
philipel539f9b32020-01-09 15:12:2576 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);
philipel79aab3f2018-03-26 12:31:2381
philipel539f9b32020-01-09 15:12:2582 VideoDecoder* GetDecoder(int payload_type) RTC_RUN_ON(decode_queue_);
83 VideoStreamDecoderImpl::DecodeResult DecodeFrame(
philipelca188092021-03-23 11:00:4984 std::unique_ptr<EncodedFrame> frame) RTC_RUN_ON(decode_queue_);
philipel6847f9b2018-04-20 13:05:3785
philipel97187112018-03-23 09:43:2186 VCMTiming timing_;
philipel539f9b32020-01-09 15:12:2587 DecodeCallbacks decode_callbacks_;
philipel844876d2018-04-05 09:02:5488
philipel6847f9b2018-04-20 13:05:3789 // Some decoders are pipelined so it is not sufficient to save frame info
90 // for the last frame only.
philipel3dc47802020-09-10 13:30:0291 static constexpr int kFrameInfoMemory = 8;
92 std::array<FrameInfo, kFrameInfoMemory> frame_info_
philipel844876d2018-04-05 09:02:5493 RTC_GUARDED_BY(bookkeeping_queue_);
philipel3dc47802020-09-10 13:30:0294 int next_frame_info_index_ RTC_GUARDED_BY(bookkeeping_queue_);
philipel539f9b32020-01-09 15:12:2595 VideoStreamDecoderInterface::Callbacks* const callbacks_
96 RTC_PT_GUARDED_BY(bookkeeping_queue_);
philipel9aa9b8d2021-02-15 12:31:2997 int64_t last_continuous_frame_id_ RTC_GUARDED_BY(bookkeeping_queue_) = -1;
philipel539f9b32020-01-09 15:12:2598 bool keyframe_required_ RTC_GUARDED_BY(bookkeeping_queue_);
99
100 absl::optional<int> current_payload_type_ RTC_GUARDED_BY(decode_queue_);
101 VideoDecoderFactory* const decoder_factory_ RTC_PT_GUARDED_BY(decode_queue_);
102 std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings_
103 RTC_GUARDED_BY(decode_queue_);
104
Artem Titovab30d722021-07-27 14:22:11105 // The `bookkeeping_queue_` use the `frame_buffer_` and also posts tasks to
106 // the `decode_queue_`. The `decode_queue_` in turn use the `decoder_` to
107 // decode frames. When the `decoder_` is done it will post back to the
108 // `bookkeeping_queue_` with the decoded frame. During shutdown we start by
109 // isolating the `bookkeeping_queue_` from the `decode_queue_`, so now it's
110 // safe for the `decode_queue_` to be destructed. After that the `decoder_`
111 // can be destructed, and then the `bookkeeping_queue_`. Finally the
112 // `frame_buffer_` can be destructed.
Markus Handell265931e2020-07-10 10:23:48113 Mutex shut_down_mutex_;
114 bool shut_down_ RTC_GUARDED_BY(shut_down_mutex_);
philipel539f9b32020-01-09 15:12:25115 video_coding::FrameBuffer frame_buffer_ RTC_GUARDED_BY(bookkeeping_queue_);
116 rtc::TaskQueue bookkeeping_queue_;
117 std::unique_ptr<VideoDecoder> decoder_ RTC_GUARDED_BY(decode_queue_);
118 rtc::TaskQueue decode_queue_;
philipel2fee4d62018-03-21 15:52:13119};
120
121} // namespace webrtc
122
123#endif // VIDEO_VIDEO_STREAM_DECODER_IMPL_H_