blob: 1aba62d1e72c46c5f154ed1e370d678f9061b5d1 [file] [log] [blame]
stefan@webrtc.org5f284982012-06-28 07:51:16 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef VIDEO_STREAM_SYNCHRONIZATION_H_
12#define VIDEO_STREAM_SYNCHRONIZATION_H_
stefan@webrtc.org5f284982012-06-28 07:51:16 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stdint.h>
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "system_wrappers/include/rtp_to_ntp_estimator.h"
stefan@webrtc.org5f284982012-06-28 07:51:16 +000017
18namespace webrtc {
19
stefan@webrtc.org5f284982012-06-28 07:51:16 +000020class StreamSynchronization {
21 public:
22 struct Measurements {
asaperssonfe50b4d2016-12-22 07:53:51 -080023 Measurements() : latest_receive_time_ms(0), latest_timestamp(0) {}
24 RtpToNtpEstimator rtp_to_ntp;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000025 int64_t latest_receive_time_ms;
26 uint32_t latest_timestamp;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000027 };
28
Åsa Persson74d2b1d2020-02-10 16:33:29 +010029 StreamSynchronization(uint32_t video_stream_id, uint32_t audio_stream_id);
stefan@webrtc.org5f284982012-06-28 07:51:16 +000030
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000031 bool ComputeDelays(int relative_delay_ms,
32 int current_audio_delay_ms,
Åsa Persson4fc52c82019-12-09 12:41:56 +010033 int* total_audio_delay_target_ms,
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000034 int* total_video_delay_target_ms);
35
Åsa Persson4fc52c82019-12-09 12:41:56 +010036 // 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.
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000039 static bool ComputeRelativeDelay(const Measurements& audio_measurement,
40 const Measurements& video_measurement,
41 int* relative_delay_ms);
Åsa Persson4fc52c82019-12-09 12:41:56 +010042
43 // Set target buffering delay. Audio and video will be delayed by at least
44 // |target_delay_ms|.
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +000045 void SetTargetBufferingDelay(int target_delay_ms);
stefan@webrtc.org5f284982012-06-28 07:51:16 +000046
Åsa Persson74d2b1d2020-02-10 16:33:29 +010047 uint32_t audio_stream_id() const { return audio_stream_id_; }
48 uint32_t video_stream_id() const { return video_stream_id_; }
49
stefan@webrtc.org5f284982012-06-28 07:51:16 +000050 private:
mflodman4cd27902016-08-05 06:28:45 -070051 struct SynchronizationDelays {
Åsa Persson4fc52c82019-12-09 12:41:56 +010052 int extra_ms = 0;
53 int last_ms = 0;
mflodman4cd27902016-08-05 06:28:45 -070054 };
55
Åsa Persson74d2b1d2020-02-10 16:33:29 +010056 const uint32_t video_stream_id_;
57 const uint32_t audio_stream_id_;
Åsa Persson4fc52c82019-12-09 12:41:56 +010058 SynchronizationDelays audio_delay_;
59 SynchronizationDelays video_delay_;
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +000060 int base_target_delay_ms_;
pwestin@webrtc.org63117332013-04-22 18:57:14 +000061 int avg_diff_ms_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000062};
stefan@webrtc.org5f284982012-06-28 07:51:16 +000063} // namespace webrtc
64
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#endif // VIDEO_STREAM_SYNCHRONIZATION_H_