philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 1 | /* |
| 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" |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 24 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
| 25 | #include "webrtc/modules/video_coding/inter_frame_delay.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | class Clock; |
| 30 | class VCMJitterEstimator; |
| 31 | class VCMTiming; |
| 32 | |
| 33 | namespace video_coding { |
| 34 | |
| 35 | class FrameObject; |
| 36 | |
| 37 | class FrameBuffer { |
| 38 | public: |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame^] | 39 | enum ReturnReason { kFrameFound, kTimeout, kStopped }; |
| 40 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 41 | FrameBuffer(Clock* clock, |
| 42 | VCMJitterEstimator* jitter_estimator, |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 43 | VCMTiming* timing); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 44 | |
| 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 |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame^] | 49 | // |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); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 57 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 58 | // 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 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 64 | // 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 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 72 | 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_; |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 96 | VCMJitterEstimator* const jitter_estimator_ GUARDED_BY(crit_); |
| 97 | VCMTiming* const timing_ GUARDED_BY(crit_); |
| 98 | VCMInterFrameDelay inter_frame_delay_ GUARDED_BY(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 99 | int newest_picture_id_ GUARDED_BY(crit_); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 100 | bool stopped_ GUARDED_BY(crit_); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 101 | VCMVideoProtection protection_mode_ GUARDED_BY(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 102 | |
| 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_ |