rtp_streams_synchronizer2.h 2.8 KB

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