blob: 0af2bf952350bc76674f5f42dcbf8f86fff8e80e [file] [log] [blame]
philipelbe7a9e52016-05-19 12:19:35 +02001/*
2 * Copyright (c) 2016 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 WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_
12#define WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_
13
14#include <array>
15#include <map>
16#include <memory>
17#include <set>
18#include <utility>
19
20#include "webrtc/base/constructormagic.h"
21#include "webrtc/base/criticalsection.h"
22#include "webrtc/base/event.h"
23#include "webrtc/base/thread_annotations.h"
philipel4f6cd6a2016-08-03 10:59:32 +020024#include "webrtc/modules/video_coding/include/video_coding_defines.h"
25#include "webrtc/modules/video_coding/inter_frame_delay.h"
philipelbe7a9e52016-05-19 12:19:35 +020026
27namespace webrtc {
28
29class Clock;
30class VCMJitterEstimator;
31class VCMTiming;
32
33namespace video_coding {
34
35class FrameObject;
36
37class FrameBuffer {
38 public:
39 FrameBuffer(Clock* clock,
40 VCMJitterEstimator* jitter_estimator,
philipel4f6cd6a2016-08-03 10:59:32 +020041 VCMTiming* timing);
philipelbe7a9e52016-05-19 12:19:35 +020042
43 // Insert a frame into the frame buffer.
44 void InsertFrame(std::unique_ptr<FrameObject> frame);
45
46 // Get the next frame for decoding. Will return at latest after
47 // |max_wait_time_ms|, with either a managed FrameObject or an empty
48 // unique ptr if there is no available frame for decoding.
49 std::unique_ptr<FrameObject> NextFrame(int64_t max_wait_time_ms);
50
philipel4f6cd6a2016-08-03 10:59:32 +020051 // Tells the FrameBuffer which protection mode that is in use. Affects
52 // the frame timing.
53 // TODO(philipel): Remove this when new timing calculations has been
54 // implemented.
55 void SetProtectionMode(VCMVideoProtection mode);
56
philipel504c47d2016-06-30 17:33:02 +020057 // Start the frame buffer, has no effect if the frame buffer is started.
58 // The frame buffer is started upon construction.
59 void Start();
60
61 // Stop the frame buffer, causing any sleeping thread in NextFrame to
62 // return immediately.
63 void Stop();
64
philipelbe7a9e52016-05-19 12:19:35 +020065 private:
66 // FrameKey is a pair of (picture id, spatial layer).
67 using FrameKey = std::pair<uint16_t, uint8_t>;
68
69 // Comparator used to sort frames, first on their picture id, and second
70 // on their spatial layer.
71 struct FrameComp {
72 bool operator()(const FrameKey& f1, const FrameKey& f2) const;
73 };
74
75 // Determines whether a frame is continuous.
76 bool IsContinuous(const FrameObject& frame) const
77 EXCLUSIVE_LOCKS_REQUIRED(crit_);
78
79 // Keep track of decoded frames.
80 std::set<FrameKey, FrameComp> decoded_frames_ GUARDED_BY(crit_);
81
82 // The actual buffer that holds the FrameObjects.
83 std::map<FrameKey, std::unique_ptr<FrameObject>, FrameComp> frames_
84 GUARDED_BY(crit_);
85
86 rtc::CriticalSection crit_;
87 Clock* const clock_;
88 rtc::Event frame_inserted_event_;
philipel4f6cd6a2016-08-03 10:59:32 +020089 VCMJitterEstimator* const jitter_estimator_ GUARDED_BY(crit_);
90 VCMTiming* const timing_ GUARDED_BY(crit_);
91 VCMInterFrameDelay inter_frame_delay_ GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +020092 int newest_picture_id_ GUARDED_BY(crit_);
philipel504c47d2016-06-30 17:33:02 +020093 bool stopped_ GUARDED_BY(crit_);
philipel4f6cd6a2016-08-03 10:59:32 +020094 VCMVideoProtection protection_mode_ GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +020095
96 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
97};
98
99} // namespace video_coding
100} // namespace webrtc
101
102#endif // WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_