Skip to content

Commit

Permalink
Don't crash if audio sink suddenly goes invalid.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mysticial committed Dec 15, 2024
1 parent ea40918 commit f5fd553
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ class AudioOutputDevice : public AudioFloatToStream, private StreamListener{
, m_logger(logger)
, m_sink(device, format)
{
m_sink.connect(
&m_sink, &NativeAudioSink::stateChanged,
&m_sink, [this](QAudio::State state){
if (state == QAudio::State::StoppedState){
m_io_device = nullptr;
m_logger.log("AudioOutputDevice has prematurely stopped.", COLOR_RED);
}
}
);

m_io_device = m_sink.start();
m_sink.setVolume(convertAudioVolumeFromSlider(volume));
add_listener(*this);
Expand All @@ -74,7 +84,9 @@ class AudioOutputDevice : public AudioFloatToStream, private StreamListener{

virtual void on_objects(const void* data, size_t objects) override{
auto scope_check = m_sanitizer.check_scope();
m_io_device->write((const char*)data, objects * object_size);
if (m_io_device != nullptr){
m_io_device->write((const char*)data, objects * object_size);
}
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ bool StreamRecording::stop_and_save(const std::string& filename){


void StreamRecording::push_samples(WallClock timestamp, const float* data, size_t frames){
#if 1
WallClock now = current_time();
if (m_audio_samples_per_frame == 0 || now < m_start_time){
return;
Expand Down Expand Up @@ -150,8 +151,10 @@ void StreamRecording::push_samples(WallClock timestamp, const float* data, size_
data, frames * m_audio_samples_per_frame
);
m_cv.notify_all();
#endif
}
void StreamRecording::push_frame(std::shared_ptr<const VideoFrame> frame){
#if 1
WallClock now = current_time();
if (!m_has_video || now < m_start_time){
return;
Expand Down Expand Up @@ -190,6 +193,7 @@ void StreamRecording::push_frame(std::shared_ptr<const VideoFrame> frame){
m_buffered_frames.emplace_back(std::move(frame));
m_last_frame_time = frame_time;
m_cv.notify_all();
#endif
}


Expand Down Expand Up @@ -313,6 +317,7 @@ void StreamRecording::internal_run(){
// cout << "Bit Rate = " << (int)recorder.videoBitRate() << endl;


#if 1
// cout << "starting recording" << endl;
m_recorder->record();

Expand Down Expand Up @@ -380,6 +385,7 @@ void StreamRecording::internal_run(){

m_recorder->stop();
// cout << "recorder.stop()" << endl;
#endif


while (m_recorder->recorderState() != QMediaRecorder::StoppedState){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <chrono>
#include <iostream>
//#include <QThread>
#include <QCamera>
#include <QPainter>
#include <QMediaDevices>
Expand Down Expand Up @@ -286,6 +287,8 @@ void CameraSession::connect_video_sink(QVideoSink* sink){
}
}

// cout << QThread::currentThread()->isMainThread() << endl;

},
Qt::DirectConnection
);
Expand Down

0 comments on commit f5fd553

Please sign in to comment.