blob: d0f896133f116cecef7491070c1ebd6508ae28ff [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:
philipel75562822016-09-05 10:57:41 +020039 enum ReturnReason { kFrameFound, kTimeout, kStopped };
40
philipelbe7a9e52016-05-19 12:19:35 +020041 FrameBuffer(Clock* clock,
42 VCMJitterEstimator* jitter_estimator,
philipel4f6cd6a2016-08-03 10:59:32 +020043 VCMTiming* timing);
philipelbe7a9e52016-05-19 12:19:35 +020044
45 // Insert a frame into the frame buffer.
46 void InsertFrame(std::unique_ptr<FrameObject> frame);
47
48 // Get the next frame for decoding. Will return at latest after
philipel75562822016-09-05 10:57:41 +020049 // |max_wait_time_ms|.
50 // - If a frame is availiable within |max_wait_time_ms| it will return
51 // kFrameFound and set |frame_out| to the resulting frame.
52 // - If no frame is available after |max_wait_time_ms| it will return
53 // kTimeout.
54 // - If the FrameBuffer is stopped then it will return kStopped.
55 ReturnReason NextFrame(int64_t max_wait_time_ms,
56 std::unique_ptr<FrameObject>* frame_out);
philipelbe7a9e52016-05-19 12:19:35 +020057
philipel4f6cd6a2016-08-03 10:59:32 +020058 // Tells the FrameBuffer which protection mode that is in use. Affects
59 // the frame timing.
60 // TODO(philipel): Remove this when new timing calculations has been
61 // implemented.
62 void SetProtectionMode(VCMVideoProtection mode);
63
philipel504c47d2016-06-30 17:33:02 +020064 // Start the frame buffer, has no effect if the frame buffer is started.
65 // The frame buffer is started upon construction.
66 void Start();
67
68 // Stop the frame buffer, causing any sleeping thread in NextFrame to
69 // return immediately.
70 void Stop();
71
philipelbe7a9e52016-05-19 12:19:35 +020072 private:
73 // FrameKey is a pair of (picture id, spatial layer).
74 using FrameKey = std::pair<uint16_t, uint8_t>;
75
76 // Comparator used to sort frames, first on their picture id, and second
77 // on their spatial layer.
78 struct FrameComp {
79 bool operator()(const FrameKey& f1, const FrameKey& f2) const;
80 };
81
82 // Determines whether a frame is continuous.
83 bool IsContinuous(const FrameObject& frame) const
84 EXCLUSIVE_LOCKS_REQUIRED(crit_);
85
86 // Keep track of decoded frames.
87 std::set<FrameKey, FrameComp> decoded_frames_ GUARDED_BY(crit_);
88
89 // The actual buffer that holds the FrameObjects.
90 std::map<FrameKey, std::unique_ptr<FrameObject>, FrameComp> frames_
91 GUARDED_BY(crit_);
92
93 rtc::CriticalSection crit_;
94 Clock* const clock_;
95 rtc::Event frame_inserted_event_;
philipel4f6cd6a2016-08-03 10:59:32 +020096 VCMJitterEstimator* const jitter_estimator_ GUARDED_BY(crit_);
97 VCMTiming* const timing_ GUARDED_BY(crit_);
98 VCMInterFrameDelay inter_frame_delay_ GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +020099 int newest_picture_id_ GUARDED_BY(crit_);
philipel504c47d2016-06-30 17:33:02 +0200100 bool stopped_ GUARDED_BY(crit_);
philipel4f6cd6a2016-08-03 10:59:32 +0200101 VCMVideoProtection protection_mode_ GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +0200102
103 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
104};
105
106} // namespace video_coding
107} // namespace webrtc
108
109#endif // WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_