| // Copyright 2023 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. | |
| #ifndef QUICHE_QUIC_MOQT_SUBSCRIBE_WINDOWS_H | |
| #define QUICHE_QUIC_MOQT_SUBSCRIBE_WINDOWS_H | |
| #include <cstdint> | |
| #include <optional> | |
| #include <vector> | |
| #include "absl/container/btree_map.h" | |
| #include "quiche/quic/moqt/moqt_messages.h" | |
| #include "quiche/common/platform/api/quiche_export.h" | |
| #include "quiche/web_transport/web_transport.h" | |
| namespace moqt { | |
| // SubscribeWindow represents a window of objects for which an MoQT subscription | |
| // can be valid. | |
| class QUICHE_EXPORT SubscribeWindow { | |
| public: | |
| // Creates a half-open window. |next_object| is the expected sequence number | |
| // of the next published object on the track. | |
| SubscribeWindow(uint64_t start_group, uint64_t start_object) | |
| : SubscribeWindow(FullSequence(start_group, start_object), std::nullopt) { | |
| } | |
| // Creates a closed window. | |
| SubscribeWindow(uint64_t start_group, uint64_t start_object, | |
| uint64_t end_group, uint64_t end_object) | |
| : SubscribeWindow(FullSequence(start_group, start_object), | |
| FullSequence(end_group, end_object)) {} | |
| SubscribeWindow(FullSequence start, std::optional<FullSequence> end) | |
| : start_(start), end_(end) {} | |
| bool InWindow(const FullSequence& seq) const; | |
| const std::optional<FullSequence>& end() const { return end_; } | |
| FullSequence start() const { return start_; } | |
| // Updates the subscription window. Returns true if the update is valid (in | |
| // MoQT, subscription windows are only allowed to shrink, not to expand). | |
| bool UpdateStartEnd(FullSequence start, std::optional<FullSequence> end); | |
| private: | |
| // The subgroups in these sequences have no meaning. | |
| FullSequence start_; | |
| std::optional<FullSequence> end_; | |
| }; | |
| // ReducedSequenceIndex represents an index object such that if two sequence | |
| // numbers are mapped to the same stream, they will be mapped to the same index. | |
| class ReducedSequenceIndex { | |
| public: | |
| ReducedSequenceIndex(FullSequence sequence, | |
| MoqtForwardingPreference preference); | |
| bool operator==(const ReducedSequenceIndex& other) const { | |
| return sequence_ == other.sequence_; | |
| } | |
| bool operator!=(const ReducedSequenceIndex& other) const { | |
| return sequence_ != other.sequence_; | |
| } | |
| FullSequence sequence() { return sequence_; } | |
| template <typename H> | |
| friend H AbslHashValue(H h, const ReducedSequenceIndex& m) { | |
| return H::combine(std::move(h), m.sequence_); | |
| } | |
| private: | |
| FullSequence sequence_; | |
| }; | |
| // A map of outgoing data streams indexed by object sequence numbers. | |
| class QUICHE_EXPORT SendStreamMap { | |
| public: | |
| explicit SendStreamMap(MoqtForwardingPreference forwarding_preference) | |
| : forwarding_preference_(forwarding_preference) {} | |
| std::optional<webtransport::StreamId> GetStreamForSequence( | |
| FullSequence sequence) const; | |
| void AddStream(FullSequence sequence, webtransport::StreamId stream_id); | |
| void RemoveStream(FullSequence sequence, webtransport::StreamId stream_id); | |
| std::vector<webtransport::StreamId> GetAllStreams() const; | |
| std::vector<webtransport::StreamId> GetStreamsForGroup( | |
| uint64_t group_id) const; | |
| private: | |
| using Group = absl::btree_map<uint64_t, webtransport::StreamId>; | |
| absl::btree_map<uint64_t, Group> send_streams_; | |
| MoqtForwardingPreference forwarding_preference_; | |
| }; | |
| } // namespace moqt | |
| #endif // QUICHE_QUIC_MOQT_SUBSCRIBE_WINDOWS_H |