| // Copyright (c) 2026 The Chromium Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "quiche/quic/moqt/moqt_namespace_stream.h" | |
| #include <cstdint> | |
| #include <memory> | |
| #include <optional> | |
| #include <utility> | |
| #include <variant> | |
| #include "absl/base/nullability.h" | |
| #include "absl/functional/overload.h" | |
| #include "absl/status/status.h" | |
| #include "absl/strings/string_view.h" | |
| #include "quiche/quic/moqt/moqt_bidi_stream.h" | |
| #include "quiche/quic/moqt/moqt_error.h" | |
| #include "quiche/quic/moqt/moqt_fetch_task.h" | |
| #include "quiche/quic/moqt/moqt_framer.h" | |
| #include "quiche/quic/moqt/moqt_key_value_pair.h" | |
| #include "quiche/quic/moqt/moqt_messages.h" | |
| #include "quiche/quic/moqt/moqt_names.h" | |
| #include "quiche/quic/moqt/moqt_parser.h" | |
| #include "quiche/quic/moqt/moqt_session_callbacks.h" | |
| #include "quiche/common/platform/api/quiche_logging.h" | |
| #include "quiche/common/quiche_status_utils.h" | |
| #include "quiche/web_transport/stream_helpers.h" | |
| namespace moqt { | |
| MoqtSubscribeNamespaceRequestStream::~MoqtSubscribeNamespaceRequestStream() { | |
| NamespaceTask* task = task_.GetIfAvailable(); | |
| if (task != nullptr) { | |
| task->DeclareEof(); | |
| } | |
| Detach(); | |
| } | |
| absl::Status MoqtSubscribeNamespaceRequestStream::OnRawControlMessage( | |
| const MoqtRawControlMessage& message) { | |
| return ControlMessageDispatcher::DispatchControlMessage( | |
| *this, message_parser(), message, "namespace subscriber"); | |
| } | |
| void MoqtSubscribeNamespaceRequestStream::OnStreamBound() { | |
| // TODO(martinduke): Set the priority for this stream. | |
| } | |
| absl::Status MoqtSubscribeNamespaceRequestStream::OnControlMessage( | |
| const MoqtRequestOk& message) { | |
| if (!message.properties.empty()) { | |
| OnFatalError( | |
| absl::InvalidArgumentError("REQUEST_OK received with properties")); | |
| return absl::OkStatus(); | |
| } | |
| if (response_callback_ != nullptr) { | |
| if (!ParametersAllowedByRequestOk(message.parameters, | |
| MoqtMessageType::kSubscribeNamespace)) { | |
| return absl::InvalidArgumentError( | |
| "REQUEST_OK contains invalid parameters for SUBSCRIBE_NAMESPACE"); | |
| } | |
| MoqtResponseCallback callback = std::move(response_callback_); | |
| response_callback_ = nullptr; | |
| std::move(callback)(message.parameters); | |
| return absl::OkStatus(); | |
| } | |
| if (!ParametersAllowedByRequestOk(message.parameters, | |
| MoqtMessageType::kRequestUpdate)) { | |
| return absl::InvalidArgumentError( | |
| "REQUEST_OK contains invalid parameters for REQUEST_UPDATE"); | |
| } | |
| NamespaceTask* task = task_.GetIfAvailable(); | |
| if (task == nullptr) { | |
| // The application has already unsubscribed, and the stream has been reset. | |
| // This is irrelevant. | |
| return absl::OkStatus(); | |
| } | |
| // TODO(martinduke): update parameters. | |
| return request_update_queue().OnControlMessage(message); | |
| } | |
| absl::Status MoqtSubscribeNamespaceRequestStream::OnControlMessage( | |
| const MoqtRequestError& message) { | |
| if (response_callback_ != nullptr) { | |
| if (!RedirectAllowedByRequestError(message.redirect, | |
| MoqtMessageType::kSubscribeNamespace)) { | |
| return absl::InvalidArgumentError( | |
| "REQUEST_ERROR contains invalid redirect for SUBSCRIBE_NAMESPACE"); | |
| } | |
| std::move(response_callback_)(message); | |
| response_callback_ = nullptr; | |
| return absl::OkStatus(); | |
| } | |
| if (!RedirectAllowedByRequestError(message.redirect, | |
| MoqtMessageType::kRequestUpdate)) { | |
| return absl::InvalidArgumentError( | |
| "REQUEST_ERROR contains invalid redirect for REQUEST_UPDATE"); | |
| } | |
| NamespaceTask* task = task_.GetIfAvailable(); | |
| if (task == nullptr) { | |
| // The application has already unsubscribed, and the stream has been reset. | |
| // This is irrelevant. | |
| return absl::OkStatus(); | |
| } | |
| return request_update_queue().OnControlMessage(message); | |
| } | |
| absl::Status MoqtSubscribeNamespaceRequestStream::OnControlMessage( | |
| const MoqtNamespace& message) { | |
| if (response_callback_ != nullptr) { | |
| return absl::InvalidArgumentError( | |
| "First message must be REQUEST_OK or REQUEST_ERROR"); | |
| } | |
| NamespaceTask* task = task_.GetIfAvailable(); | |
| if (task == nullptr) { | |
| // The application has already unsubscribed, and the stream has been reset. | |
| // This is irrelevant. | |
| return absl::OkStatus(); | |
| } | |
| if (task->prefix().number_of_elements() + | |
| message.track_namespace_suffix.number_of_elements() > | |
| kMaxNamespaceElements) { | |
| return absl::InvalidArgumentError("Too many namespace elements"); | |
| } | |
| if (task->prefix().total_length() + | |
| message.track_namespace_suffix.total_length() > | |
| kMaxFullTrackNameSize) { | |
| return absl::InvalidArgumentError("Namespace too large"); | |
| } | |
| auto [it, inserted] = | |
| published_suffixes_.insert(message.track_namespace_suffix); | |
| if (!inserted) { | |
| return absl::InvalidArgumentError( | |
| "Two NAMESPACE messages for the same track namespace"); | |
| } | |
| QUIC_DLOG(INFO) << "Received NAMESPACE message for " | |
| << message.track_namespace_suffix; | |
| task->AddPendingSuffix(message.track_namespace_suffix, TransactionType::kAdd); | |
| return absl::OkStatus(); | |
| } | |
| absl::Status MoqtSubscribeNamespaceRequestStream::OnControlMessage( | |
| const MoqtNamespaceDone& message) { | |
| if (response_callback_ != nullptr) { | |
| return absl::InvalidArgumentError( | |
| "First message must be REQUEST_OK or REQUEST_ERROR"); | |
| } | |
| NamespaceTask* task = task_.GetIfAvailable(); | |
| if (task == nullptr) { | |
| return absl::OkStatus(); | |
| } | |
| if (published_suffixes_.erase(message.track_namespace_suffix) == 0) { | |
| return absl::InvalidArgumentError( | |
| "NAMESPACE_DONE with no active namespace"); | |
| } | |
| QUIC_DLOG(INFO) << "Received NAMESPACE_DONE message for " | |
| << message.track_namespace_suffix; | |
| task->AddPendingSuffix(message.track_namespace_suffix, | |
| TransactionType::kDelete); | |
| return absl::OkStatus(); | |
| } | |
| std::unique_ptr<MoqtNamespaceTask> | |
| MoqtSubscribeNamespaceRequestStream::CreateTask(const TrackNamespace& prefix) { | |
| auto task = std::make_unique<NamespaceTask>(this, prefix); | |
| QUICHE_DCHECK(task != nullptr); | |
| task_ = task->GetWeakPtr(); | |
| QUICHE_DCHECK(task_.IsValid()); | |
| return std::move(task); | |
| } | |
| MoqtSubscribeNamespaceRequestStream::NamespaceTask::~NamespaceTask() { | |
| if (state_ != nullptr) { | |
| state_->Reset(kResetCodeCancelled); | |
| } | |
| } | |
| void MoqtSubscribeNamespaceRequestStream::NamespaceTask:: | |
| SetObjectsAvailableCallback(ObjectsAvailableCallback | |
| absl_nullable callback) { | |
| callback_ = std::move(callback); | |
| if (!pending_suffixes_.empty() && callback_ != nullptr) { | |
| callback_(); | |
| } | |
| } | |
| void MoqtSubscribeNamespaceRequestStream::NamespaceTask::Update( | |
| const MessageParameters& parameters, | |
| MoqtResponseCallback response_callback) { | |
| if (state_ == nullptr) { | |
| std::move(response_callback)( | |
| MoqtRequestErrorInfo{RequestErrorCode::kInternalError, std::nullopt, | |
| "Stream has been reset"}); | |
| return; | |
| } | |
| state_->CheckStatus(state_->SendRequestUpdate( | |
| next_request_id_, parameters, std::move(response_callback), | |
| MoqtMessageType::kSubscribeNamespace)); | |
| next_request_id_ += 2; | |
| } | |
| GetNextResult MoqtSubscribeNamespaceRequestStream::NamespaceTask::GetNextSuffix( | |
| TrackNamespace& suffix, TransactionType& type) { | |
| if (pending_suffixes_.empty()) { | |
| if (error_.has_value()) { | |
| return kError; | |
| } | |
| if (eof_) { | |
| return kEof; | |
| } | |
| return kPending; | |
| } | |
| suffix = pending_suffixes_.front().suffix; | |
| type = pending_suffixes_.front().type; | |
| pending_suffixes_.pop_front(); | |
| return kSuccess; | |
| } | |
| void MoqtSubscribeNamespaceRequestStream::NamespaceTask::AddPendingSuffix( | |
| TrackNamespace suffix, TransactionType type) { | |
| if (pending_suffixes_.size() == kMaxPendingSuffixes) { | |
| error_ = kResetCodeTooFarBehind; | |
| if (state_ != nullptr) { | |
| state_->Reset(kResetCodeTooFarBehind); | |
| } | |
| return; | |
| } | |
| pending_suffixes_.push_back(PendingSuffix{std::move(suffix), type}); | |
| if (callback_ != nullptr) { | |
| callback_(); | |
| } | |
| } | |
| void MoqtSubscribeNamespaceRequestStream::NamespaceTask::DeclareEof() { | |
| if (eof_) { | |
| return; | |
| } | |
| eof_ = true; | |
| state_ = nullptr; | |
| if (callback_ != nullptr) { | |
| callback_(); | |
| } | |
| } | |
| MoqtSubscribeNamespaceResponseStream::MoqtSubscribeNamespaceResponseStream( | |
| MoqtFramer* framer, const MoqtControlMessageParser& message_parser, | |
| AddPrefixCallback add_callback, RemovePrefixCallback remove_callback, | |
| SessionErrorCallback session_error_callback, | |
| ValidateRequestIdCallback validate_request_id, | |
| MoqtIncomingSubscribeNamespaceCallback& application) | |
| // No stream_deleted_callback because there's no state yet. | |
| : MoqtBidiStreamBase(framer, message_parser, | |
| std::move(session_error_callback)), | |
| add_callback_(std::move(add_callback)), | |
| remove_callback_(std::move(remove_callback)), | |
| validate_request_id_(std::move(validate_request_id)), | |
| application_(application) {} | |
| absl::Status MoqtSubscribeNamespaceResponseStream::OnRawControlMessage( | |
| const MoqtRawControlMessage& message) { | |
| return ControlMessageDispatcher::DispatchControlMessage( | |
| *this, message_parser(), message, "namespace publisher"); | |
| } | |
| absl::Status MoqtSubscribeNamespaceResponseStream::OnControlMessage( | |
| const MoqtSubscribeNamespace& message) { | |
| if (add_callback_ == nullptr) { | |
| return absl::InvalidArgumentError("Two SUBSCRIBE_NAMESPACE on one stream"); | |
| } | |
| QUICHE_RETURN_IF_ERROR(validate_request_id_(message.request_id)); | |
| request_id_ = message.request_id; | |
| if (message.track_namespace_prefix.DoesNotExist()) { | |
| add_callback_ = nullptr; | |
| remove_callback_ = nullptr; | |
| return SendRequestError(RequestErrorCode::kDoesNotExist, | |
| /*retry_interval=*/std::nullopt, | |
| "Reserved track namespace"); | |
| } | |
| if (!std::move(add_callback_)(message.track_namespace_prefix)) { | |
| add_callback_ = nullptr; | |
| remove_callback_ = nullptr; | |
| return SendRequestError(RequestErrorCode::kPrefixOverlap, std::nullopt, ""); | |
| } | |
| add_callback_ = nullptr; | |
| QUICHE_DCHECK(task_ == nullptr); | |
| task_ = application_(message.track_namespace_prefix, message.parameters, | |
| ResponseCallback(MoqtMessageType::kSubscribeNamespace)); | |
| if (task_ != nullptr) { | |
| task_->SetObjectsAvailableCallback([this]() { ProcessNamespaces(); }); | |
| } else if (remove_callback_ != nullptr) { | |
| RemovePrefixCallback callback = std::move(remove_callback_); | |
| remove_callback_ = nullptr; | |
| std::move(callback)(message.track_namespace_prefix); | |
| } | |
| return absl::OkStatus(); | |
| } | |
| absl::Status MoqtSubscribeNamespaceResponseStream::OnControlMessage( | |
| const MoqtRequestUpdate& message) { | |
| QUICHE_RETURN_IF_ERROR(validate_request_id_(message.request_id)); | |
| if (!ParametersAllowedByRequestUpdate(message.parameters, | |
| MoqtMessageType::kSubscribeNamespace)) { | |
| return absl::InvalidArgumentError( | |
| "REQUEST_UPDATE contains invalid parameters for SUBSCRIBE_NAMESPACE"); | |
| } | |
| if (task_ == nullptr) { | |
| // This stream is dying. | |
| return absl::OkStatus(); | |
| } | |
| task_->Update(message.parameters, | |
| ResponseCallback(MoqtMessageType::kRequestUpdate)); | |
| return absl::OkStatus(); | |
| } | |
| void MoqtSubscribeNamespaceResponseStream::Detach() { | |
| if (task_ == nullptr) { | |
| return; | |
| } | |
| if (remove_callback_ != nullptr) { | |
| RemovePrefixCallback callback = std::move(remove_callback_); | |
| remove_callback_ = nullptr; | |
| std::move(callback)(task_->prefix()); | |
| } | |
| // In general, a remove_callback should not be followed by further operations | |
| // on class members. But in this case, the remove_callback merely removes the | |
| // prefix from SessionNamespaceTree; it doesn't destroy the class. If task_ | |
| // is destroyed first, it would have to make an expensive copy of | |
| // TrackNamespace to send to the remove_callback. | |
| task_.reset(); | |
| } | |
| void MoqtSubscribeNamespaceResponseStream::ProcessNamespaces() { | |
| if (task_ == nullptr) { | |
| return; | |
| } | |
| TrackNamespace suffix; | |
| TransactionType type; | |
| while (!QueueIsFull()) { | |
| GetNextResult result = task_->GetNextSuffix(suffix, type); | |
| switch (result) { | |
| case kPending: | |
| return; | |
| case kEof: | |
| if (absl::Status status = webtransport::SendFinOnStream(*stream()); | |
| !status.ok()) { | |
| OnFatalError(status); | |
| }; | |
| return; | |
| case kError: | |
| Reset(kResetCodeCancelled); | |
| return; | |
| case kSuccess: { | |
| absl::Status write_status; | |
| switch (type) { | |
| case TransactionType::kAdd: { | |
| auto [it, inserted] = published_suffixes_.insert(suffix); | |
| if (!inserted) { | |
| // This should never happen. Do not send something that would | |
| // cause a protocol violation. | |
| return; | |
| } | |
| write_status = SendOrBufferMessage( | |
| framer()->SerializeNamespace(MoqtNamespace{suffix})); | |
| break; | |
| } | |
| case TransactionType::kDelete: { | |
| if (published_suffixes_.erase(suffix) == 0) { | |
| // This should never happen. Do not send something that would | |
| // cause a protocol violation. | |
| return; | |
| } | |
| write_status = SendOrBufferMessage( | |
| framer()->SerializeNamespaceDone(MoqtNamespaceDone{suffix})); | |
| break; | |
| } | |
| } | |
| if (!write_status.ok()) { | |
| if (absl::IsResourceExhausted(write_status)) { | |
| // The peer is not reading data fast enough, and the sender has | |
| // reached its buffer limit; reset the stream. | |
| Reset(kResetCodeTooFarBehind); | |
| return; | |
| } | |
| // All other write errors are fatal. | |
| OnFatalError(write_status); | |
| return; | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| MoqtResponseCallback MoqtSubscribeNamespaceResponseStream::ResponseCallback( | |
| MoqtMessageType type_of_ok) { | |
| return [this, type_of_ok]( | |
| std::variant<MessageParameters, MoqtRequestErrorInfo> response) { | |
| std::visit( | |
| absl::Overload{[this, type_of_ok](const MessageParameters& parameters) { | |
| // In draft-18, there are no useful parameters | |
| // in SUBSCRIBE_NAMESPACE_OK, but Issue #1639 | |
| // would change that. | |
| CheckStatus(SendRequestOk(parameters, type_of_ok)); | |
| }, | |
| [this](const MoqtRequestErrorInfo& error_info) { | |
| CheckStatus(SendRequestError(error_info)); | |
| }}, | |
| response); | |
| }; | |
| } | |
| } // namespace moqt |