From 9e62e6872ace197539c83654e82299ea5bbbe81e Mon Sep 17 00:00:00 2001 From: xufuji456 Date: Tue, 20 Sep 2022 11:04:07 +0800 Subject: [PATCH] Feature: check if encoder open success or not --- Live/src/main/cpp/AudioStream.cpp | 30 ++++++++++++++---------------- Live/src/main/cpp/AudioStream.h | 2 +- Live/src/main/cpp/PushInterface.h | 18 ++++++++++++++++++ Live/src/main/cpp/RtmpPusher.cpp | 28 ++++++++-------------------- Live/src/main/cpp/VideoStream.cpp | 30 +++++++++++++++++++++--------- Live/src/main/cpp/VideoStream.h | 2 +- 6 files changed, 63 insertions(+), 47 deletions(-) diff --git a/Live/src/main/cpp/AudioStream.cpp b/Live/src/main/cpp/AudioStream.cpp index 37dbd7b..459b3ff 100644 --- a/Live/src/main/cpp/AudioStream.cpp +++ b/Live/src/main/cpp/AudioStream.cpp @@ -20,24 +20,22 @@ void AudioStream::setAudioCallback(AudioCallback callback) { this->audioCallback = callback; } -void AudioStream::setAudioEncInfo(int samplesInHZ, int channels) { +int AudioStream::setAudioEncInfo(int samplesInHZ, int channels) { mChannels = channels; //open faac encoder audioCodec = faacEncOpen(static_cast(samplesInHZ), static_cast(channels), &inputSamples, &maxOutputBytes); + buffer = new u_char[maxOutputBytes]; //set encoder params faacEncConfigurationPtr config = faacEncGetCurrentConfiguration(audioCodec); - config->mpegVersion = MPEG4; + config->mpegVersion = MPEG4; config->aacObjectType = LOW; - config->inputFormat = FAAC_INPUT_16BIT; - config->outputFormat = 0; - faacEncSetConfiguration(audioCodec, config); - - //output buffer - buffer = new u_char[maxOutputBytes]; + config->inputFormat = FAAC_INPUT_16BIT; + config->outputFormat = 0; + return faacEncSetConfiguration(audioCodec, config); } int AudioStream::getInputSamples() const { @@ -61,10 +59,10 @@ RTMPPacket *AudioStream::getAudioTag() { memcpy(&packet->m_body[2], buf, len); packet->m_hasAbsTimestamp = 0; - packet->m_nBodySize = bodySize; - packet->m_packetType = RTMP_PACKET_TYPE_AUDIO; - packet->m_nChannel = 0x11; - packet->m_headerType = RTMP_PACKET_SIZE_LARGE; + packet->m_nBodySize = bodySize; + packet->m_packetType = RTMP_PACKET_TYPE_AUDIO; + packet->m_nChannel = 0x11; + packet->m_headerType = RTMP_PACKET_SIZE_LARGE; return packet; } @@ -88,10 +86,10 @@ void AudioStream::encodeData(int8_t *data) { memcpy(&packet->m_body[2], buffer, static_cast(byteLen)); packet->m_hasAbsTimestamp = 0; - packet->m_nBodySize = bodySize; - packet->m_packetType = RTMP_PACKET_TYPE_AUDIO; - packet->m_nChannel = 0x11; - packet->m_headerType = RTMP_PACKET_SIZE_LARGE; + packet->m_nBodySize = bodySize; + packet->m_packetType = RTMP_PACKET_TYPE_AUDIO; + packet->m_nChannel = 0x11; + packet->m_headerType = RTMP_PACKET_SIZE_LARGE; audioCallback(packet); } } diff --git a/Live/src/main/cpp/AudioStream.h b/Live/src/main/cpp/AudioStream.h index c246ddb..474262a 100644 --- a/Live/src/main/cpp/AudioStream.h +++ b/Live/src/main/cpp/AudioStream.h @@ -14,7 +14,7 @@ public: ~AudioStream(); - void setAudioEncInfo(int samplesInHZ, int channels); + int setAudioEncInfo(int samplesInHZ, int channels); void setAudioCallback(AudioCallback audioCallback); diff --git a/Live/src/main/cpp/PushInterface.h b/Live/src/main/cpp/PushInterface.h index f930545..43b6ab5 100644 --- a/Live/src/main/cpp/PushInterface.h +++ b/Live/src/main/cpp/PushInterface.h @@ -7,4 +7,22 @@ #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,"FrankLive",__VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,"FrankLive",__VA_ARGS__) +/***************relative to Java**************/ +//error code for opening video encoder +const int ERROR_VIDEO_ENCODER_OPEN = 0x01; +//error code for video encoding +const int ERROR_VIDEO_ENCODE = 0x02; +//error code for opening audio encoder +const int ERROR_AUDIO_ENCODER_OPEN = 0x03; +//error code for audio encoding +const int ERROR_AUDIO_ENCODE = 0x04; +//error code for RTMP connecting +const int ERROR_RTMP_CONNECT = 0x05; +//error code for connecting stream +const int ERROR_RTMP_CONNECT_STREAM = 0x06; +//error code for sending packet +const int ERROR_RTMP_SEND_PACKET = 0x07; + +/***************relative to Java**************/ + #endif diff --git a/Live/src/main/cpp/RtmpPusher.cpp b/Live/src/main/cpp/RtmpPusher.cpp index 6b9225d..5609cfb 100644 --- a/Live/src/main/cpp/RtmpPusher.cpp +++ b/Live/src/main/cpp/RtmpPusher.cpp @@ -22,24 +22,6 @@ JavaVM *javaVM; //callback object jobject jobject_error; -/***************relative to Java**************/ -//error code for opening video encoder -const int ERROR_VIDEO_ENCODER_OPEN = 0x01; -//error code for video encoding -const int ERROR_VIDEO_ENCODE = 0x02; -//error code for opening audio encoder -const int ERROR_AUDIO_ENCODER_OPEN = 0x03; -//error code for audio encoding -const int ERROR_AUDIO_ENCODE = 0x04; -//error code for RTMP connecting -const int ERROR_RTMP_CONNECT = 0x05; -//error code for connecting stream -const int ERROR_RTMP_CONNECT_STREAM = 0x06; -//error code for sending packet -const int ERROR_RTMP_SEND_PACKET = 0x07; - -/***************relative to Java**************/ - //when calling System.loadLibrary, will callback it jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { javaVM = vm; @@ -152,7 +134,10 @@ RTMP_PUSHER_FUNC(void, native_1init) { RTMP_PUSHER_FUNC(void, native_1setVideoCodecInfo, jint width, jint height, jint fps, jint bitrate) { if (videoStream) { - videoStream->setVideoEncInfo(width, height, fps, bitrate); + int ret = videoStream->setVideoEncInfo(width, height, fps, bitrate); + if (ret < 0) { + throwErrToJava(ERROR_VIDEO_ENCODER_OPEN); + } } } @@ -181,7 +166,10 @@ RTMP_PUSHER_FUNC(void, native_1pushVideo, jbyteArray yuv, jint camera_type) { RTMP_PUSHER_FUNC(void, native_1setAudioCodecInfo, jint sampleRateInHz, jint channels) { if (audioStream) { - audioStream->setAudioEncInfo(sampleRateInHz, channels); + int ret = audioStream->setAudioEncInfo(sampleRateInHz, channels); + if (ret < 0) { + throwErrToJava(ERROR_AUDIO_ENCODER_OPEN); + } } } diff --git a/Live/src/main/cpp/VideoStream.cpp b/Live/src/main/cpp/VideoStream.cpp index 5fe6ff7..2310979 100644 --- a/Live/src/main/cpp/VideoStream.cpp +++ b/Live/src/main/cpp/VideoStream.cpp @@ -23,7 +23,7 @@ VideoStream::~VideoStream() { } } -void VideoStream::setVideoEncInfo(int width, int height, int fps, int bitrate) { +int VideoStream::setVideoEncInfo(int width, int height, int fps, int bitrate) { pthread_mutex_lock(&mutex); m_frameLen = width * height; if (videoCodec) { @@ -38,7 +38,10 @@ void VideoStream::setVideoEncInfo(int width, int height, int fps, int bitrate) { //setting x264 params x264_param_t param; - x264_param_default_preset(¶m, "ultrafast", "zerolatency"); + int ret = x264_param_default_preset(¶m, "ultrafast", "zerolatency"); + if (ret < 0) { + goto end; + } param.i_level_idc = 32; //input format param.i_csp = X264_CSP_I420; @@ -69,12 +72,21 @@ void VideoStream::setVideoEncInfo(int width, int height, int fps, int bitrate) { //thread number param.i_threads = 1; - x264_param_apply_profile(¶m, "baseline"); + ret = x264_param_apply_profile(¶m, "baseline"); + if (ret < 0) { + goto end; + } //open encoder videoCodec = x264_encoder_open(¶m); + if (!videoCodec) { + ret = -1; + goto end; + } pic_in = new x264_picture_t(); x264_picture_alloc(pic_in, X264_CSP_I420, width, height); +end: pthread_mutex_unlock(&mutex); + return ret; } void VideoStream::setVideoCallback(VideoCallback callback) { @@ -162,8 +174,8 @@ void VideoStream::sendSpsPps(uint8_t *sps, uint8_t *pps, int sps_len, int pps_le //video packet->m_packetType = RTMP_PACKET_TYPE_VIDEO; - packet->m_nBodySize = bodySize; - packet->m_nChannel = 10; + packet->m_nBodySize = bodySize; + packet->m_nChannel = 10; //sps and pps no timestamp packet->m_nTimeStamp = 0; packet->m_hasAbsTimestamp = 0; @@ -203,9 +215,9 @@ void VideoStream::sendFrame(int type, uint8_t *payload, int i_payload) { memcpy(&packet->m_body[9], payload, static_cast(i_payload)); packet->m_hasAbsTimestamp = 0; - packet->m_nBodySize = bodySize; - packet->m_packetType = RTMP_PACKET_TYPE_VIDEO; - packet->m_nChannel = 0x10; - packet->m_headerType = RTMP_PACKET_SIZE_LARGE; + packet->m_nBodySize = bodySize; + packet->m_packetType = RTMP_PACKET_TYPE_VIDEO; + packet->m_nChannel = 0x10; + packet->m_headerType = RTMP_PACKET_SIZE_LARGE; videoCallback(packet); } diff --git a/Live/src/main/cpp/VideoStream.h b/Live/src/main/cpp/VideoStream.h index 80aad3d..e0336c0 100644 --- a/Live/src/main/cpp/VideoStream.h +++ b/Live/src/main/cpp/VideoStream.h @@ -15,7 +15,7 @@ public: ~VideoStream(); - void setVideoEncInfo(int width, int height, int fps, int bitrate); + int setVideoEncInfo(int width, int height, int fps, int bitrate); void encodeVideo(int8_t *data, int camera_type);