1/*
2 *  Copyright (c) 2013 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_AUDIO_CODING_MAIN_ACM2_INITIAL_DELAY_MANAGER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_MAIN_ACM2_INITIAL_DELAY_MANAGER_H_
13
14#include "webrtc/modules/interface/module_common_types.h"
15#include "webrtc/system_wrappers/interface/scoped_ptr.h"
16
17namespace webrtc {
18
19namespace acm2 {
20
21class InitialDelayManager {
22 public:
23  enum PacketType {
24    kUndefinedPacket, kCngPacket, kAvtPacket, kAudioPacket, kSyncPacket };
25
26  // Specifies a stream of sync-packets.
27  struct SyncStream {
28    SyncStream()
29        : num_sync_packets(0),
30          receive_timestamp(0),
31          timestamp_step(0) {
32      memset(&rtp_info, 0, sizeof(rtp_info));
33    }
34
35    int num_sync_packets;
36
37    // RTP header of the first sync-packet in the sequence.
38    WebRtcRTPHeader rtp_info;
39
40    // Received timestamp of the first sync-packet in the sequence.
41    uint32_t receive_timestamp;
42
43    // Samples per packet.
44    uint32_t timestamp_step;
45  };
46
47  InitialDelayManager(int initial_delay_ms, int late_packet_threshold);
48
49  // Update with the last received RTP header, |header|, and received timestamp,
50  // |received_timestamp|. |type| indicates the packet type. If codec is changed
51  // since the last time |new_codec| should be true. |sample_rate_hz| is the
52  // decoder's sampling rate in Hz. |header| has a field to store sampling rate
53  // but we are not sure if that is properly set at the send side, and |header|
54  // is declared constant in the caller of this function
55  // (AcmReceiver::InsertPacket()). |sync_stream| contains information required
56  // to generate a stream of sync packets.
57  void UpdateLastReceivedPacket(const WebRtcRTPHeader& header,
58                                uint32_t receive_timestamp,
59                                PacketType type,
60                                bool new_codec,
61                                int sample_rate_hz,
62                                SyncStream* sync_stream);
63
64  // Based on the last received timestamp and given the current timestamp,
65  // sequence of late (or perhaps missing) packets is computed.
66  void LatePackets(uint32_t timestamp_now, SyncStream* sync_stream);
67
68  // Get playout timestamp.
69  // Returns true if the timestamp is valid (when buffering), otherwise false.
70  bool GetPlayoutTimestamp(uint32_t* playout_timestamp);
71
72  // True if buffered audio is less than the given initial delay (specified at
73  // the constructor). Buffering might be disabled by the client of this class.
74  bool buffering() { return buffering_; }
75
76  // Disable buffering in the class.
77  void DisableBuffering();
78
79  // True if any packet received for buffering.
80  bool PacketBuffered() { return last_packet_type_ != kUndefinedPacket; }
81
82 private:
83  static const uint8_t kInvalidPayloadType = 0xFF;
84
85  // Update playout timestamps. While buffering, this is about
86  // |initial_delay_ms| millisecond behind the latest received timestamp.
87  void UpdatePlayoutTimestamp(const RTPHeader& current_header,
88                              int sample_rate_hz);
89
90  // Record an RTP headr and related parameter
91  void RecordLastPacket(const WebRtcRTPHeader& rtp_info,
92                        uint32_t receive_timestamp,
93                        PacketType type);
94
95  PacketType last_packet_type_;
96  WebRtcRTPHeader last_packet_rtp_info_;
97  uint32_t last_receive_timestamp_;
98  uint32_t timestamp_step_;
99  uint8_t audio_payload_type_;
100  const int initial_delay_ms_;
101  int buffered_audio_ms_;
102  bool buffering_;
103
104  // During the initial phase where packets are being accumulated and silence
105  // is played out, |playout_ts| is a timestamp which is equal to
106  // |initial_delay_ms_| milliseconds earlier than the most recently received
107  // RTP timestamp.
108  uint32_t playout_timestamp_;
109
110  // If the number of late packets exceed this value (computed based on current
111  // timestamp and last received timestamp), sequence of sync-packets is
112  // specified.
113  const int late_packet_threshold_;
114};
115
116}  // namespace acm2
117
118}  // namespace webrtc
119
120#endif  // WEBRTC_MODULES_AUDIO_CODING_MAIN_ACM2_INITIAL_DELAY_MANAGER_H_
121