-
Notifications
You must be signed in to change notification settings - Fork 2
/
mk_media_service.cpp
261 lines (234 loc) · 7.34 KB
/
mk_media_service.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
#include <string.h>
#include "mk_media_service.h"
#include "mk_rtsp_service.h"
#include "mk_rtsp_connection.h"
#include "mk_rtmp_connection.h"
#include "mk_media_common.h"
mk_media_service::mk_media_service()
{
m_NetWorkArray = NULL;
m_ulEvnCount = 0;
m_ulFrameBufSize = FRAME_RECV_BUF_SIZE;
m_ulFramebufCount = FRAME_RECV_BUF_COUNT;
}
mk_media_service::~mk_media_service()
{
}
int32_t mk_media_service::init(uint32_t EvnCount,uint32_t MaxClient)
{
int32_t nRet = AS_ERROR_CODE_OK;
uint32_t i = 0;
m_ulEvnCount = EvnCount;
as_network_svr* pNetWork = NULL;
/* create the network service array */
m_NetWorkArray = AS_NEW(m_NetWorkArray,m_ulEvnCount);
if(NULL == m_NetWorkArray){
return AS_ERROR_CODE_FAIL;
}
for(i = 0;i < m_ulEvnCount;i++) {
pNetWork = AS_NEW(pNetWork);
if(NULL == pNetWork) {
return AS_ERROR_CODE_FAIL;
}
pNetWork->setLogWriter(&m_connLog);
nRet = pNetWork->init(DEFAULT_SELECT_PERIOD,AS_TRUE,AS_TRUE,AS_TRUE);
if (AS_ERROR_CODE_OK != nRet)
{
MK_LOG(AS_LOG_WARNING,"init the network module fail.");
return AS_ERROR_CODE_FAIL;
}
nRet = pNetWork->run();
if (AS_ERROR_CODE_OK != nRet)
{
MK_LOG(AS_LOG_WARNING,"run the network module fail.");
return AS_ERROR_CODE_FAIL;
}
m_NetWorkArray[i] = pNetWork;
}
for(i = 0;i < MaxClient;i++) {
m_ClientFreeList.push_back(i);
}
MK_LOG(AS_LOG_INFO,"init the network module success.");
nRet = create_frame_recv_bufs();
if (AS_ERROR_CODE_OK != nRet)
{
MK_LOG(AS_LOG_WARNING,"create frame recv bufs fail.");
return AS_ERROR_CODE_FAIL;
}
MK_LOG(AS_LOG_INFO,"create frame recv bufs success.");
if(AS_ERROR_CODE_OK != m_CheckTimer.init(DefaultTimerScale)) {
MK_LOG(AS_LOG_WARNING,"init the timer fail.");
return AS_ERROR_CODE_FAIL;
}
if(AS_ERROR_CODE_OK != m_CheckTimer.run()) {
MK_LOG(AS_LOG_WARNING,"run the timer fail.");
return AS_ERROR_CODE_FAIL;
}
MK_LOG(AS_LOG_INFO,"run the timer success.");
return mk_rtsp_service::instance().init();
}
void mk_media_service::release()
{
destory_frame_recv_bufs();
as_network_svr* pNetWork = NULL;
for(uint32_t i = 0;i < m_ulEvnCount;i++) {
pNetWork = m_NetWorkArray[i];
if(NULL == pNetWork) {
continue;
}
pNetWork->exit();
AS_DELETE(pNetWork);
m_NetWorkArray[i] = NULL;
}
mk_rtsp_service::instance().release();
return;
}
mk_rtsp_server* mk_media_service::create_rtsp_server(uint16_t port,rtsp_server_request cb,void* ctx)
{
mk_rtsp_server* pRtspServer = NULL;
pRtspServer = AS_NEW(pRtspServer);
if(NULL == pRtspServer) {
return NULL;
}
pRtspServer->set_callback(cb,ctx);
as_network_addr local;
local.m_ulIpAddr = 0;
local.m_usPort = port;
int32_t nRet = m_NetWorkArray[0]->regTcpServer(&local,pRtspServer);
if(AS_ERROR_CODE_OK != nRet) {
AS_DELETE(pRtspServer);
pRtspServer = NULL;
}
return pRtspServer;
}
void mk_media_service::destory_rtsp_server(mk_rtsp_server* pServer)
{
if(NULL == pServer) {
return;
}
m_NetWorkArray[0]->removeTcpServer(pServer);
AS_DELETE(pServer);
return;
}
mk_client_connection* mk_media_service::create_client(char* url,handle_client_status cb,void* ctx)
{
mk_client_connection* pClient = NULL;
mk_rtsp_connection* pRtspClient = NULL;
mk_rtmp_connection* pRtmpClient = NULL;
as_network_addr local;
as_network_addr peer;
if(m_ClientFreeList.empty()) {
return NULL;
}
if(0 == strncmp(url,RTSP_URL_PREFIX,strlen(RTSP_URL_PREFIX))) {
pClient = AS_NEW(pRtspClient);
}
else if(0 == strncmp(url,RTMP_URL_PREFIX,strlen(RTMP_URL_PREFIX))) {
pClient = AS_NEW(pRtmpClient);
}
if(NULL == pClient) {
return NULL;
}
uint32_t ulIdx = m_ClientFreeList.front();
m_ClientFreeList.pop_front();
pClient->set_index(ulIdx);
pClient->set_status_callback(cb,ctx);
pClient->set_url(url);
return pClient;
}
void mk_media_service::destory_client(mk_client_connection* pClient)
{
if(NULL == pClient) {
return;
}
uint32_t ulIdx = pClient->get_index();
AS_DELETE(pClient);
m_ClientFreeList.push_back(ulIdx);
return;
}
as_network_svr* mk_media_service::get_client_network_svr(uint32_t ulIndex)
{
uint32_t ulIdx = ulIndex%m_ulEvnCount;
return m_NetWorkArray[ulIdx];
}
as_timer& mk_media_service::get_client_check_timer()
{
return m_CheckTimer;
}
void mk_media_service::set_rtp_rtcp_udp_port(uint16_t udpPort,uint32_t count)
{
mk_rtsp_service::instance().set_rtp_rtcp_udp_port(udpPort,count);
}
void mk_media_service::get_rtp_rtcp_udp_port(uint16_t& udpPort,uint32_t& count)
{
mk_rtsp_service::instance().get_rtp_rtcp_udp_port(udpPort,count);
}
void mk_media_service::set_rtp_recv_buf_info(uint32_t maxCount)
{
mk_rtsp_service::instance().set_rtp_recv_buf_info(maxCount);
}
void mk_media_service::get_rtp_recv_buf_info(uint32_t& maxCount)
{
mk_rtsp_service::instance().get_rtp_recv_buf_info(maxCount);
}
void mk_media_service::set_media_frame_buffer(uint32_t maxSize,uint32_t maxCount)
{
m_ulFrameBufSize = maxSize;
m_ulFramebufCount = maxCount;
}
void mk_media_service::get_media_frame_buffer(uint32_t& maxSize,uint32_t& maxCount)
{
maxSize = m_ulFrameBufSize;
maxCount = m_ulFramebufCount;
}
char* mk_media_service::get_frame_buf()
{
if(m_FrameBufList.empty()) {
return NULL;
}
char* buf = m_FrameBufList.front();
m_FrameBufList.pop_front();
return buf;
}
void mk_media_service::free_frame_buf(char* buf)
{
if(NULL == buf) {
return;
}
m_FrameBufList.push_back(buf);
return;
}
int32_t mk_media_service::create_frame_recv_bufs()
{
MK_LOG(AS_LOG_INFO,"create frame recv buf begin.");
if((0 == m_ulFrameBufSize)||(0 == m_ulFramebufCount)) {
MK_LOG(AS_LOG_ERROR,"there is no init frame recv buf arguments,size:[%d] count:[%d].",m_ulFrameBufSize,m_ulFramebufCount);
return AS_ERROR_CODE_FAIL;
}
char* pBuf = NULL;
for(uint32_t i = 0;i < m_ulFramebufCount;i++) {
pBuf = AS_NEW(pBuf,m_ulFrameBufSize);
if(NULL == pBuf) {
MK_LOG(AS_LOG_ERROR,"create the frame recv buf fail,size:[%d] index:[%d].",m_ulFrameBufSize,i);
return AS_ERROR_CODE_FAIL;
}
m_FrameBufList.push_back(pBuf);
}
MK_LOG(AS_LOG_INFO,"create frame recv buf end.");
return AS_ERROR_CODE_OK;
}
void mk_media_service::destory_frame_recv_bufs()
{
MK_LOG(AS_LOG_INFO,"destory frame recv buf begin.");
char* pBuf = NULL;
while(0 <m_FrameBufList.size()) {
pBuf = m_FrameBufList.front();
m_FrameBufList.pop_front();
if(NULL == pBuf) {
continue;
}
AS_DELETE(pBuf,MULTI);
}
MK_LOG(AS_LOG_INFO,"destory frame recv buf end.");
return;
}