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