OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 namespace webrtc { | 25 namespace webrtc { |
26 | 26 |
27 Operations DecisionLogicNormal::GetDecisionSpecialized( | 27 Operations DecisionLogicNormal::GetDecisionSpecialized( |
28 const SyncBuffer& sync_buffer, | 28 const SyncBuffer& sync_buffer, |
29 const Expand& expand, | 29 const Expand& expand, |
30 size_t decoder_frame_length, | 30 size_t decoder_frame_length, |
31 const RTPHeader* packet_header, | 31 const RTPHeader* packet_header, |
32 Modes prev_mode, | 32 Modes prev_mode, |
33 bool play_dtmf, | 33 bool play_dtmf, |
34 bool* reset_decoder) { | 34 bool* reset_decoder, |
| 35 size_t generated_noise_samples) { |
35 assert(playout_mode_ == kPlayoutOn || playout_mode_ == kPlayoutStreaming); | 36 assert(playout_mode_ == kPlayoutOn || playout_mode_ == kPlayoutStreaming); |
36 // Guard for errors, to avoid getting stuck in error mode. | 37 // Guard for errors, to avoid getting stuck in error mode. |
37 if (prev_mode == kModeError) { | 38 if (prev_mode == kModeError) { |
38 if (!packet_header) { | 39 if (!packet_header) { |
39 return kExpand; | 40 return kExpand; |
40 } else { | 41 } else { |
41 return kUndefined; // Use kUndefined to flag for a reset. | 42 return kUndefined; // Use kUndefined to flag for a reset. |
42 } | 43 } |
43 } | 44 } |
44 | 45 |
45 uint32_t target_timestamp = sync_buffer.end_timestamp(); | 46 uint32_t target_timestamp = sync_buffer.end_timestamp(); |
46 uint32_t available_timestamp = 0; | 47 uint32_t available_timestamp = 0; |
47 bool is_cng_packet = false; | 48 bool is_cng_packet = false; |
48 if (packet_header) { | 49 if (packet_header) { |
49 available_timestamp = packet_header->timestamp; | 50 available_timestamp = packet_header->timestamp; |
50 is_cng_packet = | 51 is_cng_packet = |
51 decoder_database_->IsComfortNoise(packet_header->payloadType); | 52 decoder_database_->IsComfortNoise(packet_header->payloadType); |
52 } | 53 } |
53 | 54 |
54 if (is_cng_packet) { | 55 if (is_cng_packet) { |
55 return CngOperation(prev_mode, target_timestamp, available_timestamp); | 56 return CngOperation(prev_mode, target_timestamp, available_timestamp, |
| 57 generated_noise_samples); |
56 } | 58 } |
57 | 59 |
58 // Handle the case with no packet at all available (except maybe DTMF). | 60 // Handle the case with no packet at all available (except maybe DTMF). |
59 if (!packet_header) { | 61 if (!packet_header) { |
60 return NoPacket(play_dtmf); | 62 return NoPacket(play_dtmf); |
61 } | 63 } |
62 | 64 |
63 // If the expand period was very long, reset NetEQ since it is likely that the | 65 // If the expand period was very long, reset NetEQ since it is likely that the |
64 // sender was restarted. | 66 // sender was restarted. |
65 if (num_consecutive_expands_ > kReinitAfterExpands) { | 67 if (num_consecutive_expands_ > kReinitAfterExpands) { |
66 *reset_decoder = true; | 68 *reset_decoder = true; |
67 return kNormal; | 69 return kNormal; |
68 } | 70 } |
69 | 71 |
70 const uint32_t five_seconds_samples = | 72 const uint32_t five_seconds_samples = |
71 static_cast<uint32_t>(5 * 8000 * fs_mult_); | 73 static_cast<uint32_t>(5 * 8000 * fs_mult_); |
72 // Check if the required packet is available. | 74 // Check if the required packet is available. |
73 if (target_timestamp == available_timestamp) { | 75 if (target_timestamp == available_timestamp) { |
74 return ExpectedPacketAvailable(prev_mode, play_dtmf); | 76 return ExpectedPacketAvailable(prev_mode, play_dtmf); |
75 } else if (!PacketBuffer::IsObsoleteTimestamp( | 77 } else if (!PacketBuffer::IsObsoleteTimestamp( |
76 available_timestamp, target_timestamp, five_seconds_samples)) { | 78 available_timestamp, target_timestamp, five_seconds_samples)) { |
77 return FuturePacketAvailable(sync_buffer, expand, decoder_frame_length, | 79 return FuturePacketAvailable(sync_buffer, expand, decoder_frame_length, |
78 prev_mode, target_timestamp, | 80 prev_mode, target_timestamp, |
79 available_timestamp, play_dtmf); | 81 available_timestamp, play_dtmf, |
| 82 generated_noise_samples); |
80 } else { | 83 } else { |
81 // This implies that available_timestamp < target_timestamp, which can | 84 // This implies that available_timestamp < target_timestamp, which can |
82 // happen when a new stream or codec is received. Signal for a reset. | 85 // happen when a new stream or codec is received. Signal for a reset. |
83 return kUndefined; | 86 return kUndefined; |
84 } | 87 } |
85 } | 88 } |
86 | 89 |
87 Operations DecisionLogicNormal::CngOperation(Modes prev_mode, | 90 Operations DecisionLogicNormal::CngOperation(Modes prev_mode, |
88 uint32_t target_timestamp, | 91 uint32_t target_timestamp, |
89 uint32_t available_timestamp) { | 92 uint32_t available_timestamp, |
| 93 size_t generated_noise_samples) { |
90 // Signed difference between target and available timestamp. | 94 // Signed difference between target and available timestamp. |
91 int32_t timestamp_diff = static_cast<int32_t>( | 95 int32_t timestamp_diff = static_cast<int32_t>( |
92 static_cast<uint32_t>(generated_noise_samples_ + target_timestamp) - | 96 static_cast<uint32_t>(generated_noise_samples + target_timestamp) - |
93 available_timestamp); | 97 available_timestamp); |
94 int32_t optimal_level_samp = static_cast<int32_t>( | 98 int32_t optimal_level_samp = static_cast<int32_t>( |
95 (delay_manager_->TargetLevel() * packet_length_samples_) >> 8); | 99 (delay_manager_->TargetLevel() * packet_length_samples_) >> 8); |
96 int32_t excess_waiting_time_samp = -timestamp_diff - optimal_level_samp; | 100 int32_t excess_waiting_time_samp = -timestamp_diff - optimal_level_samp; |
97 | 101 |
98 if (excess_waiting_time_samp > optimal_level_samp / 2) { | 102 if (excess_waiting_time_samp > optimal_level_samp / 2) { |
99 // The waiting time for this packet will be longer than 1.5 | 103 // The waiting time for this packet will be longer than 1.5 |
100 // times the wanted buffer delay. Advance the clock to cut | 104 // times the wanted buffer delay. Apply fast-forward to cut the |
101 // waiting time down to the optimal. | 105 // waiting time down to the optimal. |
102 generated_noise_samples_ += excess_waiting_time_samp; | 106 noise_fast_forward_ += excess_waiting_time_samp; |
103 timestamp_diff += excess_waiting_time_samp; | 107 timestamp_diff += excess_waiting_time_samp; |
104 } | 108 } |
105 | 109 |
106 if (timestamp_diff < 0 && prev_mode == kModeRfc3389Cng) { | 110 if (timestamp_diff < 0 && prev_mode == kModeRfc3389Cng) { |
107 // Not time to play this packet yet. Wait another round before using this | 111 // Not time to play this packet yet. Wait another round before using this |
108 // packet. Keep on playing CNG from previous CNG parameters. | 112 // packet. Keep on playing CNG from previous CNG parameters. |
109 return kRfc3389CngNoPacket; | 113 return kRfc3389CngNoPacket; |
110 } else { | 114 } else { |
111 // Otherwise, go for the CNG packet now. | 115 // Otherwise, go for the CNG packet now. |
| 116 noise_fast_forward_ = 0; |
112 return kRfc3389Cng; | 117 return kRfc3389Cng; |
113 } | 118 } |
114 } | 119 } |
115 | 120 |
116 Operations DecisionLogicNormal::NoPacket(bool play_dtmf) { | 121 Operations DecisionLogicNormal::NoPacket(bool play_dtmf) { |
117 if (cng_state_ == kCngRfc3389On) { | 122 if (cng_state_ == kCngRfc3389On) { |
118 // Keep on playing comfort noise. | 123 // Keep on playing comfort noise. |
119 return kRfc3389CngNoPacket; | 124 return kRfc3389CngNoPacket; |
120 } else if (cng_state_ == kCngInternalOn) { | 125 } else if (cng_state_ == kCngInternalOn) { |
121 // Keep on playing codec internal comfort noise. | 126 // Keep on playing codec internal comfort noise. |
(...skipping 24 matching lines...) Expand all Loading... |
146 return kNormal; | 151 return kNormal; |
147 } | 152 } |
148 | 153 |
149 Operations DecisionLogicNormal::FuturePacketAvailable( | 154 Operations DecisionLogicNormal::FuturePacketAvailable( |
150 const SyncBuffer& sync_buffer, | 155 const SyncBuffer& sync_buffer, |
151 const Expand& expand, | 156 const Expand& expand, |
152 size_t decoder_frame_length, | 157 size_t decoder_frame_length, |
153 Modes prev_mode, | 158 Modes prev_mode, |
154 uint32_t target_timestamp, | 159 uint32_t target_timestamp, |
155 uint32_t available_timestamp, | 160 uint32_t available_timestamp, |
156 bool play_dtmf) { | 161 bool play_dtmf, |
| 162 size_t generated_noise_samples) { |
157 // Required packet is not available, but a future packet is. | 163 // Required packet is not available, but a future packet is. |
158 // Check if we should continue with an ongoing expand because the new packet | 164 // Check if we should continue with an ongoing expand because the new packet |
159 // is too far into the future. | 165 // is too far into the future. |
160 uint32_t timestamp_leap = available_timestamp - target_timestamp; | 166 uint32_t timestamp_leap = available_timestamp - target_timestamp; |
161 if ((prev_mode == kModeExpand) && | 167 if ((prev_mode == kModeExpand) && |
162 !ReinitAfterExpands(timestamp_leap) && | 168 !ReinitAfterExpands(timestamp_leap) && |
163 !MaxWaitForPacket() && | 169 !MaxWaitForPacket() && |
164 PacketTooEarly(timestamp_leap) && | 170 PacketTooEarly(timestamp_leap) && |
165 UnderTargetLevel()) { | 171 UnderTargetLevel()) { |
166 if (play_dtmf) { | 172 if (play_dtmf) { |
(...skipping 10 matching lines...) Expand all Loading... |
177 const size_t cur_size_samples = samples_left + | 183 const size_t cur_size_samples = samples_left + |
178 packet_buffer_.NumPacketsInBuffer() * decoder_frame_length; | 184 packet_buffer_.NumPacketsInBuffer() * decoder_frame_length; |
179 | 185 |
180 // If previous was comfort noise, then no merge is needed. | 186 // If previous was comfort noise, then no merge is needed. |
181 if (prev_mode == kModeRfc3389Cng || | 187 if (prev_mode == kModeRfc3389Cng || |
182 prev_mode == kModeCodecInternalCng) { | 188 prev_mode == kModeCodecInternalCng) { |
183 // Keep the same delay as before the CNG (or maximum 70 ms in buffer as | 189 // Keep the same delay as before the CNG (or maximum 70 ms in buffer as |
184 // safety precaution), but make sure that the number of samples in buffer | 190 // safety precaution), but make sure that the number of samples in buffer |
185 // is no higher than 4 times the optimal level. (Note that TargetLevel() | 191 // is no higher than 4 times the optimal level. (Note that TargetLevel() |
186 // is in Q8.) | 192 // is in Q8.) |
187 if (static_cast<uint32_t>(generated_noise_samples_ + target_timestamp) >= | 193 if (static_cast<uint32_t>(generated_noise_samples + target_timestamp) >= |
188 available_timestamp || | 194 available_timestamp || |
189 cur_size_samples > | 195 cur_size_samples > |
190 ((delay_manager_->TargetLevel() * packet_length_samples_) >> 8) * | 196 ((delay_manager_->TargetLevel() * packet_length_samples_) >> 8) * |
191 4) { | 197 4) { |
192 // Time to play this new packet. | 198 // Time to play this new packet. |
193 return kNormal; | 199 return kNormal; |
194 } else { | 200 } else { |
195 // Too early to play this new packet; keep on playing comfort noise. | 201 // Too early to play this new packet; keep on playing comfort noise. |
196 if (prev_mode == kModeRfc3389Cng) { | 202 if (prev_mode == kModeRfc3389Cng) { |
197 return kRfc3389CngNoPacket; | 203 return kRfc3389CngNoPacket; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 bool DecisionLogicNormal::PacketTooEarly(uint32_t timestamp_leap) const { | 235 bool DecisionLogicNormal::PacketTooEarly(uint32_t timestamp_leap) const { |
230 return timestamp_leap > | 236 return timestamp_leap > |
231 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_); | 237 static_cast<uint32_t>(output_size_samples_ * num_consecutive_expands_); |
232 } | 238 } |
233 | 239 |
234 bool DecisionLogicNormal::MaxWaitForPacket() const { | 240 bool DecisionLogicNormal::MaxWaitForPacket() const { |
235 return num_consecutive_expands_ >= kMaxWaitForPacket; | 241 return num_consecutive_expands_ >= kMaxWaitForPacket; |
236 } | 242 } |
237 | 243 |
238 } // namespace webrtc | 244 } // namespace webrtc |
OLD | NEW |