1d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org/* 2d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * 4d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * Use of this source code is governed by a BSD-style license 5d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * that can be found in the LICENSE file in the root of the source 6d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * tree. An additional intellectual property rights grant can be found 7d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * in the file PATENTS. All contributing project authors may 8d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * be found in the AUTHORS file in the root of the source tree. 9d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org */ 10d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 119c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_ 129c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_ 13d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 1488fbb2d86b33a3886bba1af4d098efa2c19eb1e7henrike@webrtc.org#include "webrtc/base/constructormagic.h" 159c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/packet.h" 16d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org#include "webrtc/typedefs.h" 17d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 18d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgnamespace webrtc { 19d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 20d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// Forward declaration. 21d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgclass DecoderDatabase; 22d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 23d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// This is the actual buffer holding the packets before decoding. 24d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgclass PacketBuffer { 25d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org public: 26d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org enum BufferReturnCodes { 27d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org kOK = 0, 28d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org kFlushed, 29d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org kNotFound, 30d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org kBufferEmpty, 31d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org kInvalidPacket, 32116ed1d4f0445fdf215cffa2d9d0946a1cdab3ebhenrik.lundin@webrtc.org kInvalidPointer 33d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org }; 34d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 35d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Constructor creates a buffer which can hold a maximum of 36116ed1d4f0445fdf215cffa2d9d0946a1cdab3ebhenrik.lundin@webrtc.org // |max_number_of_packets| packets. 37116ed1d4f0445fdf215cffa2d9d0946a1cdab3ebhenrik.lundin@webrtc.org PacketBuffer(size_t max_number_of_packets); 38d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 39d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Deletes all packets in the buffer before destroying the buffer. 40d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org virtual ~PacketBuffer(); 41d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 42d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Flushes the buffer and deletes all packets in it. 43d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org virtual void Flush(); 44d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 45d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Returns true for an empty buffer. 467f6c4d42a2605d1da39af3f957a46cf57b043b84Karl Wiberg virtual bool Empty() const; 47d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 48d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Inserts |packet| into the buffer. The buffer will take over ownership of 49d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // the packet object. 50d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Returns PacketBuffer::kOK on success, PacketBuffer::kFlushed if the buffer 51d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // was flushed due to overfilling. 52d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org virtual int InsertPacket(Packet* packet); 53d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 54d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Inserts a list of packets into the buffer. The buffer will take over 55d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // ownership of the packet objects. 56d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Returns PacketBuffer::kOK if all packets were inserted successfully. 57d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // If the buffer was flushed due to overfilling, only a subset of the list is 58d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // inserted, and PacketBuffer::kFlushed is returned. 59d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // The last three parameters are included for legacy compatibility. 60d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // TODO(hlundin): Redesign to not use current_*_payload_type and 61d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // decoder_database. 62d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org virtual int InsertPacketList(PacketList* packet_list, 63d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org const DecoderDatabase& decoder_database, 64d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org uint8_t* current_rtp_payload_type, 65d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org uint8_t* current_cng_rtp_payload_type); 66d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 67d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Gets the timestamp for the first packet in the buffer and writes it to the 68d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // output variable |next_timestamp|. 69d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Returns PacketBuffer::kBufferEmpty if the buffer is empty, 70d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // PacketBuffer::kOK otherwise. 71d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org virtual int NextTimestamp(uint32_t* next_timestamp) const; 72d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 73d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Gets the timestamp for the first packet in the buffer with a timestamp no 74d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // lower than the input limit |timestamp|. The result is written to the output 75d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // variable |next_timestamp|. 76d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Returns PacketBuffer::kBufferEmpty if the buffer is empty, 77d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // PacketBuffer::kOK otherwise. 78d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org virtual int NextHigherTimestamp(uint32_t timestamp, 79d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org uint32_t* next_timestamp) const; 80d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 81d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Returns a (constant) pointer the RTP header of the first packet in the 82d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // buffer. Returns NULL if the buffer is empty. 83d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org virtual const RTPHeader* NextRtpHeader() const; 84d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 85d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Extracts the first packet in the buffer and returns a pointer to it. 86d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Returns NULL if the buffer is empty. The caller is responsible for deleting 87d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // the packet. 88d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Subsequent packets with the same timestamp as the one extracted will be 89d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // discarded and properly deleted. The number of discarded packets will be 90d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // written to the output variable |discard_count|. 91dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting virtual Packet* GetNextPacket(size_t* discard_count); 92d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 93d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Discards the first packet in the buffer. The packet is deleted. 94d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Returns PacketBuffer::kBufferEmpty if the buffer is empty, 95d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // PacketBuffer::kOK otherwise. 96d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org virtual int DiscardNextPacket(); 97d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 9852b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // Discards all packets that are (strictly) older than timestamp_limit, 9952b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // but newer than timestamp_limit - horizon_samples. Setting horizon_samples 10052b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // to zero implies that the horizon is set to half the timestamp range. That 10152b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // is, if a packet is more than 2^31 timestamps into the future compared with 10252b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // timestamp_limit (including wrap-around), it is considered old. 103d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Returns number of packets discarded. 10452b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org virtual int DiscardOldPackets(uint32_t timestamp_limit, 10552b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org uint32_t horizon_samples); 10652b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org 10752b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // Discards all packets that are (strictly) older than timestamp_limit. 1087f6c4d42a2605d1da39af3f957a46cf57b043b84Karl Wiberg virtual int DiscardAllOldPackets(uint32_t timestamp_limit); 109d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 110d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Returns the number of packets in the buffer, including duplicates and 111d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // redundant packets. 112dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting virtual size_t NumPacketsInBuffer() const; 113d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 114d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Returns the number of samples in the buffer, including samples carried in 115d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // duplicate and redundant packets. 116dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting virtual size_t NumSamplesInBuffer(DecoderDatabase* decoder_database, 117dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting size_t last_decoded_length) const; 118d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 119d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Increase the waiting time counter for every packet in the buffer by |inc|. 120d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // The default value for |inc| is 1. 121d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org virtual void IncrementWaitingTimes(int inc = 1); 122d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 123116ed1d4f0445fdf215cffa2d9d0946a1cdab3ebhenrik.lundin@webrtc.org virtual void BufferStat(int* num_packets, int* max_num_packets) const; 124d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 125d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Static method that properly deletes the first packet, and its payload 126d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // array, in |packet_list|. Returns false if |packet_list| already was empty, 127d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // otherwise true. 128d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org static bool DeleteFirstPacket(PacketList* packet_list); 129d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 130d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Static method that properly deletes all packets, and their payload arrays, 131d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // in |packet_list|. 132d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org static void DeleteAllPackets(PacketList* packet_list); 133d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 13452b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // Static method returning true if |timestamp| is older than |timestamp_limit| 13552b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // but less than |horizon_samples| behind |timestamp_limit|. For instance, 13652b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // with timestamp_limit = 100 and horizon_samples = 10, a timestamp in the 13752b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // range (90, 100) is considered obsolete, and will yield true. 13852b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // Setting |horizon_samples| to 0 is the same as setting it to 2^31, i.e., 13952b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org // half the 32-bit timestamp range. 14052b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org static bool IsObsoleteTimestamp(uint32_t timestamp, 14152b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org uint32_t timestamp_limit, 14252b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org uint32_t horizon_samples) { 14352b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org return IsNewerTimestamp(timestamp_limit, timestamp) && 14452b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org (horizon_samples == 0 || 14552b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org IsNewerTimestamp(timestamp, timestamp_limit - horizon_samples)); 14652b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org } 14752b42cb069a035f10e951195c28cf6d05d1fd91chenrik.lundin@webrtc.org 148d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org private: 149d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org size_t max_number_of_packets_; 150d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org PacketList buffer_; 1513c089d751ede283e21e186885eaf705c3257ccd2henrikg RTC_DISALLOW_COPY_AND_ASSIGN(PacketBuffer); 152d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}; 153d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 154d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org} // namespace webrtc 1559c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_ 156