blob: cd1f953a6d571c31627d2619624b7f8c54c81704 [file] [log] [blame]
philipel2fee4d62018-03-21 15:52:131/*
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 15:52:1314#include <map>
15#include <memory>
16#include <utility>
17
Danil Chapovalovb9b146c2018-06-15 10:28:0718#include "absl/types/optional.h"
philipel2fee4d62018-03-21 15:52:1319#include "api/video/video_stream_decoder.h"
philipel97187112018-03-23 09:43:2120#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 09:02:5423#include "rtc_base/platform_thread.h"
philipel97187112018-03-23 09:43:2124#include "rtc_base/task_queue.h"
25#include "rtc_base/thread_checker.h"
26#include "system_wrappers/include/clock.h"
philipel2fee4d62018-03-21 15:52:1327
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 09:02:5443 enum DecodeResult {
44 kOk,
45 kDecodeFailure,
46 kNoFrame,
47 kNoDecoder,
48 kShutdown,
49 };
50
philipel6847f9b2018-04-20 13:05:3751 struct FrameTimestamps {
52 int64_t timestamp;
53 int64_t decode_start_time_ms;
54 int64_t render_time_us;
55 };
56
philipel79aab3f2018-03-26 12:31:2357 VideoDecoder* GetDecoder(int payload_type);
philipel844876d2018-04-05 09:02:5458 static void DecodeLoop(void* ptr);
59 DecodeResult DecodeNextFrame(int max_wait_time_ms, bool keyframe_required);
philipel79aab3f2018-03-26 12:31:2360
philipel6847f9b2018-04-20 13:05:3761 FrameTimestamps* GetFrameTimestamps(int64_t timestamp);
62
philipel2fee4d62018-03-21 15:52:1363 // 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 10:28:0767 absl::optional<int32_t> decode_time_ms,
68 absl::optional<uint8_t> qp) override;
philipel2fee4d62018-03-21 15:52:1369
philipel97187112018-03-23 09:43:2170 VideoStreamDecoder::Callbacks* const callbacks_
71 RTC_PT_GUARDED_BY(bookkeeping_queue_);
72 VideoDecoderFactory* const decoder_factory_;
philipel2fee4d62018-03-21 15:52:1373 std::map<int, std::pair<SdpVideoFormat, int>> decoder_settings_;
philipel97187112018-03-23 09:43:2174
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 09:02:5481 rtc::PlatformThread decode_thread_;
philipel97187112018-03-23 09:43:2182 VCMJitterEstimator jitter_estimator_;
83 VCMTiming timing_;
84 video_coding::FrameBuffer frame_buffer_;
85 video_coding::VideoLayerFrameId last_continuous_id_;
Danil Chapovalovb9b146c2018-06-15 10:28:0786 absl::optional<int> current_payload_type_;
philipel79aab3f2018-03-26 12:31:2387 std::unique_ptr<VideoDecoder> decoder_;
philipel844876d2018-04-05 09:02:5488
philipel6847f9b2018-04-20 13:05:3789 // 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 09:02:5493 RTC_GUARDED_BY(bookkeeping_queue_);
philipel6847f9b2018-04-20 13:05:3794 int next_frame_timestamps_index_ RTC_GUARDED_BY(bookkeeping_queue_);
philipel2fee4d62018-03-21 15:52:1395};
96
97} // namespace webrtc
98
99#endif // VIDEO_VIDEO_STREAM_DECODER_IMPL_H_