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 
47   // Lowers the audio delay by 10%. Can be used to recover from errors.
48   void ReduceAudioDelay();
49 
50   // Lowers the video delay by 10%. Can be used to recover from errors.
51   void ReduceVideoDelay();
52 
audio_stream_id()53   uint32_t audio_stream_id() const { return audio_stream_id_; }
video_stream_id()54   uint32_t video_stream_id() const { return video_stream_id_; }
55 
56  private:
57   struct SynchronizationDelays {
58     int extra_ms = 0;
59     int last_ms = 0;
60   };
61 
62   const uint32_t video_stream_id_;
63   const uint32_t audio_stream_id_;
64   SynchronizationDelays audio_delay_;
65   SynchronizationDelays video_delay_;
66   int base_target_delay_ms_;
67   int avg_diff_ms_;
68 };
69 }  // namespace webrtc
70 
71 #endif  // VIDEO_STREAM_SYNCHRONIZATION_H_
72