Home | History | Annotate | Download | only in video
      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 "webrtc/video/send_statistics_proxy.h"
     12 
     13 #include <map>
     14 
     15 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
     16 
     17 namespace webrtc {
     18 
     19 SendStatisticsProxy::SendStatisticsProxy(
     20     const VideoSendStream::Config& config)
     21     : config_(config),
     22       crit_(CriticalSectionWrapper::CreateCriticalSection()) {
     23 }
     24 
     25 SendStatisticsProxy::~SendStatisticsProxy() {}
     26 
     27 void SendStatisticsProxy::OutgoingRate(const int video_channel,
     28                                        const unsigned int framerate,
     29                                        const unsigned int bitrate) {
     30   CriticalSectionScoped lock(crit_.get());
     31   stats_.encode_frame_rate = framerate;
     32 }
     33 
     34 void SendStatisticsProxy::SuspendChange(int video_channel, bool is_suspended) {
     35   CriticalSectionScoped lock(crit_.get());
     36   stats_.suspended = is_suspended;
     37 }
     38 
     39 void SendStatisticsProxy::CapturedFrameRate(const int capture_id,
     40                                             const unsigned char frame_rate) {
     41   CriticalSectionScoped lock(crit_.get());
     42   stats_.input_frame_rate = frame_rate;
     43 }
     44 
     45 VideoSendStream::Stats SendStatisticsProxy::GetStats() const {
     46   CriticalSectionScoped lock(crit_.get());
     47   return stats_;
     48 }
     49 
     50 StreamStats* SendStatisticsProxy::GetStatsEntry(uint32_t ssrc) {
     51   std::map<uint32_t, StreamStats>::iterator it = stats_.substreams.find(ssrc);
     52   if (it != stats_.substreams.end())
     53     return &it->second;
     54 
     55   if (std::find(config_.rtp.ssrcs.begin(), config_.rtp.ssrcs.end(), ssrc) ==
     56           config_.rtp.ssrcs.end() &&
     57       std::find(config_.rtp.rtx.ssrcs.begin(),
     58                 config_.rtp.rtx.ssrcs.end(),
     59                 ssrc) == config_.rtp.rtx.ssrcs.end()) {
     60     return NULL;
     61   }
     62 
     63   return &stats_.substreams[ssrc];  // Insert new entry and return ptr.
     64 }
     65 
     66 void SendStatisticsProxy::StatisticsUpdated(const RtcpStatistics& statistics,
     67                                             uint32_t ssrc) {
     68   CriticalSectionScoped lock(crit_.get());
     69   StreamStats* stats = GetStatsEntry(ssrc);
     70   if (stats == NULL)
     71     return;
     72 
     73   stats->rtcp_stats = statistics;
     74 }
     75 
     76 void SendStatisticsProxy::DataCountersUpdated(
     77     const StreamDataCounters& counters,
     78     uint32_t ssrc) {
     79   CriticalSectionScoped lock(crit_.get());
     80   StreamStats* stats = GetStatsEntry(ssrc);
     81   if (stats == NULL)
     82     return;
     83 
     84   stats->rtp_stats = counters;
     85 }
     86 
     87 void SendStatisticsProxy::Notify(const BitrateStatistics& bitrate,
     88                                  uint32_t ssrc) {
     89   CriticalSectionScoped lock(crit_.get());
     90   StreamStats* stats = GetStatsEntry(ssrc);
     91   if (stats == NULL)
     92     return;
     93 
     94   stats->bitrate_bps = bitrate.bitrate_bps;
     95 }
     96 
     97 void SendStatisticsProxy::FrameCountUpdated(FrameType frame_type,
     98                                             uint32_t frame_count,
     99                                             const unsigned int ssrc) {
    100   CriticalSectionScoped lock(crit_.get());
    101   StreamStats* stats = GetStatsEntry(ssrc);
    102   if (stats == NULL)
    103     return;
    104 
    105   switch (frame_type) {
    106     case kVideoFrameDelta:
    107       stats->delta_frames = frame_count;
    108       break;
    109     case kVideoFrameKey:
    110       stats->key_frames = frame_count;
    111       break;
    112     case kFrameEmpty:
    113     case kAudioFrameSpeech:
    114     case kAudioFrameCN:
    115       break;
    116   }
    117 }
    118 
    119 void SendStatisticsProxy::SendSideDelayUpdated(int avg_delay_ms,
    120                                                int max_delay_ms,
    121                                                uint32_t ssrc) {
    122   CriticalSectionScoped lock(crit_.get());
    123   StreamStats* stats = GetStatsEntry(ssrc);
    124   if (stats == NULL)
    125     return;
    126   stats->avg_delay_ms = avg_delay_ms;
    127   stats->max_delay_ms = max_delay_ms;
    128 }
    129 
    130 }  // namespace webrtc
    131