philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [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_PACKET_BUFFER_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |
| 13 | |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 14 | #include <vector> |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 15 | |
| 16 | #include "webrtc/base/criticalsection.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 17 | #include "webrtc/base/thread_annotations.h" |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 18 | #include "webrtc/modules/include/module_common_types.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 19 | #include "webrtc/modules/video_coding/packet.h" |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 20 | #include "webrtc/modules/video_coding/rtp_frame_reference_finder.h" |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 21 | #include "webrtc/modules/video_coding/sequence_number_util.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 24 | |
| 25 | class Clock; |
| 26 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 27 | namespace video_coding { |
| 28 | |
| 29 | class FrameObject; |
| 30 | class RtpFrameObject; |
| 31 | |
| 32 | class OnCompleteFrameCallback { |
| 33 | public: |
| 34 | virtual ~OnCompleteFrameCallback() {} |
| 35 | virtual void OnCompleteFrame(std::unique_ptr<FrameObject> frame) = 0; |
| 36 | }; |
| 37 | |
| 38 | class PacketBuffer { |
| 39 | public: |
| 40 | // Both |start_buffer_size| and |max_buffer_size| must be a power of 2. |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 41 | PacketBuffer(Clock* clock, |
| 42 | size_t start_buffer_size, |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 43 | size_t max_buffer_size, |
| 44 | OnCompleteFrameCallback* frame_callback); |
| 45 | |
| 46 | bool InsertPacket(const VCMPacket& packet); |
| 47 | void ClearTo(uint16_t seq_num); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 48 | void Clear(); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 49 | |
| 50 | private: |
| 51 | friend RtpFrameObject; |
| 52 | // Since we want the packet buffer to be as packet type agnostic |
| 53 | // as possible we extract only the information needed in order |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 54 | // to determine whether a sequence of packets is continuous or not. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 55 | struct ContinuityInfo { |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 56 | // The sequence number of the packet. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 57 | uint16_t seq_num = 0; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 58 | |
| 59 | // If this is the first packet of the frame. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 60 | bool frame_begin = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 61 | |
| 62 | // If this is the last packet of the frame. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 63 | bool frame_end = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 64 | |
| 65 | // If this slot is currently used. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 66 | bool used = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 67 | |
| 68 | // If all its previous packets have been inserted into the packet buffer. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 69 | bool continuous = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 70 | |
| 71 | // If this packet has been used to create a frame already. |
| 72 | bool frame_created = false; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 75 | Clock* const clock_; |
| 76 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 77 | // Tries to expand the buffer. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 78 | bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 79 | |
| 80 | // Test if all previous packets has arrived for the given sequence number. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 81 | bool IsContinuous(uint16_t seq_num) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 82 | |
| 83 | // Test if all packets of a frame has arrived, and if so, creates a frame. |
| 84 | // May create multiple frames per invocation. |
| 85 | void FindFrames(uint16_t seq_num) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 86 | |
| 87 | // Copy the bitstream for |frame| to |destination|. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 88 | bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 89 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 90 | // Get the packet with sequence number |seq_num|. |
| 91 | VCMPacket* GetPacket(uint16_t seq_num); |
| 92 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 93 | // Mark all slots used by |frame| as not used. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 94 | void ReturnFrame(RtpFrameObject* frame); |
| 95 | |
| 96 | rtc::CriticalSection crit_; |
| 97 | |
| 98 | // Buffer size_ and max_size_ must always be a power of two. |
| 99 | size_t size_ GUARDED_BY(crit_); |
| 100 | const size_t max_size_; |
| 101 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 102 | // The fist sequence number currently in the buffer. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 103 | uint16_t first_seq_num_ GUARDED_BY(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 104 | |
| 105 | // The last sequence number currently in the buffer. |
| 106 | uint16_t last_seq_num_ GUARDED_BY(crit_); |
| 107 | |
| 108 | // If the packet buffer has received its first packet. |
| 109 | bool first_packet_received_ GUARDED_BY(crit_); |
| 110 | |
| 111 | // Buffer that holds the inserted packets. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 112 | std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 113 | |
| 114 | // Buffer that holds the information about which slot that is currently in use |
| 115 | // and information needed to determine the continuity between packets. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 116 | std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_); |
| 117 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 118 | // Frames that have received all their packets are handed off to the |
| 119 | // |reference_finder_| which finds the dependencies between the frames. |
| 120 | RtpFrameReferenceFinder reference_finder_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | } // namespace video_coding |
| 124 | } // namespace webrtc |
| 125 | |
| 126 | #endif // WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |