1 /*
2  *  Copyright 2019 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 #ifndef API_TRANSPORT_RTP_RTP_SOURCE_H_
12 #define API_TRANSPORT_RTP_RTP_SOURCE_H_
13 
14 #include <stdint.h>
15 
16 #include "absl/types/optional.h"
17 #include "api/rtp_headers.h"
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 
22 enum class RtpSourceType {
23   SSRC,
24   CSRC,
25 };
26 
27 class RtpSource {
28  public:
29   struct Extensions {
30     absl::optional<uint8_t> audio_level;
31     absl::optional<AbsoluteCaptureTime> absolute_capture_time;
32   };
33 
34   RtpSource() = delete;
35 
36   // TODO(bugs.webrtc.org/10739): Remove this constructor once all clients
37   // migrate to the version with absolute capture time.
RtpSource(int64_t timestamp_ms,uint32_t source_id,RtpSourceType source_type,absl::optional<uint8_t> audio_level,uint32_t rtp_timestamp)38   RtpSource(int64_t timestamp_ms,
39             uint32_t source_id,
40             RtpSourceType source_type,
41             absl::optional<uint8_t> audio_level,
42             uint32_t rtp_timestamp)
43       : RtpSource(timestamp_ms,
44                   source_id,
45                   source_type,
46                   rtp_timestamp,
47                   {audio_level, absl::nullopt}) {}
48 
RtpSource(int64_t timestamp_ms,uint32_t source_id,RtpSourceType source_type,uint32_t rtp_timestamp,const RtpSource::Extensions & extensions)49   RtpSource(int64_t timestamp_ms,
50             uint32_t source_id,
51             RtpSourceType source_type,
52             uint32_t rtp_timestamp,
53             const RtpSource::Extensions& extensions)
54       : timestamp_ms_(timestamp_ms),
55         source_id_(source_id),
56         source_type_(source_type),
57         extensions_(extensions),
58         rtp_timestamp_(rtp_timestamp) {}
59 
60   RtpSource(const RtpSource&) = default;
61   RtpSource& operator=(const RtpSource&) = default;
62   ~RtpSource() = default;
63 
timestamp_ms()64   int64_t timestamp_ms() const { return timestamp_ms_; }
update_timestamp_ms(int64_t timestamp_ms)65   void update_timestamp_ms(int64_t timestamp_ms) {
66     RTC_DCHECK_LE(timestamp_ms_, timestamp_ms);
67     timestamp_ms_ = timestamp_ms;
68   }
69 
70   // The identifier of the source can be the CSRC or the SSRC.
source_id()71   uint32_t source_id() const { return source_id_; }
72 
73   // The source can be either a contributing source or a synchronization source.
source_type()74   RtpSourceType source_type() const { return source_type_; }
75 
audio_level()76   absl::optional<uint8_t> audio_level() const {
77     return extensions_.audio_level;
78   }
79 
set_audio_level(const absl::optional<uint8_t> & level)80   void set_audio_level(const absl::optional<uint8_t>& level) {
81     extensions_.audio_level = level;
82   }
83 
rtp_timestamp()84   uint32_t rtp_timestamp() const { return rtp_timestamp_; }
85 
absolute_capture_time()86   absl::optional<AbsoluteCaptureTime> absolute_capture_time() const {
87     return extensions_.absolute_capture_time;
88   }
89 
90   bool operator==(const RtpSource& o) const {
91     return timestamp_ms_ == o.timestamp_ms() && source_id_ == o.source_id() &&
92            source_type_ == o.source_type() &&
93            extensions_.audio_level == o.extensions_.audio_level &&
94            extensions_.absolute_capture_time ==
95                o.extensions_.absolute_capture_time &&
96            rtp_timestamp_ == o.rtp_timestamp();
97   }
98 
99  private:
100   int64_t timestamp_ms_;
101   uint32_t source_id_;
102   RtpSourceType source_type_;
103   RtpSource::Extensions extensions_;
104   uint32_t rtp_timestamp_;
105 };
106 
107 }  // namespace webrtc
108 
109 #endif  // API_TRANSPORT_RTP_RTP_SOURCE_H_
110