Repository navigation
Expand file tree
/
Copy pathrtmpcollection.cpp
More file actions
executable file
·143 lines (131 loc) · 3.52 KB
/
Copy pathrtmpcollection.cpp
File metadata and controls
executable file
·143 lines (131 loc) · 3.52 KB
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
#include <jni.h>
#include <map>
#include <librtmp/rtmp.h>
#include <librtmp/log.h>
#include <mutex>
#include <queue>
#include <fcntl.h>
#include <iostream>
#include "util.cpp"
class RTMPCollection
{
public:
RTMPCollection() : counter(0) {}
~RTMPCollection()
{
for (auto &pair : collection)
{
RTMP_Close(pair.second);
RTMP_Free(pair.second);
}
}
int addRTMPConnection(const char *url)
{
std::lock_guard<std::mutex> lock(collectionMutex);
RTMP *rtmp = RTMP_Alloc();
RTMP_Init(rtmp);
rtmp->Link.timeout = 5;
if (!RTMP_SetupURL(rtmp, (char *)url))
{
RTMP_Log(RTMP_LOGERROR, "SetupURL Err\n");
RTMP_Free(rtmp);
return -1;
}
RTMP_EnableWrite(rtmp);
if (!RTMP_Connect(rtmp, NULL))
{
RTMP_Log(RTMP_LOGERROR, "Connect Err\n");
RTMP_Free(rtmp);
return -1;
}
if (!RTMP_ConnectStream(rtmp, 0))
{
RTMP_Log(RTMP_LOGERROR, "ConnectStream Err\n");
RTMP_Close(rtmp);
RTMP_Free(rtmp);
return -1;
}
int fd = RTMP_Socket(rtmp);
int flags = fcntl(fd, F_GETFL, 0);
// fcntl(fd, F_SETFL, flags | O_NONBLOCK);
fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
if (counter == 2147483647)
{
counter = 0;
}
int32_t id = ++counter;
collection.insert(std::make_pair(id, rtmp));
std::queue<char *> q;
bufferQueue.insert(std::make_pair(id, q));
return id;
}
RTMP *getRTMPConnection(int id)
{
std::lock_guard<std::mutex> lock(collectionMutex);
if (collection.find(id) != collection.end())
{
return collection[id];
}
return NULL;
}
int closeRTMPConnection(int id)
{
std::lock_guard<std::mutex> lock1(collectionMutex);
auto it = collection.find(id);
int result = -1;
if (it != collection.end())
{
RTMP_Close(it->second);
RTMP_Free(it->second);
collection.erase(it);
result = 0;
}
std::lock_guard<std::mutex> lock2(bufferQueueMutex);
auto it2 = bufferQueue.find(id);
if (it2 != bufferQueue.end())
{
while (!it2->second.empty())
{
free(it2->second.front());
it2->second.pop();
}
bufferQueue.erase(it2);
}
return result;
}
char *dequeBuffer(int id)
{
std::lock_guard<std::mutex> lock(bufferQueueMutex);
if (bufferQueue.find(id) != bufferQueue.end())
{
char *buffer = bufferQueue[id].front();
bufferQueue[id].pop();
return buffer;
}
return NULL;
}
void enqueueBuffer(int id, char *buffer)
{
std::lock_guard<std::mutex> lock(bufferQueueMutex);
if (bufferQueue.find(id) != bufferQueue.end())
{
bufferQueue[id].push(buffer);
}
}
bool isQueueEmpty(int id)
{
std::lock_guard<std::mutex> lock(bufferQueueMutex);
if (bufferQueue.find(id) != bufferQueue.end())
{
return bufferQueue[id].empty();
}
return true;
}
private:
const char *url;
std::map<int, RTMP *> collection;
std::mutex collectionMutex;
std::map<int, std::queue<char *>> bufferQueue;
std::mutex bufferQueueMutex;
int32_t counter;
};