winlin

substitute all TAB with 4spaces.

要显示太多修改。

为保证性能只显示 5 of 5+ 个文件。

@@ -45,28 +45,28 @@ SrsBandwidth::~SrsBandwidth() @@ -45,28 +45,28 @@ SrsBandwidth::~SrsBandwidth()
45 45
46 int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServer* _rtmp) 46 int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServer* _rtmp)
47 { 47 {
48 - int ret = ERROR_SUCCESS;  
49 -  
50 - rtmp = _rtmp;  
51 - req = _req;  
52 -  
53 - if (!_srs_config->get_bw_check_enabled(req->vhost)) {  
54 - return ret;  
55 - }  
56 -  
57 - // validate the bandwidth check key  
58 - std::string key = "key=" + _srs_config->get_bw_check_key(req->vhost);  
59 - if (req->tcUrl.find(key) == std::string::npos) {  
60 - ret = ERROR_SYSTEM_BANDWIDTH_KEY;  
61 - srs_error("check the vhost=%s %s failed, tcUrl=%s, ret=%d",  
62 - req->vhost.c_str(), key.c_str(), req->tcUrl.c_str(), ret);  
63 - return ret;  
64 - }  
65 -  
66 - // shared global last check time,  
67 - // to avoid attach by bandwidth check,  
68 - // if client request check in the window(specifeid by interval),  
69 - // directly reject the request. 48 + int ret = ERROR_SUCCESS;
  49 +
  50 + rtmp = _rtmp;
  51 + req = _req;
  52 +
  53 + if (!_srs_config->get_bw_check_enabled(req->vhost)) {
  54 + return ret;
  55 + }
  56 +
  57 + // validate the bandwidth check key
  58 + std::string key = "key=" + _srs_config->get_bw_check_key(req->vhost);
  59 + if (req->tcUrl.find(key) == std::string::npos) {
  60 + ret = ERROR_SYSTEM_BANDWIDTH_KEY;
  61 + srs_error("check the vhost=%s %s failed, tcUrl=%s, ret=%d",
  62 + req->vhost.c_str(), key.c_str(), req->tcUrl.c_str(), ret);
  63 + return ret;
  64 + }
  65 +
  66 + // shared global last check time,
  67 + // to avoid attach by bandwidth check,
  68 + // if client request check in the window(specifeid by interval),
  69 + // directly reject the request.
70 static int64_t last_check_time = 0; 70 static int64_t last_check_time = 0;
71 int interval_ms = _srs_config->get_bw_check_interval_ms(req->vhost); 71 int interval_ms = _srs_config->get_bw_check_interval_ms(req->vhost);
72 72
@@ -75,16 +75,16 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe @@ -75,16 +75,16 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe
75 if (last_check_time > 0 && time_now - last_check_time < interval_ms) { 75 if (last_check_time > 0 && time_now - last_check_time < interval_ms) {
76 ret = ERROR_SYSTEM_BANDWIDTH_DENIED; 76 ret = ERROR_SYSTEM_BANDWIDTH_DENIED;
77 srs_trace("bandcheck denied, " 77 srs_trace("bandcheck denied, "
78 - "last_check=%"PRId64", now=%"PRId64", interval=%d",  
79 - last_check_time, time_now, interval_ms);  
80 - 78 + "last_check=%"PRId64", now=%"PRId64", interval=%d",
  79 + last_check_time, time_now, interval_ms);
  80 +
81 rtmp->response_connect_reject(req, "bandcheck rejected"); 81 rtmp->response_connect_reject(req, "bandcheck rejected");
82 return ret; 82 return ret;
83 } 83 }
84 84
85 // accept and do bandwidth check. 85 // accept and do bandwidth check.
86 - last_check_time = time_now;  
87 - 86 + last_check_time = time_now;
  87 +
88 char* local_ip = 0; 88 char* local_ip = 0;
89 if ((ret = get_local_ip(stfd, local_ip)) != ERROR_SUCCESS) { 89 if ((ret = get_local_ip(stfd, local_ip)) != ERROR_SUCCESS) {
90 srs_error("get local ip failed. ret = %d", ret); 90 srs_error("get local ip failed. ret = %d", ret);
@@ -137,7 +137,7 @@ int SrsBandwidth::do_bandwidth_check() @@ -137,7 +137,7 @@ int SrsBandwidth::do_bandwidth_check()
137 { 137 {
138 int ret = ERROR_SUCCESS; 138 int ret = ERROR_SUCCESS;
139 139
140 - SrsProtocol* protocol = rtmp->get_protocol(); 140 + SrsProtocol* protocol = rtmp->get_protocol();
141 141
142 int play_duration_ms = 3000; 142 int play_duration_ms = 3000;
143 int play_interval_ms = 0; 143 int play_interval_ms = 0;
@@ -154,14 +154,14 @@ int SrsBandwidth::do_bandwidth_check() @@ -154,14 +154,14 @@ int SrsBandwidth::do_bandwidth_check()
154 int64_t start_time = srs_get_system_time_ms(); 154 int64_t start_time = srs_get_system_time_ms();
155 155
156 ret = check_play(play_duration_ms, 156 ret = check_play(play_duration_ms,
157 - play_interval_ms, play_actual_duration_ms, play_bytes, limit_kbps); 157 + play_interval_ms, play_actual_duration_ms, play_bytes, limit_kbps);
158 if (ret != ERROR_SUCCESS) { 158 if (ret != ERROR_SUCCESS) {
159 srs_error("band width play check failed. ret=%d", ret); 159 srs_error("band width play check failed. ret=%d", ret);
160 return ret; 160 return ret;
161 } 161 }
162 162
163 ret = check_publish(publish_duration_ms, 163 ret = check_publish(publish_duration_ms,
164 - publish_interval_ms, publish_actual_duration_ms, publish_bytes, limit_kbps); 164 + publish_interval_ms, publish_actual_duration_ms, publish_bytes, limit_kbps);
165 if (ret != ERROR_SUCCESS) { 165 if (ret != ERROR_SUCCESS) {
166 srs_error("band width publish check failed. ret=%d", ret); 166 srs_error("band width publish check failed. ret=%d", ret);
167 return ret; 167 return ret;
@@ -188,29 +188,29 @@ int SrsBandwidth::do_bandwidth_check() @@ -188,29 +188,29 @@ int SrsBandwidth::do_bandwidth_check()
188 pkt->data->set("publish_bytes", SrsAmf0Any::number(publish_bytes)); 188 pkt->data->set("publish_bytes", SrsAmf0Any::number(publish_bytes));
189 pkt->data->set("publish_time", SrsAmf0Any::number(publish_actual_duration_ms)); 189 pkt->data->set("publish_time", SrsAmf0Any::number(publish_actual_duration_ms));
190 190
191 - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0); 191 + SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
192 if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) { 192 if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
193 srs_error("send bandwidth check finish message failed. ret=%d", ret); 193 srs_error("send bandwidth check finish message failed. ret=%d", ret);
194 return ret; 194 return ret;
195 } 195 }
196 196
197 // if flash, we notice the result, and expect a final packet. 197 // if flash, we notice the result, and expect a final packet.
198 - while (true) {  
199 - SrsCommonMessage* msg = NULL;  
200 - SrsBandwidthPacket* pkt = NULL;  
201 - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {  
202 - // info level to ignore and return success.  
203 - srs_info("expect final message failed. ret=%d", ret);  
204 - return ERROR_SUCCESS;  
205 - }  
206 - SrsAutoFree(SrsCommonMessage, msg, false);  
207 - srs_info("get final message success.");  
208 -  
209 - if (pkt->is_flash_final()) {  
210 - srs_info("BW check recv flash final response.");  
211 - break;  
212 - }  
213 - } 198 + while (true) {
  199 + SrsCommonMessage* msg = NULL;
  200 + SrsBandwidthPacket* pkt = NULL;
  201 + if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
  202 + // info level to ignore and return success.
  203 + srs_info("expect final message failed. ret=%d", ret);
  204 + return ERROR_SUCCESS;
  205 + }
  206 + SrsAutoFree(SrsCommonMessage, msg, false);
  207 + srs_info("get final message success.");
  208 +
  209 + if (pkt->is_flash_final()) {
  210 + srs_info("BW check recv flash final response.");
  211 + break;
  212 + }
  213 + }
214 214
215 srs_info("BW check finished."); 215 srs_info("BW check finished.");
216 216
@@ -218,44 +218,44 @@ int SrsBandwidth::do_bandwidth_check() @@ -218,44 +218,44 @@ int SrsBandwidth::do_bandwidth_check()
218 } 218 }
219 219
220 int SrsBandwidth::check_play( 220 int SrsBandwidth::check_play(
221 - int duration_ms, int interval_ms, int& actual_duration_ms,  
222 - int& play_bytes, int max_play_kbps) 221 + int duration_ms, int interval_ms, int& actual_duration_ms,
  222 + int& play_bytes, int max_play_kbps)
223 { 223 {
224 int ret = ERROR_SUCCESS; 224 int ret = ERROR_SUCCESS;
225 225
226 - SrsProtocol* protocol = rtmp->get_protocol();  
227 -  
228 - if (true) {  
229 - // send start play command to client  
230 - SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_play();  
231 -  
232 - pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));  
233 - pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));  
234 -  
235 - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);  
236 - if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {  
237 - srs_error("send bandwidth check start play message failed. ret=%d", ret);  
238 - return ret;  
239 - }  
240 - srs_info("BW check begin.");  
241 - }  
242 -  
243 - while (true) {  
244 - // recv client's starting play response  
245 - SrsCommonMessage* msg = NULL;  
246 - SrsBandwidthPacket* pkt = NULL;  
247 - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {  
248 - srs_error("expect bandwidth message failed. ret=%d", ret);  
249 - return ret;  
250 - }  
251 - SrsAutoFree(SrsCommonMessage, msg, false);  
252 - srs_info("get bandwidth message succes.");  
253 -  
254 - if (pkt->is_starting_play()) {  
255 - srs_info("BW check recv play begin response.");  
256 - break;  
257 - }  
258 - } 226 + SrsProtocol* protocol = rtmp->get_protocol();
  227 +
  228 + if (true) {
  229 + // send start play command to client
  230 + SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_play();
  231 +
  232 + pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));
  233 + pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));
  234 +
  235 + SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
  236 + if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
  237 + srs_error("send bandwidth check start play message failed. ret=%d", ret);
  238 + return ret;
  239 + }
  240 + srs_info("BW check begin.");
  241 + }
  242 +
  243 + while (true) {
  244 + // recv client's starting play response
  245 + SrsCommonMessage* msg = NULL;
  246 + SrsBandwidthPacket* pkt = NULL;
  247 + if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
  248 + srs_error("expect bandwidth message failed. ret=%d", ret);
  249 + return ret;
  250 + }
  251 + SrsAutoFree(SrsCommonMessage, msg, false);
  252 + srs_info("get bandwidth message succes.");
  253 +
  254 + if (pkt->is_starting_play()) {
  255 + srs_info("BW check recv play begin response.");
  256 + break;
  257 + }
  258 + }
259 259
260 // send play data to client 260 // send play data to client
261 int64_t current_time = srs_get_system_time_ms(); 261 int64_t current_time = srs_get_system_time_ms();
@@ -280,10 +280,10 @@ int SrsBandwidth::check_play( @@ -280,10 +280,10 @@ int SrsBandwidth::check_play(
280 } 280 }
281 data_count += 2; 281 data_count += 2;
282 282
283 - // TODO: FIXME: get length from the rtmp protocol stack. 283 + // TODO: FIXME: get length from the rtmp protocol stack.
284 play_bytes += pkt->get_payload_length(); 284 play_bytes += pkt->get_payload_length();
285 285
286 - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0); 286 + SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
287 if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) { 287 if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
288 srs_error("send bandwidth check play messages failed. ret=%d", ret); 288 srs_error("send bandwidth check play messages failed. ret=%d", ret);
289 return ret; 289 return ret;
@@ -305,81 +305,81 @@ int SrsBandwidth::check_play( @@ -305,81 +305,81 @@ int SrsBandwidth::check_play(
305 actual_duration_ms = srs_get_system_time_ms() - current_time; 305 actual_duration_ms = srs_get_system_time_ms() - current_time;
306 srs_info("BW check send play bytes over."); 306 srs_info("BW check send play bytes over.");
307 307
308 - if (true) {  
309 - // notify client to stop play  
310 - SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_play();  
311 - pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));  
312 - pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));  
313 - pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms));  
314 - pkt->data->set("bytes_delta", SrsAmf0Any::number(play_bytes)); 308 + if (true) {
  309 + // notify client to stop play
  310 + SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_play();
  311 + pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));
  312 + pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));
  313 + pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms));
  314 + pkt->data->set("bytes_delta", SrsAmf0Any::number(play_bytes));
315 315
316 - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0); 316 + SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
317 if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) { 317 if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
318 - srs_error("send bandwidth check stop play message failed. ret=%d", ret);  
319 - return ret;  
320 - }  
321 - srs_info("BW check stop play bytes.");  
322 - }  
323 -  
324 - while (true) {  
325 - // recv client's stop play response.  
326 - SrsCommonMessage* msg = NULL;  
327 - SrsBandwidthPacket* pkt = NULL;  
328 - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {  
329 - srs_error("expect bandwidth message failed. ret=%d", ret);  
330 - return ret;  
331 - }  
332 - SrsAutoFree(SrsCommonMessage, msg, false);  
333 - srs_info("get bandwidth message succes.");  
334 -  
335 - if (pkt->is_stopped_play()) {  
336 - srs_info("BW check recv stop play response.");  
337 - break;  
338 - }  
339 - } 318 + srs_error("send bandwidth check stop play message failed. ret=%d", ret);
  319 + return ret;
  320 + }
  321 + srs_info("BW check stop play bytes.");
  322 + }
  323 +
  324 + while (true) {
  325 + // recv client's stop play response.
  326 + SrsCommonMessage* msg = NULL;
  327 + SrsBandwidthPacket* pkt = NULL;
  328 + if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
  329 + srs_error("expect bandwidth message failed. ret=%d", ret);
  330 + return ret;
  331 + }
  332 + SrsAutoFree(SrsCommonMessage, msg, false);
  333 + srs_info("get bandwidth message succes.");
  334 +
  335 + if (pkt->is_stopped_play()) {
  336 + srs_info("BW check recv stop play response.");
  337 + break;
  338 + }
  339 + }
340 340
341 return ret; 341 return ret;
342 } 342 }
343 343
344 int SrsBandwidth::check_publish( 344 int SrsBandwidth::check_publish(
345 - int duration_ms, int interval_ms, int& actual_duration_ms,  
346 - int& publish_bytes, int max_pub_kbps) 345 + int duration_ms, int interval_ms, int& actual_duration_ms,
  346 + int& publish_bytes, int max_pub_kbps)
347 { 347 {
348 int ret = ERROR_SUCCESS; 348 int ret = ERROR_SUCCESS;
349 349
350 - SrsProtocol* protocol = rtmp->get_protocol();  
351 -  
352 - if (true) {  
353 - // notify client to start publish  
354 - SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_publish();  
355 -  
356 - pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));  
357 - pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));  
358 -  
359 - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);  
360 - if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {  
361 - srs_error("send bandwidth check start publish message failed. ret=%d", ret);  
362 - return ret;  
363 - }  
364 - srs_info("BW check publish begin.");  
365 - }  
366 -  
367 - while (true) {  
368 - // read client's notification of starting publish  
369 - SrsCommonMessage* msg = NULL;  
370 - SrsBandwidthPacket* pkt = NULL;  
371 - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {  
372 - srs_error("expect bandwidth message failed. ret=%d", ret);  
373 - return ret;  
374 - }  
375 - SrsAutoFree(SrsCommonMessage, msg, false);  
376 - srs_info("get bandwidth message succes.");  
377 -  
378 - if (pkt->is_starting_publish()) {  
379 - srs_info("BW check recv publish begin response.");  
380 - break;  
381 - }  
382 - } 350 + SrsProtocol* protocol = rtmp->get_protocol();
  351 +
  352 + if (true) {
  353 + // notify client to start publish
  354 + SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_publish();
  355 +
  356 + pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));
  357 + pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));
  358 +
  359 + SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
  360 + if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
  361 + srs_error("send bandwidth check start publish message failed. ret=%d", ret);
  362 + return ret;
  363 + }
  364 + srs_info("BW check publish begin.");
  365 + }
  366 +
  367 + while (true) {
  368 + // read client's notification of starting publish
  369 + SrsCommonMessage* msg = NULL;
  370 + SrsBandwidthPacket* pkt = NULL;
  371 + if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
  372 + srs_error("expect bandwidth message failed. ret=%d", ret);
  373 + return ret;
  374 + }
  375 + SrsAutoFree(SrsCommonMessage, msg, false);
  376 + srs_info("get bandwidth message succes.");
  377 +
  378 + if (pkt->is_starting_publish()) {
  379 + srs_info("BW check recv publish begin response.");
  380 + break;
  381 + }
  382 + }
383 383
384 // recv publish msgs until @duration_ms ms 384 // recv publish msgs until @duration_ms ms
385 int64_t current_time = srs_get_system_time_ms(); 385 int64_t current_time = srs_get_system_time_ms();
@@ -393,7 +393,7 @@ int SrsBandwidth::check_publish( @@ -393,7 +393,7 @@ int SrsBandwidth::check_publish(
393 } 393 }
394 SrsAutoFree(SrsCommonMessage, msg, false); 394 SrsAutoFree(SrsCommonMessage, msg, false);
395 395
396 - // TODO: FIXME. 396 + // TODO: FIXME.
397 publish_bytes += msg->header.payload_length; 397 publish_bytes += msg->header.payload_length;
398 398
399 int kbps = 0; 399 int kbps = 0;
@@ -411,21 +411,21 @@ int SrsBandwidth::check_publish( @@ -411,21 +411,21 @@ int SrsBandwidth::check_publish(
411 actual_duration_ms = srs_get_system_time_ms() - current_time; 411 actual_duration_ms = srs_get_system_time_ms() - current_time;
412 srs_info("BW check recv publish data over."); 412 srs_info("BW check recv publish data over.");
413 413
414 - if (true) {  
415 - // notify client to stop publish  
416 - SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_publish();  
417 - pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));  
418 - pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));  
419 - pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms));  
420 - pkt->data->set("bytes_delta", SrsAmf0Any::number(publish_bytes)); 414 + if (true) {
  415 + // notify client to stop publish
  416 + SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_publish();
  417 + pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));
  418 + pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));
  419 + pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms));
  420 + pkt->data->set("bytes_delta", SrsAmf0Any::number(publish_bytes));
421 421
422 - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0); 422 + SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0);
423 if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) { 423 if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
424 - srs_error("send bandwidth check stop publish message failed. ret=%d", ret);  
425 - return ret;  
426 - }  
427 - srs_info("BW check stop publish bytes.");  
428 - } 424 + srs_error("send bandwidth check stop publish message failed. ret=%d", ret);
  425 + return ret;
  426 + }
  427 + srs_info("BW check stop publish bytes.");
  428 + }
