-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathk_mobile_handler.cpp
More file actions
348 lines (281 loc) · 6.94 KB
/
Copy pathk_mobile_handler.cpp
File metadata and controls
348 lines (281 loc) · 6.94 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
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
#include "k_mobile_handler.h"
#include "json/json.h"
#include "k_util/k_socket.h"
#include "k_media_server.h"
#include "k_util/k_util.h"
#define RTP_VERSION 2
#define RTP_FLAG_MARKER 0x2
static const uint8_t start_sequence[] = { 0, 0, 0, 1 };
k_mobile_handler::k_mobile_handler()
: m_rebuf(NULL)
, m_rebuf_size(0)
{
fopen_s(&m_video_fp, "C:/Users/Si_Li/Desktop/tmp/test.264", "wb");
fopen_s(&m_auido_fp, "C:/Users/Si_Li/Desktop/tmp/test.aac", "wb");
}
k_mobile_handler::~k_mobile_handler()
{
if (m_rebuf)
{
free(m_rebuf);
}
fclose(m_video_fp);
fclose(m_auido_fp);
}
int k_mobile_handler::handle_read(k_thread_task* task, k_event* ev, k_socket* sock)
{
int ret = sock->k_recv(buf, 4096);
if (ret <= 0)
{
printf("rev failed:%d\n", ret);
task->del_event(sock);
return -1;
}
char* p = buf;
uint32_t size = ret;
while (size > 0)
{
if (size < K_MSG_HEAD_LEN)
{
int need = K_MSG_HEAD_LEN - size;
memcpy(head_buf, p, size);
if (sock->k_recv_n(head_buf + size, need))
{
printf("recv_n failed\n");
task->del_event(sock);
return -1;
}
p = head_buf;
size = K_MSG_HEAD_LEN;
}
uint16_t magic = *(uint16_t*)p;
magic = ntohs(magic);
p += sizeof(uint16_t);
size -= sizeof(uint16_t);
if (magic != K_MAGIC)
{
printf("magic:%d wrong\n", magic);
return -1;
}
uint32_t msg_len = *(uint32_t*)p;
msg_len = ntohl(msg_len);
p += sizeof(uint32_t);
uint32_t msg_id = *(uint32_t*)p;
msg_id = ntohl(msg_id);
p += sizeof(uint32_t);
size -= sizeof(uint32_t) * 2;
msg_len -= K_MSG_HEAD_LEN;
if (msg_len > size)
{
uint32_t need = msg_len - size;
if (msg_len > m_rebuf_size)
{
if (m_rebuf)
{
free(m_rebuf);
}
m_rebuf = (char*)malloc(msg_len + 4);
m_rebuf_size = msg_len;
}
memcpy(m_rebuf + 4, p, size);
if (sock->k_recv_n(m_rebuf + 4 + size, need))
{
printf("recv_n 2 failed\n");
task->del_event(sock);
return -1;
}
p = m_rebuf + 4;
size = msg_len;
}
if (this->incoming_msg(msg_id, p, msg_len, task, ev, sock))
{
return -1;
}
p += msg_len;
size -= msg_len;
}
return 0;
}
int k_mobile_handler::incoming_msg(uint32_t msg_id, const char* buf, int len,
k_thread_task* task, k_event* ev, k_socket* sock)
{
switch(msg_id)
{
case K_LOGIN:
return this->on_login_msg(buf, len, sock, task);
break;
case K_VIDEO:
case K_AUDIO:
return this->on_media_msg(task, msg_id, (uint8_t*)buf, len);
default:
return -1;
}
}
int k_mobile_handler::on_login_msg(const char* buf, int len, k_socket* sock, k_thread_task* task)
{
Json::Reader reader;
Json::Value root;
// reader将Json字符串解析到root,root将包含Json里所有子元素
if (!reader.parse(buf, buf + len, root))
{
printf("json parse failed\n");
return 0;
}
k_string user_id = root["user_id"].asCString();
k_string password = root["password"].asCString();
printf("user_id:%s, password:%s\n", user_id.c_str(), password.c_str());
Json::Value rsp;
int result = 0;
rsp["result"] = result;
rsp["describe"] = "ha ha ha";
Json::StreamWriterBuilder writer;
std::string document = Json::writeString(writer, rsp);
char send_buf[4096];
uint8_t * p_buf = (uint8_t*)send_buf;
int tot_len = document.size() + K_MSG_HEAD_LEN;
int rsp_msg_id = K_LOGIN_RSP;
k_util::avio_wb16(p_buf, K_MAGIC);//magic
k_util::avio_wb32(p_buf, tot_len);
k_util::avio_wb32(p_buf, rsp_msg_id);
memcpy(p_buf, document.c_str(), document.size());
int ret = sock->k_send(send_buf, tot_len);
if (ret != tot_len)
{
printf("send failed:%d\n", ret);
task->del_event(sock);
printf("send failed\n");
return -1;
}
if (result)
{
printf("login error:%d\n", result);
task->del_event(sock);
return -1;
}
return 0;
}
int k_mobile_handler::ff_h264_handle_frag_packet(const uint8_t *buf, int len,
int start_bit, const uint8_t *nal_header,
int nal_header_len)
{
if (start_bit) {
fwrite(start_sequence, 1, sizeof(start_sequence), m_video_fp);
fwrite(nal_header, 1, nal_header_len, m_video_fp);
}
fwrite(buf, 1, len, m_video_fp);
return 0;
}
int k_mobile_handler::h264_handle_packet_fu_a(const uint8_t *buf, int len)
{
uint8_t fu_indicator, fu_header, start_bit, nal_type, nal;
if (len < 3) {
printf("Too short data for FU-A H.264 RTP packet\n");
return -1;
}
fu_indicator = buf[0];
fu_header = buf[1];
start_bit = fu_header >> 7;
nal_type = fu_header & 0x1f;
nal = fu_indicator & 0xe0 | nal_type;
// skip the fu_indicator and fu_header
buf += 2;
len -= 2;
return ff_h264_handle_frag_packet(buf, len, start_bit, &nal, 1);
}
int k_mobile_handler::on_media_msg(k_thread_task* task, uint32_t msg_id, uint8_t* buf, int len)
{
k_media_server* server = dynamic_cast<k_media_server*>(task);
if (msg_id == K_VIDEO)
{
server->on_video(buf, len);
}
else if (msg_id == K_AUDIO)
{
server->on_audio(buf, len);
}
return 0;
if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
{
printf("rtp version err\n");
return -1;
}
unsigned int ssrc;
int payload_type, seq, flags = 0;
int ext, csrc;
uint32_t timestamp;
csrc = buf[0] & 0x0f;
ext = buf[0] & 0x10;
payload_type = buf[1] & 0x7f;
if (buf[1] & 0x80)
flags |= RTP_FLAG_MARKER;
seq = AV_RB16(buf + 2);
timestamp = AV_RB32(buf + 4);
ssrc = AV_RB32(buf + 8);
if (buf[0] & 0x20) {
int padding = buf[len - 1];
if (len >= 12 + padding)
len -= padding;
}
len -= 12;
buf += 12;
len -= 4 * csrc;
buf += 4 * csrc;
if (len < 0)
{
printf("rtp len err\n");
return -1;
}
/* RFC 3550 Section 5.3.1 RTP Header Extension handling */
if (ext) {
if (len < 4)
{
printf("rtp ext len err\n");
return -1;
}
/* calculate the header extension length (stored as number
* of 32-bit words) */
ext = (AV_RB16(buf + 2) + 1) << 2;
if (len < ext)
{
printf("rtp ext len err2\n");
return -1;
}
// skip past RTP header extension
len -= ext;
buf += ext;
}
if (!len) {
printf("rtp len err2\n");
return -1;
}
printf("payload:%d, seq:%d, len:%d, ts:%u\n", payload_type, seq, len, timestamp);
if (msg_id == K_AUDIO)
{
fwrite(buf, 1, len, m_auido_fp);
return 0;
}
uint8_t nal;
uint8_t type;
int result = 0;
nal = buf[0];
type = nal & 0x1f;
/* Simplify the case (these are all the NAL types used internally by
* the H.264 codec). */
if (type >= 1 && type <= 23)
type = 1;
switch (type) {
case 0: // undefined, but pass them through
case 1:
fwrite(start_sequence, 1, sizeof(start_sequence), m_video_fp);
fwrite(buf, 1, len, m_video_fp);
break;
case 28: // FU-A (fragmented nal)
result = h264_handle_packet_fu_a(buf, len);
break;
default:
printf("Undefined type (%d)\n", type);
return -1;
break;
}
return result;
}