Feature: adjust the order

pull/221/head
xufuji456 2 years ago
parent a69b72c4e2
commit 9a990b5e17
  1. 52
      Live/src/main/cpp/AudioStream.cpp
  2. 15
      Live/src/main/cpp/AudioStream.h
  3. 38
      Live/src/main/cpp/PacketQueue.h
  4. 24
      Live/src/main/cpp/VideoStream.cpp
  5. 24
      Live/src/main/cpp/VideoStream.h

@ -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<unsigned long>(samplesInHZ),
m_audioCodec = faacEncOpen(static_cast<unsigned long>(samplesInHZ),
static_cast<unsigned int>(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<int>(inputSamples);
return static_cast<int>(m_inputSamples);
}
RTMPPacket *AudioStream::getAudioTag() {
u_char *buf;
u_long len;
faacEncGetDecoderSpecificInfo(audioCodec, &buf, &len);
faacEncGetDecoderSpecificInfo(m_audioCodec, &buf, &len);
int bodySize = static_cast<int>(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<int32_t *>(data),
static_cast<unsigned int>(inputSamples),
buffer,
static_cast<unsigned int>(maxOutputBytes));
int byteLen = faacEncEncode(m_audioCodec, reinterpret_cast<int32_t *>(data),
static_cast<unsigned int>(m_inputSamples),
m_buffer,
static_cast<unsigned int>(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<size_t>(byteLen));
memcpy(&packet->m_body[2], m_buffer, static_cast<size_t>(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;
}
}

@ -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;
};

@ -9,6 +9,14 @@ template<typename T>
class PacketQueue {
typedef void (*ReleaseCallback)(T &);
private:
std::mutex m_mutex;
std::condition_variable m_cond;
std::queue<T> 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<std::mutex> 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<std::mutex> lock(m_mutex);
m_running = run;
}
int empty() {
bool empty() {
std::lock_guard<std::mutex> lock(m_mutex);
return m_queue.empty();
}
@ -48,28 +66,10 @@ public:
return static_cast<int>(m_queue.size());
}
void clear() {
std::lock_guard<std::mutex> 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<T> m_queue;
bool m_running;
ReleaseCallback releaseCallback;
};
#endif // PACKET_QUEUE_H

@ -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<std::mutex> 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;
}
}

@ -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

Loading…
Cancel
Save