blob: 5578e7054756854ba4868d8cd71bf29f91b9e78b [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"
21#include "modules/video_coding/jitter_estimator.h"
22#include "modules/video_coding/timing.h"
philipel844876d2018-04-05 11:02:54 +020023#include "rtc_base/platform_thread.h"
philipel97187112018-03-23 10:43:21 +010024#include "rtc_base/task_queue.h"
25#include "rtc_base/thread_checker.h"
26#include "system_wrappers/include/clock.h"
philipel2fee4d62018-03-21 16:52:13 +010027
28namespace webrtc {
29
Danil Chapovalovb703db92019-04-08 16:59:28 +020030class VideoStreamDecoderImpl : public VideoStreamDecoderInterface,
philipel2fee4d62018-03-21 16:52:13 +010031 private DecodedImageCallback {
32 public:
33 VideoStreamDecoderImpl(
Danil Chapovalovb703db92019-04-08 16:59:28 +020034 VideoStreamDecoderInterface::Callbacks* callbacks,
philipel2fee4d62018-03-21 16:52:13 +010035 VideoDecoderFactory* decoder_factory,
Danil Chapovalovb703db92019-04-08 16:59:28 +020036 TaskQueueFactory* task_queue_factory,
philipel2fee4d62018-03-21 16:52:13 +010037 std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings);
38
39 ~VideoStreamDecoderImpl() override;
40
41 void OnFrame(std::unique_ptr<video_coding::EncodedFrame> frame) override;
42
43 private:
philipel844876d2018-04-05 11:02:54 +020044 enum DecodeResult {
45 kOk,
46 kDecodeFailure,
47 kNoFrame,
48 kNoDecoder,
49 kShutdown,
50 };
51
philipel6847f9b2018-04-20 15:05:37 +020052 struct FrameTimestamps {
53 int64_t timestamp;
54 int64_t decode_start_time_ms;
55 int64_t render_time_us;
56 };
57
philipel79aab3f2018-03-26 14:31:23 +020058 VideoDecoder* GetDecoder(int payload_type);
philipel844876d2018-04-05 11:02:54 +020059 static void DecodeLoop(void* ptr);
60 DecodeResult DecodeNextFrame(int max_wait_time_ms, bool keyframe_required);
philipel79aab3f2018-03-26 14:31:23 +020061
philipel6847f9b2018-04-20 15:05:37 +020062 FrameTimestamps* GetFrameTimestamps(int64_t timestamp);
63
philipel2fee4d62018-03-21 16:52:13 +010064 // Implements DecodedImageCallback interface
65 int32_t Decoded(VideoFrame& decodedImage) override;
66 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
67 void Decoded(VideoFrame& decodedImage,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020068 absl::optional<int32_t> decode_time_ms,
69 absl::optional<uint8_t> qp) override;
philipel2fee4d62018-03-21 16:52:13 +010070
Danil Chapovalovb703db92019-04-08 16:59:28 +020071 VideoStreamDecoderInterface::Callbacks* const callbacks_
philipel97187112018-03-23 10:43:21 +010072 RTC_PT_GUARDED_BY(bookkeeping_queue_);
73 VideoDecoderFactory* const decoder_factory_;
philipel2fee4d62018-03-21 16:52:13 +010074 std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings_;
philipel97187112018-03-23 10:43:21 +010075
76 // The |bookkeeping_queue_| is used to:
77 // - Make |callbacks_|.
78 // - Insert/extract frames from the |frame_buffer_|
79 // - Synchronize with whatever thread that makes the Decoded callback.
80 rtc::TaskQueue bookkeeping_queue_;
81
philipel844876d2018-04-05 11:02:54 +020082 rtc::PlatformThread decode_thread_;
philipel97187112018-03-23 10:43:21 +010083 VCMJitterEstimator jitter_estimator_;
84 VCMTiming timing_;
85 video_coding::FrameBuffer frame_buffer_;
86 video_coding::VideoLayerFrameId last_continuous_id_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020087 absl::optional<int> current_payload_type_;
philipel79aab3f2018-03-26 14:31:23 +020088 std::unique_ptr<VideoDecoder> decoder_;
philipel844876d2018-04-05 11:02:54 +020089
philipel6847f9b2018-04-20 15:05:37 +020090 // 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_
philipel844876d2018-04-05 11:02:54 +020094 RTC_GUARDED_BY(bookkeeping_queue_);
philipel6847f9b2018-04-20 15:05:37 +020095 int next_frame_timestamps_index_ RTC_GUARDED_BY(bookkeeping_queue_);
philipel2fee4d62018-03-21 16:52:13 +010096};
97
98} // namespace webrtc
99
100#endif // VIDEO_VIDEO_STREAM_DECODER_IMPL_H_