Skip to content

Commit

Permalink
Use an actual queue for media
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean-Der committed Oct 9, 2023
1 parent e19eeab commit 6520d7e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
26 changes: 5 additions & 21 deletions plugins/obs-webrtc/whep-source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

{
Expand Down Expand Up @@ -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;
Expand All @@ -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<std::byte>{msg});
}

void WHEPSource::SetupPeerConnection()
Expand Down
35 changes: 31 additions & 4 deletions plugins/obs-webrtc/whep-source.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,36 @@ extern "C" {

#include <rtc/rtc.hpp>

class RTPQueue {
private:
std::queue<std::vector<std::byte>> pkts;
std::mutex lock;
std::condition_variable notify;

public:
void push(std::vector<std::byte> item)
{

std::unique_lock<std::mutex> l(lock);

pkts.push(item);
notify.notify_one();
}

std::vector<std::byte> pop()
{

std::unique_lock<std::mutex> 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);
Expand Down Expand Up @@ -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<std::byte> 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
Expand Down

0 comments on commit 6520d7e

Please sign in to comment.