Skip to content

Commit

Permalink
Check function return value and then log
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyet committed Aug 10, 2023
1 parent ef24d64 commit ffddbd5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
5 changes: 4 additions & 1 deletion NanaRecorder/NanaRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ void NanaRecorder::startBtnClicked() {
info.insert("recordPath", path);
m_recorder = onlyet::createRecorder(info);
}
m_recorder->startRecord();
if (-1 == m_recorder->startRecord()) {
qDebug() << "startRecord failed";
return;
}

m_recordDuration = 0;
ui.durationLabel->setText("00:00:00");
Expand Down
1 change: 1 addition & 0 deletions RecordCore/src/AudioCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ int AudioCapture::initCapture(AudioCaptureDevice dev) {

string audioDeviceName = FFmpegHelper::getAudioDevice(dev);
if ("" == audioDeviceName) {
qCritical() << "Can not find audio device";
return -1;
}
if ((ret = avformat_open_input(&m_aFmtCtx, audioDeviceName.c_str(), ifmt, &options)) != 0) {
Expand Down
6 changes: 0 additions & 6 deletions RecordCore/src/FFmpegHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ std::string FFmpegHelper::getAudioDevice(AudioCaptureDevice type) {
hr = pCreateDevEnum->CreateClassEnumerator(guid, &pEm, 0);
if (hr != NOERROR) {
::CoUninitialize();
//return "";
return ret;
}

Expand All @@ -137,14 +136,9 @@ std::string FFmpegHelper::getAudioDevice(AudioCaptureDevice type) {

for (const auto& dev : audioDevSet) {
if (tmpName.find(dev) != string::npos) {
#if 0
tmpName = QString::fromLocal8Bit(tmpName.c_str()).toStdString();
qDebug() << "Audio device:" << QString::fromLocal8Bit(tmpName.c_str());
#else
// 包含中文需要转UTF8编码
tmpName = AnsiToUTF8(tmpName.c_str(), tmpName.length());
qInfo() << "Audio device:" << QString::fromStdString(tmpName);
#endif
ret = tmpName;
isFound = true;
break;
Expand Down
24 changes: 14 additions & 10 deletions RecordCore/src/Recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ int Recorder::startRecord() {

FFmpegHelper::registerAll();

startCapture();
// init
int ret = 0;
ret = startCapture();
if (ret != 0) return ret;

m_videoFrameQueue->initBuf(g_record.outWidth, g_record.outHeight, AV_PIX_FMT_YUV420P);

int ret = 0;
if (m_amixFilter) {
ret = m_amixFilter->init(
{nullptr, nullptr,
Expand All @@ -158,7 +159,7 @@ int Recorder::startRecord() {
m_microphoneCap->channel(),
m_microphoneCap->channelLayout()},
{nullptr, nullptr, {1, AV_TIME_BASE}, g_record.sampleRate, AV_SAMPLE_FMT_FLTP, g_record.channel, av_get_default_channel_layout(g_record.channel)});
if (ret != 0) return -1;
if (ret != 0) return ret;

m_amixFilter->setFilterFrameCb(bind(static_cast<void (Recorder::*)(AVFrame*)>(&Recorder::writeAudioFrameCb), this, _1));
m_amixFilter->start();
Expand All @@ -179,22 +180,21 @@ int Recorder::startRecord() {
ctx_out.sample_rate = g_record.sampleRate;

ret = m_resampleFilter->init(ctx_in, ctx_out);
if (ret != 0) return -1;
if (ret != 0) return ret;

m_resampleFilter->setFilterFrameCb(bind(static_cast<void (Recorder::*)(AVFrame*)>(&Recorder::writeAudioFrameCb), this, _1));
m_resampleFilter->start();
}

m_outputer->init();

// start
m_startTime = duration_cast<chrono::microseconds>(chrono::system_clock::now().time_since_epoch()).count();
qInfo() << "start time:" << QDateTime::fromMSecsSinceEpoch(m_startTime / 1000).toString("yyyy-MM-dd hh:mm:ss.zzz");
m_outputer->start(m_startTime);

g_record.status = Running;

return 0;
return ret;
}

int Recorder::pauseRecord() {
Expand Down Expand Up @@ -236,25 +236,29 @@ int Recorder::stopRecord() {
return 0;
}

void Recorder::startCapture() {
int ret;
m_videoCap->startCapture();
int Recorder::startCapture() {
int ret = 0;
ret = m_videoCap->startCapture();
if (-1 == ret) return ret;
if (g_record.enableAudio) {
if (m_speakerCap) {
ret = m_speakerCap->startCapture(AudioCaptureDevice::Speaker);
// 找不到音频或打开失败
if (-1 == ret) {
g_record.enableAudio = false;
return ret;
}
}
if (m_microphoneCap) {
ret = m_microphoneCap->startCapture(AudioCaptureDevice::Microphone);
// 找不到音频或打开失败
if (-1 == ret) {
g_record.enableAudio = false;
return ret;
}
}
}
return ret;
}

void Recorder::stopCapture() {
Expand Down
2 changes: 1 addition & 1 deletion RecordCore/src/Recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Recorder : public IRecorder {
int stopRecord() override;

private:
void startCapture();
int startCapture();
void stopCapture();

void writeVideoFrameCb(AVFrame* frame, const VideoCaptureInfo& info);
Expand Down

0 comments on commit ffddbd5

Please sign in to comment.