1 // Copyright (c) 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "third_party/blink/renderer/platform/peerconnection/rtc_rtp_source.h"
6 
7 #include <cmath>
8 
9 #include "base/notreached.h"
10 #include "base/time/time.h"
11 #include "third_party/webrtc/api/scoped_refptr.h"
12 #include "third_party/webrtc/system_wrappers/include/ntp_time.h"
13 
14 namespace blink {
15 
RTCRtpSource(const webrtc::RtpSource & source)16 RTCRtpSource::RTCRtpSource(const webrtc::RtpSource& source) : source_(source) {}
17 
~RTCRtpSource()18 RTCRtpSource::~RTCRtpSource() {}
19 
SourceType() const20 RTCRtpSource::Type RTCRtpSource::SourceType() const {
21   switch (source_.source_type()) {
22     case webrtc::RtpSourceType::SSRC:
23       return RTCRtpSource::Type::kSSRC;
24     case webrtc::RtpSourceType::CSRC:
25       return RTCRtpSource::Type::kCSRC;
26     default:
27       NOTREACHED();
28       return RTCRtpSource::Type::kSSRC;
29   }
30 }
31 
Timestamp() const32 base::TimeTicks RTCRtpSource::Timestamp() const {
33   return base::TimeTicks() +
34          base::TimeDelta::FromMilliseconds(source_.timestamp_ms());
35 }
36 
Source() const37 uint32_t RTCRtpSource::Source() const {
38   return source_.source_id();
39 }
40 
AudioLevel() const41 base::Optional<double> RTCRtpSource::AudioLevel() const {
42   if (!source_.audio_level())
43     return base::nullopt;
44   // Converted according to equation defined here:
45   // https://w3c.github.io/webrtc-pc/#dom-rtcrtpcontributingsource-audiolevel
46   uint8_t rfc_level = *source_.audio_level();
47   if (rfc_level > 127u)
48     rfc_level = 127u;
49   if (rfc_level == 127u)
50     return 0.0;
51   return std::pow(10.0, -(double)rfc_level / 20.0);
52 }
53 
RtpTimestamp() const54 uint32_t RTCRtpSource::RtpTimestamp() const {
55   return source_.rtp_timestamp();
56 }
57 
CaptureTimestamp() const58 base::Optional<int64_t> RTCRtpSource::CaptureTimestamp() const {
59   if (!source_.absolute_capture_time())
60     return base::nullopt;
61   return webrtc::UQ32x32ToInt64Ms(
62       source_.absolute_capture_time()->absolute_capture_timestamp);
63 }
64 
65 }  // namespace blink
66