rtp_packet_history.h revision a678a3baee2e680bd521f3a6caf97707fffd6093
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 "module_common_types.h"
19#include "rtp_rtcp_defines.h"
20#include "typedefs.h"
21
22namespace webrtc {
23
24class Clock;
25class CriticalSectionWrapper;
26
27class RTPPacketHistory {
28 public:
29  RTPPacketHistory(Clock* clock);
30  ~RTPPacketHistory();
31
32  void SetStorePacketsStatus(bool enable, uint16_t number_to_store);
33
34  bool StorePackets() const;
35
36  // Stores RTP packet.
37  int32_t PutRTPPacket(const uint8_t* packet,
38                       uint16_t packet_length,
39                       uint16_t max_packet_length,
40                       int64_t capture_time_ms,
41                       StorageType type);
42
43  // Replaces the stored RTP packet with matching sequence number with the
44  // RTP header of the provided packet.
45  // Note: Calling this function assumes that the RTP header length should not
46  // have changed since the packet was stored.
47  int32_t ReplaceRTPHeader(const uint8_t* packet,
48                           uint16_t sequence_number,
49                           uint16_t rtp_header_length);
50
51  // Gets stored RTP packet corresponding to the input sequence number.
52  // The packet is copied to the buffer pointed to by ptr_rtp_packet.
53  // The rtp_packet_length should show the available buffer size.
54  // Returns true if packet is found.
55  // rtp_packet_length: returns the copied packet length on success.
56  // min_elapsed_time_ms: the minimum time that must have elapsed since the last
57  // time the packet was resent (parameter is ignored if set to zero).
58  // If the packet is found but the minimum time has not elaped, no bytes are
59  // copied.
60  // stored_time_ms: returns the time when the packet was stored.
61  // type: returns the storage type set in PutRTPPacket.
62  bool GetRTPPacket(uint16_t sequence_number,
63                    uint32_t min_elapsed_time_ms,
64                    uint8_t* packet,
65                    uint16_t* packet_length,
66                    int64_t* stored_time_ms,
67                    StorageType* type) const;
68
69  bool HasRTPPacket(uint16_t sequence_number) const;
70
71  void UpdateResendTime(uint16_t sequence_number);
72
73 private:
74  void Allocate(uint16_t number_to_store);
75  void Free();
76  void VerifyAndAllocatePacketLength(uint16_t packet_length);
77  bool FindSeqNum(uint16_t sequence_number, int32_t* index) const;
78
79 private:
80  Clock* clock_;
81  CriticalSectionWrapper* critsect_;
82  bool store_;
83  uint32_t prev_index_;
84  uint16_t max_packet_length_;
85
86  std::vector<std::vector<uint8_t> > stored_packets_;
87  std::vector<uint16_t> stored_seq_nums_;
88  std::vector<uint16_t> stored_lengths_;
89  std::vector<int64_t> stored_times_;
90  std::vector<int64_t> stored_resend_times_;
91  std::vector<StorageType> stored_types_;
92};
93}  // namespace webrtc
94#endif  // WEBRTC_MODULES_RTP_RTCP_RTP_PACKET_HISTORY_H_
95