| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 |
| 11 #include "webrtc/video/stream_synchronization.h" | 11 #include "webrtc/video/stream_synchronization.h" |
| 12 | 12 |
| 13 #include <assert.h> | 13 #include <assert.h> |
| 14 #include <math.h> | 14 #include <math.h> |
| 15 #include <stdlib.h> | 15 #include <stdlib.h> |
| 16 | 16 |
| 17 #include <algorithm> | 17 #include <algorithm> |
| 18 | 18 |
| 19 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
| 20 | 20 |
| 21 namespace webrtc { | 21 namespace webrtc { |
| 22 | 22 |
| 23 static const int kMaxChangeMs = 80; | 23 static const int kMaxChangeMs = 80; |
| 24 static const int kMaxDeltaDelayMs = 10000; | 24 static const int kMaxDeltaDelayMs = 10000; |
| 25 static const int kFilterLength = 4; | 25 static const int kFilterLength = 4; |
| 26 // Minimum difference between audio and video to warrant a change. | 26 // Minimum difference between audio and video to warrant a change. |
| 27 static const int kMinDeltaMs = 30; | 27 static const int kMinDeltaMs = 30; |
| 28 | 28 |
| 29 StreamSynchronization::StreamSynchronization(uint32_t video_primary_ssrc, | 29 StreamSynchronization::StreamSynchronization(int video_stream_id, |
| 30 int audio_channel_id) | 30 int audio_stream_id) |
| 31 : video_primary_ssrc_(video_primary_ssrc), | 31 : video_stream_id_(video_stream_id), |
| 32 audio_channel_id_(audio_channel_id), | 32 audio_stream_id_(audio_stream_id), |
| 33 base_target_delay_ms_(0), | 33 base_target_delay_ms_(0), |
| 34 avg_diff_ms_(0) { | 34 avg_diff_ms_(0) { |
| 35 } | 35 } |
| 36 | 36 |
| 37 bool StreamSynchronization::ComputeRelativeDelay( | 37 bool StreamSynchronization::ComputeRelativeDelay( |
| 38 const Measurements& audio_measurement, | 38 const Measurements& audio_measurement, |
| 39 const Measurements& video_measurement, | 39 const Measurements& video_measurement, |
| 40 int* relative_delay_ms) { | 40 int* relative_delay_ms) { |
| 41 assert(relative_delay_ms); | 41 assert(relative_delay_ms); |
| 42 int64_t audio_last_capture_time_ms; | 42 int64_t audio_last_capture_time_ms; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 65 | 65 |
| 66 bool StreamSynchronization::ComputeDelays(int relative_delay_ms, | 66 bool StreamSynchronization::ComputeDelays(int relative_delay_ms, |
| 67 int current_audio_delay_ms, | 67 int current_audio_delay_ms, |
| 68 int* total_audio_delay_target_ms, | 68 int* total_audio_delay_target_ms, |
| 69 int* total_video_delay_target_ms) { | 69 int* total_video_delay_target_ms) { |
| 70 assert(total_audio_delay_target_ms && total_video_delay_target_ms); | 70 assert(total_audio_delay_target_ms && total_video_delay_target_ms); |
| 71 | 71 |
| 72 int current_video_delay_ms = *total_video_delay_target_ms; | 72 int current_video_delay_ms = *total_video_delay_target_ms; |
| 73 LOG(LS_VERBOSE) << "Audio delay: " << current_audio_delay_ms | 73 LOG(LS_VERBOSE) << "Audio delay: " << current_audio_delay_ms |
| 74 << " current diff: " << relative_delay_ms | 74 << " current diff: " << relative_delay_ms |
| 75 << " for channel " << audio_channel_id_; | 75 << " for stream " << audio_stream_id_; |
| 76 // Calculate the difference between the lowest possible video delay and | 76 // Calculate the difference between the lowest possible video delay and |
| 77 // the current audio delay. | 77 // the current audio delay. |
| 78 int current_diff_ms = current_video_delay_ms - current_audio_delay_ms + | 78 int current_diff_ms = current_video_delay_ms - current_audio_delay_ms + |
| 79 relative_delay_ms; | 79 relative_delay_ms; |
| 80 | 80 |
| 81 avg_diff_ms_ = ((kFilterLength - 1) * avg_diff_ms_ + | 81 avg_diff_ms_ = ((kFilterLength - 1) * avg_diff_ms_ + |
| 82 current_diff_ms) / kFilterLength; | 82 current_diff_ms) / kFilterLength; |
| 83 if (abs(avg_diff_ms_) < kMinDeltaMs) { | 83 if (abs(avg_diff_ms_) < kMinDeltaMs) { |
| 84 // Don't adjust if the diff is within our margin. | 84 // Don't adjust if the diff is within our margin. |
| 85 return false; | 85 return false; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 159 |
| 160 // Verify we don't go above the maximum allowed audio delay. | 160 // Verify we don't go above the maximum allowed audio delay. |
| 161 new_audio_delay_ms = | 161 new_audio_delay_ms = |
| 162 std::min(new_audio_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs); | 162 std::min(new_audio_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs); |
| 163 | 163 |
| 164 // Remember our last audio and video delays. | 164 // Remember our last audio and video delays. |
| 165 channel_delay_.last_video_delay_ms = new_video_delay_ms; | 165 channel_delay_.last_video_delay_ms = new_video_delay_ms; |
| 166 channel_delay_.last_audio_delay_ms = new_audio_delay_ms; | 166 channel_delay_.last_audio_delay_ms = new_audio_delay_ms; |
| 167 | 167 |
| 168 LOG(LS_VERBOSE) << "Sync video delay " << new_video_delay_ms | 168 LOG(LS_VERBOSE) << "Sync video delay " << new_video_delay_ms |
| 169 << " for video primary SSRC " << video_primary_ssrc_ | 169 << " for video stream " << video_stream_id_ |
| 170 << " and audio delay " << channel_delay_.extra_audio_delay_ms | 170 << " and audio delay " << channel_delay_.extra_audio_delay_ms |
| 171 << " for audio channel " << audio_channel_id_; | 171 << " for audio stream " << audio_stream_id_; |
| 172 | 172 |
| 173 // Return values. | 173 // Return values. |
| 174 *total_video_delay_target_ms = new_video_delay_ms; | 174 *total_video_delay_target_ms = new_video_delay_ms; |
| 175 *total_audio_delay_target_ms = new_audio_delay_ms; | 175 *total_audio_delay_target_ms = new_audio_delay_ms; |
| 176 return true; | 176 return true; |
| 177 } | 177 } |
| 178 | 178 |
| 179 void StreamSynchronization::SetTargetBufferingDelay(int target_delay_ms) { | 179 void StreamSynchronization::SetTargetBufferingDelay(int target_delay_ms) { |
| 180 // Initial extra delay for audio (accounting for existing extra delay). | 180 // Initial extra delay for audio (accounting for existing extra delay). |
| 181 channel_delay_.extra_audio_delay_ms += | 181 channel_delay_.extra_audio_delay_ms += |
| 182 target_delay_ms - base_target_delay_ms_; | 182 target_delay_ms - base_target_delay_ms_; |
| 183 channel_delay_.last_audio_delay_ms += | 183 channel_delay_.last_audio_delay_ms += |
| 184 target_delay_ms - base_target_delay_ms_; | 184 target_delay_ms - base_target_delay_ms_; |
| 185 | 185 |
| 186 // The video delay is compared to the last value (and how much we can update | 186 // The video delay is compared to the last value (and how much we can update |
| 187 // is limited by that as well). | 187 // is limited by that as well). |
| 188 channel_delay_.last_video_delay_ms += | 188 channel_delay_.last_video_delay_ms += |
| 189 target_delay_ms - base_target_delay_ms_; | 189 target_delay_ms - base_target_delay_ms_; |
| 190 | 190 |
| 191 channel_delay_.extra_video_delay_ms += | 191 channel_delay_.extra_video_delay_ms += |
| 192 target_delay_ms - base_target_delay_ms_; | 192 target_delay_ms - base_target_delay_ms_; |
| 193 | 193 |
| 194 // Video is already delayed by the desired amount. | 194 // Video is already delayed by the desired amount. |
| 195 base_target_delay_ms_ = target_delay_ms; | 195 base_target_delay_ms_ = target_delay_ms; |
| 196 } | 196 } |
| 197 | 197 |
| 198 } // namespace webrtc | 198 } // namespace webrtc |
| OLD | NEW |