1/*
2 *  Copyright (c) 2013 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 <string.h>
12
13#include <map>
14#include <vector>
15
16#include "webrtc/audio/audio_receive_stream.h"
17#include "webrtc/audio/audio_send_stream.h"
18#include "webrtc/audio/audio_state.h"
19#include "webrtc/audio/scoped_voe_interface.h"
20#include "webrtc/base/checks.h"
21#include "webrtc/base/logging.h"
22#include "webrtc/base/scoped_ptr.h"
23#include "webrtc/base/thread_annotations.h"
24#include "webrtc/base/thread_checker.h"
25#include "webrtc/base/trace_event.h"
26#include "webrtc/call.h"
27#include "webrtc/call/bitrate_allocator.h"
28#include "webrtc/call/congestion_controller.h"
29#include "webrtc/call/rtc_event_log.h"
30#include "webrtc/common.h"
31#include "webrtc/config.h"
32#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
33#include "webrtc/modules/pacing/paced_sender.h"
34#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
35#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
36#include "webrtc/modules/utility/include/process_thread.h"
37#include "webrtc/system_wrappers/include/cpu_info.h"
38#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
39#include "webrtc/system_wrappers/include/metrics.h"
40#include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
41#include "webrtc/system_wrappers/include/trace.h"
42#include "webrtc/video/call_stats.h"
43#include "webrtc/video/video_receive_stream.h"
44#include "webrtc/video/video_send_stream.h"
45#include "webrtc/voice_engine/include/voe_codec.h"
46
47namespace webrtc {
48
49const int Call::Config::kDefaultStartBitrateBps = 300000;
50
51namespace internal {
52
53class Call : public webrtc::Call, public PacketReceiver,
54             public BitrateObserver {
55 public:
56  explicit Call(const Call::Config& config);
57  virtual ~Call();
58
59  PacketReceiver* Receiver() override;
60
61  webrtc::AudioSendStream* CreateAudioSendStream(
62      const webrtc::AudioSendStream::Config& config) override;
63  void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override;
64
65  webrtc::AudioReceiveStream* CreateAudioReceiveStream(
66      const webrtc::AudioReceiveStream::Config& config) override;
67  void DestroyAudioReceiveStream(
68      webrtc::AudioReceiveStream* receive_stream) override;
69
70  webrtc::VideoSendStream* CreateVideoSendStream(
71      const webrtc::VideoSendStream::Config& config,
72      const VideoEncoderConfig& encoder_config) override;
73  void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override;
74
75  webrtc::VideoReceiveStream* CreateVideoReceiveStream(
76      const webrtc::VideoReceiveStream::Config& config) override;
77  void DestroyVideoReceiveStream(
78      webrtc::VideoReceiveStream* receive_stream) override;
79
80  Stats GetStats() const override;
81
82  DeliveryStatus DeliverPacket(MediaType media_type,
83                               const uint8_t* packet,
84                               size_t length,
85                               const PacketTime& packet_time) override;
86
87  void SetBitrateConfig(
88      const webrtc::Call::Config::BitrateConfig& bitrate_config) override;
89  void SignalNetworkState(NetworkState state) override;
90
91  void OnSentPacket(const rtc::SentPacket& sent_packet) override;
92
93  // Implements BitrateObserver.
94  void OnNetworkChanged(uint32_t bitrate_bps, uint8_t fraction_loss,
95                        int64_t rtt_ms) override;
96
97 private:
98  DeliveryStatus DeliverRtcp(MediaType media_type, const uint8_t* packet,
99                             size_t length);
100  DeliveryStatus DeliverRtp(MediaType media_type,
101                            const uint8_t* packet,
102                            size_t length,
103                            const PacketTime& packet_time);
104
105  void ConfigureSync(const std::string& sync_group)
106      EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
107
108  VoiceEngine* voice_engine() {
109    internal::AudioState* audio_state =
110        static_cast<internal::AudioState*>(config_.audio_state.get());
111    if (audio_state)
112      return audio_state->voice_engine();
113    else
114      return nullptr;
115  }
116
117  void UpdateSendHistograms() EXCLUSIVE_LOCKS_REQUIRED(&bitrate_crit_);
118  void UpdateReceiveHistograms();
119
120  Clock* const clock_;
121
122  const int num_cpu_cores_;
123  const rtc::scoped_ptr<ProcessThread> module_process_thread_;
124  const rtc::scoped_ptr<CallStats> call_stats_;
125  const rtc::scoped_ptr<BitrateAllocator> bitrate_allocator_;
126  Call::Config config_;
127  rtc::ThreadChecker configuration_thread_checker_;
128
129  bool network_enabled_;
130
131  rtc::scoped_ptr<RWLockWrapper> receive_crit_;
132  // Audio and Video receive streams are owned by the client that creates them.
133  std::map<uint32_t, AudioReceiveStream*> audio_receive_ssrcs_
134      GUARDED_BY(receive_crit_);
135  std::map<uint32_t, VideoReceiveStream*> video_receive_ssrcs_
136      GUARDED_BY(receive_crit_);
137  std::set<VideoReceiveStream*> video_receive_streams_
138      GUARDED_BY(receive_crit_);
139  std::map<std::string, AudioReceiveStream*> sync_stream_mapping_
140      GUARDED_BY(receive_crit_);
141
142  rtc::scoped_ptr<RWLockWrapper> send_crit_;
143  // Audio and Video send streams are owned by the client that creates them.
144  std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_ GUARDED_BY(send_crit_);
145  std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ GUARDED_BY(send_crit_);
146  std::set<VideoSendStream*> video_send_streams_ GUARDED_BY(send_crit_);
147
148  VideoSendStream::RtpStateMap suspended_video_send_ssrcs_;
149
150  RtcEventLog* event_log_ = nullptr;
151
152  // The following members are only accessed (exclusively) from one thread and
153  // from the destructor, and therefore doesn't need any explicit
154  // synchronization.
155  int64_t received_video_bytes_;
156  int64_t received_audio_bytes_;
157  int64_t received_rtcp_bytes_;
158  int64_t first_rtp_packet_received_ms_;
159  int64_t last_rtp_packet_received_ms_;
160  int64_t first_packet_sent_ms_;
161
162  // TODO(holmer): Remove this lock once BitrateController no longer calls
163  // OnNetworkChanged from multiple threads.
164  rtc::CriticalSection bitrate_crit_;
165  int64_t estimated_send_bitrate_sum_kbits_ GUARDED_BY(&bitrate_crit_);
166  int64_t pacer_bitrate_sum_kbits_ GUARDED_BY(&bitrate_crit_);
167  int64_t num_bitrate_updates_ GUARDED_BY(&bitrate_crit_);
168
169  const rtc::scoped_ptr<CongestionController> congestion_controller_;
170
171  RTC_DISALLOW_COPY_AND_ASSIGN(Call);
172};
173}  // namespace internal
174
175Call* Call::Create(const Call::Config& config) {
176  return new internal::Call(config);
177}
178
179namespace internal {
180
181Call::Call(const Call::Config& config)
182    : clock_(Clock::GetRealTimeClock()),
183      num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
184      module_process_thread_(ProcessThread::Create("ModuleProcessThread")),
185      call_stats_(new CallStats(clock_)),
186      bitrate_allocator_(new BitrateAllocator()),
187      config_(config),
188      network_enabled_(true),
189      receive_crit_(RWLockWrapper::CreateRWLock()),
190      send_crit_(RWLockWrapper::CreateRWLock()),
191      received_video_bytes_(0),
192      received_audio_bytes_(0),
193      received_rtcp_bytes_(0),
194      first_rtp_packet_received_ms_(-1),
195      last_rtp_packet_received_ms_(-1),
196      first_packet_sent_ms_(-1),
197      estimated_send_bitrate_sum_kbits_(0),
198      pacer_bitrate_sum_kbits_(0),
199      num_bitrate_updates_(0),
200      congestion_controller_(
201          new CongestionController(module_process_thread_.get(),
202                                   call_stats_.get(),
203                                   this)) {
204  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
205  RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0);
206  RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps,
207                config.bitrate_config.min_bitrate_bps);
208  if (config.bitrate_config.max_bitrate_bps != -1) {
209    RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps,
210                  config.bitrate_config.start_bitrate_bps);
211  }
212  if (config.audio_state.get()) {
213    ScopedVoEInterface<VoECodec> voe_codec(voice_engine());
214    event_log_ = voe_codec->GetEventLog();
215  }
216
217  Trace::CreateTrace();
218  module_process_thread_->Start();
219  module_process_thread_->RegisterModule(call_stats_.get());
220
221  congestion_controller_->SetBweBitrates(
222      config_.bitrate_config.min_bitrate_bps,
223      config_.bitrate_config.start_bitrate_bps,
224      config_.bitrate_config.max_bitrate_bps);
225
226  congestion_controller_->GetBitrateController()->SetEventLog(event_log_);
227}
228
229Call::~Call() {
230  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
231  UpdateSendHistograms();
232  UpdateReceiveHistograms();
233  RTC_CHECK(audio_send_ssrcs_.empty());
234  RTC_CHECK(video_send_ssrcs_.empty());
235  RTC_CHECK(video_send_streams_.empty());
236  RTC_CHECK(audio_receive_ssrcs_.empty());
237  RTC_CHECK(video_receive_ssrcs_.empty());
238  RTC_CHECK(video_receive_streams_.empty());
239
240  module_process_thread_->DeRegisterModule(call_stats_.get());
241  module_process_thread_->Stop();
242  Trace::ReturnTrace();
243}
244
245void Call::UpdateSendHistograms() {
246  if (num_bitrate_updates_ == 0 || first_packet_sent_ms_ == -1)
247    return;
248  int64_t elapsed_sec =
249      (clock_->TimeInMilliseconds() - first_packet_sent_ms_) / 1000;
250  if (elapsed_sec < metrics::kMinRunTimeInSeconds)
251    return;
252  int send_bitrate_kbps =
253      estimated_send_bitrate_sum_kbits_ / num_bitrate_updates_;
254  int pacer_bitrate_kbps = pacer_bitrate_sum_kbits_ / num_bitrate_updates_;
255  if (send_bitrate_kbps > 0) {
256    RTC_HISTOGRAM_COUNTS_SPARSE_100000("WebRTC.Call.EstimatedSendBitrateInKbps",
257                                       send_bitrate_kbps);
258  }
259  if (pacer_bitrate_kbps > 0) {
260    RTC_HISTOGRAM_COUNTS_SPARSE_100000("WebRTC.Call.PacerBitrateInKbps",
261                                       pacer_bitrate_kbps);
262  }
263}
264
265void Call::UpdateReceiveHistograms() {
266  if (first_rtp_packet_received_ms_ == -1)
267    return;
268  int64_t elapsed_sec =
269      (last_rtp_packet_received_ms_ - first_rtp_packet_received_ms_) / 1000;
270  if (elapsed_sec < metrics::kMinRunTimeInSeconds)
271    return;
272  int audio_bitrate_kbps = received_audio_bytes_ * 8 / elapsed_sec / 1000;
273  int video_bitrate_kbps = received_video_bytes_ * 8 / elapsed_sec / 1000;
274  int rtcp_bitrate_bps = received_rtcp_bytes_ * 8 / elapsed_sec;
275  if (video_bitrate_kbps > 0) {
276    RTC_HISTOGRAM_COUNTS_SPARSE_100000("WebRTC.Call.VideoBitrateReceivedInKbps",
277                                       video_bitrate_kbps);
278  }
279  if (audio_bitrate_kbps > 0) {
280    RTC_HISTOGRAM_COUNTS_SPARSE_100000("WebRTC.Call.AudioBitrateReceivedInKbps",
281                                       audio_bitrate_kbps);
282  }
283  if (rtcp_bitrate_bps > 0) {
284    RTC_HISTOGRAM_COUNTS_SPARSE_100000("WebRTC.Call.RtcpBitrateReceivedInBps",
285                                       rtcp_bitrate_bps);
286  }
287  RTC_HISTOGRAM_COUNTS_SPARSE_100000(
288      "WebRTC.Call.BitrateReceivedInKbps",
289      audio_bitrate_kbps + video_bitrate_kbps + rtcp_bitrate_bps / 1000);
290}
291
292PacketReceiver* Call::Receiver() {
293  // TODO(solenberg): Some test cases in EndToEndTest use this from a different
294  // thread. Re-enable once that is fixed.
295  // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
296  return this;
297}
298
299webrtc::AudioSendStream* Call::CreateAudioSendStream(
300    const webrtc::AudioSendStream::Config& config) {
301  TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream");
302  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
303  AudioSendStream* send_stream = new AudioSendStream(
304      config, config_.audio_state, congestion_controller_.get());
305  if (!network_enabled_)
306    send_stream->SignalNetworkState(kNetworkDown);
307  {
308    WriteLockScoped write_lock(*send_crit_);
309    RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
310               audio_send_ssrcs_.end());
311    audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
312  }
313  return send_stream;
314}
315
316void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
317  TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream");
318  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
319  RTC_DCHECK(send_stream != nullptr);
320
321  send_stream->Stop();
322
323  webrtc::internal::AudioSendStream* audio_send_stream =
324      static_cast<webrtc::internal::AudioSendStream*>(send_stream);
325  {
326    WriteLockScoped write_lock(*send_crit_);
327    size_t num_deleted = audio_send_ssrcs_.erase(
328        audio_send_stream->config().rtp.ssrc);
329    RTC_DCHECK(num_deleted == 1);
330  }
331  delete audio_send_stream;
332}
333
334webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream(
335    const webrtc::AudioReceiveStream::Config& config) {
336  TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream");
337  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
338  AudioReceiveStream* receive_stream = new AudioReceiveStream(
339      congestion_controller_.get(), config, config_.audio_state);
340  {
341    WriteLockScoped write_lock(*receive_crit_);
342    RTC_DCHECK(audio_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
343               audio_receive_ssrcs_.end());
344    audio_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
345    ConfigureSync(config.sync_group);
346  }
347  return receive_stream;
348}
349
350void Call::DestroyAudioReceiveStream(
351    webrtc::AudioReceiveStream* receive_stream) {
352  TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream");
353  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
354  RTC_DCHECK(receive_stream != nullptr);
355  webrtc::internal::AudioReceiveStream* audio_receive_stream =
356      static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream);
357  {
358    WriteLockScoped write_lock(*receive_crit_);
359    size_t num_deleted = audio_receive_ssrcs_.erase(
360        audio_receive_stream->config().rtp.remote_ssrc);
361    RTC_DCHECK(num_deleted == 1);
362    const std::string& sync_group = audio_receive_stream->config().sync_group;
363    const auto it = sync_stream_mapping_.find(sync_group);
364    if (it != sync_stream_mapping_.end() &&
365        it->second == audio_receive_stream) {
366      sync_stream_mapping_.erase(it);
367      ConfigureSync(sync_group);
368    }
369  }
370  delete audio_receive_stream;
371}
372
373webrtc::VideoSendStream* Call::CreateVideoSendStream(
374    const webrtc::VideoSendStream::Config& config,
375    const VideoEncoderConfig& encoder_config) {
376  TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream");
377  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
378
379  // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if
380  // the call has already started.
381  VideoSendStream* send_stream = new VideoSendStream(
382      num_cpu_cores_, module_process_thread_.get(), call_stats_.get(),
383      congestion_controller_.get(), bitrate_allocator_.get(), config,
384      encoder_config, suspended_video_send_ssrcs_);
385
386  if (!network_enabled_)
387    send_stream->SignalNetworkState(kNetworkDown);
388
389  WriteLockScoped write_lock(*send_crit_);
390  for (uint32_t ssrc : config.rtp.ssrcs) {
391    RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end());
392    video_send_ssrcs_[ssrc] = send_stream;
393  }
394  video_send_streams_.insert(send_stream);
395
396  if (event_log_)
397    event_log_->LogVideoSendStreamConfig(config);
398
399  return send_stream;
400}
401
402void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
403  TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream");
404  RTC_DCHECK(send_stream != nullptr);
405  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
406
407  send_stream->Stop();
408
409  VideoSendStream* send_stream_impl = nullptr;
410  {
411    WriteLockScoped write_lock(*send_crit_);
412    auto it = video_send_ssrcs_.begin();
413    while (it != video_send_ssrcs_.end()) {
414      if (it->second == static_cast<VideoSendStream*>(send_stream)) {
415        send_stream_impl = it->second;
416        video_send_ssrcs_.erase(it++);
417      } else {
418        ++it;
419      }
420    }
421    video_send_streams_.erase(send_stream_impl);
422  }
423  RTC_CHECK(send_stream_impl != nullptr);
424
425  VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates();
426
427  for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin();
428       it != rtp_state.end();
429       ++it) {
430    suspended_video_send_ssrcs_[it->first] = it->second;
431  }
432
433  delete send_stream_impl;
434}
435
436webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream(
437    const webrtc::VideoReceiveStream::Config& config) {
438  TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream");
439  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
440  VideoReceiveStream* receive_stream = new VideoReceiveStream(
441      num_cpu_cores_, congestion_controller_.get(), config,
442      voice_engine(), module_process_thread_.get(), call_stats_.get());
443
444  WriteLockScoped write_lock(*receive_crit_);
445  RTC_DCHECK(video_receive_ssrcs_.find(config.rtp.remote_ssrc) ==
446             video_receive_ssrcs_.end());
447  video_receive_ssrcs_[config.rtp.remote_ssrc] = receive_stream;
448  // TODO(pbos): Configure different RTX payloads per receive payload.
449  VideoReceiveStream::Config::Rtp::RtxMap::const_iterator it =
450      config.rtp.rtx.begin();
451  if (it != config.rtp.rtx.end())
452    video_receive_ssrcs_[it->second.ssrc] = receive_stream;
453  video_receive_streams_.insert(receive_stream);
454
455  ConfigureSync(config.sync_group);
456
457  if (!network_enabled_)
458    receive_stream->SignalNetworkState(kNetworkDown);
459
460  if (event_log_)
461    event_log_->LogVideoReceiveStreamConfig(config);
462
463  return receive_stream;
464}
465
466void Call::DestroyVideoReceiveStream(
467    webrtc::VideoReceiveStream* receive_stream) {
468  TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream");
469  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
470  RTC_DCHECK(receive_stream != nullptr);
471  VideoReceiveStream* receive_stream_impl = nullptr;
472  {
473    WriteLockScoped write_lock(*receive_crit_);
474    // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a
475    // separate SSRC there can be either one or two.
476    auto it = video_receive_ssrcs_.begin();
477    while (it != video_receive_ssrcs_.end()) {
478      if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
479        if (receive_stream_impl != nullptr)
480          RTC_DCHECK(receive_stream_impl == it->second);
481        receive_stream_impl = it->second;
482        video_receive_ssrcs_.erase(it++);
483      } else {
484        ++it;
485      }
486    }
487    video_receive_streams_.erase(receive_stream_impl);
488    RTC_CHECK(receive_stream_impl != nullptr);
489    ConfigureSync(receive_stream_impl->config().sync_group);
490  }
491  delete receive_stream_impl;
492}
493
494Call::Stats Call::GetStats() const {
495  // TODO(solenberg): Some test cases in EndToEndTest use this from a different
496  // thread. Re-enable once that is fixed.
497  // RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
498  Stats stats;
499  // Fetch available send/receive bitrates.
500  uint32_t send_bandwidth = 0;
501  congestion_controller_->GetBitrateController()->AvailableBandwidth(
502      &send_bandwidth);
503  std::vector<unsigned int> ssrcs;
504  uint32_t recv_bandwidth = 0;
505  congestion_controller_->GetRemoteBitrateEstimator(false)->LatestEstimate(
506      &ssrcs, &recv_bandwidth);
507  stats.send_bandwidth_bps = send_bandwidth;
508  stats.recv_bandwidth_bps = recv_bandwidth;
509  stats.pacer_delay_ms = congestion_controller_->GetPacerQueuingDelayMs();
510  {
511    ReadLockScoped read_lock(*send_crit_);
512    // TODO(solenberg): Add audio send streams.
513    for (const auto& kv : video_send_ssrcs_) {
514      int rtt_ms = kv.second->GetRtt();
515      if (rtt_ms > 0)
516        stats.rtt_ms = rtt_ms;
517    }
518  }
519  return stats;
520}
521
522void Call::SetBitrateConfig(
523    const webrtc::Call::Config::BitrateConfig& bitrate_config) {
524  TRACE_EVENT0("webrtc", "Call::SetBitrateConfig");
525  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
526  RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
527  if (bitrate_config.max_bitrate_bps != -1)
528    RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
529  if (config_.bitrate_config.min_bitrate_bps ==
530          bitrate_config.min_bitrate_bps &&
531      (bitrate_config.start_bitrate_bps <= 0 ||
532       config_.bitrate_config.start_bitrate_bps ==
533           bitrate_config.start_bitrate_bps) &&
534      config_.bitrate_config.max_bitrate_bps ==
535          bitrate_config.max_bitrate_bps) {
536    // Nothing new to set, early abort to avoid encoder reconfigurations.
537    return;
538  }
539  config_.bitrate_config = bitrate_config;
540  congestion_controller_->SetBweBitrates(bitrate_config.min_bitrate_bps,
541                                         bitrate_config.start_bitrate_bps,
542                                         bitrate_config.max_bitrate_bps);
543}
544
545void Call::SignalNetworkState(NetworkState state) {
546  RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread());
547  network_enabled_ = state == kNetworkUp;
548  congestion_controller_->SignalNetworkState(state);
549  {
550    ReadLockScoped write_lock(*send_crit_);
551    for (auto& kv : audio_send_ssrcs_) {
552      kv.second->SignalNetworkState(state);
553    }
554    for (auto& kv : video_send_ssrcs_) {
555      kv.second->SignalNetworkState(state);
556    }
557  }
558  {
559    ReadLockScoped write_lock(*receive_crit_);
560    for (auto& kv : video_receive_ssrcs_) {
561      kv.second->SignalNetworkState(state);
562    }
563  }
564}
565
566void Call::OnSentPacket(const rtc::SentPacket& sent_packet) {
567  if (first_packet_sent_ms_ == -1)
568    first_packet_sent_ms_ = clock_->TimeInMilliseconds();
569  congestion_controller_->OnSentPacket(sent_packet);
570}
571
572void Call::OnNetworkChanged(uint32_t target_bitrate_bps, uint8_t fraction_loss,
573                            int64_t rtt_ms) {
574  uint32_t allocated_bitrate_bps = bitrate_allocator_->OnNetworkChanged(
575      target_bitrate_bps, fraction_loss, rtt_ms);
576
577  int pad_up_to_bitrate_bps = 0;
578  {
579    ReadLockScoped read_lock(*send_crit_);
580    // No need to update as long as we're not sending.
581    if (video_send_streams_.empty())
582      return;
583
584    for (VideoSendStream* stream : video_send_streams_)
585      pad_up_to_bitrate_bps += stream->GetPaddingNeededBps();
586  }
587  // Allocated bitrate might be higher than bitrate estimate if enforcing min
588  // bitrate, or lower if estimate is higher than the sum of max bitrates, so
589  // set the pacer bitrate to the maximum of the two.
590  uint32_t pacer_bitrate_bps =
591      std::max(target_bitrate_bps, allocated_bitrate_bps);
592  {
593    rtc::CritScope lock(&bitrate_crit_);
594    // We only update these stats if we have send streams, and assume that
595    // OnNetworkChanged is called roughly with a fixed frequency.
596    estimated_send_bitrate_sum_kbits_ += target_bitrate_bps / 1000;
597    pacer_bitrate_sum_kbits_ += pacer_bitrate_bps / 1000;
598    ++num_bitrate_updates_;
599  }
600  congestion_controller_->UpdatePacerBitrate(
601      target_bitrate_bps / 1000,
602      PacedSender::kDefaultPaceMultiplier * pacer_bitrate_bps / 1000,
603      pad_up_to_bitrate_bps / 1000);
604}
605
606void Call::ConfigureSync(const std::string& sync_group) {
607  // Set sync only if there was no previous one.
608  if (voice_engine() == nullptr || sync_group.empty())
609    return;
610
611  AudioReceiveStream* sync_audio_stream = nullptr;
612  // Find existing audio stream.
613  const auto it = sync_stream_mapping_.find(sync_group);
614  if (it != sync_stream_mapping_.end()) {
615    sync_audio_stream = it->second;
616  } else {
617    // No configured audio stream, see if we can find one.
618    for (const auto& kv : audio_receive_ssrcs_) {
619      if (kv.second->config().sync_group == sync_group) {
620        if (sync_audio_stream != nullptr) {
621          LOG(LS_WARNING) << "Attempting to sync more than one audio stream "
622                             "within the same sync group. This is not "
623                             "supported in the current implementation.";
624          break;
625        }
626        sync_audio_stream = kv.second;
627      }
628    }
629  }
630  if (sync_audio_stream)
631    sync_stream_mapping_[sync_group] = sync_audio_stream;
632  size_t num_synced_streams = 0;
633  for (VideoReceiveStream* video_stream : video_receive_streams_) {
634    if (video_stream->config().sync_group != sync_group)
635      continue;
636    ++num_synced_streams;
637    if (num_synced_streams > 1) {
638      // TODO(pbos): Support synchronizing more than one A/V pair.
639      // https://code.google.com/p/webrtc/issues/detail?id=4762
640      LOG(LS_WARNING) << "Attempting to sync more than one audio/video pair "
641                         "within the same sync group. This is not supported in "
642                         "the current implementation.";
643    }
644    // Only sync the first A/V pair within this sync group.
645    if (sync_audio_stream != nullptr && num_synced_streams == 1) {
646      video_stream->SetSyncChannel(voice_engine(),
647                                   sync_audio_stream->config().voe_channel_id);
648    } else {
649      video_stream->SetSyncChannel(voice_engine(), -1);
650    }
651  }
652}
653
654PacketReceiver::DeliveryStatus Call::DeliverRtcp(MediaType media_type,
655                                                 const uint8_t* packet,
656                                                 size_t length) {
657  TRACE_EVENT0("webrtc", "Call::DeliverRtcp");
658  // TODO(pbos): Figure out what channel needs it actually.
659  //             Do NOT broadcast! Also make sure it's a valid packet.
660  //             Return DELIVERY_UNKNOWN_SSRC if it can be determined that
661  //             there's no receiver of the packet.
662  received_rtcp_bytes_ += length;
663  bool rtcp_delivered = false;
664  if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
665    ReadLockScoped read_lock(*receive_crit_);
666    for (VideoReceiveStream* stream : video_receive_streams_) {
667      if (stream->DeliverRtcp(packet, length)) {
668        rtcp_delivered = true;
669        if (event_log_)
670          event_log_->LogRtcpPacket(true, media_type, packet, length);
671      }
672    }
673  }
674  if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
675    ReadLockScoped read_lock(*send_crit_);
676    for (VideoSendStream* stream : video_send_streams_) {
677      if (stream->DeliverRtcp(packet, length)) {
678        rtcp_delivered = true;
679        if (event_log_)
680          event_log_->LogRtcpPacket(false, media_type, packet, length);
681      }
682    }
683  }
684  return rtcp_delivered ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
685}
686
687PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
688                                                const uint8_t* packet,
689                                                size_t length,
690                                                const PacketTime& packet_time) {
691  TRACE_EVENT0("webrtc", "Call::DeliverRtp");
692  // Minimum RTP header size.
693  if (length < 12)
694    return DELIVERY_PACKET_ERROR;
695
696  last_rtp_packet_received_ms_ = clock_->TimeInMilliseconds();
697  if (first_rtp_packet_received_ms_ == -1)
698    first_rtp_packet_received_ms_ = last_rtp_packet_received_ms_;
699
700  uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
701  ReadLockScoped read_lock(*receive_crit_);
702  if (media_type == MediaType::ANY || media_type == MediaType::AUDIO) {
703    auto it = audio_receive_ssrcs_.find(ssrc);
704    if (it != audio_receive_ssrcs_.end()) {
705      received_audio_bytes_ += length;
706      auto status = it->second->DeliverRtp(packet, length, packet_time)
707                        ? DELIVERY_OK
708                        : DELIVERY_PACKET_ERROR;
709      if (status == DELIVERY_OK && event_log_)
710        event_log_->LogRtpHeader(true, media_type, packet, length);
711      return status;
712    }
713  }
714  if (media_type == MediaType::ANY || media_type == MediaType::VIDEO) {
715    auto it = video_receive_ssrcs_.find(ssrc);
716    if (it != video_receive_ssrcs_.end()) {
717      received_video_bytes_ += length;
718      auto status = it->second->DeliverRtp(packet, length, packet_time)
719                        ? DELIVERY_OK
720                        : DELIVERY_PACKET_ERROR;
721      if (status == DELIVERY_OK && event_log_)
722        event_log_->LogRtpHeader(true, media_type, packet, length);
723      return status;
724    }
725  }
726  return DELIVERY_UNKNOWN_SSRC;
727}
728
729PacketReceiver::DeliveryStatus Call::DeliverPacket(
730    MediaType media_type,
731    const uint8_t* packet,
732    size_t length,
733    const PacketTime& packet_time) {
734  // TODO(solenberg): Tests call this function on a network thread, libjingle
735  // calls on the worker thread. We should move towards always using a network
736  // thread. Then this check can be enabled.
737  // RTC_DCHECK(!configuration_thread_checker_.CalledOnValidThread());
738  if (RtpHeaderParser::IsRtcp(packet, length))
739    return DeliverRtcp(media_type, packet, length);
740
741  return DeliverRtp(media_type, packet, length, packet_time);
742}
743
744}  // namespace internal
745}  // namespace webrtc
746