blob: 42c1b946d25c25eba10cd595624ce4c34de41011 [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
philipel79aab3f2018-03-26 14:31:23 +020051 VideoDecoder* GetDecoder(int payload_type);
philipel844876d2018-04-05 11:02:54 +020052 static void DecodeLoop(void* ptr);
53 DecodeResult DecodeNextFrame(int max_wait_time_ms, bool keyframe_required);
philipel79aab3f2018-03-26 14:31:23 +020054
philipel2fee4d62018-03-21 16:52:13 +010055 // Implements DecodedImageCallback interface
56 int32_t Decoded(VideoFrame& decodedImage) override;
57 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
58 void Decoded(VideoFrame& decodedImage,
59 rtc::Optional<int32_t> decode_time_ms,
60 rtc::Optional<uint8_t> qp) override;
61
philipel97187112018-03-23 10:43:21 +010062 VideoStreamDecoder::Callbacks* const callbacks_
63 RTC_PT_GUARDED_BY(bookkeeping_queue_);
64 VideoDecoderFactory* const decoder_factory_;
philipel2fee4d62018-03-21 16:52:13 +010065 std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings_;
philipel97187112018-03-23 10:43:21 +010066
67 // The |bookkeeping_queue_| is used to:
68 // - Make |callbacks_|.
69 // - Insert/extract frames from the |frame_buffer_|
70 // - Synchronize with whatever thread that makes the Decoded callback.
71 rtc::TaskQueue bookkeeping_queue_;
72
philipel844876d2018-04-05 11:02:54 +020073 rtc::PlatformThread decode_thread_;
philipel97187112018-03-23 10:43:21 +010074 VCMJitterEstimator jitter_estimator_;
75 VCMTiming timing_;
76 video_coding::FrameBuffer frame_buffer_;
77 video_coding::VideoLayerFrameId last_continuous_id_;
philipel79aab3f2018-03-26 14:31:23 +020078 rtc::Optional<int> current_payload_type_;
79 std::unique_ptr<VideoDecoder> decoder_;
philipel844876d2018-04-05 11:02:54 +020080
81 // Keep track of the |decode_start_time_| of the last |kDecodeTimeMemory|
82 // number of frames. The |decode_start_time_| array contain
83 // <frame timestamp> --> <decode start time> pairs.
84 static constexpr int kDecodeTimeMemory = 8;
85 std::array<std::pair<int64_t, int64_t>, kDecodeTimeMemory> decode_start_time_
86 RTC_GUARDED_BY(bookkeeping_queue_);
87 int next_start_time_index_ RTC_GUARDED_BY(bookkeeping_queue_);
philipel2fee4d62018-03-21 16:52:13 +010088};
89
90} // namespace webrtc
91
92#endif // VIDEO_VIDEO_STREAM_DECODER_IMPL_H_