Tommi | ad84d02 | 2020-05-10 19:03:43 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 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 VIDEO_RTP_STREAMS_SYNCHRONIZER2_H_ |
| 12 | #define VIDEO_RTP_STREAMS_SYNCHRONIZER2_H_ |
| 13 | |
| 14 | #include <memory> |
| 15 | |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 16 | #include "api/sequence_checker.h" |
Danil Chapovalov | 03f8b8a | 2022-07-18 13:11:42 +0200 | [diff] [blame] | 17 | #include "api/task_queue/task_queue_base.h" |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 18 | #include "rtc_base/system/no_unique_address.h" |
Tommi | a0a4480 | 2020-05-13 18:27:26 +0200 | [diff] [blame] | 19 | #include "rtc_base/task_utils/repeating_task.h" |
Tommi | ad84d02 | 2020-05-10 19:03:43 +0200 | [diff] [blame] | 20 | #include "video/stream_synchronization.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | class Syncable; |
| 25 | |
| 26 | namespace internal { |
| 27 | |
| 28 | // RtpStreamsSynchronizer is responsible for synchronizing audio and video for |
| 29 | // a given audio receive stream and video receive stream. |
| 30 | class RtpStreamsSynchronizer { |
| 31 | public: |
| 32 | RtpStreamsSynchronizer(TaskQueueBase* main_queue, Syncable* syncable_video); |
| 33 | ~RtpStreamsSynchronizer(); |
| 34 | |
| 35 | void ConfigureSync(Syncable* syncable_audio); |
| 36 | |
| 37 | // Gets the estimated playout NTP timestamp for the video frame with |
Artem Titov | ab30d72 | 2021-07-27 16:22:11 +0200 | [diff] [blame] | 38 | // `rtp_timestamp` and the sync offset between the current played out audio |
Tommi | ad84d02 | 2020-05-10 19:03:43 +0200 | [diff] [blame] | 39 | // frame and the video frame. Returns true on success, false otherwise. |
Artem Titov | ab30d72 | 2021-07-27 16:22:11 +0200 | [diff] [blame] | 40 | // The `estimated_freq_khz` is the frequency used in the RTP to NTP timestamp |
Tommi | ad84d02 | 2020-05-10 19:03:43 +0200 | [diff] [blame] | 41 | // conversion. |
| 42 | bool GetStreamSyncOffsetInMs(uint32_t rtp_timestamp, |
| 43 | int64_t render_time_ms, |
| 44 | int64_t* video_playout_ntp_ms, |
| 45 | int64_t* stream_offset_ms, |
| 46 | double* estimated_freq_khz) const; |
| 47 | |
| 48 | private: |
Tommi | ad84d02 | 2020-05-10 19:03:43 +0200 | [diff] [blame] | 49 | void UpdateDelay(); |
| 50 | |
| 51 | TaskQueueBase* const task_queue_; |
| 52 | |
| 53 | // Used to check if we're running on the main thread/task queue. |
| 54 | // The reason we currently don't use RTC_DCHECK_RUN_ON(task_queue_) is because |
| 55 | // we might be running on an rtc::Thread implementation of TaskQueue, which |
| 56 | // does not consistently set itself as the active TaskQueue. |
| 57 | // Instead, we rely on a SequenceChecker for now. |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 58 | RTC_NO_UNIQUE_ADDRESS SequenceChecker main_checker_; |
Tommi | ad84d02 | 2020-05-10 19:03:43 +0200 | [diff] [blame] | 59 | |
| 60 | Syncable* const syncable_video_; |
| 61 | |
| 62 | Syncable* syncable_audio_ RTC_GUARDED_BY(main_checker_) = nullptr; |
| 63 | std::unique_ptr<StreamSynchronization> sync_ RTC_GUARDED_BY(main_checker_); |
| 64 | StreamSynchronization::Measurements audio_measurement_ |
| 65 | RTC_GUARDED_BY(main_checker_); |
| 66 | StreamSynchronization::Measurements video_measurement_ |
| 67 | RTC_GUARDED_BY(main_checker_); |
Tommi | a0a4480 | 2020-05-13 18:27:26 +0200 | [diff] [blame] | 68 | RepeatingTaskHandle repeating_task_ RTC_GUARDED_BY(main_checker_); |
Tommi | ad84d02 | 2020-05-10 19:03:43 +0200 | [diff] [blame] | 69 | int64_t last_stats_log_ms_ RTC_GUARDED_BY(&main_checker_); |
Tommi | ad84d02 | 2020-05-10 19:03:43 +0200 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | } // namespace internal |
| 73 | } // namespace webrtc |
| 74 | |
| 75 | #endif // VIDEO_RTP_STREAMS_SYNCHRONIZER2_H_ |