Skip to content

Commit

Permalink
feat(rtsp-server): Added frame auto-drop feature to solve the problem…
Browse files Browse the repository at this point in the history
… of delay accumulation.(#102)

Signed-off-by: scottxu <[email protected]>
  • Loading branch information
iamscottxu committed Mar 20, 2023
1 parent 50a191b commit 764b2af
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
18 changes: 16 additions & 2 deletions rtsp_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define ERROR_START_MULTICAST 4
#define ERROR_ENCODE OBS_OUTPUT_ENCODE_ERROR

#define OBS_RTSPSERVER_QUEUE_SIZE_LIMIT 2

struct queue_frame {
queue_frame(size_t size = 0)
Expand Down Expand Up @@ -359,8 +360,8 @@ static void rtsp_output_rtsp_start(void *data)
}
}

out_data->frame_queue =
std::make_unique<threadsafe_queue<queue_frame>>();
out_data->frame_queue = std::make_unique<threadsafe_queue<queue_frame>>(
OBS_RTSPSERVER_QUEUE_SIZE_LIMIT);

session->AddNotifyConnectedCallback(
[](const xop::MediaSessionId session_id,
Expand Down Expand Up @@ -643,6 +644,18 @@ static uint64_t rtsp_output_total_bytes_sent(void *data)
return out_data->total_bytes_sent;
}

static int rtsp_output_get_dropped_frames(void *data)
{
const auto *out_data = static_cast<rtsp_out_data *>(data);
if (!active(out_data) || out_data->frame_queue == nullptr) {
return 0;
}
auto dropped_count = out_data->frame_queue->dropped_count();
while (dropped_count > INT32_MAX)
dropped_count -= INT32_MAX;
return static_cast<int>(dropped_count);
}

void rtsp_output_register()
{
struct obs_output_info output_info = {};
Expand All @@ -661,6 +674,7 @@ void rtsp_output_register()
output_info.update = rtsp_output_update;
output_info.get_properties = rtsp_output_properties;
output_info.get_total_bytes = rtsp_output_total_bytes_sent;
output_info.get_dropped_frames = rtsp_output_get_dropped_frames;

obs_register_output(&output_info);
}
10 changes: 10 additions & 0 deletions rtsp_output_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ uint64_t RtspOutputHelper::GetTotalBytes() const
return obs_output_get_total_bytes(obsOutput);
}

int RtspOutputHelper::GetTotalFrames() const
{
return obs_output_get_total_frames(obsOutput);
}

int RtspOutputHelper::GetFramesDropped() const
{
return obs_output_get_frames_dropped(obsOutput);
}

bool RtspOutputHelper::IsActive() const
{
return obs_output_active(obsOutput);
Expand Down
2 changes: 2 additions & 0 deletions rtsp_output_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class RtspOutputHelper {
void *data) const;
std::string GetOutputName() const;
uint64_t GetTotalBytes() const;
int GetTotalFrames() const;
int GetFramesDropped() const;
bool IsActive() const;

private:
Expand Down
5 changes: 5 additions & 0 deletions threadsafe_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ template<typename T> class threadsafe_queue
shared_ptr<T> data(make_shared<T>(std::move(new_value)));
unique_lock<mutex> lk(mut);
data_queue.push(data);
if (data_queue.size() > size_limit) {
data_queue.pop();
m_dropped_count.fetch_add(1, memory_order_relaxed);
}
data_cond.notify_one();
}

Expand Down Expand Up @@ -139,6 +143,7 @@ template<typename T> class threadsafe_queue
private:
mutex mut;
queue<shared_ptr<T>> data_queue;
const size_t size_limit;
condition_variable data_cond;
atomic_bool m_termination;
atomic_size_t m_dropped_count;
Expand Down
4 changes: 2 additions & 2 deletions ui/rtsp_properties.ui
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</item>
<item row="3" column="1">
<widget class="QWidget" name="widgetEnabledAudioTracks" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QHBoxLayout" name="widgetEnabledAudioTracks_layout">
<property name="leftMargin">
<number>0</number>
</property>
Expand Down Expand Up @@ -439,7 +439,7 @@
<layout class="QVBoxLayout" name="status_layout">
<item>
<widget class="QWidget" name="widgetStatus" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<layout class="QGridLayout" name="widgetStatus_layout">
<property name="leftMargin">
<number>0</number>
</property>
Expand Down

0 comments on commit 764b2af

Please sign in to comment.