stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef VIDEO_STREAM_SYNCHRONIZATION_H_ |
| 12 | #define VIDEO_STREAM_SYNCHRONIZATION_H_ |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <stdint.h> |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "system_wrappers/include/rtp_to_ntp_estimator.h" |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 20 | class StreamSynchronization { |
| 21 | public: |
| 22 | struct Measurements { |
asapersson | fe50b4d | 2016-12-22 15:53:51 | [diff] [blame] | 23 | Measurements() : latest_receive_time_ms(0), latest_timestamp(0) {} |
| 24 | RtpToNtpEstimator rtp_to_ntp; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 25 | int64_t latest_receive_time_ms; |
| 26 | uint32_t latest_timestamp; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 27 | }; |
| 28 | |
Åsa Persson | 74d2b1d | 2020-02-10 15:33:29 | [diff] [blame] | 29 | StreamSynchronization(uint32_t video_stream_id, uint32_t audio_stream_id); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 30 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 31 | bool ComputeDelays(int relative_delay_ms, |
| 32 | int current_audio_delay_ms, |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 33 | int* total_audio_delay_target_ms, |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 34 | int* total_video_delay_target_ms); |
| 35 | |
Artem Titov | ab30d72 | 2021-07-27 14:22:11 | [diff] [blame] | 36 | // On success `relative_delay_ms` contains the number of milliseconds later |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 37 | // video is rendered relative audio. If audio is played back later than video |
Artem Titov | ab30d72 | 2021-07-27 14:22:11 | [diff] [blame] | 38 | // `relative_delay_ms` will be negative. |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 39 | static bool ComputeRelativeDelay(const Measurements& audio_measurement, |
| 40 | const Measurements& video_measurement, |
| 41 | int* relative_delay_ms); |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 42 | |
| 43 | // Set target buffering delay. Audio and video will be delayed by at least |
Artem Titov | ab30d72 | 2021-07-27 14:22:11 | [diff] [blame] | 44 | // `target_delay_ms`. |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 | [diff] [blame] | 45 | void SetTargetBufferingDelay(int target_delay_ms); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 46 | |
Ivo Creusen | bef7b05 | 2020-09-08 14:30:25 | [diff] [blame] | 47 | // Lowers the audio delay by 10%. Can be used to recover from errors. |
| 48 | void ReduceAudioDelay(); |
| 49 | |
| 50 | // Lowers the video delay by 10%. Can be used to recover from errors. |
| 51 | void ReduceVideoDelay(); |
| 52 | |
Åsa Persson | 74d2b1d | 2020-02-10 15:33:29 | [diff] [blame] | 53 | uint32_t audio_stream_id() const { return audio_stream_id_; } |
| 54 | uint32_t video_stream_id() const { return video_stream_id_; } |
| 55 | |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 56 | private: |
mflodman | 4cd2790 | 2016-08-05 13:28:45 | [diff] [blame] | 57 | struct SynchronizationDelays { |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 58 | int extra_ms = 0; |
| 59 | int last_ms = 0; |
mflodman | 4cd2790 | 2016-08-05 13:28:45 | [diff] [blame] | 60 | }; |
| 61 | |
Åsa Persson | 74d2b1d | 2020-02-10 15:33:29 | [diff] [blame] | 62 | const uint32_t video_stream_id_; |
| 63 | const uint32_t audio_stream_id_; |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 64 | SynchronizationDelays audio_delay_; |
| 65 | SynchronizationDelays video_delay_; |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 | [diff] [blame] | 66 | int base_target_delay_ms_; |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 | [diff] [blame] | 67 | int avg_diff_ms_; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 68 | }; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 69 | } // namespace webrtc |
| 70 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 71 | #endif // VIDEO_STREAM_SYNCHRONIZATION_H_ |