rtp_packet_history.h revision 4591fbd09f9cb6e83433c49a12dd8524c2806502
1/*
2 *  Copyright (c) 2012 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 *  Class for storing RTP packets.
11 */
12
13#ifndef WEBRTC_MODULES_RTP_RTCP_RTP_PACKET_HISTORY_H_
14#define WEBRTC_MODULES_RTP_RTCP_RTP_PACKET_HISTORY_H_
15
16#include <vector>
17
18#include "webrtc/base/thread_annotations.h"
19#include "webrtc/modules/interface/module_common_types.h"
20#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
21#include "webrtc/typedefs.h"
22
23namespace webrtc {
24
25class Clock;
26class CriticalSectionWrapper;
27
28class RTPPacketHistory {
29 public:
30  RTPPacketHistory(Clock* clock);
31  ~RTPPacketHistory();
32
33  void SetStorePacketsStatus(bool enable, uint16_t number_to_store);
34
35  bool StorePackets() const;
36
37  // Stores RTP packet.
38  int32_t PutRTPPacket(const uint8_t* packet,
39                       size_t packet_length,
40                       size_t max_packet_length,
41                       int64_t capture_time_ms,
42                       StorageType type);
43
44  // Gets stored RTP packet corresponding to the input sequence number.
45  // The packet is copied to the buffer pointed to by ptr_rtp_packet.
46  // The rtp_packet_length should show the available buffer size.
47  // Returns true if packet is found.
48  // rtp_packet_length: returns the copied packet length on success.
49  // min_elapsed_time_ms: the minimum time that must have elapsed since the last
50  // time the packet was resent (parameter is ignored if set to zero).
51  // If the packet is found but the minimum time has not elaped, no bytes are
52  // copied.
53  // stored_time_ms: returns the time when the packet was stored.
54  // type: returns the storage type set in PutRTPPacket.
55  bool GetPacketAndSetSendTime(uint16_t sequence_number,
56                               uint32_t min_elapsed_time_ms,
57                               bool retransmit,
58                               uint8_t* packet,
59                               size_t* packet_length,
60                               int64_t* stored_time_ms);
61
62  bool GetBestFittingPacket(uint8_t* packet, size_t* packet_length,
63                            int64_t* stored_time_ms);
64
65  bool HasRTPPacket(uint16_t sequence_number) const;
66
67 private:
68  void GetPacket(int index, uint8_t* packet, size_t* packet_length,
69                 int64_t* stored_time_ms) const;
70  void Allocate(uint16_t number_to_store) EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
71  void Free() EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
72  void VerifyAndAllocatePacketLength(size_t packet_length);
73  bool FindSeqNum(uint16_t sequence_number, int32_t* index) const;
74  int FindBestFittingPacket(size_t size) const;
75
76 private:
77  Clock* clock_;
78  CriticalSectionWrapper* critsect_;
79  bool store_;
80  uint32_t prev_index_;
81  size_t max_packet_length_;
82
83  std::vector<std::vector<uint8_t> > stored_packets_;
84  std::vector<uint16_t> stored_seq_nums_;
85  std::vector<size_t> stored_lengths_;
86  std::vector<int64_t> stored_times_;
87  std::vector<int64_t> stored_send_times_;
88  std::vector<StorageType> stored_types_;
89};
90}  // namespace webrtc
91#endif  // WEBRTC_MODULES_RTP_RTCP_RTP_PACKET_HISTORY_H_
92