From 9a990b5e175d65da5ad5e074ed4f09d27a6da981 Mon Sep 17 00:00:00 2001 From: xufuji456 Date: Thu, 22 Sep 2022 10:25:59 +0800 Subject: [PATCH] Feature: adjust the order --- Live/src/main/cpp/AudioStream.cpp | 52 +++++++++++++++---------------- Live/src/main/cpp/AudioStream.h | 15 ++++----- Live/src/main/cpp/PacketQueue.h | 38 +++++++++++----------- Live/src/main/cpp/VideoStream.cpp | 24 +++++++------- Live/src/main/cpp/VideoStream.h | 24 +++++++------- 5 files changed, 78 insertions(+), 75 deletions(-) diff --git a/Live/src/main/cpp/AudioStream.cpp b/Live/src/main/cpp/AudioStream.cpp index 459b3ff..f4a58a0 100644 --- a/Live/src/main/cpp/AudioStream.cpp +++ b/Live/src/main/cpp/AudioStream.cpp @@ -7,51 +7,42 @@ AudioStream::AudioStream() { } -AudioStream::~AudioStream() { - delete buffer; - buffer = nullptr; - if (audioCodec) { - faacEncClose(audioCodec); - audioCodec = nullptr; - } -} - void AudioStream::setAudioCallback(AudioCallback callback) { - this->audioCallback = callback; + audioCallback = callback; } int AudioStream::setAudioEncInfo(int samplesInHZ, int channels) { - mChannels = channels; + m_channels = channels; //open faac encoder - audioCodec = faacEncOpen(static_cast(samplesInHZ), + m_audioCodec = faacEncOpen(static_cast(samplesInHZ), static_cast(channels), - &inputSamples, - &maxOutputBytes); - buffer = new u_char[maxOutputBytes]; + &m_inputSamples, + &m_maxOutputBytes); + m_buffer = new u_char[m_maxOutputBytes]; //set encoder params - faacEncConfigurationPtr config = faacEncGetCurrentConfiguration(audioCodec); + faacEncConfigurationPtr config = faacEncGetCurrentConfiguration(m_audioCodec); config->mpegVersion = MPEG4; config->aacObjectType = LOW; config->inputFormat = FAAC_INPUT_16BIT; config->outputFormat = 0; - return faacEncSetConfiguration(audioCodec, config); + return faacEncSetConfiguration(m_audioCodec, config); } int AudioStream::getInputSamples() const { - return static_cast(inputSamples); + return static_cast(m_inputSamples); } RTMPPacket *AudioStream::getAudioTag() { u_char *buf; u_long len; - faacEncGetDecoderSpecificInfo(audioCodec, &buf, &len); + faacEncGetDecoderSpecificInfo(m_audioCodec, &buf, &len); int bodySize = static_cast(2 + len); auto *packet = new RTMPPacket(); RTMPPacket_Alloc(packet, bodySize); //channel layout: stereo packet->m_body[0] = 0xAF; - if (mChannels == 1) { + if (m_channels == 1) { packet->m_body[0] = 0xAE; } packet->m_body[1] = 0x00; @@ -68,22 +59,22 @@ RTMPPacket *AudioStream::getAudioTag() { void AudioStream::encodeData(int8_t *data) { //encode a frame, and return encoded len - int byteLen = faacEncEncode(audioCodec, reinterpret_cast(data), - static_cast(inputSamples), - buffer, - static_cast(maxOutputBytes)); + int byteLen = faacEncEncode(m_audioCodec, reinterpret_cast(data), + static_cast(m_inputSamples), + m_buffer, + static_cast(m_maxOutputBytes)); if (byteLen > 0) { int bodySize = 2 + byteLen; auto *packet = new RTMPPacket(); RTMPPacket_Alloc(packet, bodySize); //stereo packet->m_body[0] = 0xAF; - if (mChannels == 1) { + if (m_channels == 1) { packet->m_body[0] = 0xAE; } packet->m_body[1] = 0x01; - memcpy(&packet->m_body[2], buffer, static_cast(byteLen)); + memcpy(&packet->m_body[2], m_buffer, static_cast(byteLen)); packet->m_hasAbsTimestamp = 0; packet->m_nBodySize = bodySize; @@ -94,3 +85,12 @@ void AudioStream::encodeData(int8_t *data) { } } +AudioStream::~AudioStream() { + delete m_buffer; + m_buffer = nullptr; + if (m_audioCodec) { + faacEncClose(m_audioCodec); + m_audioCodec = nullptr; + } +} + diff --git a/Live/src/main/cpp/AudioStream.h b/Live/src/main/cpp/AudioStream.h index 474262a..70b64b1 100644 --- a/Live/src/main/cpp/AudioStream.h +++ b/Live/src/main/cpp/AudioStream.h @@ -9,6 +9,14 @@ class AudioStream { typedef void (*AudioCallback)(RTMPPacket *packet); +private: + AudioCallback audioCallback; + int m_channels; + faacEncHandle m_audioCodec = 0; + u_long m_inputSamples; + u_long m_maxOutputBytes; + u_char *m_buffer = 0; + public: AudioStream(); @@ -24,13 +32,6 @@ public: RTMPPacket *getAudioTag(); -private: - AudioCallback audioCallback; - int mChannels; - faacEncHandle audioCodec = 0; - u_long inputSamples; - u_long maxOutputBytes; - u_char *buffer = 0; }; diff --git a/Live/src/main/cpp/PacketQueue.h b/Live/src/main/cpp/PacketQueue.h index b2c3ac5..f43a81f 100644 --- a/Live/src/main/cpp/PacketQueue.h +++ b/Live/src/main/cpp/PacketQueue.h @@ -9,6 +9,14 @@ template class PacketQueue { typedef void (*ReleaseCallback)(T &); +private: + std::mutex m_mutex; + std::condition_variable m_cond; + std::queue m_queue; + bool m_running; + + ReleaseCallback releaseCallback; + public: void push(T new_value) { @@ -33,12 +41,22 @@ public: return ret; } + void clear() { + std::lock_guard lock(m_mutex); + int size = m_queue.size(); + for (int i = 0; i < size; ++i) { + T value = m_queue.front(); + releaseCallback(value); + m_queue.pop(); + } + } + void setRunning(bool run) { std::lock_guard lock(m_mutex); m_running = run; } - int empty() { + bool empty() { std::lock_guard lock(m_mutex); return m_queue.empty(); } @@ -48,28 +66,10 @@ public: return static_cast(m_queue.size()); } - void clear() { - std::lock_guard lock(m_mutex); - int size = m_queue.size(); - for (int i = 0; i < size; ++i) { - T value = m_queue.front(); - releaseCallback(value); - m_queue.pop(); - } - } - void setReleaseCallback(ReleaseCallback callback) { releaseCallback = callback; } -private: - - std::mutex m_mutex; - std::condition_variable m_cond; - std::queue m_queue; - bool m_running; - - ReleaseCallback releaseCallback; }; #endif // PACKET_QUEUE_H diff --git a/Live/src/main/cpp/VideoStream.cpp b/Live/src/main/cpp/VideoStream.cpp index 24e6125..e11f8ee 100644 --- a/Live/src/main/cpp/VideoStream.cpp +++ b/Live/src/main/cpp/VideoStream.cpp @@ -10,18 +10,6 @@ VideoStream::VideoStream():m_frameLen(0), } -VideoStream::~VideoStream() { - if (videoCodec) { - x264_encoder_close(videoCodec); - videoCodec = nullptr; - } - if (pic_in) { - x264_picture_clean(pic_in); - delete pic_in; - pic_in = nullptr; - } -} - int VideoStream::setVideoEncInfo(int width, int height, int fps, int bitrate) { std::lock_guard l(m_mutex); m_frameLen = width * height; @@ -216,3 +204,15 @@ void VideoStream::sendFrame(int type, uint8_t *payload, int i_payload) { packet->m_headerType = RTMP_PACKET_SIZE_LARGE; videoCallback(packet); } + +VideoStream::~VideoStream() { + if (videoCodec) { + x264_encoder_close(videoCodec); + videoCodec = nullptr; + } + if (pic_in) { + x264_picture_clean(pic_in); + delete pic_in; + pic_in = nullptr; + } +} diff --git a/Live/src/main/cpp/VideoStream.h b/Live/src/main/cpp/VideoStream.h index 3737719..7c2bbb0 100644 --- a/Live/src/main/cpp/VideoStream.h +++ b/Live/src/main/cpp/VideoStream.h @@ -10,28 +10,30 @@ class VideoStream { typedef void (*VideoCallback)(RTMPPacket *packet); -public: - VideoStream(); - - ~VideoStream(); - - int setVideoEncInfo(int width, int height, int fps, int bitrate); - - void encodeVideo(int8_t *data, int camera_type); - - void setVideoCallback(VideoCallback videoCallback); - private: std::mutex m_mutex; int m_frameLen; x264_t *videoCodec = 0; x264_picture_t *pic_in = 0; + VideoCallback videoCallback; void sendSpsPps(uint8_t *sps, uint8_t *pps, int sps_len, int pps_len); void sendFrame(int type, uint8_t *payload, int i_payload); + +public: + VideoStream(); + + ~VideoStream(); + + int setVideoEncInfo(int width, int height, int fps, int bitrate); + + void encodeVideo(int8_t *data, int camera_type); + + void setVideoCallback(VideoCallback videoCallback); + }; #endif