bug改掉 完美一版

This commit is contained in:
2026-08-29 22:16:54 +08:00
parent bb6bcec901
commit 96828ce084
2 changed files with 14 additions and 31 deletions
@@ -1,5 +1,4 @@
#include "Gallery_Video_WebSocket.hpp"
#include <concurrentqueue-1.0.5/concurrentqueue.h>
#include <nlohmann/json.hpp>
#include <algorithm>
#include <atomic>
@@ -64,19 +63,11 @@ struct Gallery_Video_WebSocket::Private final {
std::string connection_id{};
std::atomic_bool attached{};
std::atomic_bool decoder_ready{};
moodycamel::ConcurrentQueue<
std::shared_ptr<const Encoded_Video_Frame>> outstanding_frames{};
std::atomic_uint64_t latest_sent_sequence{};
std::atomic_uint64_t latest_acknowledged_sequence{};
std::atomic_uint64_t sent_count{};
std::atomic_uint64_t acknowledged_count{};
std::atomic_uint64_t rejected_count{};
void release_outstanding_frames() noexcept {
std::shared_ptr<const Encoded_Video_Frame> outstanding;
while (outstanding_frames.try_dequeue(outstanding))
outstanding.reset();
}
};
Gallery_Video_WebSocket::Gallery_Video_WebSocket(
@@ -120,8 +111,6 @@ void Gallery_Video_WebSocket::start() {
{"kind", "drogon_h264_state"},
{"decoder_ready", socket->d->decoder_ready.load(
std::memory_order_acquire)},
{"outstanding_frames",
socket->d->outstanding_frames.size_approx()},
{"latest_sent_sequence", socket->d->latest_sent_sequence.load(
std::memory_order_relaxed)},
{"latest_acknowledged_sequence",
@@ -173,12 +162,9 @@ bool Gallery_Video_WebSocket::queue_video_frame(
const auto sequence = frame->sequence;
try {
auto packet = websocket_packet(*frame);
if (!d->outstanding_frames.enqueue(frame)) throw std::bad_alloc{};
if (!d->attached.load(std::memory_order_acquire) ||
!d->decoder_ready.load(std::memory_order_acquire)) {
d->release_outstanding_frames();
!d->decoder_ready.load(std::memory_order_acquire))
return false;
}
connection->send(
reinterpret_cast<const char*>(packet.data()), packet.size(),
drogon::WebSocketMessageType::Binary);
@@ -192,20 +178,19 @@ bool Gallery_Video_WebSocket::queue_video_frame(
}
}
void Gallery_Video_WebSocket::acknowledge_video_frame(std::uint64_t sequence) {
if (sequence == 0 || sequence <= d->latest_acknowledged_sequence.load(
std::memory_order_acquire))
return;
std::shared_ptr<const Encoded_Video_Frame> acknowledged;
if (!d->outstanding_frames.try_dequeue(acknowledged) ||
!acknowledged || acknowledged->sequence != sequence) {
d->rejected_count.fetch_add(1, std::memory_order_relaxed);
close();
return;
void Gallery_Video_WebSocket::acknowledge_video_frame(
std::uint64_t sequence) noexcept {
if (sequence == 0) return;
auto acknowledged = d->latest_acknowledged_sequence.load(
std::memory_order_acquire);
while (sequence > acknowledged) {
if (d->latest_acknowledged_sequence.compare_exchange_weak(
acknowledged, sequence, std::memory_order_acq_rel,
std::memory_order_acquire)) {
d->acknowledged_count.fetch_add(1, std::memory_order_relaxed);
return;
}
}
d->latest_acknowledged_sequence.store(
sequence, std::memory_order_release);
d->acknowledged_count.fetch_add(1, std::memory_order_relaxed);
}
void Gallery_Video_WebSocket::receive(std::string_view message) {
@@ -220,7 +205,6 @@ void Gallery_Video_WebSocket::receive(std::string_view message) {
}
if (kind == "h264_stream_not_ready") {
d->decoder_ready.store(false, std::memory_order_release);
d->release_outstanding_frames();
return;
}
if (kind == "h264_frame_accepted") {
@@ -241,7 +225,6 @@ void Gallery_Video_WebSocket::close() noexcept {
d->subscription = 0;
}
d->decoder_ready.store(false, std::memory_order_release);
d->release_outstanding_frames();
}
Gallery_Video_WebSocket_Controller::Gallery_Video_WebSocket_Controller(
@@ -25,7 +25,7 @@ private:
[[nodiscard]] bool deliver(Gallery_Stream_Frame frame);
[[nodiscard]] bool queue_video_frame(
std::shared_ptr<const Encoded_Video_Frame> frame);
void acknowledge_video_frame(std::uint64_t sequence);
void acknowledge_video_frame(std::uint64_t sequence) noexcept;
struct Private;
std::unique_ptr<Private> d;
};