diff --git a/Live/src/main/cpp/safe_queue.h b/Live/src/main/cpp/PacketQueue.h similarity index 87% rename from Live/src/main/cpp/safe_queue.h rename to Live/src/main/cpp/PacketQueue.h index 5a619d9..ac62817 100644 --- a/Live/src/main/cpp/safe_queue.h +++ b/Live/src/main/cpp/PacketQueue.h @@ -8,34 +8,25 @@ using namespace std; template -class SafeQueue { +class PacketQueue { typedef void (*ReleaseCallback)(T &); typedef void (*SyncHandle)(queue &); public: - SafeQueue() { - - } - - ~SafeQueue() { - - } void push(T new_value) { lock_guard lk(mt); - if (work) { + if (running) { q.push(new_value); cv.notify_one(); } } - int pop(T &value) { int ret = 0; - unique_lock lk(mt); - if (!work) { + if (!running) { return ret; } if (!q.empty()) { @@ -46,9 +37,9 @@ public: return ret; } - void setWork(int run) { + void setRunning(bool run) { lock_guard lk(mt); - this->work = run; + this->running = run; } int empty() { @@ -84,7 +75,7 @@ private: condition_variable cv; queue q; - int work; + bool running; ReleaseCallback releaseCallback; SyncHandle syncHandle; }; diff --git a/Live/src/main/cpp/RtmpPusher.cpp b/Live/src/main/cpp/RtmpPusher.cpp index ffb5775..1ac9cee 100644 --- a/Live/src/main/cpp/RtmpPusher.cpp +++ b/Live/src/main/cpp/RtmpPusher.cpp @@ -1,6 +1,6 @@ #include #include -#include "safe_queue.h" +#include "PacketQueue.h" #include "PushInterface.h" #include "VideoStream.h" #include "AudioStream.h" @@ -10,12 +10,12 @@ JNIEXPORT RETURN_TYPE JNICALL Java_com_frank_live_LivePusherNew_ ## FUNC_NAME \ (JNIEnv *env, jobject instance, ##__VA_ARGS__)\ -SafeQueue packets; +PacketQueue packets; VideoStream *videoStream = nullptr; int isStart = 0; pthread_t pid; -int readyPushing = 0; +std::atomic isPushing; uint32_t start_time; AudioStream *audioStream = nullptr; @@ -107,13 +107,13 @@ void *start(void *args) { //start time start_time = RTMP_GetTime(); //start pushing - readyPushing = 1; - packets.setWork(1); + isPushing = true; + packets.setRunning(true); callback(audioStream->getAudioTag()); RTMPPacket *packet = nullptr; - while (readyPushing) { + while (isPushing) { packets.pop(packet); - if (!readyPushing) { + if (!isPushing) { break; } if (!packet) { @@ -132,8 +132,8 @@ void *start(void *args) { releasePackets(packet); } while (0); isStart = 0; - readyPushing = 0; - packets.setWork(0); + isPushing = false; + packets.setRunning(false); packets.clear(); if (rtmp) { RTMP_Close(rtmp); @@ -174,7 +174,7 @@ RTMP_PUSHER_FUNC(void, native_1start, jstring path_) { } RTMP_PUSHER_FUNC(void, native_1pushVideo, jbyteArray yuv, jint camera_type) { - if (!videoStream || !readyPushing) { + if (!videoStream || !isPushing) { return; } jbyte *yuv_plane = env->GetByteArrayElements(yuv, JNI_FALSE); @@ -196,7 +196,7 @@ RTMP_PUSHER_FUNC(jint, native_1getInputSamples) { } RTMP_PUSHER_FUNC(void, native_1pushAudio, jbyteArray data_) { - if (!audioStream || !readyPushing) { + if (!audioStream || !isPushing) { return; } jbyte *data = env->GetByteArrayElements(data_, nullptr); @@ -206,8 +206,8 @@ RTMP_PUSHER_FUNC(void, native_1pushAudio, jbyteArray data_) { RTMP_PUSHER_FUNC(void, native_1stop) { LOGI("native stop..."); - readyPushing = 0; - packets.setWork(0); + isPushing = false; + packets.setRunning(false); pthread_join(pid, nullptr); }