From 23d2602c34433d60123a558cbf265cb7cdc612fb Mon Sep 17 00:00:00 2001 From: Winlin Date: Tue, 9 Jul 2024 10:29:36 +0800 Subject: [PATCH] UniquePtr: Support SrsUniquePtr to replace SrsAutoFree. v6.0.136 (#4109) To manage an object: ```cpp // Before MyClass* ptr = new MyClass(); SrsAutoFree(MyClass, ptr); ptr->do_something(); // Now SrsUniquePtr ptr(new MyClass()); ptr->do_something(); ``` To manage an array of objects: ```cpp // Before char* ptr = new char[10]; SrsAutoFreeA(char, ptr); ptr[0] = 0xf; // Now SrsUniquePtr ptr(new char[10]); ptr[0] = 0xf; ``` In fact, SrsUniquePtr is a limited subset of SrsAutoFree, mainly managing pointers and arrays. SrsUniquePtr is better than SrsAutoFree because it has the same API to standard unique ptr. ```cpp SrsUniquePtr ptr(new MyClass()); ptr->do_something(); MyClass* p = ptr.get(); ``` SrsAutoFree actually uses a pointer to a pointer, so it can be set to NULL, allowing the pointer's value to be changed later (this usage is different from SrsUniquePtr). ```cpp // OK to free ptr correctly. MyClass* ptr; SrsAutoFree(MyClass, ptr); ptr = new MyClass(); // Crash because ptr is an invalid pointer. MyClass* ptr; SrsUniquePtr ptr(ptr); ptr = new MyClass(); ``` Additionally, SrsAutoFreeH can use specific release functions, which SrsUniquePtr does not support. --------- Co-authored-by: Jacob Su --- trunk/3rdparty/st-srs/README.md | 1 + trunk/3rdparty/st-srs/stk.c | 6 +- trunk/3rdparty/st-srs/utest/st_utest.hpp | 2 +- trunk/configure | 2 +- trunk/doc/CHANGELOG.md | 1 + trunk/ide/srs_clion/CMakeLists.txt | 4 +- trunk/scripts/copy_to_gits.sh | 14 +- trunk/src/app/srs_app_caster_flv.cpp | 5 +- trunk/src/app/srs_app_config.cpp | 28 +- trunk/src/app/srs_app_conn.cpp | 9 +- trunk/src/app/srs_app_dash.cpp | 14 +- trunk/src/app/srs_app_dvr.cpp | 67 ++-- trunk/src/app/srs_app_edge.cpp | 51 ++- trunk/src/app/srs_app_forward.cpp | 7 +- trunk/src/app/srs_app_gb28181.cpp | 19 +- trunk/src/app/srs_app_hds.cpp | 24 +- trunk/src/app/srs_app_heartbeat.cpp | 14 +- trunk/src/app/srs_app_hls.cpp | 18 +- trunk/src/app/srs_app_http_api.cpp | 102 ++--- trunk/src/app/srs_app_http_conn.cpp | 25 +- trunk/src/app/srs_app_http_hooks.cpp | 88 ++--- trunk/src/app/srs_app_http_static.cpp | 40 +- trunk/src/app/srs_app_http_stream.cpp | 100 +++-- trunk/src/app/srs_app_latest_version.cpp | 12 +- trunk/src/app/srs_app_listener.cpp | 6 +- trunk/src/app/srs_app_mpegts_udp.cpp | 15 +- trunk/src/app/srs_app_pithy_print.hpp | 3 +- trunk/src/app/srs_app_recv_thread.cpp | 6 +- trunk/src/app/srs_app_rtc_api.cpp | 27 +- trunk/src/app/srs_app_rtc_conn.cpp | 51 ++- trunk/src/app/srs_app_rtc_dtls.cpp | 23 +- trunk/src/app/srs_app_rtc_network.cpp | 10 +- trunk/src/app/srs_app_rtc_source.cpp | 37 +- trunk/src/app/srs_app_rtmp_conn.cpp | 99 +++-- trunk/src/app/srs_app_source.cpp | 26 +- trunk/src/app/srs_app_srt_conn.cpp | 31 +- trunk/src/app/srs_app_srt_source.cpp | 26 +- trunk/src/app/srs_app_tencentcloud.cpp | 43 +-- trunk/src/core/srs_core_autofree.hpp | 151 +++++--- trunk/src/core/srs_core_deprecated.cpp | 8 + trunk/src/core/srs_core_deprecated.hpp | 95 +++++ trunk/src/core/srs_core_version6.hpp | 2 +- trunk/src/kernel/srs_kernel_aac.cpp | 7 +- trunk/src/kernel/srs_kernel_codec.cpp | 29 +- trunk/src/kernel/srs_kernel_flv.cpp | 31 +- trunk/src/kernel/srs_kernel_mp3.cpp | 7 +- trunk/src/kernel/srs_kernel_mp4.cpp | 159 ++++---- trunk/src/kernel/srs_kernel_ps.cpp | 5 +- trunk/src/kernel/srs_kernel_ts.cpp | 99 +++-- trunk/src/kernel/srs_kernel_utility.cpp | 1 + trunk/src/main/srs_main_ingest_hls.cpp | 48 ++- trunk/src/main/srs_main_mp4_parser.cpp | 20 +- .../src/protocol/srs_protocol_http_client.cpp | 9 +- .../src/protocol/srs_protocol_http_stack.cpp | 14 +- trunk/src/protocol/srs_protocol_raw_avc.cpp | 32 +- trunk/src/protocol/srs_protocol_rtc_stun.cpp | 15 +- .../protocol/srs_protocol_rtmp_handshake.cpp | 82 ++-- .../src/protocol/srs_protocol_rtmp_stack.cpp | 238 ++++++------ trunk/src/protocol/srs_protocol_srt.cpp | 1 + trunk/src/protocol/srs_protocol_st.cpp | 1 + trunk/src/protocol/srs_protocol_utility.cpp | 7 +- trunk/src/utest/srs_utest_amf0.cpp | 276 ++++++------- trunk/src/utest/srs_utest_core.cpp | 82 +++- trunk/src/utest/srs_utest_gb28181.cpp | 61 +-- trunk/src/utest/srs_utest_http.cpp | 24 +- trunk/src/utest/srs_utest_kernel.cpp | 32 +- trunk/src/utest/srs_utest_protocol.cpp | 120 +++--- trunk/src/utest/srs_utest_protocol2.cpp | 364 ++++++++++-------- trunk/src/utest/srs_utest_rtc.cpp | 20 +- trunk/src/utest/srs_utest_rtmp.cpp | 245 ++++++------ trunk/src/utest/srs_utest_service.cpp | 47 +-- trunk/src/utest/srs_utest_srt.cpp | 3 +- 72 files changed, 1721 insertions(+), 1670 deletions(-) create mode 100644 trunk/src/core/srs_core_deprecated.cpp create mode 100644 trunk/src/core/srs_core_deprecated.hpp diff --git a/trunk/3rdparty/st-srs/README.md b/trunk/3rdparty/st-srs/README.md index 67d224ab2a..e1959a7349 100644 --- a/trunk/3rdparty/st-srs/README.md +++ b/trunk/3rdparty/st-srs/README.md @@ -119,6 +119,7 @@ The branch [srs](https://github.com/ossrs/state-threads/tree/srs) was patched an - [x] Check capability for backtrack. - [x] Support set specifics for any thread. - [x] Support st_destroy to free resources for asan. +- [x] Support free the stack, [#38](https://github.com/ossrs/state-threads/issues/38). - [ ] System: Support sendmmsg for UDP, [#12](https://github.com/ossrs/state-threads/issues/12). ## GDB Tools diff --git a/trunk/3rdparty/st-srs/stk.c b/trunk/3rdparty/st-srs/stk.c index ed5a932fbb..02088a42bb 100644 --- a/trunk/3rdparty/st-srs/stk.c +++ b/trunk/3rdparty/st-srs/stk.c @@ -65,6 +65,7 @@ _st_stack_t *_st_stack_new(int stack_size) _st_stack_t *ts; int extra; + /* If cache stack, we try to use stack from the cache list. */ #ifdef MD_CACHE_STACK for (qp = _st_free_stacks.next; qp != &_st_free_stacks; qp = qp->next) { ts = _ST_THREAD_STACK_PTR(qp); @@ -80,10 +81,13 @@ _st_stack_t *_st_stack_new(int stack_size) #endif extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0; + /* If not cache stack, we will free all stack in the list, which contains the stack to be freed. + * Note that we should never directly free it at _st_stack_free, because it is still be used, + * and will cause crash. */ #ifndef MD_CACHE_STACK for (qp = _st_free_stacks.next; qp != &_st_free_stacks;) { ts = _ST_THREAD_STACK_PTR(qp); - // Before qp is freed, move to next one, because the qp will be freed when free the ts. + /* Before qp is freed, move to next one, because the qp will be freed when free the ts. */ qp = qp->next; ST_REMOVE_LINK(&ts->links); diff --git a/trunk/3rdparty/st-srs/utest/st_utest.hpp b/trunk/3rdparty/st-srs/utest/st_utest.hpp index 470b9ed2af..26d993398e 100644 --- a/trunk/3rdparty/st-srs/utest/st_utest.hpp +++ b/trunk/3rdparty/st-srs/utest/st_utest.hpp @@ -44,7 +44,7 @@ struct ErrorObject { }; extern std::ostream& operator<<(std::ostream& out, const ErrorObject* err); #define ST_ASSERT_ERROR(error, r0, message) if (error) return new ErrorObject(r0, message) -#define ST_COROUTINE_JOIN(trd, r0) ErrorObject* r0 = NULL; SrsAutoFree(ErrorObject, r0); if (trd) st_thread_join(trd, (void**)&r0) +#define ST_COROUTINE_JOIN(trd, r0) ErrorObject* r0 = NULL; if (trd) st_thread_join(trd, (void**)&r0); SrsUniquePtr r0_uptr(r0) #define ST_EXPECT_SUCCESS(r0) EXPECT_TRUE(!r0) << r0 #define ST_EXPECT_FAILED(r0) EXPECT_TRUE(r0) << r0 diff --git a/trunk/configure b/trunk/configure index 355d484329..10cea9f86d 100755 --- a/trunk/configure +++ b/trunk/configure @@ -269,7 +269,7 @@ MODULE_ID="CORE" MODULE_DEPENDS=() ModuleLibIncs=(${SRS_OBJS}) MODULE_FILES=("srs_core" "srs_core_version" "srs_core_version5" "srs_core_autofree" "srs_core_performance" - "srs_core_time" "srs_core_platform") + "srs_core_time" "srs_core_platform" "srs_core_deprecated") CORE_INCS="src/core"; MODULE_DIR=${CORE_INCS} . $SRS_WORKDIR/auto/modules.sh CORE_OBJS="${MODULE_OBJS[@]}" # diff --git a/trunk/doc/CHANGELOG.md b/trunk/doc/CHANGELOG.md index a6d206da55..58b7ab7515 100644 --- a/trunk/doc/CHANGELOG.md +++ b/trunk/doc/CHANGELOG.md @@ -7,6 +7,7 @@ The changelog for SRS. ## SRS 6.0 Changelog +* v6.0, 2024-07-09, Merge [#4109](https://github.com/ossrs/srs/pull/4109): UniquePtr: Support SrsUniquePtr to replace SrsAutoFree. v6.0.136 (#4109) * v6.0, 2024-07-08, Merge [#4042](https://github.com/ossrs/srs/pull/4042): Refine config directive token parsing. v6.0.135 (#4042) * v6.0, 2024-07-04, Merge [#4106](https://github.com/ossrs/srs/pull/4106): SmartPtr: Fix SRT source memory leaking. v6.0.134 (#4106) * v6.0, 2024-06-29, Merge [#4077](https://github.com/ossrs/srs/pull/4077): Fix misspelling error in app config. v6.0.133 (#4077) diff --git a/trunk/ide/srs_clion/CMakeLists.txt b/trunk/ide/srs_clion/CMakeLists.txt index 90146911f9..63cb7fd7a1 100755 --- a/trunk/ide/srs_clion/CMakeLists.txt +++ b/trunk/ide/srs_clion/CMakeLists.txt @@ -27,11 +27,11 @@ ProcessorCount(JOBS) # We should always configure SRS for switching between branches. IF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") EXECUTE_PROCESS( - COMMAND ./configure --osx --srt=on --gb28181=on --apm=on --h265=on --utest=on --ffmpeg-opus=off --jobs=${JOBS} + COMMAND ./configure --osx --srt=on --gb28181=on --apm=on --h265=on --hds=on --utest=on --ffmpeg-opus=off --jobs=${JOBS} WORKING_DIRECTORY ${SRS_DIR} RESULT_VARIABLE ret) ELSE () EXECUTE_PROCESS( - COMMAND ./configure --srt=on --gb28181=on --apm=on --h265=on --utest=on --ffmpeg-opus=off --jobs=${JOBS} + COMMAND ./configure --srt=on --gb28181=on --apm=on --h265=on --hds=on --utest=on --ffmpeg-opus=off --jobs=${JOBS} WORKING_DIRECTORY ${SRS_DIR} RESULT_VARIABLE ret) ENDIF () if(NOT ret EQUAL 0) diff --git a/trunk/scripts/copy_to_gits.sh b/trunk/scripts/copy_to_gits.sh index 941959b284..f5d886cf31 100755 --- a/trunk/scripts/copy_to_gits.sh +++ b/trunk/scripts/copy_to_gits.sh @@ -14,18 +14,26 @@ if [[ ! -f ~/git/srs-bench/go.mod ]]; then exit -1 fi +if [[ ! -d ~/git/state-threads ]]; then + echo "no state-threads at ~/git" + exit -1 +fi + echo "Copy signaling" cp -R 3rdparty/signaling/* ~/git/signaling/ && cp -R 3rdparty/signaling/.gitignore ~/git/signaling/ && -(cd ~/git/signaling && git st) +(cd ~/git/signaling && git status) echo "Copy httpx-static" cp -R 3rdparty/httpx-static/* ~/git/go-oryx/httpx-static/ && cp -R 3rdparty/httpx-static/.gitignore ~/git/go-oryx/httpx-static/ && -(cd ~/git/go-oryx && git st) +(cd ~/git/go-oryx && git status) echo "Copy srs-bench" cp -R 3rdparty/srs-bench/* ~/git/srs-bench/ && cp -R 3rdparty/srs-bench/.gitignore ~/git/srs-bench/ && -(cd ~/git/srs-bench && git st) +(cd ~/git/srs-bench && git status) +echo "Copy state-threads" +cp -R 3rdparty/st-srs/* ~/git/state-threads/ && +(cd ~/git/state-threads && git st) diff --git a/trunk/src/app/srs_app_caster_flv.cpp b/trunk/src/app/srs_app_caster_flv.cpp index 758bfaf7d7..ff992308b3 100644 --- a/trunk/src/app/srs_app_caster_flv.cpp +++ b/trunk/src/app/srs_app_caster_flv.cpp @@ -215,10 +215,7 @@ srs_error_t SrsDynamicHttpConn::proxy(ISrsHttpResponseWriter* w, ISrsHttpMessage output = o; srs_trace("flv: proxy %s:%d %s to %s", ip.c_str(), port, r->uri().c_str(), output.c_str()); - - char* buffer = new char[SRS_HTTP_FLV_STREAM_BUFFER]; - SrsAutoFreeA(char, buffer); - + ISrsHttpResponseReader* rr = r->body_reader(); SrsHttpFileReader reader(rr); SrsFlvDecoder dec; diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index eb7a356a26..5401a9432b 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -1157,13 +1157,13 @@ srs_error_t SrsConfDirective::parse_conf(SrsConfigBuffer* buffer, SrsDirectiveCo srs_assert(!file.empty()); srs_trace("config parse include %s", file.c_str()); - SrsConfigBuffer* include_file_buffer = NULL; - SrsAutoFree(SrsConfigBuffer, include_file_buffer); - if ((err = conf->build_buffer(file, &include_file_buffer)) != srs_success) { + SrsConfigBuffer* include_file_buffer_raw = NULL; + if ((err = conf->build_buffer(file, &include_file_buffer_raw)) != srs_success) { return srs_error_wrap(err, "buffer fullfill %s", file.c_str()); } + SrsUniquePtr include_file_buffer(include_file_buffer_raw); - if ((err = parse_conf(include_file_buffer, SrsDirectiveContextFile, conf)) != srs_success) { + if ((err = parse_conf(include_file_buffer.get(), SrsDirectiveContextFile, conf)) != srs_success) { return srs_error_wrap(err, "parse include buffer %s", file.c_str()); } } @@ -1628,10 +1628,8 @@ srs_error_t SrsConfig::reload_vhost(SrsConfDirective* old_root) srs_error_t SrsConfig::reload_conf(SrsConfig* conf) { srs_error_t err = srs_success; - - SrsConfDirective* old_root = root; - SrsAutoFree(SrsConfDirective, old_root); - + + SrsUniquePtr old_root(root); root = conf->root; conf->root = NULL; @@ -1665,14 +1663,14 @@ srs_error_t SrsConfig::reload_conf(SrsConfig* conf) } // Merge config: rtc_server - if ((err = reload_rtc_server(old_root)) != srs_success) { + if ((err = reload_rtc_server(old_root.get())) != srs_success) { return srs_error_wrap(err, "http steram");; } // TODO: FIXME: support reload stream_caster. // merge config: vhost - if ((err = reload_vhost(old_root)) != srs_success) { + if ((err = reload_vhost(old_root.get())) != srs_success) { return srs_error_wrap(err, "vhost");; } @@ -2260,13 +2258,13 @@ srs_error_t SrsConfig::parse_file(const char* filename) return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "empty config"); } - SrsConfigBuffer* buffer = NULL; - SrsAutoFree(SrsConfigBuffer, buffer); - if ((err = build_buffer(config_file, &buffer)) != srs_success) { + SrsConfigBuffer* buffer_raw = NULL; + if ((err = build_buffer(config_file, &buffer_raw)) != srs_success) { return srs_error_wrap(err, "buffer fullfill %s", filename); } - - if ((err = parse_buffer(buffer)) != srs_success) { + + SrsUniquePtr buffer(buffer_raw); + if ((err = parse_buffer(buffer.get())) != srs_success) { return srs_error_wrap(err, "parse buffer %s", filename); } diff --git a/trunk/src/app/srs_app_conn.cpp b/trunk/src/app/srs_app_conn.cpp index 17033411ce..a70e21de46 100644 --- a/trunk/src/app/srs_app_conn.cpp +++ b/trunk/src/app/srs_app_conn.cpp @@ -902,19 +902,18 @@ srs_error_t SrsSslConnection::read(void* plaintext, size_t nn_plaintext, ssize_t if (r0 == -1 && r1 == SSL_ERROR_WANT_READ) { // TODO: Can we avoid copy? int nn_cipher = nn_plaintext; - char* cipher = new char[nn_cipher]; - SrsAutoFreeA(char, cipher); + SrsUniquePtr cipher(new char[nn_cipher]); // Read the cipher from SSL. ssize_t nn = 0; - if ((err = transport->read(cipher, nn_cipher, &nn)) != srs_success) { + if ((err = transport->read(cipher.get(), nn_cipher, &nn)) != srs_success) { return srs_error_wrap(err, "https: read"); } - int r0 = BIO_write(bio_in, cipher, nn); + int r0 = BIO_write(bio_in, cipher.get(), nn); if (r0 <= 0) { // TODO: 0 or -1 maybe block, use BIO_should_retry to check. - return srs_error_new(ERROR_HTTPS_READ, "BIO_write r0=%d, cipher=%p, size=%d", r0, cipher, nn); + return srs_error_new(ERROR_HTTPS_READ, "BIO_write r0=%d, cipher=%p, size=%d", r0, cipher.get(), nn); } continue; } diff --git a/trunk/src/app/srs_app_dash.cpp b/trunk/src/app/srs_app_dash.cpp index 29bf527eee..1c5d99a957 100644 --- a/trunk/src/app/srs_app_dash.cpp +++ b/trunk/src/app/srs_app_dash.cpp @@ -293,10 +293,9 @@ srs_error_t SrsMpdWriter::write(SrsFormat* format, SrsFragmentWindow* afragments } ss << " " << endl; ss << "" << endl; - - SrsFileWriter* fw = new SrsFileWriter(); - SrsAutoFree(SrsFileWriter, fw); - + + SrsUniquePtr fw(new SrsFileWriter()); + string full_path_tmp = full_path + ".tmp"; if ((err = fw->open(full_path_tmp)) != srs_success) { return srs_error_wrap(err, "Open MPD file=%s failed", full_path_tmp.c_str()); @@ -651,10 +650,9 @@ srs_error_t SrsDashController::refresh_init_mp4(SrsSharedPtrMessage* msg, SrsFor } else { path += "/audio-init.mp4"; } - - SrsInitMp4* init_mp4 = new SrsInitMp4(); - SrsAutoFree(SrsInitMp4, init_mp4); - + + SrsUniquePtr init_mp4(new SrsInitMp4()); + init_mp4->set_path(path); int tid = msg->is_video()? video_track_id : audio_track_id; diff --git a/trunk/src/app/srs_app_dvr.cpp b/trunk/src/app/srs_app_dvr.cpp index 492c6f12fb..f56c06da07 100644 --- a/trunk/src/app/srs_app_dvr.cpp +++ b/trunk/src/app/srs_app_dvr.cpp @@ -119,19 +119,19 @@ srs_error_t SrsDvrSegmenter::write_metadata(SrsSharedPtrMessage* metadata) srs_error_t SrsDvrSegmenter::write_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format) { srs_error_t err = srs_success; - - SrsSharedPtrMessage* audio = shared_audio->copy(); - SrsAutoFree(SrsSharedPtrMessage, audio); - - if ((err = jitter->correct(audio, jitter_algorithm)) != srs_success) { + + // TODO: FIXME: Use SrsSharedPtr instead. + SrsUniquePtr audio(shared_audio->copy()); + + if ((err = jitter->correct(audio.get(), jitter_algorithm)) != srs_success) { return srs_error_wrap(err, "jitter"); } - if ((err = on_update_duration(audio)) != srs_success) { + if ((err = on_update_duration(audio.get())) != srs_success) { return srs_error_wrap(err, "update duration"); } - if ((err = encode_audio(audio, format)) != srs_success) { + if ((err = encode_audio(audio.get(), format)) != srs_success) { return srs_error_wrap(err, "encode audio"); } @@ -141,19 +141,19 @@ srs_error_t SrsDvrSegmenter::write_audio(SrsSharedPtrMessage* shared_audio, SrsF srs_error_t SrsDvrSegmenter::write_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) { srs_error_t err = srs_success; - - SrsSharedPtrMessage* video = shared_video->copy(); - SrsAutoFree(SrsSharedPtrMessage, video); - - if ((err = jitter->correct(video, jitter_algorithm)) != srs_success) { + + // TODO: FIXME: Use SrsSharedPtr instead. + SrsUniquePtr video(shared_video->copy()); + + if ((err = jitter->correct(video.get(), jitter_algorithm)) != srs_success) { return srs_error_wrap(err, "jitter"); } - if ((err = encode_video(video, format)) != srs_success) { + if ((err = encode_video(video.get(), format)) != srs_success) { return srs_error_wrap(err, "encode video"); } - if ((err = on_update_duration(video)) != srs_success) { + if ((err = on_update_duration(video.get())) != srs_success) { return srs_error_wrap(err, "update duration"); } @@ -256,15 +256,12 @@ srs_error_t SrsDvrFlvSegmenter::refresh_metadata() int64_t cur = fs->tellg(); // buffer to write the size. - char* buf = new char[SrsAmf0Size::number()]; - SrsAutoFreeA(char, buf); - - SrsBuffer stream(buf, SrsAmf0Size::number()); + SrsUniquePtr buf(new char[SrsAmf0Size::number()]); + SrsBuffer stream(buf.get(), SrsAmf0Size::number()); // filesize to buf. - SrsAmf0Any* size = SrsAmf0Any::number((double)cur); - SrsAutoFree(SrsAmf0Any, size); - + SrsUniquePtr size(SrsAmf0Any::number((double)cur)); + stream.skip(-1 * stream.pos()); if ((err = size->write(&stream)) != srs_success) { return srs_error_wrap(err, "write filesize"); @@ -272,14 +269,13 @@ srs_error_t SrsDvrFlvSegmenter::refresh_metadata() // update the flesize. fs->seek2(filesize_offset); - if ((err = fs->write(buf, SrsAmf0Size::number(), NULL)) != srs_success) { + if ((err = fs->write(buf.get(), SrsAmf0Size::number(), NULL)) != srs_success) { return srs_error_wrap(err, "update filesize"); } // duration to buf - SrsAmf0Any* dur = SrsAmf0Any::number((double)srsu2ms(fragment->duration()) / 1000.0); - SrsAutoFree(SrsAmf0Any, dur); - + SrsUniquePtr dur(SrsAmf0Any::number((double)srsu2ms(fragment->duration()) / 1000.0)); + stream.skip(-1 * stream.pos()); if ((err = dur->write(&stream)) != srs_success) { return srs_error_wrap(err, "write duration"); @@ -287,7 +283,7 @@ srs_error_t SrsDvrFlvSegmenter::refresh_metadata() // update the duration fs->seek2(duration_offset); - if ((err = fs->write(buf, SrsAmf0Size::number(), NULL)) != srs_success) { + if ((err = fs->write(buf.get(), SrsAmf0Size::number(), NULL)) != srs_success) { return srs_error_wrap(err, "update duration"); } @@ -332,15 +328,13 @@ srs_error_t SrsDvrFlvSegmenter::encode_metadata(SrsSharedPtrMessage* metadata) } SrsBuffer stream(metadata->payload, metadata->size); - - SrsAmf0Any* name = SrsAmf0Any::str(); - SrsAutoFree(SrsAmf0Any, name); + + SrsUniquePtr name(SrsAmf0Any::str()); if ((err = name->read(&stream)) != srs_success) { return srs_error_wrap(err, "read name"); } - - SrsAmf0Object* obj = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, obj); + + SrsUniquePtr obj(SrsAmf0Any::object()); if ((err = obj->read(&stream)) != srs_success) { return srs_error_wrap(err, "read object"); } @@ -355,16 +349,15 @@ srs_error_t SrsDvrFlvSegmenter::encode_metadata(SrsSharedPtrMessage* metadata) obj->set("duration", SrsAmf0Any::number(0)); int size = name->total_size() + obj->total_size(); - char* payload = new char[size]; - SrsAutoFreeA(char, payload); - + SrsUniquePtr payload(new char[size]); + // 11B flv header, 3B object EOF, 8B number value, 1B number flag. duration_offset = fs->tellg() + size + 11 - SrsAmf0Size::object_eof() - SrsAmf0Size::number(); // 2B string flag, 8B number value, 8B string 'duration', 1B number flag filesize_offset = duration_offset - SrsAmf0Size::utf8("duration") - SrsAmf0Size::number(); // convert metadata to bytes. - SrsBuffer buf(payload, size); + SrsBuffer buf(payload.get(), size); if ((err = name->write(&buf)) != srs_success) { return srs_error_wrap(err, "write name"); @@ -374,7 +367,7 @@ srs_error_t SrsDvrFlvSegmenter::encode_metadata(SrsSharedPtrMessage* metadata) } // to flv file. - if ((err = enc->write_metadata(18, payload, size)) != srs_success) { + if ((err = enc->write_metadata(18, payload.get(), size)) != srs_success) { return srs_error_wrap(err, "write metadata"); } diff --git a/trunk/src/app/srs_app_edge.cpp b/trunk/src/app/srs_app_edge.cpp index ba9e12f21d..c4913ba8ab 100644 --- a/trunk/src/app/srs_app_edge.cpp +++ b/trunk/src/app/srs_app_edge.cpp @@ -112,8 +112,8 @@ srs_error_t SrsEdgeRtmpUpstream::connect(SrsRequest* r, SrsLbRoundRobin* lb) #ifdef SRS_APM // Create a client span and store it to an AMF0 propagator. - ISrsApmSpan* span_client = _srs_apm->inject(_srs_apm->span("edge-pull")->set_kind(SrsApmKindClient)->as_child(_srs_apm->load()), sdk->extra_args()); - SrsAutoFree(ISrsApmSpan, span_client); + SrsUniquePtr span_client(_srs_apm->inject(_srs_apm->span("edge-pull")->set_kind(SrsApmKindClient) + ->as_child(_srs_apm->load()), sdk->extra_args())); #endif if ((err = sdk->connect()) != srs_success) { @@ -594,9 +594,8 @@ srs_error_t SrsEdgeIngester::do_cycle() srs_error_t SrsEdgeIngester::ingest(string& redirect) { srs_error_t err = srs_success; - - SrsPithyPrint* pprint = SrsPithyPrint::create_edge(); - SrsAutoFree(SrsPithyPrint, pprint); + + SrsUniquePtr pprint(SrsPithyPrint::create_edge()); // we only use the redict once. // reset the redirect to empty, for maybe the origin changed. @@ -615,15 +614,15 @@ srs_error_t SrsEdgeIngester::ingest(string& redirect) } // read from client. - SrsCommonMessage* msg = NULL; - if ((err = upstream->recv_message(&msg)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + if ((err = upstream->recv_message(&msg_raw)) != srs_success) { return srs_error_wrap(err, "recv message"); } - srs_assert(msg); - SrsAutoFree(SrsCommonMessage, msg); - - if ((err = process_publish_message(msg, redirect)) != srs_success) { + srs_assert(msg_raw); + SrsUniquePtr msg(msg_raw); + + if ((err = process_publish_message(msg.get(), redirect)) != srs_success) { return srs_error_wrap(err, "process message"); } } @@ -659,14 +658,14 @@ srs_error_t SrsEdgeIngester::process_publish_message(SrsCommonMessage* msg, stri // process onMetaData if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) { - SrsPacket* pkt = NULL; - if ((err = upstream->decode_message(msg, &pkt)) != srs_success) { + SrsPacket* pkt_raw = NULL; + if ((err = upstream->decode_message(msg, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "decode message"); } - SrsAutoFree(SrsPacket, pkt); + SrsUniquePtr pkt(pkt_raw); - if (dynamic_cast(pkt)) { - SrsOnMetaDataPacket* metadata = dynamic_cast(pkt); + if (dynamic_cast(pkt.get())) { + SrsOnMetaDataPacket* metadata = dynamic_cast(pkt.get()); if ((err = source_->on_meta_data(msg, metadata)) != srs_success) { return srs_error_wrap(err, "source consume metadata"); } @@ -678,15 +677,15 @@ srs_error_t SrsEdgeIngester::process_publish_message(SrsCommonMessage* msg, stri // call messages, for example, reject, redirect. if (msg->header.is_amf0_command() || msg->header.is_amf3_command()) { - SrsPacket* pkt = NULL; - if ((err = upstream->decode_message(msg, &pkt)) != srs_success) { + SrsPacket* pkt_raw = NULL; + if ((err = upstream->decode_message(msg, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "decode message"); } - SrsAutoFree(SrsPacket, pkt); + SrsUniquePtr pkt(pkt_raw); // RTMP 302 redirect - if (dynamic_cast(pkt)) { - SrsCallPacket* call = dynamic_cast(pkt); + if (dynamic_cast(pkt.get())) { + SrsCallPacket* call = dynamic_cast(pkt.get()); if (!call->arguments->is_object()) { return err; } @@ -793,8 +792,8 @@ srs_error_t SrsEdgeForwarder::start() #ifdef SRS_APM // Create a client span and store it to an AMF0 propagator. // Note that we are able to load the span from coroutine context because in the same coroutine. - ISrsApmSpan* span_client = _srs_apm->inject(_srs_apm->span("edge-push")->set_kind(SrsApmKindClient)->as_child(_srs_apm->load()), sdk->extra_args()); - SrsAutoFree(ISrsApmSpan, span_client); + SrsUniquePtr span_client(_srs_apm->inject(_srs_apm->span("edge-push")->set_kind(SrsApmKindClient) + ->as_child(_srs_apm->load()), sdk->extra_args())); #endif if ((err = sdk->connect()) != srs_success) { @@ -858,10 +857,8 @@ srs_error_t SrsEdgeForwarder::do_cycle() srs_error_t err = srs_success; sdk->set_recv_timeout(SRS_CONSTS_RTMP_PULSE); - - SrsPithyPrint* pprint = SrsPithyPrint::create_edge(); - SrsAutoFree(SrsPithyPrint, pprint); - + + SrsUniquePtr pprint(SrsPithyPrint::create_edge()); SrsMessageArray msgs(SYS_MAX_EDGE_SEND_MSGS); while (true) { diff --git a/trunk/src/app/srs_app_forward.cpp b/trunk/src/app/srs_app_forward.cpp index fc3a287275..332ca59939 100755 --- a/trunk/src/app/srs_app_forward.cpp +++ b/trunk/src/app/srs_app_forward.cpp @@ -246,10 +246,9 @@ srs_error_t SrsForwarder::forward() srs_error_t err = srs_success; sdk->set_recv_timeout(SRS_CONSTS_RTMP_PULSE); - - SrsPithyPrint* pprint = SrsPithyPrint::create_forwarder(); - SrsAutoFree(SrsPithyPrint, pprint); - + + SrsUniquePtr pprint(SrsPithyPrint::create_forwarder()); + SrsMessageArray msgs(SYS_MAX_FORWARD_SEND_MSGS); // update sequence header diff --git a/trunk/src/app/srs_app_gb28181.cpp b/trunk/src/app/srs_app_gb28181.cpp index 98bfb0d2b9..d412a61cb6 100644 --- a/trunk/src/app/srs_app_gb28181.cpp +++ b/trunk/src/app/srs_app_gb28181.cpp @@ -164,8 +164,7 @@ void SrsGbSession::on_ps_pack(SrsPackContext* ctx, SrsPsPacket* ps, const std::v media_reserved_ = ctx->media_reserved_; // Group all video in pack to a video frame, because only allows one video for each PS pack. - SrsTsMessage* video = new SrsTsMessage(); - SrsAutoFree(SrsTsMessage, video); + SrsUniquePtr video(new SrsTsMessage()); for (vector::const_iterator it = msgs.begin(); it != msgs.end(); ++it) { SrsTsMessage* msg = *it; @@ -190,7 +189,7 @@ void SrsGbSession::on_ps_pack(SrsPackContext* ctx, SrsPsPacket* ps, const std::v // Send the generated video message. if (video->payload->length() > 0) { - srs_error_t err = muxer_->on_ts_message(video); + srs_error_t err = muxer_->on_ts_message(video.get()); if (err != srs_success) { srs_warn("Muxer: Ignore video err %s", srs_error_desc(err).c_str()); srs_freep(err); @@ -1051,8 +1050,7 @@ srs_error_t SrsGbSipTcpReceiver::do_cycle() { srs_error_t err = srs_success; - SrsHttpParser* parser = new SrsHttpParser(); - SrsAutoFree(SrsHttpParser, parser); + SrsUniquePtr parser(new SrsHttpParser()); // We might get SIP request or response message. if ((err = parser->initialize(HTTP_BOTH)) != srs_success) { @@ -1065,14 +1063,14 @@ srs_error_t SrsGbSipTcpReceiver::do_cycle() } // Use HTTP parser to parse SIP messages. - ISrsHttpMessage* hmsg = NULL; - SrsAutoFree(ISrsHttpMessage, hmsg); - if ((err = parser->parse_message(conn_, &hmsg)) != srs_success) { + ISrsHttpMessage* hmsg_raw = NULL; + if ((err = parser->parse_message(conn_, &hmsg_raw)) != srs_success) { return srs_error_wrap(err, "parse message"); } + SrsUniquePtr hmsg(hmsg_raw); SrsSipMessage smsg; - if ((err = smsg.parse(hmsg)) != srs_success) { + if ((err = smsg.parse(hmsg.get())) != srs_success) { srs_warn("SIP: Drop msg type=%d, method=%d, err is %s", hmsg->message_type(), hmsg->method(), srs_error_summary(err).c_str()); srs_freep(err); continue; } @@ -1156,9 +1154,8 @@ srs_error_t SrsGbSipTcpSender::do_cycle() return srs_error_wrap(err, "pull"); } - SrsSipMessage* msg = msgs_.front(); + SrsUniquePtr msg(msgs_.front()); msgs_.erase(msgs_.begin()); - SrsAutoFree(SrsSipMessage, msg); if (msg->type_ == HTTP_RESPONSE) { SrsSipResponseWriter res(conn_); diff --git a/trunk/src/app/srs_app_hds.cpp b/trunk/src/app/srs_app_hds.cpp index bb749344fa..de9496eeb6 100644 --- a/trunk/src/app/srs_app_hds.cpp +++ b/trunk/src/app/srs_app_hds.cpp @@ -43,10 +43,9 @@ string serialFlv(SrsSharedPtrMessage *msg) { int size = 15 + msg->size; char *byte = new char[size]; - - SrsBuffer *stream = new SrsBuffer(byte, size); - SrsAutoFree(SrsBuffer, stream); - + + SrsUniquePtr stream(new SrsBuffer(byte, size)); + // tag header long long dts = msg->timestamp; char type = msg->is_video() ? 0x09 : 0x08; @@ -449,17 +448,16 @@ srs_error_t SrsHds::flush_bootstrap() srs_error_t err = srs_success; int size = 1024*100; - - char *start_abst = new char[1024*100]; - SrsAutoFreeA(char, start_abst); - + + SrsUniquePtr start_abst(new char[1024*100]); + int size_abst = 0; char *start_asrt = NULL; int size_asrt = 0; char *start_afrt = NULL; int size_afrt = 0; - SrsBuffer abst(start_abst, size); + SrsBuffer abst(start_abst.get(), size); // @see video_file_format_spec_v10_1 // page: 46 @@ -554,7 +552,7 @@ srs_error_t SrsHds::flush_bootstrap() abst.write_1bytes(1); size_abst += 1; - start_asrt = start_abst + size_abst; + start_asrt = start_abst.get() + size_abst; // follows by asrt abst.write_4bytes(0); @@ -622,7 +620,7 @@ srs_error_t SrsHds::flush_bootstrap() size_abst += 1; // follows by afrt - start_afrt = start_abst + size_abst; + start_afrt = start_abst.get() + size_abst; abst.write_4bytes(0); abst.write_string("afrt"); @@ -672,7 +670,7 @@ srs_error_t SrsHds::flush_bootstrap() update_box(start_afrt, size_afrt); size_abst += size_afrt; - update_box(start_abst, size_abst); + update_box(start_abst.get(), size_abst); string path = _srs_config->get_hds_path(hds_req->vhost) + "/" + hds_req->app + "/" + hds_req->stream +".abst"; @@ -681,7 +679,7 @@ srs_error_t SrsHds::flush_bootstrap() return srs_error_new(ERROR_HDS_OPEN_BOOTSTRAP_FAILED, "open bootstrap file failed, path=%s", path.c_str()); } - if (write(fd, start_abst, size_abst) != size_abst) { + if (write(fd, start_abst.get(), size_abst) != size_abst) { close(fd); return srs_error_new(ERROR_HDS_WRITE_BOOTSTRAP_FAILED, "write bootstrap file failed, path=", path.c_str()); } diff --git a/trunk/src/app/srs_app_heartbeat.cpp b/trunk/src/app/srs_app_heartbeat.cpp index 873ecdce40..819075a687 100644 --- a/trunk/src/app/srs_app_heartbeat.cpp +++ b/trunk/src/app/srs_app_heartbeat.cpp @@ -55,10 +55,9 @@ srs_error_t SrsHttpHeartbeat::do_heartbeat() if (!ips.empty()) { ip = ips[_srs_config->get_stats_network() % (int)ips.size()]; } - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); - + + SrsUniquePtr obj(SrsJsonAny::object()); + obj->set("device_id", SrsJsonAny::str(device_id.c_str())); obj->set("ip", SrsJsonAny::str(ip->ip.c_str())); @@ -75,11 +74,12 @@ srs_error_t SrsHttpHeartbeat::do_heartbeat() } std::string req = obj->dumps(); - ISrsHttpMessage* msg = NULL; - if ((err = http.post(uri.get_path(), req, &msg)) != srs_success) { + ISrsHttpMessage* msg_raw = NULL; + if ((err = http.post(uri.get_path(), req, &msg_raw)) != srs_success) { return srs_error_wrap(err, "http post hartbeart uri failed. url=%s, request=%s", url.c_str(), req.c_str()); } - SrsAutoFree(ISrsHttpMessage, msg); + + SrsUniquePtr msg(msg_raw); std::string res; if ((err = msg->body_read_all(res)) != srs_success) { diff --git a/trunk/src/app/srs_app_hls.cpp b/trunk/src/app/srs_app_hls.cpp index 5c261d18d7..9bc9bdfcb6 100644 --- a/trunk/src/app/srs_app_hls.cpp +++ b/trunk/src/app/srs_app_hls.cpp @@ -1373,10 +1373,9 @@ srs_error_t SrsHls::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* forma // update the hls time, for hls_dispose. last_update_time = srs_get_system_time(); - - SrsSharedPtrMessage* audio = shared_audio->copy(); - SrsAutoFree(SrsSharedPtrMessage, audio); - + + SrsUniquePtr audio(shared_audio->copy()); + // ts support audio codec: aac/mp3 SrsAudioCodecId acodec = format->acodec->id; if (acodec != SrsAudioCodecIdAAC && acodec != SrsAudioCodecIdMP3) { @@ -1390,7 +1389,7 @@ srs_error_t SrsHls::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* forma } // TODO: FIXME: config the jitter of HLS. - if ((err = jitter->correct(audio, SrsRtmpJitterAlgorithmOFF)) != srs_success) { + if ((err = jitter->correct(audio.get(), SrsRtmpJitterAlgorithmOFF)) != srs_success) { return srs_error_wrap(err, "hls: jitter"); } @@ -1455,10 +1454,9 @@ srs_error_t SrsHls::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* forma // update the hls time, for hls_dispose. last_update_time = srs_get_system_time(); - - SrsSharedPtrMessage* video = shared_video->copy(); - SrsAutoFree(SrsSharedPtrMessage, video); - + + SrsUniquePtr video(shared_video->copy()); + // ignore info frame, // @see https://github.com/ossrs/srs/issues/288#issuecomment-69863909 srs_assert(format->video); @@ -1477,7 +1475,7 @@ srs_error_t SrsHls::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* forma } // TODO: FIXME: config the jitter of HLS. - if ((err = jitter->correct(video, SrsRtmpJitterAlgorithmOFF)) != srs_success) { + if ((err = jitter->correct(video.get(), SrsRtmpJitterAlgorithmOFF)) != srs_success) { return srs_error_wrap(err, "hls: jitter"); } diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index af1b986b25..c8bc710103 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -66,9 +66,8 @@ srs_error_t srs_api_response_jsonp(ISrsHttpResponseWriter* w, string callback, s srs_error_t srs_api_response_jsonp_code(ISrsHttpResponseWriter* w, string callback, int code) { - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); - + SrsUniquePtr obj(SrsJsonAny::object()); + obj->set("code", SrsJsonAny::integer(code)); return srs_api_response_jsonp(w, callback, obj->dumps()); @@ -76,8 +75,7 @@ srs_error_t srs_api_response_jsonp_code(ISrsHttpResponseWriter* w, string callba srs_error_t srs_api_response_jsonp_code(ISrsHttpResponseWriter* w, string callback, srs_error_t err) { - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(srs_error_code(err))); @@ -104,8 +102,7 @@ srs_error_t srs_api_response_json(ISrsHttpResponseWriter* w, string data) srs_error_t srs_api_response_json_code(ISrsHttpResponseWriter* w, int code) { - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(code)); @@ -114,8 +111,7 @@ srs_error_t srs_api_response_json_code(ISrsHttpResponseWriter* w, int code) srs_error_t srs_api_response_json_code(ISrsHttpResponseWriter* w, srs_error_t code) { - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(srs_error_code(code))); @@ -178,9 +174,8 @@ SrsGoApiRoot::~SrsGoApiRoot() srs_error_t SrsGoApiRoot::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -218,9 +213,8 @@ SrsGoApiApi::~SrsGoApiApi() srs_error_t SrsGoApiApi::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -246,9 +240,8 @@ SrsGoApiV1::~SrsGoApiV1() srs_error_t SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -297,9 +290,8 @@ SrsGoApiVersion::~SrsGoApiVersion() srs_error_t SrsGoApiVersion::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -328,16 +320,15 @@ SrsGoApiSummaries::~SrsGoApiSummaries() srs_error_t SrsGoApiSummaries::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); obj->set("service", SrsJsonAny::str(stat->service_id().c_str())); obj->set("pid", SrsJsonAny::str(stat->service_pid().c_str())); - srs_api_dump_summaries(obj); + srs_api_dump_summaries(obj.get()); return srs_api_response(w, r, obj->dumps()); } @@ -353,9 +344,8 @@ SrsGoApiRusages::~SrsGoApiRusages() srs_error_t SrsGoApiRusages::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -400,9 +390,8 @@ SrsGoApiSelfProcStats::~SrsGoApiSelfProcStats() srs_error_t SrsGoApiSelfProcStats::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -479,9 +468,8 @@ SrsGoApiSystemProcStats::~SrsGoApiSystemProcStats() srs_error_t SrsGoApiSystemProcStats::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -520,9 +508,8 @@ SrsGoApiMemInfos::~SrsGoApiMemInfos() srs_error_t SrsGoApiMemInfos::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -562,9 +549,8 @@ SrsGoApiAuthors::~SrsGoApiAuthors() srs_error_t SrsGoApiAuthors::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -591,9 +577,8 @@ SrsGoApiFeatures::~SrsGoApiFeatures() srs_error_t SrsGoApiFeatures::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -661,9 +646,8 @@ SrsGoApiRequests::~SrsGoApiRequests() srs_error_t SrsGoApiRequests::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -718,9 +702,8 @@ srs_error_t SrsGoApiVhosts::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessag if (!vid.empty() && (vhost = stat->find_vhost_by_id(vid)) == NULL) { return srs_api_response_code(w, r, ERROR_RTMP_VHOST_NOT_FOUND); } - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -776,9 +759,8 @@ srs_error_t SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa if (!sid.empty() && (stream = stat->find_stream(sid)) == NULL) { return srs_api_response_code(w, r, ERROR_RTMP_STREAM_NOT_FOUND); } - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -838,9 +820,8 @@ srs_error_t SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa if (!client_id.empty() && (client = stat->find_client(client_id)) == NULL) { return srs_api_response_code(w, r, ERROR_RTMP_CLIENT_NOT_FOUND); } - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); @@ -918,14 +899,13 @@ srs_error_t SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* std::string rpc = r->query_get("rpc"); // the object to return for request. - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); // for rpc=raw, to query the raw api config for http api. if (rpc == "raw") { // query global scope. - if ((err = _srs_config->raw_to_json(obj)) != srs_success) { + if ((err = _srs_config->raw_to_json(obj.get())) != srs_success) { int code = srs_error_code(err); srs_error_reset(err); return srs_api_response_code(w, r, code); @@ -989,8 +969,7 @@ SrsGoApiClusters::~SrsGoApiClusters() srs_error_t SrsGoApiClusters::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); SrsJsonObject* data = SrsJsonAny::object(); @@ -1057,8 +1036,7 @@ srs_error_t SrsGoApiTcmalloc::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess } // By default, response the json style response. - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); SrsJsonObject* data = SrsJsonAny::object(); diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index f7bab8a4c8..246db282dc 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -155,14 +155,13 @@ srs_error_t SrsHttpConn::do_cycle() return srs_error_wrap(err, "start"); } - SrsRequest* last_req = NULL; - SrsAutoFree(SrsRequest, last_req); - // process all http messages. - err = process_requests(&last_req); + SrsRequest* last_req_raw = NULL; + err = process_requests(&last_req_raw); + SrsUniquePtr last_req(last_req_raw); srs_error_t r0 = srs_success; - if ((r0 = on_disconnect(last_req)) != srs_success) { + if ((r0 = on_disconnect(last_req.get())) != srs_success) { err = srs_error_wrap(err, "on disconnect %s", srs_error_desc(r0).c_str()); srs_freep(r0); } @@ -180,18 +179,18 @@ srs_error_t SrsHttpConn::process_requests(SrsRequest** preq) } // get a http message - ISrsHttpMessage* req = NULL; - if ((err = parser->parse_message(skt, &req)) != srs_success) { + ISrsHttpMessage* req_raw = NULL; + if ((err = parser->parse_message(skt, &req_raw)) != srs_success) { return srs_error_wrap(err, "parse message"); } // if SUCCESS, always NOT-NULL. // always free it in this scope. - srs_assert(req); - SrsAutoFree(ISrsHttpMessage, req); + srs_assert(req_raw); + SrsUniquePtr req(req_raw); // Attach owner connection to message. - SrsHttpMessage* hreq = (SrsHttpMessage*)req; + SrsHttpMessage* hreq = (SrsHttpMessage*)req.get(); hreq->set_connection(this); // copy request to last request object. @@ -200,17 +199,17 @@ srs_error_t SrsHttpConn::process_requests(SrsRequest** preq) // may should discard the body. SrsHttpResponseWriter writer(skt); - if ((err = handler_->on_http_message(req, &writer)) != srs_success) { + if ((err = handler_->on_http_message(req.get(), &writer)) != srs_success) { return srs_error_wrap(err, "on http message"); } // ok, handle http request. - if ((err = process_request(&writer, req, req_id)) != srs_success) { + if ((err = process_request(&writer, req.get(), req_id)) != srs_success) { return srs_error_wrap(err, "process request=%d", req_id); } // After the request is processed. - if ((err = handler_->on_message_done(req, &writer)) != srs_success) { + if ((err = handler_->on_message_done(req.get(), &writer)) != srs_success) { return srs_error_wrap(err, "on message done"); } diff --git a/trunk/src/app/srs_app_http_hooks.cpp b/trunk/src/app/srs_app_http_hooks.cpp index d8dd85d952..31ea1e431e 100644 --- a/trunk/src/app/srs_app_http_hooks.cpp +++ b/trunk/src/app/srs_app_http_hooks.cpp @@ -46,12 +46,9 @@ srs_error_t SrsHttpHooks::on_connect(string url, SrsRequest* req) srs_error_t err = srs_success; SrsContextId cid = _srs_context->get_id(); - SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); - + SrsUniquePtr obj(SrsJsonAny::object()); + obj->set("server_id", SrsJsonAny::str(stat->server_id().c_str())); obj->set("service_id", SrsJsonAny::str(stat->service_id().c_str())); obj->set("action", SrsJsonAny::str("on_connect")); @@ -85,11 +82,8 @@ void SrsHttpHooks::on_close(string url, SrsRequest* req, int64_t send_bytes, int srs_error_t err = srs_success; SrsContextId cid = _srs_context->get_id(); - SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("server_id", SrsJsonAny::str(stat->server_id().c_str())); obj->set("service_id", SrsJsonAny::str(stat->service_id().c_str())); @@ -125,11 +119,8 @@ srs_error_t SrsHttpHooks::on_publish(string url, SrsRequest* req) srs_error_t err = srs_success; SrsContextId cid = _srs_context->get_id(); - SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("server_id", SrsJsonAny::str(stat->server_id().c_str())); obj->set("service_id", SrsJsonAny::str(stat->service_id().c_str())); @@ -169,11 +160,8 @@ void SrsHttpHooks::on_unpublish(string url, SrsRequest* req) srs_error_t err = srs_success; SrsContextId cid = _srs_context->get_id(); - SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("server_id", SrsJsonAny::str(stat->server_id().c_str())); obj->set("service_id", SrsJsonAny::str(stat->service_id().c_str())); @@ -216,11 +204,8 @@ srs_error_t SrsHttpHooks::on_play(string url, SrsRequest* req) srs_error_t err = srs_success; SrsContextId cid = _srs_context->get_id(); - SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("server_id", SrsJsonAny::str(stat->server_id().c_str())); obj->set("service_id", SrsJsonAny::str(stat->service_id().c_str())); @@ -261,11 +246,8 @@ void SrsHttpHooks::on_stop(string url, SrsRequest* req) srs_error_t err = srs_success; SrsContextId cid = _srs_context->get_id(); - SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("server_id", SrsJsonAny::str(stat->server_id().c_str())); obj->set("service_id", SrsJsonAny::str(stat->service_id().c_str())); @@ -311,9 +293,7 @@ srs_error_t SrsHttpHooks::on_dvr(SrsContextId c, string url, SrsRequest* req, st std::string cwd = _srs_config->cwd(); SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("server_id", SrsJsonAny::str(stat->server_id().c_str())); obj->set("service_id", SrsJsonAny::str(stat->service_id().c_str())); @@ -364,9 +344,7 @@ srs_error_t SrsHttpHooks::on_hls(SrsContextId c, string url, SrsRequest* req, st } SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("server_id", SrsJsonAny::str(stat->server_id().c_str())); obj->set("service_id", SrsJsonAny::str(stat->service_id().c_str())); @@ -449,21 +427,20 @@ srs_error_t SrsHttpHooks::on_hls_notify(SrsContextId c, std::string url, SrsRequ } srs_info("GET %s", path.c_str()); - ISrsHttpMessage* msg = NULL; - if ((err = http.get(path.c_str(), "", &msg)) != srs_success) { + ISrsHttpMessage* msg_raw = NULL; + if ((err = http.get(path.c_str(), "", &msg_raw)) != srs_success) { return srs_error_wrap(err, "http: get %s", url.c_str()); } - SrsAutoFree(ISrsHttpMessage, msg); - + SrsUniquePtr msg(msg_raw); + int nb_buf = srs_min(nb_notify, SRS_HTTP_READ_BUFFER); - char* buf = new char[nb_buf]; - SrsAutoFreeA(char, buf); - + SrsUniquePtr buf(new char[nb_buf]); + int nb_read = 0; ISrsHttpResponseReader* br = msg->body_reader(); while (nb_read < nb_notify && !br->eof()) { ssize_t nb_bytes = 0; - if ((err = br->read(buf, nb_buf, &nb_bytes)) != srs_success) { + if ((err = br->read(buf.get(), nb_buf, &nb_bytes)) != srs_success) { break; } nb_read += (int)nb_bytes; @@ -490,9 +467,7 @@ srs_error_t SrsHttpHooks::discover_co_workers(string url, string& host, int& por return srs_error_wrap(err, "http: post %s, status=%d, res=%s", url.c_str(), status_code, res.c_str()); } - SrsJsonObject* robj = NULL; - SrsAutoFree(SrsJsonObject, robj); - + SrsJsonObject* robj_raw = NULL; if (true) { SrsJsonAny* jr = NULL; if ((jr = SrsJsonAny::loads(res)) == NULL) { @@ -503,9 +478,10 @@ srs_error_t SrsHttpHooks::discover_co_workers(string url, string& host, int& por srs_freep(jr); return srs_error_new(ERROR_OCLUSTER_DISCOVER, "response %s", res.c_str()); } - - robj = jr->to_object(); + + robj_raw = jr->to_object(); } + SrsUniquePtr robj(robj_raw); SrsJsonAny* prop = NULL; if ((prop = robj->ensure_property_object("data")) == NULL) { @@ -540,9 +516,7 @@ srs_error_t SrsHttpHooks::on_forward_backend(string url, SrsRequest* req, std::v SrsContextId cid = _srs_context->get_id(); SrsStatistic* stat = SrsStatistic::instance(); - - SrsJsonObject* obj = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, obj); + SrsUniquePtr obj(SrsJsonAny::object()); obj->set("action", SrsJsonAny::str("on_forward")); obj->set("server_id", SrsJsonAny::str(stat->server_id().c_str())); @@ -566,11 +540,10 @@ srs_error_t SrsHttpHooks::on_forward_backend(string url, SrsRequest* req, std::v } // parse string res to json. - SrsJsonAny* info = SrsJsonAny::loads(res); - if (!info) { + SrsUniquePtr info(SrsJsonAny::loads(res)); + if (!info.get()) { return srs_error_new(ERROR_SYSTEM_FORWARD_LOOP, "load json from %s", res.c_str()); } - SrsAutoFree(SrsJsonAny, info); // response error code in string. if (!info->is_object()) { @@ -622,12 +595,12 @@ srs_error_t SrsHttpHooks::do_post(SrsHttpClient* hc, std::string url, std::strin path += "?" + uri.get_query(); } - ISrsHttpMessage* msg = NULL; - if ((err = hc->post(path, req, &msg)) != srs_success) { + ISrsHttpMessage* msg_raw = NULL; + if ((err = hc->post(path, req, &msg_raw)) != srs_success) { return srs_error_wrap(err, "http: client post"); } - SrsAutoFree(ISrsHttpMessage, msg); - + SrsUniquePtr msg(msg_raw); + code = msg->status_code(); if ((err = msg->body_read_all(res)) != srs_success) { return srs_error_wrap(err, "http: body read"); @@ -644,12 +617,11 @@ srs_error_t SrsHttpHooks::do_post(SrsHttpClient* hc, std::string url, std::strin } // parse string res to json. - SrsJsonAny* info = SrsJsonAny::loads(res); - if (!info) { + SrsUniquePtr info(SrsJsonAny::loads(res)); + if (!info.get()) { return srs_error_new(ERROR_HTTP_DATA_INVALID, "http: not json %s", res.c_str()); } - SrsAutoFree(SrsJsonAny, info); - + // response error code in string. if (!info->is_object()) { if (res == SRS_HTTP_RESPONSE_OK) { diff --git a/trunk/src/app/srs_app_http_static.cpp b/trunk/src/app/srs_app_http_static.cpp index a126853db6..18ba11dfe0 100644 --- a/trunk/src/app/srs_app_http_static.cpp +++ b/trunk/src/app/srs_app_http_static.cpp @@ -209,15 +209,14 @@ srs_error_t SrsHlsStream::serve_exists_session(ISrsHttpResponseWriter* w, ISrsHt srs_error_t err = srs_success; // Read m3u8 content. - SrsFileReader* fs = factory->create_file_reader(); - SrsAutoFree(SrsFileReader, fs); + SrsUniquePtr fs(factory->create_file_reader()); if ((err = fs->open(fullpath)) != srs_success) { return srs_error_wrap(err, "open %s", fullpath.c_str()); } string content; - if ((err = srs_ioutil_read_all(fs, content)) != srs_success) { + if ((err = srs_ioutil_read_all(fs.get(), content)) != srs_success) { return srs_error_wrap(err, "read %s", fullpath.c_str()); } @@ -399,10 +398,9 @@ SrsVodStream::~SrsVodStream() srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, string fullpath, int64_t offset) { srs_error_t err = srs_success; - - SrsFileReader* fs = fs_factory->create_file_reader(); - SrsAutoFree(SrsFileReader, fs); - + + SrsUniquePtr fs(fs_factory->create_file_reader()); + // open flv file if ((err = fs->open(fullpath)) != srs_success) { return srs_error_wrap(err, "open file"); @@ -416,7 +414,7 @@ srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMe SrsFlvVodStreamDecoder ffd; // open fast decoder - if ((err = ffd.initialize(fs)) != srs_success) { + if ((err = ffd.initialize(fs.get())) != srs_success) { return srs_error_wrap(err, "init ffd"); } @@ -429,9 +427,7 @@ srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMe } // save sequence header, send later - char* sh_data = NULL; int sh_size = 0; - if (true) { // send sequence header int64_t start = 0; @@ -442,9 +438,9 @@ srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMe return srs_error_new(ERROR_HTTP_REMUX_SEQUENCE_HEADER, "no sequence, size=%d", sh_size); } } - sh_data = new char[sh_size]; - SrsAutoFreeA(char, sh_data); - if ((err = fs->read(sh_data, sh_size, NULL)) != srs_success) { + + SrsUniquePtr sh_data(new char[sh_size]); + if ((err = fs->read(sh_data.get(), sh_size, NULL)) != srs_success) { return srs_error_wrap(err, "fs read"); } @@ -460,7 +456,7 @@ srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMe if ((err = w->write(flv_header, sizeof(flv_header))) != srs_success) { return srs_error_wrap(err, "write flv header"); } - if (sh_size > 0 && (err = w->write(sh_data, sh_size)) != srs_success) { + if (sh_size > 0 && (err = w->write(sh_data.get(), sh_size)) != srs_success) { return srs_error_wrap(err, "write sequence"); } @@ -470,7 +466,7 @@ srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMe } // send data - if ((err = copy(w, fs, r, left)) != srs_success) { + if ((err = copy(w, fs.get(), r, left)) != srs_success) { return srs_error_wrap(err, "read flv=%s size=%" PRId64, fullpath.c_str(), left); } @@ -483,10 +479,9 @@ srs_error_t SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMe srs_assert(start >= 0); srs_assert(end == -1 || end >= 0); - - SrsFileReader* fs = fs_factory->create_file_reader(); - SrsAutoFree(SrsFileReader, fs); - + + SrsUniquePtr fs(fs_factory->create_file_reader()); + // open flv file if ((err = fs->open(fullpath)) != srs_success) { return srs_error_wrap(err, "fs open"); @@ -520,7 +515,7 @@ srs_error_t SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMe fs->seek2(start); // send data - if ((err = copy(w, fs, r, left)) != srs_success) { + if ((err = copy(w, fs.get(), r, left)) != srs_success) { return srs_error_wrap(err, "read mp4=%s size=%" PRId64, fullpath.c_str(), left); } @@ -534,8 +529,7 @@ srs_error_t SrsVodStream::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMes SrsHttpMessage* hr = dynamic_cast(r); srs_assert(hr); - SrsRequest* req = hr->to_request(hr->host())->as_http(); - SrsAutoFree(SrsRequest, req); + SrsUniquePtr req(hr->to_request(hr->host())->as_http()); // discovery vhost, resolve the vhost from config SrsConfDirective* parsed_vhost = _srs_config->get_vhost(req->vhost); @@ -545,7 +539,7 @@ srs_error_t SrsVodStream::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMes // Try to serve by HLS streaming. bool served = false; - if ((err = hls_.serve_m3u8_ctx(w, r, fs_factory, fullpath, req, &served)) != srs_success) { + if ((err = hls_.serve_m3u8_ctx(w, r, fs_factory, fullpath, req.get(), &served)) != srs_success) { return srs_error_wrap(err, "hls ctx"); } diff --git a/trunk/src/app/srs_app_http_stream.cpp b/trunk/src/app/srs_app_http_stream.cpp index 2cb9f91343..d7da269e21 100755 --- a/trunk/src/app/srs_app_http_stream.cpp +++ b/trunk/src/app/srs_app_http_stream.cpp @@ -129,18 +129,18 @@ srs_error_t SrsBufferCache::cycle() // the stream cache will create consumer to cache stream, // which will trigger to fetch stream from origin for edge. - SrsLiveConsumer* consumer = NULL; - SrsAutoFree(SrsLiveConsumer, consumer); - if ((err = live_source->create_consumer(consumer)) != srs_success) { + SrsLiveConsumer* consumer_raw = NULL; + if ((err = live_source->create_consumer(consumer_raw)) != srs_success) { return srs_error_wrap(err, "create consumer"); } - if ((err = live_source->consumer_dumps(consumer, false, false, true)) != srs_success) { + SrsUniquePtr consumer(consumer_raw); + + if ((err = live_source->consumer_dumps(consumer.get(), false, false, true)) != srs_success) { return srs_error_wrap(err, "dumps consumer"); } - - SrsPithyPrint* pprint = SrsPithyPrint::create_http_stream_cache(); - SrsAutoFree(SrsPithyPrint, pprint); - + + SrsUniquePtr pprint(SrsPithyPrint::create_http_stream_cache()); + SrsMessageArray msgs(SRS_PERF_MW_MSGS); // set the queue size, which used for max cache. @@ -647,7 +647,7 @@ srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess srs_error_t err = srs_success; string enc_desc; - ISrsBufferEncoder* enc = NULL; + ISrsBufferEncoder* enc_raw = NULL; srs_assert(entry); bool drop_if_not_match = _srs_config->get_vhost_http_remux_drop_if_not_match(req->vhost); @@ -658,29 +658,29 @@ srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess if (srs_string_ends_with(entry->pattern, ".flv")) { w->header()->set_content_type("video/x-flv"); enc_desc = "FLV"; - enc = new SrsFlvStreamEncoder(); - ((SrsFlvStreamEncoder*)enc)->set_drop_if_not_match(drop_if_not_match); - ((SrsFlvStreamEncoder*)enc)->set_has_audio(has_audio); - ((SrsFlvStreamEncoder*)enc)->set_has_video(has_video); - ((SrsFlvStreamEncoder*)enc)->set_guess_has_av(guess_has_av); + enc_raw = new SrsFlvStreamEncoder(); + ((SrsFlvStreamEncoder*)enc_raw)->set_drop_if_not_match(drop_if_not_match); + ((SrsFlvStreamEncoder*)enc_raw)->set_has_audio(has_audio); + ((SrsFlvStreamEncoder*)enc_raw)->set_has_video(has_video); + ((SrsFlvStreamEncoder*)enc_raw)->set_guess_has_av(guess_has_av); } else if (srs_string_ends_with(entry->pattern, ".aac")) { w->header()->set_content_type("audio/x-aac"); enc_desc = "AAC"; - enc = new SrsAacStreamEncoder(); + enc_raw = new SrsAacStreamEncoder(); } else if (srs_string_ends_with(entry->pattern, ".mp3")) { w->header()->set_content_type("audio/mpeg"); enc_desc = "MP3"; - enc = new SrsMp3StreamEncoder(); + enc_raw = new SrsMp3StreamEncoder(); } else if (srs_string_ends_with(entry->pattern, ".ts")) { w->header()->set_content_type("video/MP2T"); enc_desc = "TS"; - enc = new SrsTsStreamEncoder(); - ((SrsTsStreamEncoder*)enc)->set_has_audio(has_audio); - ((SrsTsStreamEncoder*)enc)->set_has_video(has_video); + enc_raw = new SrsTsStreamEncoder(); + ((SrsTsStreamEncoder*)enc_raw)->set_has_audio(has_audio); + ((SrsTsStreamEncoder*)enc_raw)->set_has_video(has_video); } else { return srs_error_new(ERROR_HTTP_LIVE_STREAM_EXT, "invalid pattern=%s", entry->pattern.c_str()); } - SrsAutoFree(ISrsBufferEncoder, enc); + SrsUniquePtr enc(enc_raw); // Enter chunked mode, because we didn't set the content-length. w->write_header(SRS_CONSTS_HTTP_OK); @@ -691,18 +691,18 @@ srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess } // create consumer of souce, ignore gop cache, use the audio gop cache. - SrsLiveConsumer* consumer = NULL; - SrsAutoFree(SrsLiveConsumer, consumer); - if ((err = live_source->create_consumer(consumer)) != srs_success) { + SrsLiveConsumer* consumer_raw = NULL; + if ((err = live_source->create_consumer(consumer_raw)) != srs_success) { return srs_error_wrap(err, "create consumer"); } - if ((err = live_source->consumer_dumps(consumer, true, true, !enc->has_cache())) != srs_success) { + SrsUniquePtr consumer(consumer_raw); + + if ((err = live_source->consumer_dumps(consumer.get(), true, true, !enc->has_cache())) != srs_success) { return srs_error_wrap(err, "dumps consumer"); } - SrsPithyPrint* pprint = SrsPithyPrint::create_http_stream(); - SrsAutoFree(SrsPithyPrint, pprint); - + SrsUniquePtr pprint(SrsPithyPrint::create_http_stream()); + SrsMessageArray msgs(SRS_PERF_MW_MSGS); // Use receive thread to accept the close event to avoid FD leak. @@ -718,22 +718,21 @@ srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess // if gop cache enabled for encoder, dump to consumer. if (enc->has_cache()) { - if ((err = enc->dump_cache(consumer, live_source->jitter())) != srs_success) { + if ((err = enc->dump_cache(consumer.get(), live_source->jitter())) != srs_success) { return srs_error_wrap(err, "encoder dump cache"); } } // Try to use fast flv encoder, remember that it maybe NULL. - SrsFlvStreamEncoder* ffe = dynamic_cast(enc); + SrsFlvStreamEncoder* ffe = dynamic_cast(enc.get()); // Note that the handler of hc now is hxc. SrsHttpxConn* hxc = dynamic_cast(hc->handler()); srs_assert(hxc); // Start a thread to receive all messages from client, then drop them. - SrsHttpRecvThread* trd = new SrsHttpRecvThread(hxc); - SrsAutoFree(SrsHttpRecvThread, trd); - + SrsUniquePtr trd(new SrsHttpRecvThread(hxc)); + if ((err = trd->start()) != srs_success) { return srs_error_wrap(err, "start recv thread"); } @@ -777,7 +776,7 @@ srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess if (ffe) { err = ffe->write_tags(msgs.msgs, count); } else { - err = streaming_send_messages(enc, msgs.msgs, count); + err = streaming_send_messages(enc.get(), msgs.msgs, count); } // TODO: FIXME: Update the stat. @@ -809,9 +808,8 @@ srs_error_t SrsLiveStream::http_hooks_on_play(ISrsHttpMessage* r) // Create request to report for the specified connection. SrsHttpMessage* hr = dynamic_cast(r); - SrsRequest* nreq = hr->to_request(req->vhost); - SrsAutoFree(SrsRequest, nreq); - + SrsUniquePtr nreq(hr->to_request(req->vhost)); + // the http hooks will cause context switch, // so we must copy all hooks for the on_connect may freed. // @see https://github.com/ossrs/srs/issues/475 @@ -829,7 +827,7 @@ srs_error_t SrsLiveStream::http_hooks_on_play(ISrsHttpMessage* r) for (int i = 0; i < (int)hooks.size(); i++) { std::string url = hooks.at(i); - if ((err = SrsHttpHooks::on_play(url, nreq)) != srs_success) { + if ((err = SrsHttpHooks::on_play(url, nreq.get())) != srs_success) { return srs_error_wrap(err, "http on_play %s", url.c_str()); } } @@ -845,9 +843,8 @@ void SrsLiveStream::http_hooks_on_stop(ISrsHttpMessage* r) // Create request to report for the specified connection. SrsHttpMessage* hr = dynamic_cast(r); - SrsRequest* nreq = hr->to_request(req->vhost); - SrsAutoFree(SrsRequest, nreq); - + SrsUniquePtr nreq(hr->to_request(req->vhost)); + // the http hooks will cause context switch, // so we must copy all hooks for the on_connect may freed. // @see https://github.com/ossrs/srs/issues/475 @@ -866,7 +863,7 @@ void SrsLiveStream::http_hooks_on_stop(ISrsHttpMessage* r) for (int i = 0; i < (int)hooks.size(); i++) { std::string url = hooks.at(i); - SrsHttpHooks::on_stop(url, nreq); + SrsHttpHooks::on_stop(url, nreq.get()); } return; @@ -1063,15 +1060,11 @@ void SrsHttpStreamServer::http_unmount(SrsRequest* r) } // Free all HTTP resources. - SrsLiveEntry* entry = it->second; - SrsAutoFree(SrsLiveEntry, entry); + SrsUniquePtr entry(it->second); streamHandlers.erase(it); - SrsLiveStream* stream = entry->stream; - SrsAutoFree(SrsLiveStream, stream); - - SrsBufferCache* cache = entry->cache; - SrsAutoFree(SrsBufferCache, cache); + SrsUniquePtr stream(entry->stream); + SrsUniquePtr cache(entry->cache); // Notify cache and stream to stop. if (stream->entry) stream->entry->enabled = false; @@ -1088,7 +1081,7 @@ void SrsHttpStreamServer::http_unmount(SrsRequest* r) // Unmount the HTTP handler, which will free the entry. Note that we must free it after cache and // stream stopped for it uses it. - mux.unhandle(entry->mount, stream); + mux.unhandle(entry->mount, stream.get()); srs_trace("http: unmount flv stream for sid=%s, i=%d", sid.c_str(), i); } @@ -1170,9 +1163,8 @@ srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandle srs_assert(hreq); // hijack for entry. - SrsRequest* r = hreq->to_request(vhost->arg0()); - SrsAutoFree(SrsRequest, r); - + SrsUniquePtr r(hreq->to_request(vhost->arg0())); + std::string sid = r->get_stream_url(); // check whether the http remux is enabled, // for example, user disable the http flv then reload. @@ -1189,7 +1181,7 @@ srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandle } SrsSharedPtr live_source; - if ((err = _srs_sources->fetch_or_create(r, server, live_source)) != srs_success) { + if ((err = _srs_sources->fetch_or_create(r.get(), server, live_source)) != srs_success) { return srs_error_wrap(err, "source create"); } srs_assert(live_source.get() != NULL); @@ -1200,7 +1192,7 @@ srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandle live_source->set_gop_cache_max_frames(gcmf); // create http streaming handler. - if ((err = http_mount(r)) != srs_success) { + if ((err = http_mount(r.get())) != srs_success) { return srs_error_wrap(err, "http mount"); } diff --git a/trunk/src/app/srs_app_latest_version.cpp b/trunk/src/app/srs_app_latest_version.cpp index 7220862d99..7f8361c009 100644 --- a/trunk/src/app/srs_app_latest_version.cpp +++ b/trunk/src/app/srs_app_latest_version.cpp @@ -266,11 +266,12 @@ srs_error_t SrsLatestVersion::query_latest_version(string& url) path += "?"; path += uri.get_query(); - ISrsHttpMessage* msg = NULL; - if ((err = http.get(path, "", &msg)) != srs_success) { + ISrsHttpMessage* msg_raw = NULL; + if ((err = http.get(path, "", &msg_raw)) != srs_success) { return err; } - SrsAutoFree(ISrsHttpMessage, msg); + + SrsUniquePtr msg(msg_raw); string res; int code = msg->status_code(); @@ -288,11 +289,10 @@ srs_error_t SrsLatestVersion::query_latest_version(string& url) } // Response in json object. - SrsJsonAny* jres = SrsJsonAny::loads((char*)res.c_str()); - if (!jres || !jres->is_object()) { + SrsUniquePtr jres(SrsJsonAny::loads((char*)res.c_str())); + if (!jres.get() || !jres->is_object()) { return srs_error_new(ERROR_HTTP_DATA_INVALID, "invalid response %s", res.c_str()); } - SrsAutoFree(SrsJsonAny, jres); SrsJsonObject* obj = jres->to_object(); SrsJsonAny* prop = NULL; diff --git a/trunk/src/app/srs_app_listener.cpp b/trunk/src/app/srs_app_listener.cpp index 8216352d43..da4e2690b3 100755 --- a/trunk/src/app/srs_app_listener.cpp +++ b/trunk/src/app/srs_app_listener.cpp @@ -688,8 +688,7 @@ srs_error_t SrsUdpMuxListener::cycle() { srs_error_t err = srs_success; - SrsPithyPrint* pprint = SrsPithyPrint::create_rtc_recv(srs_netfd_fileno(lfd)); - SrsAutoFree(SrsPithyPrint, pprint); + SrsUniquePtr pprint(SrsPithyPrint::create_rtc_recv(srs_netfd_fileno(lfd))); uint64_t nn_msgs = 0; uint64_t nn_msgs_stage = 0; @@ -697,8 +696,7 @@ srs_error_t SrsUdpMuxListener::cycle() uint64_t nn_loop = 0; srs_utime_t time_last = srs_get_system_time(); - SrsErrorPithyPrint* pp_pkt_handler_err = new SrsErrorPithyPrint(); - SrsAutoFree(SrsErrorPithyPrint, pp_pkt_handler_err); + SrsUniquePtr pp_pkt_handler_err(new SrsErrorPithyPrint()); set_socket_buffer(); diff --git a/trunk/src/app/srs_app_mpegts_udp.cpp b/trunk/src/app/srs_app_mpegts_udp.cpp index 1b5adc73dd..84346c20e7 100644 --- a/trunk/src/app/srs_app_mpegts_udp.cpp +++ b/trunk/src/app/srs_app_mpegts_udp.cpp @@ -236,13 +236,12 @@ srs_error_t SrsMpegtsOverUdp::on_udp_bytes(string host, int port, char* buf, int } buffer->erase(buffer->length()); int nb_fbuf = fr.filesize(); - char* fbuf = new char[nb_fbuf]; - SrsAutoFreeA(char, fbuf); - if ((err = fr.read(fbuf, nb_fbuf, NULL)) != srs_success) { + SrsUniquePtr fbuf(new char[nb_fbuf]); + if ((err = fr.read(fbuf.get(), nb_fbuf, NULL)) != srs_success) { return srs_error_wrap(err, "read data"); } fr.close(); - buffer->append(fbuf, nb_fbuf); + buffer->append(fbuf.get(), nb_fbuf); #endif // find the sync byte of mpegts. @@ -268,12 +267,10 @@ srs_error_t SrsMpegtsOverUdp::on_udp_bytes(string host, int port, char* buf, int int nb_packet = buffer->length() / SRS_TS_PACKET_SIZE; for (int i = 0; i < nb_packet; i++) { char* p = buffer->bytes() + (i * SRS_TS_PACKET_SIZE); - - SrsBuffer* stream = new SrsBuffer(p, SRS_TS_PACKET_SIZE); - SrsAutoFree(SrsBuffer, stream); - + SrsUniquePtr stream(new SrsBuffer(p, SRS_TS_PACKET_SIZE)); + // process each ts packet - if ((err = context->decode(stream, this)) != srs_success) { + if ((err = context->decode(stream.get(), this)) != srs_success) { srs_info("parse ts packet err=%s", srs_error_desc(err).c_str()); srs_error_reset(err); continue; diff --git a/trunk/src/app/srs_app_pithy_print.hpp b/trunk/src/app/srs_app_pithy_print.hpp index 707ca576a8..1c92e1e9e6 100644 --- a/trunk/src/app/srs_app_pithy_print.hpp +++ b/trunk/src/app/srs_app_pithy_print.hpp @@ -94,8 +94,7 @@ class SrsAlonePithyPrint // if there is 1client, it will print every 3s. // if there is 10clients, random select one to print every 3s. // Usage: -// SrsPithyPrint* pprint = SrsPithyPrint::create_rtmp_play(); -// SrsAutoFree(SrsPithyPrint, pprint); +// SrsUniquePtr pprint(SrsPithyPrint::create_rtmp_play()); // while (true) { // pprint->elapse(); // if (pprint->can_print()) { diff --git a/trunk/src/app/srs_app_recv_thread.cpp b/trunk/src/app/srs_app_recv_thread.cpp index b25fdc9f1b..cef9d2021e 100644 --- a/trunk/src/app/srs_app_recv_thread.cpp +++ b/trunk/src/app/srs_app_recv_thread.cpp @@ -577,10 +577,8 @@ srs_error_t SrsHttpRecvThread::cycle() srs_error_t err = srs_success; while ((err = trd->pull()) == srs_success) { - ISrsHttpMessage* req = NULL; - SrsAutoFree(ISrsHttpMessage, req); - - if ((err = conn->pop_message(&req)) != srs_success) { + // Ignore any received messages. + if ((err = conn->pop_message(NULL)) != srs_success) { return srs_error_wrap(err, "pop message"); } } diff --git a/trunk/src/app/srs_app_rtc_api.cpp b/trunk/src/app/srs_app_rtc_api.cpp index 1f2880633f..1a5a5d65a3 100644 --- a/trunk/src/app/srs_app_rtc_api.cpp +++ b/trunk/src/app/srs_app_rtc_api.cpp @@ -53,10 +53,9 @@ srs_error_t SrsGoApiRtcPlay::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa { srs_error_t err = srs_success; - SrsJsonObject* res = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, res); + SrsUniquePtr res(SrsJsonAny::object()); - if ((err = do_serve_http(w, r, res)) != srs_success) { + if ((err = do_serve_http(w, r, res.get())) != srs_success) { srs_warn("RTC error %s", srs_error_desc(err).c_str()); srs_freep(err); return srs_api_response_code(w, r, SRS_CONSTS_HTTP_BadRequest); } @@ -73,8 +72,7 @@ srs_error_t SrsGoApiRtcPlay::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMe hdr->set("Connection", "Close"); // Parse req, the request json object, from body. - SrsJsonObject* req = NULL; - SrsAutoFree(SrsJsonObject, req); + SrsJsonObject* req_raw = NULL; if (true) { string req_json; if ((err = r->body_read_all(req_json)) != srs_success) { @@ -86,8 +84,9 @@ srs_error_t SrsGoApiRtcPlay::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMe return srs_error_new(ERROR_RTC_API_BODY, "invalid body %s", req_json.c_str()); } - req = json->to_object(); + req_raw = json->to_object(); } + SrsUniquePtr req(req_raw); // Fetch params from req object. SrsJsonAny* prop = NULL; @@ -351,10 +350,9 @@ srs_error_t SrsGoApiRtcPublish::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMe { srs_error_t err = srs_success; - SrsJsonObject* res = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, res); + SrsUniquePtr res(SrsJsonAny::object()); - if ((err = do_serve_http(w, r, res)) != srs_success) { + if ((err = do_serve_http(w, r, res.get())) != srs_success) { srs_warn("RTC error %s", srs_error_desc(err).c_str()); srs_freep(err); return srs_api_response_code(w, r, SRS_CONSTS_HTTP_BadRequest); } @@ -370,8 +368,7 @@ srs_error_t SrsGoApiRtcPublish::do_serve_http(ISrsHttpResponseWriter* w, ISrsHtt w->header()->set("Connection", "Close"); // Parse req, the request json object, from body. - SrsJsonObject* req = NULL; - SrsAutoFree(SrsJsonObject, req); + SrsJsonObject* req_raw = NULL; if (true) { string req_json; if ((err = r->body_read_all(req_json)) != srs_success) { @@ -383,8 +380,9 @@ srs_error_t SrsGoApiRtcPublish::do_serve_http(ISrsHttpResponseWriter* w, ISrsHtt return srs_error_new(ERROR_RTC_API_BODY, "invalid body %s", req_json.c_str()); } - req = json->to_object(); + req_raw = json->to_object(); } + SrsUniquePtr req(req_raw); // Fetch params from req object. SrsJsonAny* prop = NULL; @@ -775,12 +773,11 @@ srs_error_t SrsGoApiRtcNACK::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa { srs_error_t err = srs_success; - SrsJsonObject* res = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, res); + SrsUniquePtr res(SrsJsonAny::object()); res->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - if ((err = do_serve_http(w, r, res)) != srs_success) { + if ((err = do_serve_http(w, r, res.get())) != srs_success) { srs_warn("RTC: NACK err %s", srs_error_desc(err).c_str()); res->set("code", SrsJsonAny::integer(srs_error_code(err))); srs_freep(err); diff --git a/trunk/src/app/srs_app_rtc_conn.cpp b/trunk/src/app/srs_app_rtc_conn.cpp index 52d142a4ca..8ac1ab2d20 100644 --- a/trunk/src/app/srs_app_rtc_conn.cpp +++ b/trunk/src/app/srs_app_rtc_conn.cpp @@ -644,17 +644,18 @@ srs_error_t SrsRtcPlayStream::cycle() SrsSharedPtr& source = source_; srs_assert(source.get()); - SrsRtcConsumer* consumer = NULL; - SrsAutoFree(SrsRtcConsumer, consumer); - if ((err = source->create_consumer(consumer)) != srs_success) { + SrsRtcConsumer* consumer_raw = NULL; + if ((err = source->create_consumer(consumer_raw)) != srs_success) { return srs_error_wrap(err, "create consumer, source=%s", req_->get_stream_url().c_str()); } - srs_assert(consumer); + srs_assert(consumer_raw); + SrsUniquePtr consumer(consumer_raw); + consumer->set_handler(this); // TODO: FIXME: Dumps the SPS/PPS from gop cache, without other frames. - if ((err = source->consumer_dumps(consumer)) != srs_success) { + if ((err = source->consumer_dumps(consumer.get())) != srs_success) { return srs_error_wrap(err, "dumps consumer, url=%s", req_->get_stream_url().c_str()); } @@ -666,8 +667,7 @@ srs_error_t SrsRtcPlayStream::cycle() srs_trace("RTC: start play url=%s, source_id=%s/%s, realtime=%d, mw_msgs=%d", req_->get_stream_url().c_str(), cid.c_str(), source->pre_source_id().c_str(), realtime, mw_msgs); - SrsErrorPithyPrint* epp = new SrsErrorPithyPrint(); - SrsAutoFree(SrsErrorPithyPrint, epp); + SrsUniquePtr epp(new SrsErrorPithyPrint()); while (true) { if ((err = trd_->pull()) != srs_success) { @@ -1541,13 +1541,12 @@ srs_error_t SrsRtcPublishStream::send_periodic_twcc() // limit the max count=1024 to avoid dead loop. for (int i = 0; i < 1024 && rtcp_twcc_.need_feedback(); ++i) { char pkt[kMaxUDPDataSize]; - SrsBuffer *buffer = new SrsBuffer(pkt, sizeof(pkt)); - SrsAutoFree(SrsBuffer, buffer); + SrsUniquePtr buffer(new SrsBuffer(pkt, sizeof(pkt))); rtcp_twcc_.set_feedback_count(twcc_fb_count_); twcc_fb_count_++; - if((err = rtcp_twcc_.encode(buffer)) != srs_success) { + if((err = rtcp_twcc_.encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode, count=%u", twcc_fb_count_); } @@ -1937,15 +1936,14 @@ srs_error_t SrsRtcConnection::add_publisher(SrsRtcUserConfig* ruc, SrsSdp& local SrsRequest* req = ruc->req_; - SrsRtcSourceDescription* stream_desc = new SrsRtcSourceDescription(); - SrsAutoFree(SrsRtcSourceDescription, stream_desc); + SrsUniquePtr stream_desc(new SrsRtcSourceDescription()); // TODO: FIXME: Change to api of stream desc. - if ((err = negotiate_publish_capability(ruc, stream_desc)) != srs_success) { + if ((err = negotiate_publish_capability(ruc, stream_desc.get())) != srs_success) { return srs_error_wrap(err, "publish negotiate, offer=%s", srs_string_replace(ruc->remote_sdp_str_.c_str(), "\r\n", "\\r\\n").c_str()); } - if ((err = generate_publish_local_sdp(req, local_sdp, stream_desc, ruc->remote_sdp_.is_unified(), ruc->audio_before_video_)) != srs_success) { + if ((err = generate_publish_local_sdp(req, local_sdp, stream_desc.get(), ruc->remote_sdp_.is_unified(), ruc->audio_before_video_)) != srs_success) { return srs_error_wrap(err, "generate local sdp"); } @@ -1962,10 +1960,10 @@ srs_error_t SrsRtcConnection::add_publisher(SrsRtcUserConfig* ruc, SrsSdp& local source->set_stream_created(); // Apply the SDP to source. - source->set_stream_desc(stream_desc); + source->set_stream_desc(stream_desc.get()); // TODO: FIXME: What happends when error? - if ((err = create_publisher(req, stream_desc)) != srs_success) { + if ((err = create_publisher(req, stream_desc.get())) != srs_success) { return srs_error_wrap(err, "create publish"); } @@ -1988,8 +1986,7 @@ srs_error_t SrsRtcConnection::add_player(SrsRtcUserConfig* ruc, SrsSdp& local_sd return srs_error_new(ERROR_RTC_SDP_EXCHANGE, "no play relations"); } - SrsRtcSourceDescription* stream_desc = new SrsRtcSourceDescription(); - SrsAutoFree(SrsRtcSourceDescription, stream_desc); + SrsUniquePtr stream_desc(new SrsRtcSourceDescription()); std::map::iterator it = play_sub_relations.begin(); while (it != play_sub_relations.end()) { SrsRtcTrackDescription* track_desc = it->second; @@ -2005,7 +2002,7 @@ srs_error_t SrsRtcConnection::add_player(SrsRtcUserConfig* ruc, SrsSdp& local_sd ++it; } - if ((err = generate_play_local_sdp(req, local_sdp, stream_desc, ruc->remote_sdp_.is_unified(), ruc->audio_before_video_)) != srs_success) { + if ((err = generate_play_local_sdp(req, local_sdp, stream_desc.get(), ruc->remote_sdp_.is_unified(), ruc->audio_before_video_)) != srs_success) { return srs_error_wrap(err, "generate local sdp"); } @@ -2046,20 +2043,19 @@ srs_error_t SrsRtcConnection::on_rtcp(char* unprotected_buf, int nb_unprotected_ { srs_error_t err = srs_success; - SrsBuffer* buffer = new SrsBuffer(unprotected_buf, nb_unprotected_buf); - SrsAutoFree(SrsBuffer, buffer); + SrsUniquePtr buffer(new SrsBuffer(unprotected_buf, nb_unprotected_buf)); SrsRtcpCompound rtcp_compound; - if(srs_success != (err = rtcp_compound.decode(buffer))) { + if(srs_success != (err = rtcp_compound.decode(buffer.get()))) { return srs_error_wrap(err, "decode rtcp plaintext=%u, bytes=[%s], at=%s", nb_unprotected_buf, srs_string_dumps_hex(unprotected_buf, nb_unprotected_buf, 8).c_str(), srs_string_dumps_hex(buffer->head(), buffer->left(), 8).c_str()); } - SrsRtcpCommon* rtcp = NULL; - while(NULL != (rtcp = rtcp_compound.get_next_rtcp())) { - err = dispatch_rtcp(rtcp); - SrsAutoFree(SrsRtcpCommon, rtcp); + SrsRtcpCommon* rtcp_raw = NULL; + while(NULL != (rtcp_raw = rtcp_compound.get_next_rtcp())) { + err = dispatch_rtcp(rtcp_raw); + SrsUniquePtr rtcp(rtcp_raw); if(srs_success != err) { return srs_error_wrap(err, "plaintext=%u, bytes=[%s], rtcp=(%u,%u,%u,%u)", nb_unprotected_buf, @@ -2626,8 +2622,7 @@ srs_error_t SrsRtcConnection::negotiate_publish_capability(SrsRtcUserConfig* ruc if (remote_media_desc.is_video()) nn_any_video_parsed++; - SrsRtcTrackDescription* track_desc = new SrsRtcTrackDescription(); - SrsAutoFree(SrsRtcTrackDescription, track_desc); + SrsUniquePtr track_desc(new SrsRtcTrackDescription()); track_desc->set_direction("recvonly"); track_desc->set_mid(remote_media_desc.mid_); diff --git a/trunk/src/app/srs_app_rtc_dtls.cpp b/trunk/src/app/srs_app_rtc_dtls.cpp index c83b5c29f0..df4aaa44ba 100644 --- a/trunk/src/app/srs_app_rtc_dtls.cpp +++ b/trunk/src/app/srs_app_rtc_dtls.cpp @@ -329,13 +329,12 @@ srs_error_t SrsDtlsCertificate::initialize() // TODO: FIXME: Unused variable. /*int r = */X509_digest(dtls_cert, EVP_sha256(), md, &n); - char* fp = new char[3 * n]; - SrsAutoFreeA(char, fp); - char *p = fp; + SrsUniquePtr fp(new char[3 * n]); + char* p = fp.get(); for (unsigned int i = 0; i < n; i++, ++p) { int nb = snprintf(p, 3, "%02X", md[i]); - srs_assert(nb > 0 && nb < (3 * n - (p - fp))); + srs_assert(nb > 0 && nb < (3 * n - (p - fp.get()))); p += nb; if(i < (n-1)) { @@ -345,7 +344,7 @@ srs_error_t SrsDtlsCertificate::initialize() } } - fingerprint.assign(fp, strlen(fp)); + fingerprint.assign(fp.get(), strlen(fp.get())); srs_trace("fingerprint=%s", fingerprint.c_str()); } @@ -985,10 +984,9 @@ srs_error_t SrsSRTP::initialize(string recv_key, std::string send_key) // init recv context policy.ssrc.type = ssrc_any_inbound; - uint8_t *rkey = new uint8_t[recv_key.size()]; - SrsAutoFreeA(uint8_t, rkey); - memcpy(rkey, recv_key.data(), recv_key.size()); - policy.key = rkey; + SrsUniquePtr rkey(new uint8_t[recv_key.size()]); + memcpy(rkey.get(), recv_key.data(), recv_key.size()); + policy.key = rkey.get(); srtp_err_status_t r0 = srtp_err_status_ok; if ((r0 = srtp_create(&recv_ctx_, &policy)) != srtp_err_status_ok) { @@ -996,10 +994,9 @@ srs_error_t SrsSRTP::initialize(string recv_key, std::string send_key) } policy.ssrc.type = ssrc_any_outbound; - uint8_t *skey = new uint8_t[send_key.size()]; - SrsAutoFreeA(uint8_t, skey); - memcpy(skey, send_key.data(), send_key.size()); - policy.key = skey; + SrsUniquePtr skey(new uint8_t[send_key.size()]); + memcpy(skey.get(), send_key.data(), send_key.size()); + policy.key = skey.get(); if ((r0 = srtp_create(&send_ctx_, &policy)) != srtp_err_status_ok) { return srs_error_new(ERROR_RTC_SRTP_INIT, "srtp create r0=%u", r0); diff --git a/trunk/src/app/srs_app_rtc_network.cpp b/trunk/src/app/srs_app_rtc_network.cpp index 3725afaf10..216938eae1 100644 --- a/trunk/src/app/srs_app_rtc_network.cpp +++ b/trunk/src/app/srs_app_rtc_network.cpp @@ -391,8 +391,7 @@ srs_error_t SrsRtcUdpNetwork::on_binding_request(SrsStunPacket* r, string ice_pw SrsStunPacket stun_binding_response; char buf[kRtpPacketSize]; - SrsBuffer* stream = new SrsBuffer(buf, sizeof(buf)); - SrsAutoFree(SrsBuffer, stream); + SrsUniquePtr stream(new SrsBuffer(buf, sizeof(buf))); stun_binding_response.set_message_type(BindingResponse); stun_binding_response.set_local_ufrag(r->get_remote_ufrag()); @@ -402,7 +401,7 @@ srs_error_t SrsRtcUdpNetwork::on_binding_request(SrsStunPacket* r, string ice_pw stun_binding_response.set_mapped_address(be32toh(inet_addr(get_peer_ip().c_str()))); stun_binding_response.set_mapped_port(get_peer_port()); - if ((err = stun_binding_response.encode(ice_pwd, stream)) != srs_success) { + if ((err = stun_binding_response.encode(ice_pwd, stream.get())) != srs_success) { return srs_error_wrap(err, "stun binding response encode failed"); } @@ -520,8 +519,7 @@ srs_error_t SrsRtcTcpNetwork::on_binding_request(SrsStunPacket* r, std::string i SrsStunPacket stun_binding_response; char buf[kRtpPacketSize]; - SrsBuffer* stream = new SrsBuffer(buf, sizeof(buf)); - SrsAutoFree(SrsBuffer, stream); + SrsUniquePtr stream(new SrsBuffer(buf, sizeof(buf))); stun_binding_response.set_message_type(BindingResponse); stun_binding_response.set_local_ufrag(r->get_remote_ufrag()); @@ -531,7 +529,7 @@ srs_error_t SrsRtcTcpNetwork::on_binding_request(SrsStunPacket* r, std::string i stun_binding_response.set_mapped_address(be32toh(inet_addr(get_peer_ip().c_str()))); stun_binding_response.set_mapped_port(get_peer_port()); - if ((err = stun_binding_response.encode(ice_pwd, stream)) != srs_success) { + if ((err = stun_binding_response.encode(ice_pwd, stream.get())) != srs_success) { return srs_error_wrap(err, "stun binding response encode failed"); } diff --git a/trunk/src/app/srs_app_rtc_source.cpp b/trunk/src/app/srs_app_rtc_source.cpp index c3b8ddfe4e..8aebf5d530 100644 --- a/trunk/src/app/srs_app_rtc_source.cpp +++ b/trunk/src/app/srs_app_rtc_source.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef SRS_FFMPEG_FIT #include @@ -449,8 +450,7 @@ void SrsRtcSource::init_for_play_before_publishing() return; } - SrsRtcSourceDescription* stream_desc = new SrsRtcSourceDescription(); - SrsAutoFree(SrsRtcSourceDescription, stream_desc); + SrsUniquePtr stream_desc(new SrsRtcSourceDescription()); // audio track description if (true) { @@ -485,7 +485,7 @@ void SrsRtcSource::init_for_play_before_publishing() video_payload->set_h264_param_desc("level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f"); } - set_stream_desc(stream_desc); + set_stream_desc(stream_desc.get()); } void SrsRtcSource::update_auth(SrsRequest* r) @@ -1006,16 +1006,14 @@ srs_error_t SrsRtcRtpBuilder::transcode(SrsAudioFrame* audio) for (std::vector::iterator it = out_audios.begin(); it != out_audios.end(); ++it) { SrsAudioFrame* out_audio = *it; + SrsUniquePtr pkt(new SrsRtpPacket()); - SrsRtpPacket* pkt = new SrsRtpPacket(); - SrsAutoFree(SrsRtpPacket, pkt); - - if ((err = package_opus(out_audio, pkt)) != srs_success) { + if ((err = package_opus(out_audio, pkt.get())) != srs_success) { err = srs_error_wrap(err, "package opus"); break; } - if ((err = bridge_->on_rtp(pkt)) != srs_success) { + if ((err = bridge_->on_rtp(pkt.get())) != srs_success) { err = srs_error_wrap(err, "consume opus"); break; } @@ -1083,14 +1081,13 @@ srs_error_t SrsRtcRtpBuilder::on_video(SrsSharedPtrMessage* msg) // Well, for each IDR, we append a SPS/PPS before it, which is packaged in STAP-A. if (has_idr) { - SrsRtpPacket* pkt = new SrsRtpPacket(); - SrsAutoFree(SrsRtpPacket, pkt); + SrsUniquePtr pkt(new SrsRtpPacket()); - if ((err = package_stap_a(msg, pkt)) != srs_success) { + if ((err = package_stap_a(msg, pkt.get())) != srs_success) { return srs_error_wrap(err, "package stap-a"); } - if ((err = bridge_->on_rtp(pkt)) != srs_success) { + if ((err = bridge_->on_rtp(pkt.get())) != srs_success) { return srs_error_wrap(err, "consume sps/pps"); } } @@ -1231,7 +1228,7 @@ srs_error_t SrsRtcRtpBuilder::package_nalus(SrsSharedPtrMessage* msg, const vect { srs_error_t err = srs_success; - SrsRtpRawNALUs* raw = new SrsRtpRawNALUs(); + SrsRtpRawNALUs* raw_raw = new SrsRtpRawNALUs(); SrsAvcNaluType first_nalu_type = SrsAvcNaluTypeReserved; for (int i = 0; i < (int)samples.size(); i++) { @@ -1245,13 +1242,13 @@ srs_error_t SrsRtcRtpBuilder::package_nalus(SrsSharedPtrMessage* msg, const vect first_nalu_type = SrsAvcNaluType((uint8_t)(sample->bytes[0] & kNalTypeMask)); } - raw->push_back(sample->copy()); + raw_raw->push_back(sample->copy()); } // Ignore empty. - int nn_bytes = raw->nb_bytes(); + int nn_bytes = raw_raw->nb_bytes(); if (nn_bytes <= 0) { - srs_freep(raw); + srs_freep(raw_raw); return err; } @@ -1266,12 +1263,12 @@ srs_error_t SrsRtcRtpBuilder::package_nalus(SrsSharedPtrMessage* msg, const vect pkt->nalu_type = (SrsAvcNaluType)first_nalu_type; pkt->header.set_sequence(video_sequence++); pkt->header.set_timestamp(msg->timestamp * 90); - pkt->set_payload(raw, SrsRtspPacketPayloadTypeNALU); + pkt->set_payload(raw_raw, SrsRtspPacketPayloadTypeNALU); pkt->wrap(msg); } else { // We must free it, should never use RTP packets to free it, // because more than one RTP packet will refer to it. - SrsAutoFree(SrsRtpRawNALUs, raw); + SrsUniquePtr raw(raw_raw); // Package NALUs in FU-A RTP packets. int fu_payload_size = kRtpMaxPayloadSize; @@ -1642,13 +1639,13 @@ srs_error_t SrsRtcFrameBuilder::packet_video_key_frame(SrsRtpPacket* pkt) } // Reset SPS/PPS cache, ensuring that the next SPS/PPS will be handled when both are received. + // Note that we should use SrsAutoFree to set the ptr to NULL. SrsAutoFree(SrsRtpPacket, obs_whip_sps_); SrsAutoFree(SrsRtpPacket, obs_whip_pps_); // h264 raw to h264 packet. std::string sh; - SrsRawH264Stream* avc = new SrsRawH264Stream(); - SrsAutoFree(SrsRawH264Stream, avc); + SrsUniquePtr avc(new SrsRawH264Stream()); if ((err = avc->mux_sequence_header(string(sps->bytes, sps->size), string(pps->bytes, pps->size), sh)) != srs_success) { return srs_error_wrap(err, "mux sequence header"); diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index fd01a831ef..b08262281c 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -755,25 +755,26 @@ srs_error_t SrsRtmpConn::playing(SrsSharedPtr source) set_sock_options(); // Create a consumer of source. - SrsLiveConsumer* consumer = NULL; - SrsAutoFree(SrsLiveConsumer, consumer); - if ((err = source->create_consumer(consumer)) != srs_success) { + SrsLiveConsumer* consumer_raw = NULL; + if ((err = source->create_consumer(consumer_raw)) != srs_success) { return srs_error_wrap(err, "rtmp: create consumer"); } - if ((err = source->consumer_dumps(consumer)) != srs_success) { + SrsUniquePtr consumer(consumer_raw); + + if ((err = source->consumer_dumps(consumer.get())) != srs_success) { return srs_error_wrap(err, "rtmp: dumps consumer"); } // Use receiving thread to receive packets from peer. - SrsQueueRecvThread trd(consumer, rtmp, SRS_PERF_MW_SLEEP, _srs_context->get_id()); + SrsQueueRecvThread trd(consumer.get(), rtmp, SRS_PERF_MW_SLEEP, _srs_context->get_id()); if ((err = trd.start()) != srs_success) { return srs_error_wrap(err, "rtmp: start receive thread"); } // Deliver packets to peer. - wakable = consumer; - err = do_playing(source, consumer, &trd); + wakable = consumer.get(); + err = do_playing(source, consumer.get(), &trd); wakable = NULL; trd.stop(); @@ -795,9 +796,8 @@ srs_error_t SrsRtmpConn::do_playing(SrsSharedPtr source, SrsLiveC srs_assert(consumer); // initialize other components - SrsPithyPrint* pprint = SrsPithyPrint::create_rtmp_play(); - SrsAutoFree(SrsPithyPrint, pprint); - + SrsUniquePtr pprint(SrsPithyPrint::create_rtmp_play()); + SrsMessageArray msgs(SRS_PERF_MW_MSGS); bool user_specified_duration_to_stop = (req->duration > 0); int64_t starttime = -1; @@ -816,9 +816,8 @@ srs_error_t SrsRtmpConn::do_playing(SrsSharedPtr source, SrsLiveC srsu2msi(send_min_interval), srsu2msi(mw_sleep), mw_msgs, realtime, tcp_nodelay); #ifdef SRS_APM - ISrsApmSpan* span = _srs_apm->span("play-cycle")->set_kind(SrsApmKindProducer)->as_child(span_client_) - ->attr("realtime", srs_fmt("%d", realtime))->end(); - SrsAutoFree(ISrsApmSpan, span); + SrsUniquePtr span(_srs_apm->span("play-cycle")->set_kind(SrsApmKindProducer)->as_child(span_client_) + ->attr("realtime", srs_fmt("%d", realtime))->end()); #endif while (true) { @@ -866,7 +865,7 @@ srs_error_t SrsRtmpConn::do_playing(SrsSharedPtr source, SrsLiveC #ifdef SRS_APM // TODO: Do not use pithy print for frame span. - ISrsApmSpan* sample = _srs_apm->span("play-frame")->set_kind(SrsApmKindConsumer)->as_child(span) + ISrsApmSpan* sample = _srs_apm->span("play-frame")->set_kind(SrsApmKindConsumer)->as_child(span.get()) ->attr("msgs", srs_fmt("%d", count))->attr("kbps", srs_fmt("%d", kbps->get_send_kbps_30s())); srs_freep(sample); #endif @@ -974,8 +973,7 @@ srs_error_t SrsRtmpConn::do_publishing(SrsSharedPtr source, SrsPu srs_error_t err = srs_success; SrsRequest* req = info->req; - SrsPithyPrint* pprint = SrsPithyPrint::create_rtmp_publish(); - SrsAutoFree(SrsPithyPrint, pprint); + SrsUniquePtr pprint(SrsPithyPrint::create_rtmp_publish()); // start isolate recv thread. // TODO: FIXME: Pass the callback here. @@ -998,9 +996,8 @@ srs_error_t SrsRtmpConn::do_publishing(SrsSharedPtr source, SrsPu } #ifdef SRS_APM - ISrsApmSpan* span = _srs_apm->span("publish-cycle")->set_kind(SrsApmKindProducer)->as_child(span_client_) - ->attr("timeout", srs_fmt("%d", srsu2msi(publish_normal_timeout)))->end(); - SrsAutoFree(ISrsApmSpan, span); + SrsUniquePtr span(_srs_apm->span("publish-cycle")->set_kind(SrsApmKindProducer)->as_child(span_client_) + ->attr("timeout", srs_fmt("%d", srsu2msi(publish_normal_timeout)))->end()); #endif // Response the start publishing message, let client start to publish messages. @@ -1062,7 +1059,7 @@ srs_error_t SrsRtmpConn::do_publishing(SrsSharedPtr source, SrsPu #ifdef SRS_APM // TODO: Do not use pithy print for frame span. - ISrsApmSpan* sample = _srs_apm->span("publish-frame")->set_kind(SrsApmKindConsumer)->as_child(span) + ISrsApmSpan* sample = _srs_apm->span("publish-frame")->set_kind(SrsApmKindConsumer)->as_child(span.get()) ->attr("msgs", srs_fmt("%" PRId64, nb_frames))->attr("kbps", srs_fmt("%d", kbps->get_recv_kbps_30s())); srs_freep(sample); #endif @@ -1158,12 +1155,12 @@ srs_error_t SrsRtmpConn::handle_publish_message(SrsSharedPtr& sou // process publish event. if (msg->header.is_amf0_command() || msg->header.is_amf3_command()) { - SrsPacket* pkt = NULL; - if ((err = rtmp->decode_message(msg, &pkt)) != srs_success) { + SrsPacket* pkt_raw = NULL; + if ((err = rtmp->decode_message(msg, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "rtmp: decode message"); } - SrsAutoFree(SrsPacket, pkt); - + SrsUniquePtr pkt(pkt_raw); + // for flash, any packet is republish. if (info->type == SrsRtmpConnFlashPublish) { // flash unpublish. @@ -1173,8 +1170,8 @@ srs_error_t SrsRtmpConn::handle_publish_message(SrsSharedPtr& sou } // for fmle, drop others except the fmle start packet. - if (dynamic_cast(pkt)) { - SrsFMLEStartPacket* unpublish = dynamic_cast(pkt); + if (dynamic_cast(pkt.get())) { + SrsFMLEStartPacket* unpublish = dynamic_cast(pkt.get()); if ((err = rtmp->fmle_unpublish(info->res->stream_id, unpublish->transaction_id)) != srs_success) { return srs_error_wrap(err, "rtmp: republish"); } @@ -1230,14 +1227,14 @@ srs_error_t SrsRtmpConn::process_publish_message(SrsSharedPtr& so // process onMetaData if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) { - SrsPacket* pkt = NULL; - if ((err = rtmp->decode_message(msg, &pkt)) != srs_success) { + SrsPacket* pkt_raw = NULL; + if ((err = rtmp->decode_message(msg, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "rtmp: decode message"); } - SrsAutoFree(SrsPacket, pkt); - - if (dynamic_cast(pkt)) { - SrsOnMetaDataPacket* metadata = dynamic_cast(pkt); + SrsUniquePtr pkt(pkt_raw); + + if (dynamic_cast(pkt.get())) { + SrsOnMetaDataPacket* metadata = dynamic_cast(pkt.get()); if ((err = source->on_meta_data(msg, metadata)) != srs_success) { return srs_error_wrap(err, "rtmp: consume metadata"); } @@ -1249,27 +1246,27 @@ srs_error_t SrsRtmpConn::process_publish_message(SrsSharedPtr& so return err; } -srs_error_t SrsRtmpConn::process_play_control_msg(SrsLiveConsumer* consumer, SrsCommonMessage* msg) +srs_error_t SrsRtmpConn::process_play_control_msg(SrsLiveConsumer* consumer, SrsCommonMessage* msg_raw) { srs_error_t err = srs_success; - if (!msg) { + if (!msg_raw) { return err; } - SrsAutoFree(SrsCommonMessage, msg); - + SrsUniquePtr msg(msg_raw); + if (!msg->header.is_amf0_command() && !msg->header.is_amf3_command()) { return err; } - SrsPacket* pkt = NULL; - if ((err = rtmp->decode_message(msg, &pkt)) != srs_success) { + SrsPacket* pkt_raw = NULL; + if ((err = rtmp->decode_message(msg.get(), &pkt_raw)) != srs_success) { return srs_error_wrap(err, "rtmp: decode message"); } - SrsAutoFree(SrsPacket, pkt); - + SrsUniquePtr pkt(pkt_raw); + // for jwplayer/flowplayer, which send close as pause message. - SrsCloseStreamPacket* close = dynamic_cast(pkt); + SrsCloseStreamPacket* close = dynamic_cast(pkt.get()); if (close) { return srs_error_new(ERROR_CONTROL_RTMP_CLOSE, "rtmp: close stream"); } @@ -1277,7 +1274,7 @@ srs_error_t SrsRtmpConn::process_play_control_msg(SrsLiveConsumer* consumer, Srs // call msg, // support response null first, // TODO: FIXME: response in right way, or forward in edge mode. - SrsCallPacket* call = dynamic_cast(pkt); + SrsCallPacket* call = dynamic_cast(pkt.get()); if (call) { // only response it when transaction id not zero, // for the zero means donot need response. @@ -1293,7 +1290,7 @@ srs_error_t SrsRtmpConn::process_play_control_msg(SrsLiveConsumer* consumer, Srs } // pause - SrsPausePacket* pause = dynamic_cast(pkt); + SrsPausePacket* pause = dynamic_cast(pkt.get()); if (pause) { if ((err = rtmp->on_play_client_pause(info->res->stream_id, pause->is_pause)) != srs_success) { return srs_error_wrap(err, "rtmp: pause"); @@ -1343,19 +1340,16 @@ srs_error_t SrsRtmpConn::check_edge_token_traverse_auth() string server; int port = SRS_CONSTS_RTMP_DEFAULT_PORT; srs_parse_hostport(hostport, server, port); - - SrsTcpClient* transport = new SrsTcpClient(server, port, SRS_EDGE_TOKEN_TRAVERSE_TIMEOUT); - SrsAutoFree(SrsTcpClient, transport); - + + SrsUniquePtr transport(new SrsTcpClient(server, port, SRS_EDGE_TOKEN_TRAVERSE_TIMEOUT)); if ((err = transport->connect()) != srs_success) { srs_warn("Illegal edge token, tcUrl=%s, %s", req->tcUrl.c_str(), srs_error_desc(err).c_str()); srs_freep(err); continue; } - - SrsRtmpClient* client = new SrsRtmpClient(transport); - SrsAutoFree(SrsRtmpClient, client); - return do_token_traverse_auth(client); + + SrsUniquePtr client(new SrsRtmpClient(transport.get())); + return do_token_traverse_auth(client.get()); } return srs_error_new(ERROR_EDGE_PORT_INVALID, "rtmp: Illegal edge token, server=%d", (int)args.size()); @@ -1611,8 +1605,7 @@ srs_error_t SrsRtmpConn::cycle() #ifdef SRS_APM // Final APM span, parent is the last span, not the root span. Note that only client or server kind will be filtered // for error or exception report. - ISrsApmSpan* span_final = _srs_apm->span("final")->set_kind(SrsApmKindServer)->as_child(span_client_); - SrsAutoFree(ISrsApmSpan, span_final); + SrsUniquePtr span_final(_srs_apm->span("final")->set_kind(SrsApmKindServer)->as_child(span_client_)); if (srs_error_code(err) != 0) { span_final->record_error(err)->set_status(SrsApmStatusError, srs_fmt("fail code=%d", srs_error_code(err))); } diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index 3a47ebdf33..2a9c59a58d 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -1530,8 +1530,7 @@ srs_error_t SrsOriginHub::create_backend_forwarders(bool& applied) std::string url = *it; // create temp Request by url - SrsRequest* req = new SrsRequest(); - SrsAutoFree(SrsRequest, req); + SrsUniquePtr req(new SrsRequest()); srs_parse_rtmp_url(url, req->tcUrl, req->stream); srs_discovery_tc_url(req->tcUrl, req->schema, req->host, req->vhost, req->app, req->stream, req->port, req->param); @@ -1543,7 +1542,7 @@ srs_error_t SrsOriginHub::create_backend_forwarders(bool& applied) forward_server << req->host << ":" << req->port; // initialize the forwarder with request. - if ((err = forwarder->initialize(req, forward_server.str())) != srs_success) { + if ((err = forwarder->initialize(req.get(), forward_server.str())) != srs_success) { return srs_error_wrap(err, "init backend forwarder failed, forward-to=%s", forward_server.str().c_str()); } @@ -2474,10 +2473,9 @@ srs_error_t SrsLiveSource::on_video_imp(SrsSharedPtrMessage* msg) srs_error_t SrsLiveSource::on_aggregate(SrsCommonMessage* msg) { srs_error_t err = srs_success; - - SrsBuffer* stream = new SrsBuffer(msg->payload, msg->size); - SrsAutoFree(SrsBuffer, stream); - + + SrsUniquePtr stream(new SrsBuffer(msg->payload, msg->size)); + // the aggregate message always use abs time. int delta = -1; @@ -2662,13 +2660,6 @@ void SrsLiveSource::on_unpublish() srs_error_t SrsLiveSource::create_consumer(SrsLiveConsumer*& consumer) { srs_error_t err = srs_success; - - consumer = new SrsLiveConsumer(this); - consumers.push_back(consumer); - - // There should be one consumer, so reset the timeout. - stream_die_at_ = 0; - publisher_idle_at_ = 0; // for edge, when play edge stream, check the state if (_srs_config->get_vhost_is_edge(req->vhost)) { @@ -2677,6 +2668,13 @@ srs_error_t SrsLiveSource::create_consumer(SrsLiveConsumer*& consumer) return srs_error_wrap(err, "play edge"); } } + + consumer = new SrsLiveConsumer(this); + consumers.push_back(consumer); + + // There are more than one consumer, so reset the timeout. + stream_die_at_ = 0; + publisher_idle_at_ = 0; return err; } diff --git a/trunk/src/app/srs_app_srt_conn.cpp b/trunk/src/app/srs_app_srt_conn.cpp index 47d3428953..4845f9ea7f 100644 --- a/trunk/src/app/srs_app_srt_conn.cpp +++ b/trunk/src/app/srs_app_srt_conn.cpp @@ -439,8 +439,7 @@ srs_error_t SrsMpegtsSrtConn::do_publishing() { srs_error_t err = srs_success; - SrsPithyPrint* pprint = SrsPithyPrint::create_srt_publish(); - SrsAutoFree(SrsPithyPrint, pprint); + SrsUniquePtr pprint(SrsPithyPrint::create_srt_publish()); int nb_packets = 0; @@ -487,20 +486,20 @@ srs_error_t SrsMpegtsSrtConn::do_playing() { srs_error_t err = srs_success; - SrsSrtConsumer* consumer = NULL; - SrsAutoFree(SrsSrtConsumer, consumer); - if ((err = srt_source_->create_consumer(consumer)) != srs_success) { + SrsSrtConsumer* consumer_raw = NULL; + if ((err = srt_source_->create_consumer(consumer_raw)) != srs_success) { return srs_error_wrap(err, "create consumer, ts source=%s", req_->get_stream_url().c_str()); } - srs_assert(consumer); + + srs_assert(consumer_raw); + SrsUniquePtr consumer(consumer_raw); // TODO: FIXME: Dumps the SPS/PPS from gop cache, without other frames. - if ((err = srt_source_->consumer_dumps(consumer)) != srs_success) { + if ((err = srt_source_->consumer_dumps(consumer.get())) != srs_success) { return srs_error_wrap(err, "dumps consumer, url=%s", req_->get_stream_url().c_str()); } - SrsPithyPrint* pprint = SrsPithyPrint::create_srt_play(); - SrsAutoFree(SrsPithyPrint, pprint); + SrsUniquePtr pprint(SrsPithyPrint::create_srt_play()); SrsSrtRecvThread srt_recv_trd(srt_conn_); if ((err = srt_recv_trd.start()) != srs_success) { @@ -519,15 +518,16 @@ srs_error_t SrsMpegtsSrtConn::do_playing() } // Wait for amount of packets. - SrsSrtPacket* pkt = NULL; - SrsAutoFree(SrsSrtPacket, pkt); - consumer->dump_packet(&pkt); - if (!pkt) { + SrsSrtPacket* pkt_raw = NULL; + consumer->dump_packet(&pkt_raw); + if (!pkt_raw) { // TODO: FIXME: We should check the quit event. consumer->wait(1, 1000 * SRS_UTIME_MILLISECONDS); continue; } + SrsUniquePtr pkt(pkt_raw); + ++nb_packets; // reportable @@ -580,11 +580,10 @@ srs_error_t SrsMpegtsSrtConn::on_srt_packet(char* buf, int nb_buf) return srs_error_new(ERROR_SRT_CONN, "invalid ts packet first=%#x", (uint8_t)buf[0]); } - SrsSrtPacket* packet = new SrsSrtPacket(); - SrsAutoFree(SrsSrtPacket, packet); + SrsUniquePtr packet(new SrsSrtPacket()); packet->wrap(buf, nb_buf); - if ((err = srt_source_->on_packet(packet)) != srs_success) { + if ((err = srt_source_->on_packet(packet.get())) != srs_success) { return srs_error_wrap(err, "on srt packet"); } diff --git a/trunk/src/app/srs_app_srt_source.cpp b/trunk/src/app/srs_app_srt_source.cpp index b3383e6fa3..98470c879d 100644 --- a/trunk/src/app/srs_app_srt_source.cpp +++ b/trunk/src/app/srs_app_srt_source.cpp @@ -307,13 +307,11 @@ srs_error_t SrsSrtFrameBuilder::on_packet(SrsSrtPacket *pkt) int nb_packet = nb_buf / SRS_TS_PACKET_SIZE; for (int i = 0; i < nb_packet; i++) { char* p = buf + (i * SRS_TS_PACKET_SIZE); - - SrsBuffer* stream = new SrsBuffer(p, SRS_TS_PACKET_SIZE); - SrsAutoFree(SrsBuffer, stream); + SrsUniquePtr stream(new SrsBuffer(p, SRS_TS_PACKET_SIZE)); // Process each ts packet. Note that the jitter of UDP may cause video glitch when packet loss or wrong seq. We // don't handle it because SRT will, see tlpktdrop at https://ossrs.net/lts/zh-cn/docs/v4/doc/srt-params - if ((err = ts_ctx_->decode(stream, this)) != srs_success) { + if ((err = ts_ctx_->decode(stream.get(), this)) != srs_success) { srs_warn("parse ts packet err=%s", srs_error_desc(err).c_str()); srs_error_reset(err); continue; @@ -391,9 +389,8 @@ srs_error_t SrsSrtFrameBuilder::on_ts_video_avc(SrsTsMessage* msg, SrsBuffer* av vector > ipb_frames; - SrsRawH264Stream* avc = new SrsRawH264Stream(); - SrsAutoFree(SrsRawH264Stream, avc); - + SrsUniquePtr avc(new SrsRawH264Stream()); + // send each frame. while (!avs->empty()) { char* frame = NULL; @@ -465,8 +462,7 @@ srs_error_t SrsSrtFrameBuilder::check_sps_pps_change(SrsTsMessage* msg) uint32_t dts = (uint32_t)(msg->dts / 90); std::string sh; - SrsRawH264Stream* avc = new SrsRawH264Stream(); - SrsAutoFree(SrsRawH264Stream, avc); + SrsUniquePtr avc(new SrsRawH264Stream()); if ((err = avc->mux_sequence_header(sps_, pps_, sh)) != srs_success) { return srs_error_wrap(err, "mux sequence header"); @@ -565,9 +561,7 @@ srs_error_t SrsSrtFrameBuilder::on_ts_video_hevc(SrsTsMessage *msg, SrsBuffer *a srs_error_t err = srs_success; vector > ipb_frames; - - SrsRawHEVCStream *hevc = new SrsRawHEVCStream(); - SrsAutoFree(SrsRawHEVCStream, hevc); + SrsUniquePtr hevc(new SrsRawHEVCStream()); std::vector hevc_pps; // send each frame. @@ -660,8 +654,7 @@ srs_error_t SrsSrtFrameBuilder::check_vps_sps_pps_change(SrsTsMessage* msg) uint32_t dts = (uint32_t)(msg->dts / 90); std::string sh; - SrsRawHEVCStream* hevc = new SrsRawHEVCStream(); - SrsAutoFree(SrsRawHEVCStream, hevc); + SrsUniquePtr hevc(new SrsRawHEVCStream()); if ((err = hevc->mux_sequence_header(hevc_vps_, hevc_sps_, hevc_pps_, sh)) != srs_success) { return srs_error_wrap(err, "mux sequence header"); @@ -765,9 +758,8 @@ srs_error_t SrsSrtFrameBuilder::on_hevc_frame(SrsTsMessage* msg, vector aac(new SrsRawAacStream()); // ts tbn to flv tbn. uint32_t pts = (uint32_t)(msg->pts / 90); diff --git a/trunk/src/app/srs_app_tencentcloud.cpp b/trunk/src/app/srs_app_tencentcloud.cpp index 68a2f4ae34..e904f197f8 100644 --- a/trunk/src/app/srs_app_tencentcloud.cpp +++ b/trunk/src/app/srs_app_tencentcloud.cpp @@ -706,11 +706,10 @@ srs_error_t SrsClsClient::report() return err; } - SrsClsSugars* sugars = sugars_; - SrsAutoFree(SrsClsSugars, sugars); + SrsUniquePtr sugars(sugars_); sugars_ = new SrsClsSugars(); - if ((err = send_logs(sugars)) != srs_success) { + if ((err = send_logs(sugars.get())) != srs_success) { return srs_error_wrap(err, "cls"); } @@ -727,16 +726,15 @@ srs_error_t SrsClsClient::do_send_logs(ISrsEncoder* sugar, int count, int total) return srs_error_new(ERROR_CLS_EXCEED_SIZE, "exceed 5MB actual %d", size); } - char* buf = new char[size]; - SrsAutoFreeA(char, buf); + SrsUniquePtr buf(new char[size]); - memset(buf, 0, size); - SrsBuffer b(buf, size); + memset(buf.get(), 0, size); + SrsBuffer b(buf.get(), size); if ((err = sugar->encode(&b)) != srs_success) { return srs_error_wrap(err, "encode log"); } - string body(buf, size); + string body(buf.get(), size); // Write a CLS log to service specified by url. string url = "http://" + endpoint_ + ":80/structuredlog?topic_id=" + topic_; @@ -776,11 +774,11 @@ srs_error_t SrsClsClient::do_send_logs(ISrsEncoder* sugar, int count, int total) } // Start request and parse response. - ISrsHttpMessage* msg = NULL; - if ((err = http.post(path, body, &msg)) != srs_success) { + ISrsHttpMessage* msg_raw = NULL; + if ((err = http.post(path, body, &msg_raw)) != srs_success) { return srs_error_wrap(err, "http: client post"); } - SrsAutoFree(ISrsHttpMessage, msg); + SrsUniquePtr msg(msg_raw); string res; uint16_t code = msg->status_code(); @@ -819,10 +817,9 @@ srs_error_t SrsClsClient::send_logs(SrsClsSugars* sugars) // Never do infinite loop, limit to a max loop and drop logs if exceed. for (int i = 0; i < 128 && !sugars->empty(); ++i) { - SrsClsSugars* v = sugars->slice(SRS_CLS_BATCH_MAX_LOG_SIZE); - SrsAutoFree(SrsClsSugars, v); + SrsUniquePtr v(sugars->slice(SRS_CLS_BATCH_MAX_LOG_SIZE)); - if ((err = do_send_logs((ISrsEncoder*)v, v->size(), total)) != srs_success) { + if ((err = do_send_logs((ISrsEncoder*)v.get(), v->size(), total)) != srs_success) { return srs_error_wrap(err, "send %d/%d/%d logs", v->size(), i, total); } } @@ -2148,8 +2145,7 @@ srs_error_t SrsApmClient::do_report() // Update statistaic for APM. nn_spans_ += spans_.size(); - SrsOtelExportTraceServiceRequest* sugar = new SrsOtelExportTraceServiceRequest(); - SrsAutoFree(SrsOtelExportTraceServiceRequest, sugar); + SrsUniquePtr sugar(new SrsOtelExportTraceServiceRequest()); SrsOtelResourceSpans* rs = sugar->append(); // See https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions @@ -2169,16 +2165,15 @@ srs_error_t SrsApmClient::do_report() return srs_error_new(ERROR_APM_EXCEED_SIZE, "exceed 5MB actual %d", size); } - char* buf = new char[size]; - SrsAutoFreeA(char, buf); + SrsUniquePtr buf(new char[size]); - memset(buf, 0, size); - SrsBuffer b(buf, size); + memset(buf.get(), 0, size); + SrsBuffer b(buf.get(), size); if ((err = sugar->encode(&b)) != srs_success) { return srs_error_wrap(err, "encode log"); } - string body(buf, size); + string body(buf.get(), size); // Write a CLS log to service specified by url. string url = "http://" + endpoint_ + "/v1/traces"; @@ -2204,11 +2199,11 @@ srs_error_t SrsApmClient::do_report() } // Start request and parse response. - ISrsHttpMessage* msg = NULL; - if ((err = http.post(path, body, &msg)) != srs_success) { + ISrsHttpMessage* msg_raw = NULL; + if ((err = http.post(path, body, &msg_raw)) != srs_success) { return srs_error_wrap(err, "http: client post"); } - SrsAutoFree(ISrsHttpMessage, msg); + SrsUniquePtr msg(msg_raw); string res; uint16_t code = msg->status_code(); diff --git a/trunk/src/core/srs_core_autofree.hpp b/trunk/src/core/srs_core_autofree.hpp index 7bc3935c83..b0a4231067 100644 --- a/trunk/src/core/srs_core_autofree.hpp +++ b/trunk/src/core/srs_core_autofree.hpp @@ -9,79 +9,106 @@ #include -#include - -// The auto free helper, which is actually the unique ptr, without the move feature, +// Unique ptr smart pointer, only support unique ptr, with limited APIs and features, // see https://github.com/ossrs/srs/discussions/3667#discussioncomment-8969107 // -// To free the instance in the current scope, for instance, MyClass* ptr, -// which is a ptr and this class will: -// 1. free the ptr. -// 2. set ptr to NULL. -// // Usage: -// MyClass* po = new MyClass(); -// // ...... use po -// SrsAutoFree(MyClass, po); +// SrsUniquePtr ptr(new MyClass()); +// ptr->do_something(); // -// Usage for array: -// MyClass** pa = new MyClass*[size]; -// // ....... use pa -// SrsAutoFreeA(MyClass*, pa); +// Note that the ptr should be initialized before use it, or it will crash if not set, for example: +// Myclass* p; +// SrsUniquePtr ptr(p); // crash because p is an invalid pointer. // -// @remark the MyClass can be basic type, for instance, SrsAutoFreeA(char, pstr), -// where the char* pstr = new char[size]. -// To delete object. -#define SrsAutoFree(className, instance) \ - impl_SrsAutoFree _auto_free_##instance(&instance, false, false, NULL) -// To delete array. -#define SrsAutoFreeA(className, instance) \ - impl_SrsAutoFree _auto_free_array_##instance(&instance, true, false, NULL) -// Use free instead of delete. -#define SrsAutoFreeF(className, instance) \ - impl_SrsAutoFree _auto_free_##instance(&instance, false, true, NULL) -// Use hook instead of delete. -#define SrsAutoFreeH(className, instance, hook) \ - impl_SrsAutoFree _auto_free_##instance(&instance, false, false, hook) -// The template implementation. +// Note that do not support array or object created by malloc, because we only use delete to dispose +// the resource. template -class impl_SrsAutoFree +class SrsUniquePtr { private: - T** ptr; - bool is_array; - bool _use_free; - void (*_hook)(T*); + T* ptr_; public: - // If use_free, use free(void*) to release the p. - // If specified hook, use hook(p) to release it. - // Use delete to release p, or delete[] if p is an array. - impl_SrsAutoFree(T** p, bool array, bool use_free, void (*hook)(T*)) { - ptr = p; - is_array = array; - _use_free = use_free; - _hook = hook; - } - - virtual ~impl_SrsAutoFree() { - if (ptr == NULL || *ptr == NULL) { - return; - } + SrsUniquePtr(T* ptr = NULL) { + ptr_ = ptr; + } + virtual ~SrsUniquePtr() { + delete ptr_; + } +public: + // Get the object. + T* get() { + return ptr_; + } + // Overload the -> operator. + T* operator->() { + return ptr_; + } +private: + // Copy the unique ptr. + SrsUniquePtr(const SrsUniquePtr&) = delete; + // The assign operator. + SrsUniquePtr& operator=(const SrsUniquePtr&) = delete; +private: + // Overload the * operator. + T& operator*() = delete; + // Overload the bool operator. + operator bool() const = delete; +#if __cplusplus >= 201103L // C++11 +private: + // The move constructor. + SrsUniquePtr(SrsUniquePtr&&) = delete; + // The move assign operator. + SrsUniquePtr& operator=(SrsUniquePtr&&) = delete; +#endif +}; - if (_use_free) { - free(*ptr); - } else if (_hook) { - _hook(*ptr); - } else { - if (is_array) { - delete[] *ptr; - } else { - delete *ptr; - } - } - - *ptr = NULL; +// The unique ptr for array objects, only support unique ptr, with limited APIs and features, +// see https://github.com/ossrs/srs/discussions/3667#discussioncomment-8969107 +// +// Usage: +// SrsUniquePtr ptr(new MyClass[10]); +// ptr[0]->do_something(); +template +class SrsUniquePtr +{ +private: + T* ptr_; +public: + SrsUniquePtr(T* ptr = NULL) { + ptr_ = ptr; + } + virtual ~SrsUniquePtr() { + delete[] ptr_; + } +public: + // Get the object. + T* get() { + return ptr_; + } + // Overload the [] operator. + T& operator[](std::size_t index) { + return ptr_[index]; + } + const T& operator[](std::size_t index) const { + return ptr_[index]; } +private: + // Copy the unique ptr. + SrsUniquePtr(const SrsUniquePtr&) = delete; + // The assign operator. + SrsUniquePtr& operator=(const SrsUniquePtr&) = delete; +private: + // Overload the * operator. + T& operator*() = delete; + // Overload the bool operator. + operator bool() const = delete; +#if __cplusplus >= 201103L // C++11 +private: + // The move constructor. + SrsUniquePtr(SrsUniquePtr&&) = delete; + // The move assign operator. + SrsUniquePtr& operator=(SrsUniquePtr&&) = delete; +#endif }; // Shared ptr smart pointer, only support shared ptr, no weak ptr, no shared from this, no inheritance, diff --git a/trunk/src/core/srs_core_deprecated.cpp b/trunk/src/core/srs_core_deprecated.cpp new file mode 100644 index 0000000000..0560bcaf56 --- /dev/null +++ b/trunk/src/core/srs_core_deprecated.cpp @@ -0,0 +1,8 @@ +// +// Copyright (c) 2013-2024 The SRS Authors +// +// SPDX-License-Identifier: MIT +// + +#include + diff --git a/trunk/src/core/srs_core_deprecated.hpp b/trunk/src/core/srs_core_deprecated.hpp new file mode 100644 index 0000000000..dfc7e43b3e --- /dev/null +++ b/trunk/src/core/srs_core_deprecated.hpp @@ -0,0 +1,95 @@ +// +// Copyright (c) 2013-2024 The SRS Authors +// +// SPDX-License-Identifier: MIT +// + +#ifndef SRS_CORE_DEPRECATED_HPP +#define SRS_CORE_DEPRECATED_HPP + +#include + +#include + +// Note that the SrsAutoFree is deprecated, please use SrsUniquePtr instead. +// +// Note: Please use SrsUniquePtr if possible. Please aware that there is a slight difference between SrsAutoFree +// and SrsUniquePtr. SrsAutoFree will track the address of pointer, while SrsUniquePtr will not. +// MyClass* p; +// SrsAutoFree(MyClass, p); // p will be freed even p is changed later. +// SrsUniquePtr ptr(p); // crash because p is an invalid pointer. +// +// The auto free helper, which is actually the unique ptr, without the move feature, +// see https://github.com/ossrs/srs/discussions/3667#discussioncomment-8969107 +// +// To free the instance in the current scope, for instance, MyClass* ptr, +// which is a ptr and this class will: +// 1. free the ptr. +// 2. set ptr to NULL. +// +// Usage: +// MyClass* po = new MyClass(); +// // ...... use po +// SrsAutoFree(MyClass, po); +// +// Usage for array: +// MyClass** pa = new MyClass*[size]; +// // ....... use pa +// SrsAutoFreeA(MyClass*, pa); +// +// @remark the MyClass can be basic type, for instance, SrsAutoFreeA(char, pstr), +// where the char* pstr = new char[size]. +// To delete object. +#define SrsAutoFree(className, instance) \ + impl_SrsAutoFree _auto_free_##instance(&instance, false, false, NULL) +// To delete array. +#define SrsAutoFreeA(className, instance) \ + impl_SrsAutoFree _auto_free_array_##instance(&instance, true, false, NULL) +// Use free instead of delete. +#define SrsAutoFreeF(className, instance) \ + impl_SrsAutoFree _auto_free_##instance(&instance, false, true, NULL) +// Use hook instead of delete. +#define SrsAutoFreeH(className, instance, hook) \ + impl_SrsAutoFree _auto_free_##instance(&instance, false, false, hook) +// The template implementation. +template +class impl_SrsAutoFree +{ +private: + T** ptr; + bool is_array; + bool _use_free; + void (*_hook)(T*); +public: + // If use_free, use free(void*) to release the p. + // If specified hook, use hook(p) to release it. + // Use delete to release p, or delete[] if p is an array. + impl_SrsAutoFree(T** p, bool array, bool use_free, void (*hook)(T*)) { + ptr = p; + is_array = array; + _use_free = use_free; + _hook = hook; + } + + virtual ~impl_SrsAutoFree() { + if (ptr == NULL || *ptr == NULL) { + return; + } + + if (_use_free) { + free(*ptr); + } else if (_hook) { + _hook(*ptr); + } else { + if (is_array) { + delete[] *ptr; + } else { + delete *ptr; + } + } + + *ptr = NULL; + } +}; + +#endif diff --git a/trunk/src/core/srs_core_version6.hpp b/trunk/src/core/srs_core_version6.hpp index a961f89592..f3646d3534 100644 --- a/trunk/src/core/srs_core_version6.hpp +++ b/trunk/src/core/srs_core_version6.hpp @@ -9,6 +9,6 @@ #define VERSION_MAJOR 6 #define VERSION_MINOR 0 -#define VERSION_REVISION 135 +#define VERSION_REVISION 136 #endif diff --git a/trunk/src/kernel/srs_kernel_aac.cpp b/trunk/src/kernel/srs_kernel_aac.cpp index a3dad0c006..8e448db5ed 100644 --- a/trunk/src/kernel/srs_kernel_aac.cpp +++ b/trunk/src/kernel/srs_kernel_aac.cpp @@ -53,10 +53,9 @@ srs_error_t SrsAacTransmuxer::write_audio(int64_t timestamp, char* data, int siz srs_assert(data); timestamp &= 0x7fffffff; - - SrsBuffer* stream = new SrsBuffer(data, size); - SrsAutoFree(SrsBuffer, stream); - + + SrsUniquePtr stream(new SrsBuffer(data, size)); + // audio decode if (!stream->require(1)) { return srs_error_new(ERROR_AAC_DECODE_ERROR, "aac decode audio sound_format failed"); diff --git a/trunk/src/kernel/srs_kernel_codec.cpp b/trunk/src/kernel/srs_kernel_codec.cpp index dc651a6db6..00d7d9cb4f 100644 --- a/trunk/src/kernel/srs_kernel_codec.cpp +++ b/trunk/src/kernel/srs_kernel_codec.cpp @@ -730,13 +730,12 @@ srs_error_t SrsVideoFrame::parse_avc_b_frame(const SrsSample* sample, bool& is_b return err; } - SrsBuffer* stream = new SrsBuffer(sample->bytes, sample->size); - SrsAutoFree(SrsBuffer, stream); + SrsUniquePtr stream(new SrsBuffer(sample->bytes, sample->size)); // Skip nalu header. stream->skip(1); - SrsBitBuffer bitstream(stream); + SrsBitBuffer bitstream(stream.get()); int32_t first_mb_in_slice = 0; if ((err = srs_avc_nalu_read_uev(&bitstream, first_mb_in_slice)) != srs_success) { return srs_error_wrap(err, "nalu read uev"); @@ -793,10 +792,9 @@ srs_error_t SrsFormat::on_audio(int64_t timestamp, char* data, int size) srs_info("no audio present, ignore it."); return err; } - - SrsBuffer* buffer = new SrsBuffer(data, size); - SrsAutoFree(SrsBuffer, buffer); - + + SrsUniquePtr buffer(new SrsBuffer(data, size)); + // We already checked the size is positive and data is not NULL. srs_assert(buffer->require(1)); @@ -824,10 +822,10 @@ srs_error_t SrsFormat::on_audio(int64_t timestamp, char* data, int size) buffer->skip(-1 * buffer->pos()); if (codec == SrsAudioCodecIdMP3) { - return audio_mp3_demux(buffer, timestamp, fresh); + return audio_mp3_demux(buffer.get(), timestamp, fresh); } - return audio_aac_demux(buffer, timestamp); + return audio_aac_demux(buffer.get(), timestamp); } srs_error_t SrsFormat::on_video(int64_t timestamp, char* data, int size) @@ -838,11 +836,9 @@ srs_error_t SrsFormat::on_video(int64_t timestamp, char* data, int size) srs_trace("no video present, ignore it."); return err; } - - SrsBuffer* buffer = new SrsBuffer(data, size); - SrsAutoFree(SrsBuffer, buffer); - return video_avc_demux(buffer, timestamp); + SrsUniquePtr buffer(new SrsBuffer(data, size)); + return video_avc_demux(buffer.get(), timestamp); } srs_error_t SrsFormat::on_aac_sequence_header(char* data, int size) @@ -2807,10 +2803,9 @@ srs_error_t SrsFormat::audio_mp3_demux(SrsBuffer* stream, int64_t timestamp, boo srs_error_t SrsFormat::audio_aac_sequence_header_demux(char* data, int size) { srs_error_t err = srs_success; - - SrsBuffer* buffer = new SrsBuffer(data, size); - SrsAutoFree(SrsBuffer, buffer); - + + SrsUniquePtr buffer(new SrsBuffer(data, size)); + // only need to decode the first 2bytes: // audioObjectType, aac_profile, 5bits. // samplingFrequencyIndex, aac_sample_rate, 4bits. diff --git a/trunk/src/kernel/srs_kernel_flv.cpp b/trunk/src/kernel/srs_kernel_flv.cpp index 86ef2b3c38..7975975b8f 100644 --- a/trunk/src/kernel/srs_kernel_flv.cpp +++ b/trunk/src/kernel/srs_kernel_flv.cpp @@ -584,10 +584,9 @@ void SrsFlvTransmuxer::cache_metadata(char type, char* data, int size, char* cac (char)0x00, // TimestampExtended UI8 (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0. };*/ - - SrsBuffer* tag_stream = new SrsBuffer(cache, 11); - SrsAutoFree(SrsBuffer, tag_stream); - + + SrsUniquePtr tag_stream(new SrsBuffer(cache, 11)); + // write data size. tag_stream->write_1bytes(type); tag_stream->write_3bytes(size); @@ -610,10 +609,9 @@ void SrsFlvTransmuxer::cache_audio(int64_t timestamp, char* data, int size, char (char)0x00, // TimestampExtended UI8 (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0. };*/ - - SrsBuffer* tag_stream = new SrsBuffer(cache, 11); - SrsAutoFree(SrsBuffer, tag_stream); - + + SrsUniquePtr tag_stream(new SrsBuffer(cache, 11)); + // write data size. tag_stream->write_1bytes(SrsFrameTypeAudio); tag_stream->write_3bytes(size); @@ -637,10 +635,9 @@ void SrsFlvTransmuxer::cache_video(int64_t timestamp, char* data, int size, char (char)0x00, // TimestampExtended UI8 (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0. };*/ - - SrsBuffer* tag_stream = new SrsBuffer(cache, 11); - SrsAutoFree(SrsBuffer, tag_stream); - + + SrsUniquePtr tag_stream(new SrsBuffer(cache, 11)); + // write data size. tag_stream->write_1bytes(SrsFrameTypeVideo); tag_stream->write_3bytes(size); @@ -652,8 +649,7 @@ void SrsFlvTransmuxer::cache_video(int64_t timestamp, char* data, int size, char void SrsFlvTransmuxer::cache_pts(int size, char* cache) { - SrsBuffer* tag_stream = new SrsBuffer(cache, 11); - SrsAutoFree(SrsBuffer, tag_stream); + SrsUniquePtr tag_stream(new SrsBuffer(cache, 11)); tag_stream->write_4bytes(size); } @@ -860,10 +856,9 @@ srs_error_t SrsFlvVodStreamDecoder::read_sequence_header_summary(int64_t* pstart if ((err = reader->read(tag_header, SRS_FLV_TAG_HEADER_SIZE, NULL)) != srs_success) { return srs_error_wrap(err, "read tag header"); } - - SrsBuffer* tag_stream = new SrsBuffer(tag_header, SRS_FLV_TAG_HEADER_SIZE); - SrsAutoFree(SrsBuffer, tag_stream); - + + SrsUniquePtr tag_stream(new SrsBuffer(tag_header, SRS_FLV_TAG_HEADER_SIZE)); + int8_t tag_type = tag_stream->read_1bytes(); int32_t data_size = tag_stream->read_3bytes(); diff --git a/trunk/src/kernel/srs_kernel_mp3.cpp b/trunk/src/kernel/srs_kernel_mp3.cpp index c18a28beee..032ddd2a58 100644 --- a/trunk/src/kernel/srs_kernel_mp3.cpp +++ b/trunk/src/kernel/srs_kernel_mp3.cpp @@ -74,10 +74,9 @@ srs_error_t SrsMp3Transmuxer::write_audio(int64_t timestamp, char* data, int siz srs_assert(data); timestamp &= 0x7fffffff; - - SrsBuffer* stream = new SrsBuffer(data, size); - SrsAutoFree(SrsBuffer, stream); - + + SrsUniquePtr stream(new SrsBuffer(data, size)); + // audio decode if (!stream->require(1)) { return srs_error_new(ERROR_MP3_DECODE_ERROR, "decode sound_format"); diff --git a/trunk/src/kernel/srs_kernel_mp4.cpp b/trunk/src/kernel/srs_kernel_mp4.cpp index f0ce4c1f93..b4d597f7ee 100644 --- a/trunk/src/kernel/srs_kernel_mp4.cpp +++ b/trunk/src/kernel/srs_kernel_mp4.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -36,10 +37,8 @@ srs_error_t srs_mp4_write_box(ISrsWriter* writer, ISrsCodec* box) int nb_data = box->nb_bytes(); std::vector data(nb_data); - SrsBuffer* buffer = new SrsBuffer(&data[0], nb_data); - SrsAutoFree(SrsBuffer, buffer); - - if ((err = box->encode(buffer)) != srs_success) { + SrsUniquePtr buffer(new SrsBuffer(&data[0], nb_data)); + if ((err = box->encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode box"); } @@ -5316,6 +5315,7 @@ srs_error_t SrsMp4BoxReader::read(SrsSimpleStream* stream, SrsMp4Box** ppbox) srs_error_t err = srs_success; SrsMp4Box* box = NULL; + // Note that we should use SrsAutoFree to free the ptr which is set later. SrsAutoFree(SrsMp4Box, box); while (true) { @@ -5338,12 +5338,11 @@ srs_error_t SrsMp4BoxReader::read(SrsSimpleStream* stream, SrsMp4Box** ppbox) srs_assert(nread > 0); stream->append(buf, (int)nread); } - - SrsBuffer* buffer = new SrsBuffer(stream->bytes(), stream->length()); - SrsAutoFree(SrsBuffer, buffer); - + + SrsUniquePtr buffer(new SrsBuffer(stream->bytes(), stream->length())); + // Discovery the box with basic header. - if (!box && (err = SrsMp4Box::discovery(buffer, &box)) != srs_success) { + if (!box && (err = SrsMp4Box::discovery(buffer.get(), &box)) != srs_success) { if (srs_error_code(err) == ERROR_MP4_BOX_REQUIRE_SPACE) { srs_freep(err); continue; @@ -5431,15 +5430,14 @@ srs_error_t SrsMp4Decoder::initialize(ISrsReadSeeker* rs) off_t offset = -1; while (true) { - SrsMp4Box* box = NULL; - SrsAutoFree(SrsMp4Box, box); - - if ((err = load_next_box(&box, 0)) != srs_success) { + SrsMp4Box* box_raw = NULL; + if ((err = load_next_box(&box_raw, 0)) != srs_success) { return srs_error_wrap(err, "load box"); } + SrsUniquePtr box(box_raw); if (box->is_ftyp()) { - SrsMp4FileTypeBox* ftyp = dynamic_cast(box); + SrsMp4FileTypeBox* ftyp = dynamic_cast(box.get()); if ((err = parse_ftyp(ftyp)) != srs_success) { return srs_error_wrap(err, "parse ftyp"); } @@ -5450,7 +5448,7 @@ srs_error_t SrsMp4Decoder::initialize(ISrsReadSeeker* rs) } offset = off_t(cur - box->sz()); } else if (box->is_moov()) { - SrsMp4MovieBox* moov = dynamic_cast(box); + SrsMp4MovieBox* moov = dynamic_cast(box.get()); if ((err = parse_moov(moov)) != srs_success) { return srs_error_wrap(err, "parse moov"); } @@ -5653,6 +5651,7 @@ srs_error_t SrsMp4Decoder::load_next_box(SrsMp4Box** ppbox, uint32_t required_bo while (true) { SrsMp4Box* box = NULL; + // Note that we should use SrsAutoFree to free the ptr which is set later. SrsAutoFree(SrsMp4Box, box); if ((err = do_load_next_box(&box, required_box_type)) != srs_success) { @@ -5679,16 +5678,15 @@ srs_error_t SrsMp4Decoder::do_load_next_box(SrsMp4Box** ppbox, uint32_t required if ((err = br->read(stream, &box)) != srs_success) { return srs_error_wrap(err, "read box"); } - - SrsBuffer* buffer = new SrsBuffer(stream->bytes(), stream->length()); - SrsAutoFree(SrsBuffer, buffer); - + + SrsUniquePtr buffer(new SrsBuffer(stream->bytes(), stream->length())); + // Decode the box: // 1. Any box, when no box type is required. // 2. Matched box, when box type match the required type. // 3. Mdat box, always decode the mdat because we only decode the header of it. if (!required_box_type || (uint32_t)box->type == required_box_type || box->is_mdat()) { - err = box->decode(buffer); + err = box->decode(buffer.get()); } // Skip the box from stream, move stream to next box. @@ -5741,20 +5739,17 @@ srs_error_t SrsMp4Encoder::initialize(ISrsWriteSeeker* ws) // Write ftyp box. if (true) { - SrsMp4FileTypeBox* ftyp = new SrsMp4FileTypeBox(); - SrsAutoFree(SrsMp4FileTypeBox, ftyp); - + SrsUniquePtr ftyp(new SrsMp4FileTypeBox()); + ftyp->major_brand = SrsMp4BoxBrandISOM; ftyp->minor_version = 512; ftyp->set_compatible_brands(SrsMp4BoxBrandISOM, SrsMp4BoxBrandISO2, SrsMp4BoxBrandMP41); int nb_data = ftyp->nb_bytes(); std::vector data(nb_data); - - SrsBuffer* buffer = new SrsBuffer(&data[0], nb_data); - SrsAutoFree(SrsBuffer, buffer); - - if ((err = ftyp->encode(buffer)) != srs_success) { + + SrsUniquePtr buffer(new SrsBuffer(&data[0], nb_data)); + if ((err = ftyp->encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode ftyp"); } @@ -5766,16 +5761,13 @@ srs_error_t SrsMp4Encoder::initialize(ISrsWriteSeeker* ws) // 8B reserved free box. if (true) { - SrsMp4FreeSpaceBox* freeb = new SrsMp4FreeSpaceBox(SrsMp4BoxTypeFREE); - SrsAutoFree(SrsMp4FreeSpaceBox, freeb); + SrsUniquePtr freeb(new SrsMp4FreeSpaceBox(SrsMp4BoxTypeFREE)); int nb_data = freeb->nb_bytes(); std::vector data(nb_data); - SrsBuffer* buffer = new SrsBuffer(&data[0], nb_data); - SrsAutoFree(SrsBuffer, buffer); - - if ((err = freeb->encode(buffer)) != srs_success) { + SrsUniquePtr buffer(new SrsBuffer(&data[0], nb_data)); + if ((err = freeb->encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode free box"); } @@ -5789,27 +5781,23 @@ srs_error_t SrsMp4Encoder::initialize(ISrsWriteSeeker* ws) // Write empty mdat box, // its payload will be writen by samples, // and we will update its header(size) when flush. - SrsMp4MediaDataBox* mdat = new SrsMp4MediaDataBox(); - SrsAutoFree(SrsMp4MediaDataBox, mdat); - + SrsUniquePtr mdat(new SrsMp4MediaDataBox()); + // Update the mdat box from this offset. if ((err = wsio->lseek(0, SEEK_CUR, &mdat_offset)) != srs_success) { return srs_error_wrap(err, "seek to mdat"); } int nb_data = mdat->sz_header(); - uint8_t* data = new uint8_t[nb_data]; - SrsAutoFreeA(uint8_t, data); - - SrsBuffer* buffer = new SrsBuffer((char*)data, nb_data); - SrsAutoFree(SrsBuffer, buffer); - - if ((err = mdat->encode(buffer)) != srs_success) { + SrsUniquePtr data(new uint8_t[nb_data]); + + SrsUniquePtr buffer(new SrsBuffer((char*)data.get(), nb_data)); + if ((err = mdat->encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode mdat"); } // TODO: FIXME: Ensure all bytes are writen. - if ((err = wsio->write(data, nb_data, NULL)) != srs_success) { + if ((err = wsio->write(data.get(), nb_data, NULL)) != srs_success) { return srs_error_wrap(err, "write mdat"); } @@ -5874,9 +5862,8 @@ srs_error_t SrsMp4Encoder::flush() // Write moov. if (true) { - SrsMp4MovieBox* moov = new SrsMp4MovieBox(); - SrsAutoFree(SrsMp4MovieBox, moov); - + SrsUniquePtr moov(new SrsMp4MovieBox()); + SrsMp4MovieHeaderBox* mvhd = new SrsMp4MovieHeaderBox(); moov->set_mvhd(mvhd); @@ -6057,23 +6044,20 @@ srs_error_t SrsMp4Encoder::flush() } } - if ((err = samples->write(moov)) != srs_success) { + if ((err = samples->write(moov.get())) != srs_success) { return srs_error_wrap(err, "write samples"); } int nb_data = moov->nb_bytes(); - uint8_t* data = new uint8_t[nb_data]; - SrsAutoFreeA(uint8_t, data); - - SrsBuffer* buffer = new SrsBuffer((char*)data, nb_data); - SrsAutoFree(SrsBuffer, buffer); - - if ((err = moov->encode(buffer)) != srs_success) { + SrsUniquePtr data(new uint8_t[nb_data]); + + SrsUniquePtr buffer(new SrsBuffer((char*)data.get(), nb_data)); + if ((err = moov->encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode moov"); } // TODO: FIXME: Ensure all bytes are writen. - if ((err = wsio->write(data, nb_data, NULL)) != srs_success) { + if ((err = wsio->write(data.get(), nb_data, NULL)) != srs_success) { return srs_error_wrap(err, "write moov"); } } @@ -6083,21 +6067,17 @@ srs_error_t SrsMp4Encoder::flush() // Write mdat box with size of data, // its payload already writen by samples, // and we will update its header(size) when flush. - SrsMp4MediaDataBox* mdat = new SrsMp4MediaDataBox(); - SrsAutoFree(SrsMp4MediaDataBox, mdat); + SrsUniquePtr mdat(new SrsMp4MediaDataBox()); // Update the size of mdat first, for over 2GB file. mdat->nb_data = mdat_bytes; mdat->update_size(); int nb_data = mdat->sz_header(); - uint8_t* data = new uint8_t[nb_data]; - SrsAutoFreeA(uint8_t, data); - - SrsBuffer* buffer = new SrsBuffer((char*)data, nb_data); - SrsAutoFree(SrsBuffer, buffer); + SrsUniquePtr data(new uint8_t[nb_data]); - if ((err = mdat->encode(buffer)) != srs_success) { + SrsUniquePtr buffer(new SrsBuffer((char*)data.get(), nb_data)); + if ((err = mdat->encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode mdat"); } @@ -6117,7 +6097,7 @@ srs_error_t SrsMp4Encoder::flush() } // TODO: FIXME: Ensure all bytes are writen. - if ((err = wsio->write(data, nb_data, NULL)) != srs_success) { + if ((err = wsio->write(data.get(), nb_data, NULL)) != srs_success) { return srs_error_wrap(err, "write mdat"); } } @@ -6231,22 +6211,20 @@ srs_error_t SrsMp4M2tsInitEncoder::write(SrsFormat* format, bool video, int tid) // Write ftyp box. if (true) { - SrsMp4FileTypeBox* ftyp = new SrsMp4FileTypeBox(); - SrsAutoFree(SrsMp4FileTypeBox, ftyp); + SrsUniquePtr ftyp(new SrsMp4FileTypeBox()); ftyp->major_brand = SrsMp4BoxBrandISO5; ftyp->minor_version = 512; ftyp->set_compatible_brands(SrsMp4BoxBrandISO6, SrsMp4BoxBrandMP41); - if ((err = srs_mp4_write_box(writer, ftyp)) != srs_success) { + if ((err = srs_mp4_write_box(writer, ftyp.get())) != srs_success) { return srs_error_wrap(err, "write ftyp"); } } // Write moov. if (true) { - SrsMp4MovieBox* moov = new SrsMp4MovieBox(); - SrsAutoFree(SrsMp4MovieBox, moov); + SrsUniquePtr moov(new SrsMp4MovieBox()); SrsMp4MovieHeaderBox* mvhd = new SrsMp4MovieHeaderBox(); moov->set_mvhd(mvhd); @@ -6456,7 +6434,7 @@ srs_error_t SrsMp4M2tsInitEncoder::write(SrsFormat* format, bool video, int tid) trex->default_sample_description_index = 1; } - if ((err = srs_mp4_write_box(writer, moov)) != srs_success) { + if ((err = srs_mp4_write_box(writer, moov.get())) != srs_success) { return srs_error_wrap(err, "write moov"); } } @@ -6492,9 +6470,8 @@ srs_error_t SrsMp4M2tsSegmentEncoder::initialize(ISrsWriter* w, uint32_t sequenc // Write styp box. if (true) { - SrsMp4SegmentTypeBox* styp = new SrsMp4SegmentTypeBox(); - SrsAutoFree(SrsMp4SegmentTypeBox, styp); - + SrsUniquePtr styp(new SrsMp4SegmentTypeBox()); + styp->major_brand = SrsMp4BoxBrandMSDH; styp->minor_version = 0; styp->set_compatible_brands(SrsMp4BoxBrandMSDH, SrsMp4BoxBrandMSIX); @@ -6502,7 +6479,7 @@ srs_error_t SrsMp4M2tsSegmentEncoder::initialize(ISrsWriter* w, uint32_t sequenc // Used for sidx to calcalute the referenced size. styp_bytes = styp->nb_bytes(); - if ((err = srs_mp4_write_box(writer, styp)) != srs_success) { + if ((err = srs_mp4_write_box(writer, styp.get())) != srs_success) { return srs_error_wrap(err, "write styp"); } } @@ -6556,8 +6533,7 @@ srs_error_t SrsMp4M2tsSegmentEncoder::flush(uint64_t& dts) } // Although the sidx is not required to start play DASH, but it's required for AV sync. - SrsMp4SegmentIndexBox* sidx = new SrsMp4SegmentIndexBox(); - SrsAutoFree(SrsMp4SegmentIndexBox, sidx); + SrsUniquePtr sidx(new SrsMp4SegmentIndexBox()); if (true) { sidx->version = 1; sidx->reference_id = 1; @@ -6580,14 +6556,12 @@ srs_error_t SrsMp4M2tsSegmentEncoder::flush(uint64_t& dts) // Create a mdat box. // its payload will be writen by samples, // and we will update its header(size) when flush. - SrsMp4MediaDataBox* mdat = new SrsMp4MediaDataBox(); - SrsAutoFree(SrsMp4MediaDataBox, mdat); + SrsUniquePtr mdat(new SrsMp4MediaDataBox()); // Write moof. if (true) { - SrsMp4MovieFragmentBox* moof = new SrsMp4MovieFragmentBox(); - SrsAutoFree(SrsMp4MovieFragmentBox, moof); - + SrsUniquePtr moof(new SrsMp4MovieFragmentBox()); + SrsMp4MovieFragmentHeaderBox* mfhd = new SrsMp4MovieFragmentHeaderBox(); moof->set_mfhd(mfhd); @@ -6611,7 +6585,7 @@ srs_error_t SrsMp4M2tsSegmentEncoder::flush(uint64_t& dts) SrsMp4TrackFragmentRunBox* trun = new SrsMp4TrackFragmentRunBox(); traf->set_trun(trun); - if ((err = samples->write(moof, dts)) != srs_success) { + if ((err = samples->write(moof.get(), dts)) != srs_success) { return srs_error_wrap(err, "write samples"); } @@ -6623,11 +6597,11 @@ srs_error_t SrsMp4M2tsSegmentEncoder::flush(uint64_t& dts) // Update the size of sidx. SrsMp4SegmentIndexEntry* entry = &sidx->entries[0]; entry->referenced_size = moof_bytes + mdat->nb_bytes(); - if ((err = srs_mp4_write_box(writer, sidx)) != srs_success) { + if ((err = srs_mp4_write_box(writer, sidx.get())) != srs_success) { return srs_error_wrap(err, "write sidx"); } - if ((err = srs_mp4_write_box(writer, moof)) != srs_success) { + if ((err = srs_mp4_write_box(writer, moof.get())) != srs_success) { return srs_error_wrap(err, "write moof"); } } @@ -6635,18 +6609,15 @@ srs_error_t SrsMp4M2tsSegmentEncoder::flush(uint64_t& dts) // Write mdat. if (true) { int nb_data = mdat->sz_header(); - uint8_t* data = new uint8_t[nb_data]; - SrsAutoFreeA(uint8_t, data); - - SrsBuffer* buffer = new SrsBuffer((char*)data, nb_data); - SrsAutoFree(SrsBuffer, buffer); - - if ((err = mdat->encode(buffer)) != srs_success) { + SrsUniquePtr data(new uint8_t[nb_data]); + + SrsUniquePtr buffer(new SrsBuffer((char*)data.get(), nb_data)); + if ((err = mdat->encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode mdat"); } // TODO: FIXME: Ensure all bytes are writen. - if ((err = writer->write(data, nb_data, NULL)) != srs_success) { + if ((err = writer->write(data.get(), nb_data, NULL)) != srs_success) { return srs_error_wrap(err, "write mdat"); } diff --git a/trunk/src/kernel/srs_kernel_ps.cpp b/trunk/src/kernel/srs_kernel_ps.cpp index 6207a446b8..807e9ff36d 100644 --- a/trunk/src/kernel/srs_kernel_ps.cpp +++ b/trunk/src/kernel/srs_kernel_ps.cpp @@ -107,8 +107,7 @@ srs_error_t SrsPsContext::decode(SrsBuffer* stream, ISrsPsMessageHandler* handle } // Reap the last completed PS message. - SrsTsMessage* msg = reap(); - SrsAutoFree(SrsTsMessage, msg); + SrsUniquePtr msg(reap()); if (msg->sid == SrsTsPESStreamIdProgramStreamMap) { if (!msg->payload || !msg->payload->length()) { @@ -136,7 +135,7 @@ srs_error_t SrsPsContext::decode(SrsBuffer* stream, ISrsPsMessageHandler* handle helper_.pack_nn_msgs_++; //srs_error("PS: Got message %s, dts=%" PRId64 ", payload=%dB", msg->is_video() ? "Video" : "Audio", msg->dts/9000, msg->PES_packet_length); - if (handler && (err = handler->on_ts_message(msg)) != srs_success) { + if (handler && (err = handler->on_ts_message(msg.get())) != srs_success) { return srs_error_wrap(err, "handle PS message"); } } else { diff --git a/trunk/src/kernel/srs_kernel_ts.cpp b/trunk/src/kernel/srs_kernel_ts.cpp index a2fca00eab..5ae6e3e7d5 100644 --- a/trunk/src/kernel/srs_kernel_ts.cpp +++ b/trunk/src/kernel/srs_kernel_ts.cpp @@ -257,20 +257,20 @@ srs_error_t SrsTsContext::decode(SrsBuffer* stream, ISrsTsHandler* handler) // parse util EOF of stream. // for example, parse multiple times for the PES_packet_length(0) packet. while (!stream->empty()) { - SrsTsPacket* packet = new SrsTsPacket(this); - SrsAutoFree(SrsTsPacket, packet); - - SrsTsMessage* msg = NULL; - if ((err = packet->decode(stream, &msg)) != srs_success) { + SrsUniquePtr packet(new SrsTsPacket(this)); + + SrsTsMessage* msg_raw = NULL; + if ((err = packet->decode(stream, &msg_raw)) != srs_success) { return srs_error_wrap(err, "ts: ts packet decode"); } - if (!msg) { + if (!msg_raw) { continue; } - SrsAutoFree(SrsTsMessage, msg); + + SrsUniquePtr msg(msg_raw); - if ((err = handler->on_ts_message(msg)) != srs_success) { + if ((err = handler->on_ts_message(msg.get())) != srs_success) { return srs_error_wrap(err, "ts: handle ts message"); } } @@ -381,46 +381,42 @@ srs_error_t SrsTsContext::encode_pat_pmt(ISrsStreamWriter* writer, int16_t vpid, int16_t pmt_number = TS_PMT_NUMBER; int16_t pmt_pid = TS_PMT_PID; if (true) { - SrsTsPacket* pkt = SrsTsPacket::create_pat(this, pmt_number, pmt_pid); - SrsAutoFree(SrsTsPacket, pkt); - + SrsUniquePtr pkt(SrsTsPacket::create_pat(this, pmt_number, pmt_pid)); + pkt->sync_byte = sync_byte; - - char* buf = new char[SRS_TS_PACKET_SIZE]; - SrsAutoFreeA(char, buf); - + + SrsUniquePtr buf(new char[SRS_TS_PACKET_SIZE]); + // set the left bytes with 0xFF. int nb_buf = pkt->size(); srs_assert(nb_buf < SRS_TS_PACKET_SIZE); - memset(buf + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf); + memset(buf.get() + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf); - SrsBuffer stream(buf, nb_buf); + SrsBuffer stream(buf.get(), nb_buf); if ((err = pkt->encode(&stream)) != srs_success) { return srs_error_wrap(err, "ts: encode packet"); } - if ((err = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != srs_success) { + if ((err = writer->write(buf.get(), SRS_TS_PACKET_SIZE, NULL)) != srs_success) { return srs_error_wrap(err, "ts: write packet"); } } if (true) { - SrsTsPacket* pkt = SrsTsPacket::create_pmt(this, pmt_number, pmt_pid, vpid, vs, apid, as); - SrsAutoFree(SrsTsPacket, pkt); - + SrsUniquePtr pkt(SrsTsPacket::create_pmt(this, pmt_number, pmt_pid, vpid, vs, apid, as)); + pkt->sync_byte = sync_byte; - - char* buf = new char[SRS_TS_PACKET_SIZE]; - SrsAutoFreeA(char, buf); - + + SrsUniquePtr buf(new char[SRS_TS_PACKET_SIZE]); + // set the left bytes with 0xFF. int nb_buf = pkt->size(); srs_assert(nb_buf < SRS_TS_PACKET_SIZE); - memset(buf + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf); + memset(buf.get() + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf); - SrsBuffer stream(buf, nb_buf); + SrsBuffer stream(buf.get(), nb_buf); if ((err = pkt->encode(&stream)) != srs_success) { return srs_error_wrap(err, "ts: encode packet"); } - if ((err = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != srs_success) { + if ((err = writer->write(buf.get(), SRS_TS_PACKET_SIZE, NULL)) != srs_success) { return srs_error_wrap(err, "ts: write packet"); } } @@ -461,7 +457,7 @@ srs_error_t SrsTsContext::encode_pes(ISrsStreamWriter* writer, SrsTsMessage* msg char* p = start; while (p < end) { - SrsTsPacket* pkt = NULL; + SrsTsPacket* pkt_raw = NULL; if (p == start) { // write pcr according to message. bool write_pcr = msg->write_pcr; @@ -485,20 +481,19 @@ srs_error_t SrsTsContext::encode_pes(ISrsStreamWriter* writer, SrsTsMessage* msg int64_t pcr = write_pcr? msg->dts : -1; // TODO: FIXME: finger it why use discontinuity of msg. - pkt = SrsTsPacket::create_pes_first(this, + pkt_raw = SrsTsPacket::create_pes_first(this, pid, msg->sid, channel->continuity_counter++, msg->is_discontinuity, pcr, msg->dts, msg->pts, msg->payload->length() ); } else { - pkt = SrsTsPacket::create_pes_continue(this, pid, msg->sid, channel->continuity_counter++); + pkt_raw = SrsTsPacket::create_pes_continue(this, pid, msg->sid, channel->continuity_counter++); } - SrsAutoFree(SrsTsPacket, pkt); - + SrsUniquePtr pkt(pkt_raw); + pkt->sync_byte = sync_byte; - - char* buf = new char[SRS_TS_PACKET_SIZE]; - SrsAutoFreeA(char, buf); - + + SrsUniquePtr buf(new char[SRS_TS_PACKET_SIZE]); + // set the left bytes with 0xFF. int nb_buf = pkt->size(); srs_assert(nb_buf < SRS_TS_PACKET_SIZE); @@ -507,7 +502,7 @@ srs_error_t SrsTsContext::encode_pes(ISrsStreamWriter* writer, SrsTsMessage* msg int nb_stuffings = SRS_TS_PACKET_SIZE - nb_buf - left; if (nb_stuffings > 0) { // set all bytes to stuffings. - memset(buf, 0xFF, SRS_TS_PACKET_SIZE); + memset(buf.get(), 0xFF, SRS_TS_PACKET_SIZE); // padding with stuffings. pkt->padding(nb_stuffings); @@ -520,14 +515,14 @@ srs_error_t SrsTsContext::encode_pes(ISrsStreamWriter* writer, SrsTsMessage* msg nb_stuffings = SRS_TS_PACKET_SIZE - nb_buf - left; srs_assert(nb_stuffings == 0); } - memcpy(buf + nb_buf, p, left); + memcpy(buf.get() + nb_buf, p, left); p += left; - SrsBuffer stream(buf, nb_buf); + SrsBuffer stream(buf.get(), nb_buf); if ((err = pkt->encode(&stream)) != srs_success) { return srs_error_wrap(err, "ts: encode packet"); } - if ((err = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != srs_success) { + if ((err = writer->write(buf.get(), SRS_TS_PACKET_SIZE, NULL)) != srs_success) { return srs_error_wrap(err, "ts: write packet"); } } @@ -2773,14 +2768,13 @@ srs_error_t SrsEncFileWriter::write(void* data, size_t count, ssize_t* pnwrite) if (nb_buf == HLS_AES_ENCRYPT_BLOCK_LENGTH) { nb_buf = 0; - - char* cipher = new char[HLS_AES_ENCRYPT_BLOCK_LENGTH]; - SrsAutoFreeA(char, cipher); - + + SrsUniquePtr cipher(new char[HLS_AES_ENCRYPT_BLOCK_LENGTH]); + AES_KEY* k = (AES_KEY*)key; - AES_cbc_encrypt((unsigned char *)buf, (unsigned char *)cipher, HLS_AES_ENCRYPT_BLOCK_LENGTH, k, iv, AES_ENCRYPT); + AES_cbc_encrypt((unsigned char *)buf, (unsigned char *)cipher.get(), HLS_AES_ENCRYPT_BLOCK_LENGTH, k, iv, AES_ENCRYPT); - if ((err = SrsFileWriter::write(cipher, HLS_AES_ENCRYPT_BLOCK_LENGTH, pnwrite)) != srs_success) { + if ((err = SrsFileWriter::write(cipher.get(), HLS_AES_ENCRYPT_BLOCK_LENGTH, pnwrite)) != srs_success) { return srs_error_wrap(err, "write cipher"); } } @@ -2809,15 +2803,14 @@ void SrsEncFileWriter::close() if (nb_padding > 0) { memset(buf + nb_buf, nb_padding, nb_padding); } - - char* cipher = new char[nb_buf + nb_padding]; - SrsAutoFreeA(char, cipher); - + + SrsUniquePtr cipher(new char[nb_buf + nb_padding]); + AES_KEY* k = (AES_KEY*)key; - AES_cbc_encrypt((unsigned char *)buf, (unsigned char *)cipher, nb_buf + nb_padding, k, iv, AES_ENCRYPT); + AES_cbc_encrypt((unsigned char *)buf, (unsigned char *)cipher.get(), nb_buf + nb_padding, k, iv, AES_ENCRYPT); srs_error_t err = srs_success; - if ((err = SrsFileWriter::write(cipher, nb_buf + nb_padding, NULL)) != srs_success) { + if ((err = SrsFileWriter::write(cipher.get(), nb_buf + nb_padding, NULL)) != srs_success) { srs_warn("ignore err %s", srs_error_desc(err).c_str()); srs_error_reset(err); } diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp index 356515bdd8..71ffd6d252 100644 --- a/trunk/src/kernel/srs_kernel_utility.cpp +++ b/trunk/src/kernel/srs_kernel_utility.cpp @@ -28,6 +28,7 @@ using namespace std; #include #include #include +#include // this value must: // equals to (SRS_SYS_CYCLE_INTERVAL*SRS_SYS_TIME_RESOLUTION_MS_TIMES)*1000 diff --git a/trunk/src/main/srs_main_ingest_hls.cpp b/trunk/src/main/srs_main_ingest_hls.cpp index fc34bf2118..4c17044eb9 100644 --- a/trunk/src/main/srs_main_ingest_hls.cpp +++ b/trunk/src/main/srs_main_ingest_hls.cpp @@ -294,11 +294,10 @@ int SrsIngestHlsInput::parseTs(ISrsTsHandler* handler, char* body, int nb_body) int nb_packet = (int)nb_body / SRS_TS_PACKET_SIZE; for (int i = 0; i < nb_packet; i++) { char* p = (char*)body + (i * SRS_TS_PACKET_SIZE); - SrsBuffer* stream = new SrsBuffer(p, SRS_TS_PACKET_SIZE); - SrsAutoFree(SrsBuffer, stream); - + SrsUniquePtr stream(new SrsBuffer(p, SRS_TS_PACKET_SIZE)); + // process each ts packet - if ((err = context->decode(stream, handler)) != srs_success) { + if ((err = context->decode(stream.get(), handler)) != srs_success) { // TODO: FIXME: Use error ret = srs_error_code(err); srs_freep(err); @@ -315,10 +314,9 @@ int SrsIngestHlsInput::parseTs(ISrsTsHandler* handler, char* body, int nb_body) int SrsIngestHlsInput::parseAac(ISrsAacHandler* handler, char* body, int nb_body, double duration) { int ret = ERROR_SUCCESS; - - SrsBuffer* stream = new SrsBuffer(body, nb_body); - SrsAutoFree(SrsBuffer, stream); - + + SrsUniquePtr stream(new SrsBuffer(body, nb_body)); + // atleast 2bytes. if (!stream->require(3)) { ret = ERROR_AAC_BYTES_INVALID; @@ -384,8 +382,8 @@ int SrsIngestHlsInput::parseM3u8(SrsHttpUri* url, double& td, double& duration) return ret; } - ISrsHttpMessage* msg = NULL; - if ((err = client.get(url->get_path(), "", &msg)) != srs_success) { + ISrsHttpMessage* msg_raw = NULL; + if ((err = client.get(url->get_path(), "", &msg_raw)) != srs_success) { // TODO: FIXME: Use error ret = srs_error_code(err); srs_freep(err); @@ -393,8 +391,8 @@ int SrsIngestHlsInput::parseM3u8(SrsHttpUri* url, double& td, double& duration) return ret; } - srs_assert(msg); - SrsAutoFree(ISrsHttpMessage, msg); + srs_assert(msg_raw); + SrsUniquePtr msg(msg_raw); std::string body; if ((err = msg->body_read_all(body)) != srs_success) { @@ -619,8 +617,8 @@ int SrsIngestHlsInput::SrsTsPiece::fetch(string m3u8) return ret; } - ISrsHttpMessage* msg = NULL; - if ((err = client.get(uri.get_path(), "", &msg)) != srs_success) { + ISrsHttpMessage* msg_raw = NULL; + if ((err = client.get(uri.get_path(), "", &msg_raw)) != srs_success) { // TODO: FIXME: Use error ret = srs_error_code(err); srs_freep(err); @@ -628,8 +626,8 @@ int SrsIngestHlsInput::SrsTsPiece::fetch(string m3u8) return ret; } - srs_assert(msg); - SrsAutoFree(ISrsHttpMessage, msg); + srs_assert(msg_raw); + SrsUniquePtr msg(msg_raw); if ((err = msg->body_read_all(body)) != srs_success) { // TODO: FIXME: Use error @@ -911,9 +909,8 @@ int SrsIngestHlsOutput::parse_message_queue() while ((cpa && queue.size() > 1) || nb_videos > 1) { srs_assert(!queue.empty()); std::multimap::iterator it = queue.begin(); - - SrsTsMessage* msg = it->second; - SrsAutoFree(SrsTsMessage, msg); + + SrsUniquePtr msg(it->second); queue.erase(it); if (msg->channel->stream == SrsTsStreamVideoH264) { @@ -925,12 +922,12 @@ int SrsIngestHlsOutput::parse_message_queue() // publish audio or video. if (msg->channel->stream == SrsTsStreamVideoH264) { - if ((ret = on_ts_video(msg, &avs)) != ERROR_SUCCESS) { + if ((ret = on_ts_video(msg.get(), &avs)) != ERROR_SUCCESS) { return ret; } } if (msg->channel->stream == SrsTsStreamAudioAAC) { - if ((ret = on_ts_audio(msg, &avs)) != ERROR_SUCCESS) { + if ((ret = on_ts_audio(msg.get(), &avs)) != ERROR_SUCCESS) { return ret; } } @@ -946,9 +943,8 @@ int SrsIngestHlsOutput::flush_message_queue() // parse messages util the last video. while (!queue.empty()) { std::multimap::iterator it = queue.begin(); - - SrsTsMessage* msg = it->second; - SrsAutoFree(SrsTsMessage, msg); + + SrsUniquePtr msg(it->second); queue.erase(it); // parse the stream. @@ -956,12 +952,12 @@ int SrsIngestHlsOutput::flush_message_queue() // publish audio or video. if (msg->channel->stream == SrsTsStreamVideoH264) { - if ((ret = on_ts_video(msg, &avs)) != ERROR_SUCCESS) { + if ((ret = on_ts_video(msg.get(), &avs)) != ERROR_SUCCESS) { return ret; } } if (msg->channel->stream == SrsTsStreamAudioAAC) { - if ((ret = on_ts_audio(msg, &avs)) != ERROR_SUCCESS) { + if ((ret = on_ts_audio(msg.get(), &avs)) != ERROR_SUCCESS) { return ret; } } diff --git a/trunk/src/main/srs_main_mp4_parser.cpp b/trunk/src/main/srs_main_mp4_parser.cpp index 72641d9d50..dadf1aa764 100644 --- a/trunk/src/main/srs_main_mp4_parser.cpp +++ b/trunk/src/main/srs_main_mp4_parser.cpp @@ -54,30 +54,28 @@ srs_error_t parse(std::string mp4_file, bool verbose) return srs_error_wrap(err, "open box reader"); } srs_trace("MP4 box reader open success"); - - SrsSimpleStream* stream = new SrsSimpleStream(); - SrsAutoFree(SrsSimpleStream, stream); - + + SrsUniquePtr stream(new SrsSimpleStream()); + fprintf(stderr, "\n%s\n", mp4_file.c_str()); while (true) { SrsMp4Box* box = NULL; + // Note that we should use SrsAutoFree to free the ptr which is set later. SrsAutoFree(SrsMp4Box, box); - if ((err = br.read(stream, &box)) != srs_success) { + if ((err = br.read(stream.get(), &box)) != srs_success) { if (srs_error_code(err) == ERROR_SYSTEM_FILE_EOF) { fprintf(stderr, "\n"); } return srs_error_wrap(err, "read box"); } - - SrsBuffer* buffer = new SrsBuffer(stream->bytes(), stream->length()); - SrsAutoFree(SrsBuffer, buffer); - - if ((err = box->decode(buffer)) != srs_success) { + + SrsUniquePtr buffer(new SrsBuffer(stream->bytes(), stream->length())); + if ((err = box->decode(buffer.get())) != srs_success) { return srs_error_wrap(err, "decode box"); } - if ((err = br.skip(box, stream)) != srs_success) { + if ((err = br.skip(box, stream.get())) != srs_success) { return srs_error_wrap(err, "skip box"); } diff --git a/trunk/src/protocol/srs_protocol_http_client.cpp b/trunk/src/protocol/srs_protocol_http_client.cpp index eff241e9d9..633288c4b9 100644 --- a/trunk/src/protocol/srs_protocol_http_client.cpp +++ b/trunk/src/protocol/srs_protocol_http_client.cpp @@ -201,19 +201,18 @@ srs_error_t SrsSslClient::read(void* plaintext, size_t nn_plaintext, ssize_t* nr if (r0 == -1 && r1 == SSL_ERROR_WANT_READ) { // TODO: Can we avoid copy? int nn_cipher = nn_plaintext; - char* cipher = new char[nn_cipher]; - SrsAutoFreeA(char, cipher); + SrsUniquePtr cipher(new char[nn_cipher]); // Read the cipher from SSL. ssize_t nn = 0; - if ((err = transport->read(cipher, nn_cipher, &nn)) != srs_success) { + if ((err = transport->read(cipher.get(), nn_cipher, &nn)) != srs_success) { return srs_error_wrap(err, "https: read"); } - int r0 = BIO_write(bio_in, cipher, nn); + int r0 = BIO_write(bio_in, cipher.get(), nn); if (r0 <= 0) { // TODO: 0 or -1 maybe block, use BIO_should_retry to check. - return srs_error_new(ERROR_HTTPS_READ, "BIO_write r0=%d, cipher=%p, size=%d", r0, cipher, nn); + return srs_error_new(ERROR_HTTPS_READ, "BIO_write r0=%d, cipher=%p, size=%d", r0, cipher.get(), nn); } continue; } diff --git a/trunk/src/protocol/srs_protocol_http_stack.cpp b/trunk/src/protocol/srs_protocol_http_stack.cpp index 198b755b68..b280fca387 100644 --- a/trunk/src/protocol/srs_protocol_http_stack.cpp +++ b/trunk/src/protocol/srs_protocol_http_stack.cpp @@ -421,8 +421,7 @@ srs_error_t SrsHttpFileServer::serve_file(ISrsHttpResponseWriter* w, ISrsHttpMes { srs_error_t err = srs_success; - SrsFileReader* fs = fs_factory->create_file_reader(); - SrsAutoFree(SrsFileReader, fs); + SrsUniquePtr fs(fs_factory->create_file_reader()); if ((err = fs->open(fullpath)) != srs_success) { return srs_error_wrap(err, "open file %s", fullpath.c_str()); @@ -484,7 +483,7 @@ srs_error_t SrsHttpFileServer::serve_file(ISrsHttpResponseWriter* w, ISrsHttpMes // write body. int64_t left = length; - if ((err = copy(w, fs, r, left)) != srs_success) { + if ((err = copy(w, fs.get(), r, left)) != srs_success) { return srs_error_wrap(err, "copy file=%s size=%" PRId64, fullpath.c_str(), left); } @@ -595,18 +594,17 @@ srs_error_t SrsHttpFileServer::copy(ISrsHttpResponseWriter* w, SrsFileReader* fs srs_error_t err = srs_success; int64_t left = size; - char* buf = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE]; - SrsAutoFreeA(char, buf); - + SrsUniquePtr buf(new char[SRS_HTTP_TS_SEND_BUFFER_SIZE]); + while (left > 0) { ssize_t nread = -1; int max_read = srs_min(left, SRS_HTTP_TS_SEND_BUFFER_SIZE); - if ((err = fs->read(buf, max_read, &nread)) != srs_success) { + if ((err = fs->read(buf.get(), max_read, &nread)) != srs_success) { return srs_error_wrap(err, "read limit=%d, left=%" PRId64, max_read, left); } left -= nread; - if ((err = w->write(buf, (int)nread)) != srs_success) { + if ((err = w->write(buf.get(), (int)nread)) != srs_success) { return srs_error_wrap(err, "write limit=%d, bytes=%d, left=%" PRId64, max_read, (int)nread, left); } } diff --git a/trunk/src/protocol/srs_protocol_raw_avc.cpp b/trunk/src/protocol/srs_protocol_raw_avc.cpp index 2e1ea9f77b..e25a87f753 100644 --- a/trunk/src/protocol/srs_protocol_raw_avc.cpp +++ b/trunk/src/protocol/srs_protocol_raw_avc.cpp @@ -125,11 +125,10 @@ srs_error_t SrsRawH264Stream::mux_sequence_header(string sps, string pps, string // Nbytes of pps: // pictureParameterSetNALUnit int nb_packet = 5 + (3 + (int)sps.length()) + (3 + (int)pps.length()); - char* packet = new char[nb_packet]; - SrsAutoFreeA(char, packet); - + SrsUniquePtr packet(new char[nb_packet]); + // use stream to generate the h264 packet. - SrsBuffer stream(packet, nb_packet); + SrsBuffer stream(packet.get(), nb_packet); // decode the SPS: // @see: 7.3.2.1.1, ISO_IEC_14496-10-AVC-2012.pdf, page 62 @@ -186,7 +185,7 @@ srs_error_t SrsRawH264Stream::mux_sequence_header(string sps, string pps, string // 5.3.4.2.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 16 // profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 144 - sh = string(packet, nb_packet); + sh = string(packet.get(), nb_packet); return err; } @@ -200,11 +199,10 @@ srs_error_t SrsRawH264Stream::mux_ipb_frame(char* frame, int nb_frame, string& i // Nbytes of nalu. // NALUnit int nb_packet = 4 + nb_frame; - char* packet = new char[nb_packet]; - SrsAutoFreeA(char, packet); - + SrsUniquePtr packet(new char[nb_packet]); + // use stream to generate the h264 packet. - SrsBuffer stream(packet, nb_packet); + SrsBuffer stream(packet.get(), nb_packet); // 5.3.4.2.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 16 // lengthSizeMinusOne, or NAL_unit_length, always use 4bytes size @@ -217,7 +215,7 @@ srs_error_t SrsRawH264Stream::mux_ipb_frame(char* frame, int nb_frame, string& i // NALUnit stream.write_bytes(frame, nb_frame); - ibp = string(packet, nb_packet); + ibp = string(packet.get(), nb_packet); return err; } @@ -406,11 +404,10 @@ srs_error_t SrsRawHEVCStream::mux_sequence_header(std::string vps, std::string s } int nb_packet = 23 + 5 + (int)vps.length() + 5 + (int)sps.length() + 5 + pps_size - 2; - char* packet = new char[nb_packet]; - SrsAutoFreeA(char, packet); + SrsUniquePtr packet(new char[nb_packet]); // use stream to generate the hevc packet. - SrsBuffer stream(packet, nb_packet); + SrsBuffer stream(packet.get(), nb_packet); SrsFormat format; if ((err = format.initialize()) != srs_success) { @@ -511,7 +508,7 @@ srs_error_t SrsRawHEVCStream::mux_sequence_header(std::string vps, std::string s } } - hvcC = string(packet, nb_packet); + hvcC = string(packet.get(), nb_packet); return err; } @@ -525,11 +522,10 @@ srs_error_t SrsRawHEVCStream::mux_ipb_frame(char *frame, int nb_frame, std::stri // Nbytes of nalu. // NALUnit int nb_packet = 4 + nb_frame; - char *packet = new char[nb_packet]; - SrsAutoFreeA(char, packet); + SrsUniquePtr packet(new char[nb_packet]); // use stream to generate the h265 packet. - SrsBuffer stream(packet, nb_packet); + SrsBuffer stream(packet.get(), nb_packet); // 5.3.4.2.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 16 // lengthSizeMinusOne, or NAL_unit_length, always use 4bytes size @@ -542,7 +538,7 @@ srs_error_t SrsRawHEVCStream::mux_ipb_frame(char *frame, int nb_frame, std::stri // NALUnit stream.write_bytes(frame, nb_frame); - ibp = string(packet, nb_packet); + ibp = string(packet.get(), nb_packet); return err; } diff --git a/trunk/src/protocol/srs_protocol_rtc_stun.cpp b/trunk/src/protocol/srs_protocol_rtc_stun.cpp index e52e7002c6..493836acb6 100644 --- a/trunk/src/protocol/srs_protocol_rtc_stun.cpp +++ b/trunk/src/protocol/srs_protocol_rtc_stun.cpp @@ -177,8 +177,7 @@ srs_error_t SrsStunPacket::decode(const char* buf, const int nb_buf) { srs_error_t err = srs_success; - SrsBuffer* stream = new SrsBuffer(const_cast(buf), nb_buf); - SrsAutoFree(SrsBuffer, stream); + SrsUniquePtr stream(new SrsBuffer(const_cast(buf), nb_buf)); if (stream->left() < 20) { return srs_error_new(ERROR_RTC_STUN, "invalid stun packet, size=%d", stream->size()); @@ -306,8 +305,7 @@ srs_error_t SrsStunPacket::encode_binding_response(const string& pwd, SrsBuffer* string SrsStunPacket::encode_username() { char buf[1460]; - SrsBuffer* stream = new SrsBuffer(buf, sizeof(buf)); - SrsAutoFree(SrsBuffer, stream); + SrsUniquePtr stream(new SrsBuffer(buf, sizeof(buf))); string username = remote_ufrag + ":" + local_ufrag; @@ -326,8 +324,7 @@ string SrsStunPacket::encode_username() string SrsStunPacket::encode_mapped_address() { char buf[1460]; - SrsBuffer* stream = new SrsBuffer(buf, sizeof(buf)); - SrsAutoFree(SrsBuffer, stream); + SrsUniquePtr stream(new SrsBuffer(buf, sizeof(buf))); stream->write_2bytes(XorMappedAddress); stream->write_2bytes(8); @@ -342,8 +339,7 @@ string SrsStunPacket::encode_mapped_address() string SrsStunPacket::encode_hmac(char* hmac_buf, const int hmac_buf_len) { char buf[1460]; - SrsBuffer* stream = new SrsBuffer(buf, sizeof(buf)); - SrsAutoFree(SrsBuffer, stream); + SrsUniquePtr stream(new SrsBuffer(buf, sizeof(buf))); stream->write_2bytes(MessageIntegrity); stream->write_2bytes(hmac_buf_len); @@ -355,8 +351,7 @@ string SrsStunPacket::encode_hmac(char* hmac_buf, const int hmac_buf_len) string SrsStunPacket::encode_fingerprint(uint32_t crc32) { char buf[1460]; - SrsBuffer* stream = new SrsBuffer(buf, sizeof(buf)); - SrsAutoFree(SrsBuffer, stream); + SrsUniquePtr stream(new SrsBuffer(buf, sizeof(buf))); stream->write_2bytes(Fingerprint); stream->write_2bytes(4); diff --git a/trunk/src/protocol/srs_protocol_rtmp_handshake.cpp b/trunk/src/protocol/srs_protocol_rtmp_handshake.cpp index 0e8c9f097e..868b50f6ce 100644 --- a/trunk/src/protocol/srs_protocol_rtmp_handshake.cpp +++ b/trunk/src/protocol/srs_protocol_rtmp_handshake.cpp @@ -522,16 +522,16 @@ namespace srs_internal srs_error_t err = srs_success; // generate digest - char* c1_digest = NULL; + char* c1_digest_raw = NULL; - if ((err = calc_c1_digest(owner, c1_digest)) != srs_success) { + if ((err = calc_c1_digest(owner, c1_digest_raw)) != srs_success) { return srs_error_wrap(err, "sign c1"); } - srs_assert(c1_digest != NULL); - SrsAutoFreeA(char, c1_digest); - - memcpy(digest.digest, c1_digest, 32); + srs_assert(c1_digest_raw != NULL); + SrsUniquePtr c1_digest(c1_digest_raw); + + memcpy(digest.digest, c1_digest.get(), 32); return err; } @@ -540,16 +540,16 @@ namespace srs_internal { srs_error_t err = srs_success; - char* c1_digest = NULL; + char* c1_digest_raw = NULL; - if ((err = calc_c1_digest(owner, c1_digest)) != srs_success) { + if ((err = calc_c1_digest(owner, c1_digest_raw)) != srs_success) { return srs_error_wrap(err, "validate c1"); } - srs_assert(c1_digest != NULL); - SrsAutoFreeA(char, c1_digest); + srs_assert(c1_digest_raw != NULL); + SrsUniquePtr c1_digest(c1_digest_raw); - is_valid = srs_bytes_equals(digest.digest, c1_digest, 32); + is_valid = srs_bytes_equals(digest.digest, c1_digest.get(), 32); return err; } @@ -576,15 +576,15 @@ namespace srs_internal // TODO: FIXME: use the actual key size. //srs_assert(pkey_size == 128); - char* s1_digest = NULL; - if ((err = calc_s1_digest(owner, s1_digest)) != srs_success) { + char* s1_digest_raw = NULL; + if ((err = calc_s1_digest(owner, s1_digest_raw)) != srs_success) { return srs_error_wrap(err, "calc s1 digest"); } - srs_assert(s1_digest != NULL); - SrsAutoFreeA(char, s1_digest); - - memcpy(digest.digest, s1_digest, 32); + srs_assert(s1_digest_raw != NULL); + SrsUniquePtr s1_digest(s1_digest_raw); + + memcpy(digest.digest, s1_digest.get(), 32); return err; } @@ -593,16 +593,16 @@ namespace srs_internal { srs_error_t err = srs_success; - char* s1_digest = NULL; + char* s1_digest_raw = NULL; - if ((err = calc_s1_digest(owner, s1_digest)) != srs_success) { + if ((err = calc_s1_digest(owner, s1_digest_raw)) != srs_success) { return srs_error_wrap(err, "validate s1"); } - srs_assert(s1_digest != NULL); - SrsAutoFreeA(char, s1_digest); + srs_assert(s1_digest_raw != NULL); + SrsUniquePtr s1_digest(s1_digest_raw); - is_valid = srs_bytes_equals(digest.digest, s1_digest, 32); + is_valid = srs_bytes_equals(digest.digest, s1_digest.get(), 32); return err; } @@ -611,21 +611,18 @@ namespace srs_internal { srs_error_t err = srs_success; - /** - * c1s1 is splited by digest: - * c1s1-part1: n bytes (time, version, key and digest-part1). - * digest-data: 32bytes - * c1s1-part2: (1536-n-32)bytes (digest-part2) - * @return a new allocated bytes, user must free it. - */ - char* c1s1_joined_bytes = new char[1536 -32]; - SrsAutoFreeA(char, c1s1_joined_bytes); - if ((err = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != srs_success) { + // c1s1 is splited by digest: + // c1s1-part1: n bytes (time, version, key and digest-part1). + // digest-data: 32bytes + // c1s1-part2: (1536-n-32)bytes (digest-part2) + // @return a new allocated bytes, user must free it. + SrsUniquePtr c1s1_joined_bytes(new char[1536 -32]); + if ((err = copy_to(owner, c1s1_joined_bytes.get(), 1536 - 32, false)) != srs_success) { return srs_error_wrap(err, "copy bytes"); } c1_digest = new char[SRS_OpensslHashSize]; - if ((err = openssl_HMACsha256(SrsGenuineFPKey, 30, c1s1_joined_bytes, 1536 - 32, c1_digest)) != srs_success) { + if ((err = openssl_HMACsha256(SrsGenuineFPKey, 30, c1s1_joined_bytes.get(), 1536 - 32, c1_digest)) != srs_success) { srs_freepa(c1_digest); return srs_error_wrap(err, "calc c1 digest"); } @@ -637,21 +634,18 @@ namespace srs_internal { srs_error_t err = srs_success; - /** - * c1s1 is splited by digest: - * c1s1-part1: n bytes (time, version, key and digest-part1). - * digest-data: 32bytes - * c1s1-part2: (1536-n-32)bytes (digest-part2) - * @return a new allocated bytes, user must free it. - */ - char* c1s1_joined_bytes = new char[1536 -32]; - SrsAutoFreeA(char, c1s1_joined_bytes); - if ((err = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != srs_success) { + // c1s1 is splited by digest: + // c1s1-part1: n bytes (time, version, key and digest-part1). + // digest-data: 32bytes + // c1s1-part2: (1536-n-32)bytes (digest-part2) + // @return a new allocated bytes, user must free it. + SrsUniquePtr c1s1_joined_bytes(new char[1536 -32]); + if ((err = copy_to(owner, c1s1_joined_bytes.get(), 1536 - 32, false)) != srs_success) { return srs_error_wrap(err, "copy bytes"); } s1_digest = new char[SRS_OpensslHashSize]; - if ((err = openssl_HMACsha256(SrsGenuineFMSKey, 36, c1s1_joined_bytes, 1536 - 32, s1_digest)) != srs_success) { + if ((err = openssl_HMACsha256(SrsGenuineFMSKey, 36, c1s1_joined_bytes.get(), 1536 - 32, s1_digest)) != srs_success) { srs_freepa(s1_digest); return srs_error_wrap(err, "calc s1 digest"); } diff --git a/trunk/src/protocol/srs_protocol_rtmp_stack.cpp b/trunk/src/protocol/srs_protocol_rtmp_stack.cpp index 4a250114a5..7b1a20395e 100644 --- a/trunk/src/protocol/srs_protocol_rtmp_stack.cpp +++ b/trunk/src/protocol/srs_protocol_rtmp_stack.cpp @@ -128,11 +128,9 @@ srs_error_t SrsPacket::encode(int& psize, char*& ppayload) if (size > 0) { payload = new char[size]; - - SrsBuffer* stream = new SrsBuffer(payload, size); - SrsAutoFree(SrsBuffer, stream); - - if ((err = encode_packet(stream)) != srs_success) { + SrsUniquePtr stream(new SrsBuffer(payload, size)); + + if ((err = encode_packet(stream.get())) != srs_success) { srs_freepa(payload); return srs_error_wrap(err, "encode packet"); } @@ -556,22 +554,20 @@ srs_error_t SrsProtocol::do_iovs_send(iovec* iovs, int size) return srs_write_large_iovs(skt, iovs, size); } -srs_error_t SrsProtocol::do_send_and_free_packet(SrsPacket* packet, int stream_id) +srs_error_t SrsProtocol::do_send_and_free_packet(SrsPacket* packet_raw, int stream_id) { srs_error_t err = srs_success; - srs_assert(packet); - SrsAutoFree(SrsPacket, packet); - - SrsCommonMessage* msg = new SrsCommonMessage(); - SrsAutoFree(SrsCommonMessage, msg); + srs_assert(packet_raw); + SrsUniquePtr packet(packet_raw); + SrsUniquePtr msg(new SrsCommonMessage()); - if ((err = packet->to_msg(msg, stream_id)) != srs_success) { + if ((err = packet->to_msg(msg.get(), stream_id)) != srs_success) { return srs_error_wrap(err, "to message"); } SrsSharedPtrMessage* shared_msg = new SrsSharedPtrMessage(); - if ((err = shared_msg->create(msg)) != srs_success) { + if ((err = shared_msg->create(msg.get())) != srs_success) { srs_freep(shared_msg); return srs_error_wrap(err, "create message"); } @@ -580,7 +576,7 @@ srs_error_t SrsProtocol::do_send_and_free_packet(SrsPacket* packet, int stream_i return srs_error_wrap(err, "send packet"); } - if ((err = on_send_packet(&msg->header, packet)) != srs_success) { + if ((err = on_send_packet(&msg->header, packet.get())) != srs_success) { return srs_error_wrap(err, "on send packet"); } @@ -1237,12 +1233,12 @@ srs_error_t SrsProtocol::on_recv_message(SrsCommonMessage* msg) return srs_error_wrap(err, "response ack"); } - SrsPacket* packet = NULL; + SrsPacket* packet_raw = NULL; switch (msg->header.message_type) { case RTMP_MSG_SetChunkSize: case RTMP_MSG_UserControlMessage: case RTMP_MSG_WindowAcknowledgementSize: - if ((err = decode_message(msg, &packet)) != srs_success) { + if ((err = decode_message(msg, &packet_raw)) != srs_success) { return srs_error_wrap(err, "decode message"); } break; @@ -1252,15 +1248,14 @@ srs_error_t SrsProtocol::on_recv_message(SrsCommonMessage* msg) default: return err; } - - srs_assert(packet); - + // always free the packet. - SrsAutoFree(SrsPacket, packet); - + srs_assert(packet_raw); + SrsUniquePtr packet(packet_raw); + switch (msg->header.message_type) { case RTMP_MSG_WindowAcknowledgementSize: { - SrsSetWindowAckSizePacket* pkt = dynamic_cast(packet); + SrsSetWindowAckSizePacket* pkt = dynamic_cast(packet.get()); srs_assert(pkt != NULL); if (pkt->ackowledgement_window_size > 0) { @@ -1272,7 +1267,7 @@ srs_error_t SrsProtocol::on_recv_message(SrsCommonMessage* msg) break; } case RTMP_MSG_SetChunkSize: { - SrsSetChunkSizePacket* pkt = dynamic_cast(packet); + SrsSetChunkSizePacket* pkt = dynamic_cast(packet.get()); srs_assert(pkt != NULL); // for some server, the actual chunk size can greater than the max value(65536), @@ -1292,7 +1287,7 @@ srs_error_t SrsProtocol::on_recv_message(SrsCommonMessage* msg) break; } case RTMP_MSG_UserControlMessage: { - SrsUserControlPacket* pkt = dynamic_cast(packet); + SrsUserControlPacket* pkt = dynamic_cast(packet.get()); srs_assert(pkt != NULL); if (pkt->event_type == SrcPCUCSetBufferLength) { @@ -1822,9 +1817,8 @@ srs_error_t SrsRtmpClient::handshake() // maybe st has problem when alloc object on stack, always alloc object at heap. // @see https://github.com/ossrs/srs/issues/509 - SrsComplexHandshake* complex_hs = new SrsComplexHandshake(); - SrsAutoFree(SrsComplexHandshake, complex_hs); - + SrsUniquePtr complex_hs(new SrsComplexHandshake()); + if ((err = complex_hs->handshake_with_server(hs_bytes, io)) != srs_success) { // As client, we never verify s0s1s2, because some server doesn't follow the RTMP spec. // So we never have chance to use simple handshake. @@ -1922,14 +1916,15 @@ srs_error_t SrsRtmpClient::connect_app(string app, string tcUrl, SrsRequest* r, } // expect connect _result - SrsCommonMessage* msg = NULL; - SrsConnectAppResPacket* pkt = NULL; - if ((err = expect_message(&msg, &pkt)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + SrsConnectAppResPacket* pkt_raw = NULL; + if ((err = expect_message(&msg_raw, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "expect connect app response"); } - SrsAutoFree(SrsCommonMessage, msg); - SrsAutoFree(SrsConnectAppResPacket, pkt); - + + SrsUniquePtr msg(msg_raw); + SrsUniquePtr pkt(pkt_raw); + // server info SrsAmf0Any* data = pkt->info->get_property("data"); if (si && data && data->is_ecma_array()) { @@ -1989,13 +1984,14 @@ srs_error_t SrsRtmpClient::create_stream(int& stream_id) // CreateStream _result. if (true) { - SrsCommonMessage* msg = NULL; - SrsCreateStreamResPacket* pkt = NULL; - if ((err = expect_message(&msg, &pkt)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + SrsCreateStreamResPacket* pkt_raw = NULL; + if ((err = expect_message(&msg_raw, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "expect create stream response"); } - SrsAutoFree(SrsCommonMessage, msg); - SrsAutoFree(SrsCreateStreamResPacket, pkt); + + SrsUniquePtr msg(msg_raw); + SrsUniquePtr pkt(pkt_raw); stream_id = (int)pkt->stream_id; } @@ -2100,13 +2096,14 @@ srs_error_t SrsRtmpClient::fmle_publish(string stream, int& stream_id) // expect result of CreateStream if (true) { - SrsCommonMessage* msg = NULL; - SrsCreateStreamResPacket* pkt = NULL; - if ((err = expect_message(&msg, &pkt)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + SrsCreateStreamResPacket* pkt_raw = NULL; + if ((err = expect_message(&msg_raw, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "expect create stream response message failed"); } - SrsAutoFree(SrsCommonMessage, msg); - SrsAutoFree(SrsCreateStreamResPacket, pkt); + + SrsUniquePtr msg(msg_raw); + SrsUniquePtr pkt(pkt_raw); stream_id = (int)pkt->stream_id; } @@ -2242,14 +2239,15 @@ srs_error_t SrsRtmpServer::connect_app(SrsRequest* req) { srs_error_t err = srs_success; - SrsCommonMessage* msg = NULL; - SrsConnectAppPacket* pkt = NULL; - if ((err = expect_message(&msg, &pkt)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + SrsConnectAppPacket* pkt_raw = NULL; + if ((err = expect_message(&msg_raw, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "expect connect app response"); } - SrsAutoFree(SrsCommonMessage, msg); - SrsAutoFree(SrsConnectAppPacket, pkt); - + + SrsUniquePtr msg(msg_raw); + SrsUniquePtr pkt(pkt_raw); + SrsAmf0Any* prop = NULL; if ((prop = pkt->command_object->ensure_property_string("tcUrl")) == NULL) { @@ -2383,16 +2381,17 @@ srs_error_t SrsRtmpServer::redirect(SrsRequest* r, string url, bool& accepted) // or we never know whether the client is ok to redirect. protocol->set_recv_timeout(SRS_RTMP_REDIRECT_TIMEOUT); if (true) { - SrsCommonMessage* msg = NULL; - SrsCallPacket* pkt = NULL; - if ((err = expect_message(&msg, &pkt)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + SrsCallPacket* pkt_raw = NULL; + if ((err = expect_message(&msg_raw, &pkt_raw)) != srs_success) { srs_freep(err); // ignore any error of redirect response. return srs_success; } - SrsAutoFree(SrsCommonMessage, msg); - SrsAutoFree(SrsCallPacket, pkt); - + + SrsUniquePtr msg(msg_raw); + SrsUniquePtr pkt(pkt_raw); + string message; if (pkt->arguments && pkt->arguments->is_string()) { message = pkt->arguments->to_str(); @@ -2438,12 +2437,12 @@ srs_error_t SrsRtmpServer::identify_client(int stream_id, SrsRtmpConnType& type, srs_error_t err = srs_success; while (true) { - SrsCommonMessage* msg = NULL; - if ((err = protocol->recv_message(&msg)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + if ((err = protocol->recv_message(&msg_raw)) != srs_success) { return srs_error_wrap(err, "recv identify message"); } - - SrsAutoFree(SrsCommonMessage, msg); + + SrsUniquePtr msg(msg_raw); SrsMessageHeader& h = msg->header; if (h.is_ackledgement() || h.is_set_chunk_size() || h.is_window_ackledgement_size() || h.is_user_control_message()) { @@ -2455,27 +2454,26 @@ srs_error_t SrsRtmpServer::identify_client(int stream_id, SrsRtmpConnType& type, continue; } - SrsPacket* pkt = NULL; - if ((err = protocol->decode_message(msg, &pkt)) != srs_success) { + SrsPacket* pkt_raw = NULL; + if ((err = protocol->decode_message(msg.get(), &pkt_raw)) != srs_success) { return srs_error_wrap(err, "decode identify"); } + SrsUniquePtr pkt(pkt_raw); - SrsAutoFree(SrsPacket, pkt); - - if (dynamic_cast(pkt)) { - return identify_create_stream_client(dynamic_cast(pkt), stream_id, 3, type, stream_name, duration); + if (dynamic_cast(pkt.get())) { + return identify_create_stream_client(dynamic_cast(pkt.get()), stream_id, 3, type, stream_name, duration); } - if (dynamic_cast(pkt)) { - return identify_fmle_publish_client(dynamic_cast(pkt), type, stream_name); + if (dynamic_cast(pkt.get())) { + return identify_fmle_publish_client(dynamic_cast(pkt.get()), type, stream_name); } - if (dynamic_cast(pkt)) { - return identify_play_client(dynamic_cast(pkt), type, stream_name, duration); + if (dynamic_cast(pkt.get())) { + return identify_play_client(dynamic_cast(pkt.get()), type, stream_name, duration); } // call msg, // support response null first, // TODO: FIXME: response in right way, or forward in edge mode. - SrsCallPacket* call = dynamic_cast(pkt); + SrsCallPacket* call = dynamic_cast(pkt.get()); if (call) { SrsCallResPacket* res = new SrsCallResPacket(call->transaction_id); res->command_object = SrsAmf0Any::null(); @@ -2646,14 +2644,14 @@ srs_error_t SrsRtmpServer::start_fmle_publish(int stream_id) // FCPublish double fc_publish_tid = 0; if (true) { - SrsCommonMessage* msg = NULL; - SrsFMLEStartPacket* pkt = NULL; - if ((err = expect_message(&msg, &pkt)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + SrsFMLEStartPacket* pkt_raw = NULL; + if ((err = expect_message(&msg_raw, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "recv FCPublish"); } - - SrsAutoFree(SrsCommonMessage, msg); - SrsAutoFree(SrsFMLEStartPacket, pkt); + + SrsUniquePtr msg(msg_raw); + SrsUniquePtr pkt(pkt_raw); fc_publish_tid = pkt->transaction_id; } @@ -2668,15 +2666,15 @@ srs_error_t SrsRtmpServer::start_fmle_publish(int stream_id) // createStream double create_stream_tid = 0; if (true) { - SrsCommonMessage* msg = NULL; - SrsCreateStreamPacket* pkt = NULL; - if ((err = expect_message(&msg, &pkt)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + SrsCreateStreamPacket* pkt_raw = NULL; + if ((err = expect_message(&msg_raw, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "recv createStream"); } - - SrsAutoFree(SrsCommonMessage, msg); - SrsAutoFree(SrsCreateStreamPacket, pkt); - + + SrsUniquePtr msg(msg_raw); + SrsUniquePtr pkt(pkt_raw); + create_stream_tid = pkt->transaction_id; } // createStream response @@ -2689,14 +2687,14 @@ srs_error_t SrsRtmpServer::start_fmle_publish(int stream_id) // publish if (true) { - SrsCommonMessage* msg = NULL; - SrsPublishPacket* pkt = NULL; - if ((err = expect_message(&msg, &pkt)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + SrsPublishPacket* pkt_raw = NULL; + if ((err = expect_message(&msg_raw, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "recv publish"); } - - SrsAutoFree(SrsCommonMessage, msg); - SrsAutoFree(SrsPublishPacket, pkt); + + SrsUniquePtr msg(msg_raw); + SrsUniquePtr pkt(pkt_raw); } // publish response onFCPublish(NetStream.Publish.Start) if (true) { @@ -2720,14 +2718,14 @@ srs_error_t SrsRtmpServer::start_haivision_publish(int stream_id) // publish if (true) { - SrsCommonMessage* msg = NULL; - SrsPublishPacket* pkt = NULL; - if ((err = expect_message(&msg, &pkt)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + SrsPublishPacket* pkt_raw = NULL; + if ((err = expect_message(&msg_raw, &pkt_raw)) != srs_success) { return srs_error_wrap(err, "recv publish"); } - - SrsAutoFree(SrsCommonMessage, msg); - SrsAutoFree(SrsPublishPacket, pkt); + + SrsUniquePtr msg(msg_raw); + SrsUniquePtr pkt(pkt_raw); } // publish response onFCPublish(NetStream.Publish.Start) @@ -2829,12 +2827,12 @@ srs_error_t SrsRtmpServer::identify_create_stream_client(SrsCreateStreamPacket* } while (true) { - SrsCommonMessage* msg = NULL; - if ((err = protocol->recv_message(&msg)) != srs_success) { + SrsCommonMessage* msg_raw = NULL; + if ((err = protocol->recv_message(&msg_raw)) != srs_success) { return srs_error_wrap(err, "recv identify"); } - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg(msg_raw); SrsMessageHeader& h = msg->header; if (h.is_ackledgement() || h.is_set_chunk_size() || h.is_window_ackledgement_size() || h.is_user_control_message()) { @@ -2846,24 +2844,23 @@ srs_error_t SrsRtmpServer::identify_create_stream_client(SrsCreateStreamPacket* continue; } - SrsPacket* pkt = NULL; - if ((err = protocol->decode_message(msg, &pkt)) != srs_success) { + SrsPacket* pkt_raw = NULL; + if ((err = protocol->decode_message(msg.get(), &pkt_raw)) != srs_success) { return srs_error_wrap(err, "decode identify"); } + SrsUniquePtr pkt(pkt_raw); - SrsAutoFree(SrsPacket, pkt); - - if (dynamic_cast(pkt)) { - return identify_play_client(dynamic_cast(pkt), type, stream_name, duration); + if (dynamic_cast(pkt.get())) { + return identify_play_client(dynamic_cast(pkt.get()), type, stream_name, duration); } - if (dynamic_cast(pkt)) { - return identify_flash_publish_client(dynamic_cast(pkt), type, stream_name); + if (dynamic_cast(pkt.get())) { + return identify_flash_publish_client(dynamic_cast(pkt.get()), type, stream_name); } - if (dynamic_cast(pkt)) { - return identify_create_stream_client(dynamic_cast(pkt), stream_id, depth-1, type, stream_name, duration); + if (dynamic_cast(pkt.get())) { + return identify_create_stream_client(dynamic_cast(pkt.get()), stream_id, depth-1, type, stream_name, duration); } - if (dynamic_cast(pkt)) { - return identify_haivision_publish_client(dynamic_cast(pkt), type, stream_name); + if (dynamic_cast(pkt.get())) { + return identify_haivision_publish_client(dynamic_cast(pkt.get()), type, stream_name); } srs_trace("ignore AMF0/AMF3 command message."); @@ -3873,13 +3870,13 @@ srs_error_t SrsPlayPacket::decode(SrsBuffer* stream) return err; } - SrsAmf0Any* reset_value = NULL; - if ((err = srs_amf0_read_any(stream, &reset_value)) != srs_success) { + SrsAmf0Any* reset_value_raw = NULL; + if ((err = srs_amf0_read_any(stream, &reset_value_raw)) != srs_success) { return srs_error_wrap(err, "reset"); } - SrsAutoFree(SrsAmf0Any, reset_value); + SrsUniquePtr reset_value(reset_value_raw); - if (reset_value) { + if (reset_value.get()) { // check if the value is bool or number // An optional Boolean value or number that specifies whether // to flush any previous playlist @@ -4274,20 +4271,19 @@ srs_error_t SrsOnMetaDataPacket::decode(SrsBuffer* stream) } // the metadata maybe object or ecma array - SrsAmf0Any* any = NULL; - if ((err = srs_amf0_read_any(stream, &any)) != srs_success) { + SrsAmf0Any* any_raw = NULL; + if ((err = srs_amf0_read_any(stream, &any_raw)) != srs_success) { return srs_error_wrap(err, "metadata"); } - srs_assert(any); - if (any->is_object()) { + srs_assert(any_raw); + if (any_raw->is_object()) { srs_freep(metadata); - metadata = any->to_object(); + metadata = any_raw->to_object(); return err; } - SrsAutoFree(SrsAmf0Any, any); - + SrsUniquePtr any(any_raw); if (any->is_ecma_array()) { SrsAmf0EcmaArray* arr = any->to_ecma_array(); diff --git a/trunk/src/protocol/srs_protocol_srt.cpp b/trunk/src/protocol/srs_protocol_srt.cpp index efad018e85..3f494dddf9 100644 --- a/trunk/src/protocol/srs_protocol_srt.cpp +++ b/trunk/src/protocol/srs_protocol_srt.cpp @@ -13,6 +13,7 @@ using namespace std; #include #include #include +#include #include diff --git a/trunk/src/protocol/srs_protocol_st.cpp b/trunk/src/protocol/srs_protocol_st.cpp index e6127691f6..f15f7252c8 100644 --- a/trunk/src/protocol/srs_protocol_st.cpp +++ b/trunk/src/protocol/srs_protocol_st.cpp @@ -17,6 +17,7 @@ using namespace std; #include #include #include +#include // nginx also set to 512 #define SERVER_LISTEN_BACKLOG 512 diff --git a/trunk/src/protocol/srs_protocol_utility.cpp b/trunk/src/protocol/srs_protocol_utility.cpp index 237538d134..749ea3342f 100644 --- a/trunk/src/protocol/srs_protocol_utility.cpp +++ b/trunk/src/protocol/srs_protocol_utility.cpp @@ -920,13 +920,12 @@ srs_error_t srs_ioutil_read_all(ISrsReader* in, std::string& content) srs_error_t err = srs_success; // Cache to read, it might cause coroutine switch, so we use local cache here. - char* buf = new char[SRS_HTTP_READ_CACHE_BYTES]; - SrsAutoFreeA(char, buf); + SrsUniquePtr buf(new char[SRS_HTTP_READ_CACHE_BYTES]); // Whatever, read util EOF. while (true) { ssize_t nb_read = 0; - if ((err = in->read(buf, SRS_HTTP_READ_CACHE_BYTES, &nb_read)) != srs_success) { + if ((err = in->read(buf.get(), SRS_HTTP_READ_CACHE_BYTES, &nb_read)) != srs_success) { int code = srs_error_code(err); if (code == ERROR_SYSTEM_FILE_EOF || code == ERROR_HTTP_RESPONSE_EOF || code == ERROR_HTTP_REQUEST_EOF || code == ERROR_HTTP_STREAM_EOF @@ -938,7 +937,7 @@ srs_error_t srs_ioutil_read_all(ISrsReader* in, std::string& content) } if (nb_read > 0) { - content.append(buf, nb_read); + content.append(buf.get(), nb_read); } } diff --git a/trunk/src/utest/srs_utest_amf0.cpp b/trunk/src/utest/srs_utest_amf0.cpp index e8c446f4f5..fdf3ccd342 100644 --- a/trunk/src/utest/srs_utest_amf0.cpp +++ b/trunk/src/utest/srs_utest_amf0.cpp @@ -43,13 +43,15 @@ VOID TEST(ProtocolAMF0Test, ScenarioMain) // version: string // srs_sig: string SrsAmf0Object* props = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, props); + SrsUniquePtr props_uptr(props); + props->set("fmsVer", SrsAmf0Any::str("FMS/3,5,3,888")); props->set("capabilities", SrsAmf0Any::number(253)); props->set("mode", SrsAmf0Any::number(123)); SrsAmf0Object* info = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, info); + SrsUniquePtr info_uptr(info); + info->set("level", SrsAmf0Any::str("info")); info->set("code", SrsAmf0Any::str("NetStream.Connnect.Success")); info->set("descrption", SrsAmf0Any::str("connected")); @@ -75,7 +77,7 @@ VOID TEST(ProtocolAMF0Test, ScenarioMain) EXPECT_EQ(0x03, bytes[0]); EXPECT_EQ(0x09, bytes[nb_bytes - 1]); } - SrsAutoFreeA(char, bytes); + SrsUniquePtr bytes_uptr(bytes); // decoding amf0 object from bytes // when user know the schema @@ -89,12 +91,12 @@ VOID TEST(ProtocolAMF0Test, ScenarioMain) // if user know the schema, for instance, it's an amf0 object, // user can use specified object to decoding. SrsAmf0Object* props = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, props); + SrsUniquePtr props_uptr(props); EXPECT_EQ(srs_success, props->read(&s)); // user can use specified object to decoding. SrsAmf0Object* info = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, info); + SrsUniquePtr info_uptr(info); EXPECT_EQ(srs_success, info->read(&s)); // use the decoded data. @@ -138,8 +140,8 @@ VOID TEST(ProtocolAMF0Test, ScenarioMain) // decoding a amf0 any, for user donot know SrsAmf0Any* any = NULL; EXPECT_EQ(srs_success, srs_amf0_read_any(&s, &any)); - SrsAutoFree(SrsAmf0Any, any); - + SrsUniquePtr any_uptr(any); + // for amf0 object if (any->is_object()) { SrsAmf0Object* obj = any->to_object(); @@ -183,15 +185,15 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+3; SrsAmf0Object* o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); - + SrsUniquePtr o_uptr(o); + EXPECT_EQ(size, SrsAmf0Size::object(o)); } // object: elem if (true) { int size = 1+3; SrsAmf0Object* o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("name")+SrsAmf0Size::str("winlin"); o->set("name", SrsAmf0Any::str("winlin")); @@ -201,7 +203,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+3; SrsAmf0Object* o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("age")+SrsAmf0Size::number(); o->set("age", SrsAmf0Any::number(9)); @@ -211,7 +213,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+3; SrsAmf0Object* o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("email")+SrsAmf0Size::null(); o->set("email", SrsAmf0Any::null()); @@ -221,7 +223,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+3; SrsAmf0Object* o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("email")+SrsAmf0Size::undefined(); o->set("email", SrsAmf0Any::undefined()); @@ -231,7 +233,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+3; SrsAmf0Object* o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("sex")+SrsAmf0Size::boolean(); o->set("sex", SrsAmf0Any::boolean(true)); @@ -243,15 +245,15 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+4+3; SrsAmf0EcmaArray* o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); - + SrsUniquePtr o_uptr(o); + EXPECT_EQ(size, SrsAmf0Size::ecma_array(o)); } // array: elem if (true) { int size = 1+4+3; SrsAmf0EcmaArray* o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("name")+SrsAmf0Size::str("winlin"); o->set("name", SrsAmf0Any::str("winlin")); @@ -261,7 +263,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+4+3; SrsAmf0EcmaArray* o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("age")+SrsAmf0Size::number(); o->set("age", SrsAmf0Any::number(9)); @@ -271,7 +273,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+4+3; SrsAmf0EcmaArray* o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("email")+SrsAmf0Size::null(); o->set("email", SrsAmf0Any::null()); @@ -281,7 +283,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+4+3; SrsAmf0EcmaArray* o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("email")+SrsAmf0Size::undefined(); o->set("email", SrsAmf0Any::undefined()); @@ -291,7 +293,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+4+3; SrsAmf0EcmaArray* o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("sex")+SrsAmf0Size::boolean(); o->set("sex", SrsAmf0Any::boolean(true)); @@ -303,7 +305,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+3; SrsAmf0Object* o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("name")+SrsAmf0Size::str("winlin"); o->set("name", SrsAmf0Any::str("winlin")); @@ -318,7 +320,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+3; SrsAmf0Object* o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("name")+SrsAmf0Size::str("winlin"); o->set("name", SrsAmf0Any::str("winlin")); @@ -340,7 +342,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+4+3; SrsAmf0EcmaArray* o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("name")+SrsAmf0Size::str("winlin"); o->set("name", SrsAmf0Any::str("winlin")); @@ -355,7 +357,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+4+3; SrsAmf0EcmaArray* o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("name")+SrsAmf0Size::str("winlin"); o->set("name", SrsAmf0Any::str("winlin")); @@ -377,7 +379,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+3; SrsAmf0Object* o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("name")+SrsAmf0Size::str("winlin"); o->set("name", SrsAmf0Any::str("winlin")); @@ -399,7 +401,7 @@ VOID TEST(ProtocolAMF0Test, ApiSize) if (true) { int size = 1+4+3; SrsAmf0EcmaArray* o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); size += SrsAmf0Size::utf8("name")+SrsAmf0Size::str("winlin"); o->set("name", SrsAmf0Any::str("winlin")); @@ -428,14 +430,14 @@ VOID TEST(ProtocolAMF0Test, ApiAnyElem) // string if (true) { o = SrsAmf0Any::str(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); ASSERT_TRUE(NULL != o); EXPECT_TRUE(o->is_string()); EXPECT_STREQ("", o->to_str().c_str()); } if (true) { o = SrsAmf0Any::str("winlin"); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); ASSERT_TRUE(NULL != o); EXPECT_TRUE(o->is_string()); EXPECT_STREQ("winlin", o->to_str().c_str()); @@ -444,21 +446,21 @@ VOID TEST(ProtocolAMF0Test, ApiAnyElem) // bool if (true) { o = SrsAmf0Any::boolean(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); ASSERT_TRUE(NULL != o); EXPECT_TRUE(o->is_boolean()); EXPECT_FALSE(o->to_boolean()); } if (true) { o = SrsAmf0Any::boolean(false); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); ASSERT_TRUE(NULL != o); EXPECT_TRUE(o->is_boolean()); EXPECT_FALSE(o->to_boolean()); } if (true) { o = SrsAmf0Any::boolean(true); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); ASSERT_TRUE(NULL != o); EXPECT_TRUE(o->is_boolean()); EXPECT_TRUE(o->to_boolean()); @@ -467,21 +469,21 @@ VOID TEST(ProtocolAMF0Test, ApiAnyElem) // number if (true) { o = SrsAmf0Any::number(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); ASSERT_TRUE(NULL != o); EXPECT_TRUE(o->is_number()); EXPECT_DOUBLE_EQ(0, o->to_number()); } if (true) { o = SrsAmf0Any::number(100); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); ASSERT_TRUE(NULL != o); EXPECT_TRUE(o->is_number()); EXPECT_DOUBLE_EQ(100, o->to_number()); } if (true) { o = SrsAmf0Any::number(-100); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); ASSERT_TRUE(NULL != o); EXPECT_TRUE(o->is_number()); EXPECT_DOUBLE_EQ(-100, o->to_number()); @@ -490,7 +492,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyElem) // null if (true) { o = SrsAmf0Any::null(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); ASSERT_TRUE(NULL != o); EXPECT_TRUE(o->is_null()); } @@ -498,7 +500,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyElem) // undefined if (true) { o = SrsAmf0Any::undefined(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); ASSERT_TRUE(NULL != o); EXPECT_TRUE(o->is_undefined()); } @@ -523,7 +525,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) (s.data() + s.pos())[2] = 0x09; o = SrsAmf0Any::object_eof(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); HELPER_EXPECT_SUCCESS(o->read(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -538,7 +540,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::object_eof(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -553,7 +555,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::str("winlin"); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -576,7 +578,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::number(10); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -595,7 +597,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::boolean(true); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -612,7 +614,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::boolean(false); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -631,7 +633,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::null(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -650,7 +652,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::undefined(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -669,7 +671,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::str("winlin"); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -679,7 +681,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) SrsAmf0Any* po = NULL; EXPECT_EQ(srs_success, srs_amf0_read_any(&s, &po)); ASSERT_TRUE(NULL != po); - SrsAutoFree(SrsAmf0Any, po); + SrsUniquePtr po_uptr(po); ASSERT_TRUE(po->is_string()); EXPECT_STREQ("winlin", po->to_str().c_str()); } @@ -689,7 +691,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::number(10); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -699,7 +701,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) SrsAmf0Any* po = NULL; EXPECT_EQ(srs_success, srs_amf0_read_any(&s, &po)); ASSERT_TRUE(NULL != po); - SrsAutoFree(SrsAmf0Any, po); + SrsUniquePtr po_uptr(po); ASSERT_TRUE(po->is_number()); EXPECT_DOUBLE_EQ(10, po->to_number()); } @@ -709,7 +711,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::boolean(true); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -719,7 +721,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) SrsAmf0Any* po = NULL; EXPECT_EQ(srs_success, srs_amf0_read_any(&s, &po)); ASSERT_TRUE(NULL != po); - SrsAutoFree(SrsAmf0Any, po); + SrsUniquePtr po_uptr(po); ASSERT_TRUE(po->is_boolean()); EXPECT_TRUE(po->to_boolean()); } @@ -729,7 +731,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::null(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -739,7 +741,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) SrsAmf0Any* po = NULL; EXPECT_EQ(srs_success, srs_amf0_read_any(&s, &po)); ASSERT_TRUE(NULL != po); - SrsAutoFree(SrsAmf0Any, po); + SrsUniquePtr po_uptr(po); ASSERT_TRUE(po->is_null()); } @@ -748,7 +750,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) s.skip(-1 * s.pos()); o = SrsAmf0Any::undefined(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(o->total_size(), s.pos()); @@ -758,7 +760,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyIO) SrsAmf0Any* po = NULL; EXPECT_EQ(srs_success, srs_amf0_read_any(&s, &po)); ASSERT_TRUE(NULL != po); - SrsAutoFree(SrsAmf0Any, po); + SrsUniquePtr po_uptr(po); ASSERT_TRUE(po->is_undefined()); } @@ -843,49 +845,49 @@ VOID TEST(ProtocolAMF0Test, ApiAnyTypeAssert) // any convert if (true) { o = SrsAmf0Any::str(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(o->is_string()); } if (true) { o = SrsAmf0Any::number(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(o->is_number()); } if (true) { o = SrsAmf0Any::boolean(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(o->is_boolean()); } if (true) { o = SrsAmf0Any::null(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(o->is_null()); } if (true) { o = SrsAmf0Any::undefined(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(o->is_undefined()); } if (true) { o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(o->is_object()); } if (true) { o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(o->is_ecma_array()); } if (true) { o = SrsAmf0Any::strict_array(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(o->is_strict_array()); } // empty object if (true) { o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); s.skip(-1 * s.pos()); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(1+3, s.pos()); @@ -894,7 +896,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyTypeAssert) // empty ecma array if (true) { o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); s.skip(-1 * s.pos()); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(1+4+3, s.pos()); @@ -903,7 +905,7 @@ VOID TEST(ProtocolAMF0Test, ApiAnyTypeAssert) // strict array if (true) { o = SrsAmf0Any::strict_array(); - SrsAutoFree(SrsAmf0Any, o); + SrsUniquePtr o_uptr(o); s.skip(-1 * s.pos()); EXPECT_EQ(srs_success, o->write(&s)); EXPECT_EQ(1+4, s.pos()); @@ -920,7 +922,7 @@ VOID TEST(ProtocolAMF0Test, ApiObjectProps) // get/set property if (true) { o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(NULL == o->get_property("name")); @@ -936,7 +938,7 @@ VOID TEST(ProtocolAMF0Test, ApiObjectProps) // index property if (true) { o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); o->set("name", SrsAmf0Any::str("winlin")); EXPECT_STREQ("name", o->key_at(0).c_str()); @@ -956,7 +958,7 @@ VOID TEST(ProtocolAMF0Test, ApiObjectProps) // ensure property if (true) { o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(NULL == o->ensure_property_string("name")); EXPECT_TRUE(NULL == o->ensure_property_number("age")); @@ -976,7 +978,7 @@ VOID TEST(ProtocolAMF0Test, ApiObjectProps) // count if (true) { o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(0, o->count()); @@ -1001,7 +1003,7 @@ VOID TEST(ProtocolAMF0Test, ApiEcmaArrayProps) // get/set property if (true) { o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(NULL == o->get_property("name")); @@ -1017,7 +1019,7 @@ VOID TEST(ProtocolAMF0Test, ApiEcmaArrayProps) // index property if (true) { o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); o->set("name", SrsAmf0Any::str("winlin")); EXPECT_STREQ("name", o->key_at(0).c_str()); @@ -1037,7 +1039,7 @@ VOID TEST(ProtocolAMF0Test, ApiEcmaArrayProps) // ensure property if (true) { o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); EXPECT_TRUE(NULL == o->ensure_property_string("name")); EXPECT_TRUE(NULL == o->ensure_property_number("age")); @@ -1057,7 +1059,7 @@ VOID TEST(ProtocolAMF0Test, ApiEcmaArrayProps) // count if (true) { o = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0EcmaArray, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(0, o->count()); @@ -1086,8 +1088,8 @@ VOID TEST(ProtocolAMF0Test, ApiStrictArray) // append property if (true) { o = SrsAmf0Any::strict_array(); - SrsAutoFree(SrsAmf0StrictArray, o); - + SrsUniquePtr o_uptr(o); + o->append(SrsAmf0Any::number(100)); EXPECT_DOUBLE_EQ(100, o->at(0)->to_number()); @@ -1101,7 +1103,7 @@ VOID TEST(ProtocolAMF0Test, ApiStrictArray) // count if (true) { o = SrsAmf0Any::strict_array(); - SrsAutoFree(SrsAmf0StrictArray, o); + SrsUniquePtr o_uptr(o); EXPECT_EQ(0, o->count()); @@ -1118,7 +1120,7 @@ VOID TEST(ProtocolAMF0Test, ApiStrictArray) // io if (true) { o = SrsAmf0Any::strict_array(); - SrsAutoFree(SrsAmf0StrictArray, o); + SrsUniquePtr o_uptr(o); s.skip(-1 * s.pos()); EXPECT_EQ(srs_success, o->write(&s)); @@ -1131,8 +1133,8 @@ VOID TEST(ProtocolAMF0Test, ApiStrictArray) if (true) { o = SrsAmf0Any::strict_array(); - SrsAutoFree(SrsAmf0StrictArray, o); - + SrsUniquePtr o_uptr(o); + o->append(SrsAmf0Any::number(0)); s.skip(-1 * s.pos()); @@ -1147,7 +1149,7 @@ VOID TEST(ProtocolAMF0Test, ApiStrictArray) VOID TEST(ProtocolAMF0Test, ObjectObjectObject) { SrsAmf0Any* obj = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Any, obj); + SrsUniquePtr o_uptr(obj); EXPECT_EQ(0, obj->to_object()->count()); SrsAmf0Any* child1 = SrsAmf0Any::object(); @@ -1175,7 +1177,7 @@ VOID TEST(ProtocolAMF0Test, ObjectObjectObject) VOID TEST(ProtocolAMF0Test, EcmaEcmaEcma) { SrsAmf0Any* arr = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0Any, arr); + SrsUniquePtr o_uptr(arr); EXPECT_EQ(0, arr->to_ecma_array()->count()); SrsAmf0Any* arr1 = SrsAmf0Any::ecma_array(); @@ -1203,7 +1205,7 @@ VOID TEST(ProtocolAMF0Test, EcmaEcmaEcma) VOID TEST(ProtocolAMF0Test, StrictStrictStrict) { SrsAmf0Any* arr = SrsAmf0Any::strict_array(); - SrsAutoFree(SrsAmf0Any, arr); + SrsUniquePtr o_uptr(arr); EXPECT_EQ(0, arr->to_strict_array()->count()); SrsAmf0Any* arr1 = SrsAmf0Any::strict_array(); @@ -1232,7 +1234,7 @@ VOID TEST(ProtocolAMF0Test, StrictStrictStrict) VOID TEST(ProtocolAMF0Test, ObjectEcmaStrict) { SrsAmf0Any* obj = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Any, obj); + SrsUniquePtr o_uptr(obj); EXPECT_EQ(0, obj->to_object()->count()); SrsAmf0Any* arr1 = SrsAmf0Any::ecma_array(); @@ -1260,7 +1262,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesString) if (true) { SrsAmf0Any* p = SrsAmf0Any::str("hello"); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr o_uptr(p); EXPECT_TRUE(p->is_string()); EXPECT_FALSE(p->is_boolean()); @@ -1287,22 +1289,23 @@ VOID TEST(ProtocolAMF0Test, InterfacesString) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); b.skip(-1 * b.pos()); SrsAmf0Any* pp = NULL; - SrsAutoFree(SrsAmf0Any, pp); HELPER_EXPECT_SUCCESS(SrsAmf0Any::discovery(&b, &pp)); + SrsUniquePtr oo_uptr(pp); b.skip(-1 * b.pos()); HELPER_EXPECT_SUCCESS(pp->read(&b)); EXPECT_TRUE(string("hello") == pp->to_str()); // For copy. - SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); + SrsAmf0Any* cp = p->copy(); EXPECT_TRUE(string("hello") == cp->to_str()); + SrsUniquePtr cp_uptr(cp); } if (true) { @@ -1390,7 +1393,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesBoolean) if (true) { SrsAmf0Any* p = SrsAmf0Any::boolean(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr o_uptr(p); EXPECT_FALSE(p->is_string()); EXPECT_TRUE(p->is_boolean()); @@ -1416,22 +1419,23 @@ VOID TEST(ProtocolAMF0Test, InterfacesBoolean) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); b.skip(-1 * b.pos()); SrsAmf0Any* pp = NULL; - SrsAutoFree(SrsAmf0Any, pp); HELPER_EXPECT_SUCCESS(SrsAmf0Any::discovery(&b, &pp)); + SrsUniquePtr pp_uptr(pp); b.skip(-1 * b.pos()); HELPER_EXPECT_SUCCESS(pp->read(&b)); EXPECT_FALSE(p->to_boolean()); // For copy. - SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); + SrsAmf0Any* cp = p->copy(); EXPECT_FALSE(cp->to_boolean()); + SrsUniquePtr cp_uptr(cp); } if (true) { @@ -1479,7 +1483,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesNumber) if (true) { SrsAmf0Any* p = SrsAmf0Any::number(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr o_uptr(p); EXPECT_FALSE(p->is_string()); EXPECT_FALSE(p->is_boolean()); @@ -1513,22 +1517,23 @@ VOID TEST(ProtocolAMF0Test, InterfacesNumber) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); b.skip(-1 * b.pos()); SrsAmf0Any* pp = NULL; - SrsAutoFree(SrsAmf0Any, pp); HELPER_EXPECT_SUCCESS(SrsAmf0Any::discovery(&b, &pp)); + SrsUniquePtr pp_uptr(pp); b.skip(-1 * b.pos()); HELPER_EXPECT_SUCCESS(pp->read(&b)); EXPECT_TRUE(100.1 == p->to_number()); // For copy. - SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); + SrsAmf0Any* cp = p->copy(); EXPECT_TRUE(100.1 == cp->to_number()); + SrsUniquePtr cp_uptr(cp); } if (true) { @@ -1576,7 +1581,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesDate) if (true) { SrsAmf0Any* p = SrsAmf0Any::date(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr o_uptr(p); EXPECT_FALSE(p->is_string()); EXPECT_FALSE(p->is_boolean()); @@ -1598,14 +1603,14 @@ VOID TEST(ProtocolAMF0Test, InterfacesDate) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); b.skip(-1 * b.pos()); SrsAmf0Any* pp = NULL; - SrsAutoFree(SrsAmf0Any, pp); HELPER_EXPECT_SUCCESS(SrsAmf0Any::discovery(&b, &pp)); + SrsUniquePtr pp_uptr(pp); b.skip(-1 * b.pos()); HELPER_EXPECT_SUCCESS(pp->read(&b)); @@ -1618,7 +1623,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesNull) if (true) { SrsAmf0Any* p = SrsAmf0Any::null(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr o_uptr(p); EXPECT_FALSE(p->is_string()); EXPECT_FALSE(p->is_boolean()); @@ -1642,14 +1647,14 @@ VOID TEST(ProtocolAMF0Test, InterfacesNull) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); b.skip(-1 * b.pos()); SrsAmf0Any* pp = NULL; - SrsAutoFree(SrsAmf0Any, pp); HELPER_EXPECT_SUCCESS(SrsAmf0Any::discovery(&b, &pp)); + SrsUniquePtr pp_uptr(pp); b.skip(-1 * b.pos()); HELPER_EXPECT_SUCCESS(pp->read(&b)); @@ -1684,7 +1689,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesUndefined) if (true) { SrsAmf0Any* p = SrsAmf0Any::undefined(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); EXPECT_FALSE(p->is_string()); EXPECT_FALSE(p->is_boolean()); @@ -1708,14 +1713,14 @@ VOID TEST(ProtocolAMF0Test, InterfacesUndefined) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); b.skip(-1 * b.pos()); SrsAmf0Any* pp = NULL; - SrsAutoFree(SrsAmf0Any, pp); HELPER_EXPECT_SUCCESS(SrsAmf0Any::discovery(&b, &pp)); + SrsUniquePtr pp_uptr(pp); b.skip(-1 * b.pos()); HELPER_EXPECT_SUCCESS(pp->read(&b)); @@ -1750,7 +1755,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesObject) if (true) { SrsAmf0Any* p = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); EXPECT_FALSE(p->is_string()); EXPECT_FALSE(p->is_boolean()); @@ -1775,14 +1780,14 @@ VOID TEST(ProtocolAMF0Test, InterfacesObject) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); b.skip(-1 * b.pos()); SrsAmf0Any* pp = NULL; - SrsAutoFree(SrsAmf0Any, pp); HELPER_EXPECT_SUCCESS(SrsAmf0Any::discovery(&b, &pp)); + SrsUniquePtr pp_uptr(pp); b.skip(-1 * b.pos()); HELPER_EXPECT_SUCCESS(pp->read(&b)); @@ -1790,8 +1795,8 @@ VOID TEST(ProtocolAMF0Test, InterfacesObject) // For copy. SrsAmf0Any* cp = p->copy(); - SrsAutoFree(SrsAmf0Any, cp); EXPECT_TRUE(NULL != cp->to_object()); + SrsUniquePtr cp_uptr(cp); } if (true) { @@ -1864,7 +1869,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesObject2) { if (true) { SrsAmf0Any* p = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); SrsAmf0Object* o = p->to_object(); EXPECT_TRUE(NULL != o); @@ -1897,7 +1902,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesObject2) if (true) { SrsAmf0Any* p = SrsAmf0Any::strict_array(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); SrsAmf0StrictArray* o = p->to_strict_array(); EXPECT_TRUE(NULL != o); @@ -1922,7 +1927,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesObject2) if (true) { SrsAmf0Any* p = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); SrsAmf0EcmaArray* o = p->to_ecma_array(); EXPECT_TRUE(NULL != o); @@ -1960,7 +1965,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesObjectEOF) if (true) { SrsAmf0Any* p = SrsAmf0Any::object_eof(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); EXPECT_FALSE(p->is_string()); EXPECT_FALSE(p->is_boolean()); @@ -1980,21 +1985,22 @@ VOID TEST(ProtocolAMF0Test, InterfacesObjectEOF) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); b.skip(-1 * b.pos()); SrsAmf0Any* pp = NULL; - SrsAutoFree(SrsAmf0Any, pp); HELPER_EXPECT_SUCCESS(SrsAmf0Any::discovery(&b, &pp)); + SrsUniquePtr pp_uptr(pp); b.skip(-1 * b.pos()); HELPER_EXPECT_SUCCESS(pp->read(&b)); // For copy. - SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); + SrsAmf0Any* cp = p->copy(); EXPECT_TRUE(cp->is_object_eof()); + SrsUniquePtr cp_uptr(cp); } if (true) { @@ -2050,7 +2056,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesEcmaArray) if (true) { SrsAmf0Any* p = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); EXPECT_FALSE(p->is_string()); EXPECT_FALSE(p->is_boolean()); @@ -2071,22 +2077,23 @@ VOID TEST(ProtocolAMF0Test, InterfacesEcmaArray) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); b.skip(-1 * b.pos()); SrsAmf0Any* pp = NULL; - SrsAutoFree(SrsAmf0Any, pp); HELPER_EXPECT_SUCCESS(SrsAmf0Any::discovery(&b, &pp)); + SrsUniquePtr pp_uptr(pp); b.skip(-1 * b.pos()); HELPER_EXPECT_SUCCESS(pp->read(&b)); EXPECT_TRUE(NULL != pp->to_ecma_array()); // For copy. - SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); + SrsAmf0Any* cp = p->copy(); EXPECT_TRUE(NULL != cp->to_ecma_array()); + SrsUniquePtr cp_uptr(cp); } if (true) { @@ -2177,7 +2184,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesStrictArray) if (true) { SrsAmf0Any* p = SrsAmf0Any::strict_array(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); EXPECT_FALSE(p->is_string()); EXPECT_FALSE(p->is_boolean()); @@ -2198,22 +2205,23 @@ VOID TEST(ProtocolAMF0Test, InterfacesStrictArray) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); b.skip(-1 * b.pos()); SrsAmf0Any* pp = NULL; - SrsAutoFree(SrsAmf0Any, pp); HELPER_EXPECT_SUCCESS(SrsAmf0Any::discovery(&b, &pp)); + SrsUniquePtr pp_uptr(pp); b.skip(-1 * b.pos()); HELPER_EXPECT_SUCCESS(pp->read(&b)); EXPECT_TRUE(NULL != pp->to_strict_array()); // For copy. - SrsAmf0Any* cp = p->copy(); SrsAutoFree(SrsAmf0Any, cp); + SrsAmf0Any* cp = p->copy(); EXPECT_TRUE(NULL != cp->to_strict_array()); + SrsUniquePtr cp_uptr(cp); } if (true) { @@ -2302,25 +2310,25 @@ VOID TEST(ProtocolAMF0Test, InterfacesOthers) { if (true) { SrsAmf0Any* p = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); EXPECT_FALSE(!p->is_complex_object()); } if (true) { SrsAmf0Any* p = SrsAmf0Any::object_eof(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); EXPECT_FALSE(!p->is_complex_object()); } if (true) { SrsAmf0Any* p = SrsAmf0Any::ecma_array(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); EXPECT_FALSE(!p->is_complex_object()); } if (true) { SrsAmf0Any* p = SrsAmf0Any::strict_array(); - SrsAutoFree(SrsAmf0Any, p); + SrsUniquePtr p_uptr(p); EXPECT_FALSE(!p->is_complex_object()); } } @@ -2342,7 +2350,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesError) if (true) { SrsAmf0Object* o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); o->set("name", SrsAmf0Any::number(3.0)); o->set("name", SrsAmf0Any::str("srs")); @@ -2361,7 +2369,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesError) if (true) { SrsAmf0Object* o = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, o); + SrsUniquePtr o_uptr(o); o->set("name", SrsAmf0Any::str("srs")); o->set("name", SrsAmf0Any::number(3.0)); @@ -2380,13 +2388,13 @@ VOID TEST(ProtocolAMF0Test, InterfacesError) if (true) { SrsAmf0Object* src = SrsAmf0Any::object(); - SrsAutoFree(SrsAmf0Object, src); + SrsUniquePtr src_uptr(src); src->set("name", SrsAmf0Any::str("srs")); src->set("name", SrsAmf0Any::number(3.0)); SrsAmf0Any* cp = src->copy(); - SrsAutoFree(SrsAmf0Any, cp); + SrsUniquePtr cp_uptr(cp); SrsAmf0Object* o = cp->to_object(); @@ -2753,7 +2761,7 @@ VOID TEST(ProtocolJSONTest, Parse) VOID TEST(ProtocolJSONTest, ObjectAPI) { SrsJsonObject* p = SrsJsonAny::object(); - SrsAutoFree(SrsJsonObject, p); + SrsUniquePtr p_uptr(p); p->set("id", SrsJsonAny::integer(3)); p->set("name", SrsJsonAny::str("srs")); @@ -2812,7 +2820,7 @@ VOID TEST(ProtocolJSONTest, ObjectAPI) VOID TEST(ProtocolJSONTest, ArrayAPI) { SrsJsonArray* p = SrsJsonAny::array(); - SrsAutoFree(SrsJsonArray, p); + SrsUniquePtr p_uptr(p); p->add(SrsJsonAny::integer(2019)); p->add(SrsJsonAny::str("srs")); diff --git a/trunk/src/utest/srs_utest_core.cpp b/trunk/src/utest/srs_utest_core.cpp index 155621be0b..8d2e54ba6e 100644 --- a/trunk/src/utest/srs_utest_core.cpp +++ b/trunk/src/utest/srs_utest_core.cpp @@ -10,6 +10,7 @@ using namespace std; #include #include #include +#include VOID TEST(CoreAutoFreeTest, Free) { @@ -88,7 +89,7 @@ VOID TEST(CoreLogger, CheckVsnprintf) } } -VOID TEST(CoreLogger, SharedPtrTypical) +VOID TEST(CoreSmartPtr, SharedPtrTypical) { if (true) { SrsSharedPtr p(new int(100)); @@ -118,7 +119,7 @@ VOID TEST(CoreLogger, SharedPtrTypical) } } -VOID TEST(CoreLogger, SharedPtrReset) +VOID TEST(CoreSmartPtr, SharedPtrReset) { if (true) { SrsSharedPtr p(new int(100)); @@ -143,21 +144,21 @@ SrsSharedPtr mock_create_from_ptr(SrsSharedPtr p) { return p; } -VOID TEST(CoreLogger, SharedPtrContructor) +VOID TEST(CoreSmartPtr, SharedPtrContructor) { int* p = new int(100); SrsSharedPtr q = mock_create_from_ptr(p); EXPECT_EQ(100, *q); } -VOID TEST(CoreLogger, SharedPtrObject) +VOID TEST(CoreSmartPtr, SharedPtrObject) { SrsSharedPtr p(new MyNormalObject(100)); EXPECT_TRUE(p); EXPECT_EQ(100, p->id()); } -VOID TEST(CoreLogger, SharedPtrNullptr) +VOID TEST(CoreSmartPtr, SharedPtrNullptr) { SrsSharedPtr p(NULL); EXPECT_FALSE(p); @@ -176,17 +177,17 @@ class MockWrapper public: MockWrapper(int* p) { ptr = p; - *ptr = *ptr + 1; + if (ptr) *ptr = *ptr + 1; } ~MockWrapper() { - *ptr = *ptr - 1; + if (ptr) *ptr = *ptr - 1; } }; -VOID TEST(CoreLogger, SharedPtrWrapper) +VOID TEST(CoreSmartPtr, SharedPtrWrapper) { int* ptr = new int(100); - SrsAutoFree(int, ptr); + SrsUniquePtr ptr_uptr(ptr); EXPECT_EQ(100, *ptr); if (true) { @@ -222,7 +223,7 @@ VOID TEST(CoreLogger, SharedPtrWrapper) EXPECT_EQ(100, *ptr); } -VOID TEST(CoreLogger, SharedPtrAssign) +VOID TEST(CoreSmartPtr, SharedPtrAssign) { if (true) { SrsSharedPtr p(new int(100)); @@ -242,11 +243,11 @@ VOID TEST(CoreLogger, SharedPtrAssign) } int* ptr0 = new int(100); - SrsAutoFree(int, ptr0); + SrsUniquePtr ptr0_uptr(ptr0); EXPECT_EQ(100, *ptr0); int* ptr1 = new int(200); - SrsAutoFree(int, ptr1); + SrsUniquePtr ptr1_uptr(ptr1); EXPECT_EQ(200, *ptr1); if (true) { @@ -280,7 +281,7 @@ SrsSharedPtr mock_shared_ptr_move_ctr(SrsSharedPtr p) { return p; } -VOID TEST(CoreLogger, SharedPtrMove) +VOID TEST(CoreSmartPtr, SharedPtrMove) { if (true) { SrsSharedPtr p(new int(100)); @@ -297,7 +298,7 @@ VOID TEST(CoreLogger, SharedPtrMove) } int* ptr = new int(100); - SrsAutoFree(int, ptr); + SrsUniquePtr ptr_uptr(ptr); EXPECT_EQ(100, *ptr); if (true) { @@ -358,7 +359,7 @@ class MockIntResource : public ISrsResource } }; -VOID TEST(CoreLogger, SharedResourceTypical) +VOID TEST(CoreSmartPtr, SharedResourceTypical) { if (true) { SrsSharedResource* p = new SrsSharedResource(new MockIntResource(100)); @@ -420,7 +421,7 @@ SrsSharedResource mock_shared_resource_move_ctr(SrsSharedResource p) { return p; } -VOID TEST(CoreLogger, SharedResourceMove) +VOID TEST(CoreSmartPtr, SharedResourceMove) { if (true) { SrsSharedResource p(new MockIntResource(100)); @@ -439,3 +440,52 @@ VOID TEST(CoreLogger, SharedResourceMove) } } +VOID TEST(CoreSmartPtr, UniquePtrNormal) +{ + if (true) { + SrsUniquePtr p(new int(100)); + EXPECT_EQ(100, *p.get()); + } + + int* ptr = new int(100); + SrsUniquePtr ptr_uptr(ptr); + EXPECT_EQ(100, *ptr); + + if (true) { + SrsUniquePtr p(new MockWrapper(ptr)); + EXPECT_EQ(101, *ptr); + EXPECT_EQ(101, *p->ptr); + + SrsUniquePtr p0(new MockWrapper(ptr)); + EXPECT_EQ(102, *ptr); + EXPECT_EQ(102, *p0->ptr); + } + EXPECT_EQ(100, *ptr); +} + +VOID TEST(CoreSmartPtr, UniquePtrArray) +{ + if (true) { + int* ptr = new int[100]; + ptr[0] = 100; + + SrsUniquePtr p(ptr); + EXPECT_EQ(100, *p.get()); + } + + int* ptr = new int(100); + SrsUniquePtr ptr_uptr(ptr); + EXPECT_EQ(100, *ptr); + + if (true) { + SrsUniquePtr p(new MockWrapper[1]{MockWrapper(ptr)}); + EXPECT_EQ(101, *ptr); + EXPECT_EQ(101, *(p[0].ptr)); + + SrsUniquePtr p0(new MockWrapper[1]{MockWrapper(ptr)}); + EXPECT_EQ(102, *ptr); + EXPECT_EQ(102, *(p0[0].ptr)); + } + EXPECT_EQ(100, *ptr); +} + diff --git a/trunk/src/utest/srs_utest_gb28181.cpp b/trunk/src/utest/srs_utest_gb28181.cpp index 5961f10f61..7f953afe5a 100644 --- a/trunk/src/utest/srs_utest_gb28181.cpp +++ b/trunk/src/utest/srs_utest_gb28181.cpp @@ -95,8 +95,8 @@ VOID TEST(ProtocolGbSipTest, SipViaBranchMagic) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_BOTH)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); SrsSipMessage smsg; HELPER_ASSERT_FAILED(smsg.parse(msg)); @@ -127,8 +127,9 @@ VOID TEST(ProtocolGbSipTest, SipRegisterRequest) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_REQUEST)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_REGISTER, msg->method()); EXPECT_STREQ("/sip:registrar.biloxi.com", msg->path().c_str()); EXPECT_STREQ("SIP/2.0/UDP bobspc.biloxi.com:5060;branch=z9hG4bKnashds7", msg->header()->get("Via").c_str()); @@ -178,8 +179,9 @@ VOID TEST(ProtocolGbSipTest, SipRegisterRequest) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_REQUEST)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_REQUEST, (http_parser_type)msg->message_type()); EXPECT_EQ(HTTP_REGISTER, msg->method()); EXPECT_STREQ("/sip:registrar.biloxi.com", msg->path().c_str()); @@ -193,8 +195,9 @@ VOID TEST(ProtocolGbSipTest, SipRegisterRequest) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_BOTH)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_REQUEST, (http_parser_type)msg->message_type()); EXPECT_EQ(HTTP_REGISTER, msg->method()); EXPECT_STREQ("/sip:registrar.biloxi.com", msg->path().c_str()); @@ -225,8 +228,9 @@ VOID TEST(ProtocolGbSipTest, SipRegisterResponse) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_RESPONSE)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_RESPONSE, (http_parser_type) msg->message_type()); EXPECT_EQ(200, msg->status_code()); EXPECT_STREQ("SIP/2.0/UDP bobspc.biloxi.com:5060;branch=z9hG4bKnashds7;received=192.0.2.4", @@ -273,8 +277,9 @@ VOID TEST(ProtocolGbSipTest, SipRegisterResponse) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_RESPONSE)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_RESPONSE, (http_parser_type)msg->message_type()); EXPECT_EQ(200, msg->status_code()); } @@ -287,8 +292,9 @@ VOID TEST(ProtocolGbSipTest, SipRegisterResponse) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_BOTH)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_RESPONSE, (http_parser_type)msg->message_type()); EXPECT_EQ(200, msg->status_code()); } @@ -317,8 +323,9 @@ VOID TEST(ProtocolGbSipTest, SipSessionUacInviteRequest) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_BOTH)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_INVITE, msg->method()); EXPECT_STREQ("/sip:bob@biloxi.com", msg->path().c_str()); EXPECT_STREQ("SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds8", msg->header()->get("Via").c_str()); @@ -378,8 +385,9 @@ VOID TEST(ProtocolGbSipTest, SipSessionUasTryingResponse) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_RESPONSE)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_RESPONSE, (http_parser_type) msg->message_type()); EXPECT_EQ(100, msg->status_code()); EXPECT_STREQ("SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds8;received=192.0.2.1", msg->header()->get("Via").c_str()); @@ -434,8 +442,9 @@ VOID TEST(ProtocolGbSipTest, SipSessionUas200OkResponse) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_RESPONSE)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_RESPONSE, (http_parser_type) msg->message_type()); EXPECT_EQ(200, msg->status_code()); EXPECT_STREQ("SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds8;received=192.0.2.1", msg->header()->get("Via").c_str()); @@ -493,8 +502,9 @@ VOID TEST(ProtocolGbSipTest, SipSessionUacAckRequest) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_BOTH)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_ACK, msg->method()); EXPECT_STREQ("/sip:bob@192.0.2.4", msg->path().c_str()); EXPECT_STREQ("SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds9", msg->header()->get("Via").c_str()); @@ -548,8 +558,9 @@ VOID TEST(ProtocolGbSipTest, SipSessionUacByeRequest) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_BOTH)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_BYE, msg->method()); EXPECT_STREQ("/sip:alice@pc33.atlanta.com", msg->path().c_str()); EXPECT_STREQ("SIP/2.0/UDP 192.0.2.4;branch=z9hG4bKnashds10", msg->header()->get("Via").c_str()); @@ -603,8 +614,8 @@ VOID TEST(ProtocolGbSipTest, SipRegisterExpires) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_REQUEST)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); SrsSipMessage smsg; HELPER_ASSERT_SUCCESS(smsg.parse(msg)); @@ -629,8 +640,8 @@ VOID TEST(ProtocolGbSipTest, SipRegisterExpires) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_REQUEST)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); SrsSipMessage smsg; HELPER_ASSERT_SUCCESS(smsg.parse(msg)); @@ -670,8 +681,8 @@ VOID TEST(ProtocolGbSipTest, SipSmallMessagesInOneBuffer) if (true) { ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); SrsSipMessage smsg; HELPER_ASSERT_SUCCESS(smsg.parse(msg)); @@ -681,8 +692,8 @@ VOID TEST(ProtocolGbSipTest, SipSmallMessagesInOneBuffer) if (true) { ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); SrsSipMessage smsg; HELPER_ASSERT_SUCCESS(smsg.parse(msg)); @@ -723,8 +734,8 @@ VOID TEST(ProtocolGbSipTest, SipSmallMessagesWithBody) if (true) { ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); SrsSipMessage smsg; HELPER_ASSERT_SUCCESS(smsg.parse(msg)); @@ -735,8 +746,8 @@ VOID TEST(ProtocolGbSipTest, SipSmallMessagesWithBody) if (true) { ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); SrsSipMessage smsg; HELPER_ASSERT_SUCCESS(smsg.parse(msg)); @@ -999,8 +1010,9 @@ VOID TEST(ProtocolGbSipTest, GbRegisterRequest) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_REQUEST)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_REGISTER, msg->method()); EXPECT_EQ(0, msg->content_length()); @@ -1061,8 +1073,9 @@ VOID TEST(ProtocolGbSipTest, GbRegisterResponse) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_RESPONSE)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); + EXPECT_EQ(HTTP_RESPONSE, (http_parser_type) msg->message_type()); EXPECT_EQ(200, msg->status_code()); EXPECT_EQ(0, msg->content_length()); @@ -1133,8 +1146,8 @@ VOID TEST(ProtocolGbSipTest, GbInviteRequest) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_BOTH)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); SrsSipMessage smsg; HELPER_ASSERT_SUCCESS(smsg.parse(msg)); @@ -1190,8 +1203,8 @@ VOID TEST(ProtocolGbSipTest, GbTringResponse) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_RESPONSE)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); SrsSipMessage smsg; HELPER_ASSERT_SUCCESS(smsg.parse(msg)); @@ -1255,8 +1268,8 @@ VOID TEST(ProtocolGbSipTest, Gb200OkResponse) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_RESPONSE)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); SrsSipMessage smsg; HELPER_ASSERT_SUCCESS(smsg.parse(msg)); @@ -1309,8 +1322,8 @@ VOID TEST(ProtocolGbSipTest, GbAckRequest) HELPER_ASSERT_SUCCESS(p.initialize(HTTP_BOTH)); ISrsHttpMessage* msg = NULL; - SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(p.parse_message(&r, &msg)); + SrsUniquePtr msg_uptr(msg); SrsSipMessage smsg; HELPER_ASSERT_SUCCESS(smsg.parse(msg)); diff --git a/trunk/src/utest/srs_utest_http.cpp b/trunk/src/utest/srs_utest_http.cpp index 77a42c6e0e..b49f436d05 100644 --- a/trunk/src/utest/srs_utest_http.cpp +++ b/trunk/src/utest/srs_utest_http.cpp @@ -620,7 +620,7 @@ VOID TEST(ProtocolHTTPTest, HTTPHeaderOrder) if (true) { SrsJsonObject* o = SrsJsonObject::object(); - SrsAutoFree(SrsJsonObject, o); + SrsUniquePtr o_uptr(o); h.dumps(o); ASSERT_EQ(3, o->count()); @@ -633,7 +633,7 @@ VOID TEST(ProtocolHTTPTest, HTTPHeaderOrder) h.del("User-Agent"); SrsJsonObject* o = SrsJsonObject::object(); - SrsAutoFree(SrsJsonObject, o); + SrsUniquePtr o_uptr(o); h.dumps(o); ASSERT_EQ(2, o->count()); @@ -645,7 +645,7 @@ VOID TEST(ProtocolHTTPTest, HTTPHeaderOrder) h.del("Server"); SrsJsonObject* o = SrsJsonObject::object(); - SrsAutoFree(SrsJsonObject, o); + SrsUniquePtr o_uptr(o); h.dumps(o); ASSERT_EQ(1, o->count()); @@ -656,7 +656,7 @@ VOID TEST(ProtocolHTTPTest, HTTPHeaderOrder) h.del("Connection"); SrsJsonObject* o = SrsJsonObject::object(); - SrsAutoFree(SrsJsonObject, o); + SrsUniquePtr o_uptr(o); h.dumps(o); ASSERT_EQ(0, o->count()); @@ -748,7 +748,7 @@ VOID TEST(ProtocolHTTPTest, HTTPServerMuxerImplicitHandler) HELPER_ASSERT_SUCCESS(s.handle("/api/", h0)); MockHttpHandler* h1 = new MockHttpHandler("Done"); - SrsAutoFree(MockHttpHandler, h1); + SrsUniquePtr o_uptr(h1); HELPER_EXPECT_FAILED(s.handle("/api/", h1)); } @@ -893,7 +893,7 @@ VOID TEST(ProtocolHTTPTest, HTTPServerMuxerBasic) HELPER_ASSERT_SUCCESS(s.initialize()); MockHttpHandler* h0 = new MockHttpHandler("Hello, world!"); - SrsAutoFree(MockHttpHandler, h0); + SrsUniquePtr o_uptr(h0); HELPER_EXPECT_FAILED(s.handle("", h0)); } @@ -2154,7 +2154,7 @@ VOID TEST(ProtocolHTTPTest, ParsingLargeMessages) if (true) { // First message, 144 header + 315 body. io.append(p, 144 + 315); p += 144 + 315; - ISrsHttpMessage* msg = NULL; SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); + ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); SrsUniquePtr msg_uptr(msg); EXPECT_EQ(315, msg->content_length()); string body; HELPER_ASSERT_SUCCESS(msg->body_read_all(body)); @@ -2164,7 +2164,7 @@ VOID TEST(ProtocolHTTPTest, ParsingLargeMessages) if (true) { // Second message, 164 header + 683 body. io.append(p, 164 + 683); p += 164 + 683; - ISrsHttpMessage* msg = NULL; SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); + ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); SrsUniquePtr msg_uptr(msg); EXPECT_EQ(683, msg->content_length()); string body; HELPER_ASSERT_SUCCESS(msg->body_read_all(body)); @@ -2174,7 +2174,7 @@ VOID TEST(ProtocolHTTPTest, ParsingLargeMessages) if (true) { // Thrid message, 144 header + 315 body. io.append(p, 144 + 315); p += 144 + 315; - ISrsHttpMessage* msg = NULL; SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); + ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); SrsUniquePtr msg_uptr(msg); EXPECT_EQ(315, msg->content_length()); string body; HELPER_ASSERT_SUCCESS(msg->body_read_all(body)); @@ -2184,7 +2184,7 @@ VOID TEST(ProtocolHTTPTest, ParsingLargeMessages) if (true) { // Forth message, 164 header + 255 body. io.append(p, 164 + 255); p += 164 + 255; - ISrsHttpMessage* msg = NULL; SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); + ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); SrsUniquePtr msg_uptr(msg); EXPECT_EQ(255, msg->content_length()); string body; HELPER_ASSERT_SUCCESS(msg->body_read_all(body)); @@ -2197,7 +2197,7 @@ VOID TEST(ProtocolHTTPTest, ParsingLargeMessages) // First, we got 4k bytes, then got the left bytes, to simulate the network read. r.in_bytes.push_back(string((char*)p, 4096)); p += 4096; r.in_bytes.push_back(string((char*)p, 165 + 6317 - 4096)); p += 165 + 6317 - 4096; - ISrsHttpMessage* msg = NULL; SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(hp.parse_message(&r, &msg)); + ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&r, &msg)); SrsUniquePtr msg_uptr(msg); EXPECT_EQ(6317, msg->content_length()); string body; HELPER_ASSERT_SUCCESS(msg->body_read_all(body)); @@ -2209,7 +2209,7 @@ VOID TEST(ProtocolHTTPTest, ParsingLargeMessages) io.append(p, 164 + 354); p += 164 + 354; EXPECT_EQ((int)sizeof(data), p - data); - ISrsHttpMessage* msg = NULL; SrsAutoFree(ISrsHttpMessage, msg); HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); + ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); SrsUniquePtr msg_uptr(msg); EXPECT_EQ(354, msg->content_length()); string body; HELPER_ASSERT_SUCCESS(msg->body_read_all(body)); diff --git a/trunk/src/utest/srs_utest_kernel.cpp b/trunk/src/utest/srs_utest_kernel.cpp index 7525963bcb..00875638d1 100644 --- a/trunk/src/utest/srs_utest_kernel.cpp +++ b/trunk/src/utest/srs_utest_kernel.cpp @@ -439,29 +439,27 @@ void mock_print_mp4(string data) return; } - SrsSimpleStream* stream = new SrsSimpleStream(); - SrsAutoFree(SrsSimpleStream, stream); + SrsUniquePtr stream(new SrsSimpleStream()); while (true) { SrsMp4Box* box = NULL; - SrsAutoFree(SrsMp4Box, box); - - if ((err = br.read(stream, &box)) != srs_success) { + if ((err = br.read(stream.get(), &box)) != srs_success) { if (srs_error_code(err) != ERROR_SYSTEM_FILE_EOF) { mock_print_err(srs_error_wrap(err, "read")); } return; } + SrsUniquePtr box_uptr(box); SrsBuffer* buffer = new SrsBuffer(stream->bytes(), stream->length()); - SrsAutoFree(SrsBuffer, buffer); + SrsUniquePtr buffer_uptr(buffer); if ((err = box->decode(buffer)) != srs_success) { mock_print_err(srs_error_wrap(err, "decode")); return; } - if ((err = br.skip(box, stream)) != srs_success) { + if ((err = br.skip(box, stream.get())) != srs_success) { mock_print_err(srs_error_wrap(err, "skip")); return; } @@ -3458,7 +3456,7 @@ VOID TEST(KernelCodecTest, AVFrame) if (true) { SrsAudioFrame f; SrsAudioCodecConfig* cc = new SrsAudioCodecConfig(); - SrsAutoFree(SrsAudioCodecConfig, cc); + SrsUniquePtr cc_uptr(cc); HELPER_EXPECT_SUCCESS(f.initialize(cc)); EXPECT_TRUE(f.acodec() != NULL); @@ -3497,7 +3495,7 @@ VOID TEST(KernelCodecTest, AVFrame) if (true) { SrsVideoFrame f; SrsVideoCodecConfig* cc = new SrsVideoCodecConfig(); - SrsAutoFree(SrsVideoCodecConfig, cc); + SrsUniquePtr cc_uptr(cc); HELPER_EXPECT_SUCCESS(f.initialize(cc)); EXPECT_TRUE(f.vcodec() != NULL); @@ -3509,7 +3507,7 @@ VOID TEST(KernelCodecTest, AVFrame) if (true) { SrsVideoFrame f; SrsVideoCodecConfig* cc = new SrsVideoCodecConfig(); - SrsAutoFree(SrsVideoCodecConfig, cc); + SrsUniquePtr cc_uptr(cc); HELPER_EXPECT_SUCCESS(f.initialize(cc)); EXPECT_TRUE(f.vcodec() != NULL); @@ -3520,7 +3518,7 @@ VOID TEST(KernelCodecTest, AVFrame) if (true) { SrsVideoFrame f; SrsVideoCodecConfig* cc = new SrsVideoCodecConfig(); - SrsAutoFree(SrsVideoCodecConfig, cc); + SrsUniquePtr cc_uptr(cc); HELPER_EXPECT_SUCCESS(f.initialize(cc)); EXPECT_TRUE(f.vcodec() != NULL); @@ -3531,7 +3529,7 @@ VOID TEST(KernelCodecTest, AVFrame) if (true) { SrsVideoFrame f; SrsVideoCodecConfig* cc = new SrsVideoCodecConfig(); - SrsAutoFree(SrsVideoCodecConfig, cc); + SrsUniquePtr cc_uptr(cc); HELPER_EXPECT_SUCCESS(f.initialize(cc)); EXPECT_TRUE(f.vcodec() != NULL); @@ -3542,7 +3540,7 @@ VOID TEST(KernelCodecTest, AVFrame) if (true) { SrsVideoFrame f; SrsVideoCodecConfig* cc = new SrsVideoCodecConfig(); - SrsAutoFree(SrsVideoCodecConfig, cc); + SrsUniquePtr cc_uptr(cc); HELPER_EXPECT_SUCCESS(f.initialize(cc)); EXPECT_TRUE(f.vcodec() != NULL); @@ -6295,7 +6293,7 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) HELPER_EXPECT_SUCCESS(fmt.on_video(0, (char*)raw, sizeof(raw))); uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); - SrsAutoFreeA(uint8_t, cp); + SrsUniquePtr cp_uptr(cp); HELPER_EXPECT_SUCCESS(enc.write_sample( SrsMp4HandlerTypeVIDE, fmt.video->frame_type, 0, 0, cp, fmt.nb_raw )); @@ -6308,7 +6306,7 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) HELPER_EXPECT_SUCCESS(fmt.on_audio(0, (char*)raw, sizeof(raw))); uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); - SrsAutoFreeA(uint8_t, cp); + SrsUniquePtr cp_uptr(cp); HELPER_EXPECT_SUCCESS(enc.write_sample( SrsMp4HandlerTypeSOUN, 0x00, 0, 0, cp, fmt.nb_raw )); @@ -6327,7 +6325,7 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) HELPER_EXPECT_SUCCESS(fmt.on_audio(0, (char*)raw, sizeof(raw))); uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); - SrsAutoFreeA(uint8_t, cp); + SrsUniquePtr cp_uptr(cp); HELPER_EXPECT_SUCCESS(enc.write_sample( SrsMp4HandlerTypeSOUN, 0x00, 34, 34, cp, fmt.nb_raw )); @@ -6349,7 +6347,7 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) HELPER_EXPECT_SUCCESS(fmt.on_video(0, (char*)raw, sizeof(raw))); uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); - SrsAutoFreeA(uint8_t, cp); + SrsUniquePtr cp_uptr(cp); HELPER_EXPECT_SUCCESS(enc.write_sample( SrsMp4HandlerTypeVIDE, fmt.video->frame_type, 40, 40, cp, fmt.nb_raw )); diff --git a/trunk/src/utest/srs_utest_protocol.cpp b/trunk/src/utest/srs_utest_protocol.cpp index 09590c697f..02ad943249 100644 --- a/trunk/src/utest/srs_utest_protocol.cpp +++ b/trunk/src/utest/srs_utest_protocol.cpp @@ -19,6 +19,7 @@ using namespace std; #include #include #include +#include MockEmptyIO::MockEmptyIO() { @@ -399,7 +400,7 @@ VOID TEST(ProtocolHandshakeTest, VerifyFPC0C1) // manually validate the c1 // @see: calc_c1_digest char* c1s1_joined_bytes = new char[1536 -32]; - SrsAutoFreeA(char, c1s1_joined_bytes); + SrsUniquePtr cp_uptr(c1s1_joined_bytes); HELPER_ASSERT_SUCCESS( c1.payload->copy_to(&c1, c1s1_joined_bytes, 1536 - 32, false)); bool is_valid; @@ -739,12 +740,12 @@ VOID TEST(ProtocolStackTest, ProtocolRecvMessage) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt = NULL; HELPER_EXPECT_SUCCESS(proto.decode_message(msg, &pkt)); - SrsAutoFree(SrsPacket, pkt); - + SrsUniquePtr pkt_uptr(pkt); + SrsConnectAppPacket* spkt = dynamic_cast(pkt); ASSERT_TRUE(NULL != spkt); } @@ -776,11 +777,11 @@ VOID TEST(ProtocolStackTest, ProtocolRecvMessageBug98) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt = NULL; HELPER_EXPECT_SUCCESS(proto.decode_message(msg, &pkt)); - SrsAutoFree(SrsPacket, pkt); + SrsUniquePtr pkt_uptr(pkt); SrsUserControlPacket* spkt = dynamic_cast(pkt); ASSERT_TRUE(NULL != spkt); @@ -813,11 +814,11 @@ VOID TEST(ProtocolStackTest, ProtocolRecvAckSizeMessage) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt = NULL; HELPER_EXPECT_SUCCESS(proto.decode_message(msg, &pkt)); - SrsAutoFree(SrsPacket, pkt); + SrsUniquePtr pkt_uptr(pkt); SrsSetWindowAckSizePacket* spkt = dynamic_cast(pkt); ASSERT_TRUE(NULL != spkt); @@ -849,7 +850,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVMessage) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); } @@ -878,7 +879,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvAMessage) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_audio()); } @@ -926,7 +927,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVMessage2Trunk) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); } @@ -1019,13 +1020,13 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAMessage) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_audio()); } if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); } } @@ -1139,13 +1140,13 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAFmt1) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_audio()); } if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); } } @@ -1257,13 +1258,13 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAFmt2) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_audio()); } if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); } } @@ -1374,13 +1375,13 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAFmt3) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_audio()); } if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); } } @@ -1519,7 +1520,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVMessage) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -1527,7 +1528,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVMessage) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -1535,7 +1536,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVMessage) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x20, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -1689,7 +1690,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVFmt1) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -1697,7 +1698,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVFmt1) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -1705,7 +1706,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVFmt1) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x22, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -1857,7 +1858,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVFmt2) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -1865,7 +1866,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVFmt2) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -1873,7 +1874,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVFmt2) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x22, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -2024,7 +2025,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVFmt3) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -2032,7 +2033,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVFmt3) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -2040,7 +2041,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVFmt3) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x20, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -2094,7 +2095,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid1BNormal) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 1B cid(6bits), cid in 2-63 EXPECT_EQ(0x09, msg->header.prefer_cid); @@ -2147,7 +2148,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid1BMax) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 1B cid(6bits), max is 63 EXPECT_EQ(0x3F, msg->header.prefer_cid); @@ -2200,7 +2201,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid2BMin) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 2B cid(8bits), min is 64 EXPECT_EQ(64, msg->header.prefer_cid); @@ -2253,7 +2254,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid2BNormal) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 2B cid(8bits), cid in 64-319 EXPECT_EQ(0x10+64, msg->header.prefer_cid); @@ -2306,7 +2307,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid2BNormal2) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 2B cid(8bits), cid in 64-319 EXPECT_EQ(0x11+64, msg->header.prefer_cid); @@ -2359,7 +2360,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid2BMax) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 2B cid(68bits), max is 319 EXPECT_EQ(319, msg->header.prefer_cid); @@ -2412,7 +2413,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid3BMin) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 3B cid(16bits), min is 64 EXPECT_EQ(64, msg->header.prefer_cid); @@ -2465,7 +2466,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid3BNormal) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 3B cid(16bits), cid in 64-65599 EXPECT_EQ(0x10*256+64, msg->header.prefer_cid); @@ -2518,7 +2519,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid3BNormal2) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 3B cid(16bits), cid in 64-65599 EXPECT_EQ(0x01 + (0x10*256) + 64, msg->header.prefer_cid); @@ -2571,7 +2572,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid3BNormal3) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 3B cid(16bits), cid in 64-65599 EXPECT_EQ(0xFF + (0x10*256) + 64, msg->header.prefer_cid); @@ -2624,7 +2625,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid3BNormal4) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 3B cid(16bits), cid in 64-65599 EXPECT_EQ(0x02 + (0x10*256) + 64, msg->header.prefer_cid); @@ -2677,7 +2678,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid3BMax) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // 2B cid(16bits), max is 65599 EXPECT_EQ(65599, msg->header.prefer_cid); @@ -2717,7 +2718,7 @@ VOID TEST(ProtocolStackTest, ProtocolRecvV0LenMessage) SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_video()); // protocol stack will ignore the empty video message. EXPECT_EQ(4, msg->header.payload_length); @@ -2735,7 +2736,7 @@ VOID TEST(ProtocolStackTest, ProtocolSendVMessage) uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; - SrsCommonMessage* msg = new SrsCommonMessage(); SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = new SrsCommonMessage(); SrsUniquePtr msg_uptr(msg); msg->size = sizeof(data); msg->payload = new char[msg->size]; memcpy(msg->payload, data, msg->size); @@ -3314,7 +3315,7 @@ VOID TEST(ProtocolStackTest, ProtocolAckSizeFlow) } if (true) { - SrsCommonMessage* msg = new SrsCommonMessage(); SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = new SrsCommonMessage(); SrsUniquePtr msg_uptr(msg); msg->header.payload_length = msg->size = 4096; msg->payload = new char[msg->size]; @@ -3337,14 +3338,14 @@ VOID TEST(ProtocolStackTest, ProtocolAckSizeFlow) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); ASSERT_TRUE(msg->header.is_window_ackledgement_size()); } // recv video if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); ASSERT_TRUE(msg->header.is_video()); } @@ -3355,14 +3356,16 @@ VOID TEST(ProtocolStackTest, ProtocolAckSizeFlow) } // recv auto send acked size. #1 if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); ASSERT_TRUE(msg->header.is_ackledgement()); } // send again if (true) { - SrsCommonMessage* msg = new SrsCommonMessage(); SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = new SrsCommonMessage(); + SrsUniquePtr msg_uptr(msg); msg->header.payload_length = msg->size = 4096; msg->payload = new char[msg->size]; @@ -3383,7 +3386,7 @@ VOID TEST(ProtocolStackTest, ProtocolAckSizeFlow) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); ASSERT_TRUE(msg->header.is_video()); } @@ -3396,7 +3399,7 @@ VOID TEST(ProtocolStackTest, ProtocolAckSizeFlow) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); ASSERT_TRUE(msg->header.is_ackledgement()); } } @@ -3427,7 +3430,7 @@ VOID TEST(ProtocolStackTest, ProtocolPingFlow) if (true) { SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); EXPECT_TRUE(msg->header.is_user_control_message()); } @@ -3439,12 +3442,15 @@ VOID TEST(ProtocolStackTest, ProtocolPingFlow) } // recv ping if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); ASSERT_TRUE(msg->header.is_user_control_message()); - SrsPacket* pkt = NULL; SrsAutoFree(SrsPacket, pkt); + SrsPacket* pkt = NULL; HELPER_ASSERT_SUCCESS(proto.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); + SrsUserControlPacket* spkt = dynamic_cast(pkt); ASSERT_TRUE(spkt != NULL); @@ -3504,8 +3510,8 @@ VOID TEST(ProtocolStackTest, ProtocolExcpectMessage) SrsCommonMessage* msg = NULL; SrsConnectAppPacket* pkt = NULL; HELPER_ASSERT_SUCCESS(proto.expect_message(&msg, &pkt)); - SrsAutoFree(SrsCommonMessage, msg); - SrsAutoFree(SrsConnectAppPacket, pkt); + SrsUniquePtr msg_uptr(msg); + SrsUniquePtr pkt_uptr(pkt); ASSERT_TRUE(NULL != pkt); } diff --git a/trunk/src/utest/srs_utest_protocol2.cpp b/trunk/src/utest/srs_utest_protocol2.cpp index b24529db9b..552abebaff 100644 --- a/trunk/src/utest/srs_utest_protocol2.cpp +++ b/trunk/src/utest/srs_utest_protocol2.cpp @@ -209,33 +209,37 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVVFmt11) } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x20, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x40, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -433,33 +437,37 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVVFmt11Length) } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x20, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x40, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -653,33 +661,37 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVVFmt12) } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x20, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x40, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -876,35 +888,39 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVVFmt12Length) } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); EXPECT_EQ(0x110, msg->header.payload_length); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x20, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); EXPECT_EQ(0x120, msg->header.payload_length); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x40, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -949,9 +965,10 @@ VOID TEST(ProtocolStackTest, ProtocolRecvExtTimeMessage) }; bio.in_buffer.append((char*)data, sizeof(data)); - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); } @@ -993,9 +1010,10 @@ VOID TEST(ProtocolStackTest, ProtocolRecvExtTimeMessage2) }; bio.in_buffer.append((char*)data, sizeof(data)); - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x7f010203, msg->header.timestamp); } @@ -1038,9 +1056,10 @@ VOID TEST(ProtocolStackTest, ProtocolRecvExtTimeMessage3) }; bio.in_buffer.append((char*)data, sizeof(data)); - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); // always use 31bits timestamp EXPECT_EQ(0x7f010203, msg->header.timestamp); @@ -1113,9 +1132,10 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVExtTime2Trunk) }; bio.in_buffer.append((char*)data, sizeof(data)); - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); // 0xCX with extended timestamp. EXPECT_EQ(0x00010203, msg->header.timestamp); @@ -1170,9 +1190,10 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVExtTime2Trunk2) }; bio.in_buffer.append((char*)data, sizeof(data)); - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); // 0xCX without extended timestamp. EXPECT_EQ(0x00010203, msg->header.timestamp); @@ -1223,9 +1244,10 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVCid1BMin) }; bio.in_buffer.append((char*)data, sizeof(data)); - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); // 1B cid(6bits), min is 2 EXPECT_EQ(0x02, msg->header.prefer_cid); @@ -1235,15 +1257,15 @@ VOID TEST(ProtocolKbpsTest, Connections) { if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); SrsKbps* kbps = new SrsKbps(clock->set_clock(0)); - SrsAutoFree(SrsKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); SrsNetworkDelta* delta = new SrsNetworkDelta(); - SrsAutoFree(SrsNetworkDelta, delta); + SrsUniquePtr delta_uptr(delta); delta->set_io(io, io); // No data, 0kbps. @@ -1289,15 +1311,15 @@ VOID TEST(ProtocolKbpsTest, Connections) if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); SrsKbps* kbps = new SrsKbps(clock->set_clock(0)); - SrsAutoFree(SrsKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); SrsNetworkDelta* delta = new SrsNetworkDelta(); - SrsAutoFree(SrsNetworkDelta, delta); + SrsUniquePtr delta_uptr(delta); delta->set_io(io, io); // No data, 0kbps. @@ -1343,15 +1365,15 @@ VOID TEST(ProtocolKbpsTest, Connections) if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); SrsKbps* kbps = new SrsKbps(clock->set_clock(0)); - SrsAutoFree(SrsKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); SrsNetworkDelta* delta = new SrsNetworkDelta(); - SrsAutoFree(SrsNetworkDelta, delta); + SrsUniquePtr delta_uptr(delta); delta->set_io(io, io); // No data, 0kbps. @@ -1400,12 +1422,12 @@ VOID TEST(ProtocolKbpsTest, Delta) { if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); SrsNetworkDelta* delta = new SrsNetworkDelta(); - SrsAutoFree(SrsNetworkDelta, delta); + SrsUniquePtr delta_uptr(delta); delta->set_io(io, io); // No data. @@ -1428,12 +1450,12 @@ VOID TEST(ProtocolKbpsTest, Delta) if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); SrsNetworkDelta* delta = new SrsNetworkDelta(); - SrsAutoFree(SrsNetworkDelta, delta); + SrsUniquePtr delta_uptr(delta); delta->set_io(io, io); // No data. @@ -1450,7 +1472,7 @@ VOID TEST(ProtocolKbpsTest, Delta) // Kbps without io, gather delta. SrsKbps* kbps = new SrsKbps(clock->set_clock(0)); - SrsAutoFree(SrsKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); // No data, 0kbps. kbps->sample(); @@ -1482,16 +1504,16 @@ VOID TEST(ProtocolKbpsTest, RAWStatistic) { if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); SrsNetworkDelta* delta = new SrsNetworkDelta(); - SrsAutoFree(SrsNetworkDelta, delta); + SrsUniquePtr delta_uptr(delta); delta->set_io(io, io); SrsKbps* kbps = new SrsKbps(clock->set_clock(0)); - SrsAutoFree(SrsKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); // No data, 0kbps. kbps->add_delta(delta); @@ -1522,10 +1544,10 @@ VOID TEST(ProtocolKbpsTest, RAWStatistic) if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); SrsKbps* kbps = new SrsKbps(clock->set_clock(0)); - SrsAutoFree(SrsKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); // No io, no data. EXPECT_EQ(0, kbps->get_recv_bytes()); @@ -1533,10 +1555,10 @@ VOID TEST(ProtocolKbpsTest, RAWStatistic) // With io, zero data. MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); SrsNetworkDelta* delta = new SrsNetworkDelta(); - SrsAutoFree(SrsNetworkDelta, delta); + SrsUniquePtr delta_uptr(delta); delta->set_io(io, io); kbps->add_delta(delta); @@ -1646,12 +1668,12 @@ VOID TEST(ProtocolKbpsTest, ConnectionsSugar) { if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); SrsNetworkKbps* kbps = new SrsNetworkKbps(clock->set_clock(0)); - SrsAutoFree(SrsNetworkKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); kbps->set_io(io, io); // No data, 0kbps. @@ -1694,12 +1716,12 @@ VOID TEST(ProtocolKbpsTest, ConnectionsSugar) if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); SrsNetworkKbps* kbps = new SrsNetworkKbps(clock->set_clock(0)); - SrsAutoFree(SrsNetworkKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); kbps->set_io(io, io); // No data, 0kbps. @@ -1742,12 +1764,12 @@ VOID TEST(ProtocolKbpsTest, ConnectionsSugar) if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); SrsNetworkKbps* kbps = new SrsNetworkKbps(clock->set_clock(0)); - SrsAutoFree(SrsNetworkKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); kbps->set_io(io, io); // No data, 0kbps. @@ -1793,13 +1815,13 @@ VOID TEST(ProtocolKbpsTest, DeltaSugar) { if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); // Kbps without io, gather delta. SrsNetworkKbps* kbps = new SrsNetworkKbps(clock->set_clock(0)); - SrsAutoFree(SrsNetworkKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); kbps->set_io(io, io); // No data, 0kbps. @@ -1832,12 +1854,12 @@ VOID TEST(ProtocolKbpsTest, RAWStatisticSugar) { if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); SrsNetworkKbps* kbps = new SrsNetworkKbps(clock->set_clock(0)); - SrsAutoFree(SrsNetworkKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); kbps->set_io(io, io); // No data, 0kbps. @@ -1867,10 +1889,10 @@ VOID TEST(ProtocolKbpsTest, RAWStatisticSugar) if (true) { MockWallClock* clock = new MockWallClock(); - SrsAutoFree(MockWallClock, clock); + SrsUniquePtr clock_uptr(clock); SrsNetworkKbps* kbps = new SrsNetworkKbps(clock->set_clock(0)); - SrsAutoFree(SrsNetworkKbps, kbps); + SrsUniquePtr kbps_uptr(kbps); // No io, no data. EXPECT_EQ(0, kbps->get_recv_bytes()); @@ -1878,7 +1900,7 @@ VOID TEST(ProtocolKbpsTest, RAWStatisticSugar) // With io, zero data. MockStatistic* io = new MockStatistic(); - SrsAutoFree(MockStatistic, io); + SrsUniquePtr io_uptr(io); kbps->set_io(io, io); kbps->sample(); @@ -1977,8 +1999,8 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage) HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST)); ISrsHttpMessage* req = NULL; - SrsAutoFree(ISrsHttpMessage, req); HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req)); + SrsUniquePtr req_uptr(req); char v[64] = {0}; HELPER_ASSERT_SUCCESS(req->body_reader()->read(v, sizeof(v), NULL)); @@ -1995,8 +2017,8 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage) HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST)); ISrsHttpMessage* req = NULL; - SrsAutoFree(ISrsHttpMessage, req); HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req)); + SrsUniquePtr req_uptr(req); } if (true) { @@ -2007,8 +2029,8 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage) HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST)); ISrsHttpMessage* req = NULL; - SrsAutoFree(ISrsHttpMessage, req); HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req)); + SrsUniquePtr req_uptr(req); } if (true) { @@ -2019,8 +2041,8 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage) HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST)); ISrsHttpMessage* req = NULL; - SrsAutoFree(ISrsHttpMessage, req); HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req)); + SrsUniquePtr req_uptr(req); } } @@ -2497,33 +2519,37 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVVMessage) } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x20, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x30, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -2719,33 +2745,37 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVVFmt1) } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x20, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x30, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -2937,33 +2967,37 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVVFmt2) } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x20, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x30, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); @@ -3153,33 +3187,37 @@ VOID TEST(ProtocolStackTest, ProtocolRecvVAVVFmt3) } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x10, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_audio()); EXPECT_EQ(0x15, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x20, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; - HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); - SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg_raw = NULL; + HELPER_ASSERT_SUCCESS(proto.recv_message(&msg_raw)); + SrsUniquePtr msg(msg_raw); + EXPECT_TRUE(msg->header.is_video()); EXPECT_EQ(0x30, msg->header.timestamp); EXPECT_EQ(0x01, msg->header.stream_id); diff --git a/trunk/src/utest/srs_utest_rtc.cpp b/trunk/src/utest/srs_utest_rtc.cpp index 38f587c73b..481fe99a9e 100644 --- a/trunk/src/utest/srs_utest_rtc.cpp +++ b/trunk/src/utest/srs_utest_rtc.cpp @@ -711,7 +711,7 @@ VOID TEST(KernelRTCTest, NACKFetchRTPPacket) SrsRtcTrackDescription ds; SrsRtcVideoSendTrack* track = new SrsRtcVideoSendTrack(&s, &ds); - SrsAutoFree(SrsRtcVideoSendTrack, track); + SrsUniquePtr track_uptr(track); // The RTP queue will free the packet. if (true) { @@ -975,7 +975,7 @@ VOID TEST(KernelRTCTest, SyncTimestampBySenderReportNormal) if (true) { SrsRtpPacket* video_rtp_pkt = new SrsRtpPacket(); - SrsAutoFree(SrsRtpPacket, video_rtp_pkt); + SrsUniquePtr video_rtp_pkt_uptr(video_rtp_pkt); uint32_t video_absolute_ts = srs_get_system_time(); uint32_t video_rtp_ts = random(); @@ -988,7 +988,7 @@ VOID TEST(KernelRTCTest, SyncTimestampBySenderReportNormal) SrsNtp ntp = SrsNtp::from_time_ms(video_absolute_ts); SrsRtcpSR* video_sr = new SrsRtcpSR(); - SrsAutoFree(SrsRtcpSR, video_sr); + SrsUniquePtr video_sr_uptr(video_sr); video_sr->set_ssrc(200); video_sr->set_ntp(ntp.ntp_); @@ -1042,7 +1042,7 @@ VOID TEST(KernelRTCTest, SyncTimestampBySenderReportOutOfOrder) if (true) { SrsRtpPacket* video_rtp_pkt = new SrsRtpPacket(); - SrsAutoFree(SrsRtpPacket, video_rtp_pkt); + SrsUniquePtr video_rtp_pkt_uptr(video_rtp_pkt); uint32_t video_absolute_ts = srs_get_system_time(); uint32_t video_rtp_ts = random(); @@ -1055,7 +1055,7 @@ VOID TEST(KernelRTCTest, SyncTimestampBySenderReportOutOfOrder) SrsNtp ntp = SrsNtp::from_time_ms(video_absolute_ts); SrsRtcpSR* video_sr1 = new SrsRtcpSR(); - SrsAutoFree(SrsRtcpSR, video_sr1); + SrsUniquePtr video_sr1_uptr(video_sr1); video_sr1->set_ssrc(200); video_sr1->set_ntp(ntp.ntp_); @@ -1072,7 +1072,7 @@ VOID TEST(KernelRTCTest, SyncTimestampBySenderReportOutOfOrder) ntp = SrsNtp::from_time_ms(video_absolute_ts); SrsRtcpSR* video_sr2 = new SrsRtcpSR(); - SrsAutoFree(SrsRtcpSR, video_sr2); + SrsUniquePtr video_sr2_uptr(video_sr2); video_sr2->set_ssrc(200); video_sr2->set_ntp(ntp.ntp_); video_sr2->set_rtp_ts(video_rtp_ts); @@ -1114,7 +1114,7 @@ VOID TEST(KernelRTCTest, SyncTimestampBySenderReportConsecutive) if (true) { SrsRtpPacket* video_rtp_pkt = new SrsRtpPacket(); - SrsAutoFree(SrsRtpPacket, video_rtp_pkt); + SrsUniquePtr video_rtp_pkt_uptr(video_rtp_pkt); uint32_t video_absolute_ts = srs_get_system_time(); uint32_t video_rtp_ts = random(); @@ -1127,7 +1127,7 @@ VOID TEST(KernelRTCTest, SyncTimestampBySenderReportConsecutive) SrsNtp ntp = SrsNtp::from_time_ms(video_absolute_ts); SrsRtcpSR* video_sr = new SrsRtcpSR(); - SrsAutoFree(SrsRtcpSR, video_sr); + SrsUniquePtr video_sr_uptr(video_sr); video_sr->set_ssrc(200); video_sr->set_ntp(ntp.ntp_); @@ -1219,7 +1219,7 @@ VOID TEST(KernelRTCTest, SyncTimestampBySenderReportDuplicated) if (true) { SrsRtpPacket* video_rtp_pkt = new SrsRtpPacket(); - SrsAutoFree(SrsRtpPacket, video_rtp_pkt); + SrsUniquePtr video_rtp_pkt_uptr(video_rtp_pkt); uint32_t video_absolute_ts = srs_get_system_time(); uint32_t video_rtp_ts = random(); @@ -1232,7 +1232,7 @@ VOID TEST(KernelRTCTest, SyncTimestampBySenderReportDuplicated) SrsNtp ntp = SrsNtp::from_time_ms(video_absolute_ts); SrsRtcpSR* video_sr = new SrsRtcpSR(); - SrsAutoFree(SrsRtcpSR, video_sr); + SrsUniquePtr video_sr_uptr(video_sr); video_sr->set_ssrc(200); video_sr->set_ntp(ntp.ntp_); diff --git a/trunk/src/utest/srs_utest_rtmp.cpp b/trunk/src/utest/srs_utest_rtmp.cpp index 66aa31745d..86fbcb2731 100644 --- a/trunk/src/utest/srs_utest_rtmp.cpp +++ b/trunk/src/utest/srs_utest_rtmp.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #define SRS_DEFAULT_RECV_BUFFER_SIZE 131072 @@ -246,7 +247,7 @@ VOID TEST(ProtocolRTMPTest, SendPacketsError) SrsSharedPtrMessage* msg = new SrsSharedPtrMessage(); msg->create(&pkt); - SrsAutoFree(SrsSharedPtrMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsSharedPtrMessage* msgs[10240]; for (int i = 0; i < 10240; i++) { @@ -358,7 +359,7 @@ VOID TEST(ProtocolRTMPTest, HugeMessages) SrsSharedPtrMessage* msg = new SrsSharedPtrMessage(); msg->create(&pkt); - SrsAutoFree(SrsSharedPtrMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsSharedPtrMessage* msgs[1024]; for (int i = 0; i < 1024; i++) { @@ -380,7 +381,7 @@ VOID TEST(ProtocolRTMPTest, HugeMessages) SrsSharedPtrMessage* msg = new SrsSharedPtrMessage(); msg->create(&pkt); - SrsAutoFree(SrsSharedPtrMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsSharedPtrMessage* msgs[10240]; for (int i = 0; i < 10240; i++) { @@ -435,9 +436,10 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages) // Always response ACK message. HELPER_EXPECT_SUCCESS(p.set_in_window_ack_size(1)); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; io.in_buffer.append(&bytes); HELPER_EXPECT_FAILED(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } } @@ -461,12 +463,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages2) uint8_t bytes[] = {0x17, 0x02, 0x00, 0x01, 's', 0x00, 0,0,0,0,0,0,0,0, 0x03,0,0,9}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); msg->header.message_type = RTMP_MSG_AMF3CommandMessage; SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); HELPER_EXPECT_SUCCESS(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); SrsCallPacket* call = (SrsCallPacket*)pkt; EXPECT_STREQ("s", call->command_name.c_str()); @@ -478,13 +480,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages2) uint8_t bytes[] = {0x17, 0x02, 0x00, 0x01, 's'}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); msg->header.message_type = RTMP_MSG_AMF3CommandMessage; SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -493,13 +494,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages2) uint8_t bytes[] = {0x00}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); msg->header.message_type = 0xff; SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - HELPER_EXPECT_SUCCESS(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -508,13 +508,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages2) uint8_t bytes[] = {0x02, 0x00, 0x01, 's'}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); msg->header.message_type = RTMP_MSG_AMF0DataMessage; SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - HELPER_EXPECT_SUCCESS(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } } @@ -528,14 +527,13 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages3) uint8_t bytes[] = {0x02, 0x00, 0x07, '_','r','e','s','u','l','t'}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); msg->header.message_type = RTMP_MSG_AMF0DataMessage; SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Decode the response failed, no transaction ID was set by request. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -544,14 +542,13 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages3) uint8_t bytes[] = {0x17, 0x02, 0x00, 0x07, '_','r','e','s','u','l','t'}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); msg->header.message_type = RTMP_MSG_AMF3DataMessage; SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Decode the response failed, no transaction ID was set by request. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -560,14 +557,13 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages3) uint8_t bytes[] = {0x17, 0x02, 0x00, 0x07, '_','r','e','s','u','l','t', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); msg->header.message_type = RTMP_MSG_AMF3CommandMessage; SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Decode the response failed, no transaction ID was set by request. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -580,13 +576,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages3) uint8_t bytes[] = {0x02, 0x00, 0x07, '_','r','e','s','u','l','t', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the response packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -599,13 +594,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages3) uint8_t bytes[] = {0x02, 0x00, 0x07, '_','r','e','s','u','l','t', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the response packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -618,13 +612,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages3) uint8_t bytes[] = {0x02, 0x00, 0x07, '_','r','e','s','u','l','t', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the response packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -637,13 +630,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages3) uint8_t bytes[] = {0x02, 0x00, 0x07, '_','r','e','s','u','l','t', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the response packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -657,13 +649,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages3) uint8_t bytes[] = {0x02, 0x00, 0x07, '_','r','e','s','u','l','t', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the response packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -677,13 +668,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages3) uint8_t bytes[] = {0x02, 0x00, 0x07, '_','r','e','s','u','l','t', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the response packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } } @@ -697,13 +687,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 0x07, 'c','o','n','n','e','c','t', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the request packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -712,13 +701,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 12, 'c','r','e','a','t','e','S','t','r','e','a','m', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the request packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -727,13 +715,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 4, 'p','l','a','y', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the request packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -742,13 +729,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 5, 'p','a','u','s','e', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the request packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -757,13 +743,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 13, 'r','e','l','e','a','s','e','S','t','r','e','a','m', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the request packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -772,13 +757,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 9, 'F','C','P','u','b','l','i','s','h', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the request packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -787,13 +771,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 7, 'p','u','b','l','i','s','h', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the request packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -802,13 +785,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 11, 'F','C','U','n','p','u','b','l','i','s','h', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the request packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -817,13 +799,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 13, '@','s','e','t','D','a','t','a','F','r','a','m','e', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the request packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -832,12 +813,11 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 10, 'o','n','M','e','t','a','D','a','t','a', 03,0,0,9}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - HELPER_EXPECT_SUCCESS(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -846,13 +826,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 11, 'c','l','o','s','e','S','t','r','e','a','m', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the request packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } if (true) { @@ -862,13 +841,12 @@ VOID TEST(ProtocolRTMPTest, OnDecodeMessages4) uint8_t bytes[] = {0x02, 0x00, 3, 's','r','s', 0x00,0,0,0,0,0,0,0,0}; SrsCommonMessage* msg = _create_amf0((char*)bytes, sizeof(bytes), 1); msg->header.message_type = RTMP_MSG_AMF0CommandMessage; - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); SrsPacket* pkt; - SrsAutoFree(SrsPacket, pkt); - // Without enough data, it fail when decoding the request packet. HELPER_EXPECT_FAILED(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } } @@ -883,8 +861,9 @@ VOID TEST(ProtocolRTMPTest, RecvMessage) uint8_t bytes[] = {0x01, 0x00, 0x00}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_FAILED(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } if (true) { @@ -894,8 +873,9 @@ VOID TEST(ProtocolRTMPTest, RecvMessage) uint8_t bytes[] = {0x00, 0x00}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_FAILED(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } if (true) { @@ -905,16 +885,18 @@ VOID TEST(ProtocolRTMPTest, RecvMessage) uint8_t bytes[] = {0x00}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_FAILED(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } if (true) { MockBufferIO io; SrsProtocol p(&io); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_FAILED(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } } @@ -929,8 +911,9 @@ VOID TEST(ProtocolRTMPTest, RecvMessage2) uint8_t bytes[] = {0x03, 0,0,0, 0,0,4, 0, 0,0,0,0, 1,2,3}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_FAILED(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } if (true) { @@ -943,14 +926,16 @@ VOID TEST(ProtocolRTMPTest, RecvMessage2) io.in_buffer.append((char*)bytes, sizeof(bytes)); if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_FAILED(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; uint8_t bytes2[] = {0x43, 0,0,0, 0,0,5, 0, 0,0,0,0, 1,2,3}; io.in_buffer.append((char*)bytes2, sizeof(bytes2)); HELPER_EXPECT_FAILED(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } if (true) { @@ -960,8 +945,9 @@ VOID TEST(ProtocolRTMPTest, RecvMessage2) uint8_t bytes[] = {0x03}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_FAILED(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } if (true) { @@ -971,8 +957,9 @@ VOID TEST(ProtocolRTMPTest, RecvMessage2) uint8_t bytes[] = {0x43, 0,0,0, 0,0,0, 0}; io.in_buffer.append((char*)bytes, sizeof(bytes)); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_FAILED(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } } @@ -1054,8 +1041,9 @@ VOID TEST(ProtocolRTMPTest, RecvMessage4) io.in_buffer.append(&io.out_buffer); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); EXPECT_EQ(256, p.out_chunk_size); } @@ -1071,8 +1059,9 @@ VOID TEST(ProtocolRTMPTest, RecvMessage4) io.in_buffer.append(&io.out_buffer); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); EXPECT_EQ(256, p.in_buffer_length); } @@ -1526,8 +1515,9 @@ VOID TEST(ProtocolRTMPTest, ServerCommandMessage) SrsProtocol p(&tio); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); EXPECT_EQ(1024, p.in_chunk_size); } @@ -2247,8 +2237,9 @@ VOID TEST(ProtocolRTMPTest, CoverAll) EXPECT_EQ(0, r.get_recv_bytes()); EXPECT_EQ(0, r.get_send_bytes()); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_FAILED(r.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); SrsCallPacket* pkt = new SrsCallPacket(); HELPER_EXPECT_SUCCESS(r.send_and_free_packet(pkt, 0)); @@ -2270,8 +2261,9 @@ VOID TEST(ProtocolRTMPTest, CoverAll) EXPECT_EQ(0, r.get_recv_bytes()); EXPECT_EQ(0, r.get_send_bytes()); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_FAILED(r.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); SrsCallPacket* pkt = new SrsCallPacket(); HELPER_EXPECT_SUCCESS(r.send_and_free_packet(pkt, 0)); @@ -2328,7 +2320,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) int nn = name->total_size() + arr->total_size(); char* b = new char[nn]; - SrsAutoFreeA(char, b); + SrsUniquePtr cp_uptr(b); SrsBuffer buf(b, nn); HELPER_ASSERT_SUCCESS(name->write(&buf)); @@ -2336,7 +2328,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) srs_freep(name); srs_freep(arr); SrsOnMetaDataPacket* p = new SrsOnMetaDataPacket(); - SrsAutoFree(SrsOnMetaDataPacket, p); + SrsUniquePtr p_uptr(p); buf.skip(-1 * buf.pos()); HELPER_ASSERT_SUCCESS(p->decode(&buf)); @@ -2405,7 +2397,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) int nn = name->total_size() + tid->total_size() + null->total_size() + is_pause->total_size() + ts->total_size(); char* b = new char[nn]; - SrsAutoFreeA(char, b); + SrsUniquePtr cp_uptr(b); SrsBuffer buf(b, nn); HELPER_ASSERT_SUCCESS(name->write(&buf)); @@ -2416,7 +2408,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) srs_freep(name); srs_freep(tid); srs_freep(null); srs_freep(is_pause); srs_freep(ts); SrsPausePacket* p = new SrsPausePacket(); - SrsAutoFree(SrsPausePacket, p); + SrsUniquePtr p_uptr(p); buf.skip(-1 * buf.pos()); HELPER_ASSERT_SUCCESS(p->decode(&buf)); @@ -2435,7 +2427,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) int nn = name->total_size() + tid->total_size() + null->total_size() + stream_name->total_size() + start->total_size() + duration->total_size() + reset->total_size(); char* b = new char[nn]; - SrsAutoFreeA(char, b); + SrsUniquePtr cp_uptr(b); SrsBuffer buf(b, nn); HELPER_ASSERT_SUCCESS(name->write(&buf)); @@ -2448,7 +2440,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) srs_freep(name); srs_freep(tid); srs_freep(null); srs_freep(stream_name); srs_freep(start); srs_freep(duration); srs_freep(reset); SrsPlayPacket* p = new SrsPlayPacket(); - SrsAutoFree(SrsPlayPacket, p); + SrsUniquePtr p_uptr(p); buf.skip(-1 * buf.pos()); HELPER_ASSERT_SUCCESS(p->decode(&buf)); @@ -2469,7 +2461,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) int nn = name->total_size() + tid->total_size() + null->total_size() + stream_name->total_size() + start->total_size() + duration->total_size() + reset->total_size(); char* b = new char[nn]; - SrsAutoFreeA(char, b); + SrsUniquePtr cp_uptr(b); SrsBuffer buf(b, nn); HELPER_ASSERT_SUCCESS(name->write(&buf)); @@ -2482,7 +2474,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) srs_freep(name); srs_freep(tid); srs_freep(null); srs_freep(stream_name); srs_freep(start); srs_freep(duration); srs_freep(reset); SrsPlayPacket* p = new SrsPlayPacket(); - SrsAutoFree(SrsPlayPacket, p); + SrsUniquePtr p_uptr(p); buf.skip(-1 * buf.pos()); HELPER_ASSERT_SUCCESS(p->decode(&buf)); @@ -2503,7 +2495,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) int nn = name->total_size() + tid->total_size() + null->total_size() + stream_name->total_size() + start->total_size() + duration->total_size() + reset->total_size(); char* b = new char[nn]; - SrsAutoFreeA(char, b); + SrsUniquePtr cp_uptr(b); SrsBuffer buf(b, nn); HELPER_ASSERT_SUCCESS(name->write(&buf)); @@ -2516,7 +2508,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) srs_freep(name); srs_freep(tid); srs_freep(null); srs_freep(stream_name); srs_freep(start); srs_freep(duration); srs_freep(reset); SrsPlayPacket* p = new SrsPlayPacket(); - SrsAutoFree(SrsPlayPacket, p); + SrsUniquePtr p_uptr(p); buf.skip(-1 * buf.pos()); HELPER_EXPECT_FAILED(p->decode(&buf)); @@ -2593,9 +2585,11 @@ VOID TEST(ProtocolRTMPTest, ConnectAppWithArgs) if (true) { tio.in_buffer.append(&io.out_buffer); - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); - SrsConnectAppPacket* pkt = NULL; SrsAutoFree(SrsConnectAppPacket, pkt); + SrsCommonMessage* msg = NULL; + SrsConnectAppPacket* pkt = NULL; HELPER_ASSERT_SUCCESS(p.expect_message(&msg, &pkt)); + SrsUniquePtr msg_uptr(msg); + SrsUniquePtr pkt_uptr(pkt); SrsAmf0Any* prop = pkt->command_object->get_property("tcUrl"); ASSERT_TRUE(prop && prop->is_string()); @@ -2624,8 +2618,9 @@ VOID TEST(ProtocolRTMPTest, AgentMessageCodec) } if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } } @@ -2640,11 +2635,13 @@ VOID TEST(ProtocolRTMPTest, AgentMessageCodec) } if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); - SrsPacket* pkt = NULL; SrsAutoFree(SrsPacket, pkt); + SrsPacket* pkt = NULL; HELPER_EXPECT_SUCCESS(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } } @@ -2659,8 +2656,9 @@ VOID TEST(ProtocolRTMPTest, AgentMessageCodec) } if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } } @@ -2675,11 +2673,13 @@ VOID TEST(ProtocolRTMPTest, AgentMessageCodec) } if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); - SrsPacket* pkt = NULL; SrsAutoFree(SrsPacket, pkt); + SrsPacket* pkt = NULL; HELPER_EXPECT_SUCCESS(p.decode_message(msg, &pkt)); + SrsUniquePtr pkt_uptr(pkt); } } } @@ -2689,7 +2689,7 @@ srs_error_t _mock_packet_to_shared_msg(SrsPacket* packet, int stream_id, SrsShar srs_error_t err = srs_success; SrsCommonMessage* msg = new SrsCommonMessage(); - SrsAutoFree(SrsCommonMessage, msg); + SrsUniquePtr msg_uptr(msg); if ((err = packet->to_msg(msg, stream_id)) != srs_success) { srs_freep(msg); @@ -2714,7 +2714,7 @@ VOID TEST(ProtocolRTMPTest, CheckStreamID) if (true) { SrsSharedPtrMessage* shared_msgs[2]; SrsConnectAppPacket* res = new SrsConnectAppPacket(); - SrsAutoFree(SrsConnectAppPacket, res); + SrsUniquePtr res_uptr(res); if (true) { SrsSharedPtrMessage* shared_msg = new SrsSharedPtrMessage(); @@ -2733,14 +2733,16 @@ VOID TEST(ProtocolRTMPTest, CheckStreamID) } if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); EXPECT_EQ(1, msg->header.stream_id); } if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); EXPECT_EQ(2, msg->header.stream_id); } } @@ -2765,8 +2767,9 @@ VOID TEST(ProtocolRTMPTest, AgentMessageTransform) } if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } } @@ -2785,8 +2788,9 @@ VOID TEST(ProtocolRTMPTest, AgentMessageTransform) } if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } } @@ -2805,8 +2809,9 @@ VOID TEST(ProtocolRTMPTest, AgentMessageTransform) } if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } } @@ -2825,8 +2830,9 @@ VOID TEST(ProtocolRTMPTest, AgentMessageTransform) } if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(p.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } } } @@ -2859,8 +2865,9 @@ VOID TEST(ProtocolRTMPTest, MergeReadHandler) r.set_merge_read(true, &h); if (true) { - SrsCommonMessage* msg = NULL; SrsAutoFree(SrsCommonMessage, msg); + SrsCommonMessage* msg = NULL; HELPER_EXPECT_SUCCESS(r.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); } EXPECT_TRUE(h.nn > 0); diff --git a/trunk/src/utest/srs_utest_service.cpp b/trunk/src/utest/srs_utest_service.cpp index 06e529f20c..06ad78c5a2 100644 --- a/trunk/src/utest/srs_utest_service.cpp +++ b/trunk/src/utest/srs_utest_service.cpp @@ -11,6 +11,7 @@ using namespace std; #include #include #include +#include #include #include @@ -635,8 +636,8 @@ VOID TEST(HTTPServerTest, ContentLength) io.append("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n"); SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE)); - ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); - SrsAutoFree(ISrsHttpMessage, msg); + ISrsHttpMessage* msg_raw = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg_raw)); + SrsUniquePtr msg(msg_raw); char buf[32]; ssize_t nread = 0; ISrsHttpResponseReader* r = msg->body_reader(); @@ -664,8 +665,8 @@ VOID TEST(HTTPServerTest, HTTPChunked) io.append("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE)); - ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); - SrsAutoFree(ISrsHttpMessage, msg); + ISrsHttpMessage* msg_raw = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg_raw)); + SrsUniquePtr msg(msg_raw); char buf[32]; ssize_t nread = 0; ISrsHttpResponseReader* r = msg->body_reader(); @@ -694,8 +695,8 @@ VOID TEST(HTTPServerTest, InfiniteChunked) io.append("HTTP/1.1 200 OK\r\n\r\n"); SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE)); - ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); - SrsAutoFree(ISrsHttpMessage, msg); + ISrsHttpMessage* msg_raw = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg_raw)); + SrsUniquePtr msg(msg_raw); char buf[32]; ssize_t nread = 0; ISrsHttpResponseReader* r = msg->body_reader(); @@ -721,8 +722,8 @@ VOID TEST(HTTPServerTest, InfiniteChunked) io.append("HTTP/1.1 200 OK\r\n\r\n"); SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE)); - ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); - SrsAutoFree(ISrsHttpMessage, msg); + ISrsHttpMessage* msg_raw = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg_raw)); + SrsUniquePtr msg(msg_raw); char buf[32]; ssize_t nread = 0; ISrsHttpResponseReader* r = msg->body_reader(); @@ -750,8 +751,8 @@ VOID TEST(HTTPServerTest, OPTIONSRead) io.append("OPTIONS /rtc/v1/play HTTP/1.1\r\n\r\n"); SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST)); - ISrsHttpMessage* req = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req)); - SrsAutoFree(ISrsHttpMessage, req); + ISrsHttpMessage* req_raw = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req_raw)); + SrsUniquePtr req(req_raw); ISrsHttpResponseReader* br = req->body_reader(); EXPECT_TRUE(br->eof()); @@ -763,8 +764,8 @@ VOID TEST(HTTPServerTest, OPTIONSRead) io.append("HTTP/1.1 200 OK\r\n\r\n"); SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE)); - ISrsHttpMessage* req = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req)); - SrsAutoFree(ISrsHttpMessage, req); + ISrsHttpMessage* req_raw = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req_raw)); + SrsUniquePtr req(req_raw); ISrsHttpResponseReader* br = req->body_reader(); EXPECT_FALSE(br->eof()); @@ -776,8 +777,8 @@ VOID TEST(HTTPServerTest, OPTIONSRead) io.append("OPTIONS /rtc/v1/play HTTP/1.1\r\nContent-Length: 5\r\n\r\nHello"); SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST)); - ISrsHttpMessage* req = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req)); - SrsAutoFree(ISrsHttpMessage, req); + ISrsHttpMessage* req_raw = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req_raw)); + SrsUniquePtr req(req_raw); ISrsHttpResponseReader* br = req->body_reader(); EXPECT_FALSE(br->eof()); @@ -787,8 +788,8 @@ VOID TEST(HTTPServerTest, OPTIONSRead) // The body will use as next HTTP request message. io.append("GET /rtc/v1/play HTTP/1.1\r\n\r\n"); - ISrsHttpMessage* req2 = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req2)); - SrsAutoFree(ISrsHttpMessage, req2); + ISrsHttpMessage* req2_raw = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req2_raw)); + SrsUniquePtr req2(req2_raw); } // So if OPTIONS has body, but not specified the size, we think it has no body, @@ -798,16 +799,16 @@ VOID TEST(HTTPServerTest, OPTIONSRead) io.append("OPTIONS /rtc/v1/play HTTP/1.1\r\n\r\n"); SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST)); - ISrsHttpMessage* req = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req)); - SrsAutoFree(ISrsHttpMessage, req); + ISrsHttpMessage* req_raw = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req_raw)); + SrsUniquePtr req(req_raw); ISrsHttpResponseReader* br = req->body_reader(); EXPECT_TRUE(br->eof()); // The body will use as next HTTP request message. io.append("Hello"); - ISrsHttpMessage* req2 = NULL; HELPER_ASSERT_FAILED(hp.parse_message(&io, &req2)); - SrsAutoFree(ISrsHttpMessage, req2); + ISrsHttpMessage* req2_raw = NULL; HELPER_ASSERT_FAILED(hp.parse_message(&io, &req2_raw)); + SrsUniquePtr req2(req2_raw); } } @@ -1320,8 +1321,8 @@ VOID TEST(HTTPClientTest, HTTPClientUtility) HELPER_ASSERT_SUCCESS(client.initialize("http", "127.0.0.1", 8080, 1*SRS_UTIME_SECONDS)); ISrsHttpMessage* res = NULL; - SrsAutoFree(ISrsHttpMessage, res); HELPER_ASSERT_SUCCESS(client.post("/api/v1", "", &res)); + SrsUniquePtr res_uptr(res); ISrsHttpResponseReader* br = res->body_reader(); ASSERT_FALSE(br->eof()); @@ -1342,8 +1343,8 @@ VOID TEST(HTTPClientTest, HTTPClientUtility) HELPER_ASSERT_SUCCESS(client.initialize("http", "127.0.0.1", 8080, 1*SRS_UTIME_SECONDS)); ISrsHttpMessage* res = NULL; - SrsAutoFree(ISrsHttpMessage, res); HELPER_ASSERT_SUCCESS(client.get("/api/v1", "", &res)); + SrsUniquePtr res_uptr(res); ISrsHttpResponseReader* br = res->body_reader(); ASSERT_FALSE(br->eof()); @@ -1366,8 +1367,8 @@ VOID TEST(HTTPClientTest, HTTPClientUtility) client.set_header("agent", "srs"); ISrsHttpMessage* res = NULL; - SrsAutoFree(ISrsHttpMessage, res); HELPER_ASSERT_SUCCESS(client.get("/api/v1", "", &res)); + SrsUniquePtr res_uptr(res); ISrsHttpResponseReader* br = res->body_reader(); ASSERT_FALSE(br->eof()); diff --git a/trunk/src/utest/srs_utest_srt.cpp b/trunk/src/utest/srs_utest_srt.cpp index d8f458771c..9035403c2a 100644 --- a/trunk/src/utest/srs_utest_srt.cpp +++ b/trunk/src/utest/srs_utest_srt.cpp @@ -195,8 +195,7 @@ VOID TEST(ServiceStSRTTest, ListenConnectAccept) srs_srt_t srt_client_fd = srs_srt_socket_invalid(); HELPER_EXPECT_SUCCESS(srs_srt_socket(&srt_client_fd)); - SrsSrtSocket* srt_client_socket = new SrsSrtSocket(_srt_eventloop->poller(), srt_client_fd); - SrsAutoFree(SrsSrtSocket, srt_client_socket); + SrsUniquePtr srt_client_socket(new SrsSrtSocket(_srt_eventloop->poller(), srt_client_fd)); // No client connected, accept will timeout. srs_srt_t srt_fd = srs_srt_socket_invalid();