429 429
430 // expect client to stop publish 430 // expect client to stop publish
431 // if flash client, we never expect the client stop publish bytes, 431 // if flash client, we never expect the client stop publish bytes,
@@ -433,22 +433,22 @@ int SrsBandwidth::check_publish( @@ -433,22 +433,22 @@ int SrsBandwidth::check_publish(
433 // there are many many packets in the queue. 433 // there are many many packets in the queue.
434 // we just ignore the packet and send the bandwidth test data. 434 // we just ignore the packet and send the bandwidth test data.
435 // TODO: FIXME: check whether flash client. 435 // TODO: FIXME: check whether flash client.
436 - while (false) {  
437 - // recv client's stop publish response.  
438 - SrsCommonMessage* msg = NULL;  
439 - SrsBandwidthPacket* pkt = NULL;  
440 - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {  
441 - srs_error("expect bandwidth message failed. ret=%d", ret);  
442 - return ret;  
443 - }  
444 - SrsAutoFree(SrsCommonMessage, msg, false);  
445 - srs_info("get bandwidth message succes.");  
446 -  
447 - if (pkt->is_stopped_publish()) {  
448 - srs_info("BW check recv stop publish response.");  
449 - break;  
450 - }  
451 - } 436 + while (false) {
  437 + // recv client's stop publish response.
  438 + SrsCommonMessage* msg = NULL;
  439 + SrsBandwidthPacket* pkt = NULL;
  440 + if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
  441 + srs_error("expect bandwidth message failed. ret=%d", ret);
  442 + return ret;
  443 + }
  444 + SrsAutoFree(SrsCommonMessage, msg, false);
  445 + srs_info("get bandwidth message succes.");
  446 +
  447 + if (pkt->is_stopped_publish()) {
  448 + srs_info("BW check recv stop publish response.");
  449 + break;
  450 + }
  451 + }
452 452
453 return ret; 453 return ret;
454 } 454 }
@@ -44,603 +44,603 @@ using namespace std; @@ -44,603 +44,603 @@ using namespace std;
44 #include <srs_app_socket.hpp> 44 #include <srs_app_socket.hpp>
45 45
46 SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd) 46 SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd)
47 - : SrsConnection(srs_server, client_stfd) 47 + : SrsConnection(srs_server, client_stfd)
48 { 48 {
49 - ip = NULL;  
50 - req = new SrsRequest();  
51 - res = new SrsResponse();  
52 - skt = new SrsSocket(client_stfd);  
53 - rtmp = new SrsRtmpServer(skt);  
54 - refer = new SrsRefer();  
55 -#ifdef SRS_HTTP_CALLBACK  
56 - http_hooks = new SrsHttpHooks(); 49 + ip = NULL;
  50 + req = new SrsRequest();
  51 + res = new SrsResponse();
  52 + skt = new SrsSocket(client_stfd);
  53 + rtmp = new SrsRtmpServer(skt);
  54 + refer = new SrsRefer();
  55 +#ifdef SRS_HTTP_CALLBACK
  56 + http_hooks = new SrsHttpHooks();
57 #endif 57 #endif
58 - bandwidth = new SrsBandwidth();  
59 -  
60 - _srs_config->subscribe(this); 58 + bandwidth = new SrsBandwidth();
  59 +
  60 + _srs_config->subscribe(this);
61 } 61 }
62 62
63 SrsClient::~SrsClient() 63 SrsClient::~SrsClient()
64 { 64 {
65 - _srs_config->unsubscribe(this);  
66 -  
67 - srs_freepa(ip);  
68 - srs_freep(req);  
69 - srs_freep(res);  
70 - srs_freep(rtmp);  
71 - srs_freep(skt);  
72 - srs_freep(refer);  
73 -#ifdef SRS_HTTP_CALLBACK  
74 - srs_freep(http_hooks); 65 + _srs_config->unsubscribe(this);
  66 +
  67 + srs_freepa(ip);
  68 + srs_freep(req);
  69 + srs_freep(res);
  70 + srs_freep(rtmp);
  71 + srs_freep(skt);
  72 + srs_freep(refer);
  73 +#ifdef SRS_HTTP_CALLBACK
  74 + srs_freep(http_hooks);
75 #endif 75 #endif
76 - srs_freep(bandwidth); 76 + srs_freep(bandwidth);
77 } 77 }
78 78
79 // TODO: return detail message when error for client. 79 // TODO: return detail message when error for client.
80 int SrsClient::do_cycle() 80 int SrsClient::do_cycle()
81 { 81 {
82 - int ret = ERROR_SUCCESS;  
83 -  
84 - if ((ret = get_peer_ip()) != ERROR_SUCCESS) {  
85 - srs_error("get peer ip failed. ret=%d", ret);  
86 - return ret;  
87 - }  
88 - srs_trace("get peer ip success. ip=%s, send_to=%"PRId64", recv_to=%"PRId64"",  
89 - ip, SRS_SEND_TIMEOUT_US, SRS_RECV_TIMEOUT_US);  
90 -  
91 - rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US);  
92 - rtmp->set_send_timeout(SRS_SEND_TIMEOUT_US);  
93 -  
94 - if ((ret = rtmp->handshake()) != ERROR_SUCCESS) {  
95 - srs_error("rtmp handshake failed. ret=%d", ret);  
96 - return ret;  
97 - }  
98 - srs_verbose("rtmp handshake success");  
99 -  
100 - if ((ret = rtmp->connect_app(req)) != ERROR_SUCCESS) {  
101 - srs_error("rtmp connect vhost/app failed. ret=%d", ret);  
102 - return ret;  
103 - }  
104 - srs_verbose("rtmp connect app success");  
105 -  
106 - // discovery vhost, resolve the vhost from config  
107 - SrsConfDirective* parsed_vhost = _srs_config->get_vhost(req->vhost);  
108 - if (parsed_vhost) {  
109 - req->vhost = parsed_vhost->arg0();  
110 - }  
111 -  
112 - srs_info("discovery app success. schema=%s, vhost=%s, port=%s, app=%s",  
113 - req->schema.c_str(), req->vhost.c_str(), req->port.c_str(), req->app.c_str());  
114 -  
115 - if (req->schema.empty() || req->vhost.empty() || req->port.empty() || req->app.empty()) {  
116 - ret = ERROR_RTMP_REQ_TCURL;  
117 - srs_error("discovery tcUrl failed. "  
118 - "tcUrl=%s, schema=%s, vhost=%s, port=%s, app=%s, ret=%d",  
119 - req->tcUrl.c_str(), req->schema.c_str(), req->vhost.c_str(), req->port.c_str(), req->app.c_str(), ret);  
120 - return ret;  
121 - }  
122 -  
123 - // check vhost  
124 - if ((ret = check_vhost()) != ERROR_SUCCESS) {  
125 - srs_error("check vhost failed. ret=%d", ret);  
126 - return ret;  
127 - }  
128 - srs_verbose("check vhost success.");  
129 -  
130 - srs_trace("rtmp connect app success. "  
131 - "tcUrl=%s, pageUrl=%s, swfUrl=%s, schema=%s, vhost=%s, port=%s, app=%s",  
132 - req->tcUrl.c_str(), req->pageUrl.c_str(), req->swfUrl.c_str(),  
133 - req->schema.c_str(), req->vhost.c_str(), req->port.c_str(),  
134 - req->app.c_str());  
135 -  
136 - ret = service_cycle();  
137 - on_close();  
138 -  
139 - return ret; 82 + int ret = ERROR_SUCCESS;
  83 +
  84 + if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
  85 + srs_error("get peer ip failed. ret=%d", ret);
  86 + return ret;
  87 + }
  88 + srs_trace("get peer ip success. ip=%s, send_to=%"PRId64", recv_to=%"PRId64"",
  89 + ip, SRS_SEND_TIMEOUT_US, SRS_RECV_TIMEOUT_US);
  90 +
  91 + rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US);
  92 + rtmp->set_send_timeout(SRS_SEND_TIMEOUT_US);
  93 +
  94 + if ((ret = rtmp->handshake()) != ERROR_SUCCESS) {
  95 + srs_error("rtmp handshake failed. ret=%d", ret);
  96 + return ret;
  97 + }
  98 + srs_verbose("rtmp handshake success");
  99 +
  100 + if ((ret = rtmp->connect_app(req)) != ERROR_SUCCESS) {
  101 + srs_error("rtmp connect vhost/app failed. ret=%d", ret);
  102 + return ret;
  103 + }
  104 + srs_verbose("rtmp connect app success");
  105 +
  106 + // discovery vhost, resolve the vhost from config
  107 + SrsConfDirective* parsed_vhost = _srs_config->get_vhost(req->vhost);
  108 + if (parsed_vhost) {
  109 + req->vhost = parsed_vhost->arg0();
  110 + }
  111 +
  112 + srs_info("discovery app success. schema=%s, vhost=%s, port=%s, app=%s",
  113 + req->schema.c_str(), req->vhost.c_str(), req->port.c_str(), req->app.c_str());
  114 +
  115 + if (req->schema.empty() || req->vhost.empty() || req->port.empty() || req->app.empty()) {
  116 + ret = ERROR_RTMP_REQ_TCURL;
  117 + srs_error("discovery tcUrl failed. "
  118 + "tcUrl=%s, schema=%s, vhost=%s, port=%s, app=%s, ret=%d",
  119 + req->tcUrl.c_str(), req->schema.c_str(), req->vhost.c_str(), req->port.c_str(), req->app.c_str(), ret);
  120 + return ret;
  121 + }
  122 +
  123 + // check vhost
  124 + if ((ret = check_vhost()) != ERROR_SUCCESS) {
  125 + srs_error("check vhost failed. ret=%d", ret);
  126 + return ret;
  127 + }
  128 + srs_verbose("check vhost success.");
  129 +
  130 + srs_trace("rtmp connect app success. "
  131 + "tcUrl=%s, pageUrl=%s, swfUrl=%s, schema=%s, vhost=%s, port=%s, app=%s",
  132 + req->tcUrl.c_str(), req->pageUrl.c_str(), req->swfUrl.c_str(),
  133 + req->schema.c_str(), req->vhost.c_str(), req->port.c_str(),
  134 + req->app.c_str());
  135 +
  136 + ret = service_cycle();
  137 + on_close();
  138 +
  139 + return ret;
140 } 140 }
141 141
142 int SrsClient::on_reload_vhost_removed(string vhost) 142 int SrsClient::on_reload_vhost_removed(string vhost)
143 { 143 {
144 - int ret = ERROR_SUCCESS;  
145 -  
146 - if (req->vhost != vhost) {  
147 - return ret;  
148 - }  
149 -  
150 - // if the vhost connected is removed, disconnect the client.  
151 - srs_trace("vhost %s removed/disabled, close client url=%s",  
152 - vhost.c_str(), req->get_stream_url().c_str());  
153 -  
154 - srs_close_stfd(stfd);  
155 -  
156 - return ret; 144 + int ret = ERROR_SUCCESS;
  145 +
  146 + if (req->vhost != vhost) {
  147 + return ret;
  148 + }
  149 +
  150 + // if the vhost connected is removed, disconnect the client.
  151 + srs_trace("vhost %s removed/disabled, close client url=%s",
  152 + vhost.c_str(), req->get_stream_url().c_str());
  153 +
  154 + srs_close_stfd(stfd);
  155 +
  156 + return ret;
157 } 157 }
158 - 158 +
159 int SrsClient::service_cycle() 159 int SrsClient::service_cycle()
160 -{  
161 - int ret = ERROR_SUCCESS;  
162 -  
163 - if ((ret = rtmp->set_window_ack_size(2.5 * 1000 * 1000)) != ERROR_SUCCESS) {  
164 - srs_error("set window acknowledgement size failed. ret=%d", ret);  
165 - return ret;  
166 - }  
167 - srs_verbose("set window acknowledgement size success");  
168 -  
169 - if ((ret = rtmp->set_peer_bandwidth(2.5 * 1000 * 1000, 2)) != ERROR_SUCCESS) {  
170 - srs_error("set peer bandwidth failed. ret=%d", ret);  
171 - return ret;  
172 - }  
173 - srs_verbose("set peer bandwidth success");  
174 -  
175 - // do bandwidth test if connect to the vhost which is for bandwidth check.  
176 - if (_srs_config->get_bw_check_enabled(req->vhost)) {  
177 - return bandwidth->bandwidth_test(req, stfd, rtmp);  
178 - }  
179 -  
180 - if ((ret = rtmp->response_connect_app(req)) != ERROR_SUCCESS) {  
181 - srs_error("response connect app failed. ret=%d", ret);  
182 - return ret;  
183 - }  
184 - srs_verbose("response connect app success");  
185 -  
186 - if ((ret = rtmp->on_bw_done()) != ERROR_SUCCESS) {  
187 - srs_error("on_bw_done failed. ret=%d", ret);  
188 - return ret;  
189 - }  
190 - srs_verbose("on_bw_done success");  
191 -  
192 - while (true) {  
193 - ret = stream_service_cycle();  
194 -  
195 - // stream service must terminated with error, never success.  
196 - srs_assert(ret != ERROR_SUCCESS);  
197 -  
198 - // when not system control error, fatal error, return.  
199 - if (!srs_is_system_control_error(ret)) {  
200 - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) {  
201 - srs_error("stream service cycle failed. ret=%d", ret);  
202 - }  
203 - return ret;  
204 - }  
205 -  
206 - // for republish, continue service  
207 - if (ret == ERROR_CONTROL_REPUBLISH) {  
208 - // set timeout to a larger value, wait for encoder to republish.  
209 - rtmp->set_send_timeout(SRS_REPUBLISH_RECV_TIMEOUT_US);  
210 - rtmp->set_recv_timeout(SRS_REPUBLISH_SEND_TIMEOUT_US);  
211 -  
212 - srs_trace("control message(unpublish) accept, retry stream service.");  
213 - continue;  
214 - }  
215 -  
216 - // for "some" system control error,  
217 - // logical accept and retry stream service.  
218 - if (ret == ERROR_CONTROL_RTMP_CLOSE) {  
219 - // set timeout to a larger value, for user paused.  
220 - rtmp->set_recv_timeout(SRS_PAUSED_RECV_TIMEOUT_US);  
221 - rtmp->set_send_timeout(SRS_PAUSED_SEND_TIMEOUT_US);  
222 -  
223 - srs_trace("control message(close) accept, retry stream service.");  
224 - continue;  
225 - }  
226 -  
227 - // for other system control message, fatal error.  
228 - srs_error("control message(%d) reject as error. ret=%d", ret, ret);  
229 - return ret;  
230 - }  
231 -  
232 - return ret; 160 +{
  161 + int ret = ERROR_SUCCESS;
  162 +
  163 + if ((ret = rtmp->set_window_ack_size(2.5 * 1000 * 1000)) != ERROR_SUCCESS) {
  164 + srs_error("set window acknowledgement size failed. ret=%d", ret);
  165 + return ret;
  166 + }
  167 + srs_verbose("set window acknowledgement size success");
  168 +
  169 + if ((ret = rtmp->set_peer_bandwidth(2.5 * 1000 * 1000, 2)) != ERROR_SUCCESS) {
  170 + srs_error("set peer bandwidth failed. ret=%d", ret);
  171 + return ret;
  172 + }
  173 + srs_verbose("set peer bandwidth success");
  174 +
  175 + // do bandwidth test if connect to the vhost which is for bandwidth check.
  176 + if (_srs_config->get_bw_check_enabled(req->vhost)) {
  177 + return bandwidth->bandwidth_test(req, stfd, rtmp);
  178 + }
  179 +
  180 + if ((ret = rtmp->response_connect_app(req)) != ERROR_SUCCESS) {
  181 + srs_error("response connect app failed. ret=%d", ret);
  182 + return ret;
  183 + }
  184 + srs_verbose("response connect app success");
  185 +
  186 + if ((ret = rtmp->on_bw_done()) != ERROR_SUCCESS) {
  187 + srs_error("on_bw_done failed. ret=%d", ret);
  188 + return ret;
  189 + }
  190 + srs_verbose("on_bw_done success");
  191 +
  192 + while (true) {
  193 + ret = stream_service_cycle();
  194 +
  195 + // stream service must terminated with error, never success.
  196 + srs_assert(ret != ERROR_SUCCESS);
  197 +
  198 + // when not system control error, fatal error, return.
  199 + if (!srs_is_system_control_error(ret)) {
  200 + if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) {
  201 + srs_error("stream service cycle failed. ret=%d", ret);
  202 + }
  203 + return ret;
  204 + }
  205 +
  206 + // for republish, continue service
  207 + if (ret == ERROR_CONTROL_REPUBLISH) {
  208 + // set timeout to a larger value, wait for encoder to republish.
  209 + rtmp->set_send_timeout(SRS_REPUBLISH_RECV_TIMEOUT_US);
  210 + rtmp->set_recv_timeout(SRS_REPUBLISH_SEND_TIMEOUT_US);
  211 +
  212 + srs_trace("control message(unpublish) accept, retry stream service.");
  213 + continue;
  214 + }
  215 +
  216 + // for "some" system control error,
  217 + // logical accept and retry stream service.
  218 + if (ret == ERROR_CONTROL_RTMP_CLOSE) {
  219 + // set timeout to a larger value, for user paused.
  220 + rtmp->set_recv_timeout(SRS_PAUSED_RECV_TIMEOUT_US);
  221 + rtmp->set_send_timeout(SRS_PAUSED_SEND_TIMEOUT_US);
  222 +
  223 + srs_trace("control message(close) accept, retry stream service.");
  224 + continue;
  225 + }
  226 +
  227 + // for other system control message, fatal error.
  228 + srs_error("control message(%d) reject as error. ret=%d", ret, ret);
  229 + return ret;
  230 + }
  231 +
  232 + return ret;
233 } 233 }
234 234
235 int SrsClient::stream_service_cycle() 235 int SrsClient::stream_service_cycle()
236 { 236 {
237 - int ret = ERROR_SUCCESS;  
238 -  
239 - SrsClientType type;  
240 - if ((ret = rtmp->identify_client(res->stream_id, type, req->stream)) != ERROR_SUCCESS) {  
241 - srs_error("identify client failed. ret=%d", ret);  
242 - return ret;  
243 - }  
244 - req->strip();  
245 - srs_trace("identify client success. type=%s, stream_name=%s",  
246 - srs_client_type_string(type).c_str(), req->stream.c_str());  
247 -  
248 - // client is identified, set the timeout to service timeout.  
249 - rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US);  
250 - rtmp->set_send_timeout(SRS_SEND_TIMEOUT_US);  
251 -  
252 - // set chunk size to larger. 237 + int ret = ERROR_SUCCESS;
  238 +
  239 + SrsClientType type;
  240 + if ((ret = rtmp->identify_client(res->stream_id, type, req->stream)) != ERROR_SUCCESS) {
  241 + srs_error("identify client failed. ret=%d", ret);
  242 + return ret;
  243 + }
  244 + req->strip();
  245 + srs_trace("identify client success. type=%s, stream_name=%s",
  246 + srs_client_type_string(type).c_str(), req->stream.c_str());
  247 +
  248 + // client is identified, set the timeout to service timeout.
  249 + rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US);
  250 + rtmp->set_send_timeout(SRS_SEND_TIMEOUT_US);
  251 +
  252 + // set chunk size to larger.
