-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrtmp_connection.cpp
362 lines (283 loc) · 8.37 KB
/
rtmp_connection.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*******************************************************************************
* rtmp_connection.c
* Copyright: (c) 2013 Haibin Du([email protected]). All rights reserved.
* -----------------------------------------------------------------------------
*
******************************************************************************/
#include "rtmp_connection.h"
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <sys/types.h>
#include <sys/socket.h>
#else
#include <WS2tcpip.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "rtmp_server_base/amf_byte_stream.h"
#include "rtmp_server_base/librtmp/log.h"
#include "rtmp_server_base/thread.h"
#include "rtmp_server_base/util_tools.h"
#ifdef __cplusplus
}
#endif
#include "lib_rtmp.h"
#include "file_rtmp_server.h"
RtmpConnection::RtmpConnection(FileRtmpServer *pown,int clientSocket):m_pown(pown)
{
is_open_ = rtmp_false;
is_closing_ = rtmp_false;
is_has_send_header_ = rtmp_false;
is_start_playing_ = rtmp_false;
is_pause_ = rtmp_false;
librtmp_server_ = NULL;
socket_ = clientSocket;
data_list_ = DataList_Create();
SimpleMutexInit(&data_mtx_);
has_data_ = 0;
}
RtmpConnection::~RtmpConnection()
{
RtmpConnection_Close();
DataList_Destroy(data_list_);
data_list_ = NULL;
SimpleMutexFree(&data_mtx_);
}
void RtmpConnection::RtmpConnection_Open(const char* logfilename)
{
if (is_open_) return;
librtmp_server_ = new LibRtmpServer(this);
if (!librtmp_server_->LibRtmpOpen(socket_, logfilename))
{
RTMP_Log(RTMP_LOGERROR, "Open Handshake failed");
return;
}
ThreadCreate(RtmpConnectionProc, this);
}
void RtmpConnection::RtmpConnection_Close()
{
if (is_closing_) return;
is_closing_ = rtmp_true;
while (is_open_ == rtmp_true)
{
MySleep(100);
}
librtmp_server_->LibRtmpClose();
delete librtmp_server_;
librtmp_server_ = NULL;
}
void RtmpConnection::RtmpConnection_PushData(RefCountBuf* refbuf,
int isVideo, int isKeyframe)
{
SimpleMutexLock(&data_mtx_);
DataList_Pushback(data_list_, refbuf, isVideo, isKeyframe);
SimpleMutexUnlock(&data_mtx_);
has_data_ = 1;
}
TFTYPE RtmpConnection::RunRtmpConnectionPthread()
{
is_open_ = rtmp_true;
for (;;)
{
int has_data = 0;
fd_set read_fds;
fd_set write_fds;
struct timeval tv;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
FD_SET(socket_, &read_fds);
FD_SET(socket_, &write_fds);
memset(&tv, 0, sizeof(struct timeval));
if (is_closing_ == rtmp_true)
{
break;
}
if (select(socket_ + 1, &read_fds, &write_fds, NULL, &tv) <= 0)
{
MySleep(10);
continue;
}
//printf("[%s:%d]拜托,能不能先走到这里来呢????,is_has_send_header_:%d\n",__FUNCTION__,__LINE__,is_has_send_header_);
if (!is_has_send_header_)
{
RtmpConnection_Diapatch();
for(;;)
{
if (is_closing_ == rtmp_true)
goto CONNECTION_END;
if (is_start_playing_)
{
printf("[%s:%d]startintg to play suuceffulaty\n",__FUNCTION__,__LINE__);
break;
}
MySleep(100);
}
m_pown->RtmpServer_OnNewConnection(this);
}
if (FD_ISSET(socket_, &read_fds) && !is_has_send_header_)
{
rtmp_bool_t ret = RtmpConnection_OnRead();
if (ret == rtmp_false)
{
printf("connection read failed, need close\n");
goto CONNECTION_END;
}
else
{
if (is_start_playing_ && !is_has_send_header_)
{
m_pown->RtmpServer_OnNewConnection(this);
}
}
}
if (has_data_ == 1)
{
//printf("[%s:%d]是不是这里做了什么操作????\n",__FUNCTION__,__LINE__);
DataList* tmplist = NULL;
DataListNode* node = NULL;
has_data_ = 0;
tmplist = DataList_Create();
SimpleMutexLock(&data_mtx_);
DataList_Swap(data_list_, tmplist);
SimpleMutexUnlock(&data_mtx_);
for (node = DataList_Head(tmplist); node != DataList_End(tmplist);
node = node->next_)
{
if (node->is_video_)
{
if (!RtmpConnection_SendData(node->buf_->data_,
node->buf_->data_len_, FLV_TAG_TYPE_VIDEO))
{
printf("connection send failed, need close\n");
DataList_Destroy(tmplist);
goto CONNECTION_END;
}
}
else
{
RtmpConnection_SendData(node->buf_->data_,
node->buf_->data_len_, FLV_TAG_TYPE_AUDIO);
}
}
DataList_Destroy(tmplist);
}
else
{
MySleep(100);
}
}
CONNECTION_END:
is_open_ = rtmp_false;
TFRET();
}
rtmp_bool_t RtmpConnection::RtmpConnection_IsOpen()
{
return is_open_;
}
void RtmpConnection::RtmpConnection_Diapatch()
{
RTMPPacket packet = { 0 };
while (RTMP_IsConnected(librtmp_server_->rtmp) &&
RTMP_ReadPacket(librtmp_server_->rtmp, &packet))
{
if (!RTMPPacket_IsReady(&packet))
continue;
librtmp_server_->DispatchRtmpPacket(&packet);
RTMPPacket_Free(&packet);
if (is_start_playing_)
{
break;
}
}
}
rtmp_bool_t RtmpConnection::RtmpConnection_OnRead()
{
if (is_closing_) return rtmp_false;
if (RTMP_IsConnected(librtmp_server_->rtmp))
{
RTMPPacket packet = { 0 };
int err = RTMP_ReadPacket(librtmp_server_->rtmp, &packet);
if (!err)
{
RTMP_Log(RTMP_LOGERROR, "RTMP_ReadPacket failed");
return rtmp_false;
}
if (RTMPPacket_IsReady(&packet))
{
librtmp_server_->DispatchRtmpPacket( &packet);
RTMPPacket_Free(&packet);
return rtmp_true;
}
}
return rtmp_false;
}
void RtmpConnection::RtmpConnection_OnClientPlay(const char* playPath)
{
printf("客户端选择的播放URL是:[%s]\n",playPath);
is_start_playing_ = rtmp_true;
}
void RtmpConnection::RtmpConnection_OnClientPause()
{
if (is_pause_)
{
RtmpConnection_SendStart();
// if (parent_class_)
// parent_class_->OnPlayRestart(this);
is_pause_ = rtmp_false;
}
else
{
RtmpConnection_SendStop();
is_pause_ = rtmp_true;
}
}
rtmp_bool_t RtmpConnection::RtmpConnection_IsStartPlaying()
{
return is_start_playing_;
}
rtmp_bool_t RtmpConnection::RtmpConnection_IsSendHeader()
{
return RtmpConnection::is_has_send_header_;
}
rtmp_bool_t RtmpConnection::RtmpConnection_IsPause()
{
return is_pause_;
}
void RtmpConnection::RtmpConnection_SendStart()
{
if (is_closing_) return;
librtmp_server_->LibRtmpSendPlayStart();
librtmp_server_->LibRtmpResetTimestamp();
}
void RtmpConnection::RtmpConnection_SendStop()
{
if (is_closing_) return;
librtmp_server_->LibRtmpSendPlayStop();
}
void RtmpConnection::RtmpConnection_SendAVCHeader(
const char* spsBuf, int spsSize,
const char* ppsBuf, int ppsSize)
{
if (is_closing_) return;
librtmp_server_->LibRtmpSendAVCHeader( spsBuf, spsSize, ppsBuf, ppsSize);
is_has_send_header_ = rtmp_true;
}
void RtmpConnection::RtmpConnection_SendAACHeader(
int sampleRate, int channel)
{
if (is_closing_) return;
librtmp_server_->LibRtmpSendAACHeader( sampleRate, channel);
}
int RtmpConnection::RtmpConnection_SendData(
const char* buf, int bufLen, int type)
{
if (is_closing_) return rtmp_false;
return librtmp_server_->LibRtmpSendData( buf, bufLen, type,
librtmp_server_->LibRtmpGetTimestamp());
}
int RtmpConnection::RtmpConnection_SocketID()
{
return socket_;
}