Agora C++ API Reference for All Platforms

Loading...
Searching...
No Matches
track_stat_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 <mutex>
11#include <utility>
12#include <vector>
13
14 namespace agora {
15 namespace rtc {
16
17 template <typename STATE_TYPE, typename REASON_TYPE>
18 using StateEvent = std::pair<uint64_t, std::pair<STATE_TYPE, REASON_TYPE>>;
19
20 template <typename STATE_TYPE, typename REASON_TYPE>
21 using StateEvents = std::vector<StateEvent<STATE_TYPE, REASON_TYPE>>;
22
23 template <typename STATE_TYPE, typename REASON_TYPE>
24 class StateNotifier {
25 public:
26 explicit StateNotifier(STATE_TYPE init_value) : current_state_(init_value) {}
27 ~StateNotifier() {}
28
29 void Notify(uint64_t ts, STATE_TYPE state, REASON_TYPE reason) {
30 if (current_state_.exchange(state) == state) {
31 return;
32 }
33
34 std::lock_guard<std::mutex> _(lock_);
35 events_.push_back({ts, {state, reason}});
36 }
37
38 StateEvents<STATE_TYPE, REASON_TYPE> GetEvents(bool readOnly = false) {
39 StateEvents<STATE_TYPE, REASON_TYPE> retEvents;
40 std::lock_guard<std::mutex> _(lock_);
41 if (!readOnly) {
42 retEvents.swap(events_);
43 } else {
44 retEvents.assign(events_.begin(), events_.end());
45 }
46 return retEvents;
47 }
48
49 bool PopEvent(STATE_TYPE state, StateEvent<STATE_TYPE, REASON_TYPE>& retEvent) {
50 std::lock_guard<std::mutex> _(lock_);
51 bool found = false;
52 std::size_t events_size = events_.size();
53 for (std::size_t i = 0; i < events_size; ++i) {
54 if (events_[i].second.first == state) {
55 retEvent = events_[i];
56 for (std::size_t j = i + 1; j < events_size; ++j) {
57 events_[j - 1] = events_[j];
58 }
59 events_.pop_back();
60 found = true;
61 break;
62 }
63 }
64 return found;
65 }
66
67 STATE_TYPE CurrentState() { return current_state_; }
68
69 private:
70 std::atomic<STATE_TYPE> current_state_;
71 std::mutex lock_;
72 StateEvents<STATE_TYPE, REASON_TYPE> events_;
73};
74
75} // namespace rtc
76} // namespace agora
agora::rtc::StateNotifier::~StateNotifier
~StateNotifier()
Definition track_stat_i.h:27
agora::rtc::StateNotifier::Notify
void Notify(uint64_t ts, STATE_TYPE state, REASON_TYPE reason)
Definition track_stat_i.h:29
agora::rtc::StateNotifier::PopEvent
bool PopEvent(STATE_TYPE state, StateEvent< STATE_TYPE, REASON_TYPE > &retEvent)
Definition track_stat_i.h:49
agora::rtc::StateNotifier::GetEvents
StateEvents< STATE_TYPE, REASON_TYPE > GetEvents(bool readOnly=false)
Definition track_stat_i.h:38
agora::rtc::StateNotifier::StateNotifier
StateNotifier(STATE_TYPE init_value)
Definition track_stat_i.h:26
agora::rtc::StateNotifier::CurrentState
STATE_TYPE CurrentState()
Definition track_stat_i.h:67
agora::rtc
Definition AgoraExtensions.h:5
agora::rtc::StateEvent
std::pair< uint64_t, std::pair< STATE_TYPE, REASON_TYPE > > StateEvent
Definition track_stat_i.h:18
agora::rtc::StateEvents
std::vector< StateEvent< STATE_TYPE, REASON_TYPE > > StateEvents
Definition track_stat_i.h:21
agora
Definition AgoraAtomicOps.h:21