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