Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / video_engine / vie_sync_module.cc
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 #include "webrtc/video_engine/vie_sync_module.h"
12
13 #include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
14 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
15 #include "webrtc/modules/video_coding/main/interface/video_coding.h"
16 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
17 #include "webrtc/system_wrappers/interface/trace.h"
18 #include "webrtc/system_wrappers/interface/trace_event.h"
19 #include "webrtc/video_engine/stream_synchronization.h"
20 #include "webrtc/video_engine/vie_channel.h"
21 #include "webrtc/voice_engine/include/voe_video_sync.h"
22
23 namespace webrtc {
24
25 enum { kSyncInterval = 1000};
26
27 int UpdateMeasurements(StreamSynchronization::Measurements* stream,
28                        const RtpRtcp& rtp_rtcp, const RtpReceiver& receiver) {
29   if (!receiver.Timestamp(&stream->latest_timestamp))
30     return -1;
31   if (!receiver.LastReceivedTimeMs(&stream->latest_receive_time_ms))
32     return -1;
33
34   uint32_t ntp_secs = 0;
35   uint32_t ntp_frac = 0;
36   uint32_t rtp_timestamp = 0;
37   if (0 != rtp_rtcp.RemoteNTP(&ntp_secs,
38                               &ntp_frac,
39                               NULL,
40                               NULL,
41                               &rtp_timestamp)) {
42     return -1;
43   }
44
45   bool new_rtcp_sr = false;
46   if (!UpdateRtcpList(
47       ntp_secs, ntp_frac, rtp_timestamp, &stream->rtcp, &new_rtcp_sr)) {
48     return -1;
49   }
50
51   return 0;
52 }
53
54 ViESyncModule::ViESyncModule(VideoCodingModule* vcm,
55                              ViEChannel* vie_channel)
56     : data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
57       vcm_(vcm),
58       vie_channel_(vie_channel),
59       video_receiver_(NULL),
60       video_rtp_rtcp_(NULL),
61       voe_channel_id_(-1),
62       voe_sync_interface_(NULL),
63       last_sync_time_(TickTime::Now()),
64       sync_() {
65 }
66
67 ViESyncModule::~ViESyncModule() {
68 }
69
70 int ViESyncModule::ConfigureSync(int voe_channel_id,
71                                  VoEVideoSync* voe_sync_interface,
72                                  RtpRtcp* video_rtcp_module,
73                                  RtpReceiver* video_receiver) {
74   CriticalSectionScoped cs(data_cs_.get());
75   voe_channel_id_ = voe_channel_id;
76   voe_sync_interface_ = voe_sync_interface;
77   video_receiver_ = video_receiver;
78   video_rtp_rtcp_ = video_rtcp_module;
79   sync_.reset(new StreamSynchronization(voe_channel_id, vie_channel_->Id()));
80
81   if (!voe_sync_interface) {
82     voe_channel_id_ = -1;
83     if (voe_channel_id >= 0) {
84       // Trying to set a voice channel but no interface exist.
85       return -1;
86     }
87     return 0;
88   }
89   return 0;
90 }
91
92 int ViESyncModule::VoiceChannel() {
93   return voe_channel_id_;
94 }
95
96 int32_t ViESyncModule::TimeUntilNextProcess() {
97   return static_cast<int32_t>(kSyncInterval -
98       (TickTime::Now() - last_sync_time_).Milliseconds());
99 }
100
101 int32_t ViESyncModule::Process() {
102   CriticalSectionScoped cs(data_cs_.get());
103   last_sync_time_ = TickTime::Now();
104
105   const int current_video_delay_ms = vcm_->Delay();
106   WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
107                "Video delay (JB + decoder) is %d ms", current_video_delay_ms);
108
109   if (voe_channel_id_ == -1) {
110     return 0;
111   }
112   assert(video_rtp_rtcp_ && voe_sync_interface_);
113   assert(sync_.get());
114
115   int audio_jitter_buffer_delay_ms = 0;
116   int playout_buffer_delay_ms = 0;
117   if (voe_sync_interface_->GetDelayEstimate(voe_channel_id_,
118                                             &audio_jitter_buffer_delay_ms,
119                                             &playout_buffer_delay_ms) != 0) {
120     // Could not get VoE delay value, probably not a valid channel Id or
121     // the channel have not received enough packets.
122     WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideo, vie_channel_->Id(),
123                  "%s: VE_GetDelayEstimate error for voice_channel %d",
124                  __FUNCTION__, voe_channel_id_);
125     return 0;
126   }
127   const int current_audio_delay_ms = audio_jitter_buffer_delay_ms +
128       playout_buffer_delay_ms;
129
130   RtpRtcp* voice_rtp_rtcp = NULL;
131   RtpReceiver* voice_receiver = NULL;
132   if (0 != voe_sync_interface_->GetRtpRtcp(voe_channel_id_, &voice_rtp_rtcp,
133                                            &voice_receiver)) {
134     return 0;
135   }
136   assert(voice_rtp_rtcp);
137   assert(voice_receiver);
138
139   if (UpdateMeasurements(&video_measurement_, *video_rtp_rtcp_,
140                          *video_receiver_) != 0) {
141     return 0;
142   }
143
144   if (UpdateMeasurements(&audio_measurement_, *voice_rtp_rtcp,
145                          *voice_receiver) != 0) {
146     return 0;
147   }
148
149   int relative_delay_ms;
150   // Calculate how much later or earlier the audio stream is compared to video.
151   if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_,
152                                    &relative_delay_ms)) {
153     return 0;
154   }
155
156   TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay", current_video_delay_ms);
157   TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay", current_audio_delay_ms);
158   TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms);
159   int target_audio_delay_ms = 0;
160   int target_video_delay_ms = current_video_delay_ms;
161   // Calculate the necessary extra audio delay and desired total video
162   // delay to get the streams in sync.
163   if (!sync_->ComputeDelays(relative_delay_ms,
164                             current_audio_delay_ms,
165                             &target_audio_delay_ms,
166                             &target_video_delay_ms)) {
167     return 0;
168   }
169
170   WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
171                "Set delay current(a=%d v=%d rel=%d) target(a=%d v=%d)",
172                current_audio_delay_ms, current_video_delay_ms,
173                relative_delay_ms,
174                target_audio_delay_ms, target_video_delay_ms);
175   if (voe_sync_interface_->SetMinimumPlayoutDelay(
176       voe_channel_id_, target_audio_delay_ms) == -1) {
177     WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, vie_channel_->Id(),
178                  "Error setting voice delay");
179   }
180   vcm_->SetMinimumPlayoutDelay(target_video_delay_ms);
181   return 0;
182 }
183
184 int ViESyncModule::SetTargetBufferingDelay(int target_delay_ms) {
185   CriticalSectionScoped cs(data_cs_.get());
186  if (!voe_sync_interface_) {
187     WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
188                  "voe_sync_interface_ NULL, can't set playout delay.");
189     return -1;
190   }
191   sync_->SetTargetBufferingDelay(target_delay_ms);
192   // Setting initial playout delay to voice engine (video engine is updated via
193   // the VCM interface).
194   voe_sync_interface_->SetInitialPlayoutDelay(voe_channel_id_,
195                                               target_delay_ms);
196   return 0;
197 }
198
199 }  // namespace webrtc