rtp_streams_synchronizer2.h 2.7 KB

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