Skip to content

Commit

Permalink
* fix rtmp bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed Nov 28, 2024
1 parent 9dce034 commit ee01c8f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
14 changes: 7 additions & 7 deletions components/vision/include/maix_rtmp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ namespace maix::rtmp
int _socket;
void *_handler;

camera::Camera *_camera;
video::Encoder *_video_encoder;
audio::Recorder *_audio_recorder;
display::Display *_display;
thread::Thread *_thread;
thread::Thread *_push_thread;
thread::Thread *_app_thread;
camera::Camera *_camera = nullptr;
video::Encoder *_video_encoder = nullptr;
audio::Recorder *_audio_recorder = nullptr;
display::Display *_display = nullptr;
thread::Thread *_thread = nullptr;
thread::Thread *_push_thread = nullptr;
thread::Thread *_app_thread = nullptr;
pthread_mutex_t _lock;
bool _start;
std::string _path;
Expand Down
38 changes: 22 additions & 16 deletions components/vision/port/maixcam/maix_rtmp_maixcam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,12 +692,14 @@ namespace maix::rtmp {
int venc_ch = MMF_VENC_CHN;
uint64_t video_pts = 0, audio_pts = 0;
uint64_t last_read_pcm_ms = 0, last_read_cam_ms = 0;

bool has_audio = audio_recorder ? true : false;
// clear pcm buffer
Bytes *pcm_data = audio_recorder->record();
if (pcm_data) {
delete pcm_data;
pcm_data = NULL;
if (has_audio) {
Bytes *pcm_data = audio_recorder->record();
if (pcm_data) {
delete pcm_data;
pcm_data = NULL;
}
}

param->status = PrivateParam::RTMP_RUNNING;
Expand Down Expand Up @@ -755,10 +757,13 @@ namespace maix::rtmp {
time::sleep_ms(500);
log::info("Can't connect rtmp server, retry again..");
}
Bytes *pcm_data = audio_recorder->record();
if (pcm_data) {
delete pcm_data;
pcm_data = NULL;

if (has_audio) {
Bytes *pcm_data = audio_recorder->record();
if (pcm_data) {
delete pcm_data;
pcm_data = NULL;
}
}

last_read_pcm_ms = 0;
Expand Down Expand Up @@ -804,7 +809,7 @@ namespace maix::rtmp {
}
}

if (rtmp_client->is_opened()) {
if (has_audio && rtmp_client->is_opened()) {
int frame_size_per_second = rtmp_client->get_frame_size_per_second();
uint64_t loop_ms = 0;
int read_pcm_size = 0;
Expand All @@ -826,7 +831,7 @@ namespace maix::rtmp {
if (pcm_data->data_len > 0) {
if (err::ERR_NONE != rtmp_client->push(pcm_data->data, pcm_data->data_len, audio_pts, true)) {
log::error("rtmp push failed!");
break;
break;
}
}
delete pcm_data;
Expand Down Expand Up @@ -861,6 +866,7 @@ namespace maix::rtmp {
auto video_encoder_height = 0;
auto video_encoder_format = 0;
auto fps = 30;
bool has_audio = false;
RTMPClient *rtmp_client = nullptr;

if (param == nullptr) {
Expand All @@ -881,10 +887,10 @@ namespace maix::rtmp {
}

if (_audio_recorder == nullptr) {
log::info("You must use the bind_audio_recorder interface to bind a audio::Recorder object.");
ret = err::ERR_NOT_READY;
goto _error;
}
has_audio = false;
} else {
has_audio = true;
}

video_encoder_width = _camera->width();
video_encoder_height = _camera->height();
Expand All @@ -898,7 +904,7 @@ namespace maix::rtmp {
goto _error;
}

err::check_raise(rtmp_client->config("has_audio", true), "rtmp config failed!");
err::check_raise(rtmp_client->config("has_audio", has_audio), "rtmp config failed!");
err::check_raise(rtmp_client->config("video_codec_id", AV_CODEC_ID_H264), "rtmp config failed!");
err::check_raise(rtmp_client->config("video_width", video_encoder_width), "rtmp config failed!");
err::check_raise(rtmp_client->config("video_height", video_encoder_height), "rtmp config failed!");
Expand Down

0 comments on commit ee01c8f

Please sign in to comment.