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