253 int chunk_size = _srs_config->get_chunk_size(req->vhost); 253 int chunk_size = _srs_config->get_chunk_size(req->vhost);
254 - if ((ret = rtmp->set_chunk_size(chunk_size)) != ERROR_SUCCESS) {  
255 - srs_error("set chunk_size=%d failed. ret=%d", chunk_size, ret);  
256 - return ret;  
257 - }  
258 - srs_trace("set chunk_size=%d success", chunk_size);  
259 -  
260 - // find a source to serve.  
261 - SrsSource* source = SrsSource::find(req);  
262 - srs_assert(source != NULL);  
263 -  
264 - // check publish available.  
265 - if (type != SrsClientPlay && !source->can_publish()) {  
266 - ret = ERROR_SYSTEM_STREAM_BUSY;  
267 - srs_warn("stream %s is already publishing. ret=%d",  
268 - req->get_stream_url().c_str(), ret);  
269 - // to delay request  
270 - st_usleep(SRS_STREAM_BUSY_SLEEP_US);  
271 - return ret;  
272 - }  
273 -  
274 - bool enabled_cache = _srs_config->get_gop_cache(req->vhost);  
275 - srs_info("source found, url=%s, enabled_cache=%d", req->get_stream_url().c_str(), enabled_cache);  
276 - source->set_cache(enabled_cache);  
277 -  
278 - switch (type) {  
279 - case SrsClientPlay: {  
280 - srs_verbose("start to play stream %s.", req->stream.c_str());  
281 -  
282 - if ((ret = rtmp->start_play(res->stream_id)) != ERROR_SUCCESS) {  
283 - srs_error("start to play stream failed. ret=%d", ret);  
284 - return ret;  
285 - }  
286 - if ((ret = on_play()) != ERROR_SUCCESS) {  
287 - srs_error("http hook on_play failed. ret=%d", ret);  
288 - return ret;  
289 - }  
290 - srs_info("start to play stream %s success", req->stream.c_str());  
291 - ret = playing(source);  
292 - on_stop();  
293 - return ret;  
294 - }  
295 - case SrsClientFMLEPublish: {  
296 - srs_verbose("FMLE start to publish stream %s.", req->stream.c_str());  
297 -  
298 - if ((ret = rtmp->start_fmle_publish(res->stream_id)) != ERROR_SUCCESS) {  
299 - srs_error("start to publish stream failed. ret=%d", ret);  
300 - return ret;  
301 - }  
302 - if ((ret = on_publish()) != ERROR_SUCCESS) {  
303 - srs_error("http hook on_publish failed. ret=%d", ret);  
304 - return ret;  
305 - }  
306 - srs_info("start to publish stream %s success", req->stream.c_str());  
307 - ret = fmle_publish(source);  
308 - source->on_unpublish();  
309 - on_unpublish();  
310 - return ret;  
311 - }  
312 - case SrsClientFlashPublish: {  
313 - srs_verbose("flash start to publish stream %s.", req->stream.c_str());  
314 -  
315 - if ((ret = rtmp->start_flash_publish(res->stream_id)) != ERROR_SUCCESS) {  
316 - srs_error("flash start to publish stream failed. ret=%d", ret);  
317 - return ret;  
318 - }  
319 - if ((ret = on_publish()) != ERROR_SUCCESS) {  
320 - srs_error("http hook on_publish failed. ret=%d", ret);  
321 - return ret;  
322 - }  
323 - srs_info("flash start to publish stream %s success", req->stream.c_str());  
324 - ret = flash_publish(source);  
325 - source->on_unpublish();  
326 - on_unpublish();  
327 - return ret;  
328 - }  
329 - default: {  
330 - ret = ERROR_SYSTEM_CLIENT_INVALID;  
331 - srs_info("invalid client type=%d. ret=%d", type, ret);  
332 - return ret;  
333 - }  
334 - }  
335 -  
336 - return ret; 254 + if ((ret = rtmp->set_chunk_size(chunk_size)) != ERROR_SUCCESS) {
  255 + srs_error("set chunk_size=%d failed. ret=%d", chunk_size, ret);
  256 + return ret;
  257 + }
  258 + srs_trace("set chunk_size=%d success", chunk_size);
  259 +
  260 + // find a source to serve.
  261 + SrsSource* source = SrsSource::find(req);
  262 + srs_assert(source != NULL);
  263 +
  264 + // check publish available.
  265 + if (type != SrsClientPlay && !source->can_publish()) {
  266 + ret = ERROR_SYSTEM_STREAM_BUSY;
  267 + srs_warn("stream %s is already publishing. ret=%d",
  268 + req->get_stream_url().c_str(), ret);
  269 + // to delay request
  270 + st_usleep(SRS_STREAM_BUSY_SLEEP_US);
  271 + return ret;
  272 + }
  273 +
  274 + bool enabled_cache = _srs_config->get_gop_cache(req->vhost);
  275 + srs_info("source found, url=%s, enabled_cache=%d", req->get_stream_url().c_str(), enabled_cache);
  276 + source->set_cache(enabled_cache);
  277 +
  278 + switch (type) {
  279 + case SrsClientPlay: {
  280 + srs_verbose("start to play stream %s.", req->stream.c_str());
  281 +
  282 + if ((ret = rtmp->start_play(res->stream_id)) != ERROR_SUCCESS) {
  283 + srs_error("start to play stream failed. ret=%d", ret);
  284 + return ret;
  285 + }
  286 + if ((ret = on_play()) != ERROR_SUCCESS) {
  287 + srs_error("http hook on_play failed. ret=%d", ret);
  288 + return ret;
  289 + }
  290 + srs_info("start to play stream %s success", req->stream.c_str());
  291 + ret = playing(source);
  292 + on_stop();
  293 + return ret;
  294 + }
  295 + case SrsClientFMLEPublish: {
  296 + srs_verbose("FMLE start to publish stream %s.", req->stream.c_str());
  297 +
  298 + if ((ret = rtmp->start_fmle_publish(res->stream_id)) != ERROR_SUCCESS) {
  299 + srs_error("start to publish stream failed. ret=%d", ret);
  300 + return ret;
  301 + }
  302 + if ((ret = on_publish()) != ERROR_SUCCESS) {
  303 + srs_error("http hook on_publish failed. ret=%d", ret);
  304 + return ret;
  305 + }
  306 + srs_info("start to publish stream %s success", req->stream.c_str());
  307 + ret = fmle_publish(source);
  308 + source->on_unpublish();
  309 + on_unpublish();
  310 + return ret;
  311 + }
  312 + case SrsClientFlashPublish: {
  313 + srs_verbose("flash start to publish stream %s.", req->stream.c_str());
  314 +
  315 + if ((ret = rtmp->start_flash_publish(res->stream_id)) != ERROR_SUCCESS) {
  316 + srs_error("flash start to publish stream failed. ret=%d", ret);
  317 + return ret;
  318 + }
  319 + if ((ret = on_publish()) != ERROR_SUCCESS) {
  320 + srs_error("http hook on_publish failed. ret=%d", ret);
  321 + return ret;
  322 + }
  323 + srs_info("flash start to publish stream %s success", req->stream.c_str());
  324 + ret = flash_publish(source);
  325 + source->on_unpublish();
  326 + on_unpublish();
  327 + return ret;
  328 + }
  329 + default: {
  330 + ret = ERROR_SYSTEM_CLIENT_INVALID;
  331 + srs_info("invalid client type=%d. ret=%d", type, ret);
  332 + return ret;
  333 + }
  334 + }
  335 +
  336 + return ret;
337 } 337 }
338 338
339 int SrsClient::check_vhost() 339 int SrsClient::check_vhost()
340 { 340 {
341 - int ret = ERROR_SUCCESS;  
342 -  
343 - srs_assert(req != NULL);  
344 -  
345 - SrsConfDirective* vhost = _srs_config->get_vhost(req->vhost);  
346 - if (vhost == NULL) {  
347 - ret = ERROR_RTMP_VHOST_NOT_FOUND;  
348 - srs_error("vhost %s not found. ret=%d", req->vhost.c_str(), ret);  
349 - return ret;  
350 - }  
351 -  
352 - if (!_srs_config->get_vhost_enabled(req->vhost)) {  
353 - ret = ERROR_RTMP_VHOST_NOT_FOUND;  
354 - srs_error("vhost %s disabled. ret=%d", req->vhost.c_str(), ret);  
355 - return ret;  
356 - }  
357 -  
358 - if (req->vhost != vhost->arg0()) {  
359 - srs_trace("vhost change from %s to %s", req->vhost.c_str(), vhost->arg0().c_str());  
360 - req->vhost = vhost->arg0();  
361 - }  
362 -  
363 - if ((ret = refer->check(req->pageUrl, _srs_config->get_refer(req->vhost))) != ERROR_SUCCESS) {  
364 - srs_error("check refer failed. ret=%d", ret);  
365 - return ret;  
366 - }  
367 - srs_verbose("check refer success.");  
368 -  
369 - if ((ret = on_connect()) != ERROR_SUCCESS) {  
370 - return ret;  
371 - }  
372 -  
373 - return ret; 341 + int ret = ERROR_SUCCESS;
  342 +
  343 + srs_assert(req != NULL);
  344 +
  345 + SrsConfDirective* vhost = _srs_config->get_vhost(req->vhost);
  346 + if (vhost == NULL) {
  347 + ret = ERROR_RTMP_VHOST_NOT_FOUND;
  348 + srs_error("vhost %s not found. ret=%d", req->vhost.c_str(), ret);
  349 + return ret;
  350 + }
  351 +
  352 + if (!_srs_config->get_vhost_enabled(req->vhost)) {
  353 + ret = ERROR_RTMP_VHOST_NOT_FOUND;
  354 + srs_error("vhost %s disabled. ret=%d", req->vhost.c_str(), ret);
  355 + return ret;
  356 + }
  357 +
  358 + if (req->vhost != vhost->arg0()) {
  359 + srs_trace("vhost change from %s to %s", req->vhost.c_str(), vhost->arg0().c_str());
  360 + req->vhost = vhost->arg0();
  361 + }
  362 +
  363 + if ((ret = refer->check(req->pageUrl, _srs_config->get_refer(req->vhost))) != ERROR_SUCCESS) {
  364 + srs_error("check refer failed. ret=%d", ret);
  365 + return ret;
  366 + }
  367 + srs_verbose("check refer success.");
  368 +
  369 + if ((ret = on_connect()) != ERROR_SUCCESS) {
  370 + return ret;
  371 + }
  372 +
  373 + return ret;
374 } 374 }
375 375
376 int SrsClient::playing(SrsSource* source) 376 int SrsClient::playing(SrsSource* source)
377 { 377 {
378 - int ret = ERROR_SUCCESS;  
379 -  
380 - if ((ret = refer->check(req->pageUrl, _srs_config->get_refer_play(req->vhost))) != ERROR_SUCCESS) {  
381 - srs_error("check play_refer failed. ret=%d", ret);  
382 - return ret;  
383 - }  
384 - srs_verbose("check play_refer success.");  
385 -  
386 - SrsConsumer* consumer = NULL;  
387 - if ((ret = source->create_consumer(consumer)) != ERROR_SUCCESS) {  
388 - srs_error("create consumer failed. ret=%d", ret);  
389 - return ret;  
390 - }  
391 -  
392 - srs_assert(consumer != NULL);  
393 - SrsAutoFree(SrsConsumer, consumer, false);  
394 - srs_verbose("consumer created success.");  
395 -  
396 - rtmp->set_recv_timeout(SRS_PULSE_TIMEOUT_US);  
397 -  
398 - SrsPithyPrint pithy_print(SRS_STAGE_PLAY_USER);  
399 -  
400 - while (true) {  
401 - pithy_print.elapse(SRS_PULSE_TIMEOUT_US / 1000);  
402 -  
403 - // switch to other st-threads.  
404 - st_usleep(0);  
405 -  
406 - // read from client.  
407 - int ctl_msg_ret = ERROR_SUCCESS;  
408 - if (true) {  
409 - SrsCommonMessage* msg = NULL;  
410 - ctl_msg_ret = ret = rtmp->recv_message(&msg);  
411 -  
412 - srs_verbose("play loop recv message. ret=%d", ret);  
413 - if (ret != ERROR_SUCCESS && ret != ERROR_SOCKET_TIMEOUT) {  
414 - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) {  
415 - srs_error("recv client control message failed. ret=%d", ret);  
416 - }  
417 - return ret;  
418 - }  
419 - if ((ret = process_play_control_msg(consumer, msg)) != ERROR_SUCCESS) {  
420 - if (!srs_is_system_control_error(ret)) {  
421 - srs_error("process play control message failed. ret=%d", ret);  
422 - }  
423 - return ret;  
424 - }  
425 - }  
426 -  
427 - // get messages from consumer.  
428 - SrsSharedPtrMessage** msgs = NULL;  
429 - int count = 0;  
430 - if ((ret = consumer->get_packets(0, msgs, count)) != ERROR_SUCCESS) {  
431 - srs_error("get messages from consumer failed. ret=%d", ret);  
432 - return ret;  
433 - }  
434 -  
435 - // reportable  
436 - if (pithy_print.can_print()) {  
437 - srs_trace("-> time=%"PRId64", cmr=%d, msgs=%d, obytes=%"PRId64", ibytes=%"PRId64", okbps=%d, ikbps=%d",  
438 - pithy_print.get_age(), ctl_msg_ret, count, rtmp->get_send_bytes(), rtmp->get_recv_bytes(), rtmp->get_send_kbps(), rtmp->get_recv_kbps());  
439 - }  
440 -  
441 - if (count <= 0) {  
442 - srs_verbose("no packets in queue.");  
443 - continue;  
444 - }  
445 - SrsAutoFree(SrsSharedPtrMessage*, msgs, true);  
446 -  
447 - // sendout messages  
448 - for (int i = 0; i < count; i++) {  
449 - SrsSharedPtrMessage* msg = msgs[i];  
450 -  
451 - // the send_message will free the msg,  
452 - // so set the msgs[i] to NULL.  
453 - msgs[i] = NULL;  
454 -  
455 - if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {  
456 - srs_error("send message to client failed. ret=%d", ret);  
457 - return ret;  
458 - }  
459 - }  
460 - }  
461 -  
462 - return ret; 378 + int ret = ERROR_SUCCESS;
  379 +
  380 + if ((ret = refer->check(req->pageUrl, _srs_config->get_refer_play(req->vhost))) != ERROR_SUCCESS) {
  381 + srs_error("check play_refer failed. ret=%d", ret);
  382 + return ret;
  383 + }
  384 + srs_verbose("check play_refer success.");
  385 +
  386 + SrsConsumer* consumer = NULL;
  387 + if ((ret = source->create_consumer(consumer)) != ERROR_SUCCESS) {
  388 + srs_error("create consumer failed. ret=%d", ret);
  389 + return ret;
  390 + }
  391 +
  392 + srs_assert(consumer != NULL);
  393 + SrsAutoFree(SrsConsumer, consumer, false);
  394 + srs_verbose("consumer created success.");
  395 +
  396 + rtmp->set_recv_timeout(SRS_PULSE_TIMEOUT_US);
  397 +
  398 + SrsPithyPrint pithy_print(SRS_STAGE_PLAY_USER);
  399 +
  400 + while (true) {
  401 + pithy_print.elapse(SRS_PULSE_TIMEOUT_US / 1000);
  402 +
  403 + // switch to other st-threads.
  404 + st_usleep(0);
  405 +
  406 + // read from client.
  407 + int ctl_msg_ret = ERROR_SUCCESS;
  408 + if (true) {
  409 + SrsCommonMessage* msg = NULL;
  410 + ctl_msg_ret = ret = rtmp->recv_message(&msg);
  411 +
  412 + srs_verbose("play loop recv message. ret=%d", ret);
  413 + if (ret != ERROR_SUCCESS && ret != ERROR_SOCKET_TIMEOUT) {
  414 + if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) {
  415 + srs_error("recv client control message failed. ret=%d", ret);
  416 + }
  417 + return ret;
  418 + }
  419 + if ((ret = process_play_control_msg(consumer, msg)) != ERROR_SUCCESS) {
  420 + if (!srs_is_system_control_error(ret)) {
  421 + srs_error("process play control message failed. ret=%d", ret);
  422 + }
  423 + return ret;
  424 + }
  425 + }
  426 +
  427 + // get messages from consumer.
  428 + SrsSharedPtrMessage** msgs = NULL;
  429 + int count = 0;
  430 + if ((ret = consumer->get_packets(0, msgs, count)) != ERROR_SUCCESS) {
  431 + srs_error("get messages from consumer failed. ret=%d", ret);
  432 + return ret;
  433 + }
  434 +
  435 + // reportable
  436 + if (pithy_print.can_print()) {
  437 + srs_trace("-> time=%"PRId64", cmr=%d, msgs=%d, obytes=%"PRId64", ibytes=%"PRId64", okbps=%d, ikbps=%d",
  438 + pithy_print.get_age(), ctl_msg_ret, count, rtmp->get_send_bytes(), rtmp->get_recv_bytes(), rtmp->get_send_kbps(), rtmp->get_recv_kbps());
  439 + }
  440 +
  441 + if (count <= 0) {
  442 + srs_verbose("no packets in queue.");
  443 + continue;
  444 + }
  445 + SrsAutoFree(SrsSharedPtrMessage*, msgs, true);
  446 +
  447 + // sendout messages
  448 + for (int i = 0; i < count; i++) {
  449 + SrsSharedPtrMessage* msg = msgs[i];
  450 +
  451 + // the send_message will free the msg,
  452 + // so set the msgs[i] to NULL.
  453 + msgs[i] = NULL;
  454 +
  455 + if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) {
  456 + srs_error("send message to client failed. ret=%d", ret);
  457 + return ret;
  458 + }
  459 + }
  460 + }
  461 +
  462 + return ret;
