Feature: check if encoder open success or not

pull/221/head
xufuji456 2 years ago
parent 80fabc2009
commit 9e62e6872a
  1. 30
      Live/src/main/cpp/AudioStream.cpp
  2. 2
      Live/src/main/cpp/AudioStream.h
  3. 18
      Live/src/main/cpp/PushInterface.h
  4. 28
      Live/src/main/cpp/RtmpPusher.cpp
  5. 30
      Live/src/main/cpp/VideoStream.cpp
  6. 2
      Live/src/main/cpp/VideoStream.h

@ -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<unsigned long>(samplesInHZ),
static_cast<unsigned int>(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<size_t>(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);
}
}

@ -14,7 +14,7 @@ public:
~AudioStream();
void setAudioEncInfo(int samplesInHZ, int channels);
int setAudioEncInfo(int samplesInHZ, int channels);
void setAudioCallback(AudioCallback audioCallback);

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

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

@ -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(&param, "ultrafast", "zerolatency");
int ret = x264_param_default_preset(&param, "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(&param, "baseline");
ret = x264_param_apply_profile(&param, "baseline");
if (ret < 0) {
goto end;
}
//open encoder
videoCodec = x264_encoder_open(&param);
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<size_t>(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);
}

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

Loading…
Cancel
Save