正在显示
9 个修改的文件
包含
171 行增加
和
1699 行删除
| @@ -189,22 +189,22 @@ int SrsBandwidth::do_bandwidth_check() | @@ -189,22 +189,22 @@ int SrsBandwidth::do_bandwidth_check() | ||
| 189 | pkt->data->set("publish_bytes", SrsAmf0Any::number(publish_bytes)); | 189 | pkt->data->set("publish_bytes", SrsAmf0Any::number(publish_bytes)); |
| 190 | pkt->data->set("publish_time", SrsAmf0Any::number(publish_actual_duration_ms)); | 190 | pkt->data->set("publish_time", SrsAmf0Any::number(publish_actual_duration_ms)); |
| 191 | 191 | ||
| 192 | - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0); | ||
| 193 | - if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) { | 192 | + if ((ret = rtmp->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 194 | srs_error("send bandwidth check finish message failed. ret=%d", ret); | 193 | srs_error("send bandwidth check finish message failed. ret=%d", ret); |
| 195 | return ret; | 194 | return ret; |
| 196 | } | 195 | } |
| 197 | 196 | ||
| 198 | // if flash, we notice the result, and expect a final packet. | 197 | // if flash, we notice the result, and expect a final packet. |
| 199 | while (true) { | 198 | while (true) { |
| 200 | - SrsCommonMessage* msg = NULL; | 199 | + __SrsMessage* msg = NULL; |
| 201 | SrsBandwidthPacket* pkt = NULL; | 200 | SrsBandwidthPacket* pkt = NULL; |
| 202 | - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 201 | + if ((ret = __srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 203 | // info level to ignore and return success. | 202 | // info level to ignore and return success. |
| 204 | srs_info("expect final message failed. ret=%d", ret); | 203 | srs_info("expect final message failed. ret=%d", ret); |
| 205 | return ERROR_SUCCESS; | 204 | return ERROR_SUCCESS; |
| 206 | } | 205 | } |
| 207 | - SrsAutoFree(SrsCommonMessage, msg, false); | 206 | + SrsAutoFree(__SrsMessage, msg, false); |
| 207 | + SrsAutoFree(SrsBandwidthPacket, pkt, false); | ||
| 208 | srs_info("get final message success."); | 208 | srs_info("get final message success."); |
| 209 | 209 | ||
| 210 | if (pkt->is_flash_final()) { | 210 | if (pkt->is_flash_final()) { |
| @@ -233,8 +233,7 @@ int SrsBandwidth::check_play( | @@ -233,8 +233,7 @@ int SrsBandwidth::check_play( | ||
| 233 | pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); | 233 | pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); |
| 234 | pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); | 234 | pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); |
| 235 | 235 | ||
| 236 | - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0); | ||
| 237 | - if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) { | 236 | + if ((ret = rtmp->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 238 | srs_error("send bandwidth check start play message failed. ret=%d", ret); | 237 | srs_error("send bandwidth check start play message failed. ret=%d", ret); |
| 239 | return ret; | 238 | return ret; |
| 240 | } | 239 | } |
| @@ -243,13 +242,14 @@ int SrsBandwidth::check_play( | @@ -243,13 +242,14 @@ int SrsBandwidth::check_play( | ||
| 243 | 242 | ||
| 244 | while (true) { | 243 | while (true) { |
| 245 | // recv client's starting play response | 244 | // recv client's starting play response |
| 246 | - SrsCommonMessage* msg = NULL; | 245 | + __SrsMessage* msg = NULL; |
| 247 | SrsBandwidthPacket* pkt = NULL; | 246 | SrsBandwidthPacket* pkt = NULL; |
| 248 | - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 247 | + if ((ret = __srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 249 | srs_error("expect bandwidth message failed. ret=%d", ret); | 248 | srs_error("expect bandwidth message failed. ret=%d", ret); |
| 250 | return ret; | 249 | return ret; |
| 251 | } | 250 | } |
| 252 | - SrsAutoFree(SrsCommonMessage, msg, false); | 251 | + SrsAutoFree(__SrsMessage, msg, false); |
| 252 | + SrsAutoFree(SrsBandwidthPacket, pkt, false); | ||
| 253 | srs_info("get bandwidth message succes."); | 253 | srs_info("get bandwidth message succes."); |
| 254 | 254 | ||
| 255 | if (pkt->is_starting_play()) { | 255 | if (pkt->is_starting_play()) { |
| @@ -284,8 +284,7 @@ int SrsBandwidth::check_play( | @@ -284,8 +284,7 @@ int SrsBandwidth::check_play( | ||
| 284 | // TODO: FIXME: get length from the rtmp protocol stack. | 284 | // TODO: FIXME: get length from the rtmp protocol stack. |
| 285 | play_bytes += pkt->get_payload_length(); | 285 | play_bytes += pkt->get_payload_length(); |
| 286 | 286 | ||
| 287 | - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0); | ||
| 288 | - if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) { | 287 | + if ((ret = rtmp->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 289 | srs_error("send bandwidth check play messages failed. ret=%d", ret); | 288 | srs_error("send bandwidth check play messages failed. ret=%d", ret); |
| 290 | return ret; | 289 | return ret; |
| 291 | } | 290 | } |
| @@ -309,13 +308,13 @@ int SrsBandwidth::check_play( | @@ -309,13 +308,13 @@ int SrsBandwidth::check_play( | ||
| 309 | if (true) { | 308 | if (true) { |
| 310 | // notify client to stop play | 309 | // notify client to stop play |
| 311 | SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_play(); | 310 | SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_play(); |
| 311 | + | ||
| 312 | pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); | 312 | pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); |
| 313 | pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); | 313 | pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); |
| 314 | pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms)); | 314 | pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms)); |
| 315 | pkt->data->set("bytes_delta", SrsAmf0Any::number(play_bytes)); | 315 | pkt->data->set("bytes_delta", SrsAmf0Any::number(play_bytes)); |
| 316 | 316 | ||
| 317 | - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0); | ||
| 318 | - if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) { | 317 | + if ((ret = rtmp->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 319 | srs_error("send bandwidth check stop play message failed. ret=%d", ret); | 318 | srs_error("send bandwidth check stop play message failed. ret=%d", ret); |
| 320 | return ret; | 319 | return ret; |
| 321 | } | 320 | } |
| @@ -324,13 +323,14 @@ int SrsBandwidth::check_play( | @@ -324,13 +323,14 @@ int SrsBandwidth::check_play( | ||
| 324 | 323 | ||
| 325 | while (true) { | 324 | while (true) { |
| 326 | // recv client's stop play response. | 325 | // recv client's stop play response. |
| 327 | - SrsCommonMessage* msg = NULL; | 326 | + __SrsMessage* msg = NULL; |
| 328 | SrsBandwidthPacket* pkt = NULL; | 327 | SrsBandwidthPacket* pkt = NULL; |
| 329 | - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 328 | + if ((ret = __srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 330 | srs_error("expect bandwidth message failed. ret=%d", ret); | 329 | srs_error("expect bandwidth message failed. ret=%d", ret); |
| 331 | return ret; | 330 | return ret; |
| 332 | } | 331 | } |
| 333 | - SrsAutoFree(SrsCommonMessage, msg, false); | 332 | + SrsAutoFree(__SrsMessage, msg, false); |
| 333 | + SrsAutoFree(SrsBandwidthPacket, pkt, false); | ||
| 334 | srs_info("get bandwidth message succes."); | 334 | srs_info("get bandwidth message succes."); |
| 335 | 335 | ||
| 336 | if (pkt->is_stopped_play()) { | 336 | if (pkt->is_stopped_play()) { |
| @@ -357,8 +357,7 @@ int SrsBandwidth::check_publish( | @@ -357,8 +357,7 @@ int SrsBandwidth::check_publish( | ||
| 357 | pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); | 357 | pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); |
| 358 | pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); | 358 | pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); |
| 359 | 359 | ||
| 360 | - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0); | ||
| 361 | - if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) { | 360 | + if ((ret = rtmp->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 362 | srs_error("send bandwidth check start publish message failed. ret=%d", ret); | 361 | srs_error("send bandwidth check start publish message failed. ret=%d", ret); |
| 363 | return ret; | 362 | return ret; |
| 364 | } | 363 | } |
| @@ -367,13 +366,14 @@ int SrsBandwidth::check_publish( | @@ -367,13 +366,14 @@ int SrsBandwidth::check_publish( | ||
| 367 | 366 | ||
| 368 | while (true) { | 367 | while (true) { |
| 369 | // read client's notification of starting publish | 368 | // read client's notification of starting publish |
| 370 | - SrsCommonMessage* msg = NULL; | 369 | + __SrsMessage* msg = NULL; |
| 371 | SrsBandwidthPacket* pkt = NULL; | 370 | SrsBandwidthPacket* pkt = NULL; |
| 372 | - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 371 | + if ((ret = __srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 373 | srs_error("expect bandwidth message failed. ret=%d", ret); | 372 | srs_error("expect bandwidth message failed. ret=%d", ret); |
| 374 | return ret; | 373 | return ret; |
| 375 | } | 374 | } |
| 376 | - SrsAutoFree(SrsCommonMessage, msg, false); | 375 | + SrsAutoFree(__SrsMessage, msg, false); |
| 376 | + SrsAutoFree(SrsBandwidthPacket, pkt, false); | ||
| 377 | srs_info("get bandwidth message succes."); | 377 | srs_info("get bandwidth message succes."); |
| 378 | 378 | ||
| 379 | if (pkt->is_starting_publish()) { | 379 | if (pkt->is_starting_publish()) { |
| @@ -387,12 +387,12 @@ int SrsBandwidth::check_publish( | @@ -387,12 +387,12 @@ int SrsBandwidth::check_publish( | ||
| 387 | while ( (srs_get_system_time_ms() - current_time) < duration_ms ) { | 387 | while ( (srs_get_system_time_ms() - current_time) < duration_ms ) { |
| 388 | st_usleep(0); | 388 | st_usleep(0); |
| 389 | 389 | ||
| 390 | - SrsCommonMessage* msg = NULL; | ||
| 391 | - if ((ret = rtmp->recv_message(&msg)) != ERROR_SUCCESS) { | 390 | + __SrsMessage* msg = NULL; |
| 391 | + if ((ret = rtmp->__recv_message(&msg)) != ERROR_SUCCESS) { | ||
| 392 | srs_error("recv message failed. ret=%d", ret); | 392 | srs_error("recv message failed. ret=%d", ret); |
| 393 | return ret; | 393 | return ret; |
| 394 | } | 394 | } |
| 395 | - SrsAutoFree(SrsCommonMessage, msg, false); | 395 | + SrsAutoFree(__SrsMessage, msg, false); |
| 396 | 396 | ||
| 397 | // TODO: FIXME. | 397 | // TODO: FIXME. |
| 398 | publish_bytes += msg->header.payload_length; | 398 | publish_bytes += msg->header.payload_length; |
| @@ -420,8 +420,7 @@ int SrsBandwidth::check_publish( | @@ -420,8 +420,7 @@ int SrsBandwidth::check_publish( | ||
| 420 | pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms)); | 420 | pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms)); |
| 421 | pkt->data->set("bytes_delta", SrsAmf0Any::number(publish_bytes)); | 421 | pkt->data->set("bytes_delta", SrsAmf0Any::number(publish_bytes)); |
| 422 | 422 | ||
| 423 | - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0); | ||
| 424 | - if ((ret = rtmp->send_message(msg)) != ERROR_SUCCESS) { | 423 | + if ((ret = rtmp->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 425 | srs_error("send bandwidth check stop publish message failed. ret=%d", ret); | 424 | srs_error("send bandwidth check stop publish message failed. ret=%d", ret); |
| 426 | return ret; | 425 | return ret; |
| 427 | } | 426 | } |
| @@ -436,13 +435,14 @@ int SrsBandwidth::check_publish( | @@ -436,13 +435,14 @@ int SrsBandwidth::check_publish( | ||
| 436 | // TODO: FIXME: check whether flash client. | 435 | // TODO: FIXME: check whether flash client. |
| 437 | while (false) { | 436 | while (false) { |
| 438 | // recv client's stop publish response. | 437 | // recv client's stop publish response. |
| 439 | - SrsCommonMessage* msg = NULL; | 438 | + __SrsMessage* msg = NULL; |
| 440 | SrsBandwidthPacket* pkt = NULL; | 439 | SrsBandwidthPacket* pkt = NULL; |
| 441 | - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 440 | + if ((ret = __srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 442 | srs_error("expect bandwidth message failed. ret=%d", ret); | 441 | srs_error("expect bandwidth message failed. ret=%d", ret); |
| 443 | return ret; | 442 | return ret; |
| 444 | } | 443 | } |
| 445 | - SrsAutoFree(SrsCommonMessage, msg, false); | 444 | + SrsAutoFree(__SrsMessage, msg, false); |
| 445 | + SrsAutoFree(SrsBandwidthPacket, pkt, false); | ||
| 446 | srs_info("get bandwidth message succes."); | 446 | srs_info("get bandwidth message succes."); |
| 447 | 447 | ||
| 448 | if (pkt->is_stopped_publish()) { | 448 | if (pkt->is_stopped_publish()) { |
| @@ -315,8 +315,8 @@ int SrsForwarder::forward() | @@ -315,8 +315,8 @@ int SrsForwarder::forward() | ||
| 315 | 315 | ||
| 316 | // read from client. | 316 | // read from client. |
| 317 | if (true) { | 317 | if (true) { |
| 318 | - SrsCommonMessage* msg = NULL; | ||
| 319 | - ret = client->recv_message(&msg); | 318 | + __SrsMessage* msg = NULL; |
| 319 | + ret = client->__recv_message(&msg); | ||
| 320 | 320 | ||
| 321 | srs_verbose("play loop recv message. ret=%d", ret); | 321 | srs_verbose("play loop recv message. ret=%d", ret); |
| 322 | if (ret != ERROR_SUCCESS && ret != ERROR_SOCKET_TIMEOUT) { | 322 | if (ret != ERROR_SUCCESS && ret != ERROR_SOCKET_TIMEOUT) { |
| @@ -713,6 +713,7 @@ int SrsRtmpConn::process_publish_message(SrsSource* source, __SrsMessage* msg, b | @@ -713,6 +713,7 @@ int SrsRtmpConn::process_publish_message(SrsSource* source, __SrsMessage* msg, b | ||
| 713 | srs_error("source process audio message failed. ret=%d", ret); | 713 | srs_error("source process audio message failed. ret=%d", ret); |
| 714 | return ret; | 714 | return ret; |
| 715 | } | 715 | } |
| 716 | + return ret; | ||
| 716 | } | 717 | } |
| 717 | // process video packet | 718 | // process video packet |
| 718 | if (msg->header.is_video()) { | 719 | if (msg->header.is_video()) { |
| @@ -720,6 +721,7 @@ int SrsRtmpConn::process_publish_message(SrsSource* source, __SrsMessage* msg, b | @@ -720,6 +721,7 @@ int SrsRtmpConn::process_publish_message(SrsSource* source, __SrsMessage* msg, b | ||
| 720 | srs_error("source process video message failed. ret=%d", ret); | 721 | srs_error("source process video message failed. ret=%d", ret); |
| 721 | return ret; | 722 | return ret; |
| 722 | } | 723 | } |
| 724 | + return ret; | ||
| 723 | } | 725 | } |
| 724 | 726 | ||
| 725 | // process onMetaData | 727 | // process onMetaData |
| @@ -283,15 +283,15 @@ int srs_read_packet(srs_rtmp_t rtmp, int* type, u_int32_t* timestamp, char** dat | @@ -283,15 +283,15 @@ int srs_read_packet(srs_rtmp_t rtmp, int* type, u_int32_t* timestamp, char** dat | ||
| 283 | Context* context = (Context*)rtmp; | 283 | Context* context = (Context*)rtmp; |
| 284 | 284 | ||
| 285 | for (;;) { | 285 | for (;;) { |
| 286 | - SrsCommonMessage* msg = NULL; | ||
| 287 | - if ((ret = context->rtmp->recv_message(&msg)) != ERROR_SUCCESS) { | 286 | + __SrsMessage* msg = NULL; |
| 287 | + if ((ret = context->rtmp->__recv_message(&msg)) != ERROR_SUCCESS) { | ||
| 288 | return ret; | 288 | return ret; |
| 289 | } | 289 | } |
| 290 | if (!msg) { | 290 | if (!msg) { |
| 291 | continue; | 291 | continue; |
| 292 | } | 292 | } |
| 293 | 293 | ||
| 294 | - SrsAutoFree(SrsCommonMessage, msg, false); | 294 | + SrsAutoFree(__SrsMessage, msg, false); |
| 295 | 295 | ||
| 296 | if (msg->header.is_audio()) { | 296 | if (msg->header.is_audio()) { |
| 297 | *type = SRS_RTMP_TYPE_AUDIO; | 297 | *type = SRS_RTMP_TYPE_AUDIO; |
| @@ -332,13 +332,13 @@ int srs_write_packet(srs_rtmp_t rtmp, int type, u_int32_t timestamp, char* data, | @@ -332,13 +332,13 @@ int srs_write_packet(srs_rtmp_t rtmp, int type, u_int32_t timestamp, char* data, | ||
| 332 | srs_assert(rtmp != NULL); | 332 | srs_assert(rtmp != NULL); |
| 333 | Context* context = (Context*)rtmp; | 333 | Context* context = (Context*)rtmp; |
| 334 | 334 | ||
| 335 | - SrsSharedPtrMessage* msg = NULL; | 335 | + __SrsSharedPtrMessage* msg = NULL; |
| 336 | 336 | ||
| 337 | if (type == SRS_RTMP_TYPE_AUDIO) { | 337 | if (type == SRS_RTMP_TYPE_AUDIO) { |
| 338 | SrsMessageHeader header; | 338 | SrsMessageHeader header; |
| 339 | header.initialize_audio(size, timestamp, context->stream_id); | 339 | header.initialize_audio(size, timestamp, context->stream_id); |
| 340 | 340 | ||
| 341 | - msg = new SrsSharedPtrMessage(); | 341 | + msg = new __SrsSharedPtrMessage(); |
| 342 | if ((ret = msg->initialize(&header, data, size)) != ERROR_SUCCESS) { | 342 | if ((ret = msg->initialize(&header, data, size)) != ERROR_SUCCESS) { |
| 343 | srs_freepa(data); | 343 | srs_freepa(data); |
| 344 | return ret; | 344 | return ret; |
| @@ -347,7 +347,7 @@ int srs_write_packet(srs_rtmp_t rtmp, int type, u_int32_t timestamp, char* data, | @@ -347,7 +347,7 @@ int srs_write_packet(srs_rtmp_t rtmp, int type, u_int32_t timestamp, char* data, | ||
| 347 | SrsMessageHeader header; | 347 | SrsMessageHeader header; |
| 348 | header.initialize_video(size, timestamp, context->stream_id); | 348 | header.initialize_video(size, timestamp, context->stream_id); |
| 349 | 349 | ||
| 350 | - msg = new SrsSharedPtrMessage(); | 350 | + msg = new __SrsSharedPtrMessage(); |
| 351 | if ((ret = msg->initialize(&header, data, size)) != ERROR_SUCCESS) { | 351 | if ((ret = msg->initialize(&header, data, size)) != ERROR_SUCCESS) { |
| 352 | srs_freepa(data); | 352 | srs_freepa(data); |
| 353 | return ret; | 353 | return ret; |
| @@ -356,7 +356,7 @@ int srs_write_packet(srs_rtmp_t rtmp, int type, u_int32_t timestamp, char* data, | @@ -356,7 +356,7 @@ int srs_write_packet(srs_rtmp_t rtmp, int type, u_int32_t timestamp, char* data, | ||
| 356 | SrsMessageHeader header; | 356 | SrsMessageHeader header; |
| 357 | header.initialize_amf0_script(size, context->stream_id); | 357 | header.initialize_amf0_script(size, context->stream_id); |
| 358 | 358 | ||
| 359 | - msg = new SrsSharedPtrMessage(); | 359 | + msg = new __SrsSharedPtrMessage(); |
| 360 | if ((ret = msg->initialize(&header, data, size)) != ERROR_SUCCESS) { | 360 | if ((ret = msg->initialize(&header, data, size)) != ERROR_SUCCESS) { |
| 361 | srs_freepa(data); | 361 | srs_freepa(data); |
| 362 | return ret; | 362 | return ret; |
| @@ -365,7 +365,7 @@ int srs_write_packet(srs_rtmp_t rtmp, int type, u_int32_t timestamp, char* data, | @@ -365,7 +365,7 @@ int srs_write_packet(srs_rtmp_t rtmp, int type, u_int32_t timestamp, char* data, | ||
| 365 | 365 | ||
| 366 | if (msg) { | 366 | if (msg) { |
| 367 | // send out encoded msg. | 367 | // send out encoded msg. |
| 368 | - if ((ret = context->rtmp->send_message(msg)) != ERROR_SUCCESS) { | 368 | + if ((ret = context->rtmp->__send_and_free_message(msg)) != ERROR_SUCCESS) { |
| 369 | return ret; | 369 | return ret; |
| 370 | } | 370 | } |
| 371 | } else { | 371 | } else { |
| @@ -341,13 +341,14 @@ int SrsBandCheckClient::expect_start_play() | @@ -341,13 +341,14 @@ int SrsBandCheckClient::expect_start_play() | ||
| 341 | int ret = ERROR_SUCCESS; | 341 | int ret = ERROR_SUCCESS; |
| 342 | 342 | ||
| 343 | // expect connect _result | 343 | // expect connect _result |
| 344 | - SrsCommonMessage* msg = NULL; | 344 | + __SrsMessage* msg = NULL; |
| 345 | SrsBandwidthPacket* pkt = NULL; | 345 | SrsBandwidthPacket* pkt = NULL; |
| 346 | - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 346 | + if ((ret = __srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 347 | srs_error("expect bandcheck start play message failed. ret=%d", ret); | 347 | srs_error("expect bandcheck start play message failed. ret=%d", ret); |
| 348 | return ret; | 348 | return ret; |
| 349 | } | 349 | } |
| 350 | - SrsAutoFree(SrsCommonMessage, msg, false); | 350 | + SrsAutoFree(__SrsMessage, msg, false); |
| 351 | + SrsAutoFree(SrsBandwidthPacket, pkt, false); | ||
| 351 | srs_info("get bandcheck start play message"); | 352 | srs_info("get bandcheck start play message"); |
| 352 | 353 | ||
| 353 | if (pkt->command_name != SRS_BW_CHECK_START_PLAY) { | 354 | if (pkt->command_name != SRS_BW_CHECK_START_PLAY) { |
| @@ -362,12 +363,9 @@ int SrsBandCheckClient::send_starting_play() | @@ -362,12 +363,9 @@ int SrsBandCheckClient::send_starting_play() | ||
| 362 | { | 363 | { |
| 363 | int ret = ERROR_SUCCESS; | 364 | int ret = ERROR_SUCCESS; |
| 364 | 365 | ||
| 365 | - SrsCommonMessage* msg = new SrsCommonMessage; | ||
| 366 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; | 366 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; |
| 367 | pkt->command_name = SRS_BW_CHECK_STARTING_PLAY; | 367 | pkt->command_name = SRS_BW_CHECK_STARTING_PLAY; |
| 368 | - msg->set_packet(pkt, 0); | ||
| 369 | - | ||
| 370 | - if ((ret = send_message(msg)) != ERROR_SUCCESS) { | 368 | + if ((ret = __send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 371 | srs_error("send starting play msg failed. ret=%d", ret); | 369 | srs_error("send starting play msg failed. ret=%d", ret); |
| 372 | return ret; | 370 | return ret; |
| 373 | } | 371 | } |
| @@ -380,13 +378,14 @@ int SrsBandCheckClient::expect_stop_play() | @@ -380,13 +378,14 @@ int SrsBandCheckClient::expect_stop_play() | ||
| 380 | int ret = ERROR_SUCCESS; | 378 | int ret = ERROR_SUCCESS; |
| 381 | 379 | ||
| 382 | while (true) { | 380 | while (true) { |
| 383 | - SrsCommonMessage* msg = NULL; | 381 | + __SrsMessage* msg = NULL; |
| 384 | SrsBandwidthPacket* pkt = NULL; | 382 | SrsBandwidthPacket* pkt = NULL; |
| 385 | - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 383 | + if ((ret = __srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 386 | srs_error("expect stop play message failed. ret=%d", ret); | 384 | srs_error("expect stop play message failed. ret=%d", ret); |
| 387 | return ret; | 385 | return ret; |
| 388 | } | 386 | } |
| 389 | - SrsAutoFree(SrsCommonMessage, msg, false); | 387 | + SrsAutoFree(__SrsMessage, msg, false); |
| 388 | + SrsAutoFree(SrsBandwidthPacket, pkt, false); | ||
| 390 | srs_info("get bandcheck stop play message"); | 389 | srs_info("get bandcheck stop play message"); |
| 391 | 390 | ||
| 392 | if (pkt->command_name == SRS_BW_CHECK_STOP_PLAY) { | 391 | if (pkt->command_name == SRS_BW_CHECK_STOP_PLAY) { |
| @@ -401,12 +400,9 @@ int SrsBandCheckClient::send_stopped_play() | @@ -401,12 +400,9 @@ int SrsBandCheckClient::send_stopped_play() | ||
| 401 | { | 400 | { |
| 402 | int ret = ERROR_SUCCESS; | 401 | int ret = ERROR_SUCCESS; |
| 403 | 402 | ||
| 404 | - SrsCommonMessage* msg = new SrsCommonMessage; | ||
| 405 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; | 403 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; |
| 406 | pkt->command_name = SRS_BW_CHECK_STOPPED_PLAY; | 404 | pkt->command_name = SRS_BW_CHECK_STOPPED_PLAY; |
| 407 | - msg->set_packet(pkt, 0); | ||
| 408 | - | ||
| 409 | - if ((ret = send_message(msg)) != ERROR_SUCCESS) { | 405 | + if ((ret = __send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 410 | srs_error("send stopped play msg failed. ret=%d", ret); | 406 | srs_error("send stopped play msg failed. ret=%d", ret); |
| 411 | return ret; | 407 | return ret; |
| 412 | } | 408 | } |
| @@ -419,13 +415,14 @@ int SrsBandCheckClient::expect_start_pub() | @@ -419,13 +415,14 @@ int SrsBandCheckClient::expect_start_pub() | ||
| 419 | int ret = ERROR_SUCCESS; | 415 | int ret = ERROR_SUCCESS; |
| 420 | 416 | ||
| 421 | while (true) { | 417 | while (true) { |
| 422 | - SrsCommonMessage* msg = NULL; | 418 | + __SrsMessage* msg = NULL; |
| 423 | SrsBandwidthPacket* pkt = NULL; | 419 | SrsBandwidthPacket* pkt = NULL; |
| 424 | - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 420 | + if ((ret = __srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 425 | srs_error("expect start pub message failed. ret=%d", ret); | 421 | srs_error("expect start pub message failed. ret=%d", ret); |
| 426 | return ret; | 422 | return ret; |
| 427 | } | 423 | } |
| 428 | - SrsAutoFree(SrsCommonMessage, msg, false); | 424 | + SrsAutoFree(__SrsMessage, msg, false); |
| 425 | + SrsAutoFree(SrsBandwidthPacket, pkt, false); | ||
| 429 | srs_info("get bandcheck start pub message"); | 426 | srs_info("get bandcheck start pub message"); |
| 430 | 427 | ||
| 431 | if (pkt->command_name == SRS_BW_CHECK_START_PUBLISH) { | 428 | if (pkt->command_name == SRS_BW_CHECK_START_PUBLISH) { |
| @@ -440,12 +437,9 @@ int SrsBandCheckClient::send_starting_pub() | @@ -440,12 +437,9 @@ int SrsBandCheckClient::send_starting_pub() | ||
| 440 | { | 437 | { |
| 441 | int ret = ERROR_SUCCESS; | 438 | int ret = ERROR_SUCCESS; |
| 442 | 439 | ||
| 443 | - SrsCommonMessage* msg = new SrsCommonMessage; | ||
| 444 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; | 440 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; |
| 445 | pkt->command_name = SRS_BW_CHECK_STARTING_PUBLISH; | 441 | pkt->command_name = SRS_BW_CHECK_STARTING_PUBLISH; |
| 446 | - msg->set_packet(pkt, 0); | ||
| 447 | - | ||
| 448 | - if ((ret = send_message(msg)) != ERROR_SUCCESS) { | 442 | + if ((ret = __send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 449 | srs_error("send starting play msg failed. ret=%d", ret); | 443 | srs_error("send starting play msg failed. ret=%d", ret); |
| 450 | return ret; | 444 | return ret; |
| 451 | } | 445 | } |
| @@ -460,10 +454,8 @@ int SrsBandCheckClient::send_pub_data() | @@ -460,10 +454,8 @@ int SrsBandCheckClient::send_pub_data() | ||
| 460 | 454 | ||
| 461 | int data_count = 100; | 455 | int data_count = 100; |
| 462 | while (true) { | 456 | while (true) { |
| 463 | - SrsCommonMessage* msg = new SrsCommonMessage; | ||
| 464 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; | 457 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; |
| 465 | pkt->command_name = SRS_BW_CHECK_PUBLISHING; | 458 | pkt->command_name = SRS_BW_CHECK_PUBLISHING; |
| 466 | - msg->set_packet(pkt, 0); | ||
| 467 | 459 | ||
| 468 | for (int i = 0; i < data_count; ++i) { | 460 | for (int i = 0; i < data_count; ++i) { |
| 469 | std::stringstream seq; | 461 | std::stringstream seq; |
| @@ -473,7 +465,7 @@ int SrsBandCheckClient::send_pub_data() | @@ -473,7 +465,7 @@ int SrsBandCheckClient::send_pub_data() | ||
| 473 | } | 465 | } |
| 474 | data_count += 100; | 466 | data_count += 100; |
| 475 | 467 | ||
| 476 | - if ((ret = send_message(msg)) != ERROR_SUCCESS) { | 468 | + if ((ret = __send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 477 | srs_error("send publish message failed.ret=%d", ret); | 469 | srs_error("send publish message failed.ret=%d", ret); |
| 478 | return ret; | 470 | return ret; |
| 479 | } | 471 | } |
| @@ -493,11 +485,13 @@ int SrsBandCheckClient::expect_stop_pub() | @@ -493,11 +485,13 @@ int SrsBandCheckClient::expect_stop_pub() | ||
| 493 | this->set_recv_timeout(1000 * 1000); | 485 | this->set_recv_timeout(1000 * 1000); |
| 494 | this->set_send_timeout(1000 * 1000); | 486 | this->set_send_timeout(1000 * 1000); |
| 495 | 487 | ||
| 496 | - SrsCommonMessage* msg; | 488 | + __SrsMessage* msg; |
| 497 | SrsBandwidthPacket* pkt; | 489 | SrsBandwidthPacket* pkt; |
| 498 | - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(this->protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 490 | + if ((ret = __srs_rtmp_expect_message<SrsBandwidthPacket>(this->protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 499 | return ret; | 491 | return ret; |
| 500 | } | 492 | } |
| 493 | + SrsAutoFree(__SrsMessage, msg, false); | ||
| 494 | + SrsAutoFree(SrsBandwidthPacket, pkt, false); | ||
| 501 | if (pkt->command_name == SRS_BW_CHECK_STOP_PUBLISH) { | 495 | if (pkt->command_name == SRS_BW_CHECK_STOP_PUBLISH) { |
| 502 | return ret; | 496 | return ret; |
| 503 | } | 497 | } |
| @@ -510,13 +504,14 @@ int SrsBandCheckClient::expect_finished() | @@ -510,13 +504,14 @@ int SrsBandCheckClient::expect_finished() | ||
| 510 | int ret = ERROR_SUCCESS; | 504 | int ret = ERROR_SUCCESS; |
| 511 | 505 | ||
| 512 | while (true) { | 506 | while (true) { |
| 513 | - SrsCommonMessage* msg = NULL; | 507 | + __SrsMessage* msg = NULL; |
| 514 | SrsBandwidthPacket* pkt = NULL; | 508 | SrsBandwidthPacket* pkt = NULL; |
| 515 | - if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 509 | + if ((ret = __srs_rtmp_expect_message<SrsBandwidthPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 516 | srs_error("expect finished message failed. ret=%d", ret); | 510 | srs_error("expect finished message failed. ret=%d", ret); |
| 517 | return ret; | 511 | return ret; |
| 518 | } | 512 | } |
| 519 | - SrsAutoFree(SrsCommonMessage, msg, false); | 513 | + SrsAutoFree(__SrsMessage, msg, false); |
| 514 | + SrsAutoFree(SrsBandwidthPacket, pkt, false); | ||
| 520 | srs_info("get bandcheck finished message"); | 515 | srs_info("get bandcheck finished message"); |
| 521 | 516 | ||
| 522 | if (pkt->command_name == SRS_BW_CHECK_FINISHED) { | 517 | if (pkt->command_name == SRS_BW_CHECK_FINISHED) { |
| @@ -596,12 +591,9 @@ int SrsBandCheckClient::send_stopped_pub() | @@ -596,12 +591,9 @@ int SrsBandCheckClient::send_stopped_pub() | ||
| 596 | { | 591 | { |
| 597 | int ret = ERROR_SUCCESS; | 592 | int ret = ERROR_SUCCESS; |
| 598 | 593 | ||
| 599 | - SrsCommonMessage* msg = new SrsCommonMessage; | ||
| 600 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; | 594 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; |
| 601 | pkt->command_name = SRS_BW_CHECK_STOPPED_PUBLISH; | 595 | pkt->command_name = SRS_BW_CHECK_STOPPED_PUBLISH; |
| 602 | - msg->set_packet(pkt, 0); | ||
| 603 | - | ||
| 604 | - if ((ret = send_message(msg)) != ERROR_SUCCESS) { | 596 | + if ((ret = __send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 605 | srs_error("send stopped pub msg failed. ret=%d", ret); | 597 | srs_error("send stopped pub msg failed. ret=%d", ret); |
| 606 | return ret; | 598 | return ret; |
| 607 | } | 599 | } |
| @@ -614,12 +606,9 @@ int SrsBandCheckClient::send_final() | @@ -614,12 +606,9 @@ int SrsBandCheckClient::send_final() | ||
| 614 | { | 606 | { |
| 615 | int ret = ERROR_SUCCESS; | 607 | int ret = ERROR_SUCCESS; |
| 616 | 608 | ||
| 617 | - SrsCommonMessage* msg = new SrsCommonMessage; | ||
| 618 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; | 609 | SrsBandwidthPacket* pkt = new SrsBandwidthPacket; |
| 619 | pkt->command_name = SRS_BW_CHECK_FLASH_FINAL; | 610 | pkt->command_name = SRS_BW_CHECK_FLASH_FINAL; |
| 620 | - msg->set_packet(pkt, 0); | ||
| 621 | - | ||
| 622 | - if ((ret = send_message(msg)) != ERROR_SUCCESS) { | 611 | + if ((ret = __send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 623 | srs_error("send final msg failed. ret=%d", ret); | 612 | srs_error("send final msg failed. ret=%d", ret); |
| 624 | return ret; | 613 | return ret; |
| 625 | } | 614 | } |
| @@ -372,16 +372,6 @@ int SrsRtmpClient::get_send_kbps() | @@ -372,16 +372,6 @@ int SrsRtmpClient::get_send_kbps() | ||
| 372 | return protocol->get_send_kbps(); | 372 | return protocol->get_send_kbps(); |
| 373 | } | 373 | } |
| 374 | 374 | ||
| 375 | -int SrsRtmpClient::recv_message(SrsCommonMessage** pmsg) | ||
| 376 | -{ | ||
| 377 | - return protocol->recv_message(pmsg); | ||
| 378 | -} | ||
| 379 | - | ||
| 380 | -int SrsRtmpClient::send_message(ISrsMessage* msg) | ||
| 381 | -{ | ||
| 382 | - return protocol->send_message(msg); | ||
| 383 | -} | ||
| 384 | - | ||
| 385 | int SrsRtmpClient::__recv_message(__SrsMessage** pmsg) | 375 | int SrsRtmpClient::__recv_message(__SrsMessage** pmsg) |
| 386 | { | 376 | { |
| 387 | return protocol->__recv_message(pmsg); | 377 | return protocol->__recv_message(pmsg); |
| @@ -462,9 +452,7 @@ int SrsRtmpClient::connect_app(string app, string tc_url) | @@ -462,9 +452,7 @@ int SrsRtmpClient::connect_app(string app, string tc_url) | ||
| 462 | 452 | ||
| 463 | // Connect(vhost, app) | 453 | // Connect(vhost, app) |
| 464 | if (true) { | 454 | if (true) { |
| 465 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 466 | SrsConnectAppPacket* pkt = new SrsConnectAppPacket(); | 455 | SrsConnectAppPacket* pkt = new SrsConnectAppPacket(); |
| 467 | - msg->set_packet(pkt, 0); | ||
| 468 | 456 | ||
| 469 | pkt->command_object = SrsAmf0Any::object(); | 457 | pkt->command_object = SrsAmf0Any::object(); |
| 470 | pkt->command_object->set("app", SrsAmf0Any::str(app.c_str())); | 458 | pkt->command_object->set("app", SrsAmf0Any::str(app.c_str())); |
| @@ -478,32 +466,29 @@ int SrsRtmpClient::connect_app(string app, string tc_url) | @@ -478,32 +466,29 @@ int SrsRtmpClient::connect_app(string app, string tc_url) | ||
| 478 | pkt->command_object->set("pageUrl", SrsAmf0Any::str()); | 466 | pkt->command_object->set("pageUrl", SrsAmf0Any::str()); |
| 479 | pkt->command_object->set("objectEncoding", SrsAmf0Any::number(0)); | 467 | pkt->command_object->set("objectEncoding", SrsAmf0Any::number(0)); |
| 480 | 468 | ||
| 481 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 469 | + if ((ret = protocol->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 482 | return ret; | 470 | return ret; |
| 483 | } | 471 | } |
| 484 | } | 472 | } |
| 485 | 473 | ||
| 486 | // Set Window Acknowledgement size(2500000) | 474 | // Set Window Acknowledgement size(2500000) |
| 487 | if (true) { | 475 | if (true) { |
| 488 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 489 | SrsSetWindowAckSizePacket* pkt = new SrsSetWindowAckSizePacket(); | 476 | SrsSetWindowAckSizePacket* pkt = new SrsSetWindowAckSizePacket(); |
| 490 | - | ||
| 491 | pkt->ackowledgement_window_size = 2500000; | 477 | pkt->ackowledgement_window_size = 2500000; |
| 492 | - msg->set_packet(pkt, 0); | ||
| 493 | - | ||
| 494 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 478 | + if ((ret = protocol->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 495 | return ret; | 479 | return ret; |
| 496 | } | 480 | } |
| 497 | } | 481 | } |
| 498 | 482 | ||
| 499 | // expect connect _result | 483 | // expect connect _result |
| 500 | - SrsCommonMessage* msg = NULL; | 484 | + __SrsMessage* msg = NULL; |
| 501 | SrsConnectAppResPacket* pkt = NULL; | 485 | SrsConnectAppResPacket* pkt = NULL; |
| 502 | - if ((ret = srs_rtmp_expect_message<SrsConnectAppResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 486 | + if ((ret = __srs_rtmp_expect_message<SrsConnectAppResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 503 | srs_error("expect connect app response message failed. ret=%d", ret); | 487 | srs_error("expect connect app response message failed. ret=%d", ret); |
| 504 | return ret; | 488 | return ret; |
| 505 | } | 489 | } |
| 506 | - SrsAutoFree(SrsCommonMessage, msg, false); | 490 | + SrsAutoFree(__SrsMessage, msg, false); |
| 491 | + SrsAutoFree(SrsConnectAppResPacket, pkt, false); | ||
| 507 | srs_info("get connect app response message"); | 492 | srs_info("get connect app response message"); |
| 508 | 493 | ||
| 509 | return ret; | 494 | return ret; |
| @@ -515,25 +500,22 @@ int SrsRtmpClient::create_stream(int& stream_id) | @@ -515,25 +500,22 @@ int SrsRtmpClient::create_stream(int& stream_id) | ||
| 515 | 500 | ||
| 516 | // CreateStream | 501 | // CreateStream |
| 517 | if (true) { | 502 | if (true) { |
| 518 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 519 | SrsCreateStreamPacket* pkt = new SrsCreateStreamPacket(); | 503 | SrsCreateStreamPacket* pkt = new SrsCreateStreamPacket(); |
| 520 | - | ||
| 521 | - msg->set_packet(pkt, 0); | ||
| 522 | - | ||
| 523 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 504 | + if ((ret = protocol->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 524 | return ret; | 505 | return ret; |
| 525 | } | 506 | } |
| 526 | } | 507 | } |
| 527 | 508 | ||
| 528 | // CreateStream _result. | 509 | // CreateStream _result. |
| 529 | if (true) { | 510 | if (true) { |
| 530 | - SrsCommonMessage* msg = NULL; | 511 | + __SrsMessage* msg = NULL; |
| 531 | SrsCreateStreamResPacket* pkt = NULL; | 512 | SrsCreateStreamResPacket* pkt = NULL; |
| 532 | - if ((ret = srs_rtmp_expect_message<SrsCreateStreamResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 513 | + if ((ret = __srs_rtmp_expect_message<SrsCreateStreamResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 533 | srs_error("expect create stream response message failed. ret=%d", ret); | 514 | srs_error("expect create stream response message failed. ret=%d", ret); |
| 534 | return ret; | 515 | return ret; |
| 535 | } | 516 | } |
| 536 | - SrsAutoFree(SrsCommonMessage, msg, false); | 517 | + SrsAutoFree(__SrsMessage, msg, false); |
| 518 | + SrsAutoFree(SrsCreateStreamResPacket, pkt, false); | ||
| 537 | srs_info("get create stream response message"); | 519 | srs_info("get create stream response message"); |
| 538 | 520 | ||
| 539 | stream_id = (int)pkt->stream_id; | 521 | stream_id = (int)pkt->stream_id; |
| @@ -548,13 +530,9 @@ int SrsRtmpClient::play(string stream, int stream_id) | @@ -548,13 +530,9 @@ int SrsRtmpClient::play(string stream, int stream_id) | ||
| 548 | 530 | ||
| 549 | // Play(stream) | 531 | // Play(stream) |
| 550 | if (true) { | 532 | if (true) { |
| 551 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 552 | SrsPlayPacket* pkt = new SrsPlayPacket(); | 533 | SrsPlayPacket* pkt = new SrsPlayPacket(); |
| 553 | - | ||
| 554 | pkt->stream_name = stream; | 534 | pkt->stream_name = stream; |
| 555 | - msg->set_packet(pkt, stream_id); | ||
| 556 | - | ||
| 557 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 535 | + if ((ret = protocol->__send_and_free_packet(pkt, stream_id)) != ERROR_SUCCESS) { |
| 558 | srs_error("send play stream failed. " | 536 | srs_error("send play stream failed. " |
| 559 | "stream=%s, stream_id=%d, ret=%d", | 537 | "stream=%s, stream_id=%d, ret=%d", |
| 560 | stream.c_str(), stream_id, ret); | 538 | stream.c_str(), stream_id, ret); |
| @@ -565,15 +543,13 @@ int SrsRtmpClient::play(string stream, int stream_id) | @@ -565,15 +543,13 @@ int SrsRtmpClient::play(string stream, int stream_id) | ||
| 565 | // SetBufferLength(1000ms) | 543 | // SetBufferLength(1000ms) |
| 566 | int buffer_length_ms = 1000; | 544 | int buffer_length_ms = 1000; |
| 567 | if (true) { | 545 | if (true) { |
| 568 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 569 | SrsUserControlPacket* pkt = new SrsUserControlPacket(); | 546 | SrsUserControlPacket* pkt = new SrsUserControlPacket(); |
| 570 | 547 | ||
| 571 | pkt->event_type = SrcPCUCSetBufferLength; | 548 | pkt->event_type = SrcPCUCSetBufferLength; |
| 572 | pkt->event_data = stream_id; | 549 | pkt->event_data = stream_id; |
| 573 | pkt->extra_data = buffer_length_ms; | 550 | pkt->extra_data = buffer_length_ms; |
| 574 | - msg->set_packet(pkt, 0); | ||
| 575 | 551 | ||
| 576 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 552 | + if ((ret = protocol->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 577 | srs_error("send set buffer length failed. " | 553 | srs_error("send set buffer length failed. " |
| 578 | "stream=%s, stream_id=%d, bufferLength=%d, ret=%d", | 554 | "stream=%s, stream_id=%d, bufferLength=%d, ret=%d", |
| 579 | stream.c_str(), stream_id, buffer_length_ms, ret); | 555 | stream.c_str(), stream_id, buffer_length_ms, ret); |
| @@ -583,13 +559,9 @@ int SrsRtmpClient::play(string stream, int stream_id) | @@ -583,13 +559,9 @@ int SrsRtmpClient::play(string stream, int stream_id) | ||
| 583 | 559 | ||
| 584 | // SetChunkSize | 560 | // SetChunkSize |
| 585 | if (true) { | 561 | if (true) { |
| 586 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 587 | SrsSetChunkSizePacket* pkt = new SrsSetChunkSizePacket(); | 562 | SrsSetChunkSizePacket* pkt = new SrsSetChunkSizePacket(); |
| 588 | - | ||
| 589 | pkt->chunk_size = SRS_CONF_DEFAULT_CHUNK_SIZE; | 563 | pkt->chunk_size = SRS_CONF_DEFAULT_CHUNK_SIZE; |
| 590 | - msg->set_packet(pkt, 0); | ||
| 591 | - | ||
| 592 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 564 | + if ((ret = protocol->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 593 | srs_error("send set chunk size failed. " | 565 | srs_error("send set chunk size failed. " |
| 594 | "stream=%s, chunk_size=%d, ret=%d", | 566 | "stream=%s, chunk_size=%d, ret=%d", |
| 595 | stream.c_str(), SRS_CONF_DEFAULT_CHUNK_SIZE, ret); | 567 | stream.c_str(), SRS_CONF_DEFAULT_CHUNK_SIZE, ret); |
| @@ -606,13 +578,9 @@ int SrsRtmpClient::publish(string stream, int stream_id) | @@ -606,13 +578,9 @@ int SrsRtmpClient::publish(string stream, int stream_id) | ||
| 606 | 578 | ||
| 607 | // SetChunkSize | 579 | // SetChunkSize |
| 608 | if (true) { | 580 | if (true) { |
| 609 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 610 | SrsSetChunkSizePacket* pkt = new SrsSetChunkSizePacket(); | 581 | SrsSetChunkSizePacket* pkt = new SrsSetChunkSizePacket(); |
| 611 | - | ||
| 612 | pkt->chunk_size = SRS_CONF_DEFAULT_CHUNK_SIZE; | 582 | pkt->chunk_size = SRS_CONF_DEFAULT_CHUNK_SIZE; |
| 613 | - msg->set_packet(pkt, 0); | ||
| 614 | - | ||
| 615 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 583 | + if ((ret = protocol->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 616 | srs_error("send set chunk size failed. " | 584 | srs_error("send set chunk size failed. " |
| 617 | "stream=%s, chunk_size=%d, ret=%d", | 585 | "stream=%s, chunk_size=%d, ret=%d", |
| 618 | stream.c_str(), SRS_CONF_DEFAULT_CHUNK_SIZE, ret); | 586 | stream.c_str(), SRS_CONF_DEFAULT_CHUNK_SIZE, ret); |
| @@ -622,13 +590,9 @@ int SrsRtmpClient::publish(string stream, int stream_id) | @@ -622,13 +590,9 @@ int SrsRtmpClient::publish(string stream, int stream_id) | ||
| 622 | 590 | ||
| 623 | // publish(stream) | 591 | // publish(stream) |
| 624 | if (true) { | 592 | if (true) { |
| 625 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 626 | SrsPublishPacket* pkt = new SrsPublishPacket(); | 593 | SrsPublishPacket* pkt = new SrsPublishPacket(); |
| 627 | - | ||
| 628 | pkt->stream_name = stream; | 594 | pkt->stream_name = stream; |
| 629 | - msg->set_packet(pkt, stream_id); | ||
| 630 | - | ||
| 631 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 595 | + if ((ret = protocol->__send_and_free_packet(pkt, stream_id)) != ERROR_SUCCESS) { |
| 632 | srs_error("send publish message failed. " | 596 | srs_error("send publish message failed. " |
| 633 | "stream=%s, stream_id=%d, ret=%d", | 597 | "stream=%s, stream_id=%d, ret=%d", |
| 634 | stream.c_str(), stream_id, ret); | 598 | stream.c_str(), stream_id, ret); |
| @@ -647,12 +611,8 @@ int SrsRtmpClient::fmle_publish(string stream, int& stream_id) | @@ -647,12 +611,8 @@ int SrsRtmpClient::fmle_publish(string stream, int& stream_id) | ||
| 647 | 611 | ||
| 648 | // SrsFMLEStartPacket | 612 | // SrsFMLEStartPacket |
| 649 | if (true) { | 613 | if (true) { |
| 650 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 651 | SrsFMLEStartPacket* pkt = SrsFMLEStartPacket::create_release_stream(stream); | 614 | SrsFMLEStartPacket* pkt = SrsFMLEStartPacket::create_release_stream(stream); |
| 652 | - | ||
| 653 | - msg->set_packet(pkt, 0); | ||
| 654 | - | ||
| 655 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 615 | + if ((ret = protocol->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 656 | srs_error("send FMLE publish " | 616 | srs_error("send FMLE publish " |
| 657 | "release stream failed. stream=%s, ret=%d", stream.c_str(), ret); | 617 | "release stream failed. stream=%s, ret=%d", stream.c_str(), ret); |
| 658 | return ret; | 618 | return ret; |
| @@ -661,12 +621,8 @@ int SrsRtmpClient::fmle_publish(string stream, int& stream_id) | @@ -661,12 +621,8 @@ int SrsRtmpClient::fmle_publish(string stream, int& stream_id) | ||
| 661 | 621 | ||
| 662 | // FCPublish | 622 | // FCPublish |
| 663 | if (true) { | 623 | if (true) { |
| 664 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 665 | SrsFMLEStartPacket* pkt = SrsFMLEStartPacket::create_FC_publish(stream); | 624 | SrsFMLEStartPacket* pkt = SrsFMLEStartPacket::create_FC_publish(stream); |
| 666 | - | ||
| 667 | - msg->set_packet(pkt, 0); | ||
| 668 | - | ||
| 669 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 625 | + if ((ret = protocol->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 670 | srs_error("send FMLE publish " | 626 | srs_error("send FMLE publish " |
| 671 | "FCPublish failed. stream=%s, ret=%d", stream.c_str(), ret); | 627 | "FCPublish failed. stream=%s, ret=%d", stream.c_str(), ret); |
| 672 | return ret; | 628 | return ret; |
| @@ -675,13 +631,9 @@ int SrsRtmpClient::fmle_publish(string stream, int& stream_id) | @@ -675,13 +631,9 @@ int SrsRtmpClient::fmle_publish(string stream, int& stream_id) | ||
| 675 | 631 | ||
| 676 | // CreateStream | 632 | // CreateStream |
| 677 | if (true) { | 633 | if (true) { |
| 678 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 679 | SrsCreateStreamPacket* pkt = new SrsCreateStreamPacket(); | 634 | SrsCreateStreamPacket* pkt = new SrsCreateStreamPacket(); |
| 680 | - | ||
| 681 | pkt->transaction_id = 4; | 635 | pkt->transaction_id = 4; |
| 682 | - msg->set_packet(pkt, 0); | ||
| 683 | - | ||
| 684 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 636 | + if ((ret = protocol->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 685 | srs_error("send FMLE publish " | 637 | srs_error("send FMLE publish " |
| 686 | "createStream failed. stream=%s, ret=%d", stream.c_str(), ret); | 638 | "createStream failed. stream=%s, ret=%d", stream.c_str(), ret); |
| 687 | return ret; | 639 | return ret; |
| @@ -690,13 +642,14 @@ int SrsRtmpClient::fmle_publish(string stream, int& stream_id) | @@ -690,13 +642,14 @@ int SrsRtmpClient::fmle_publish(string stream, int& stream_id) | ||
| 690 | 642 | ||
| 691 | // expect result of CreateStream | 643 | // expect result of CreateStream |
| 692 | if (true) { | 644 | if (true) { |
| 693 | - SrsCommonMessage* msg = NULL; | 645 | + __SrsMessage* msg = NULL; |
| 694 | SrsCreateStreamResPacket* pkt = NULL; | 646 | SrsCreateStreamResPacket* pkt = NULL; |
| 695 | - if ((ret = srs_rtmp_expect_message<SrsCreateStreamResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { | 647 | + if ((ret = __srs_rtmp_expect_message<SrsCreateStreamResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) { |
| 696 | srs_error("expect create stream response message failed. ret=%d", ret); | 648 | srs_error("expect create stream response message failed. ret=%d", ret); |
| 697 | return ret; | 649 | return ret; |
| 698 | } | 650 | } |
| 699 | - SrsAutoFree(SrsCommonMessage, msg, false); | 651 | + SrsAutoFree(__SrsMessage, msg, false); |
| 652 | + SrsAutoFree(SrsCreateStreamResPacket, pkt, false); | ||
| 700 | srs_info("get create stream response message"); | 653 | srs_info("get create stream response message"); |
| 701 | 654 | ||
| 702 | stream_id = (int)pkt->stream_id; | 655 | stream_id = (int)pkt->stream_id; |
| @@ -704,13 +657,9 @@ int SrsRtmpClient::fmle_publish(string stream, int& stream_id) | @@ -704,13 +657,9 @@ int SrsRtmpClient::fmle_publish(string stream, int& stream_id) | ||
| 704 | 657 | ||
| 705 | // publish(stream) | 658 | // publish(stream) |
| 706 | if (true) { | 659 | if (true) { |
| 707 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 708 | SrsPublishPacket* pkt = new SrsPublishPacket(); | 660 | SrsPublishPacket* pkt = new SrsPublishPacket(); |
| 709 | - | ||
| 710 | pkt->stream_name = stream; | 661 | pkt->stream_name = stream; |
| 711 | - msg->set_packet(pkt, stream_id); | ||
| 712 | - | ||
| 713 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 662 | + if ((ret = protocol->__send_and_free_packet(pkt, stream_id)) != ERROR_SUCCESS) { |
| 714 | srs_error("send FMLE publish publish failed. " | 663 | srs_error("send FMLE publish publish failed. " |
| 715 | "stream=%s, stream_id=%d, ret=%d", stream.c_str(), stream_id, ret); | 664 | "stream=%s, stream_id=%d, ret=%d", stream.c_str(), stream_id, ret); |
| 716 | return ret; | 665 | return ret; |
| @@ -778,16 +727,6 @@ int SrsRtmpServer::get_send_kbps() | @@ -778,16 +727,6 @@ int SrsRtmpServer::get_send_kbps() | ||
| 778 | return protocol->get_send_kbps(); | 727 | return protocol->get_send_kbps(); |
| 779 | } | 728 | } |
| 780 | 729 | ||
| 781 | -int SrsRtmpServer::recv_message(SrsCommonMessage** pmsg) | ||
| 782 | -{ | ||
| 783 | - return protocol->recv_message(pmsg); | ||
| 784 | -} | ||
| 785 | - | ||
| 786 | -int SrsRtmpServer::send_message(ISrsMessage* msg) | ||
| 787 | -{ | ||
| 788 | - return protocol->send_message(msg); | ||
| 789 | -} | ||
| 790 | - | ||
| 791 | int SrsRtmpServer::__recv_message(__SrsMessage** pmsg) | 730 | int SrsRtmpServer::__recv_message(__SrsMessage** pmsg) |
| 792 | { | 731 | { |
| 793 | return protocol->__recv_message(pmsg); | 732 | return protocol->__recv_message(pmsg); |
| @@ -841,6 +780,7 @@ int SrsRtmpServer::connect_app(SrsRequest* req) | @@ -841,6 +780,7 @@ int SrsRtmpServer::connect_app(SrsRequest* req) | ||
| 841 | return ret; | 780 | return ret; |
| 842 | } | 781 | } |
| 843 | SrsAutoFree(__SrsMessage, msg, false); | 782 | SrsAutoFree(__SrsMessage, msg, false); |
| 783 | + SrsAutoFree(SrsConnectAppPacket, pkt, false); | ||
| 844 | srs_info("get connect app message"); | 784 | srs_info("get connect app message"); |
| 845 | 785 | ||
| 846 | SrsAmf0Any* prop = NULL; | 786 | SrsAmf0Any* prop = NULL; |
| @@ -954,8 +894,7 @@ void SrsRtmpServer::response_connect_reject(SrsRequest *req, const char* desc) | @@ -954,8 +894,7 @@ void SrsRtmpServer::response_connect_reject(SrsRequest *req, const char* desc) | ||
| 954 | pkt->props->set(StatusDescription, SrsAmf0Any::str(desc)); | 894 | pkt->props->set(StatusDescription, SrsAmf0Any::str(desc)); |
| 955 | //pkt->props->set("objectEncoding", SrsAmf0Any::number(req->objectEncoding)); | 895 | //pkt->props->set("objectEncoding", SrsAmf0Any::number(req->objectEncoding)); |
| 956 | 896 | ||
| 957 | - SrsCommonMessage* msg = (new SrsCommonMessage())->set_packet(pkt, 0); | ||
| 958 | - if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { | 897 | + if ((ret = protocol->__send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 959 | srs_error("send connect app response rejected message failed. ret=%d", ret); | 898 | srs_error("send connect app response rejected message failed. ret=%d", ret); |
| 960 | return; | 899 | return; |
| 961 | } | 900 | } |
| @@ -1197,6 +1136,8 @@ int SrsRtmpServer::start_fmle_publish(int stream_id) | @@ -1197,6 +1136,8 @@ int SrsRtmpServer::start_fmle_publish(int stream_id) | ||
| 1197 | srs_info("recv FCPublish request message success."); | 1136 | srs_info("recv FCPublish request message success."); |
| 1198 | 1137 | ||
| 1199 | SrsAutoFree(__SrsMessage, msg, false); | 1138 | SrsAutoFree(__SrsMessage, msg, false); |
| 1139 | + SrsAutoFree(SrsFMLEStartPacket, pkt, false); | ||
| 1140 | + | ||
| 1200 | fc_publish_tid = pkt->transaction_id; | 1141 | fc_publish_tid = pkt->transaction_id; |
| 1201 | } | 1142 | } |
| 1202 | // FCPublish response | 1143 | // FCPublish response |
| @@ -1221,6 +1162,8 @@ int SrsRtmpServer::start_fmle_publish(int stream_id) | @@ -1221,6 +1162,8 @@ int SrsRtmpServer::start_fmle_publish(int stream_id) | ||
| 1221 | srs_info("recv createStream request message success."); | 1162 | srs_info("recv createStream request message success."); |
| 1222 | 1163 | ||
| 1223 | SrsAutoFree(__SrsMessage, msg, false); | 1164 | SrsAutoFree(__SrsMessage, msg, false); |
| 1165 | + SrsAutoFree(SrsCreateStreamPacket, pkt, false); | ||
| 1166 | + | ||
| 1224 | create_stream_tid = pkt->transaction_id; | 1167 | create_stream_tid = pkt->transaction_id; |
| 1225 | } | 1168 | } |
| 1226 | // createStream response | 1169 | // createStream response |
| @@ -1244,6 +1187,7 @@ int SrsRtmpServer::start_fmle_publish(int stream_id) | @@ -1244,6 +1187,7 @@ int SrsRtmpServer::start_fmle_publish(int stream_id) | ||
| 1244 | srs_info("recv publish request message success."); | 1187 | srs_info("recv publish request message success."); |
| 1245 | 1188 | ||
| 1246 | SrsAutoFree(__SrsMessage, msg, false); | 1189 | SrsAutoFree(__SrsMessage, msg, false); |
| 1190 | + SrsAutoFree(SrsPublishPacket, pkt, false); | ||
| 1247 | } | 1191 | } |
| 1248 | // publish response onFCPublish(NetStream.Publish.Start) | 1192 | // publish response onFCPublish(NetStream.Publish.Start) |
| 1249 | if (true) { | 1193 | if (true) { |
| @@ -164,8 +164,6 @@ public: | @@ -164,8 +164,6 @@ public: | ||
| 164 | virtual int64_t get_send_bytes(); | 164 | virtual int64_t get_send_bytes(); |
| 165 | virtual int get_recv_kbps(); | 165 | virtual int get_recv_kbps(); |
| 166 | virtual int get_send_kbps(); | 166 | virtual int get_send_kbps(); |
| 167 | - virtual int recv_message(SrsCommonMessage** pmsg); | ||
| 168 | - virtual int send_message(ISrsMessage* msg); | ||
| 169 | virtual int __recv_message(__SrsMessage** pmsg); | 167 | virtual int __recv_message(__SrsMessage** pmsg); |
| 170 | virtual int __decode_message(__SrsMessage* msg, SrsPacket** ppacket); | 168 | virtual int __decode_message(__SrsMessage* msg, SrsPacket** ppacket); |
| 171 | virtual int __send_and_free_message(__SrsMessage* msg); | 169 | virtual int __send_and_free_message(__SrsMessage* msg); |
| @@ -213,8 +211,6 @@ public: | @@ -213,8 +211,6 @@ public: | ||
| 213 | virtual int64_t get_send_bytes(); | 211 | virtual int64_t get_send_bytes(); |
| 214 | virtual int get_recv_kbps(); | 212 | virtual int get_recv_kbps(); |
| 215 | virtual int get_send_kbps(); | 213 | virtual int get_send_kbps(); |
| 216 | - virtual int recv_message(SrsCommonMessage** pmsg); | ||
| 217 | - virtual int send_message(ISrsMessage* msg); | ||
| 218 | virtual int __recv_message(__SrsMessage** pmsg); | 214 | virtual int __recv_message(__SrsMessage** pmsg); |
| 219 | virtual int __decode_message(__SrsMessage* msg, SrsPacket** ppacket); | 215 | virtual int __decode_message(__SrsMessage* msg, SrsPacket** ppacket); |
| 220 | virtual int __send_and_free_message(__SrsMessage* msg); | 216 | virtual int __send_and_free_message(__SrsMessage* msg); |
| @@ -306,16 +306,6 @@ SrsProtocol::SrsProtocol(ISrsProtocolReaderWriter* io) | @@ -306,16 +306,6 @@ SrsProtocol::SrsProtocol(ISrsProtocolReaderWriter* io) | ||
| 306 | SrsProtocol::~SrsProtocol() | 306 | SrsProtocol::~SrsProtocol() |
| 307 | { | 307 | { |
| 308 | if (true) { | 308 | if (true) { |
| 309 | - std::map<int, SrsChunkStream*>::iterator it; | ||
| 310 | - | ||
| 311 | - for (it = chunk_streams.begin(); it != chunk_streams.end(); ++it) { | ||
| 312 | - SrsChunkStream* stream = it->second; | ||
| 313 | - srs_freep(stream); | ||
| 314 | - } | ||
| 315 | - | ||
| 316 | - chunk_streams.clear(); | ||
| 317 | - } | ||
| 318 | - if (true) { | ||
| 319 | std::map<int, __SrsChunkStream*>::iterator it; | 309 | std::map<int, __SrsChunkStream*>::iterator it; |
| 320 | 310 | ||
| 321 | for (it = __chunk_streams.begin(); it != __chunk_streams.end(); ++it) { | 311 | for (it = __chunk_streams.begin(); it != __chunk_streams.end(); ++it) { |
| @@ -819,6 +809,9 @@ int SrsProtocol::__recv_interlaced_message(__SrsMessage** pmsg) | @@ -819,6 +809,9 @@ int SrsProtocol::__recv_interlaced_message(__SrsMessage** pmsg) | ||
| 819 | 809 | ||
| 820 | if (__chunk_streams.find(cid) == __chunk_streams.end()) { | 810 | if (__chunk_streams.find(cid) == __chunk_streams.end()) { |
| 821 | chunk = __chunk_streams[cid] = new __SrsChunkStream(cid); | 811 | chunk = __chunk_streams[cid] = new __SrsChunkStream(cid); |
| 812 | + // set the perfer cid of chunk, | ||
| 813 | + // which will copy to the message received. | ||
| 814 | + chunk->header.perfer_cid = cid; | ||
| 822 | srs_verbose("cache new chunk stream: fmt=%d, cid=%d", fmt, cid); | 815 | srs_verbose("cache new chunk stream: fmt=%d, cid=%d", fmt, cid); |
| 823 | } else { | 816 | } else { |
| 824 | chunk = __chunk_streams[cid]; | 817 | chunk = __chunk_streams[cid]; |
| @@ -979,7 +972,6 @@ int SrsProtocol::__read_message_header(__SrsChunkStream* chunk, char fmt, int bh | @@ -979,7 +972,6 @@ int SrsProtocol::__read_message_header(__SrsChunkStream* chunk, char fmt, int bh | ||
| 979 | // create msg when new chunk stream start | 972 | // create msg when new chunk stream start |
| 980 | if (!chunk->msg) { | 973 | if (!chunk->msg) { |
| 981 | chunk->msg = new __SrsMessage(); | 974 | chunk->msg = new __SrsMessage(); |
| 982 | - chunk->msg->header.perfer_cid = chunk->cid; | ||
| 983 | srs_verbose("create message for new chunk, fmt=%d, cid=%d", fmt, chunk->cid); | 975 | srs_verbose("create message for new chunk, fmt=%d, cid=%d", fmt, chunk->cid); |
| 984 | } | 976 | } |
| 985 | 977 | ||
| @@ -1355,187 +1347,13 @@ int SrsProtocol::__on_send_message(__SrsMessage* msg, SrsPacket* packet) | @@ -1355,187 +1347,13 @@ int SrsProtocol::__on_send_message(__SrsMessage* msg, SrsPacket* packet) | ||
| 1355 | return ret; | 1347 | return ret; |
| 1356 | } | 1348 | } |
| 1357 | 1349 | ||
| 1358 | -int SrsProtocol::recv_message(SrsCommonMessage** pmsg) | ||
| 1359 | -{ | ||
| 1360 | - *pmsg = NULL; | ||
| 1361 | - | ||
| 1362 | - int ret = ERROR_SUCCESS; | ||
| 1363 | - | ||
| 1364 | - while (true) { | ||
| 1365 | - SrsCommonMessage* msg = NULL; | ||
| 1366 | - | ||
| 1367 | - if ((ret = recv_interlaced_message(&msg)) != ERROR_SUCCESS) { | ||
| 1368 | - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) { | ||
| 1369 | - srs_error("recv interlaced message failed. ret=%d", ret); | ||
| 1370 | - } | ||
| 1371 | - return ret; | ||
| 1372 | - } | ||
| 1373 | - srs_verbose("entire msg received"); | ||
| 1374 | - | ||
| 1375 | - if (!msg) { | ||
| 1376 | - continue; | ||
| 1377 | - } | ||
| 1378 | - | ||
| 1379 | - if (msg->size <= 0 || msg->header.payload_length <= 0) { | ||
| 1380 | - srs_trace("ignore empty message(type=%d, size=%d, time=%"PRId64", sid=%d).", | ||
| 1381 | - msg->header.message_type, msg->header.payload_length, | ||
| 1382 | - msg->header.timestamp, msg->header.stream_id); | ||
| 1383 | - srs_freep(msg); | ||
| 1384 | - continue; | ||
| 1385 | - } | ||
| 1386 | - | ||
| 1387 | - if ((ret = on_recv_message(msg)) != ERROR_SUCCESS) { | ||
| 1388 | - srs_error("hook the received msg failed. ret=%d", ret); | ||
| 1389 | - srs_freep(msg); | ||
| 1390 | - return ret; | ||
| 1391 | - } | ||
| 1392 | - | ||
| 1393 | - srs_verbose("get a msg with raw/undecoded payload"); | ||
| 1394 | - *pmsg = msg; | ||
| 1395 | - break; | ||
| 1396 | - } | ||
| 1397 | - | ||
| 1398 | - return ret; | ||
| 1399 | -} | ||
| 1400 | - | ||
| 1401 | -int SrsProtocol::send_message(ISrsMessage* msg) | ||
| 1402 | -{ | ||
| 1403 | - int ret = ERROR_SUCCESS; | ||
| 1404 | - | ||
| 1405 | - // free msg whatever return value. | ||
| 1406 | - SrsAutoFree(ISrsMessage, msg, false); | ||
| 1407 | - | ||
| 1408 | - if ((ret = msg->encode_packet()) != ERROR_SUCCESS) { | ||
| 1409 | - srs_error("encode packet to message payload failed. ret=%d", ret); | ||
| 1410 | - return ret; | ||
| 1411 | - } | ||
| 1412 | - srs_info("encode packet to message payload success"); | ||
| 1413 | - | ||
| 1414 | - // p set to current write position, | ||
| 1415 | - // it's ok when payload is NULL and size is 0. | ||
| 1416 | - char* p = (char*)msg->payload; | ||
| 1417 | - | ||
| 1418 | - // always write the header event payload is empty. | ||
| 1419 | - do { | ||
| 1420 | - // generate the header. | ||
| 1421 | - char* pheader = out_header_cache; | ||
| 1422 | - | ||
| 1423 | - if (p == (char*)msg->payload) { | ||
| 1424 | - // write new chunk stream header, fmt is 0 | ||
| 1425 | - *pheader++ = 0x00 | (msg->get_perfer_cid() & 0x3F); | ||
| 1426 | - | ||
| 1427 | - // chunk message header, 11 bytes | ||
| 1428 | - // timestamp, 3bytes, big-endian | ||
| 1429 | - u_int32_t timestamp = (u_int32_t)msg->header.timestamp; | ||
| 1430 | - if (timestamp >= RTMP_EXTENDED_TIMESTAMP) { | ||
| 1431 | - *pheader++ = 0xFF; | ||
| 1432 | - *pheader++ = 0xFF; | ||
| 1433 | - *pheader++ = 0xFF; | ||
| 1434 | - } else { | ||
| 1435 | - pp = (char*)×tamp; | ||
| 1436 | - *pheader++ = pp[2]; | ||
| 1437 | - *pheader++ = pp[1]; | ||
| 1438 | - *pheader++ = pp[0]; | ||
| 1439 | - } | ||
| 1440 | - | ||
| 1441 | - // message_length, 3bytes, big-endian | ||
| 1442 | - pp = (char*)&msg->header.payload_length; | ||
| 1443 | - *pheader++ = pp[2]; | ||
| 1444 | - *pheader++ = pp[1]; | ||
| 1445 | - *pheader++ = pp[0]; | ||
| 1446 | - | ||
| 1447 | - // message_type, 1bytes | ||
| 1448 | - *pheader++ = msg->header.message_type; | ||
| 1449 | - | ||
| 1450 | - // message_length, 3bytes, little-endian | ||
| 1451 | - pp = (char*)&msg->header.stream_id; | ||
| 1452 | - *pheader++ = pp[0]; | ||
| 1453 | - *pheader++ = pp[1]; | ||
| 1454 | - *pheader++ = pp[2]; | ||
| 1455 | - *pheader++ = pp[3]; | ||
| 1456 | - | ||
| 1457 | - // chunk extended timestamp header, 0 or 4 bytes, big-endian | ||
| 1458 | - if(timestamp >= RTMP_EXTENDED_TIMESTAMP){ | ||
| 1459 | - pp = (char*)×tamp; | ||
| 1460 | - *pheader++ = pp[3]; | ||
| 1461 | - *pheader++ = pp[2]; | ||
| 1462 | - *pheader++ = pp[1]; | ||
| 1463 | - *pheader++ = pp[0]; | ||
| 1464 | - } | ||
| 1465 | - } else { | ||
| 1466 | - // write no message header chunk stream, fmt is 3 | ||
| 1467 | - *pheader++ = 0xC0 | (msg->get_perfer_cid() & 0x3F); | ||
| 1468 | - | ||
| 1469 | - // chunk extended timestamp header, 0 or 4 bytes, big-endian | ||
| 1470 | - // 6.1.3. Extended Timestamp | ||
| 1471 | - // This field is transmitted only when the normal time stamp in the | ||
| 1472 | - // chunk message header is set to 0x00ffffff. If normal time stamp is | ||
| 1473 | - // set to any value less than 0x00ffffff, this field MUST NOT be | ||
| 1474 | - // present. This field MUST NOT be present if the timestamp field is not | ||
| 1475 | - // present. Type 3 chunks MUST NOT have this field. | ||
| 1476 | - // adobe changed for Type3 chunk: | ||
| 1477 | - // FMLE always sendout the extended-timestamp, | ||
| 1478 | - // must send the extended-timestamp to FMS, | ||
| 1479 | - // must send the extended-timestamp to flash-player. | ||
| 1480 | - // @see: ngx_rtmp_prepare_message | ||
| 1481 | - // @see: http://blog.csdn.net/win_lin/article/details/13363699 | ||
| 1482 | - u_int32_t timestamp = (u_int32_t)msg->header.timestamp; | ||
| 1483 | - if(timestamp >= RTMP_EXTENDED_TIMESTAMP){ | ||
| 1484 | - pp = (char*)×tamp; | ||
| 1485 | - *pheader++ = pp[3]; | ||
| 1486 | - *pheader++ = pp[2]; | ||
| 1487 | - *pheader++ = pp[1]; | ||
| 1488 | - *pheader++ = pp[0]; | ||
| 1489 | - } | ||
| 1490 | - } | ||
| 1491 | - | ||
| 1492 | - // sendout header and payload by writev. | ||
| 1493 | - // decrease the sys invoke count to get higher performance. | ||
| 1494 | - int payload_size = msg->size - (p - (char*)msg->payload); | ||
| 1495 | - payload_size = srs_min(payload_size, out_chunk_size); | ||
| 1496 | - | ||
| 1497 | - // always has header | ||
| 1498 | - int header_size = pheader - out_header_cache; | ||
| 1499 | - srs_assert(header_size > 0); | ||
| 1500 | - | ||
| 1501 | - // send by writev | ||
| 1502 | - iovec iov[2]; | ||
| 1503 | - iov[0].iov_base = out_header_cache; | ||
| 1504 | - iov[0].iov_len = header_size; | ||
| 1505 | - iov[1].iov_base = p; | ||
| 1506 | - iov[1].iov_len = payload_size; | ||
| 1507 | - | ||
| 1508 | - ssize_t nwrite; | ||
| 1509 | - if ((ret = skt->writev(iov, 2, &nwrite)) != ERROR_SUCCESS) { | ||
| 1510 | - srs_error("send with writev failed. ret=%d", ret); | ||
| 1511 | - return ret; | ||
| 1512 | - } | ||
| 1513 | - | ||
| 1514 | - // consume sendout bytes when not empty packet. | ||
| 1515 | - if (msg->payload && msg->size > 0) { | ||
| 1516 | - p += payload_size; | ||
| 1517 | - } | ||
| 1518 | - } while (p < (char*)msg->payload + msg->size); | ||
| 1519 | - | ||
| 1520 | - if ((ret = on_send_message(msg)) != ERROR_SUCCESS) { | ||
| 1521 | - srs_error("hook the send message failed. ret=%d", ret); | ||
| 1522 | - return ret; | ||
| 1523 | - } | ||
| 1524 | - | ||
| 1525 | - return ret; | ||
| 1526 | -} | ||
| 1527 | - | ||
| 1528 | int SrsProtocol::response_acknowledgement_message() | 1350 | int SrsProtocol::response_acknowledgement_message() |
| 1529 | { | 1351 | { |
| 1530 | int ret = ERROR_SUCCESS; | 1352 | int ret = ERROR_SUCCESS; |
| 1531 | 1353 | ||
| 1532 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 1533 | SrsAcknowledgementPacket* pkt = new SrsAcknowledgementPacket(); | 1354 | SrsAcknowledgementPacket* pkt = new SrsAcknowledgementPacket(); |
| 1534 | - | ||
| 1535 | in_ack_size.acked_size = pkt->sequence_number = skt->get_recv_bytes(); | 1355 | in_ack_size.acked_size = pkt->sequence_number = skt->get_recv_bytes(); |
| 1536 | - msg->set_packet(pkt, 0); | ||
| 1537 | - | ||
| 1538 | - if ((ret = send_message(msg)) != ERROR_SUCCESS) { | 1356 | + if ((ret = __send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 1539 | srs_error("send acknowledgement failed. ret=%d", ret); | 1357 | srs_error("send acknowledgement failed. ret=%d", ret); |
| 1540 | return ret; | 1358 | return ret; |
| 1541 | } | 1359 | } |
| @@ -1550,14 +1368,12 @@ int SrsProtocol::response_ping_message(int32_t timestamp) | @@ -1550,14 +1368,12 @@ int SrsProtocol::response_ping_message(int32_t timestamp) | ||
| 1550 | 1368 | ||
| 1551 | srs_trace("get a ping request, response it. timestamp=%d", timestamp); | 1369 | srs_trace("get a ping request, response it. timestamp=%d", timestamp); |
| 1552 | 1370 | ||
| 1553 | - SrsCommonMessage* msg = new SrsCommonMessage(); | ||
| 1554 | SrsUserControlPacket* pkt = new SrsUserControlPacket(); | 1371 | SrsUserControlPacket* pkt = new SrsUserControlPacket(); |
| 1555 | 1372 | ||
| 1556 | pkt->event_type = SrcPCUCPingResponse; | 1373 | pkt->event_type = SrcPCUCPingResponse; |
| 1557 | pkt->event_data = timestamp; | 1374 | pkt->event_data = timestamp; |
| 1558 | - msg->set_packet(pkt, 0); | ||
| 1559 | 1375 | ||
| 1560 | - if ((ret = send_message(msg)) != ERROR_SUCCESS) { | 1376 | + if ((ret = __send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { |
| 1561 | srs_error("send ping response failed. ret=%d", ret); | 1377 | srs_error("send ping response failed. ret=%d", ret); |
| 1562 | return ret; | 1378 | return ret; |
| 1563 | } | 1379 | } |
| @@ -1566,595 +1382,18 @@ int SrsProtocol::response_ping_message(int32_t timestamp) | @@ -1566,595 +1382,18 @@ int SrsProtocol::response_ping_message(int32_t timestamp) | ||
| 1566 | return ret; | 1382 | return ret; |
| 1567 | } | 1383 | } |
| 1568 | 1384 | ||
| 1569 | -int SrsProtocol::on_recv_message(SrsCommonMessage* msg) | 1385 | +SrsMessageHeader::SrsMessageHeader() |
| 1570 | { | 1386 | { |
| 1571 | - int ret = ERROR_SUCCESS; | ||
| 1572 | - | ||
| 1573 | - srs_assert(msg != NULL); | ||
| 1574 | - | ||
| 1575 | - // acknowledgement | ||
| 1576 | - if (in_ack_size.ack_window_size > 0 && skt->get_recv_bytes() - in_ack_size.acked_size > in_ack_size.ack_window_size) { | ||
| 1577 | - if ((ret = response_acknowledgement_message()) != ERROR_SUCCESS) { | ||
| 1578 | - return ret; | ||
| 1579 | - } | ||
| 1580 | - } | ||
| 1581 | - | ||
| 1582 | - switch (msg->header.message_type) { | ||
| 1583 | - case RTMP_MSG_SetChunkSize: | ||
| 1584 | - case RTMP_MSG_UserControlMessage: | ||
| 1585 | - case RTMP_MSG_WindowAcknowledgementSize: | ||
| 1586 | - if ((ret = msg->decode_packet(this)) != ERROR_SUCCESS) { | ||
| 1587 | - srs_error("decode packet from message payload failed. ret=%d", ret); | ||
| 1588 | - return ret; | ||
| 1589 | - } | ||
| 1590 | - srs_verbose("decode packet from message payload success."); | ||
| 1591 | - break; | ||
| 1592 | - } | ||
| 1593 | - | ||
| 1594 | - switch (msg->header.message_type) { | ||
| 1595 | - case RTMP_MSG_WindowAcknowledgementSize: { | ||
| 1596 | - SrsSetWindowAckSizePacket* pkt = dynamic_cast<SrsSetWindowAckSizePacket*>(msg->get_packet()); | ||
| 1597 | - srs_assert(pkt != NULL); | ||
| 1598 | - | ||
| 1599 | - if (pkt->ackowledgement_window_size > 0) { | ||
| 1600 | - in_ack_size.ack_window_size = pkt->ackowledgement_window_size; | ||
| 1601 | - srs_trace("set ack window size to %d", pkt->ackowledgement_window_size); | ||
| 1602 | - } else { | ||
| 1603 | - srs_warn("ignored. set ack window size is %d", pkt->ackowledgement_window_size); | ||
| 1604 | - } | ||
| 1605 | - break; | ||
| 1606 | - } | ||
| 1607 | - case RTMP_MSG_SetChunkSize: { | ||
| 1608 | - SrsSetChunkSizePacket* pkt = dynamic_cast<SrsSetChunkSizePacket*>(msg->get_packet()); | ||
| 1609 | - srs_assert(pkt != NULL); | ||
| 1610 | - | ||
| 1611 | - in_chunk_size = pkt->chunk_size; | ||
| 1612 | - | ||
| 1613 | - srs_trace("set input chunk size to %d", pkt->chunk_size); | ||
| 1614 | - break; | ||
| 1615 | - } | ||
| 1616 | - case RTMP_MSG_UserControlMessage: { | ||
| 1617 | - SrsUserControlPacket* pkt = dynamic_cast<SrsUserControlPacket*>(msg->get_packet()); | ||
| 1618 | - srs_assert(pkt != NULL); | ||
| 1619 | - | ||
| 1620 | - if (pkt->event_type == SrcPCUCSetBufferLength) { | ||
| 1621 | - srs_trace("ignored. set buffer length to %d", pkt->extra_data); | ||
| 1622 | - } | ||
| 1623 | - if (pkt->event_type == SrcPCUCPingRequest) { | ||
| 1624 | - if ((ret = response_ping_message(pkt->event_data)) != ERROR_SUCCESS) { | ||
| 1625 | - return ret; | ||
| 1626 | - } | ||
| 1627 | - } | ||
| 1628 | - break; | ||
| 1629 | - } | ||
| 1630 | - } | 1387 | + message_type = 0; |
| 1388 | + payload_length = 0; | ||
| 1389 | + timestamp_delta = 0; | ||
| 1390 | + stream_id = 0; | ||
| 1631 | 1391 | ||
| 1632 | - return ret; | 1392 | + timestamp = 0; |
| 1393 | + perfer_cid = RTMP_CID_ProtocolControl; | ||
| 1633 | } | 1394 | } |
| 1634 | 1395 | ||
| 1635 | -int SrsProtocol::on_send_message(ISrsMessage* msg) | ||
| 1636 | -{ | ||
| 1637 | - int ret = ERROR_SUCCESS; | ||
| 1638 | - | ||
| 1639 | - if (!msg->can_decode()) { | ||
| 1640 | - srs_verbose("ignore the un-decodable message."); | ||
| 1641 | - return ret; | ||
| 1642 | - } | ||
| 1643 | - | ||
| 1644 | - SrsCommonMessage* common_msg = dynamic_cast<SrsCommonMessage*>(msg); | ||
| 1645 | - if (!common_msg) { | ||
| 1646 | - srs_verbose("ignore the shared ptr message."); | ||
| 1647 | - return ret; | ||
| 1648 | - } | ||
| 1649 | - | ||
| 1650 | - // for proxy, the common msg is not decoded, ignore. | ||
| 1651 | - if (!common_msg->has_packet()) { | ||
| 1652 | - srs_verbose("ignore the proxy common message."); | ||
| 1653 | - return ret; | ||
| 1654 | - } | ||
| 1655 | - | ||
| 1656 | - srs_assert(common_msg != NULL); | ||
| 1657 | - | ||
| 1658 | - switch (common_msg->header.message_type) { | ||
| 1659 | - case RTMP_MSG_SetChunkSize: { | ||
| 1660 | - SrsSetChunkSizePacket* pkt = dynamic_cast<SrsSetChunkSizePacket*>(common_msg->get_packet()); | ||
| 1661 | - srs_assert(pkt != NULL); | ||
| 1662 | - | ||
| 1663 | - out_chunk_size = pkt->chunk_size; | ||
| 1664 | - | ||
| 1665 | - srs_trace("set output chunk size to %d", pkt->chunk_size); | ||
| 1666 | - break; | ||
| 1667 | - } | ||
| 1668 | - case RTMP_MSG_AMF0CommandMessage: | ||
| 1669 | - case RTMP_MSG_AMF3CommandMessage: { | ||
| 1670 | - if (true) { | ||
| 1671 | - SrsConnectAppPacket* pkt = NULL; | ||
| 1672 | - pkt = dynamic_cast<SrsConnectAppPacket*>(common_msg->get_packet()); | ||
| 1673 | - if (pkt) { | ||
| 1674 | - requests[pkt->transaction_id] = pkt->command_name; | ||
| 1675 | - break; | ||
| 1676 | - } | ||
| 1677 | - } | ||
| 1678 | - if (true) { | ||
| 1679 | - SrsCreateStreamPacket* pkt = NULL; | ||
| 1680 | - pkt = dynamic_cast<SrsCreateStreamPacket*>(common_msg->get_packet()); | ||
| 1681 | - if (pkt) { | ||
| 1682 | - requests[pkt->transaction_id] = pkt->command_name; | ||
| 1683 | - break; | ||
| 1684 | - } | ||
| 1685 | - } | ||
| 1686 | - if (true) { | ||
| 1687 | - SrsFMLEStartPacket* pkt = NULL; | ||
| 1688 | - pkt = dynamic_cast<SrsFMLEStartPacket*>(common_msg->get_packet()); | ||
| 1689 | - if (pkt) { | ||
| 1690 | - requests[pkt->transaction_id] = pkt->command_name; | ||
| 1691 | - break; | ||
| 1692 | - } | ||
| 1693 | - } | ||
| 1694 | - break; | ||
| 1695 | - } | ||
| 1696 | - } | ||
| 1697 | - | ||
| 1698 | - return ret; | ||
| 1699 | -} | ||
| 1700 | - | ||
| 1701 | -int SrsProtocol::recv_interlaced_message(SrsCommonMessage** pmsg) | ||
| 1702 | -{ | ||
| 1703 | - int ret = ERROR_SUCCESS; | ||
| 1704 | - | ||
| 1705 | - // chunk stream basic header. | ||
| 1706 | - char fmt = 0; | ||
| 1707 | - int cid = 0; | ||
| 1708 | - int bh_size = 0; | ||
| 1709 | - if ((ret = read_basic_header(fmt, cid, bh_size)) != ERROR_SUCCESS) { | ||
| 1710 | - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) { | ||
| 1711 | - srs_error("read basic header failed. ret=%d", ret); | ||
| 1712 | - } | ||
| 1713 | - return ret; | ||
| 1714 | - } | ||
| 1715 | - srs_verbose("read basic header success. fmt=%d, cid=%d, bh_size=%d", fmt, cid, bh_size); | ||
| 1716 | - | ||
| 1717 | - // once we got the chunk message header, | ||
| 1718 | - // that is there is a real message in cache, | ||
| 1719 | - // increase the timeout to got it. | ||
| 1720 | - // For example, in the play loop, we set timeout to 100ms, | ||
| 1721 | - // when we got a chunk header, we should increase the timeout, | ||
| 1722 | - // or we maybe timeout and disconnect the client. | ||
| 1723 | - int64_t timeout_us = skt->get_recv_timeout(); | ||
| 1724 | - if (!skt->is_never_timeout(timeout_us)) { | ||
| 1725 | - int64_t pkt_timeout_us = srs_max(timeout_us, SRS_MIN_RECV_TIMEOUT_US); | ||
| 1726 | - skt->set_recv_timeout(pkt_timeout_us); | ||
| 1727 | - srs_verbose("change recv timeout_us " | ||
| 1728 | - "from %"PRId64" to %"PRId64"", timeout_us, pkt_timeout_us); | ||
| 1729 | - } | ||
| 1730 | - | ||
| 1731 | - // get the cached chunk stream. | ||
| 1732 | - SrsChunkStream* chunk = NULL; | ||
| 1733 | - | ||
| 1734 | - if (chunk_streams.find(cid) == chunk_streams.end()) { | ||
| 1735 | - chunk = chunk_streams[cid] = new SrsChunkStream(cid); | ||
| 1736 | - srs_verbose("cache new chunk stream: fmt=%d, cid=%d", fmt, cid); | ||
| 1737 | - } else { | ||
| 1738 | - chunk = chunk_streams[cid]; | ||
| 1739 | - srs_verbose("cached chunk stream: fmt=%d, cid=%d, size=%d, message(type=%d, size=%d, time=%"PRId64", sid=%d)", | ||
| 1740 | - chunk->fmt, chunk->cid, (chunk->msg? chunk->msg->size : 0), chunk->header.message_type, chunk->header.payload_length, | ||
| 1741 | - chunk->header.timestamp, chunk->header.stream_id); | ||
| 1742 | - } | ||
| 1743 | - | ||
| 1744 | - // chunk stream message header | ||
| 1745 | - int mh_size = 0; | ||
| 1746 | - if ((ret = read_message_header(chunk, fmt, bh_size, mh_size)) != ERROR_SUCCESS) { | ||
| 1747 | - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) { | ||
| 1748 | - srs_error("read message header failed. ret=%d", ret); | ||
| 1749 | - } | ||
| 1750 | - return ret; | ||
| 1751 | - } | ||
| 1752 | - srs_verbose("read message header success. " | ||
| 1753 | - "fmt=%d, mh_size=%d, ext_time=%d, size=%d, message(type=%d, size=%d, time=%"PRId64", sid=%d)", | ||
| 1754 | - fmt, mh_size, chunk->extended_timestamp, (chunk->msg? chunk->msg->size : 0), chunk->header.message_type, | ||
| 1755 | - chunk->header.payload_length, chunk->header.timestamp, chunk->header.stream_id); | ||
| 1756 | - | ||
| 1757 | - // read msg payload from chunk stream. | ||
| 1758 | - SrsCommonMessage* msg = NULL; | ||
| 1759 | - int payload_size = 0; | ||
| 1760 | - if ((ret = read_message_payload(chunk, bh_size, mh_size, payload_size, &msg)) != ERROR_SUCCESS) { | ||
| 1761 | - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) { | ||
| 1762 | - srs_error("read message payload failed. ret=%d", ret); | ||
| 1763 | - } | ||
| 1764 | - return ret; | ||
| 1765 | - } | ||
| 1766 | - | ||
| 1767 | - // reset the recv timeout | ||
| 1768 | - if (!skt->is_never_timeout(timeout_us)) { | ||
| 1769 | - skt->set_recv_timeout(timeout_us); | ||
| 1770 | - srs_verbose("reset recv timeout_us to %"PRId64"", timeout_us); | ||
| 1771 | - } | ||
| 1772 | - | ||
| 1773 | - // not got an entire RTMP message, try next chunk. | ||
| 1774 | - if (!msg) { | ||
| 1775 | - srs_verbose("get partial message success. chunk_payload_size=%d, size=%d, message(type=%d, size=%d, time=%"PRId64", sid=%d)", | ||
| 1776 | - payload_size, (msg? msg->size : (chunk->msg? chunk->msg->size : 0)), chunk->header.message_type, chunk->header.payload_length, | ||
| 1777 | - chunk->header.timestamp, chunk->header.stream_id); | ||
| 1778 | - return ret; | ||
| 1779 | - } | ||
| 1780 | - | ||
| 1781 | - *pmsg = msg; | ||
| 1782 | - srs_info("get entire message success. chunk_payload_size=%d, size=%d, message(type=%d, size=%d, time=%"PRId64", sid=%d)", | ||
| 1783 | - payload_size, (msg? msg->size : (chunk->msg? chunk->msg->size : 0)), chunk->header.message_type, chunk->header.payload_length, | ||
| 1784 | - chunk->header.timestamp, chunk->header.stream_id); | ||
| 1785 | - | ||
| 1786 | - return ret; | ||
| 1787 | -} | ||
| 1788 | - | ||
| 1789 | -int SrsProtocol::read_basic_header(char& fmt, int& cid, int& bh_size) | ||
| 1790 | -{ | ||
| 1791 | - int ret = ERROR_SUCCESS; | ||
| 1792 | - | ||
| 1793 | - int required_size = 1; | ||
| 1794 | - if ((ret = buffer->ensure_buffer_bytes(skt, required_size)) != ERROR_SUCCESS) { | ||
| 1795 | - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) { | ||
| 1796 | - srs_error("read 1bytes basic header failed. required_size=%d, ret=%d", required_size, ret); | ||
| 1797 | - } | ||
| 1798 | - return ret; | ||
| 1799 | - } | ||
| 1800 | - | ||
| 1801 | - char* p = buffer->bytes(); | ||
| 1802 | - | ||
| 1803 | - fmt = (*p >> 6) & 0x03; | ||
| 1804 | - cid = *p & 0x3f; | ||
| 1805 | - bh_size = 1; | ||
| 1806 | - | ||
| 1807 | - if (cid > 1) { | ||
| 1808 | - srs_verbose("%dbytes basic header parsed. fmt=%d, cid=%d", bh_size, fmt, cid); | ||
| 1809 | - return ret; | ||
| 1810 | - } | ||
| 1811 | - | ||
| 1812 | - if (cid == 0) { | ||
| 1813 | - required_size = 2; | ||
| 1814 | - if ((ret = buffer->ensure_buffer_bytes(skt, required_size)) != ERROR_SUCCESS) { | ||
| 1815 | - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) { | ||
| 1816 | - srs_error("read 2bytes basic header failed. required_size=%d, ret=%d", required_size, ret); | ||
| 1817 | - } | ||
| 1818 | - return ret; | ||
| 1819 | - } | ||
| 1820 | - | ||
| 1821 | - cid = 64; | ||
| 1822 | - cid += *(++p); | ||
| 1823 | - bh_size = 2; | ||
| 1824 | - srs_verbose("%dbytes basic header parsed. fmt=%d, cid=%d", bh_size, fmt, cid); | ||
| 1825 | - } else if (cid == 1) { | ||
| 1826 | - required_size = 3; | ||
| 1827 | - if ((ret = buffer->ensure_buffer_bytes(skt, 3)) != ERROR_SUCCESS) { | ||
| 1828 | - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) { | ||
| 1829 | - srs_error("read 3bytes basic header failed. required_size=%d, ret=%d", required_size, ret); | ||
| 1830 | - } | ||
| 1831 | - return ret; | ||
| 1832 | - } | ||
| 1833 | - | ||
| 1834 | - cid = 64; | ||
| 1835 | - cid += *(++p); | ||
| 1836 | - cid += *(++p) * 256; | ||
| 1837 | - bh_size = 3; | ||
| 1838 | - srs_verbose("%dbytes basic header parsed. fmt=%d, cid=%d", bh_size, fmt, cid); | ||
| 1839 | - } else { | ||
| 1840 | - srs_error("invalid path, impossible basic header."); | ||
| 1841 | - srs_assert(false); | ||
| 1842 | - } | ||
| 1843 | - | ||
| 1844 | - return ret; | ||
| 1845 | -} | ||
| 1846 | - | ||
| 1847 | -int SrsProtocol::read_message_header(SrsChunkStream* chunk, char fmt, int bh_size, int& mh_size) | ||
| 1848 | -{ | ||
| 1849 | - int ret = ERROR_SUCCESS; | ||
| 1850 | - | ||
| 1851 | - /** | ||
| 1852 | - * we should not assert anything about fmt, for the first packet. | ||
| 1853 | - * (when first packet, the chunk->msg is NULL). | ||
| 1854 | - * the fmt maybe 0/1/2/3, the FMLE will send a 0xC4 for some audio packet. | ||
| 1855 | - * the previous packet is: | ||
| 1856 | - * 04 // fmt=0, cid=4 | ||
| 1857 | - * 00 00 1a // timestamp=26 | ||
| 1858 | - * 00 00 9d // payload_length=157 | ||
| 1859 | - * 08 // message_type=8(audio) | ||
| 1860 | - * 01 00 00 00 // stream_id=1 | ||
| 1861 | - * the current packet maybe: | ||
| 1862 | - * c4 // fmt=3, cid=4 | ||
| 1863 | - * it's ok, for the packet is audio, and timestamp delta is 26. | ||
| 1864 | - * the current packet must be parsed as: | ||
| 1865 | - * fmt=0, cid=4 | ||
| 1866 | - * timestamp=26+26=52 | ||
| 1867 | - * payload_length=157 | ||
| 1868 | - * message_type=8(audio) | ||
| 1869 | - * stream_id=1 | ||
| 1870 | - * so we must update the timestamp even fmt=3 for first packet. | ||
| 1871 | - */ | ||
| 1872 | - // fresh packet used to update the timestamp even fmt=3 for first packet. | ||
| 1873 | - bool is_fresh_packet = !chunk->msg; | ||
| 1874 | - | ||
| 1875 | - // but, we can ensure that when a chunk stream is fresh, | ||
| 1876 | - // the fmt must be 0, a new stream. | ||
| 1877 | - if (chunk->msg_count == 0 && fmt != RTMP_FMT_TYPE0) { | ||
| 1878 | - ret = ERROR_RTMP_CHUNK_START; | ||
| 1879 | - srs_error("chunk stream is fresh, " | ||
| 1880 | - "fmt must be %d, actual is %d. ret=%d", RTMP_FMT_TYPE0, fmt, ret); | ||
| 1881 | - return ret; | ||
| 1882 | - } | ||
| 1883 | - | ||
| 1884 | - // when exists cache msg, means got an partial message, | ||
| 1885 | - // the fmt must not be type0 which means new message. | ||
| 1886 | - if (chunk->msg && fmt == RTMP_FMT_TYPE0) { | ||
| 1887 | - ret = ERROR_RTMP_CHUNK_START; | ||
| 1888 | - srs_error("chunk stream exists, " | ||
| 1889 | - "fmt must not be %d, actual is %d. ret=%d", RTMP_FMT_TYPE0, fmt, ret); | ||
| 1890 | - return ret; | ||
| 1891 | - } | ||
| 1892 | - | ||
| 1893 | - // create msg when new chunk stream start | ||
| 1894 | - if (!chunk->msg) { | ||
| 1895 | - chunk->msg = new SrsCommonMessage(); | ||
| 1896 | - srs_verbose("create message for new chunk, fmt=%d, cid=%d", fmt, chunk->cid); | ||
| 1897 | - } | ||
| 1898 | - | ||
| 1899 | - // read message header from socket to buffer. | ||
| 1900 | - static char mh_sizes[] = {11, 7, 3, 0}; | ||
| 1901 | - mh_size = mh_sizes[(int)fmt]; | ||
| 1902 | - srs_verbose("calc chunk message header size. fmt=%d, mh_size=%d", fmt, mh_size); | ||
| 1903 | - | ||
| 1904 | - int required_size = bh_size + mh_size; | ||
| 1905 | - if ((ret = buffer->ensure_buffer_bytes(skt, required_size)) != ERROR_SUCCESS) { | ||
| 1906 | - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) { | ||
| 1907 | - srs_error("read %dbytes message header failed. required_size=%d, ret=%d", mh_size, required_size, ret); | ||
| 1908 | - } | ||
| 1909 | - return ret; | ||
| 1910 | - } | ||
| 1911 | - char* p = buffer->bytes() + bh_size; | ||
| 1912 | - | ||
| 1913 | - // parse the message header. | ||
| 1914 | - // see also: ngx_rtmp_recv | ||
| 1915 | - if (fmt <= RTMP_FMT_TYPE2) { | ||
| 1916 | - char* pp = (char*)&chunk->header.timestamp_delta; | ||
| 1917 | - pp[2] = *p++; | ||
| 1918 | - pp[1] = *p++; | ||
| 1919 | - pp[0] = *p++; | ||
| 1920 | - pp[3] = 0; | ||
| 1921 | - | ||
| 1922 | - // fmt: 0 | ||
| 1923 | - // timestamp: 3 bytes | ||
| 1924 | - // If the timestamp is greater than or equal to 16777215 | ||
| 1925 | - // (hexadecimal 0x00ffffff), this value MUST be 16777215, and the | ||
| 1926 | - // ‘extended timestamp header’ MUST be present. Otherwise, this value | ||
| 1927 | - // SHOULD be the entire timestamp. | ||
| 1928 | - // | ||
| 1929 | - // fmt: 1 or 2 | ||
| 1930 | - // timestamp delta: 3 bytes | ||
| 1931 | - // If the delta is greater than or equal to 16777215 (hexadecimal | ||
| 1932 | - // 0x00ffffff), this value MUST be 16777215, and the ‘extended | ||
| 1933 | - // timestamp header’ MUST be present. Otherwise, this value SHOULD be | ||
| 1934 | - // the entire delta. | ||
| 1935 | - chunk->extended_timestamp = (chunk->header.timestamp_delta >= RTMP_EXTENDED_TIMESTAMP); | ||
| 1936 | - if (chunk->extended_timestamp) { | ||
| 1937 | - // Extended timestamp: 0 or 4 bytes | ||
| 1938 | - // This field MUST be sent when the normal timsestamp is set to | ||
| 1939 | - // 0xffffff, it MUST NOT be sent if the normal timestamp is set to | ||
| 1940 | - // anything else. So for values less than 0xffffff the normal | ||
| 1941 | - // timestamp field SHOULD be used in which case the extended timestamp | ||
| 1942 | - // MUST NOT be present. For values greater than or equal to 0xffffff | ||
| 1943 | - // the normal timestamp field MUST NOT be used and MUST be set to | ||
| 1944 | - // 0xffffff and the extended timestamp MUST be sent. | ||
| 1945 | - // | ||
| 1946 | - // if extended timestamp, the timestamp must >= RTMP_EXTENDED_TIMESTAMP | ||
| 1947 | - // we set the timestamp to RTMP_EXTENDED_TIMESTAMP to identify we | ||
| 1948 | - // got an extended timestamp. | ||
| 1949 | - chunk->header.timestamp = RTMP_EXTENDED_TIMESTAMP; | ||
| 1950 | - } else { | ||
| 1951 | - if (fmt == RTMP_FMT_TYPE0) { | ||
| 1952 | - // 6.1.2.1. Type 0 | ||
| 1953 | - // For a type-0 chunk, the absolute timestamp of the message is sent | ||
| 1954 | - // here. | ||
| 1955 | - chunk->header.timestamp = chunk->header.timestamp_delta; | ||
| 1956 | - } else { | ||
| 1957 | - // 6.1.2.2. Type 1 | ||
| 1958 | - // 6.1.2.3. Type 2 | ||
| 1959 | - // For a type-1 or type-2 chunk, the difference between the previous | ||
| 1960 | - // chunk's timestamp and the current chunk's timestamp is sent here. | ||
| 1961 | - chunk->header.timestamp += chunk->header.timestamp_delta; | ||
| 1962 | - } | ||
| 1963 | - } | ||
| 1964 | - | ||
| 1965 | - if (fmt <= RTMP_FMT_TYPE1) { | ||
| 1966 | - pp = (char*)&chunk->header.payload_length; | ||
| 1967 | - pp[2] = *p++; | ||
| 1968 | - pp[1] = *p++; | ||
| 1969 | - pp[0] = *p++; | ||
| 1970 | - pp[3] = 0; | ||
| 1971 | - | ||
| 1972 | - // if msg exists in cache, the size must not changed. | ||
| 1973 | - if (chunk->msg->size > 0 && chunk->msg->size != chunk->header.payload_length) { | ||
| 1974 | - ret = ERROR_RTMP_PACKET_SIZE; | ||
| 1975 | - srs_error("msg exists in chunk cache, " | ||
| 1976 | - "size=%d cannot change to %d, ret=%d", | ||
| 1977 | - chunk->msg->size, chunk->header.payload_length, ret); | ||
| 1978 | - return ret; | ||
| 1979 | - } | ||
| 1980 | - | ||
| 1981 | - chunk->header.message_type = *p++; | ||
| 1982 | - | ||
| 1983 | - if (fmt == RTMP_FMT_TYPE0) { | ||
| 1984 | - pp = (char*)&chunk->header.stream_id; | ||
| 1985 | - pp[0] = *p++; | ||
| 1986 | - pp[1] = *p++; | ||
| 1987 | - pp[2] = *p++; | ||
| 1988 | - pp[3] = *p++; | ||
| 1989 | - srs_verbose("header read completed. fmt=%d, mh_size=%d, ext_time=%d, time=%"PRId64", payload=%d, type=%d, sid=%d", | ||
| 1990 | - fmt, mh_size, chunk->extended_timestamp, chunk->header.timestamp, chunk->header.payload_length, | ||
| 1991 | - chunk->header.message_type, chunk->header.stream_id); | ||
| 1992 | - } else { | ||
| 1993 | - srs_verbose("header read completed. fmt=%d, mh_size=%d, ext_time=%d, time=%"PRId64", payload=%d, type=%d", | ||
| 1994 | - fmt, mh_size, chunk->extended_timestamp, chunk->header.timestamp, chunk->header.payload_length, | ||
| 1995 | - chunk->header.message_type); | ||
| 1996 | - } | ||
| 1997 | - } else { | ||
| 1998 | - srs_verbose("header read completed. fmt=%d, mh_size=%d, ext_time=%d, time=%"PRId64"", | ||
| 1999 | - fmt, mh_size, chunk->extended_timestamp, chunk->header.timestamp); | ||
| 2000 | - } | ||
| 2001 | - } else { | ||
| 2002 | - // update the timestamp even fmt=3 for first stream | ||
| 2003 | - if (is_fresh_packet && !chunk->extended_timestamp) { | ||
| 2004 | - chunk->header.timestamp += chunk->header.timestamp_delta; | ||
| 2005 | - } | ||
| 2006 | - srs_verbose("header read completed. fmt=%d, size=%d, ext_time=%d", | ||
| 2007 | - fmt, mh_size, chunk->extended_timestamp); | ||
| 2008 | - } | ||
| 2009 | - | ||
| 2010 | - if (chunk->extended_timestamp) { | ||
| 2011 | - mh_size += 4; | ||
| 2012 | - required_size = bh_size + mh_size; | ||
| 2013 | - srs_verbose("read header ext time. fmt=%d, ext_time=%d, mh_size=%d", fmt, chunk->extended_timestamp, mh_size); | ||
| 2014 | - if ((ret = buffer->ensure_buffer_bytes(skt, required_size)) != ERROR_SUCCESS) { | ||
| 2015 | - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) { | ||
| 2016 | - srs_error("read %dbytes message header failed. required_size=%d, ret=%d", mh_size, required_size, ret); | ||
| 2017 | - } | ||
| 2018 | - return ret; | ||
| 2019 | - } | ||
| 2020 | - | ||
| 2021 | - u_int32_t timestamp = 0x00; | ||
| 2022 | - char* pp = (char*)×tamp; | ||
| 2023 | - pp[3] = *p++; | ||
| 2024 | - pp[2] = *p++; | ||
| 2025 | - pp[1] = *p++; | ||
| 2026 | - pp[0] = *p++; | ||
| 2027 | - | ||
| 2028 | - // ffmpeg/librtmp may donot send this filed, need to detect the value. | ||
| 2029 | - // @see also: http://blog.csdn.net/win_lin/article/details/13363699 | ||
| 2030 | - // compare to the chunk timestamp, which is set by chunk message header | ||
| 2031 | - // type 0,1 or 2. | ||
| 2032 | - u_int32_t chunk_timestamp = chunk->header.timestamp; | ||
| 2033 | - if (chunk_timestamp > RTMP_EXTENDED_TIMESTAMP && chunk_timestamp != timestamp) { | ||
| 2034 | - mh_size -= 4; | ||
| 2035 | - srs_verbose("ignore the 4bytes extended timestamp. mh_size=%d", mh_size); | ||
| 2036 | - } else { | ||
| 2037 | - chunk->header.timestamp = timestamp; | ||
| 2038 | - } | ||
| 2039 | - srs_verbose("header read ext_time completed. time=%"PRId64"", chunk->header.timestamp); | ||
| 2040 | - } | ||
| 2041 | - | ||
| 2042 | - // the extended-timestamp must be unsigned-int, | ||
| 2043 | - // 24bits timestamp: 0xffffff = 16777215ms = 16777.215s = 4.66h | ||
| 2044 | - // 32bits timestamp: 0xffffffff = 4294967295ms = 4294967.295s = 1193.046h = 49.71d | ||
| 2045 | - // because the rtmp protocol says the 32bits timestamp is about "50 days": | ||
| 2046 | - // 3. Byte Order, Alignment, and Time Format | ||
| 2047 | - // Because timestamps are generally only 32 bits long, they will roll | ||
| 2048 | - // over after fewer than 50 days. | ||
| 2049 | - // | ||
| 2050 | - // but, its sample says the timestamp is 31bits: | ||
| 2051 | - // An application could assume, for example, that all | ||
| 2052 | - // adjacent timestamps are within 2^31 milliseconds of each other, so | ||
| 2053 | - // 10000 comes after 4000000000, while 3000000000 comes before | ||
| 2054 | - // 4000000000. | ||
| 2055 | - // and flv specification says timestamp is 31bits: | ||
| 2056 | - // Extension of the Timestamp field to form a SI32 value. This | ||
| 2057 | - // field represents the upper 8 bits, while the previous | ||
| 2058 | - // Timestamp field represents the lower 24 bits of the time in | ||
| 2059 | - // milliseconds. | ||
| 2060 | - // in a word, 31bits timestamp is ok. | ||
| 2061 | - // convert extended timestamp to 31bits. | ||
| 2062 | - chunk->header.timestamp &= 0x7fffffff; | ||
| 2063 | - | ||
| 2064 | - // valid message | ||
| 2065 | - if (chunk->header.payload_length < 0) { | ||
| 2066 | - ret = ERROR_RTMP_MSG_INVLIAD_SIZE; | ||
| 2067 | - srs_error("RTMP message size must not be negative. size=%d, ret=%d", | ||
| 2068 | - chunk->header.payload_length, ret); | ||
| 2069 | - return ret; | ||
| 2070 | - } | ||
| 2071 | - | ||
| 2072 | - // copy header to msg | ||
| 2073 | - chunk->msg->header = chunk->header; | ||
| 2074 | - | ||
| 2075 | - // increase the msg count, the chunk stream can accept fmt=1/2/3 message now. | ||
| 2076 | - chunk->msg_count++; | ||
| 2077 | - | ||
| 2078 | - return ret; | ||
| 2079 | -} | ||
| 2080 | - | ||
| 2081 | -int SrsProtocol::read_message_payload(SrsChunkStream* chunk, int bh_size, int mh_size, int& payload_size, SrsCommonMessage** pmsg) | ||
| 2082 | -{ | ||
| 2083 | - int ret = ERROR_SUCCESS; | ||
| 2084 | - | ||
| 2085 | - // empty message | ||
| 2086 | - if (chunk->header.payload_length <= 0) { | ||
| 2087 | - // need erase the header in buffer. | ||
| 2088 | - buffer->erase(bh_size + mh_size); | ||
| 2089 | - | ||
| 2090 | - srs_trace("get an empty RTMP " | ||
| 2091 | - "message(type=%d, size=%d, time=%"PRId64", sid=%d)", chunk->header.message_type, | ||
| 2092 | - chunk->header.payload_length, chunk->header.timestamp, chunk->header.stream_id); | ||
| 2093 | - | ||
| 2094 | - *pmsg = chunk->msg; | ||
| 2095 | - chunk->msg = NULL; | ||
| 2096 | - | ||
| 2097 | - return ret; | ||
| 2098 | - } | ||
| 2099 | - srs_assert(chunk->header.payload_length > 0); | ||
| 2100 | - | ||
| 2101 | - // the chunk payload size. | ||
| 2102 | - payload_size = chunk->header.payload_length - chunk->msg->size; | ||
| 2103 | - payload_size = srs_min(payload_size, in_chunk_size); | ||
| 2104 | - srs_verbose("chunk payload size is %d, message_size=%d, received_size=%d, in_chunk_size=%d", | ||
| 2105 | - payload_size, chunk->header.payload_length, chunk->msg->size, in_chunk_size); | ||
| 2106 | - | ||
| 2107 | - // create msg payload if not initialized | ||
| 2108 | - if (!chunk->msg->payload) { | ||
| 2109 | - chunk->msg->payload = new int8_t[chunk->header.payload_length]; | ||
| 2110 | - memset(chunk->msg->payload, 0, chunk->header.payload_length); | ||
| 2111 | - srs_verbose("create empty payload for RTMP message. size=%d", chunk->header.payload_length); | ||
| 2112 | - } | ||
| 2113 | - | ||
| 2114 | - // read payload to buffer | ||
| 2115 | - int required_size = bh_size + mh_size + payload_size; | ||
| 2116 | - if ((ret = buffer->ensure_buffer_bytes(skt, required_size)) != ERROR_SUCCESS) { | ||
| 2117 | - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) { | ||
| 2118 | - srs_error("read payload failed. required_size=%d, ret=%d", required_size, ret); | ||
| 2119 | - } | ||
| 2120 | - return ret; | ||
| 2121 | - } | ||
| 2122 | - memcpy(chunk->msg->payload + chunk->msg->size, buffer->bytes() + bh_size + mh_size, payload_size); | ||
| 2123 | - buffer->erase(bh_size + mh_size + payload_size); | ||
| 2124 | - chunk->msg->size += payload_size; | ||
| 2125 | - | ||
| 2126 | - srs_verbose("chunk payload read completed. bh_size=%d, mh_size=%d, payload_size=%d", bh_size, mh_size, payload_size); | ||
| 2127 | - | ||
| 2128 | - // got entire RTMP message? | ||
| 2129 | - if (chunk->header.payload_length == chunk->msg->size) { | ||
| 2130 | - *pmsg = chunk->msg; | ||
| 2131 | - chunk->msg = NULL; | ||
| 2132 | - srs_verbose("get entire RTMP message(type=%d, size=%d, time=%"PRId64", sid=%d)", | ||
| 2133 | - chunk->header.message_type, chunk->header.payload_length, | ||
| 2134 | - chunk->header.timestamp, chunk->header.stream_id); | ||
| 2135 | - return ret; | ||
| 2136 | - } | ||
| 2137 | - | ||
| 2138 | - srs_verbose("get partial RTMP message(type=%d, size=%d, time=%"PRId64", sid=%d), partial size=%d", | ||
| 2139 | - chunk->header.message_type, chunk->header.payload_length, | ||
| 2140 | - chunk->header.timestamp, chunk->header.stream_id, | ||
| 2141 | - chunk->msg->size); | ||
| 2142 | - | ||
| 2143 | - return ret; | ||
| 2144 | -} | ||
| 2145 | - | ||
| 2146 | -SrsMessageHeader::SrsMessageHeader() | ||
| 2147 | -{ | ||
| 2148 | - message_type = 0; | ||
| 2149 | - payload_length = 0; | ||
| 2150 | - timestamp_delta = 0; | ||
| 2151 | - stream_id = 0; | ||
| 2152 | - | ||
| 2153 | - timestamp = 0; | ||
| 2154 | - perfer_cid = RTMP_CID_ProtocolControl; | ||
| 2155 | -} | ||
| 2156 | - | ||
| 2157 | -SrsMessageHeader::~SrsMessageHeader() | 1396 | +SrsMessageHeader::~SrsMessageHeader() |
| 2158 | { | 1397 | { |
| 2159 | } | 1398 | } |
| 2160 | 1399 | ||
| @@ -2178,496 +1417,105 @@ bool SrsMessageHeader::is_amf0_data() | @@ -2178,496 +1417,105 @@ bool SrsMessageHeader::is_amf0_data() | ||
| 2178 | return message_type == RTMP_MSG_AMF0DataMessage; | 1417 | return message_type == RTMP_MSG_AMF0DataMessage; |
| 2179 | } | 1418 | } |
| 2180 | 1419 | ||
| 2181 | -bool SrsMessageHeader::is_amf3_command() | ||
| 2182 | -{ | ||
| 2183 | - return message_type == RTMP_MSG_AMF3CommandMessage; | ||
| 2184 | -} | ||
| 2185 | - | ||
| 2186 | -bool SrsMessageHeader::is_amf3_data() | ||
| 2187 | -{ | ||
| 2188 | - return message_type == RTMP_MSG_AMF3DataMessage; | ||
| 2189 | -} | ||
| 2190 | - | ||
| 2191 | -bool SrsMessageHeader::is_window_ackledgement_size() | ||
| 2192 | -{ | ||
| 2193 | - return message_type == RTMP_MSG_WindowAcknowledgementSize; | ||
| 2194 | -} | ||
| 2195 | - | ||
| 2196 | -bool SrsMessageHeader::is_ackledgement() | ||
| 2197 | -{ | ||
| 2198 | - return message_type == RTMP_MSG_Acknowledgement; | ||
| 2199 | -} | ||
| 2200 | - | ||
| 2201 | -bool SrsMessageHeader::is_set_chunk_size() | ||
| 2202 | -{ | ||
| 2203 | - return message_type == RTMP_MSG_SetChunkSize; | ||
| 2204 | -} | ||
| 2205 | - | ||
| 2206 | -bool SrsMessageHeader::is_user_control_message() | ||
| 2207 | -{ | ||
| 2208 | - return message_type == RTMP_MSG_UserControlMessage; | ||
| 2209 | -} | ||
| 2210 | - | ||
| 2211 | -void SrsMessageHeader::initialize_amf0_script(int size, int stream) | ||
| 2212 | -{ | ||
| 2213 | - message_type = RTMP_MSG_AMF0DataMessage; | ||
| 2214 | - payload_length = (int32_t)size; | ||
| 2215 | - timestamp_delta = (int32_t)0; | ||
| 2216 | - timestamp = (int64_t)0; | ||
| 2217 | - stream_id = (int32_t)stream; | ||
| 2218 | -} | ||
| 2219 | - | ||
| 2220 | -void SrsMessageHeader::initialize_audio(int size, u_int32_t time, int stream) | ||
| 2221 | -{ | ||
| 2222 | - message_type = RTMP_MSG_AudioMessage; | ||
| 2223 | - payload_length = (int32_t)size; | ||
| 2224 | - timestamp_delta = (int32_t)time; | ||
| 2225 | - timestamp = (int64_t)time; | ||
| 2226 | - stream_id = (int32_t)stream; | ||
| 2227 | -} | ||
| 2228 | - | ||
| 2229 | -void SrsMessageHeader::initialize_video(int size, u_int32_t time, int stream) | ||
| 2230 | -{ | ||
| 2231 | - message_type = RTMP_MSG_VideoMessage; | ||
| 2232 | - payload_length = (int32_t)size; | ||
| 2233 | - timestamp_delta = (int32_t)time; | ||
| 2234 | - timestamp = (int64_t)time; | ||
| 2235 | - stream_id = (int32_t)stream; | ||
| 2236 | -} | ||
| 2237 | - | ||
| 2238 | -SrsChunkStream::SrsChunkStream(int _cid) | ||
| 2239 | -{ | ||
| 2240 | - fmt = 0; | ||
| 2241 | - cid = _cid; | ||
| 2242 | - extended_timestamp = false; | ||
| 2243 | - msg = NULL; | ||
| 2244 | - msg_count = 0; | ||
| 2245 | -} | ||
| 2246 | - | ||
| 2247 | -SrsChunkStream::~SrsChunkStream() | ||
| 2248 | -{ | ||
| 2249 | - srs_freep(msg); | ||
| 2250 | -} | ||
| 2251 | - | ||
| 2252 | -__SrsChunkStream::__SrsChunkStream(int _cid) | ||
| 2253 | -{ | ||
| 2254 | - fmt = 0; | ||
| 2255 | - cid = _cid; | ||
| 2256 | - extended_timestamp = false; | ||
| 2257 | - msg = NULL; | ||
| 2258 | - msg_count = 0; | ||
| 2259 | -} | ||
| 2260 | - | ||
| 2261 | -__SrsChunkStream::~__SrsChunkStream() | ||
| 2262 | -{ | ||
| 2263 | - srs_freep(msg); | ||
| 2264 | -} | ||
| 2265 | - | ||
| 2266 | -__SrsMessage::__SrsMessage() | ||
| 2267 | -{ | ||
| 2268 | - payload = NULL; | ||
| 2269 | - size = 0; | ||
| 2270 | -} | ||
| 2271 | - | ||
| 2272 | -__SrsMessage::~__SrsMessage() | ||
| 2273 | -{ | ||
| 2274 | -} | ||
| 2275 | - | ||
| 2276 | -__SrsSharedPtrMessage::__SrsSharedPtr::__SrsSharedPtr() | ||
| 2277 | -{ | ||
| 2278 | - payload = NULL; | ||
| 2279 | - size = 0; | ||
| 2280 | - shared_count = 0; | ||
| 2281 | -} | ||
| 2282 | - | ||
| 2283 | -__SrsSharedPtrMessage::__SrsSharedPtr::~__SrsSharedPtr() | ||
| 2284 | -{ | ||
| 2285 | - srs_freepa(payload); | ||
| 2286 | -} | ||
| 2287 | - | ||
| 2288 | -__SrsSharedPtrMessage::__SrsSharedPtrMessage() | ||
| 2289 | -{ | ||
| 2290 | - ptr = NULL; | ||
| 2291 | -} | ||
| 2292 | - | ||
| 2293 | -__SrsSharedPtrMessage::~__SrsSharedPtrMessage() | ||
| 2294 | -{ | ||
| 2295 | - if (ptr) { | ||
| 2296 | - if (ptr->shared_count == 0) { | ||
| 2297 | - srs_freep(ptr); | ||
| 2298 | - } else { | ||
| 2299 | - ptr->shared_count--; | ||
| 2300 | - } | ||
| 2301 | - } | ||
| 2302 | -} | ||
| 2303 | - | ||
| 2304 | -int __SrsSharedPtrMessage::initialize(__SrsMessage* source) | ||
| 2305 | -{ | ||
| 2306 | - int ret = ERROR_SUCCESS; | ||
| 2307 | - | ||
| 2308 | - if ((ret = initialize(&source->header, (char*)source->payload, source->size)) != ERROR_SUCCESS) { | ||
| 2309 | - return ret; | ||
| 2310 | - } | ||
| 2311 | - | ||
| 2312 | - // detach the payload from source | ||
| 2313 | - source->payload = NULL; | ||
| 2314 | - source->size = 0; | ||
| 2315 | - | ||
| 2316 | - return ret; | ||
| 2317 | -} | ||
| 2318 | - | ||
| 2319 | -int __SrsSharedPtrMessage::initialize(SrsMessageHeader* source, char* payload, int size) | ||
| 2320 | -{ | ||
| 2321 | - int ret = ERROR_SUCCESS; | ||
| 2322 | - | ||
| 2323 | - srs_assert(source != NULL); | ||
| 2324 | - if (ptr) { | ||
| 2325 | - ret = ERROR_SYSTEM_ASSERT_FAILED; | ||
| 2326 | - srs_error("should not set the payload twice. ret=%d", ret); | ||
| 2327 | - srs_assert(false); | ||
| 2328 | - | ||
| 2329 | - return ret; | ||
| 2330 | - } | ||
| 2331 | - | ||
| 2332 | - header = *source; | ||
| 2333 | - header.payload_length = size; | ||
| 2334 | - | ||
| 2335 | - ptr = new __SrsSharedPtr(); | ||
| 2336 | - | ||
| 2337 | - // direct attach the data of common message. | ||
| 2338 | - ptr->payload = payload; | ||
| 2339 | - ptr->size = size; | ||
| 2340 | - | ||
| 2341 | - __SrsMessage::payload = (int8_t*)ptr->payload; | ||
| 2342 | - __SrsMessage::size = ptr->size; | ||
| 2343 | - | ||
| 2344 | - return ret; | ||
| 2345 | -} | ||
| 2346 | - | ||
| 2347 | -__SrsSharedPtrMessage* __SrsSharedPtrMessage::copy() | ||
| 2348 | -{ | ||
| 2349 | - if (!ptr) { | ||
| 2350 | - srs_error("invoke initialize to initialize the ptr."); | ||
| 2351 | - srs_assert(false); | ||
| 2352 | - return NULL; | ||
| 2353 | - } | ||
| 2354 | - | ||
| 2355 | - __SrsSharedPtrMessage* copy = new __SrsSharedPtrMessage(); | ||
| 2356 | - | ||
| 2357 | - copy->header = header; | ||
| 2358 | - | ||
| 2359 | - copy->ptr = ptr; | ||
| 2360 | - ptr->shared_count++; | ||
| 2361 | - | ||
| 2362 | - copy->payload = (int8_t*)ptr->payload; | ||
| 2363 | - copy->size = ptr->size; | ||
| 2364 | - | ||
| 2365 | - return copy; | ||
| 2366 | -} | ||
| 2367 | - | ||
| 2368 | -ISrsMessage::ISrsMessage() | 1420 | +bool SrsMessageHeader::is_amf3_command() |
| 2369 | { | 1421 | { |
| 2370 | - payload = NULL; | ||
| 2371 | - size = 0; | 1422 | + return message_type == RTMP_MSG_AMF3CommandMessage; |
| 2372 | } | 1423 | } |
| 2373 | 1424 | ||
| 2374 | -ISrsMessage::~ISrsMessage() | ||
| 2375 | -{ | 1425 | +bool SrsMessageHeader::is_amf3_data() |
| 1426 | +{ | ||
| 1427 | + return message_type == RTMP_MSG_AMF3DataMessage; | ||
| 2376 | } | 1428 | } |
| 2377 | 1429 | ||
| 2378 | -SrsCommonMessage::SrsCommonMessage() | 1430 | +bool SrsMessageHeader::is_window_ackledgement_size() |
| 2379 | { | 1431 | { |
| 2380 | - stream = NULL; | ||
| 2381 | - packet = NULL; | 1432 | + return message_type == RTMP_MSG_WindowAcknowledgementSize; |
| 2382 | } | 1433 | } |
| 2383 | 1434 | ||
| 2384 | -SrsCommonMessage::~SrsCommonMessage() | ||
| 2385 | -{ | ||
| 2386 | - // we must directly free the ptrs, | ||
| 2387 | - // nevery use the virtual functions to delete, | ||
| 2388 | - // for in the destructor, the virtual functions is disabled. | ||
| 2389 | - | ||
| 2390 | - srs_freepa(payload); | ||
| 2391 | - srs_freep(packet); | ||
| 2392 | - srs_freep(stream); | 1435 | +bool SrsMessageHeader::is_ackledgement() |
| 1436 | +{ | ||
| 1437 | + return message_type == RTMP_MSG_Acknowledgement; | ||
| 2393 | } | 1438 | } |
| 2394 | 1439 | ||
| 2395 | -bool SrsCommonMessage::can_decode() | 1440 | +bool SrsMessageHeader::is_set_chunk_size() |
| 2396 | { | 1441 | { |
| 2397 | - return true; | 1442 | + return message_type == RTMP_MSG_SetChunkSize; |
| 2398 | } | 1443 | } |
| 2399 | 1444 | ||
| 2400 | -int SrsCommonMessage::decode_packet(SrsProtocol* protocol) | 1445 | +bool SrsMessageHeader::is_user_control_message() |
| 2401 | { | 1446 | { |
| 2402 | - int ret = ERROR_SUCCESS; | ||
| 2403 | - | ||
| 2404 | - srs_assert(payload != NULL); | ||
| 2405 | - srs_assert(size > 0); | ||
| 2406 | - | ||
| 2407 | - if (packet) { | ||
| 2408 | - srs_verbose("msg already decoded"); | ||
| 2409 | - return ret; | ||
| 2410 | - } | ||
| 2411 | - | ||
| 2412 | - if (!stream) { | ||
| 2413 | - srs_verbose("create decode stream for message."); | ||
| 2414 | - stream = new SrsStream(); | ||
| 2415 | - } | ||
| 2416 | - | ||
| 2417 | - // initialize the decode stream for all message, | ||
| 2418 | - // it's ok for the initialize if fast and without memory copy. | ||
| 2419 | - if ((ret = stream->initialize((char*)payload, size)) != ERROR_SUCCESS) { | ||
| 2420 | - srs_error("initialize stream failed. ret=%d", ret); | ||
| 2421 | - return ret; | ||
| 2422 | - } | ||
| 2423 | - srs_verbose("decode stream initialized success"); | ||
| 2424 | - | ||
| 2425 | - // decode specified packet type | ||
| 2426 | - if (header.is_amf0_command() || header.is_amf3_command() || header.is_amf0_data() || header.is_amf3_data()) { | ||
| 2427 | - srs_verbose("start to decode AMF0/AMF3 command message."); | ||
| 2428 | - | ||
| 2429 | - // skip 1bytes to decode the amf3 command. | ||
| 2430 | - if (header.is_amf3_command() && stream->require(1)) { | ||
| 2431 | - srs_verbose("skip 1bytes to decode AMF3 command"); | ||
| 2432 | - stream->skip(1); | ||
| 2433 | - } | ||
| 2434 | - | ||
| 2435 | - // amf0 command message. | ||
| 2436 | - // need to read the command name. | ||
| 2437 | - std::string command; | ||
| 2438 | - if ((ret = srs_amf0_read_string(stream, command)) != ERROR_SUCCESS) { | ||
| 2439 | - srs_error("decode AMF0/AMF3 command name failed. ret=%d", ret); | ||
| 2440 | - return ret; | ||
| 2441 | - } | ||
| 2442 | - srs_verbose("AMF0/AMF3 command message, command_name=%s", command.c_str()); | ||
| 2443 | - | ||
| 2444 | - // result/error packet | ||
| 2445 | - if (command == RTMP_AMF0_COMMAND_RESULT || command == RTMP_AMF0_COMMAND_ERROR) { | ||
| 2446 | - double transactionId = 0.0; | ||
| 2447 | - if ((ret = srs_amf0_read_number(stream, transactionId)) != ERROR_SUCCESS) { | ||
| 2448 | - srs_error("decode AMF0/AMF3 transcationId failed. ret=%d", ret); | ||
| 2449 | - return ret; | ||
| 2450 | - } | ||
| 2451 | - srs_verbose("AMF0/AMF3 command id, transcationId=%.2f", transactionId); | ||
| 2452 | - | ||
| 2453 | - // reset stream, for header read completed. | ||
| 2454 | - stream->reset(); | ||
| 2455 | - if (header.is_amf3_command()) { | ||
| 2456 | - stream->skip(1); | ||
| 2457 | - } | ||
| 2458 | - | ||
| 2459 | - std::string request_name = protocol->get_request_name(transactionId); | ||
| 2460 | - if (request_name.empty()) { | ||
| 2461 | - ret = ERROR_RTMP_NO_REQUEST; | ||
| 2462 | - srs_error("decode AMF0/AMF3 request failed. ret=%d", ret); | ||
| 2463 | - return ret; | ||
| 2464 | - } | ||
| 2465 | - srs_verbose("AMF0/AMF3 request parsed. request_name=%s", request_name.c_str()); | ||
| 2466 | - | ||
| 2467 | - if (request_name == RTMP_AMF0_COMMAND_CONNECT) { | ||
| 2468 | - srs_info("decode the AMF0/AMF3 response command(%s message).", request_name.c_str()); | ||
| 2469 | - packet = new SrsConnectAppResPacket(); | ||
| 2470 | - return packet->decode(stream); | ||
| 2471 | - } else if (request_name == RTMP_AMF0_COMMAND_CREATE_STREAM) { | ||
| 2472 | - srs_info("decode the AMF0/AMF3 response command(%s message).", request_name.c_str()); | ||
| 2473 | - packet = new SrsCreateStreamResPacket(0, 0); | ||
| 2474 | - return packet->decode(stream); | ||
| 2475 | - } else if (request_name == RTMP_AMF0_COMMAND_RELEASE_STREAM | ||
| 2476 | - || request_name == RTMP_AMF0_COMMAND_FC_PUBLISH | ||
| 2477 | - || request_name == RTMP_AMF0_COMMAND_UNPUBLISH) { | ||
| 2478 | - srs_info("decode the AMF0/AMF3 response command(%s message).", request_name.c_str()); | ||
| 2479 | - packet = new SrsFMLEStartResPacket(0); | ||
| 2480 | - return packet->decode(stream); | ||
| 2481 | - } else { | ||
| 2482 | - ret = ERROR_RTMP_NO_REQUEST; | ||
| 2483 | - srs_error("decode AMF0/AMF3 request failed. " | ||
| 2484 | - "request_name=%s, transactionId=%.2f, ret=%d", | ||
| 2485 | - request_name.c_str(), transactionId, ret); | ||
| 2486 | - return ret; | ||
| 2487 | - } | ||
| 2488 | - } | ||
| 2489 | - | ||
| 2490 | - // reset to zero(amf3 to 1) to restart decode. | ||
| 2491 | - stream->reset(); | ||
| 2492 | - if (header.is_amf3_command()) { | ||
| 2493 | - stream->skip(1); | ||
| 2494 | - } | ||
| 2495 | - | ||
| 2496 | - // decode command object. | ||
| 2497 | - if (command == RTMP_AMF0_COMMAND_CONNECT) { | ||
| 2498 | - srs_info("decode the AMF0/AMF3 command(connect vhost/app message)."); | ||
| 2499 | - packet = new SrsConnectAppPacket(); | ||
| 2500 | - return packet->decode(stream); | ||
| 2501 | - } else if(command == RTMP_AMF0_COMMAND_CREATE_STREAM) { | ||
| 2502 | - srs_info("decode the AMF0/AMF3 command(createStream message)."); | ||
| 2503 | - packet = new SrsCreateStreamPacket(); | ||
| 2504 | - return packet->decode(stream); | ||
| 2505 | - } else if(command == RTMP_AMF0_COMMAND_PLAY) { | ||
| 2506 | - srs_info("decode the AMF0/AMF3 command(paly message)."); | ||
| 2507 | - packet = new SrsPlayPacket(); | ||
| 2508 | - return packet->decode(stream); | ||
| 2509 | - } else if(command == RTMP_AMF0_COMMAND_PAUSE) { | ||
| 2510 | - srs_info("decode the AMF0/AMF3 command(pause message)."); | ||
| 2511 | - packet = new SrsPausePacket(); | ||
| 2512 | - return packet->decode(stream); | ||
| 2513 | - } else if(command == RTMP_AMF0_COMMAND_RELEASE_STREAM) { | ||
| 2514 | - srs_info("decode the AMF0/AMF3 command(FMLE releaseStream message)."); | ||
| 2515 | - packet = new SrsFMLEStartPacket(); | ||
| 2516 | - return packet->decode(stream); | ||
| 2517 | - } else if(command == RTMP_AMF0_COMMAND_FC_PUBLISH) { | ||
| 2518 | - srs_info("decode the AMF0/AMF3 command(FMLE FCPublish message)."); | ||
| 2519 | - packet = new SrsFMLEStartPacket(); | ||
| 2520 | - return packet->decode(stream); | ||
| 2521 | - } else if(command == RTMP_AMF0_COMMAND_PUBLISH) { | ||
| 2522 | - srs_info("decode the AMF0/AMF3 command(publish message)."); | ||
| 2523 | - packet = new SrsPublishPacket(); | ||
| 2524 | - return packet->decode(stream); | ||
| 2525 | - } else if(command == RTMP_AMF0_COMMAND_UNPUBLISH) { | ||
| 2526 | - srs_info("decode the AMF0/AMF3 command(unpublish message)."); | ||
| 2527 | - packet = new SrsFMLEStartPacket(); | ||
| 2528 | - return packet->decode(stream); | ||
| 2529 | - } else if(command == RTMP_AMF0_DATA_SET_DATAFRAME || command == RTMP_AMF0_DATA_ON_METADATA) { | ||
| 2530 | - srs_info("decode the AMF0/AMF3 data(onMetaData message)."); | ||
| 2531 | - packet = new SrsOnMetaDataPacket(); | ||
| 2532 | - return packet->decode(stream); | ||
| 2533 | - } else if(command == SRS_BW_CHECK_FINISHED | ||
| 2534 | - || command == SRS_BW_CHECK_PLAYING | ||
| 2535 | - || command == SRS_BW_CHECK_PUBLISHING | ||
| 2536 | - || command == SRS_BW_CHECK_STARTING_PLAY | ||
| 2537 | - || command == SRS_BW_CHECK_STARTING_PUBLISH | ||
| 2538 | - || command == SRS_BW_CHECK_START_PLAY | ||
| 2539 | - || command == SRS_BW_CHECK_START_PUBLISH | ||
| 2540 | - || command == SRS_BW_CHECK_STOPPED_PLAY | ||
| 2541 | - || command == SRS_BW_CHECK_STOP_PLAY | ||
| 2542 | - || command == SRS_BW_CHECK_STOP_PUBLISH | ||
| 2543 | - || command == SRS_BW_CHECK_STOPPED_PUBLISH | ||
| 2544 | - || command == SRS_BW_CHECK_FLASH_FINAL) | ||
| 2545 | - { | ||
| 2546 | - srs_info("decode the AMF0/AMF3 band width check message."); | ||
| 2547 | - packet = new SrsBandwidthPacket(); | ||
| 2548 | - return packet->decode(stream); | ||
| 2549 | - } else if (command == RTMP_AMF0_COMMAND_CLOSE_STREAM) { | ||
| 2550 | - srs_info("decode the AMF0/AMF3 closeStream message."); | ||
| 2551 | - packet = new SrsCloseStreamPacket(); | ||
| 2552 | - return packet->decode(stream); | ||
| 2553 | - } | ||
| 2554 | - | ||
| 2555 | - // default packet to drop message. | ||
| 2556 | - srs_trace("drop the AMF0/AMF3 command message, command_name=%s", command.c_str()); | ||
| 2557 | - packet = new SrsPacket(); | ||
| 2558 | - return ret; | ||
| 2559 | - } else if(header.is_user_control_message()) { | ||
| 2560 | - srs_verbose("start to decode user control message."); | ||
| 2561 | - packet = new SrsUserControlPacket(); | ||
| 2562 | - return packet->decode(stream); | ||
| 2563 | - } else if(header.is_window_ackledgement_size()) { | ||
| 2564 | - srs_verbose("start to decode set ack window size message."); | ||
| 2565 | - packet = new SrsSetWindowAckSizePacket(); | ||
| 2566 | - return packet->decode(stream); | ||
| 2567 | - } else if(header.is_set_chunk_size()) { | ||
| 2568 | - srs_verbose("start to decode set chunk size message."); | ||
| 2569 | - packet = new SrsSetChunkSizePacket(); | ||
| 2570 | - return packet->decode(stream); | ||
| 2571 | - } else { | ||
| 2572 | - // default packet to drop message. | ||
| 2573 | - srs_trace("drop the unknown message, type=%d", header.message_type); | ||
| 2574 | - packet = new SrsPacket(); | ||
| 2575 | - } | ||
| 2576 | - | ||
| 2577 | - return ret; | 1447 | + return message_type == RTMP_MSG_UserControlMessage; |
| 2578 | } | 1448 | } |
| 2579 | 1449 | ||
| 2580 | -bool SrsCommonMessage::has_packet() | 1450 | +void SrsMessageHeader::initialize_amf0_script(int size, int stream) |
| 2581 | { | 1451 | { |
| 2582 | - return packet != NULL; | 1452 | + message_type = RTMP_MSG_AMF0DataMessage; |
| 1453 | + payload_length = (int32_t)size; | ||
| 1454 | + timestamp_delta = (int32_t)0; | ||
| 1455 | + timestamp = (int64_t)0; | ||
| 1456 | + stream_id = (int32_t)stream; | ||
| 2583 | } | 1457 | } |
| 2584 | 1458 | ||
| 2585 | -SrsPacket* SrsCommonMessage::get_packet() | 1459 | +void SrsMessageHeader::initialize_audio(int size, u_int32_t time, int stream) |
| 2586 | { | 1460 | { |
| 2587 | - if (!packet) { | ||
| 2588 | - srs_error("the payload is raw/undecoded, invoke decode_packet to decode it."); | ||
| 2589 | - } | ||
| 2590 | - srs_assert(packet != NULL); | ||
| 2591 | - | ||
| 2592 | - return packet; | 1461 | + message_type = RTMP_MSG_AudioMessage; |
| 1462 | + payload_length = (int32_t)size; | ||
| 1463 | + timestamp_delta = (int32_t)time; | ||
| 1464 | + timestamp = (int64_t)time; | ||
| 1465 | + stream_id = (int32_t)stream; | ||
| 2593 | } | 1466 | } |
| 2594 | 1467 | ||
| 2595 | -int SrsCommonMessage::get_perfer_cid() | 1468 | +void SrsMessageHeader::initialize_video(int size, u_int32_t time, int stream) |
| 2596 | { | 1469 | { |
| 2597 | - if (!packet) { | ||
| 2598 | - return RTMP_CID_ProtocolControl; | ||
| 2599 | - } | ||
| 2600 | - | ||
| 2601 | - // we donot use the complex basic header, | ||
| 2602 | - // ensure the basic header is 1bytes. | ||
| 2603 | - if (packet->get_perfer_cid() < 2) { | ||
| 2604 | - return packet->get_perfer_cid(); | ||
| 2605 | - } | ||
| 2606 | - | ||
| 2607 | - return packet->get_perfer_cid(); | 1470 | + message_type = RTMP_MSG_VideoMessage; |
| 1471 | + payload_length = (int32_t)size; | ||
| 1472 | + timestamp_delta = (int32_t)time; | ||
| 1473 | + timestamp = (int64_t)time; | ||
| 1474 | + stream_id = (int32_t)stream; | ||
| 2608 | } | 1475 | } |
| 2609 | 1476 | ||
| 2610 | -SrsCommonMessage* SrsCommonMessage::set_packet(SrsPacket* pkt, int stream_id) | 1477 | +__SrsChunkStream::__SrsChunkStream(int _cid) |
| 2611 | { | 1478 | { |
| 2612 | - srs_freep(packet); | 1479 | + fmt = 0; |
| 1480 | + cid = _cid; | ||
| 1481 | + extended_timestamp = false; | ||
| 1482 | + msg = NULL; | ||
| 1483 | + msg_count = 0; | ||
| 1484 | +} | ||
| 2613 | 1485 | ||
| 2614 | - packet = pkt; | ||
| 2615 | - | ||
| 2616 | - header.message_type = packet->get_message_type(); | ||
| 2617 | - header.payload_length = packet->get_payload_length(); | ||
| 2618 | - header.stream_id = stream_id; | ||
| 2619 | - | ||
| 2620 | - return this; | 1486 | +__SrsChunkStream::~__SrsChunkStream() |
| 1487 | +{ | ||
| 1488 | + srs_freep(msg); | ||
| 2621 | } | 1489 | } |
| 2622 | 1490 | ||
| 2623 | -int SrsCommonMessage::encode_packet() | 1491 | +__SrsMessage::__SrsMessage() |
| 2624 | { | 1492 | { |
| 2625 | - int ret = ERROR_SUCCESS; | ||
| 2626 | - | ||
| 2627 | - // sometimes, for example, the edge proxy, | ||
| 2628 | - // the payload is not decoded, so directly sent out. | ||
| 2629 | - if (payload != NULL) { | ||
| 2630 | - header.payload_length = size; | ||
| 2631 | - return ret; | ||
| 2632 | - } | ||
| 2633 | - | ||
| 2634 | - // encode packet to payload and size. | ||
| 2635 | - if (packet == NULL) { | ||
| 2636 | - srs_warn("packet is empty, send out empty message."); | ||
| 2637 | - return ret; | ||
| 2638 | - } | ||
| 2639 | - // realloc the payload. | 1493 | + payload = NULL; |
| 2640 | size = 0; | 1494 | size = 0; |
| 2641 | - srs_freepa(payload); | ||
| 2642 | - | ||
| 2643 | - if ((ret = packet->encode(size, (char*&)payload)) != ERROR_SUCCESS) { | ||
| 2644 | - return ret; | ||
| 2645 | - } | ||
| 2646 | - | ||
| 2647 | - header.payload_length = size; | ||
| 2648 | - | ||
| 2649 | - return ret; | ||
| 2650 | } | 1495 | } |
| 2651 | 1496 | ||
| 2652 | -SrsSharedPtrMessage::SrsSharedPtr::SrsSharedPtr() | 1497 | +__SrsMessage::~__SrsMessage() |
| 1498 | +{ | ||
| 1499 | +} | ||
| 1500 | + | ||
| 1501 | +__SrsSharedPtrMessage::__SrsSharedPtr::__SrsSharedPtr() | ||
| 2653 | { | 1502 | { |
| 2654 | payload = NULL; | 1503 | payload = NULL; |
| 2655 | size = 0; | 1504 | size = 0; |
| 2656 | - perfer_cid = 0; | ||
| 2657 | shared_count = 0; | 1505 | shared_count = 0; |
| 2658 | } | 1506 | } |
| 2659 | 1507 | ||
| 2660 | -SrsSharedPtrMessage::SrsSharedPtr::~SrsSharedPtr() | 1508 | +__SrsSharedPtrMessage::__SrsSharedPtr::~__SrsSharedPtr() |
| 2661 | { | 1509 | { |
| 2662 | srs_freepa(payload); | 1510 | srs_freepa(payload); |
| 2663 | } | 1511 | } |
| 2664 | 1512 | ||
| 2665 | -SrsSharedPtrMessage::SrsSharedPtrMessage() | 1513 | +__SrsSharedPtrMessage::__SrsSharedPtrMessage() |
| 2666 | { | 1514 | { |
| 2667 | ptr = NULL; | 1515 | ptr = NULL; |
| 2668 | } | 1516 | } |
| 2669 | 1517 | ||
| 2670 | -SrsSharedPtrMessage::~SrsSharedPtrMessage() | 1518 | +__SrsSharedPtrMessage::~__SrsSharedPtrMessage() |
| 2671 | { | 1519 | { |
| 2672 | if (ptr) { | 1520 | if (ptr) { |
| 2673 | if (ptr->shared_count == 0) { | 1521 | if (ptr->shared_count == 0) { |
| @@ -2678,12 +1526,7 @@ SrsSharedPtrMessage::~SrsSharedPtrMessage() | @@ -2678,12 +1526,7 @@ SrsSharedPtrMessage::~SrsSharedPtrMessage() | ||
| 2678 | } | 1526 | } |
| 2679 | } | 1527 | } |
| 2680 | 1528 | ||
| 2681 | -bool SrsSharedPtrMessage::can_decode() | ||
| 2682 | -{ | ||
| 2683 | - return false; | ||
| 2684 | -} | ||
| 2685 | - | ||
| 2686 | -int SrsSharedPtrMessage::initialize(SrsCommonMessage* source) | 1529 | +int __SrsSharedPtrMessage::initialize(__SrsMessage* source) |
| 2687 | { | 1530 | { |
| 2688 | int ret = ERROR_SUCCESS; | 1531 | int ret = ERROR_SUCCESS; |
| 2689 | 1532 | ||
| @@ -2698,7 +1541,7 @@ int SrsSharedPtrMessage::initialize(SrsCommonMessage* source) | @@ -2698,7 +1541,7 @@ int SrsSharedPtrMessage::initialize(SrsCommonMessage* source) | ||
| 2698 | return ret; | 1541 | return ret; |
| 2699 | } | 1542 | } |
| 2700 | 1543 | ||
| 2701 | -int SrsSharedPtrMessage::initialize(SrsMessageHeader* source, char* payload, int size) | 1544 | +int __SrsSharedPtrMessage::initialize(SrsMessageHeader* source, char* payload, int size) |
| 2702 | { | 1545 | { |
| 2703 | int ret = ERROR_SUCCESS; | 1546 | int ret = ERROR_SUCCESS; |
| 2704 | 1547 | ||
| @@ -2714,27 +1557,19 @@ int SrsSharedPtrMessage::initialize(SrsMessageHeader* source, char* payload, int | @@ -2714,27 +1557,19 @@ int SrsSharedPtrMessage::initialize(SrsMessageHeader* source, char* payload, int | ||
| 2714 | header = *source; | 1557 | header = *source; |
| 2715 | header.payload_length = size; | 1558 | header.payload_length = size; |
| 2716 | 1559 | ||
| 2717 | - ptr = new SrsSharedPtr(); | 1560 | + ptr = new __SrsSharedPtr(); |
| 2718 | 1561 | ||
| 2719 | // direct attach the data of common message. | 1562 | // direct attach the data of common message. |
| 2720 | ptr->payload = payload; | 1563 | ptr->payload = payload; |
| 2721 | ptr->size = size; | 1564 | ptr->size = size; |
| 2722 | 1565 | ||
| 2723 | - if (source->is_video()) { | ||
| 2724 | - ptr->perfer_cid = RTMP_CID_Video; | ||
| 2725 | - } else if (source->is_audio()) { | ||
| 2726 | - ptr->perfer_cid = RTMP_CID_Audio; | ||
| 2727 | - } else { | ||
| 2728 | - ptr->perfer_cid = RTMP_CID_OverConnection2; | ||
| 2729 | - } | ||
| 2730 | - | ||
| 2731 | - ISrsMessage::payload = (int8_t*)ptr->payload; | ||
| 2732 | - ISrsMessage::size = ptr->size; | 1566 | + __SrsMessage::payload = (int8_t*)ptr->payload; |
| 1567 | + __SrsMessage::size = ptr->size; | ||
| 2733 | 1568 | ||
| 2734 | return ret; | 1569 | return ret; |
| 2735 | } | 1570 | } |
| 2736 | 1571 | ||
| 2737 | -SrsSharedPtrMessage* SrsSharedPtrMessage::copy() | 1572 | +__SrsSharedPtrMessage* __SrsSharedPtrMessage::copy() |
| 2738 | { | 1573 | { |
| 2739 | if (!ptr) { | 1574 | if (!ptr) { |
| 2740 | srs_error("invoke initialize to initialize the ptr."); | 1575 | srs_error("invoke initialize to initialize the ptr."); |
| @@ -2742,7 +1577,7 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy() | @@ -2742,7 +1577,7 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy() | ||
| 2742 | return NULL; | 1577 | return NULL; |
| 2743 | } | 1578 | } |
| 2744 | 1579 | ||
| 2745 | - SrsSharedPtrMessage* copy = new SrsSharedPtrMessage(); | 1580 | + __SrsSharedPtrMessage* copy = new __SrsSharedPtrMessage(); |
| 2746 | 1581 | ||
| 2747 | copy->header = header; | 1582 | copy->header = header; |
| 2748 | 1583 | ||
| @@ -2755,21 +1590,6 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy() | @@ -2755,21 +1590,6 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy() | ||
| 2755 | return copy; | 1590 | return copy; |
| 2756 | } | 1591 | } |
| 2757 | 1592 | ||
| 2758 | -int SrsSharedPtrMessage::get_perfer_cid() | ||
| 2759 | -{ | ||
| 2760 | - if (!ptr) { | ||
| 2761 | - return 0; | ||
| 2762 | - } | ||
| 2763 | - | ||
| 2764 | - return ptr->perfer_cid; | ||
| 2765 | -} | ||
| 2766 | - | ||
| 2767 | -int SrsSharedPtrMessage::encode_packet() | ||
| 2768 | -{ | ||
| 2769 | - srs_verbose("shared message ignore the encode method."); | ||
| 2770 | - return ERROR_SUCCESS; | ||
| 2771 | -} | ||
| 2772 | - | ||
| 2773 | SrsPacket::SrsPacket() | 1593 | SrsPacket::SrsPacket() |
| 2774 | { | 1594 | { |
| 2775 | } | 1595 | } |
| @@ -40,11 +40,8 @@ class ISrsProtocolReaderWriter; | @@ -40,11 +40,8 @@ class ISrsProtocolReaderWriter; | ||
| 40 | class SrsBuffer; | 40 | class SrsBuffer; |
| 41 | class SrsPacket; | 41 | class SrsPacket; |
| 42 | class SrsStream; | 42 | class SrsStream; |
| 43 | -class SrsCommonMessage; | ||
| 44 | -class SrsChunkStream; | ||
| 45 | class SrsAmf0Object; | 43 | class SrsAmf0Object; |
| 46 | class SrsAmf0Any; | 44 | class SrsAmf0Any; |
| 47 | -class ISrsMessage; | ||
| 48 | class SrsMessageHeader; | 45 | class SrsMessageHeader; |
| 49 | class __SrsMessage; | 46 | class __SrsMessage; |
| 50 | class __SrsChunkStream; | 47 | class __SrsChunkStream; |
| @@ -112,7 +109,6 @@ private: | @@ -112,7 +109,6 @@ private: | ||
| 112 | std::map<double, std::string> requests; | 109 | std::map<double, std::string> requests; |
| 113 | // peer in | 110 | // peer in |
| 114 | private: | 111 | private: |
| 115 | - std::map<int, SrsChunkStream*> chunk_streams; | ||
| 116 | // TODO: FIXME: rename to chunk_streams | 112 | // TODO: FIXME: rename to chunk_streams |
| 117 | std::map<int, __SrsChunkStream*> __chunk_streams; | 113 | std::map<int, __SrsChunkStream*> __chunk_streams; |
| 118 | SrsStream* decode_stream; | 114 | SrsStream* decode_stream; |
| @@ -220,58 +216,9 @@ private: | @@ -220,58 +216,9 @@ private: | ||
| 220 | * when message sentout, update the context. | 216 | * when message sentout, update the context. |
| 221 | */ | 217 | */ |
| 222 | virtual int __on_send_message(__SrsMessage* msg, SrsPacket* packet); | 218 | virtual int __on_send_message(__SrsMessage* msg, SrsPacket* packet); |
| 223 | -public: | ||
| 224 | - /** | ||
| 225 | - * recv a message with raw/undecoded payload from peer. | ||
| 226 | - * the payload is not decoded, use srs_rtmp_expect_message<T> if requires | ||
| 227 | - * specifies message. | ||
| 228 | - * @pmsg, user must free it. NULL if not success. | ||
| 229 | - * @remark, only when success, user can use and must free the pmsg. | ||
| 230 | - */ | ||
| 231 | - virtual int recv_message(SrsCommonMessage** pmsg); | ||
| 232 | - /** | ||
| 233 | - * send out message with encoded payload to peer. | ||
| 234 | - * use the message encode method to encode to payload, | ||
| 235 | - * then sendout over socket. | ||
| 236 | - * @msg this method will free it whatever return value. | ||
| 237 | - */ | ||
| 238 | - virtual int send_message(ISrsMessage* msg); | ||
| 239 | private: | 219 | private: |
| 240 | - /** | ||
| 241 | - * when recv message, update the context. | ||
| 242 | - */ | ||
| 243 | - virtual int on_recv_message(SrsCommonMessage* msg); | ||
| 244 | virtual int response_acknowledgement_message(); | 220 | virtual int response_acknowledgement_message(); |
| 245 | virtual int response_ping_message(int32_t timestamp); | 221 | virtual int response_ping_message(int32_t timestamp); |
| 246 | - /** | ||
| 247 | - * when message sentout, update the context. | ||
| 248 | - */ | ||
| 249 | - virtual int on_send_message(ISrsMessage* msg); | ||
| 250 | - /** | ||
| 251 | - * try to recv interlaced message from peer, | ||
| 252 | - * return error if error occur and nerver set the pmsg, | ||
| 253 | - * return success and pmsg set to NULL if no entire message got, | ||
| 254 | - * return success and pmsg set to entire message if got one. | ||
| 255 | - */ | ||
| 256 | - virtual int recv_interlaced_message(SrsCommonMessage** pmsg); | ||
| 257 | - /** | ||
| 258 | - * read the chunk basic header(fmt, cid) from chunk stream. | ||
| 259 | - * user can discovery a SrsChunkStream by cid. | ||
| 260 | - * @bh_size return the chunk basic header size, to remove the used bytes when finished. | ||
| 261 | - */ | ||
| 262 | - virtual int read_basic_header(char& fmt, int& cid, int& bh_size); | ||
| 263 | - /** | ||
| 264 | - * read the chunk message header(timestamp, payload_length, message_type, stream_id) | ||
| 265 | - * from chunk stream and save to SrsChunkStream. | ||
| 266 | - * @mh_size return the chunk message header size, to remove the used bytes when finished. | ||
| 267 | - */ | ||
| 268 | - virtual int read_message_header(SrsChunkStream* chunk, char fmt, int bh_size, int& mh_size); | ||
| 269 | - /** | ||
| 270 | - * read the chunk payload, remove the used bytes in buffer, | ||
| 271 | - * if got entire message, set the pmsg. | ||
| 272 | - * @payload_size read size in this roundtrip, generally a chunk size or left message size. | ||
| 273 | - */ | ||
| 274 | - virtual int read_message_payload(SrsChunkStream* chunk, int bh_size, int mh_size, int& payload_size, SrsCommonMessage** pmsg); | ||
| 275 | }; | 222 | }; |
| 276 | 223 | ||
| 277 | /** | 224 | /** |
| @@ -342,44 +289,6 @@ public: | @@ -342,44 +289,6 @@ public: | ||
| 342 | * incoming chunk stream maybe interlaced, | 289 | * incoming chunk stream maybe interlaced, |
| 343 | * use the chunk stream to cache the input RTMP chunk streams. | 290 | * use the chunk stream to cache the input RTMP chunk streams. |
| 344 | */ | 291 | */ |
| 345 | -class SrsChunkStream | ||
| 346 | -{ | ||
| 347 | -public: | ||
| 348 | - /** | ||
| 349 | - * represents the basic header fmt, | ||
| 350 | - * which used to identify the variant message header type. | ||
| 351 | - */ | ||
| 352 | - char fmt; | ||
| 353 | - /** | ||
| 354 | - * represents the basic header cid, | ||
| 355 | - * which is the chunk stream id. | ||
| 356 | - */ | ||
| 357 | - int cid; | ||
| 358 | - /** | ||
| 359 | - * cached message header | ||
| 360 | - */ | ||
| 361 | - SrsMessageHeader header; | ||
| 362 | - /** | ||
| 363 | - * whether the chunk message header has extended timestamp. | ||
| 364 | - */ | ||
| 365 | - bool extended_timestamp; | ||
| 366 | - /** | ||
| 367 | - * partially read message. | ||
| 368 | - */ | ||
| 369 | - SrsCommonMessage* msg; | ||
| 370 | - /** | ||
| 371 | - * decoded msg count, to identify whether the chunk stream is fresh. | ||
| 372 | - */ | ||
| 373 | - int64_t msg_count; | ||
| 374 | -public: | ||
| 375 | - SrsChunkStream(int _cid); | ||
| 376 | - virtual ~SrsChunkStream(); | ||
| 377 | -}; | ||
| 378 | - | ||
| 379 | -/** | ||
| 380 | -* incoming chunk stream maybe interlaced, | ||
| 381 | -* use the chunk stream to cache the input RTMP chunk streams. | ||
| 382 | -*/ | ||
| 383 | class __SrsChunkStream | 292 | class __SrsChunkStream |
| 384 | { | 293 | { |
| 385 | public: | 294 | public: |
| @@ -480,156 +389,6 @@ public: | @@ -480,156 +389,6 @@ public: | ||
| 480 | }; | 389 | }; |
| 481 | 390 | ||
| 482 | /** | 391 | /** |
| 483 | -* message to output. | ||
| 484 | -*/ | ||
| 485 | -class ISrsMessage | ||
| 486 | -{ | ||
| 487 | -// 4.1. Message Header | ||
| 488 | -public: | ||
| 489 | - SrsMessageHeader header; | ||
| 490 | -// 4.2. Message Payload | ||
| 491 | -public: | ||
| 492 | - /** | ||
| 493 | - * The other part which is the payload is the actual data that is | ||
| 494 | - * contained in the message. For example, it could be some audio samples | ||
| 495 | - * or compressed video data. The payload format and interpretation are | ||
| 496 | - * beyond the scope of this document. | ||
| 497 | - */ | ||
| 498 | - int32_t size; | ||
| 499 | - int8_t* payload; | ||
| 500 | -public: | ||
| 501 | - ISrsMessage(); | ||
| 502 | - virtual ~ISrsMessage(); | ||
| 503 | -public: | ||
| 504 | - /** | ||
| 505 | - * whether message canbe decoded. | ||
| 506 | - * only update the context when message canbe decoded. | ||
| 507 | - */ | ||
| 508 | - virtual bool can_decode() = 0; | ||
| 509 | -/** | ||
| 510 | -* encode functions. | ||
| 511 | -*/ | ||
| 512 | -public: | ||
| 513 | - /** | ||
| 514 | - * get the perfered cid(chunk stream id) which sendout over. | ||
| 515 | - */ | ||
| 516 | - virtual int get_perfer_cid() = 0; | ||
| 517 | - /** | ||
| 518 | - * encode the packet to message payload bytes. | ||
| 519 | - * @remark there exists empty packet, so maybe the payload is NULL. | ||
| 520 | - */ | ||
| 521 | - virtual int encode_packet() = 0; | ||
| 522 | -}; | ||
| 523 | - | ||
| 524 | -/** | ||
| 525 | -* common RTMP message defines in rtmp.part2.Message-Formats.pdf. | ||
| 526 | -* cannbe parse and decode. | ||
| 527 | -*/ | ||
| 528 | -class SrsCommonMessage : public ISrsMessage | ||
| 529 | -{ | ||
| 530 | -private: | ||
| 531 | - disable_default_copy(SrsCommonMessage); | ||
| 532 | -// decoded message payload. | ||
| 533 | -private: | ||
| 534 | - SrsStream* stream; | ||
| 535 | - SrsPacket* packet; | ||
| 536 | -public: | ||
| 537 | - SrsCommonMessage(); | ||
| 538 | - virtual ~SrsCommonMessage(); | ||
| 539 | -public: | ||
| 540 | - virtual bool can_decode(); | ||
| 541 | -/** | ||
| 542 | -* decode functions. | ||
| 543 | -*/ | ||
| 544 | -public: | ||
| 545 | - /** | ||
| 546 | - * decode packet from message payload. | ||
| 547 | - */ | ||
| 548 | - // TODO: use protocol to decode it. | ||
| 549 | - virtual int decode_packet(SrsProtocol* protocol); | ||
| 550 | - /** | ||
| 551 | - * whether msg has decoded packet. | ||
| 552 | - */ | ||
| 553 | - virtual bool has_packet(); | ||
| 554 | - /** | ||
| 555 | - * get the decoded packet which decoded by decode_packet(). | ||
| 556 | - * @remark, user never free the pkt, the message will auto free it. | ||
| 557 | - */ | ||
| 558 | - virtual SrsPacket* get_packet(); | ||
| 559 | -/** | ||
| 560 | -* encode functions. | ||
| 561 | -*/ | ||
| 562 | -public: | ||
| 563 | - /** | ||
| 564 | - * get the perfered cid(chunk stream id) which sendout over. | ||
| 565 | - */ | ||
| 566 | - virtual int get_perfer_cid(); | ||
| 567 | - /** | ||
| 568 | - * set the encoded packet to encode_packet() to payload. | ||
| 569 | - * @stream_id, the id of stream which is created by createStream. | ||
| 570 | - * @remark, user never free the pkt, the message will auto free it. | ||
| 571 | - * @return message itself. | ||
| 572 | - */ | ||
| 573 | - // TODO: refine the send methods. | ||
| 574 | - virtual SrsCommonMessage* set_packet(SrsPacket* pkt, int stream_id); | ||
| 575 | - /** | ||
| 576 | - * encode the packet to message payload bytes. | ||
| 577 | - * @remark there exists empty packet, so maybe the payload is NULL. | ||
| 578 | - */ | ||
| 579 | - virtual int encode_packet(); | ||
| 580 | -}; | ||
| 581 | - | ||
| 582 | -/** | ||
| 583 | -* shared ptr message. | ||
| 584 | -* for audio/video/data message that need less memory copy. | ||
| 585 | -* and only for output. | ||
| 586 | -*/ | ||
| 587 | -class SrsSharedPtrMessage : public ISrsMessage | ||
| 588 | -{ | ||
| 589 | -private: | ||
| 590 | - struct SrsSharedPtr | ||
| 591 | - { | ||
| 592 | - char* payload; | ||
| 593 | - int size; | ||
| 594 | - int perfer_cid; | ||
| 595 | - int shared_count; | ||
| 596 | - | ||
| 597 | - SrsSharedPtr(); | ||
| 598 | - virtual ~SrsSharedPtr(); | ||
| 599 | - }; | ||
| 600 | - SrsSharedPtr* ptr; | ||
| 601 | -public: | ||
| 602 | - SrsSharedPtrMessage(); | ||
| 603 | - virtual ~SrsSharedPtrMessage(); | ||
| 604 | -public: | ||
| 605 | - virtual bool can_decode(); | ||
| 606 | -public: | ||
| 607 | - /** | ||
| 608 | - * set the shared payload. | ||
| 609 | - * we will detach the payload of source, | ||
| 610 | - * so ensure donot use it before. | ||
| 611 | - */ | ||
| 612 | - virtual int initialize(SrsCommonMessage* source); | ||
| 613 | - /** | ||
| 614 | - * set the shared payload. | ||
| 615 | - * use source header, and specified param payload. | ||
| 616 | - */ | ||
| 617 | - virtual int initialize(SrsMessageHeader* source, char* payload, int size); | ||
| 618 | - virtual SrsSharedPtrMessage* copy(); | ||
| 619 | -public: | ||
| 620 | - /** | ||
| 621 | - * get the perfered cid(chunk stream id) which sendout over. | ||
| 622 | - */ | ||
| 623 | - virtual int get_perfer_cid(); | ||
| 624 | - /** | ||
| 625 | - * ignored. | ||
| 626 | - * for shared message, nothing should be done. | ||
| 627 | - * use initialize() to set the data. | ||
| 628 | - */ | ||
| 629 | - virtual int encode_packet(); | ||
| 630 | -}; | ||
| 631 | - | ||
| 632 | -/** | ||
| 633 | * the decoded message payload. | 392 | * the decoded message payload. |
| 634 | * @remark we seperate the packet from message, | 393 | * @remark we seperate the packet from message, |
| 635 | * for the packet focus on logic and domain data, | 394 | * for the packet focus on logic and domain data, |
| @@ -1374,44 +1133,6 @@ protected: | @@ -1374,44 +1133,6 @@ protected: | ||
| 1374 | * if need to set timeout, use set timeout of SrsProtocol. | 1133 | * if need to set timeout, use set timeout of SrsProtocol. |
| 1375 | */ | 1134 | */ |
| 1376 | template<class T> | 1135 | template<class T> |
| 1377 | -int srs_rtmp_expect_message(SrsProtocol* protocol, SrsCommonMessage** pmsg, T** ppacket) | ||
| 1378 | -{ | ||
| 1379 | - *pmsg = NULL; | ||
| 1380 | - *ppacket = NULL; | ||
| 1381 | - | ||
| 1382 | - int ret = ERROR_SUCCESS; | ||
| 1383 | - | ||
| 1384 | - while (true) { | ||
| 1385 | - SrsCommonMessage* msg = NULL; | ||
| 1386 | - if ((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS) { | ||
| 1387 | - srs_error("recv message failed. ret=%d", ret); | ||
| 1388 | - return ret; | ||
| 1389 | - } | ||
| 1390 | - srs_verbose("recv message success."); | ||
| 1391 | - | ||
| 1392 | - if ((ret = msg->decode_packet(protocol)) != ERROR_SUCCESS) { | ||
| 1393 | - delete msg; | ||
| 1394 | - srs_error("decode message failed. ret=%d", ret); | ||
| 1395 | - return ret; | ||
| 1396 | - } | ||
| 1397 | - | ||
| 1398 | - T* pkt = dynamic_cast<T*>(msg->get_packet()); | ||
| 1399 | - if (!pkt) { | ||
| 1400 | - delete msg; | ||
| 1401 | - srs_trace("drop message(type=%d, size=%d, time=%"PRId64", sid=%d).", | ||
| 1402 | - msg->header.message_type, msg->header.payload_length, | ||
| 1403 | - msg->header.timestamp, msg->header.stream_id); | ||
| 1404 | - continue; | ||
| 1405 | - } | ||
| 1406 | - | ||
| 1407 | - *pmsg = msg; | ||
| 1408 | - *ppacket = pkt; | ||
| 1409 | - break; | ||
| 1410 | - } | ||
| 1411 | - | ||
| 1412 | - return ret; | ||
| 1413 | -} | ||
| 1414 | -template<class T> | ||
| 1415 | int __srs_rtmp_expect_message(SrsProtocol* protocol, __SrsMessage** pmsg, T** ppacket) | 1136 | int __srs_rtmp_expect_message(SrsProtocol* protocol, __SrsMessage** pmsg, T** ppacket) |
| 1416 | { | 1137 | { |
| 1417 | *pmsg = NULL; | 1138 | *pmsg = NULL; |
-
请 注册 或 登录 后发表评论