blob: 3201f627ce88d8d51919763397806b42e1912740 [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"
24
25namespace webrtc {
26
27class Clock;
28class VCMJitterEstimator;
29class VCMTiming;
30
31namespace video_coding {
32
33class FrameObject;
34
35class FrameBuffer {
36 public:
37 FrameBuffer(Clock* clock,
38 VCMJitterEstimator* jitter_estimator,
39 const VCMTiming* timing);
40
41 // Insert a frame into the frame buffer.
42 void InsertFrame(std::unique_ptr<FrameObject> frame);
43
44 // Get the next frame for decoding. Will return at latest after
45 // |max_wait_time_ms|, with either a managed FrameObject or an empty
46 // unique ptr if there is no available frame for decoding.
47 std::unique_ptr<FrameObject> NextFrame(int64_t max_wait_time_ms);
48
philipel504c47d2016-06-30 17:33:02 +020049 // Start the frame buffer, has no effect if the frame buffer is started.
50 // The frame buffer is started upon construction.
51 void Start();
52
53 // Stop the frame buffer, causing any sleeping thread in NextFrame to
54 // return immediately.
55 void Stop();
56
philipelbe7a9e52016-05-19 12:19:35 +020057 private:
58 // FrameKey is a pair of (picture id, spatial layer).
59 using FrameKey = std::pair<uint16_t, uint8_t>;
60
61 // Comparator used to sort frames, first on their picture id, and second
62 // on their spatial layer.
63 struct FrameComp {
64 bool operator()(const FrameKey& f1, const FrameKey& f2) const;
65 };
66
67 // Determines whether a frame is continuous.
68 bool IsContinuous(const FrameObject& frame) const
69 EXCLUSIVE_LOCKS_REQUIRED(crit_);
70
71 // Keep track of decoded frames.
72 std::set<FrameKey, FrameComp> decoded_frames_ GUARDED_BY(crit_);
73
74 // The actual buffer that holds the FrameObjects.
75 std::map<FrameKey, std::unique_ptr<FrameObject>, FrameComp> frames_
76 GUARDED_BY(crit_);
77
78 rtc::CriticalSection crit_;
79 Clock* const clock_;
80 rtc::Event frame_inserted_event_;
81 VCMJitterEstimator* const jitter_estimator_;
82 const VCMTiming* const timing_;
83 int newest_picture_id_ GUARDED_BY(crit_);
philipel504c47d2016-06-30 17:33:02 +020084 bool stopped_ GUARDED_BY(crit_);
philipelbe7a9e52016-05-19 12:19:35 +020085
86 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
87};
88
89} // namespace video_coding
90} // namespace webrtc
91
92#endif // WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_