19a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/*
29a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
39a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *
49a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
59a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
69a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
79a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
89a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
99a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org */
109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
11e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_
12e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_
139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <list>
159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "webrtc/modules/interface/module_common_types.h"
179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "webrtc/typedefs.h"
189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgnamespace webrtc {
209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Struct for holding RTP packets.
229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgstruct Packet {
239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  RTPHeader header;
249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint8_t* payload;  // Datagram excluding RTP header and header extension.
259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int payload_length;
269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  bool primary;  // Primary, i.e., not redundant payload.
279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int waiting_time;
282f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org  bool sync_packet;
299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Constructor.
319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  Packet()
329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      : payload(NULL),
339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        payload_length(0),
349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        primary(true),
352f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        waiting_time(0),
362f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        sync_packet(false) {
379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Comparison operators. Establish a packet ordering based on (1) timestamp,
402f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org  // (2) sequence number, (3) regular packet vs sync-packet and (4) redundancy.
412f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org  // Timestamp and sequence numbers are compared taking wrap-around into
422f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org  // account. If both timestamp and sequence numbers are identical and one of
432f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org  // the packets is sync-packet, the regular packet is considered earlier. For
442f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org  // two regular packets with the same sequence number and timestamp a primary
452f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org  // payload is considered "smaller" than a secondary.
469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  bool operator==(const Packet& rhs) const {
479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return (this->header.timestamp == rhs.header.timestamp &&
489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        this->header.sequenceNumber == rhs.header.sequenceNumber &&
492f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        this->primary == rhs.primary &&
502f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        this->sync_packet == rhs.sync_packet);
519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  bool operator!=(const Packet& rhs) const { return !operator==(rhs); }
539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  bool operator<(const Packet& rhs) const {
549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if (this->header.timestamp == rhs.header.timestamp) {
559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      if (this->header.sequenceNumber == rhs.header.sequenceNumber) {
562f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        // Timestamp and sequence numbers are identical. A sync packet should
572f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        // be recognized "larger" (i.e. "later") compared to a "network packet"
582f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        // (regular packet from network not sync-packet). If none of the packets
592f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        // are sync-packets, then deem the left hand side to be "smaller"
602f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        // (i.e., "earlier") if it is  primary, and right hand side is not.
612f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        //
622f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        // The condition on sync packets to be larger than "network packets,"
632f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        // given same RTP sequence number and timestamp, guarantees that a
642f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        // "network packet" to be inserted in an earlier position into
652f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        // |packet_buffer_| compared to a sync packet of same timestamp and
662f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        // sequence number.
672f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        if (rhs.sync_packet)
682f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org          return true;
692f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org        if (this->sync_packet)
702f0a94274ee3f193c9849fcc28bd49e023f149deturaj@webrtc.org          return false;
719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        return (this->primary && !rhs.primary);
729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      }
739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      return (static_cast<uint16_t>(rhs.header.sequenceNumber
749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org          - this->header.sequenceNumber) < 0xFFFF / 2);
759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return (static_cast<uint32_t>(rhs.header.timestamp
779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        - this->header.timestamp) < 0xFFFFFFFF / 2);
789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  bool operator>(const Packet& rhs) const { return rhs.operator<(*this); }
809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  bool operator<=(const Packet& rhs) const { return !operator>(rhs); }
819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  bool operator>=(const Packet& rhs) const { return !operator<(rhs); }
829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org};
839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// A list of packets.
859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgtypedef std::list<Packet*> PacketList;
869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}  // namespace webrtc
88e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_H_
89