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 
11 #ifndef VIDEO_STREAM_SYNCHRONIZATION_H_
12 #define VIDEO_STREAM_SYNCHRONIZATION_H_
13 
14 #include <stdint.h>
15 
16 #include "system_wrappers/include/rtp_to_ntp_estimator.h"
17 
18 namespace webrtc {
19 
20 class StreamSynchronization {
21  public:
22   struct Measurements {
MeasurementsMeasurements23     Measurements() : latest_receive_time_ms(0), latest_timestamp(0) {}
24     RtpToNtpEstimator rtp_to_ntp;
25     int64_t latest_receive_time_ms;
26     uint32_t latest_timestamp;
27   };
28 
29   StreamSynchronization(uint32_t video_stream_id, uint32_t audio_stream_id);
30 
31   bool ComputeDelays(int relative_delay_ms,
32                      int current_audio_delay_ms,
33                      int* total_audio_delay_target_ms,
34                      int* total_video_delay_target_ms);
35 
36   // On success |relative_delay_ms| contains the number of milliseconds later
37   // video is rendered relative audio. If audio is played back later than video
38   // |relative_delay_ms| will be negative.
39   static bool ComputeRelativeDelay(const Measurements& audio_measurement,
40                                    const Measurements& video_measurement,
41                                    int* relative_delay_ms);
42 
43   // Set target buffering delay. Audio and video will be delayed by at least
44   // |target_delay_ms|.
45   void SetTargetBufferingDelay(int target_delay_ms);
46 
audio_stream_id()47   uint32_t audio_stream_id() const { return audio_stream_id_; }
video_stream_id()48   uint32_t video_stream_id() const { return video_stream_id_; }
49 
50  private:
51   struct SynchronizationDelays {
52     int extra_ms = 0;
53     int last_ms = 0;
54   };
55 
56   const uint32_t video_stream_id_;
57   const uint32_t audio_stream_id_;
58   SynchronizationDelays audio_delay_;
59   SynchronizationDelays video_delay_;
60   int base_target_delay_ms_;
61   int avg_diff_ms_;
62 };
63 }  // namespace webrtc
64 
65 #endif  // VIDEO_STREAM_SYNCHRONIZATION_H_
66