463 } 463 }
464 464
465 int SrsClient::fmle_publish(SrsSource* source) 465 int SrsClient::fmle_publish(SrsSource* source)
466 { 466 {
467 - int ret = ERROR_SUCCESS;  
468 -  
469 - if ((ret = refer->check(req->pageUrl, _srs_config->get_refer_publish(req->vhost))) != ERROR_SUCCESS) {  
470 - srs_error("fmle check publish_refer failed. ret=%d", ret);  
471 - return ret;  
472 - }  
473 - srs_verbose("fmle check publish_refer success.");  
474 -  
475 - SrsPithyPrint pithy_print(SRS_STAGE_PUBLISH_USER);  
476 -  
477 - // notify the hls to prepare when publish start.  
478 - if ((ret = source->on_publish(req)) != ERROR_SUCCESS) {  
479 - srs_error("fmle hls on_publish failed. ret=%d", ret);  
480 - return ret;  
481 - }  
482 - srs_verbose("fmle hls on_publish success.");  
483 -  
484 - while (true) {  
485 - // switch to other st-threads.  
486 - st_usleep(0);  
487 -  
488 - SrsCommonMessage* msg = NULL;  
489 - if ((ret = rtmp->recv_message(&msg)) != ERROR_SUCCESS) {  
490 - srs_error("fmle recv identify client message failed. ret=%d", ret);  
491 - return ret;  
492 - }  
493 -  
494 - SrsAutoFree(SrsCommonMessage, msg, false);  
495 -  
496 - pithy_print.set_age(msg->header.timestamp);  
497 -  
498 - // reportable  
499 - if (pithy_print.can_print()) {  
500 - srs_trace("<- time=%"PRId64", obytes=%"PRId64", ibytes=%"PRId64", okbps=%d, ikbps=%d",  
501 - pithy_print.get_age(), rtmp->get_send_bytes(), rtmp->get_recv_bytes(), rtmp->get_send_kbps(), rtmp->get_recv_kbps());  
502 - }  
503 -  
504 - // process UnPublish event.  
505 - if (msg->header.is_amf0_command() || msg->header.is_amf3_command()) {  
506 - if ((ret = msg->decode_packet(rtmp->get_protocol())) != ERROR_SUCCESS) {  
507 - srs_error("fmle decode unpublish message failed. ret=%d", ret);  
508 - return ret;  
509 - }  
510 -  
511 - SrsPacket* pkt = msg->get_packet();  
512 - if (dynamic_cast<SrsFMLEStartPacket*>(pkt)) {  
513 - SrsFMLEStartPacket* unpublish = dynamic_cast<SrsFMLEStartPacket*>(pkt);  
514 - if ((ret = rtmp->fmle_unpublish(res->stream_id, unpublish->transaction_id)) != ERROR_SUCCESS) {  
515 - return ret;  
516 - }  
517 - return ERROR_CONTROL_REPUBLISH;  
518 - }  
519 -  
520 - srs_trace("fmle ignore AMF0/AMF3 command message.");  
521 - continue;  
522 - }  
523 -  
524 - // video, audio, data message  
525 - if ((ret = process_publish_message(source, msg)) != ERROR_SUCCESS) {  
526 - srs_error("fmle process publish message failed. ret=%d", ret);  
527 - return ret;  
528 - }  
529 - }  
530 -  
531 - return ret; 467 + int ret = ERROR_SUCCESS;
  468 +
  469 + if ((ret = refer->check(req->pageUrl, _srs_config->get_refer_publish(req->vhost))) != ERROR_SUCCESS) {
  470 + srs_error("fmle check publish_refer failed. ret=%d", ret);
  471 + return ret;
  472 + }
  473 + srs_verbose("fmle check publish_refer success.");
  474 +
  475 + SrsPithyPrint pithy_print(SRS_STAGE_PUBLISH_USER);
  476 +
  477 + // notify the hls to prepare when publish start.
  478 + if ((ret = source->on_publish(req)) != ERROR_SUCCESS) {
  479 + srs_error("fmle hls on_publish failed. ret=%d", ret);
  480 + return ret;
  481 + }
  482 + srs_verbose("fmle hls on_publish success.");
  483 +
  484 + while (true) {
  485 + // switch to other st-threads.
  486 + st_usleep(0);
  487 +
  488 + SrsCommonMessage* msg = NULL;
  489 + if ((ret = rtmp->recv_message(&msg)) != ERROR_SUCCESS) {
  490 + srs_error("fmle recv identify client message failed. ret=%d", ret);
  491 + return ret;
  492 + }
  493 +
  494 + SrsAutoFree(SrsCommonMessage, msg, false);
  495 +
  496 + pithy_print.set_age(msg->header.timestamp);
  497 +
  498 + // reportable
  499 + if (pithy_print.can_print()) {
  500 + srs_trace("<- time=%"PRId64", obytes=%"PRId64", ibytes=%"PRId64", okbps=%d, ikbps=%d",
  501 + pithy_print.get_age(), rtmp->get_send_bytes(), rtmp->get_recv_bytes(), rtmp->get_send_kbps(), rtmp->get_recv_kbps());
  502 + }
  503 +
  504 + // process UnPublish event.
  505 + if (msg->header.is_amf0_command() || msg->header.is_amf3_command()) {
  506 + if ((ret = msg->decode_packet(rtmp->get_protocol())) != ERROR_SUCCESS) {
  507 + srs_error("fmle decode unpublish message failed. ret=%d", ret);
  508 + return ret;
  509 + }
  510 +
  511 + SrsPacket* pkt = msg->get_packet();
  512 + if (dynamic_cast<SrsFMLEStartPacket*>(pkt)) {
  513 + SrsFMLEStartPacket* unpublish = dynamic_cast<SrsFMLEStartPacket*>(pkt);
  514 + if ((ret = rtmp->fmle_unpublish(res->stream_id, unpublish->transaction_id)) != ERROR_SUCCESS) {
  515 + return ret;
  516 + }
  517 + return ERROR_CONTROL_REPUBLISH;
  518 + }
  519 +
  520 + srs_trace("fmle ignore AMF0/AMF3 command message.");
  521 + continue;
  522 + }
  523 +
  524 + // video, audio, data message
  525 + if ((ret = process_publish_message(source, msg)) != ERROR_SUCCESS) {
  526 + srs_error("fmle process publish message failed. ret=%d", ret);
  527 + return ret;
  528 + }
  529 + }
  530 +
  531 + return ret;
532 } 532 }
533 533
534 int SrsClient::flash_publish(SrsSource* source) 534 int SrsClient::flash_publish(SrsSource* source)
535 { 535 {
536 - int ret = ERROR_SUCCESS;  
537 -  
538 - if ((ret = refer->check(req->pageUrl, _srs_config->get_refer_publish(req->vhost))) != ERROR_SUCCESS) {  
539 - srs_error("flash check publish_refer failed. ret=%d", ret);  
540 - return ret;  
541 - }  
542 - srs_verbose("flash check publish_refer success.");  
543 -  
544 - SrsPithyPrint pithy_print(SRS_STAGE_PUBLISH_USER);  
545 -  
546 - // notify the hls to prepare when publish start.  
547 - if ((ret = source->on_publish(req)) != ERROR_SUCCESS) {  
548 - srs_error("flash hls on_publish failed. ret=%d", ret);  
549 - return ret;  
550 - }  
551 - srs_verbose("flash hls on_publish success.");  
552 -  
553 - while (true) {  
554 - // switch to other st-threads.  
555 - st_usleep(0);  
556 -  
557 - SrsCommonMessage* msg = NULL;  
558 - if ((ret = rtmp->recv_message(&msg)) != ERROR_SUCCESS) {  
559 - srs_error("flash recv identify client message failed. ret=%d", ret);  
560 - return ret;  
561 - }  
562 -  
563 - SrsAutoFree(SrsCommonMessage, msg, false);  
564 -  
565 - pithy_print.set_age(msg->header.timestamp);  
566 -  
567 - // reportable  
568 - if (pithy_print.can_print()) {  
569 - srs_trace("<- time=%"PRId64", obytes=%"PRId64", ibytes=%"PRId64", okbps=%d, ikbps=%d",  
570 - pithy_print.get_age(), rtmp->get_send_bytes(), rtmp->get_recv_bytes(), rtmp->get_send_kbps(), rtmp->get_recv_kbps());  
571 - }  
572 -  
573 - // process UnPublish event.  
574 - if (msg->header.is_amf0_command() || msg->header.is_amf3_command()) {  
575 - if ((ret = msg->decode_packet(rtmp->get_protocol())) != ERROR_SUCCESS) {  
576 - srs_error("flash decode unpublish message failed. ret=%d", ret);  
577 - return ret;  
578 - }  
579 -  
580 - // flash unpublish.  
581 - // TODO: maybe need to support republish.  
582 - srs_trace("flash flash publish finished.");  
583 - return ERROR_CONTROL_REPUBLISH;  
584 - }  
585 -  
586 - // video, audio, data message  
587 - if ((ret = process_publish_message(source, msg)) != ERROR_SUCCESS) {  
588 - srs_error("flash process publish message failed. ret=%d", ret);  
589 - return ret;  
590 - }  
591 - }  
592 -  
593 - return ret; 536 + int ret = ERROR_SUCCESS;
  537 +
  538 + if ((ret = refer->check(req->pageUrl, _srs_config->get_refer_publish(req->vhost))) != ERROR_SUCCESS) {
  539 + srs_error("flash check publish_refer failed. ret=%d", ret);
  540 + return ret;
  541 + }
  542 + srs_verbose("flash check publish_refer success.");
  543 +
  544 + SrsPithyPrint pithy_print(SRS_STAGE_PUBLISH_USER);
  545 +
  546 + // notify the hls to prepare when publish start.
  547 + if ((ret = source->on_publish(req)) != ERROR_SUCCESS) {
  548 + srs_error("flash hls on_publish failed. ret=%d", ret);
  549 + return ret;
  550 + }
  551 + srs_verbose("flash hls on_publish success.");
  552 +
  553 + while (true) {
  554 + // switch to other st-threads.
  555 + st_usleep(0);
  556 +
  557 + SrsCommonMessage* msg = NULL;
  558 + if ((ret = rtmp->recv_message(&msg)) != ERROR_SUCCESS) {
  559 + srs_error("flash recv identify client message failed. ret=%d", ret);
  560 + return ret;
  561 + }
  562 +
  563 + SrsAutoFree(SrsCommonMessage, msg, false);
  564 +
  565 + pithy_print.set_age(msg->header.timestamp);
  566 +
  567 + // reportable
  568 + if (pithy_print.can_print()) {
  569 + srs_trace("<- time=%"PRId64", obytes=%"PRId64", ibytes=%"PRId64", okbps=%d, ikbps=%d",
  570 + pithy_print.get_age(), rtmp->get_send_bytes(), rtmp->get_recv_bytes(), rtmp->get_send_kbps(), rtmp->get_recv_kbps());
  571 + }
  572 +
  573 + // process UnPublish event.
  574 + if (msg->header.is_amf0_command() || msg->header.is_amf3_command()) {
  575 + if ((ret = msg->decode_packet(rtmp->get_protocol())) != ERROR_SUCCESS) {
  576 + srs_error("flash decode unpublish message failed. ret=%d", ret);
  577 + return ret;
  578 + }
  579 +
  580 + // flash unpublish.
  581 + // TODO: maybe need to support republish.
  582 + srs_trace("flash flash publish finished.");
  583 + return ERROR_CONTROL_REPUBLISH;
  584 + }
  585 +
  586 + // video, audio, data message
  587 + if ((ret = process_publish_message(source, msg)) != ERROR_SUCCESS) {
  588 + srs_error("flash process publish message failed. ret=%d", ret);
  589 + return ret;
  590 + }
  591 + }
  592 +
  593 + return ret;
594 } 594 }
595 595
596 int SrsClient::process_publish_message(SrsSource* source, SrsCommonMessage* msg) 596 int SrsClient::process_publish_message(SrsSource* source, SrsCommonMessage* msg)
597 { 597 {
598 - int ret = ERROR_SUCCESS;  
599 -  
600 - // process audio packet  
601 - if (msg->header.is_audio()) {  
602 - if ((ret = source->on_audio(msg)) != ERROR_SUCCESS) {  
603 - srs_error("source process audio message failed. ret=%d", ret);  
604 - return ret;  
605 - }  
606 - }  
607 - // process video packet  
608 - if (msg->header.is_video()) {  
609 - if ((ret = source->on_video(msg)) != ERROR_SUCCESS) {  
610 - srs_error("source process video message failed. ret=%d", ret);  
611 - return ret;  
612 - }  
613 - }  
614 -  
615 - // process onMetaData  
616 - if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) {  
617 - if ((ret = msg->decode_packet(rtmp->get_protocol())) != ERROR_SUCCESS) {  
618 - srs_error("decode onMetaData message failed. ret=%d", ret);  
619 - return ret;  
620 - }  
621 -  
622 - SrsPacket* pkt = msg->get_packet();  
623 - if (dynamic_cast<SrsOnMetaDataPacket*>(pkt)) {  
624 - SrsOnMetaDataPacket* metadata = dynamic_cast<SrsOnMetaDataPacket*>(pkt);  
625 - if ((ret = source->on_meta_data(msg, metadata)) != ERROR_SUCCESS) {  
626 - srs_error("source process onMetaData message failed. ret=%d", ret);  
627 - return ret;  
628 - }  
629 - srs_trace("process onMetaData message success.");  
630 - return ret;  
631 - }  
632 -  
633 - srs_trace("ignore AMF0/AMF3 data message.");  
634 - return ret;  
635 - }  
636 -  
637 - return ret; 598 + int ret = ERROR_SUCCESS;
  599 +
  600 + // process audio packet
  601 + if (msg->header.is_audio()) {
  602 + if ((ret = source->on_audio(msg)) != ERROR_SUCCESS) {
  603 + srs_error("source process audio message failed. ret=%d", ret);
  604 + return ret;
  605 + }
  606 + }
  607 + // process video packet
  608 + if (msg->header.is_video()) {
  609 + if ((ret = source->on_video(msg)) != ERROR_SUCCESS) {
  610 + srs_error("source process video message failed. ret=%d", ret);
  611 + return ret;
  612 + }
  613 + }
  614 +
  615 + // process onMetaData
  616 + if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) {
  617 + if ((ret = msg->decode_packet(rtmp->get_protocol())) != ERROR_SUCCESS) {
  618 + srs_error("decode onMetaData message failed. ret=%d", ret);
  619 + return ret;
  620 + }
  621 +
  622 + SrsPacket* pkt = msg->get_packet();
  623 + if (dynamic_cast<SrsOnMetaDataPacket*>(pkt)) {
  624 + SrsOnMetaDataPacket* metadata = dynamic_cast<SrsOnMetaDataPacket*>(pkt);
  625 + if ((ret = source->on_meta_data(msg, metadata)) != ERROR_SUCCESS) {
  626 + srs_error("source process onMetaData message failed. ret=%d", ret);
  627 + return ret;
  628 + }
  629 + srs_trace("process onMetaData message success.");
  630 + return ret;
  631 + }
  632 +
  633 + srs_trace("ignore AMF0/AMF3 data message.");
  634 + return ret;
  635 + }
  636 +
  637 + return ret;
