blob: b7a5e41d72c359aaa7e23fde746575f57ebaf316 [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
42 private:
philipel844876d2018-04-05 11:02:54 +020043 enum DecodeResult {
44 kOk,
45 kDecodeFailure,
46 kNoFrame,
47 kNoDecoder,
48 kShutdown,
49 };
50
philipel6847f9b2018-04-20 15:05:37 +020051 struct FrameTimestamps {
52 int64_t timestamp;
53 int64_t decode_start_time_ms;
54 int64_t render_time_us;
55 };
56
philipel79aab3f2018-03-26 14:31:23 +020057 VideoDecoder* GetDecoder(int payload_type);
philipel844876d2018-04-05 11:02:54 +020058 static void DecodeLoop(void* ptr);
59 DecodeResult DecodeNextFrame(int max_wait_time_ms, bool keyframe_required);
philipel79aab3f2018-03-26 14:31:23 +020060
philipel6847f9b2018-04-20 15:05:37 +020061 FrameTimestamps* GetFrameTimestamps(int64_t timestamp);
62
philipel2fee4d62018-03-21 16:52:13 +010063 // 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 Chapovalovb9b146c2018-06-15 12:28:07 +020067 absl::optional<int32_t> decode_time_ms,
68 absl::optional<uint8_t> qp) override;
philipel2fee4d62018-03-21 16:52:13 +010069
Danil Chapovalovb703db92019-04-08 16:59:28 +020070 VideoStreamDecoderInterface::Callbacks* const callbacks_
philipel97187112018-03-23 10:43:21 +010071 RTC_PT_GUARDED_BY(bookkeeping_queue_);
72 VideoDecoderFactory* const decoder_factory_;
philipel2fee4d62018-03-21 16:52:13 +010073 std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings_;
philipel97187112018-03-23 10:43:21 +010074
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
philipel844876d2018-04-05 11:02:54 +020081 rtc::PlatformThread decode_thread_;
philipel97187112018-03-23 10:43:21 +010082 VCMTiming timing_;
83 video_coding::FrameBuffer frame_buffer_;
84 video_coding::VideoLayerFrameId last_continuous_id_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020085 absl::optional<int> current_payload_type_;
philipel79aab3f2018-03-26 14:31:23 +020086 std::unique_ptr<VideoDecoder> decoder_;
philipel844876d2018-04-05 11:02:54 +020087
philipel6847f9b2018-04-20 15:05:37 +020088 // Some decoders are pipelined so it is not sufficient to save frame info
89 // for the last frame only.
90 static constexpr int kFrameTimestampsMemory = 8;
91 std::array<FrameTimestamps, kFrameTimestampsMemory> frame_timestamps_
philipel844876d2018-04-05 11:02:54 +020092 RTC_GUARDED_BY(bookkeeping_queue_);
philipel6847f9b2018-04-20 15:05:37 +020093 int next_frame_timestamps_index_ RTC_GUARDED_BY(bookkeeping_queue_);
philipel2fee4d62018-03-21 16:52:13 +010094};
95
96} // namespace webrtc
97
98#endif // VIDEO_VIDEO_STREAM_DECODER_IMPL_H_