forked from Glimesh/janus-ftl-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFtlStreamStore.cpp
250 lines (213 loc) · 7.65 KB
/
FtlStreamStore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* @file FtlStreamStore.cpp
* @author Hayden McAfee (hayden@outlook.com)
* @version 0.1
* @date 2020-08-20
*
* @copyright Copyright (c) 2020 Hayden McAfee
*/
#include "FtlStreamStore.h"
#include "FtlStream.h"
extern "C"
{
#include <debug.h>
}
#include <stdexcept>
#pragma region Public methods
void FtlStreamStore::AddStream(std::shared_ptr<FtlStream> ftlStream)
{
std::lock_guard<std::mutex> channelIdMapGuard(channelIdMapMutex);
std::lock_guard<std::mutex> mediaPortMapGuard(mediaPortMapMutex);
// Check for collisions
uint64_t channelId = ftlStream->GetChannelId();
uint16_t mediaPort = ftlStream->GetMediaPort();
if (ftlStreamsByChannelId.count(channelId) > 0)
{
throw std::invalid_argument(
"Attempt to add FTL Stream with a channel ID that is already assigned.");
}
if (ftlStreamsByMediaPort.count(mediaPort) > 0)
{
throw std::invalid_argument(
"Attempt to add FTL Stream with a media port that is already assigned.");
}
// Add to maps
ftlStreamsByChannelId[channelId] = ftlStream;
ftlStreamsByMediaPort[mediaPort] = ftlStream;
}
void FtlStreamStore::AddViewer(
std::shared_ptr<FtlStream> ftlStream,
std::shared_ptr<JanusSession> session)
{
// First, confirm stream is present
if (!HasStream(ftlStream))
{
throw std::invalid_argument("Could not find given FTL stream.");
}
std::lock_guard<std::mutex> sessionMapGuard(sessionMapMutex);
// Make sure this session isn't already assigned to a different stream
if (ftlStreamsBySession.count(session) > 0)
{
throw std::invalid_argument("This session is already viewing another FTL stream.");
}
// Add viewer to ftl stream
ftlStream->AddViewer(session);
// Update map
ftlStreamsBySession[session] = ftlStream;
}
bool FtlStreamStore::HasStream(std::shared_ptr<FtlStream> ftlStream)
{
std::lock_guard<std::mutex> channelIdMapGuard(channelIdMapMutex);
std::lock_guard<std::mutex> mediaPortMapGuard(mediaPortMapMutex);
uint64_t channelId = ftlStream->GetChannelId();
uint64_t mediaPort = ftlStream->GetMediaPort();
return ((ftlStreamsByChannelId.count(channelId) > 0) &&
(ftlStreamsByMediaPort.count(mediaPort) > 0));
}
std::shared_ptr<FtlStream> FtlStreamStore::GetStreamByChannelId(uint64_t channelId)
{
std::lock_guard<std::mutex> channelIdMapGuard(channelIdMapMutex);
if (ftlStreamsByChannelId.count(channelId) <= 0)
{
return nullptr;
}
return ftlStreamsByChannelId.at(channelId);
}
std::shared_ptr<FtlStream> FtlStreamStore::GetStreamByMediaPort(uint16_t mediaPort)
{
std::lock_guard<std::mutex> mediaPortMapGuard(mediaPortMapMutex);
if (ftlStreamsByMediaPort.count(mediaPort) <= 0)
{
return nullptr;
}
return ftlStreamsByMediaPort.at(mediaPort);
}
std::shared_ptr<FtlStream> FtlStreamStore::GetStreamBySession(std::shared_ptr<JanusSession> session)
{
std::lock_guard<std::mutex> sessionMapGuard(sessionMapMutex);
if (ftlStreamsBySession.count(session) <= 0)
{
return nullptr;
}
return ftlStreamsBySession.at(session);
}
void FtlStreamStore::RemoveStream(std::shared_ptr<FtlStream> ftlStream)
{
std::lock_guard<std::mutex> channelIdMapGuard(channelIdMapMutex);
std::lock_guard<std::mutex> mediaPortMapGuard(mediaPortMapMutex);
uint64_t channelId = ftlStream->GetChannelId();
uint16_t mediaPort = ftlStream->GetMediaPort();
if ((ftlStreamsByChannelId.count(channelId) <= 0) ||
(ftlStreamsByMediaPort.count(mediaPort) <= 0))
{
throw std::invalid_argument("Could not find given stream's channel ID or media port.");
}
ftlStreamsByChannelId.erase(channelId);
ftlStreamsByMediaPort.erase(mediaPort);
}
void FtlStreamStore::RemoveViewer(
std::shared_ptr<FtlStream> ftlStream,
std::shared_ptr<JanusSession> session)
{
// First, confirm stream is present
if (!HasStream(ftlStream))
{
throw std::invalid_argument("Could not find given FTL stream.");
}
std::lock_guard<std::mutex> sessionMapGuard(sessionMapMutex);
// Confirm session exists
if (ftlStreamsBySession.count(session) <= 0)
{
throw std::invalid_argument("Could not find given session.");
}
// Confirm session actually points to the expected stream
if (ftlStreamsBySession.at(session) != ftlStream)
{
throw std::invalid_argument("Session is not viewing given stream.");
}
// Remove viewer from ftl stream
ftlStream->RemoveViewer(session);
// Update map
ftlStreamsBySession.erase(session);
}
void FtlStreamStore::AddPendingViewerForChannelId(
uint16_t channelId,
std::shared_ptr<JanusSession> session)
{
std::lock_guard<std::mutex> lock(pendingSessionMutex);
// We shouldn't be able to request viewership of multiple channels at once.
// But make sure anyway.
if (pendingSessionChannelId.count(session) > 0)
{
JANUS_LOG(LOG_WARN, "FTL: Session requesting pending viewership is already pending "
"viewingship for another channel. Channel ID %d. Re-assigning...",
channelId);
uint16_t existingChannelId = pendingSessionChannelId[session];
if (pendingChannelIdSessions.count(existingChannelId) > 0)
{
pendingChannelIdSessions[existingChannelId].erase(session);
}
else
{
JANUS_LOG(LOG_WARN, "FTL: Session had an existing pending channel ID, but "
"channel ID set didn't contain session. Stream store is in a bad state.");
}
}
pendingSessionChannelId[session] = channelId;
if (pendingChannelIdSessions.count(channelId) <= 0)
{
pendingChannelIdSessions[channelId] = std::set<std::shared_ptr<JanusSession>>();
}
pendingChannelIdSessions[channelId].insert(session);
}
std::set<std::shared_ptr<JanusSession>> FtlStreamStore::GetPendingViewersForChannelId(
uint16_t channelId)
{
std::lock_guard<std::mutex> lock(pendingSessionMutex);
if (pendingChannelIdSessions.count(channelId) > 0)
{
return pendingChannelIdSessions[channelId];
}
return std::set<std::shared_ptr<JanusSession>>(); // Empty set
}
std::set<std::shared_ptr<JanusSession>> FtlStreamStore::ClearPendingViewersForChannelId(
uint16_t channelId)
{
std::lock_guard<std::mutex> lock(pendingSessionMutex);
// Grab a copy to return
std::set<std::shared_ptr<JanusSession>> returnVal;
if (pendingChannelIdSessions.count(channelId) > 0)
{
returnVal = pendingChannelIdSessions[channelId];
}
for (const std::shared_ptr<JanusSession>& session : returnVal)
{
pendingSessionChannelId.erase(session);
}
pendingChannelIdSessions.erase(channelId);
return returnVal;
}
void FtlStreamStore::RemovePendingViewershipForSession(std::shared_ptr<JanusSession> session)
{
std::lock_guard<std::mutex> lock(pendingSessionMutex);
// Look up the channel ID
if (pendingSessionChannelId.count(session) <= 0)
{
JANUS_LOG(LOG_WARN, "FTL: Attempt to remove viewership for session that is not pending "
"any channel ID.");
return;
}
uint16_t channelId = pendingSessionChannelId[session];
pendingSessionChannelId.erase(session);
if (pendingChannelIdSessions.count(channelId) <= 0)
{
JANUS_LOG(LOG_WARN, "FTL: Session had an existing pending channel ID, but "
"channel ID set didn't contain session. Stream store is in a bad state.");
return;
}
if (pendingChannelIdSessions[channelId].erase(session) != 1)
{
JANUS_LOG(LOG_WARN, "FTL: Failed to erase session from pending channel ID session set.");
}
}
#pragma endregion