blob: d4de7b6ef2f48c76831753bea6a8b3885999d3ce [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
philipel79aab3f2018-03-26 14:31:23 +020018#include "api/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
30class 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:
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,
67 rtc::Optional<int32_t> decode_time_ms,
68 rtc::Optional<uint8_t> qp) override;
69
philipel97187112018-03-23 10:43:21 +010070 VideoStreamDecoder::Callbacks* const callbacks_
71 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 VCMJitterEstimator jitter_estimator_;
83 VCMTiming timing_;
84 video_coding::FrameBuffer frame_buffer_;
85 video_coding::VideoLayerFrameId last_continuous_id_;
philipel79aab3f2018-03-26 14:31:23 +020086 rtc::Optional<int> current_payload_type_;
87 std::unique_ptr<VideoDecoder> decoder_;
philipel844876d2018-04-05 11:02:54 +020088
philipel6847f9b2018-04-20 15:05:37 +020089 // 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_
philipel844876d2018-04-05 11:02:54 +020093 RTC_GUARDED_BY(bookkeeping_queue_);
philipel6847f9b2018-04-20 15:05:37 +020094 int next_frame_timestamps_index_ RTC_GUARDED_BY(bookkeeping_queue_);
philipel2fee4d62018-03-21 16:52:13 +010095};
96
97} // namespace webrtc
98
99#endif // VIDEO_VIDEO_STREAM_DECODER_IMPL_H_