Agora Java API Reference for Android
video_track_i.h
1 //
2 // Agora Media SDK
3 //
4 // Created by Rao Qi in 2019.
5 // Copyright (c) 2019 Agora IO. All rights reserved.
6 //
7 #pragma once
8 
9 #include <atomic>
10 #include <memory>
11 #include <mutex>
12 #include <unordered_map>
13 #include <vector>
14 
15 #include "AgoraBase.h"
16 #include "NGIAgoraVideoTrack.h"
17 
18 #include "rtc_connection_i.h"
19 #include "track_stat_i.h"
20 #include "video_config_i.h"
21 #include "common_defines.h"
22 
23 namespace agora {
24 namespace utils {
25 
26 enum ConfigPriority {
27  // configurations for debugging, which can overwrite anything
28  // usually used by dev/lab tuning
29  CONFIG_PRIORITY_DEBUG = 0,
30  // configurations from device capability detection
31  CONFIG_PRIORITY_DEVICE,
32  // configurations for emergency, this will overwrite user configuration.
33  // for cases like "close XXX feature immediately otherwise terrible things happen"
34  // do not use this priority level unless review board approved
35  CONFIG_PRIORITY_HIGH_FROM_SERVER,
36  // configurations from lua script
37  CONFIG_PRIORITY_LUA,
38  // configurations from user setting
39  CONFIG_PRIORITY_USER,
40  // configurations from "config service".
41  CONFIG_PRIORITY_NORMAL_FROM_SERVER,
42  // configurations from internal (usually it's default value)
43  CONFIG_PRIORITY_INTERNAL,
44  CONFIG_PRIORITY_MAX
45 };
46 
47 template <typename Observer>
49  public:
50  WeakObservers() = default;
51  ~WeakObservers() = default;
52 
53  bool add(std::shared_ptr<Observer> obs) {
54  if (!obs) {
55  return false;
56  }
57 
58  std::lock_guard<std::mutex> _(obs_mutex_);
59  observer_map_[obs.get()] = obs;
60  return true;
61  }
62 
63  int size() {
64  std::lock_guard<std::mutex> _(obs_mutex_);
65  return observer_map_.size();
66  }
67 
68  // better to remove by raw pointer since we may unregister() -> remove() in DTOR
69  bool remove(Observer* obs) {
70  if (!obs) {
71  return false;
72  }
73 
74  std::lock_guard<std::mutex> _(obs_mutex_);
75  if (observer_map_.find(obs) == observer_map_.end()) {
76  return false;
77  }
78 
79  observer_map_.erase(obs);
80  return true;
81  }
82 
83  void notify(std::function<void (std::shared_ptr<Observer>)>&& notify) {
84  std::vector<std::shared_ptr<Observer>> obs_copy;
85 
86  {
87  std::lock_guard<std::mutex> _(obs_mutex_);
88 
89  for (auto it = observer_map_.begin(); it != observer_map_.end();) {
90  auto obs_shared = it->second.lock();
91  if (!obs_shared) {
92  it = observer_map_.erase(it);
93  continue;
94  }
95 
96  obs_copy.push_back(obs_shared);
97  ++it;
98  }
99  }
100 
101  for (auto obs : obs_copy) {
102  notify(obs);
103  }
104  }
105 
106  private:
107  std::mutex obs_mutex_;
108  std::unordered_map<Observer*, std::weak_ptr<Observer>> observer_map_;
109 };
110 
111 } // namespace utils
112 
113 namespace rtc {
114 
115 class VideoNodeRtpSink;
116 class VideoNodeRtpSource;
117 class VideoTrackConfigurator;
118 
119 class IVideoTrackObserver : public std::enable_shared_from_this<IVideoTrackObserver> {
120  public:
121  virtual ~IVideoTrackObserver() = default;
122  virtual void onLocalVideoStateChanged(int id,
123  LOCAL_VIDEO_STREAM_STATE state,
124  LOCAL_VIDEO_STREAM_ERROR errorCode,
125  int timestamp_ms) {}
126 
127  virtual void onRemoteVideoStateChanged(uid_t uid,
128  REMOTE_VIDEO_STATE state,
129  REMOTE_VIDEO_STATE_REASON reason,
130  int timestamp_ms) {}
131 
132  virtual void onFirstVideoFrameRendered(uid_t uid, int width, int height, int timestamp_ms) {}
133 
134  virtual void onFirstVideoFrameDecoded(uid_t uid, int width, int height, int timestamp_ms) {}
135 
136  virtual void onSourceVideoSizeChanged(uid_t uid,
137  int width, int height,
138  int rotation, int timestamp_ms) {}
139  virtual void onSendSideDelay(int id, int send_delay) {}
140  virtual void onRecvSideDelay(uid_t uid, int recv_delay) {}
141  virtual void onRecvSideFps(uid_t uid, int fps) {}
142 };
143 
145  public:
146  enum DetachReason { MANUAL, TRACK_DESTROY, NETWORK_DESTROY };
147 
148  // keep the same as webrtc::RsfecConfig
149  struct RsfecConfig {
150  std::vector<int> fec_protection_factor;
151  std::vector<std::vector<int>> fec_ratioLevel;
152  std::vector<int> fec_rttThreshold;
153  bool pec_enabled;
154  };
155 
156  struct AttachInfo {
157  uint32_t uid;
158  uint32_t cid;
159  VideoNodeRtpSink* network;
160  WeakPipelineBuilder builder;
161  uint64_t stats_space;
162  CongestionControlType cc_type;
163  int32_t rsfec_minimum_level;
164  bool enable_two_bytes_extension;
165 
166  //hardware encoder related
167  std::string enable_hw_encoder;
168  std::string hw_encoder_provider;
169  };
170 
171  struct DetachInfo {
172  VideoNodeRtpSink* network;
173  DetachReason reason;
174  };
175 
176  ILocalVideoTrackEx() : id_(id_generator_++) {}
177  virtual ~ILocalVideoTrackEx() {}
178 
179  virtual bool hasPublished() = 0;
180 
181  virtual int SetVideoConfigEx(const VideoConfigurationEx& configEx, utils::ConfigPriority priority = utils::CONFIG_PRIORITY_USER) = 0;
182 
183  virtual int GetConfigExs(std::vector<VideoConfigurationEx>& configs) = 0;
184 
185  virtual int setUserId(uid_t uid) { user_id_ = uid; return 0; }
186 
187  virtual uid_t getUserId() { return user_id_; }
188 
189  virtual int getObserverSize() { return track_observers_.size(); }
190 
191  virtual int GetActiveStreamsCount() = 0;
192 
193  virtual int prepareNodes() = 0;
194 
195  virtual bool attach(const AttachInfo& info) = 0;
196  virtual bool detach(const DetachInfo& info) = 0;
197 
198  virtual bool registerTrackObserver(std::shared_ptr<IVideoTrackObserver> observer) {
199  return false;
200  }
201  virtual bool unregisterTrackObserver(IVideoTrackObserver* observer) {
202  return false;
203  }
204 
205  virtual int32_t Width() const = 0;
206  virtual int32_t Height() const = 0;
207  virtual bool Enabled() const = 0;
208  // TODO(Qingyou Pan): Need refine code to remove this interface.
209  virtual int addVideoWatermark(const char* watermarkUrl, const WatermarkOptions& options) { return -ERR_NOT_SUPPORTED; };
210  virtual int clearVideoWatermarks() { return -ERR_NOT_SUPPORTED; }
211 
212  virtual VideoTrackConfigurator* GetVideoTrackConfigurator() {
213  return nullptr;
214  }
215 
216  int TrackId() const { return id_; }
217 
218  public:
219  static void resetIdGenerator();
220 
221  protected:
222  int id_;
223  utils::WeakObservers<IVideoTrackObserver> track_observers_;
224  uid_t user_id_;
225 
226  private:
227  static std::atomic<int> id_generator_;
228 };
229 
231  uint64_t firstDecodingTimeTickMs = 0;
232  uint64_t firstVideoFrameRendered = 0;
233 };
234 
236  public:
237  enum DetachReason { MANUAL, TRACK_DESTROY, NETWORK_DESTROY };
238  using RemoteVideoEvents = StateEvents<REMOTE_VIDEO_STATE, REMOTE_VIDEO_STATE_REASON>;
239 
240  struct AttachInfo {
241  VideoNodeRtpSource* source;
242  VideoNodeRtpSink* rtcp_sender;
243  WeakPipelineBuilder builder;
244  bool recv_media_packet = false;
245  uint64_t stats_space = 0;
246  };
247 
248  struct DetachInfo {
249  VideoNodeRtpSource* source;
250  VideoNodeRtpSink* rtcp_sender;
251  DetachReason reason;
252  };
253 
254  IRemoteVideoTrackEx() = default;
255 
256  virtual ~IRemoteVideoTrackEx() {}
257 
258  virtual uint32_t getRemoteSsrc() = 0;
259 
260  virtual bool attach(const AttachInfo& info, REMOTE_VIDEO_STATE_REASON reason) = 0;
261  virtual bool detach(const DetachInfo& info, REMOTE_VIDEO_STATE_REASON reason) = 0;
262 
263  virtual bool getStatisticsEx(RemoteVideoTrackStatsEx& statsex) { return false; }
264 
265  virtual bool registerTrackObserver(std::shared_ptr<IVideoTrackObserver> observer) {
266  return false;
267  }
268  virtual bool unregisterTrackObserver(IVideoTrackObserver* observer) {
269  return false;
270  }
271 
272  protected:
273  utils::WeakObservers<IVideoTrackObserver> track_observers_;
274 };
275 
276 } // namespace rtc
277 } // namespace agora
agora::rtc::RemoteVideoTrackStatsEx
Definition: video_track_i.h:230
agora::rtc::ILocalVideoTrackEx
Definition: video_track_i.h:144
agora::rtc::IRemoteVideoTrackEx::DetachInfo
Definition: video_track_i.h:248
agora::rtc::IRemoteVideoTrackEx::AttachInfo
Definition: video_track_i.h:240
agora::rtc::ILocalVideoTrackEx::DetachInfo
Definition: video_track_i.h:171
agora::rtc::ILocalVideoTrack
Definition: NGIAgoraVideoTrack.h:226
agora::rtc::IRemoteVideoTrackEx
Definition: video_track_i.h:235
agora::rtc::IRemoteVideoTrack
Definition: NGIAgoraVideoTrack.h:371
agora::rtc::RemoteVideoTrackStats
Definition: NGIAgoraVideoTrack.h:295
agora::rtc::ILocalVideoTrackEx::AttachInfo
Definition: video_track_i.h:156
agora::utils::WeakObservers
Definition: video_track_i.h:48
agora::rtc::IVideoTrackObserver
Definition: video_track_i.h:119
agora::rtc::ILocalVideoTrackEx::RsfecConfig
Definition: video_track_i.h:149