blob: c439be5c1622a5e43436248b92b84dbbd6e15c23 [file] [log] [blame]
philipel2fee4d62018-03-21 16:52:13 +01001/*
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 16:52:13 +010014#include <map>
15#include <memory>
16#include <utility>
17
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020018#include "absl/types/optional.h"
philipel2fee4d62018-03-21 16:52:13 +010019#include "api/video/video_stream_decoder.h"
philipel97187112018-03-23 10:43:21 +010020#include "modules/video_coding/frame_buffer2.h"
philipel97187112018-03-23 10:43:21 +010021#include "modules/video_coding/timing.h"
philipel844876d2018-04-05 11:02:54 +020022#include "rtc_base/platform_thread.h"
philipel97187112018-03-23 10:43:21 +010023#include "rtc_base/task_queue.h"
24#include "rtc_base/thread_checker.h"
25#include "system_wrappers/include/clock.h"
philipel2fee4d62018-03-21 16:52:13 +010026
27namespace webrtc {
28
Danil Chapovalovb703db92019-04-08 16:59:28 +020029class VideoStreamDecoderImpl : public VideoStreamDecoderInterface,
philipel2fee4d62018-03-21 16:52:13 +010030 private DecodedImageCallback {
31 public:
32 VideoStreamDecoderImpl(
Danil Chapovalovb703db92019-04-08 16:59:28 +020033 VideoStreamDecoderInterface::Callbacks* callbacks,
philipel2fee4d62018-03-21 16:52:13 +010034 VideoDecoderFactory* decoder_factory,
Danil Chapovalovb703db92019-04-08 16:59:28 +020035 TaskQueueFactory* task_queue_factory,
philipel2fee4d62018-03-21 16:52:13 +010036 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
philipel781653c2019-06-04 17:10:37 +020042 void SetMinPlayoutDelay(TimeDelta min_delay) override;
43 void SetMaxPlayoutDelay(TimeDelta max_delay) override;
44
philipel2fee4d62018-03-21 16:52:13 +010045 private:
philipel844876d2018-04-05 11:02:54 +020046 enum DecodeResult {
47 kOk,
48 kDecodeFailure,
49 kNoFrame,
50 kNoDecoder,
51 kShutdown,
52 };
53
philipel6847f9b2018-04-20 15:05:37 +020054 struct FrameTimestamps {
55 int64_t timestamp;
56 int64_t decode_start_time_ms;
57 int64_t render_time_us;
58 };
59
philipel79aab3f2018-03-26 14:31:23 +020060 VideoDecoder* GetDecoder(int payload_type);
philipel844876d2018-04-05 11:02:54 +020061 static void DecodeLoop(void* ptr);
62 DecodeResult DecodeNextFrame(int max_wait_time_ms, bool keyframe_required);
philipel79aab3f2018-03-26 14:31:23 +020063
philipel6847f9b2018-04-20 15:05:37 +020064 FrameTimestamps* GetFrameTimestamps(int64_t timestamp);
65
philipel2fee4d62018-03-21 16:52:13 +010066 // 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 Chapovalovb9b146c2018-06-15 12:28:07 +020070 absl::optional<int32_t> decode_time_ms,
71 absl::optional<uint8_t> qp) override;
philipel2fee4d62018-03-21 16:52:13 +010072
Danil Chapovalovb703db92019-04-08 16:59:28 +020073 VideoStreamDecoderInterface::Callbacks* const callbacks_
philipel97187112018-03-23 10:43:21 +010074 RTC_PT_GUARDED_BY(bookkeeping_queue_);
75 VideoDecoderFactory* const decoder_factory_;
philipel2fee4d62018-03-21 16:52:13 +010076 std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings_;
philipel97187112018-03-23 10:43:21 +010077
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
philipel844876d2018-04-05 11:02:54 +020084 rtc::PlatformThread decode_thread_;
philipel97187112018-03-23 10:43:21 +010085 VCMTiming timing_;
86 video_coding::FrameBuffer frame_buffer_;
87 video_coding::VideoLayerFrameId last_continuous_id_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020088 absl::optional<int> current_payload_type_;
philipel79aab3f2018-03-26 14:31:23 +020089 std::unique_ptr<VideoDecoder> decoder_;
philipel844876d2018-04-05 11:02:54 +020090
philipel6847f9b2018-04-20 15:05:37 +020091 // 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_
philipel844876d2018-04-05 11:02:54 +020095 RTC_GUARDED_BY(bookkeeping_queue_);
philipel6847f9b2018-04-20 15:05:37 +020096 int next_frame_timestamps_index_ RTC_GUARDED_BY(bookkeeping_queue_);
philipel2fee4d62018-03-21 16:52:13 +010097};
98
99} // namespace webrtc
100
101#endif // VIDEO_VIDEO_STREAM_DECODER_IMPL_H_