638 } 638 }
639 639
640 int SrsClient::get_peer_ip() 640 int SrsClient::get_peer_ip()
641 { 641 {
642 - int ret = ERROR_SUCCESS;  
643 - 642 + int ret = ERROR_SUCCESS;
  643 +
644 int fd = st_netfd_fileno(stfd); 644 int fd = st_netfd_fileno(stfd);
645 645
646 // discovery client information 646 // discovery client information
@@ -674,71 +674,71 @@ int SrsClient::get_peer_ip() @@ -674,71 +674,71 @@ int SrsClient::get_peer_ip()
674 674
675 int SrsClient::process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg) 675 int SrsClient::process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg)
676 { 676 {
677 - int ret = ERROR_SUCCESS;  
678 -  
679 - if (!msg) {  
680 - srs_verbose("ignore all empty message.");  
681 - return ret;  
682 - }  
683 - SrsAutoFree(SrsCommonMessage, msg, false);  
684 -  
685 - if (!msg->header.is_amf0_command() && !msg->header.is_amf3_command()) {  
686 - srs_info("ignore all message except amf0/amf3 command.");  
687 - return ret;  
688 - }  
689 -  
690 - if ((ret = msg->decode_packet(rtmp->get_protocol())) != ERROR_SUCCESS) {  
691 - srs_error("decode the amf0/amf3 command packet failed. ret=%d", ret);  
692 - return ret;  
693 - }  
694 - srs_info("decode the amf0/amf3 command packet success.");  
695 -  
696 - SrsCloseStreamPacket* close = dynamic_cast<SrsCloseStreamPacket*>(msg->get_packet());  
697 - if (close) {  
698 - ret = ERROR_CONTROL_RTMP_CLOSE;  
699 - srs_trace("system control message: rtmp close stream. ret=%d", ret);  
700 - return ret;  
701 - }  
702 -  
703 - SrsPausePacket* pause = dynamic_cast<SrsPausePacket*>(msg->get_packet());  
704 - if (!pause) {  
705 - srs_info("ignore all amf0/amf3 command except pause.");  
706 - return ret;  
707 - }  
708 -  
709 - if ((ret = rtmp->on_play_client_pause(res->stream_id, pause->is_pause)) != ERROR_SUCCESS) {  
710 - srs_error("rtmp process play client pause failed. ret=%d", ret);  
711 - return ret;  
712 - }  
713 -  
714 - if ((ret = consumer->on_play_client_pause(pause->is_pause)) != ERROR_SUCCESS) {  
715 - srs_error("consumer process play client pause failed. ret=%d", ret);  
716 - return ret;  
717 - }  
718 - srs_info("process pause success, is_pause=%d, time=%d.", pause->is_pause, pause->time_ms);  
719 - 677 + int ret = ERROR_SUCCESS;
  678 +
  679 + if (!msg) {
  680 + srs_verbose("ignore all empty message.");
  681 + return ret;
  682 + }
  683 + SrsAutoFree(SrsCommonMessage, msg, false);
  684 +
  685 + if (!msg->header.is_amf0_command() && !msg->header.is_amf3_command()) {
  686 + srs_info("ignore all message except amf0/amf3 command.");
  687 + return ret;
  688 + }
  689 +
  690 + if ((ret = msg->decode_packet(rtmp->get_protocol())) != ERROR_SUCCESS) {
  691 + srs_error("decode the amf0/amf3 command packet failed. ret=%d", ret);
  692 + return ret;
  693 + }
  694 + srs_info("decode the amf0/amf3 command packet success.");
  695 +
  696 + SrsCloseStreamPacket* close = dynamic_cast<SrsCloseStreamPacket*>(msg->get_packet());
  697 + if (close) {
  698 + ret = ERROR_CONTROL_RTMP_CLOSE;
  699 + srs_trace("system control message: rtmp close stream. ret=%d", ret);
  700 + return ret;
  701 + }
  702 +
  703 + SrsPausePacket* pause = dynamic_cast<SrsPausePacket*>(msg->get_packet());
  704 + if (!pause) {
  705 + srs_info("ignore all amf0/amf3 command except pause.");
  706 + return ret;
  707 + }
  708 +
  709 + if ((ret = rtmp->on_play_client_pause(res->stream_id, pause->is_pause)) != ERROR_SUCCESS) {
  710 + srs_error("rtmp process play client pause failed. ret=%d", ret);
  711 + return ret;
  712 + }
  713 +
  714 + if ((ret = consumer->on_play_client_pause(pause->is_pause)) != ERROR_SUCCESS) {
  715 + srs_error("consumer process play client pause failed. ret=%d", ret);
  716 + return ret;
  717 + }
  718 + srs_info("process pause success, is_pause=%d, time=%d.", pause->is_pause, pause->time_ms);
  719 +
720 return ret; 720 return ret;
721 } 721 }
722 722
723 int SrsClient::on_connect() 723 int SrsClient::on_connect()
724 { 724 {
725 - int ret = ERROR_SUCCESS;  
726 -  
727 -#ifdef SRS_HTTP_CALLBACK  
728 - // HTTP: on_connect  
729 - SrsConfDirective* on_connect = _srs_config->get_vhost_on_connect(req->vhost);  
730 - if (!on_connect) {  
731 - srs_info("ignore the empty http callback: on_connect");  
732 - return ret;  
733 - }  
734 -  
735 - for (int i = 0; i < (int)on_connect->args.size(); i++) {  
736 - std::string url = on_connect->args.at(i);  
737 - if ((ret = http_hooks->on_connect(url, connection_id, ip, req)) != ERROR_SUCCESS) {  
738 - srs_error("hook client on_connect failed. url=%s, ret=%d", url.c_str(), ret);  
739 - return ret;  
740 - }  
741 - } 725 + int ret = ERROR_SUCCESS;
  726 +
  727 +#ifdef SRS_HTTP_CALLBACK
  728 + // HTTP: on_connect
  729 + SrsConfDirective* on_connect = _srs_config->get_vhost_on_connect(req->vhost);
  730 + if (!on_connect) {
  731 + srs_info("ignore the empty http callback: on_connect");
  732 + return ret;
  733 + }
  734 +
  735 + for (int i = 0; i < (int)on_connect->args.size(); i++) {
  736 + std::string url = on_connect->args.at(i);
  737 + if ((ret = http_hooks->on_connect(url, connection_id, ip, req)) != ERROR_SUCCESS) {
  738 + srs_error("hook client on_connect failed. url=%s, ret=%d", url.c_str(), ret);
  739 + return ret;
  740 + }
  741 + }
742 #endif 742 #endif
743 743
744 return ret; 744 return ret;
@@ -747,40 +747,40 @@ int SrsClient::on_connect() @@ -747,40 +747,40 @@ int SrsClient::on_connect()
747 void SrsClient::on_close() 747 void SrsClient::on_close()
748 { 748 {
749 #ifdef SRS_HTTP_CALLBACK 749 #ifdef SRS_HTTP_CALLBACK
750 - // whatever the ret code, notify the api hooks.  
751 - // HTTP: on_close  
752 - SrsConfDirective* on_close = _srs_config->get_vhost_on_close(req->vhost);  
753 - if (!on_close) {  
754 - srs_info("ignore the empty http callback: on_close");  
755 - return;  
756 - }  
757 -  
758 - for (int i = 0; i < (int)on_close->args.size(); i++) {  
759 - std::string url = on_close->args.at(i);  
760 - http_hooks->on_close(url, connection_id, ip, req);  
761 - } 750 + // whatever the ret code, notify the api hooks.
  751 + // HTTP: on_close
  752 + SrsConfDirective* on_close = _srs_config->get_vhost_on_close(req->vhost);
  753 + if (!on_close) {
  754 + srs_info("ignore the empty http callback: on_close");
  755 + return;
  756 + }
  757 +
  758 + for (int i = 0; i < (int)on_close->args.size(); i++) {
  759 + std::string url = on_close->args.at(i);
  760 + http_hooks->on_close(url, connection_id, ip, req);
  761 + }
762 #endif 762 #endif
763 } 763 }
764 764
765 int SrsClient::on_publish() 765 int SrsClient::on_publish()
766 { 766 {
767 - int ret = ERROR_SUCCESS;  
768 -  
769 -#ifdef SRS_HTTP_CALLBACK  
770 - // HTTP: on_publish  
771 - SrsConfDirective* on_publish = _srs_config->get_vhost_on_publish(req->vhost);  
772 - if (!on_publish) {  
773 - srs_info("ignore the empty http callback: on_publish");  
774 - return ret;  
775 - }  
776 -  
777 - for (int i = 0; i < (int)on_publish->args.size(); i++) {  
778 - std::string url = on_publish->args.at(i);  
779 - if ((ret = http_hooks->on_publish(url, connection_id, ip, req)) != ERROR_SUCCESS) {  
780 - srs_error("hook client on_publish failed. url=%s, ret=%d", url.c_str(), ret);  
781 - return ret;  
782 - }  
783 - } 767 + int ret = ERROR_SUCCESS;
  768 +
  769 +#ifdef SRS_HTTP_CALLBACK
  770 + // HTTP: on_publish
  771 + SrsConfDirective* on_publish = _srs_config->get_vhost_on_publish(req->vhost);
  772 + if (!on_publish) {
  773 + srs_info("ignore the empty http callback: on_publish");
  774 + return ret;
  775 + }
  776 +
  777 + for (int i = 0; i < (int)on_publish->args.size(); i++) {
  778 + std::string url = on_publish->args.at(i);
  779 + if ((ret = http_hooks->on_publish(url, connection_id, ip, req)) != ERROR_SUCCESS) {
  780 + srs_error("hook client on_publish failed. url=%s, ret=%d", url.c_str(), ret);
  781 + return ret;
  782 + }
  783 + }
784 #endif 784 #endif
785 785
786 return ret; 786 return ret;
@@ -789,40 +789,40 @@ int SrsClient::on_publish() @@ -789,40 +789,40 @@ int SrsClient::on_publish()
789 void SrsClient::on_unpublish() 789 void SrsClient::on_unpublish()
790 { 790 {
791 #ifdef SRS_HTTP_CALLBACK 791 #ifdef SRS_HTTP_CALLBACK
792 - // whatever the ret code, notify the api hooks.  
793 - // HTTP: on_unpublish  
794 - SrsConfDirective* on_unpublish = _srs_config->get_vhost_on_unpublish(req->vhost);  
795 - if (!on_unpublish) {  
796 - srs_info("ignore the empty http callback: on_unpublish");  
797 - return;  
798 - }  
799 -  
800 - for (int i = 0; i < (int)on_unpublish->args.size(); i++) {  
801 - std::string url = on_unpublish->args.at(i);  
802 - http_hooks->on_unpublish(url, connection_id, ip, req);  
803 - } 792 + // whatever the ret code, notify the api hooks.
  793 + // HTTP: on_unpublish
  794 + SrsConfDirective* on_unpublish = _srs_config->get_vhost_on_unpublish(req->vhost);
  795 + if (!on_unpublish) {
  796 + srs_info("ignore the empty http callback: on_unpublish");
  797 + return;
  798 + }
  799 +
  800 + for (int i = 0; i < (int)on_unpublish->args.size(); i++) {
  801 + std::string url = on_unpublish->args.at(i);
  802 + http_hooks->on_unpublish(url, connection_id, ip, req);
  803 + }
804 #endif 804 #endif
805 } 805 }
806 806
807 int SrsClient::on_play() 807 int SrsClient::on_play()
808 { 808 {
809 - int ret = ERROR_SUCCESS;  
810 -  
811 -#ifdef SRS_HTTP_CALLBACK  
812 - // HTTP: on_play  
813 - SrsConfDirective* on_play = _srs_config->get_vhost_on_play(req->vhost);  
814 - if (!on_play) {  
815 - srs_info("ignore the empty http callback: on_play");  
816 - return ret;  
817 - }  
818 -  
819 - for (int i = 0; i < (int)on_play->args.size(); i++) {  
820 - std::string url = on_play->args.at(i);  
821 - if ((ret = http_hooks->on_play(url, connection_id, ip, req)) != ERROR_SUCCESS) {  
822 - srs_error("hook client on_play failed. url=%s, ret=%d", url.c_str(), ret);  
823 - return ret;  
824 - }  
825 - } 809 + int ret = ERROR_SUCCESS;
  810 +
  811 +#ifdef SRS_HTTP_CALLBACK
  812 + // HTTP: on_play
  813 + SrsConfDirective* on_play = _srs_config->get_vhost_on_play(req->vhost);
  814 + if (!on_play) {
  815 + srs_info("ignore the empty http callback: on_play");
  816 + return ret;
  817 + }
  818 +
  819 + for (int i = 0; i < (int)on_play->args.size(); i++) {
  820 + std::string url = on_play->args.at(i);
  821 + if ((ret = http_hooks->on_play(url, connection_id, ip, req)) != ERROR_SUCCESS) {
  822 + srs_error("hook client on_play failed. url=%s, ret=%d", url.c_str(), ret);
  823 + return ret;
  824 + }
  825 + }
826 #endif 826 #endif
827 827
828 return ret; 828 return ret;
@@ -831,17 +831,17 @@ int SrsClient::on_play() @@ -831,17 +831,17 @@ int SrsClient::on_play()
831 void SrsClient::on_stop() 831 void SrsClient::on_stop()
832 { 832 {
833 #ifdef SRS_HTTP_CALLBACK 833 #ifdef SRS_HTTP_CALLBACK
834 - // whatever the ret code, notify the api hooks.  
835 - // HTTP: on_stop  
836 - SrsConfDirective* on_stop = _srs_config->get_vhost_on_stop(req->vhost);  
837 - if (!on_stop) {  
838 - srs_info("ignore the empty http callback: on_stop");  
839 - return;  
840 - }  
841 -  
842 - for (int i = 0; i < (int)on_stop->args.size(); i++) {  
843 - std::string url = on_stop->args.at(i);  
844 - http_hooks->on_stop(url, connection_id, ip, req);  
845 - } 834 + // whatever the ret code, notify the api hooks.
  835 + // HTTP: on_stop
  836 + SrsConfDirective* on_stop = _srs_config->get_vhost_on_stop(req->vhost);
  837 + if (!on_stop) {
  838 + srs_info("ignore the empty http callback: on_stop");
  839 + return;
  840 + }
  841 +
  842 + for (int i = 0; i < (int)on_stop->args.size(); i++) {
  843 + std::string url = on_stop->args.at(i);
  844 + http_hooks->on_stop(url, connection_id, ip, req);
  845 + }
846 #endif 846 #endif
847 } 847 }
@@ -42,7 +42,7 @@ class SrsRefer; @@ -42,7 +42,7 @@ class SrsRefer;
42 class SrsConsumer; 42 class SrsConsumer;
43 class SrsCommonMessage; 43 class SrsCommonMessage;
44 class SrsSocket; 44 class SrsSocket;
45 -#ifdef SRS_HTTP_CALLBACK 45 +#ifdef SRS_HTTP_CALLBACK
46 class SrsHttpHooks; 46 class SrsHttpHooks;
47 #endif 47 #endif
48 class SrsBandwidth; 48 class SrsBandwidth;
@@ -53,43 +53,43 @@ class SrsBandwidth; @@ -53,43 +53,43 @@ class SrsBandwidth;
53 class SrsClient : public SrsConnection, public ISrsReloadHandler 53 class SrsClient : public SrsConnection, public ISrsReloadHandler
54 { 54 {
55 private: 55 private:
56 - char* ip;  
57 - SrsRequest* req;  
58 - SrsResponse* res;  
59 - SrsSocket* skt;  
60 - SrsRtmpServer* rtmp;  
61 - SrsRefer* refer;  
62 -#ifdef SRS_HTTP_CALLBACK  
63 - SrsHttpHooks* http_hooks; 56 + char* ip;
  57 + SrsRequest* req;
  58 + SrsResponse* res;
  59 + SrsSocket* skt;
  60 + SrsRtmpServer* rtmp;
  61 + SrsRefer* refer;
  62 +#ifdef SRS_HTTP_CALLBACK
  63 + SrsHttpHooks* http_hooks;
64 #endif 64 #endif
65 - SrsBandwidth* bandwidth; 65 + SrsBandwidth* bandwidth;
66 public: 66 public:
67 - SrsClient(SrsServer* srs_server, st_netfd_t client_stfd);  
68 - virtual ~SrsClient(); 67 + SrsClient(SrsServer* srs_server, st_netfd_t client_stfd);
  68 + virtual ~SrsClient();
69 protected: 69 protected:
70 - virtual int do_cycle(); 70 + virtual int do_cycle();
71 // interface ISrsReloadHandler 71 // interface ISrsReloadHandler
72 public: 72 public:
73 - virtual int on_reload_vhost_removed(std::string vhost); 73 + virtual int on_reload_vhost_removed(std::string vhost);
74 private: 74 private:
75 - // when valid and connected to vhost/app, service the client.  
76 - virtual int service_cycle();  
77 - // stream(play/publish) service cycle, identify client first.  
78 - virtual int stream_service_cycle();  
79 - virtual int check_vhost();  
80 - virtual int playing(SrsSource* source);  
81 - virtual int fmle_publish(SrsSource* source);  
82 - virtual int flash_publish(SrsSource* source);  
83 - virtual int process_publish_message(SrsSource* source, SrsCommonMessage* msg);  
84 - virtual int get_peer_ip();  
85 - virtual int process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg); 75 + // when valid and connected to vhost/app, service the client.
  76 + virtual int service_cycle();
  77 + // stream(play/publish) service cycle, identify client first.
  78 + virtual int stream_service_cycle();
  79 + virtual int check_vhost();
  80 + virtual int playing(SrsSource* source);
  81 + virtual int fmle_publish(SrsSource* source);
  82 + virtual int flash_publish(SrsSource* source);
  83 + virtual int process_publish_message(SrsSource* source, SrsCommonMessage* msg);
  84 + virtual int get_peer_ip();
  85 + virtual int process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg);
86 private: 86 private:
87 - virtual int on_connect();  
88 - virtual void on_close();  
89 - virtual int on_publish();  
90 - virtual void on_unpublish();  
91 - virtual int on_play();  
92 - virtual void on_stop(); 87 + virtual int on_connect();
  88 + virtual void on_close();
  89 + virtual int on_publish();
  90 + virtual void on_unpublish();
  91 + virtual int on_play();
  92 + virtual void on_stop();
93 }; 93 };
94 94
95 #endif 95 #endif
@@ -33,29 +33,29 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -33,29 +33,29 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 33
34 SrsCodecBuffer::SrsCodecBuffer() 34 SrsCodecBuffer::SrsCodecBuffer()
35 { 35 {
36 - size = 0;  
37 - bytes = NULL; 36 + size = 0;
  37 + bytes = NULL;
38 } 38 }
39 39
40 void SrsCodecBuffer::append(void* data, int len) 40 void SrsCodecBuffer::append(void* data, int len)
41 { 41 {
42 - srs_assert(data);  
43 - srs_assert(len > 0); 42 + srs_assert(data);
  43 + srs_assert(len > 0);
44 44
45 - bytes = (char*)realloc(bytes, size + len);  
46 - memcpy(bytes + size, data, len);  
47 - size += len; 45 + bytes = (char*)realloc(bytes, size + len);
  46 + memcpy(bytes + size, data, len);
  47 + size += len;
48 } 48 }
49 49
50 void SrsCodecBuffer::free() 50 void SrsCodecBuffer::free()
51 { 51 {
52 - size = 0;  
53 - srs_freepa(bytes); 52 + size = 0;
  53 + srs_freepa(bytes);
54 } 54 }
55 55
56 SrsCodecSample::SrsCodecSample() 56 SrsCodecSample::SrsCodecSample()
57 { 57 {
58 - clear(); 58 + clear();
59 } 59 }
60 60
61 SrsCodecSample::~SrsCodecSample() 61 SrsCodecSample::~SrsCodecSample()
@@ -64,468 +64,468 @@ SrsCodecSample::~SrsCodecSample() @@ -64,468 +64,468 @@ SrsCodecSample::~SrsCodecSample()
64 64
65 void SrsCodecSample::clear() 65 void SrsCodecSample::clear()
66 { 66 {
67 - is_video = false;  
68 - nb_buffers = 0;  
69 -  
70 - cts = 0;  
71 - frame_type = SrsCodecVideoAVCFrameReserved;  
72 - avc_packet_type = SrsCodecVideoAVCTypeReserved;  
73 -  
74 - sound_rate = SrsCodecAudioSampleRateReserved;  
75 - sound_size = SrsCodecAudioSampleSizeReserved;  
76 - sound_type = SrsCodecAudioSoundTypeReserved;  
77 - aac_packet_type = SrsCodecAudioTypeReserved; 67 + is_video = false;
  68 + nb_buffers = 0;
  69 +
  70 + cts = 0;
  71 + frame_type = SrsCodecVideoAVCFrameReserved;
  72 + avc_packet_type = SrsCodecVideoAVCTypeReserved;
  73 +
  74 + sound_rate = SrsCodecAudioSampleRateReserved;
  75 + sound_size = SrsCodecAudioSampleSizeReserved;
  76 + sound_type = SrsCodecAudioSoundTypeReserved;
  77 + aac_packet_type = SrsCodecAudioTypeReserved;
78 } 78 }
79 79
80 int SrsCodecSample::add_sample(char* bytes, int size) 80 int SrsCodecSample::add_sample(char* bytes, int size)
81 { 81 {
82 - int ret = ERROR_SUCCESS;  
83 -  
84 - if (nb_buffers >= SRS_MAX_CODEC_SAMPLE) {  
85 - ret = ERROR_HLS_DECODE_ERROR;  
86 - srs_error("hls decode samples error, "  
87 - "exceed the max count: %d, ret=%d", SRS_MAX_CODEC_SAMPLE, ret);  
88 - return ret;  
89 - }  
90 -  
91 - SrsCodecBuffer* buf = &buffers[nb_buffers++];  
92 - buf->bytes = bytes;  
93 - buf->size = size;  
94 -  
95 - return ret; 82 + int ret = ERROR_SUCCESS;
  83 +
  84 + if (nb_buffers >= SRS_MAX_CODEC_SAMPLE) {
  85 + ret = ERROR_HLS_DECODE_ERROR;
  86 + srs_error("hls decode samples error, "
  87 + "exceed the max count: %d, ret=%d", SRS_MAX_CODEC_SAMPLE, ret);
  88 + return ret;
  89 + }
  90 +
  91 + SrsCodecBuffer* buf = &buffers[nb_buffers++];
  92 + buf->bytes = bytes;
  93 + buf->size = size;
  94 +
  95 + return ret;
96 } 96 }
97 97
98 SrsCodec::SrsCodec() 98 SrsCodec::SrsCodec()
99 { 99 {
100 - width = 0;  
101 - height = 0;  
102 - duration = 0;  
103 - NAL_unit_length = 0;  
104 - frame_rate = 0;  
105 - video_data_rate = 0;  
106 - video_codec_id = 0;  
107 - audio_data_rate = 0;  
108 - audio_codec_id = 0;  
109 - avc_profile = 0;  
110 - avc_level = 0;  
111 - aac_profile = 0;  
112 - aac_sample_rate = 0;  
113 - aac_channels = 0;  
114 - avc_extra_size = 0;  
115 - avc_extra_data = NULL;  
116 - aac_extra_size = 0;  
117 - aac_extra_data = NULL;  
118 - sequenceParameterSetLength = 0;  
119 - sequenceParameterSetNALUnit = NULL;  
120 - pictureParameterSetLength = 0;  
121 - pictureParameterSetNALUnit = NULL;  
122 -  
123 - stream = new SrsStream(); 100 + width = 0;
  101 + height = 0;
  102 + duration = 0;
  103 + NAL_unit_length = 0;
  104 + frame_rate = 0;
  105 + video_data_rate = 0;
  106 + video_codec_id = 0;
  107 + audio_data_rate = 0;
  108 + audio_codec_id = 0;
  109 + avc_profile = 0;
  110 + avc_level = 0;
  111 + aac_profile = 0;
  112 + aac_sample_rate = 0;
  113 + aac_channels = 0;
  114 + avc_extra_size = 0;
  115 + avc_extra_data = NULL;
  116 + aac_extra_size = 0;
  117 + aac_extra_data = NULL;
  118 + sequenceParameterSetLength = 0;
  119 + sequenceParameterSetNALUnit = NULL;
  120 + pictureParameterSetLength = 0;
  121 + pictureParameterSetNALUnit = NULL;
  122 +
  123 + stream = new SrsStream();
124 } 124 }
125 125
126 SrsCodec::~SrsCodec() 126 SrsCodec::~SrsCodec()
127 { 127 {
128 - srs_freepa(avc_extra_data);  
129 - srs_freepa(aac_extra_data); 128 + srs_freepa(avc_extra_data);
  129 + srs_freepa(aac_extra_data);
130 130
131 - srs_freep(stream);  
132 - srs_freepa(sequenceParameterSetNALUnit);  
133 - srs_freepa(pictureParameterSetNALUnit); 131 + srs_freep(stream);
  132 + srs_freepa(sequenceParameterSetNALUnit);
  133 + srs_freepa(pictureParameterSetNALUnit);
134 } 134 }
135 135
136 int SrsCodec::audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample) 136 int SrsCodec::audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample)
137 { 137 {
138 - int ret = ERROR_SUCCESS;  
139 -  
140 - sample->is_video = false;  
141 -  
142 - if (!data || size <= 0) {  
143 - srs_trace("no audio present, hls ignore it.");  
144 - return ret;  
145 - }  
146 -  
147 - if ((ret = stream->initialize((char*)data, size)) != ERROR_SUCCESS) {  
148 - return ret;  
149 - }  
150 -  
151 - // audio decode  
152 - if (!stream->require(1)) {  
153 - ret = ERROR_HLS_DECODE_ERROR;  
154 - srs_error("hls decode audio sound_format failed. ret=%d", ret);  
155 - return ret;  
156 - }  
157 -  
158 - int8_t sound_format = stream->read_1bytes();  
159 -  
160 - int8_t sound_type = sound_format & 0x01;  
161 - int8_t sound_size = (sound_format >> 1) & 0x01;  
162 - int8_t sound_rate = (sound_format >> 2) & 0x03;  
163 - sound_format = (sound_format >> 4) & 0x0f;  
164 -  
165 - audio_codec_id = sound_format;  
166 - sample->sound_type = (SrsCodecAudioSoundType)sound_type;  
167 - sample->sound_rate = (SrsCodecAudioSampleRate)sound_rate;  
168 - sample->sound_size = (SrsCodecAudioSampleSize)sound_size;  
169 -  
170 - // reset the sample rate by sequence header 138 + int ret = ERROR_SUCCESS;
  139 +
  140 + sample->is_video = false;
  141 +
  142 + if (!data || size <= 0) {
  143 + srs_trace("no audio present, hls ignore it.");
  144 + return ret;
  145 + }
  146 +
  147 + if ((ret = stream->initialize((char*)data, size)) != ERROR_SUCCESS) {
  148 + return ret;
  149 + }
  150 +
  151 + // audio decode
  152 + if (!stream->require(1)) {
  153 + ret = ERROR_HLS_DECODE_ERROR;
  154 + srs_error("hls decode audio sound_format failed. ret=%d", ret);
  155 + return ret;
  156 + }
  157 +
  158 + int8_t sound_format = stream->read_1bytes();
  159 +
  160 + int8_t sound_type = sound_format & 0x01;
  161 + int8_t sound_size = (sound_format >> 1) & 0x01;
  162 + int8_t sound_rate = (sound_format >> 2) & 0x03;
  163 + sound_format = (sound_format >> 4) & 0x0f;
  164 +
  165 + audio_codec_id = sound_format;
  166 + sample->sound_type = (SrsCodecAudioSoundType)sound_type;
  167 + sample->sound_rate = (SrsCodecAudioSampleRate)sound_rate;
  168 + sample->sound_size = (SrsCodecAudioSampleSize)sound_size;
  169 +
  170 + // reset the sample rate by sequence header
171 static int aac_sample_rates[] = { 171 static int aac_sample_rates[] = {
172 - 96000, 88200, 64000, 48000,  
173 - 44100, 32000, 24000, 22050,  
174 - 16000, 12000, 11025, 8000,  
175 - 7350, 0, 0, 0 172 + 96000, 88200, 64000, 48000,
  173 + 44100, 32000, 24000, 22050,
  174 + 16000, 12000, 11025, 8000,
  175 + 7350, 0, 0, 0
176 }; 176 };
177 switch (aac_sample_rates[aac_sample_rate]) { 177 switch (aac_sample_rates[aac_sample_rate]) {
178 case 11025: 178 case 11025:
179 - sample->sound_rate = SrsCodecAudioSampleRate11025;  
180 - break; 179 + sample->sound_rate = SrsCodecAudioSampleRate11025;
  180 + break;
181 case 22050: 181 case 22050:
182 - sample->sound_rate = SrsCodecAudioSampleRate22050;  
183 - break; 182 + sample->sound_rate = SrsCodecAudioSampleRate22050;
  183 + break;
184 case 44100: 184 case 44100:
185 - sample->sound_rate = SrsCodecAudioSampleRate44100;  
186 - break; 185 + sample->sound_rate = SrsCodecAudioSampleRate44100;
  186 + break;
187 }; 187 };
188 188
189 - // only support aac  
190 - if (audio_codec_id != SrsCodecAudioAAC) {  
191 - ret = ERROR_HLS_DECODE_ERROR;  
192 - srs_error("hls only support audio aac codec. ret=%d", ret);  
193 - return ret;  
194 - }  
195 -  
196 - if (!stream->require(1)) {  
197 - ret = ERROR_HLS_DECODE_ERROR;  
198 - srs_error("hls decode audio aac_packet_type failed. ret=%d", ret);  
199 - return ret;  
200 - }  
201 -  
202 - int8_t aac_packet_type = stream->read_1bytes();  
203 - sample->aac_packet_type = (SrsCodecAudioType)aac_packet_type;  
204 -  
205 - if (aac_packet_type == SrsCodecAudioTypeSequenceHeader) {  
206 - // AudioSpecificConfig  
207 - // 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33.  
208 - aac_extra_size = stream->left();  
209 - if (aac_extra_size > 0) {  
210 - srs_freepa(aac_extra_data);  
211 - aac_extra_data = new char[aac_extra_size];  
212 - memcpy(aac_extra_data, stream->current(), aac_extra_size);  
213 - }  
214 -  
215 - // only need to decode the first 2bytes:  
216 - // audioObjectType, aac_profile, 5bits.  
217 - // samplingFrequencyIndex, aac_sample_rate, 4bits.  
218 - // channelConfiguration, aac_channels, 4bits  
219 - if (!stream->require(2)) {  
220 - ret = ERROR_HLS_DECODE_ERROR;  
221 - srs_error("hls decode audio aac sequence header failed. ret=%d", ret);  
222 - return ret;  
223 - }  
224 - aac_profile = stream->read_1bytes();  
225 - aac_sample_rate = stream->read_1bytes();  
226 -  
227 - aac_channels = (aac_sample_rate >> 3) & 0x0f;  
228 - aac_sample_rate = ((aac_profile << 1) & 0x0e) | ((aac_sample_rate >> 7) & 0x01);  
229 - aac_profile = (aac_profile >> 3) & 0x1f;  
230 -  
231 - if (aac_profile == 0 || aac_profile == 0x1f) {  
232 - ret = ERROR_HLS_DECODE_ERROR;  
233 - srs_error("hls decode audio aac sequence header failed, "  
234 - "adts object=%d invalid. ret=%d", aac_profile, ret);  
235 - return ret;  
236 - }  
237 -  
238 - // aac_profile = audioObjectType - 1  
239 - aac_profile--;  
240 -  
241 - if (aac_profile > 3) {  
242 - // Mark all extended profiles as LC  
243 - // to make Android as happy as possible.  
244 - // @see: ngx_rtmp_hls_parse_aac_header  
245 - aac_profile = 1;  
246 - }  
247 - } else if (aac_packet_type == SrsCodecAudioTypeRawData) {  
248 - // ensure the sequence header demuxed  
249 - if (aac_extra_size <= 0 || !aac_extra_data) {  
250 - ret = ERROR_HLS_DECODE_ERROR;  
251 - srs_error("hls decode audio aac failed, sequence header not found. ret=%d", ret);  
252 - return ret;  
253 - }  
254 -  
255 - // Raw AAC frame data in UI8 []  
256 - // 6.3 Raw Data, aac-iso-13818-7.pdf, page 28  
257 - if ((ret = sample->add_sample(stream->current(), stream->left())) != ERROR_SUCCESS) {  
258 - srs_error("hls add audio sample failed. ret=%d", ret);  
259 - return ret;  
260 - }  
261 - } else {  
262 - // ignored.  
263 - }  
264 -  
265 - srs_info("audio decoded, type=%d, codec=%d, asize=%d, rate=%d, format=%d, size=%d",  
266 - sound_type, audio_codec_id, sound_size, sound_rate, sound_format, size);  
267 -  
268 - return ret; 189 + // only support aac
  190 + if (audio_codec_id != SrsCodecAudioAAC) {
  191 + ret = ERROR_HLS_DECODE_ERROR;
  192 + srs_error("hls only support audio aac codec. ret=%d", ret);
  193 + return ret;
  194 + }
  195 +
  196 + if (!stream->require(1)) {
  197 + ret = ERROR_HLS_DECODE_ERROR;
  198 + srs_error("hls decode audio aac_packet_type failed. ret=%d", ret);
  199 + return ret;
  200 + }
  201 +
  202 + int8_t aac_packet_type = stream->read_1bytes();
  203 + sample->aac_packet_type = (SrsCodecAudioType)aac_packet_type;
  204 +
  205 + if (aac_packet_type == SrsCodecAudioTypeSequenceHeader) {
  206 + // AudioSpecificConfig
  207 + // 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33.
  208 + aac_extra_size = stream->left();
  209 + if (aac_extra_size > 0) {
  210 + srs_freepa(aac_extra_data);
  211 + aac_extra_data = new char[aac_extra_size];
  212 + memcpy(aac_extra_data, stream->current(), aac_extra_size);
  213 + }
  214 +
  215 + // only need to decode the first 2bytes:
  216 + // audioObjectType, aac_profile, 5bits.
  217 + // samplingFrequencyIndex, aac_sample_rate, 4bits.
  218 + // channelConfiguration, aac_channels, 4bits
  219 + if (!stream->require(2)) {
  220 + ret = ERROR_HLS_DECODE_ERROR;
  221 + srs_error("hls decode audio aac sequence header failed. ret=%d", ret);
  222 + return ret;
  223 + }
  224 + aac_profile = stream->read_1bytes();
  225 + aac_sample_rate = stream->read_1bytes();
  226 +
  227 + aac_channels = (aac_sample_rate >> 3) & 0x0f;
  228 + aac_sample_rate = ((aac_profile << 1) & 0x0e) | ((aac_sample_rate >> 7) & 0x01);
  229 + aac_profile = (aac_profile >> 3) & 0x1f;
  230 +
  231 + if (aac_profile == 0 || aac_profile == 0x1f) {
  232 + ret = ERROR_HLS_DECODE_ERROR;
  233 + srs_error("hls decode audio aac sequence header failed, "
  234 + "adts object=%d invalid. ret=%d", aac_profile, ret);
  235 + return ret;
  236 + }
  237 +
  238 + // aac_profile = audioObjectType - 1
  239 + aac_profile--;
  240 +
  241 + if (aac_profile > 3) {
  242 + // Mark all extended profiles as LC
  243 + // to make Android as happy as possible.
  244 + // @see: ngx_rtmp_hls_parse_aac_header
  245 + aac_profile = 1;
  246 + }
  247 + } else if (aac_packet_type == SrsCodecAudioTypeRawData) {
  248 + // ensure the sequence header demuxed
  249 + if (aac_extra_size <= 0 || !aac_extra_data) {
  250 + ret = ERROR_HLS_DECODE_ERROR;
  251 + srs_error("hls decode audio aac failed, sequence header not found. ret=%d", ret);
  252 + return ret;
  253 + }
  254 +
  255 + // Raw AAC frame data in UI8 []
  256 + // 6.3 Raw Data, aac-iso-13818-7.pdf, page 28
  257 + if ((ret = sample->add_sample(stream->current(), stream->left())) != ERROR_SUCCESS) {
  258 + srs_error("hls add audio sample failed. ret=%d", ret);
  259 + return ret;
  260 + }
  261 + } else {
  262 + // ignored.
  263 + }
  264 +
  265 + srs_info("audio decoded, type=%d, codec=%d, asize=%d, rate=%d, format=%d, size=%d",
  266 + sound_type, audio_codec_id, sound_size, sound_rate, sound_format, size);
  267 +
  268 + return ret;
269 } 269 }
270 270
271 int SrsCodec::video_avc_demux(int8_t* data, int size, SrsCodecSample* sample) 271 int SrsCodec::video_avc_demux(int8_t* data, int size, SrsCodecSample* sample)
272 { 272 {
273 - int ret = ERROR_SUCCESS;  
274 -  
275 - sample->is_video = true;  
276 -  
277 - if (!data || size <= 0) {  
278 - srs_trace("no video present, hls ignore it.");  
279 - return ret;  
280 - }  
281 -  
282 - if ((ret = stream->initialize((char*)data, size)) != ERROR_SUCCESS) {  
283 - return ret;  
284 - }  
285 -  
286 - // video decode  
287 - if (!stream->require(1)) {  
288 - ret = ERROR_HLS_DECODE_ERROR;  
289 - srs_error("hls decode video frame_type failed. ret=%d", ret);  
290 - return ret;  
291 - }  
292 -  
293 - int8_t frame_type = stream->read_1bytes();  
294 - int8_t codec_id = frame_type & 0x0f;  
295 - frame_type = (frame_type >> 4) & 0x0f;  
296 -  
297 - sample->frame_type = (SrsCodecVideoAVCFrame)frame_type;  
298 -  
299 - // only support h.264/avc  
300 - if (codec_id != SrsCodecVideoAVC) {  
301 - ret = ERROR_HLS_DECODE_ERROR;  
302 - srs_error("hls only support video h.264/avc codec. ret=%d", ret);  
303 - return ret;  
304 - }  
305 - video_codec_id = codec_id;  
306 -  
307 - if (!stream->require(4)) {  
308 - ret = ERROR_HLS_DECODE_ERROR;  
309 - srs_error("hls decode video avc_packet_type failed. ret=%d", ret);  
310 - return ret;  
311 - }  
312 - int8_t avc_packet_type = stream->read_1bytes();  
313 - int32_t composition_time = stream->read_3bytes();  
314 -  
315 - // pts = dts + cts.  
316 - sample->cts = composition_time;  
317 - sample->avc_packet_type = (SrsCodecVideoAVCType)avc_packet_type;  
318 -  
319 - if (avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader) {  
320 - // AVCDecoderConfigurationRecord  
321 - // 5.2.4.1.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16  
322 - avc_extra_size = stream->left();  
323 - if (avc_extra_size > 0) {  
324 - srs_freepa(avc_extra_data);  
325 - avc_extra_data = new char[avc_extra_size];  
326 - memcpy(avc_extra_data, stream->current(), avc_extra_size);  
327 - }  
328 -  
329 - if (!stream->require(6)) {  
330 - ret = ERROR_HLS_DECODE_ERROR;  
331 - srs_error("hls decode video avc sequenc header failed. ret=%d", ret);  
332 - return ret;  
333 - }  
334 - //int8_t configurationVersion = stream->read_1bytes();  
335 - //int8_t AVCProfileIndication = stream->read_1bytes();  
336 - //int8_t profile_compatibility = stream->read_1bytes();  
337 - //int8_t AVCLevelIndication = stream->read_1bytes();  
338 - stream->skip(4);  
339 - // parse the NALU size.  
340 - int8_t lengthSizeMinusOne = stream->read_1bytes();  
341 - lengthSizeMinusOne &= 0x03;  
342 - NAL_unit_length = lengthSizeMinusOne;  
343 -  
344 - // 1 sps  
345 - if (!stream->require(1)) {  
346 - ret = ERROR_HLS_DECODE_ERROR;  
347 - srs_error("hls decode video avc sequenc header sps failed. ret=%d", ret);  
348 - return ret;  
349 - }  
350 - int8_t numOfSequenceParameterSets = stream->read_1bytes();  
351 - numOfSequenceParameterSets &= 0x1f;  
352 - if (numOfSequenceParameterSets != 1) {  
353 - ret = ERROR_HLS_DECODE_ERROR;  
354 - srs_error("hls decode video avc sequenc header sps failed. ret=%d", ret);  
355 - return ret;  
356 - }  
357 - if (!stream->require(2)) {  
358 - ret = ERROR_HLS_DECODE_ERROR;  
359 - srs_error("hls decode video avc sequenc header sps size failed. ret=%d", ret);  
360 - return ret;  
361 - }  
362 - sequenceParameterSetLength = stream->read_2bytes();  
363 - if (!stream->require(sequenceParameterSetLength)) {  
364 - ret = ERROR_HLS_DECODE_ERROR;  
365 - srs_error("hls decode video avc sequenc header sps data failed. ret=%d", ret);  
366 - return ret;  
367 - }  
368 - if (sequenceParameterSetLength > 0) {  
369 - srs_freepa(sequenceParameterSetNALUnit);  
370 - sequenceParameterSetNALUnit = new char[sequenceParameterSetLength];  
371 - memcpy(sequenceParameterSetNALUnit, stream->current(), sequenceParameterSetLength);  
372 - stream->skip(sequenceParameterSetLength);  
373 - }  
374 - // 1 pps  
375 - if (!stream->require(1)) {  
376 - ret = ERROR_HLS_DECODE_ERROR;  
377 - srs_error("hls decode video avc sequenc header pps failed. ret=%d", ret);  
378 - return ret;  
379 - }  
380 - int8_t numOfPictureParameterSets = stream->read_1bytes();  
381 - numOfPictureParameterSets &= 0x1f;  
382 - if (numOfPictureParameterSets != 1) {  
383 - ret = ERROR_HLS_DECODE_ERROR;  
384 - srs_error("hls decode video avc sequenc header pps failed. ret=%d", ret);  
385 - return ret;  
386 - }  
387 - if (!stream->require(2)) {  
388 - ret = ERROR_HLS_DECODE_ERROR;  
389 - srs_error("hls decode video avc sequenc header pps size failed. ret=%d", ret);  
390 - return ret;  
391 - }  
392 - pictureParameterSetLength = stream->read_2bytes();  
393 - if (!stream->require(pictureParameterSetLength)) {  
394 - ret = ERROR_HLS_DECODE_ERROR;  
395 - srs_error("hls decode video avc sequenc header pps data failed. ret=%d", ret);  
396 - return ret;  
397 - }  
398 - if (pictureParameterSetLength > 0) {  
399 - srs_freepa(pictureParameterSetNALUnit);  
400 - pictureParameterSetNALUnit = new char[pictureParameterSetLength];  
401 - memcpy(pictureParameterSetNALUnit, stream->current(), pictureParameterSetLength);  
402 - stream->skip(pictureParameterSetLength);  
403 - }  
404 - } else if (avc_packet_type == SrsCodecVideoAVCTypeNALU){  
405 - // ensure the sequence header demuxed  
406 - if (avc_extra_size <= 0 || !avc_extra_data) {  
407 - ret = ERROR_HLS_DECODE_ERROR;  
408 - srs_error("hls decode video avc failed, sequence header not found. ret=%d", ret);  
409 - return ret;  
410 - }  
411 -  
412 - // One or more NALUs (Full frames are required)  
413 - // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 20  
414 - int PictureLength = stream->left();  
415 - for (int i = 0; i < PictureLength;) {  
416 - if (!stream->require(NAL_unit_length + 1)) {  
417 - ret = ERROR_HLS_DECODE_ERROR;  
418 - srs_error("hls decode video avc NALU size failed. ret=%d", ret);  
419 - return ret;  
420 - }  
421 - int32_t NALUnitLength = 0;  
422 - if (NAL_unit_length == 3) {  
423 - NALUnitLength = stream->read_4bytes();  
424 - } else if (NALUnitLength == 2) {  
425 - NALUnitLength = stream->read_3bytes();  
426 - } else if (NALUnitLength == 1) {  
427 - NALUnitLength = stream->read_2bytes();  
428 - } else {  
429 - NALUnitLength = stream->read_1bytes();  
430 - }  
431 - // NALUnit  
432 - if (!stream->require(NALUnitLength)) {  
433 - ret = ERROR_HLS_DECODE_ERROR;  
434 - srs_error("hls decode video avc NALU data failed. ret=%d", ret);  
435 - return ret;  
436 - }  
437 - // 7.3.1 NAL unit syntax, H.264-AVC-ISO_IEC_14496-10.pdf, page 44.  
438 - if ((ret = sample->add_sample(stream->current(), NALUnitLength)) != ERROR_SUCCESS) {  
439 - srs_error("hls add video sample failed. ret=%d", ret);  
440 - return ret;  
441 - }  
442 - stream->skip(NALUnitLength);  
443 -  
444 - i += NAL_unit_length + 1 + NALUnitLength;  
445 - }  
446 - } else {  
447 - // ignored.  
448 - }  
449 -  
450 - srs_info("video decoded, type=%d, codec=%d, avc=%d, time=%d, size=%d",  
451 - frame_type, video_codec_id, avc_packet_type, composition_time, size);  
452 -  
453 - return ret; 273 + int ret = ERROR_SUCCESS;
  274 +
  275 + sample->is_video = true;
  276 +
  277 + if (!data || size <= 0) {
  278 + srs_trace("no video present, hls ignore it.");
  279 + return ret;
  280 + }
  281 +
  282 + if ((ret = stream->initialize((char*)data, size)) != ERROR_SUCCESS) {
  283 + return ret;
  284 + }
  285 +
  286 + // video decode
  287 + if (!stream->require(1)) {
  288 + ret = ERROR_HLS_DECODE_ERROR;
  289 + srs_error("hls decode video frame_type failed. ret=%d", ret);
  290 + return ret;
  291 + }
  292 +
  293 + int8_t frame_type = stream->read_1bytes();
  294 + int8_t codec_id = frame_type & 0x0f;
  295 + frame_type = (frame_type >> 4) & 0x0f;
  296 +
  297 + sample->frame_type = (SrsCodecVideoAVCFrame)frame_type;
  298 +
  299 + // only support h.264/avc
  300 + if (codec_id != SrsCodecVideoAVC) {
  301 + ret = ERROR_HLS_DECODE_ERROR;
  302 + srs_error("hls only support video h.264/avc codec. ret=%d", ret);
  303 + return ret;
  304 + }
  305 + video_codec_id = codec_id;
  306 +
  307 + if (!stream->require(4)) {
  308 + ret = ERROR_HLS_DECODE_ERROR;
  309 + srs_error("hls decode video avc_packet_type failed. ret=%d", ret);
  310 + return ret;
  311 + }
  312 + int8_t avc_packet_type = stream->read_1bytes();
  313 + int32_t composition_time = stream->read_3bytes();
  314 +
  315 + // pts = dts + cts.
  316 + sample->cts = composition_time;
  317 + sample->avc_packet_type = (SrsCodecVideoAVCType)avc_packet_type;
  318 +
  319 + if (avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader) {
  320 + // AVCDecoderConfigurationRecord
  321 + // 5.2.4.1.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
  322 + avc_extra_size = stream->left();
  323 + if (avc_extra_size > 0) {
  324 + srs_freepa(avc_extra_data);
  325 + avc_extra_data = new char[avc_extra_size];
  326 + memcpy(avc_extra_data, stream->current(), avc_extra_size);
  327 + }
  328 +
  329 + if (!stream->require(6)) {
  330 + ret = ERROR_HLS_DECODE_ERROR;
  331 + srs_error("hls decode video avc sequenc header failed. ret=%d", ret);
  332 + return ret;
  333 + }
  334 + //int8_t configurationVersion = stream->read_1bytes();
  335 + //int8_t AVCProfileIndication = stream->read_1bytes();
  336 + //int8_t profile_compatibility = stream->read_1bytes();
  337 + //int8_t AVCLevelIndication = stream->read_1bytes();
  338 + stream->skip(4);
  339 + // parse the NALU size.
  340 + int8_t lengthSizeMinusOne = stream->read_1bytes();
  341 + lengthSizeMinusOne &= 0x03;
  342 + NAL_unit_length = lengthSizeMinusOne;
  343 +
  344 + // 1 sps
  345 + if (!stream->require(1)) {
  346 + ret = ERROR_HLS_DECODE_ERROR;
  347 + srs_error("hls decode video avc sequenc header sps failed. ret=%d", ret);
  348 + return ret;
  349 + }
  350 + int8_t numOfSequenceParameterSets = stream->read_1bytes();
  351 + numOfSequenceParameterSets &= 0x1f;
  352 + if (numOfSequenceParameterSets != 1) {
  353 + ret = ERROR_HLS_DECODE_ERROR;
  354 + srs_error("hls decode video avc sequenc header sps failed. ret=%d", ret);
  355 + return ret;
  356 + }
  357 + if (!stream->require(2)) {
  358 + ret = ERROR_HLS_DECODE_ERROR;
  359 + srs_error("hls decode video avc sequenc header sps size failed. ret=%d", ret);
  360 + return ret;
  361 + }
  362 + sequenceParameterSetLength = stream->read_2bytes();
  363 + if (!stream->require(sequenceParameterSetLength)) {
  364 + ret = ERROR_HLS_DECODE_ERROR;
  365 + srs_error("hls decode video avc sequenc header sps data failed. ret=%d", ret);
  366 + return ret;
  367 + }
  368 + if (sequenceParameterSetLength > 0) {
  369 + srs_freepa(sequenceParameterSetNALUnit);
  370 + sequenceParameterSetNALUnit = new char[sequenceParameterSetLength];
  371 + memcpy(sequenceParameterSetNALUnit, stream->current(), sequenceParameterSetLength);
  372 + stream->skip(sequenceParameterSetLength);
  373 + }
  374 + // 1 pps
  375 + if (!stream->require(1)) {
  376 + ret = ERROR_HLS_DECODE_ERROR;
  377 + srs_error("hls decode video avc sequenc header pps failed. ret=%d", ret);
  378 + return ret;
  379 + }
  380 + int8_t numOfPictureParameterSets = stream->read_1bytes();
  381 + numOfPictureParameterSets &= 0x1f;
  382 + if (numOfPictureParameterSets != 1) {
  383 + ret = ERROR_HLS_DECODE_ERROR;
  384 + srs_error("hls decode video avc sequenc header pps failed. ret=%d", ret);
  385 + return ret;
  386 + }
  387 + if (!stream->require(2)) {
  388 + ret = ERROR_HLS_DECODE_ERROR;
  389 + srs_error("hls decode video avc sequenc header pps size failed. ret=%d", ret);
  390 + return ret;
  391 + }
  392 + pictureParameterSetLength = stream->read_2bytes();
  393 + if (!stream->require(pictureParameterSetLength)) {
  394 + ret = ERROR_HLS_DECODE_ERROR;
  395 + srs_error("hls decode video avc sequenc header pps data failed. ret=%d", ret);
  396 + return ret;
  397 + }
  398 + if (pictureParameterSetLength > 0) {
  399 + srs_freepa(pictureParameterSetNALUnit);
  400 + pictureParameterSetNALUnit = new char[pictureParameterSetLength];
  401 + memcpy(pictureParameterSetNALUnit, stream->current(), pictureParameterSetLength);
  402 + stream->skip(pictureParameterSetLength);
  403 + }
  404 + } else if (avc_packet_type == SrsCodecVideoAVCTypeNALU){
  405 + // ensure the sequence header demuxed
  406 + if (avc_extra_size <= 0 || !avc_extra_data) {
  407 + ret = ERROR_HLS_DECODE_ERROR;
  408 + srs_error("hls decode video avc failed, sequence header not found. ret=%d", ret);
  409 + return ret;
  410 + }
  411 +
  412 + // One or more NALUs (Full frames are required)
  413 + // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 20
  414 + int PictureLength = stream->left();
  415 + for (int i = 0; i < PictureLength;) {
  416 + if (!stream->require(NAL_unit_length + 1)) {
  417 + ret = ERROR_HLS_DECODE_ERROR;
  418 + srs_error("hls decode video avc NALU size failed. ret=%d", ret);
  419 + return ret;
  420 + }
  421 + int32_t NALUnitLength = 0;
  422 + if (NAL_unit_length == 3) {
  423 + NALUnitLength = stream->read_4bytes();
  424 + } else if (NALUnitLength == 2) {
  425 + NALUnitLength = stream->read_3bytes();
  426 + } else if (NALUnitLength == 1) {
  427 + NALUnitLength = stream->read_2bytes();
  428 + } else {
  429 + NALUnitLength = stream->read_1bytes();
  430 + }
  431 + // NALUnit
  432 + if (!stream->require(NALUnitLength)) {
  433 + ret = ERROR_HLS_DECODE_ERROR;
  434 + srs_error("hls decode video avc NALU data failed. ret=%d", ret);
  435 + return ret;
  436 + }
  437 + // 7.3.1 NAL unit syntax, H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
  438 + if ((ret = sample->add_sample(stream->current(), NALUnitLength)) != ERROR_SUCCESS) {
  439 + srs_error("hls add video sample failed. ret=%d", ret);
  440 + return ret;
  441 + }
  442 + stream->skip(NALUnitLength);
  443 +
  444 + i += NAL_unit_length + 1 + NALUnitLength;
  445 + }
  446 + } else {
  447 + // ignored.
  448 + }
  449 +
  450 + srs_info("video decoded, type=%d, codec=%d, avc=%d, time=%d, size=%d",
  451 + frame_type, video_codec_id, avc_packet_type, composition_time, size);
  452 +
  453 + return ret;
454 } 454 }
455 455
456 bool SrsCodec::video_is_keyframe(int8_t* data, int size) 456 bool SrsCodec::video_is_keyframe(int8_t* data, int size)
457 { 457 {
458 - // 2bytes required.  
459 - if (size < 1) {  
460 - return false;  
461 - }  
462 -  
463 - char frame_type = *(char*)data;  
464 - frame_type = (frame_type >> 4) & 0x0F;  
465 -  
466 - return frame_type == SrsCodecVideoAVCFrameKeyFrame; 458 + // 2bytes required.
  459 + if (size < 1) {
  460 + return false;
  461 + }
  462 +
  463 + char frame_type = *(char*)data;
  464 + frame_type = (frame_type >> 4) & 0x0F;
  465 +
  466 + return frame_type == SrsCodecVideoAVCFrameKeyFrame;
467 } 467 }
468 468
469 bool SrsCodec::video_is_sequence_header(int8_t* data, int size) 469 bool SrsCodec::video_is_sequence_header(int8_t* data, int size)
470 { 470 {
471 - // sequence header only for h264  
472 - if (!video_is_h264(data, size)) {  
473 - return false;  
474 - }  
475 -  
476 - // 2bytes required.  
477 - if (size < 2) {  
478 - return false;  
479 - }  
480 -  
481 - char frame_type = *(char*)data;  
482 - frame_type = (frame_type >> 4) & 0x0F;  
483 -  
484 - char avc_packet_type = *(char*)(data + 1);  
485 -  
486 - return frame_type == SrsCodecVideoAVCFrameKeyFrame  
487 - && avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader; 471 + // sequence header only for h264
  472 + if (!video_is_h264(data, size)) {
  473 + return false;
  474 + }
  475 +
  476 + // 2bytes required.
  477 + if (size < 2) {
  478 + return false;
  479 + }
  480 +
  481 + char frame_type = *(char*)data;
  482 + frame_type = (frame_type >> 4) & 0x0F;
  483 +
  484 + char avc_packet_type = *(char*)(data + 1);
  485 +
  486 + return frame_type == SrsCodecVideoAVCFrameKeyFrame
  487 + && avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader;
488 } 488 }
489 489
490 bool SrsCodec::audio_is_sequence_header(int8_t* data, int size) 490 bool SrsCodec::audio_is_sequence_header(int8_t* data, int size)
491 { 491 {
492 - // sequence header only for aac  
493 - if (!audio_is_aac(data, size)) {  
494 - return false;  
495 - }  
496 -  
497 - // 2bytes required.  
498 - if (size < 2) {  
499 - return false;  
500 - }  
501 -  
502 - char aac_packet_type = *(char*)(data + 1);  
503 -  
504 - return aac_packet_type == SrsCodecAudioTypeSequenceHeader; 492 + // sequence header only for aac
  493 + if (!audio_is_aac(data, size)) {
  494 + return false;
  495 + }
  496 +
  497 + // 2bytes required.
  498 + if (size < 2) {
  499 + return false;
  500 + }
  501 +
  502 + char aac_packet_type = *(char*)(data + 1);
  503 +
  504 + return aac_packet_type == SrsCodecAudioTypeSequenceHeader;
505 } 505 }
506 506
507 bool SrsCodec::video_is_h264(int8_t* data, int size) 507 bool SrsCodec::video_is_h264(int8_t* data, int size)
508 { 508 {
509 - // 1bytes required.  
510 - if (size < 1) {  
511 - return false;  
512 - }  
513 -  
514 - char codec_id = *(char*)data;  
515 - codec_id = codec_id & 0x0F;  
516 -  
517 - return codec_id == SrsCodecVideoAVC; 509 + // 1bytes required.
  510 + if (size < 1) {
  511 + return false;
  512 + }
  513 +
  514 + char codec_id = *(char*)data;
  515 + codec_id = codec_id & 0x0F;
  516 +
  517 + return codec_id == SrsCodecVideoAVC;
518 } 518 }
519 519
520 bool SrsCodec::audio_is_aac(int8_t* data, int size) 520 bool SrsCodec::audio_is_aac(int8_t* data, int size)
521 { 521 {
522 - // 1bytes required.  
523 - if (size < 1) {  
524 - return false;  
525 - }  
526 -  
527 - char sound_format = *(char*)data;  
528 - sound_format = (sound_format >> 4) & 0x0F;  
529 -  
530 - return sound_format == SrsCodecAudioAAC; 522 + // 1bytes required.
  523 + if (size < 1) {
  524 + return false;
  525 + }
  526 +
  527 + char sound_format = *(char*)data;
  528 + sound_format = (sound_format >> 4) & 0x0F;
  529 +
  530 + return sound_format == SrsCodecAudioAAC;
531 } 531 }
@@ -37,104 +37,104 @@ class SrsStream; @@ -37,104 +37,104 @@ class SrsStream;
37 // E.4.3.1 VIDEODATA 37 // E.4.3.1 VIDEODATA
38 // CodecID UB [4] 38 // CodecID UB [4]
39 // Codec Identifier. The following values are defined: 39 // Codec Identifier. The following values are defined:
40 -// 2 = Sorenson H.263  
41 -// 3 = Screen video  
42 -// 4 = On2 VP6  
43 -// 5 = On2 VP6 with alpha channel  
44 -// 6 = Screen video version 2  
45 -// 7 = AVC 40 +// 2 = Sorenson H.263
  41 +// 3 = Screen video
  42 +// 4 = On2 VP6
  43 +// 5 = On2 VP6 with alpha channel
  44 +// 6 = Screen video version 2
  45 +// 7 = AVC
46 enum SrsCodecVideo 46 enum SrsCodecVideo
47 { 47 {
48 - SrsCodecVideoReserved = 0,  
49 -  
50 - SrsCodecVideoSorensonH263 = 2,  
51 - SrsCodecVideoScreenVideo = 3,  
52 - SrsCodecVideoOn2VP6 = 4,  
53 - SrsCodecVideoOn2VP6WithAlphaChannel = 5,  
54 - SrsCodecVideoScreenVideoVersion2 = 6,  
55 - SrsCodecVideoAVC = 7, 48 + SrsCodecVideoReserved = 0,
  49 +
  50 + SrsCodecVideoSorensonH263 = 2,
  51 + SrsCodecVideoScreenVideo = 3,
  52 + SrsCodecVideoOn2VP6 = 4,
  53 + SrsCodecVideoOn2VP6WithAlphaChannel = 5,
  54 + SrsCodecVideoScreenVideoVersion2 = 6,
  55 + SrsCodecVideoAVC = 7,
56 }; 56 };
57 57
58 // E.4.3.1 VIDEODATA 58 // E.4.3.1 VIDEODATA
59 // Frame Type UB [4] 59 // Frame Type UB [4]
60 // Type of video frame. The following values are defined: 60 // Type of video frame. The following values are defined:
61 -// 1 = key frame (for AVC, a seekable frame)  
62 -// 2 = inter frame (for AVC, a non-seekable frame)  
63 -// 3 = disposable inter frame (H.263 only)  
64 -// 4 = generated key frame (reserved for server use only)  
65 -// 5 = video info/command frame 61 +// 1 = key frame (for AVC, a seekable frame)
  62 +// 2 = inter frame (for AVC, a non-seekable frame)
  63 +// 3 = disposable inter frame (H.263 only)
  64 +// 4 = generated key frame (reserved for server use only)
  65 +// 5 = video info/command frame
66 enum SrsCodecVideoAVCFrame 66 enum SrsCodecVideoAVCFrame
67 { 67 {
68 - SrsCodecVideoAVCFrameReserved = 0,  
69 -  
70 - SrsCodecVideoAVCFrameKeyFrame = 1,  
71 - SrsCodecVideoAVCFrameInterFrame = 2,  
72 - SrsCodecVideoAVCFrameDisposableInterFrame = 3,  
73 - SrsCodecVideoAVCFrameGeneratedKeyFrame = 4,  
74 - SrsCodecVideoAVCFrameVideoInfoFrame = 5, 68 + SrsCodecVideoAVCFrameReserved = 0,
  69 +
  70 + SrsCodecVideoAVCFrameKeyFrame = 1,
  71 + SrsCodecVideoAVCFrameInterFrame = 2,
  72 + SrsCodecVideoAVCFrameDisposableInterFrame = 3,
  73 + SrsCodecVideoAVCFrameGeneratedKeyFrame = 4,
  74 + SrsCodecVideoAVCFrameVideoInfoFrame = 5,
75 }; 75 };
76 76
77 // AVCPacketType IF CodecID == 7 UI8 77 // AVCPacketType IF CodecID == 7 UI8
78 // The following values are defined: 78 // The following values are defined:
79 -// 0 = AVC sequence header  
80 -// 1 = AVC NALU  
81 -// 2 = AVC end of sequence (lower level NALU sequence ender is  
82 -// not required or supported) 79 +// 0 = AVC sequence header
  80 +// 1 = AVC NALU
  81 +// 2 = AVC end of sequence (lower level NALU sequence ender is
  82 +// not required or supported)
83 enum SrsCodecVideoAVCType 83 enum SrsCodecVideoAVCType
84 { 84 {
85 - SrsCodecVideoAVCTypeReserved = -1,  
86 -  
87 - SrsCodecVideoAVCTypeSequenceHeader = 0,  
88 - SrsCodecVideoAVCTypeNALU = 1,  
89 - SrsCodecVideoAVCTypeSequenceHeaderEOF = 2, 85 + SrsCodecVideoAVCTypeReserved = -1,
  86 +
  87 + SrsCodecVideoAVCTypeSequenceHeader = 0,
  88 + SrsCodecVideoAVCTypeNALU = 1,
  89 + SrsCodecVideoAVCTypeSequenceHeaderEOF = 2,
90 }; 90 };
91 91
92 // SoundFormat UB [4] 92 // SoundFormat UB [4]
93 // Format of SoundData. The following values are defined: 93 // Format of SoundData. The following values are defined:
94 -// 0 = Linear PCM, platform endian  
95 -// 1 = ADPCM  
96 -// 2 = MP3  
97 -// 3 = Linear PCM, little endian  
98 -// 4 = Nellymoser 16 kHz mono  
99 -// 5 = Nellymoser 8 kHz mono  
100 -// 6 = Nellymoser  
101 -// 7 = G.711 A-law logarithmic PCM  
102 -// 8 = G.711 mu-law logarithmic PCM  
103 -// 9 = reserved  
104 -// 10 = AAC  
105 -// 11 = Speex  
106 -// 14 = MP3 8 kHz  
107 -// 15 = Device-specific sound 94 +// 0 = Linear PCM, platform endian
  95 +// 1 = ADPCM
  96 +// 2 = MP3
  97 +// 3 = Linear PCM, little endian
  98 +// 4 = Nellymoser 16 kHz mono
  99 +// 5 = Nellymoser 8 kHz mono
  100 +// 6 = Nellymoser
  101 +// 7 = G.711 A-law logarithmic PCM
  102 +// 8 = G.711 mu-law logarithmic PCM
  103 +// 9 = reserved
  104 +// 10 = AAC
  105 +// 11 = Speex
  106 +// 14 = MP3 8 kHz
  107 +// 15 = Device-specific sound
108 // Formats 7, 8, 14, and 15 are reserved. 108 // Formats 7, 8, 14, and 15 are reserved.
109 // AAC is supported in Flash Player 9,0,115,0 and higher. 109 // AAC is supported in Flash Player 9,0,115,0 and higher.
110 // Speex is supported in Flash Player 10 and higher. 110 // Speex is supported in Flash Player 10 and higher.
111 enum SrsCodecAudio 111 enum SrsCodecAudio
112 { 112 {
113 - SrsCodecAudioLinearPCMPlatformEndian = 0,  
114 - SrsCodecAudioADPCM = 1,  
115 - SrsCodecAudioMP3 = 2,  
116 - SrsCodecAudioLinearPCMLittleEndian = 3,  
117 - SrsCodecAudioNellymoser16kHzMono = 4,  
118 - SrsCodecAudioNellymoser8kHzMono = 5,  
119 - SrsCodecAudioNellymoser = 6,  
120 - SrsCodecAudioReservedG711AlawLogarithmicPCM = 7,  
121 - SrsCodecAudioReservedG711MuLawLogarithmicPCM = 8,  
122 - SrsCodecAudioReserved = 9,  
123 - SrsCodecAudioAAC = 10,  
124 - SrsCodecAudioSpeex = 11,  
125 - SrsCodecAudioReservedMP3_8kHz = 14,  
126 - SrsCodecAudioReservedDeviceSpecificSound = 15, 113 + SrsCodecAudioLinearPCMPlatformEndian = 0,
  114 + SrsCodecAudioADPCM = 1,
  115 + SrsCodecAudioMP3 = 2,
  116 + SrsCodecAudioLinearPCMLittleEndian = 3,
  117 + SrsCodecAudioNellymoser16kHzMono = 4,
  118 + SrsCodecAudioNellymoser8kHzMono = 5,
  119 + SrsCodecAudioNellymoser = 6,
  120 + SrsCodecAudioReservedG711AlawLogarithmicPCM = 7,
  121 + SrsCodecAudioReservedG711MuLawLogarithmicPCM = 8,
  122 + SrsCodecAudioReserved = 9,
  123 + SrsCodecAudioAAC = 10,
  124 + SrsCodecAudioSpeex = 11,
  125 + SrsCodecAudioReservedMP3_8kHz = 14,
  126 + SrsCodecAudioReservedDeviceSpecificSound = 15,
127 }; 127 };
128 128
129 // AACPacketType IF SoundFormat == 10 UI8 129 // AACPacketType IF SoundFormat == 10 UI8
130 // The following values are defined: 130 // The following values are defined:
131 -// 0 = AAC sequence header  
132 -// 1 = AAC raw 131 +// 0 = AAC sequence header
  132 +// 1 = AAC raw
133 enum SrsCodecAudioType 133 enum SrsCodecAudioType
134 { 134 {
135 - SrsCodecAudioTypeReserved = -1,  
136 - SrsCodecAudioTypeSequenceHeader = 0,  
137 - SrsCodecAudioTypeRawData = 1, 135 + SrsCodecAudioTypeReserved = -1,
  136 + SrsCodecAudioTypeSequenceHeader = 0,
  137 + SrsCodecAudioTypeRawData = 1,
138 }; 138 };
139 139
140 // Sampling rate. The following values are defined: 140 // Sampling rate. The following values are defined:
@@ -144,12 +144,12 @@ enum SrsCodecAudioType @@ -144,12 +144,12 @@ enum SrsCodecAudioType
144 // 3 = 44 kHz = 44100 Hz 144 // 3 = 44 kHz = 44100 Hz
145 enum SrsCodecAudioSampleRate 145 enum SrsCodecAudioSampleRate
146 { 146 {
147 - SrsCodecAudioSampleRateReserved = -1,  
148 -  
149 - SrsCodecAudioSampleRate5512 = 0,  
150 - SrsCodecAudioSampleRate11025 = 1,  
151 - SrsCodecAudioSampleRate22050 = 2,  
152 - SrsCodecAudioSampleRate44100 = 3, 147 + SrsCodecAudioSampleRateReserved = -1,
  148 +
  149 + SrsCodecAudioSampleRate5512 = 0,
  150 + SrsCodecAudioSampleRate11025 = 1,
  151 + SrsCodecAudioSampleRate22050 = 2,
  152 + SrsCodecAudioSampleRate44100 = 3,
153 }; 153 };
154 154
155 // Size of each audio sample. This parameter only pertains to 155 // Size of each audio sample. This parameter only pertains to
@@ -159,10 +159,10 @@ enum SrsCodecAudioSampleRate @@ -159,10 +159,10 @@ enum SrsCodecAudioSampleRate
159 // 1 = 16-bit samples 159 // 1 = 16-bit samples
160 enum SrsCodecAudioSampleSize 160 enum SrsCodecAudioSampleSize
161 { 161 {
162 - SrsCodecAudioSampleSizeReserved = -1,  
163 -  
164 - SrsCodecAudioSampleSize8bit = 0,  
165 - SrsCodecAudioSampleSize16bit = 1, 162 + SrsCodecAudioSampleSizeReserved = -1,
  163 +
  164 + SrsCodecAudioSampleSize8bit = 0,
  165 + SrsCodecAudioSampleSize16bit = 1,
166 }; 166 };
167 167
168 // Mono or stereo sound 168 // Mono or stereo sound
@@ -170,10 +170,10 @@ enum SrsCodecAudioSampleSize @@ -170,10 +170,10 @@ enum SrsCodecAudioSampleSize
170 // 1 = Stereo sound 170 // 1 = Stereo sound
171 enum SrsCodecAudioSoundType 171 enum SrsCodecAudioSoundType
172 { 172 {
173 - SrsCodecAudioSoundTypeReserved = -1,  
174 -  
175 - SrsCodecAudioSoundTypeMono = 0,  
176 - SrsCodecAudioSoundTypeStereo = 1, 173 + SrsCodecAudioSoundTypeReserved = -1,
  174 +
  175 + SrsCodecAudioSoundTypeMono = 0,
  176 + SrsCodecAudioSoundTypeStereo = 1,
177 }; 177 };
178 178
179 /** 179 /**
@@ -181,21 +181,21 @@ enum SrsCodecAudioSoundType @@ -181,21 +181,21 @@ enum SrsCodecAudioSoundType
181 */ 181 */
182 struct SrsCodecBuffer 182 struct SrsCodecBuffer
183 { 183 {
184 - /**  
185 - * @remark user must manage the bytes.  
186 - */  
187 - int size;  
188 - char* bytes;  
189 -  
190 - SrsCodecBuffer();  
191 - void append(void* data, int len);  
192 -  
193 - /**  
194 - * free the bytes,  
195 - * user can invoke it to free the bytes,  
196 - * the SrsCodecBuffer never free automatically.  
197 - */  
198 - void free(); 184 + /**
  185 + * @remark user must manage the bytes.
  186 + */
  187 + int size;
  188 + char* bytes;
  189 +
  190 + SrsCodecBuffer();
  191 + void append(void* data, int len);
  192 +
  193 + /**
  194 + * free the bytes,
  195 + * user can invoke it to free the bytes,
  196 + * the SrsCodecBuffer never free automatically.
  197 + */
  198 + void free();
199 }; 199 };
200 200
201 /** 201 /**
@@ -204,26 +204,26 @@ struct SrsCodecBuffer @@ -204,26 +204,26 @@ struct SrsCodecBuffer
204 class SrsCodecSample 204 class SrsCodecSample
205 { 205 {
206 public: 206 public:
207 - int nb_buffers;  
208 - SrsCodecBuffer buffers[SRS_MAX_CODEC_SAMPLE]; 207 + int nb_buffers;
  208 + SrsCodecBuffer buffers[SRS_MAX_CODEC_SAMPLE];
209 public: 209 public:
210 - bool is_video;  
211 - // video specified  
212 - SrsCodecVideoAVCFrame frame_type;  
213 - SrsCodecVideoAVCType avc_packet_type;  
214 - // CompositionTime, video_file_format_spec_v10_1.pdf, page 78.  
215 - // cts = pts - dts, where dts = flvheader->timestamp.  
216 - int32_t cts;  
217 - // audio specified  
218 - SrsCodecAudioSampleRate sound_rate;  
219 - SrsCodecAudioSampleSize sound_size;  
220 - SrsCodecAudioSoundType sound_type;  
221 - SrsCodecAudioType aac_packet_type; 210 + bool is_video;
  211 + // video specified
  212 + SrsCodecVideoAVCFrame frame_type;
  213 + SrsCodecVideoAVCType avc_packet_type;
  214 + // CompositionTime, video_file_format_spec_v10_1.pdf, page 78.
  215 + // cts = pts - dts, where dts = flvheader->timestamp.
  216 + int32_t cts;
  217 + // audio specified
  218 + SrsCodecAudioSampleRate sound_rate;
  219 + SrsCodecAudioSampleSize sound_size;
  220 + SrsCodecAudioSoundType sound_type;
  221 + SrsCodecAudioType aac_packet_type;
222 public: 222 public:
223 - SrsCodecSample();  
224 - virtual ~SrsCodecSample();  
225 - void clear();  
226 - int add_sample(char* bytes, int size); 223 + SrsCodecSample();
  224 + virtual ~SrsCodecSample();
  225 + void clear();
  226 + int add_sample(char* bytes, int size);
227 }; 227 };
228 228
229 /** 229 /**
@@ -232,81 +232,81 @@ public: @@ -232,81 +232,81 @@ public:
232 class SrsCodec 232 class SrsCodec
233 { 233 {
234 private: 234 private:
235 - SrsStream* stream; 235 + SrsStream* stream;
236 public: 236 public:
237 - /**  
238 - * video specified  
239 - */  
240 - // @see: SrsCodecVideo  
241 - int video_codec_id;  
242 - // profile_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.  
243 - u_int8_t avc_profile;  
244 - // level_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.  
245 - u_int8_t avc_level;  
246 - int width;  
247 - int height;  
248 - int video_data_rate; // in bps  
249 - int frame_rate;  
250 - int duration;  
251 - // lengthSizeMinusOne, H.264-AVC-ISO_IEC_14496-15.pdf, page 16  
252 - int8_t NAL_unit_length;  
253 - u_int16_t sequenceParameterSetLength;  
254 - char* sequenceParameterSetNALUnit;  
255 - u_int16_t pictureParameterSetLength;  
256 - char* pictureParameterSetNALUnit;  
257 - /**  
258 - * audio specified  
259 - */  
260 - // @see: SrsCodecAudioType  
261 - int audio_codec_id;  
262 - int audio_data_rate; // in bps  
263 - // 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33.  
264 - // audioObjectType, value defines in 7.1 Profiles, aac-iso-13818-7.pdf, page 40.  
265 - u_int8_t aac_profile;  
266 - // samplingFrequencyIndex  
267 - u_int8_t aac_sample_rate;  
268 - // channelConfiguration  
269 - u_int8_t aac_channels;  
270 - // the avc extra data, the AVC sequence header,  
271 - // without the flv codec header,  
272 - // @see: ffmpeg, AVCodecContext::extradata  
273 - int avc_extra_size;  
274 - char* avc_extra_data;  
275 - // the aac extra data, the AAC sequence header,  
276 - // without the flv codec header,  
277 - // @see: ffmpeg, AVCodecContext::extradata  
278 - int aac_extra_size;  
279 - char* aac_extra_data; 237 + /**
  238 + * video specified
  239 + */
  240 + // @see: SrsCodecVideo
  241 + int video_codec_id;
  242 + // profile_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
  243 + u_int8_t avc_profile;
  244 + // level_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
  245 + u_int8_t avc_level;
  246 + int width;
  247 + int height;
  248 + int video_data_rate; // in bps
  249 + int frame_rate;
  250 + int duration;
  251 + // lengthSizeMinusOne, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
  252 + int8_t NAL_unit_length;
  253 + u_int16_t sequenceParameterSetLength;
  254 + char* sequenceParameterSetNALUnit;
  255 + u_int16_t pictureParameterSetLength;
  256 + char* pictureParameterSetNALUnit;
  257 + /**
  258 + * audio specified
  259 + */
  260 + // @see: SrsCodecAudioType
  261 + int audio_codec_id;
  262 + int audio_data_rate; // in bps
  263 + // 1.6.2.1 AudioSpecificConfig, in aac-mp4a-format-ISO_IEC_14496-3+2001.pdf, page 33.
  264 + // audioObjectType, value defines in 7.1 Profiles, aac-iso-13818-7.pdf, page 40.
  265 + u_int8_t aac_profile;
  266 + // samplingFrequencyIndex
  267 + u_int8_t aac_sample_rate;
  268 + // channelConfiguration
  269 + u_int8_t aac_channels;
  270 + // the avc extra data, the AVC sequence header,
  271 + // without the flv codec header,
  272 + // @see: ffmpeg, AVCodecContext::extradata
  273 + int avc_extra_size;
  274 + char* avc_extra_data;
  275 + // the aac extra data, the AAC sequence header,
  276 + // without the flv codec header,
  277 + // @see: ffmpeg, AVCodecContext::extradata
  278 + int aac_extra_size;
  279 + char* aac_extra_data;
280 public: 280 public:
281 - SrsCodec();  
282 - virtual ~SrsCodec(); 281 + SrsCodec();
  282 + virtual ~SrsCodec();
283 // the following function used for hls to build the codec info. 283 // the following function used for hls to build the codec info.
284 public: 284 public:
285 - virtual int audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample);  
286 - virtual int video_avc_demux(int8_t* data, int size, SrsCodecSample* sample); 285 + virtual int audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample);
  286 + virtual int video_avc_demux(int8_t* data, int size, SrsCodecSample* sample);
