-
Notifications
You must be signed in to change notification settings - Fork 97
/
rtmp_server.cpp
300 lines (256 loc) · 6.96 KB
/
rtmp_server.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "xop/RtmpServer.h"
#include "xop/HttpFlvServer.h"
#include "xop/RtmpPublisher.h"
#include "xop/RtmpClient.h"
#include "xop/HttpFlvServer.h"
#include "xop/H264Parser.h"
#include "net/EventLoop.h"
#define TEST_RTMP_PUSHER 1
#define TEST_RTMP_CLIENT 0
#define TEST_MULTI_THREAD 0
#define RTMP_URL "rtmp://127.0.0.1:1935/live/01"
#define PUSH_FILE "./test.h264"
#define HTTP_URL "http://127.0.0.1:8080/live/01.flv"
int TestRtmpPublisher(xop::EventLoop *event_loop);
int main(int argc, char **argv)
{
int count = 1;
#if TEST_MULTI_THREAD
count = std::thread::hardware_concurrency();
#endif
xop::EventLoop event_loop(count);
/* rtmp server example */
auto rtmp_server = xop::RtmpServer::Create(&event_loop);
rtmp_server->SetChunkSize(60000);
//rtmp_server->SetGopCache(); /* enable gop cache */
rtmp_server->SetEventCallback([](std::string type, std::string stream_path) {
printf("[Event] %s, stream path: %s\n\n", type.c_str(), stream_path.c_str());
});
if (!rtmp_server->Start("0.0.0.0", 1935)) {
printf("RTMP Server listen on 1935 failed.\n");
}
/* http-flv server example */
xop::HttpFlvServer http_flv_server;
http_flv_server.Attach(rtmp_server);
if (!http_flv_server.Start("0.0.0.0", 8080)) {
printf("HTTP FLV Server listen on 8080 failed.\n");
}
#if TEST_RTMP_PUSHER
/* rtmp pusher example */
std::thread t([&event_loop] () {
TestRtmpPublisher(&event_loop);
});
t.detach();
#endif
#if TEST_RTMP_CLIENT
auto rtmp_client = xop::RtmpClient::Create(&event_loop);
rtmp_client->SetFrameCB([](uint8_t* payload, uint32_t length, uint8_t codecId, uint32_t timestamp) {
printf("recv frame, type:%u, size:%u,\n", codecId, length);
});
std::string status;
if (rtmp_client->OpenUrl(RTMP_URL, 3000, status) != 0) {
printf("Open url %s failed, status: %s\n", RTMP_URL, status.c_str());
}
#endif
while (1) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
rtmp_server->Stop();
//http_flv_server.Stop();
return 0;
}
class H264File
{
public:
H264File(int bufSize = 5000000);
~H264File();
bool open(const char *path);
void Close();
bool isOpened() const
{
return (m_file != NULL);
}
int readFrame(char *inBuf, int inBufSize, bool *bEndOfFrame);
private:
FILE *m_file = NULL;
char *m_buf = NULL;
int m_bufSize = 0;
int m_bytesUsed = 0;
int m_count = 0;
};
H264File::H264File(int bufSize)
: m_bufSize(bufSize)
{
m_buf = new char[m_bufSize];
}
H264File::~H264File()
{
delete m_buf;
}
bool H264File::open(const char *path)
{
m_file = fopen(path, "rb");
if (m_file == NULL)
{
return false;
}
return true;
}
void H264File::Close()
{
if (m_file)
{
fclose(m_file);
m_file = NULL;
m_count = 0;
m_bytesUsed = 0;
}
}
int H264File::readFrame(char *inBuf, int inBufSize, bool *bEndOfFrame)
{
if (m_file == NULL)
{
return -1;
}
int bytesRead = (int)fread(m_buf, 1, m_bufSize, m_file);
if (bytesRead == 0)
{
fseek(m_file, 0, SEEK_SET);
m_count = 0;
m_bytesUsed = 0;
bytesRead = (int)fread(m_buf, 1, m_bufSize, m_file);
if (bytesRead == 0)
{
this->Close();
return -1;
}
}
bool bFindStart = false, bFindEnd = false;
int i = 0, startCode = 3;
*bEndOfFrame = false;
for (i = 0; i < bytesRead - 5; i++)
{
if (m_buf[i] == 0 && m_buf[i + 1] == 0 && m_buf[i + 2] == 1)
{
startCode = 3;
}
else if (m_buf[i] == 0 && m_buf[i + 1] == 0 && m_buf[i + 2] == 0 && m_buf[i + 3] == 1)
{
startCode = 4;
}
else
{
continue;
}
if (((m_buf[i + startCode] & 0x1F) == 0x5 || (m_buf[i + startCode] & 0x1F) == 0x1) &&
((m_buf[i + startCode + 1] & 0x80) == 0x80))
{
bFindStart = true;
i += 4;
break;
}
}
for (; i < bytesRead - 5; i++)
{
if (m_buf[i] == 0 && m_buf[i + 1] == 0 && m_buf[i + 2] == 1)
{
startCode = 3;
}
else if (m_buf[i] == 0 && m_buf[i + 1] == 0 && m_buf[i + 2] == 0 && m_buf[i + 3] == 1)
{
startCode = 4;
}
else
{
continue;
}
if (((m_buf[i + startCode] & 0x1F) == 0x7) || ((m_buf[i + startCode] & 0x1F) == 0x8)
|| ((m_buf[i + startCode] & 0x1F) == 0x6) || (((m_buf[i + startCode] & 0x1F) == 0x5
|| (m_buf[i + startCode] & 0x1F) == 0x1) && ((m_buf[i + startCode + 1] & 0x80) == 0x80)))
{
bFindEnd = true;
break;
}
}
bool flag = false;
if (bFindStart && !bFindEnd && m_count > 0)
{
flag = bFindEnd = true;
i = bytesRead;
*bEndOfFrame = true;
}
if (!bFindStart || !bFindEnd)
{
this->Close();
return -1;
}
int size = (i <= inBufSize ? i : inBufSize);
memcpy(inBuf, m_buf, size);
if (!flag)
{
m_count += 1;
m_bytesUsed += i;
}
else
{
m_count = 0;
m_bytesUsed = 0;
}
fseek(m_file, m_bytesUsed, SEEK_SET);
return size;
}
int TestRtmpPublisher(xop::EventLoop *event_loop)
{
H264File h264_file;
if (!h264_file.open(PUSH_FILE)) {
printf("Open %s failed.\n", PUSH_FILE);
return -1;
}
/* push stream to local rtmp server */
xop::MediaInfo media_info;
auto publisher = xop::RtmpPublisher::Create(event_loop);
publisher->SetChunkSize(60000);
std::string status;
if (publisher->OpenUrl(RTMP_URL, 3000, status) < 0) {
printf("Open url %s failed, status: %s\n", RTMP_URL, status.c_str());
return -1;
}
int buf_size = 500000;
bool end_of_frame = false;
bool has_sps_pps = false;
uint8_t *frame_buf = new uint8_t[buf_size];
while (publisher->IsConnected())
{
int frameSize = h264_file.readFrame((char*)frame_buf, buf_size, &end_of_frame);
if (frameSize > 0) {
if (!has_sps_pps) {
if (frame_buf[3] == 0x67 || frame_buf[4] == 0x67) {
xop::Nal sps = xop::H264Parser::findNal(frame_buf, frameSize);
if (sps.first != nullptr && sps.second != nullptr && *sps.first == 0x67) {
media_info.sps_size = (uint32_t)(sps.second - sps.first + 1);
media_info.sps.reset(new uint8_t[media_info.sps_size], std::default_delete<uint8_t[]>());
memcpy(media_info.sps.get(), sps.first, media_info.sps_size);
xop::Nal pps = xop::H264Parser::findNal(sps.second, frameSize - (int)(sps.second - frame_buf));
if (pps.first != nullptr && pps.second != nullptr && *pps.first == 0x68) {
media_info.pps_size = (uint32_t)(pps.second - pps.first + 1);
media_info.pps.reset(new uint8_t[media_info.pps_size], std::default_delete<uint8_t[]>());
memcpy(media_info.pps.get(), pps.first, media_info.pps_size);
has_sps_pps = true;
publisher->SetMediaInfo(media_info); /* set sps pps */
printf("Start rtmp pusher, rtmp url: %s , http-flv url: %s \n\n", RTMP_URL, HTTP_URL);
}
}
}
}
if (has_sps_pps) {
publisher->PushVideoFrame(frame_buf, frameSize); /* send h.264 frame */
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(40));
}
delete frame_buf;
return 0;
}