diff --git a/plugins/obs-webrtc/whep-source.cpp b/plugins/obs-webrtc/whep-source.cpp index 1bba1fe4e29545..31a7458f138d97 100644 --- a/plugins/obs-webrtc/whep-source.cpp +++ b/plugins/obs-webrtc/whep-source.cpp @@ -22,10 +22,7 @@ WHEPSource::WHEPSource(obs_data_t *settings, obs_source_t *source) start_stop_mutex(), start_stop_thread(), av_io_context_read_count(0), - rtp_buff_lock(), - rtp_buff_notify(), - rtp_buff(), - rtp_buff_ready(false), + video_queue(), activated(false) { @@ -92,17 +89,10 @@ AVIOContext *WHEPSource ::CreateAVIOContext() return AVERROR_EOF; } - std::unique_lock lk(whep_source->rtp_buff_lock); - whep_source->rtp_buff_notify.wait(lk, [&whep_source] { - return whep_source->rtp_buff_ready; - }); + auto video_buff = whep_source->video_queue.pop(); + std::memcpy(buff, video_buff.data(), video_buff.size()); - std::memcpy(buff, whep_source->rtp_buff.data(), - whep_source->rtp_buff.size()); - - whep_source->rtp_buff_ready = false; - - return whep_source->rtp_buff.size(); + return video_buff.size(); }, [](void *, uint8_t *, int buff_size) -> int { return buff_size; @@ -118,13 +108,7 @@ AVIOContext *WHEPSource ::CreateAVIOContext() void WHEPSource::OnMessageHandler(rtc::binary msg) { - std::unique_lock lk(this->rtp_buff_lock); - - this->rtp_buff = msg; - this->rtp_buff_ready = true; - - lk.unlock(); - this->rtp_buff_notify.notify_one(); + this->video_queue.push(std::vector{msg}); } void WHEPSource::SetupPeerConnection() diff --git a/plugins/obs-webrtc/whep-source.h b/plugins/obs-webrtc/whep-source.h index fd85bf98cb2852..8d85b50b1f58ce 100644 --- a/plugins/obs-webrtc/whep-source.h +++ b/plugins/obs-webrtc/whep-source.h @@ -24,6 +24,36 @@ extern "C" { #include +class RTPQueue { +private: + std::queue> pkts; + std::mutex lock; + std::condition_variable notify; + +public: + void push(std::vector item) + { + + std::unique_lock l(lock); + + pkts.push(item); + notify.notify_one(); + } + + std::vector pop() + { + + std::unique_lock l(lock); + + notify.wait(l, [this]() { return !pkts.empty(); }); + + auto item = pkts.front(); + pkts.pop(); + + return item; + } +}; + class WHEPSource { public: WHEPSource(obs_data_t *settings, obs_source_t *source); @@ -64,10 +94,7 @@ class WHEPSource { // media-playback media_playback_t *media_video; - std::mutex rtp_buff_lock; - std::condition_variable rtp_buff_notify; - std::vector rtp_buff; - bool rtp_buff_ready; + RTPQueue video_queue; // TODO - How do you know when a URL + Bearer Token has changed? // `Update` is fired for every character change. We only want to know when the dialog is closed