287 // the following function used to finger out the flv/rtmp packet detail. 287 // the following function used to finger out the flv/rtmp packet detail.
288 public: 288 public:
289 - /**  
290 - * only check the frame_type, not check the codec type.  
291 - */  
292 - static bool video_is_keyframe(int8_t* data, int size);  
293 - /**  
294 - * check codec h264, keyframe, sequence header  
295 - */  
296 - static bool video_is_sequence_header(int8_t* data, int size);  
297 - /**  
298 - * check codec aac, sequence header  
299 - */  
300 - static bool audio_is_sequence_header(int8_t* data, int size);  
301 - /**  
302 - * check codec h264.  
303 - */  
304 - static bool video_is_h264(int8_t* data, int size); 289 + /**
  290 + * only check the frame_type, not check the codec type.
  291 + */
  292 + static bool video_is_keyframe(int8_t* data, int size);
  293 + /**
  294 + * check codec h264, keyframe, sequence header
  295 + */
  296 + static bool video_is_sequence_header(int8_t* data, int size);
  297 + /**
  298 + * check codec aac, sequence header
  299 + */
  300 + static bool audio_is_sequence_header(int8_t* data, int size);
  301 + /**
  302 + * check codec h264.
  303 + */
  304 + static bool video_is_h264(int8_t* data, int size);
305 private: 305 private:
306 - /**  
307 - * check codec aac.  
308 - */  
309 - static bool audio_is_aac(int8_t* data, int size); 306 + /**
  307 + * check codec aac.
  308 + */
  309 + static bool audio_is_aac(int8_t* data, int size);
310 }; 310 };
311 311
312 #endif 312 #endif