From 705f9ba572a8e7b206ff996673e0ffe647721507 Mon Sep 17 00:00:00 2001 From: xufuji456 Date: Tue, 20 Sep 2022 10:21:12 +0800 Subject: [PATCH] Feature: change to c++ thread --- Live/src/main/cpp/AudioStream.cpp | 4 ++-- Live/src/main/cpp/RtmpPusher.cpp | 13 ++++++------- Live/src/main/cpp/VideoStream.cpp | 6 +++--- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Live/src/main/cpp/AudioStream.cpp b/Live/src/main/cpp/AudioStream.cpp index edfd904..37dbd7b 100644 --- a/Live/src/main/cpp/AudioStream.cpp +++ b/Live/src/main/cpp/AudioStream.cpp @@ -49,7 +49,7 @@ RTMPPacket *AudioStream::getAudioTag() { u_long len; faacEncGetDecoderSpecificInfo(audioCodec, &buf, &len); int bodySize = static_cast(2 + len); - auto *packet = new RTMPPacket; + auto *packet = new RTMPPacket(); RTMPPacket_Alloc(packet, bodySize); //channel layout: stereo packet->m_body[0] = 0xAF; @@ -76,7 +76,7 @@ void AudioStream::encodeData(int8_t *data) { static_cast(maxOutputBytes)); if (byteLen > 0) { int bodySize = 2 + byteLen; - auto *packet = new RTMPPacket; + auto *packet = new RTMPPacket(); RTMPPacket_Alloc(packet, bodySize); //stereo packet->m_body[0] = 0xAF; diff --git a/Live/src/main/cpp/RtmpPusher.cpp b/Live/src/main/cpp/RtmpPusher.cpp index 13416e8..6b9225d 100644 --- a/Live/src/main/cpp/RtmpPusher.cpp +++ b/Live/src/main/cpp/RtmpPusher.cpp @@ -12,13 +12,11 @@ PacketQueue packets; VideoStream *videoStream = nullptr; -pthread_t pid; +AudioStream *audioStream = nullptr; std::atomic isPushing; uint32_t start_time; -AudioStream *audioStream = nullptr; - //use to get thread's JNIEnv JavaVM *javaVM; //callback object @@ -143,9 +141,9 @@ void *start(void *args) { RTMP_PUSHER_FUNC(void, native_1init) { LOGI("native init..."); - videoStream = new VideoStream; + videoStream = new VideoStream(); videoStream->setVideoCallback(callback); - audioStream = new AudioStream; + audioStream = new AudioStream(); audioStream->setAudioCallback(callback); packets.setReleaseCallback(releasePackets); jobject_error = env->NewGlobalRef(instance); @@ -166,7 +164,9 @@ RTMP_PUSHER_FUNC(void, native_1start, jstring path_) { const char *path = env->GetStringUTFChars(path_, nullptr); char *url = new char[strlen(path) + 1]; strcpy(url, path); - pthread_create(&pid, nullptr, start, url); + + std::thread pushThread(start, url); + pushThread.detach(); env->ReleaseStringUTFChars(path_, path); } @@ -205,7 +205,6 @@ RTMP_PUSHER_FUNC(void, native_1stop) { LOGI("native stop..."); isPushing = false; packets.setRunning(false); - pthread_join(pid, nullptr); } RTMP_PUSHER_FUNC(void, native_1release) { diff --git a/Live/src/main/cpp/VideoStream.cpp b/Live/src/main/cpp/VideoStream.cpp index dade40a..5fe6ff7 100644 --- a/Live/src/main/cpp/VideoStream.cpp +++ b/Live/src/main/cpp/VideoStream.cpp @@ -72,7 +72,7 @@ void VideoStream::setVideoEncInfo(int width, int height, int fps, int bitrate) { x264_param_apply_profile(¶m, "baseline"); //open encoder videoCodec = x264_encoder_open(¶m); - pic_in = new x264_picture_t; + pic_in = new x264_picture_t(); x264_picture_alloc(pic_in, X264_CSP_I420, width, height); pthread_mutex_unlock(&mutex); } @@ -128,7 +128,7 @@ void VideoStream::encodeVideo(int8_t *data, int camera_type) { void VideoStream::sendSpsPps(uint8_t *sps, uint8_t *pps, int sps_len, int pps_len) { int bodySize = 13 + sps_len + 3 + pps_len; - auto *packet = new RTMPPacket; + auto *packet = new RTMPPacket(); RTMPPacket_Alloc(packet, bodySize); int i = 0; //start code @@ -181,7 +181,7 @@ void VideoStream::sendFrame(int type, uint8_t *payload, int i_payload) { payload += 3; } int bodySize = 9 + i_payload; - auto *packet = new RTMPPacket; + auto *packet = new RTMPPacket(); RTMPPacket_Alloc(packet, bodySize); packet->m_body[0] = 0x27;