Skip to content

Commit

Permalink
Made Stream.cpp thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Laky-64 committed Dec 12, 2023
1 parent da57f21 commit d28e681
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
12 changes: 6 additions & 6 deletions ntgcalls/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ namespace ntgcalls {
return {bs, br};
}

void Stream::checkStream() const
{
void Stream::checkStream() const {
if (running && !changing) {
if (reader->audio && reader->audio->eof()) {
reader->audio = nullptr;
Expand All @@ -74,6 +73,7 @@ namespace ntgcalls {
}

void Stream::sendSample() {
std::lock_guard lock(mutex);
if (running) {
if (idling || changing || !reader || !(reader->audio || reader->video)) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
Expand All @@ -94,6 +94,7 @@ namespace ntgcalls {
}

void Stream::setAVStream(const MediaDescription& streamConfig, const bool noUpgrade) {
std::lock_guard lock(mutex);
changing = true;
const auto audioConfig = streamConfig.audio;
const auto videoConfig = streamConfig.video;
Expand Down Expand Up @@ -180,8 +181,7 @@ namespace ntgcalls {
return res;
}

bool Stream::mute() const
{
bool Stream::mute() const {
if (!audioTrack->isMuted() || !videoTrack->isMuted()) {
audioTrack->Mute(true);
videoTrack->Mute(true);
Expand All @@ -192,8 +192,7 @@ namespace ntgcalls {
return false;
}

bool Stream::unmute() const
{
bool Stream::unmute() const {
if (audioTrack->isMuted() || videoTrack->isMuted()) {
audioTrack->Mute(false);
videoTrack->Mute(false);
Expand All @@ -205,6 +204,7 @@ namespace ntgcalls {
}

void Stream::stop() {
std::lock_guard lock(mutex);
running = false;
idling = false;
changing = false;
Expand Down
1 change: 1 addition & 0 deletions ntgcalls/stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace ntgcalls {
wrtc::synchronized_callback<MediaState> onChangeStatus;
std::shared_ptr<DispatchQueue> streamQueue;
std::shared_ptr<DispatchQueue> updateQueue;
std::mutex mutex;

void sendSample();

Expand Down

0 comments on commit d28e681

Please sign in to comment.