Update MoQT Stream Header subgroup to draft-12.

The type can now encode that there is an end of group in the stream.

There are deep flaws in the specification for this, so this CL is close to the minimum diff to maintain interoperability until the design changes substantially. As a result, there are no new tests for this functionality and most existing tests use the non-end-of-group stream types.

PiperOrigin-RevId: 792708909
diff --git a/quiche/quic/moqt/moqt_messages.h b/quiche/quic/moqt/moqt_messages.h
index 4c19e6a..175508b 100644
--- a/quiche/quic/moqt/moqt_messages.h
+++ b/quiche/quic/moqt/moqt_messages.h
@@ -29,6 +29,7 @@
 #include "quiche/quic/moqt/moqt_priority.h"
 #include "quiche/common/platform/api/quiche_bug_tracker.h"
 #include "quiche/common/platform/api/quiche_export.h"
+#include "quiche/common/platform/api/quiche_logging.h"
 #include "quiche/common/quiche_callbacks.h"
 #include "quiche/common/quiche_data_writer.h"
 #include "quiche/web_transport/web_transport.h"
@@ -115,80 +116,77 @@
 
 class QUICHE_EXPORT MoqtDataStreamType {
  public:
+  static constexpr uint64_t kFetch = 0x05;
+  static constexpr uint64_t kPadding = 0x26d3;
+  static constexpr uint64_t kSubgroupFlag = 0x10;
+  static constexpr uint64_t kExtensionFlag = 0x01;
+  static constexpr uint64_t kEndOfGroupFlag = 0x08;
+  // These two cannot simultaneously be true;
+  static constexpr uint64_t kFirstObjectIdFlag = 0x02;
+  static constexpr uint64_t kSubgroupIdFlag = 0x04;
+
   // Factory functions.
   static std::optional<MoqtDataStreamType> FromValue(uint64_t value) {
-    MoqtDataStreamType stream_type(static_cast<StreamType>(value));
+    MoqtDataStreamType stream_type(value);
     if (stream_type.IsFetch() || stream_type.IsPadding() ||
         stream_type.IsSubgroup()) {
       return stream_type;
     }
     return std::nullopt;
   }
-  static MoqtDataStreamType Fetch() {
-    return MoqtDataStreamType(StreamType::kFetch);
-  }
-  static MoqtDataStreamType Padding() {
-    return MoqtDataStreamType(StreamType::kPadding);
-  }
+  static MoqtDataStreamType Fetch() { return MoqtDataStreamType(kFetch); }
+  static MoqtDataStreamType Padding() { return MoqtDataStreamType(kPadding); }
   static MoqtDataStreamType Subgroup(uint64_t subgroup_id,
                                      uint64_t first_object_id,
-                                     bool no_extension_headers) {
+                                     bool no_extension_headers,
+                                     bool end_of_group = false) {
+    uint64_t value = kSubgroupFlag;
+    if (!no_extension_headers) {
+      value |= kExtensionFlag;
+    }
+    if (end_of_group) {
+      value |= kEndOfGroupFlag;
+    }
     if (subgroup_id == 0) {
-      return MoqtDataStreamType(
-          no_extension_headers ? StreamType::kSubgroup0NoExtensionHeaders
-                               : StreamType::kSubgroup0WithExtensionHeaders);
+      return MoqtDataStreamType(value);
     }
     if (subgroup_id == first_object_id) {
-      return MoqtDataStreamType(
-          no_extension_headers
-              ? StreamType::kSubgroupFirstObjectNoExtensionHeaders
-              : StreamType::kSubgroupFirstObjectWithExtensionHeaders);
+      value |= kFirstObjectIdFlag;
+    } else {
+      value |= kSubgroupIdFlag;
     }
-    return MoqtDataStreamType(
-        no_extension_headers
-            ? StreamType::kSubgroupExplicitNoExtensionHeaders
-            : StreamType::kSubgroupExplicitWithExtensionHeaders);
+    return MoqtDataStreamType(value);
   }
   MoqtDataStreamType(const MoqtDataStreamType& other) = default;
-  bool IsFetch() const { return value_ == StreamType::kFetch; }
-  bool IsPadding() const { return value_ == StreamType::kPadding; }
+  bool IsFetch() const { return value_ == kFetch; }
+  bool IsPadding() const { return value_ == kPadding; }
   bool IsSubgroup() const {
-    return value_ >= StreamType::kSubgroup0NoExtensionHeaders &&
-           value_ <= StreamType::kSubgroupExplicitWithExtensionHeaders;
+    return (value_ & kSubgroupFlag) && (value_ & ~0x1f) == 0 &&
+           !((value_ & kSubgroupIdFlag) && (value_ & kFirstObjectIdFlag));
   }
   bool IsSubgroupPresent() const {
-    return value_ == StreamType::kSubgroupExplicitNoExtensionHeaders ||
-           value_ == StreamType::kSubgroupExplicitWithExtensionHeaders;
+    return IsSubgroup() && (value_ & kSubgroupIdFlag);
   }
   bool SubgroupIsZero() const {
-    return value_ == StreamType::kSubgroup0NoExtensionHeaders ||
-           value_ == StreamType::kSubgroup0WithExtensionHeaders;
+    return IsSubgroup() && !(value_ & kSubgroupIdFlag) &&
+           !(value_ & kFirstObjectIdFlag);
   }
   bool SubgroupIsFirstObjectId() const {
-    return value_ == StreamType::kSubgroupFirstObjectNoExtensionHeaders ||
-           value_ == StreamType::kSubgroupFirstObjectWithExtensionHeaders;
+    return IsSubgroup() && (value_ & kFirstObjectIdFlag);
   }
   bool AreExtensionHeadersPresent() const {
-    return value_ == StreamType::kSubgroup0WithExtensionHeaders ||
-           value_ == StreamType::kSubgroupFirstObjectWithExtensionHeaders ||
-           value_ == StreamType::kSubgroupExplicitWithExtensionHeaders;
+    return IsSubgroup() && (value_ & kExtensionFlag);
   }
-  uint64_t value() const { return static_cast<uint64_t>(value_); }
+  bool EndOfGroupInStream() const {
+    return IsSubgroup() && (value_ & kEndOfGroupFlag);
+  }
+
+  uint64_t value() const { return value_; }
   bool operator==(const MoqtDataStreamType& other) const = default;
-  enum class StreamType : uint64_t {
-    kFetch = 0x05,
-    kSubgroup0NoExtensionHeaders = 0x08,
-    kSubgroup0WithExtensionHeaders = 0x09,
-    kSubgroupFirstObjectNoExtensionHeaders = 0x0a,
-    kSubgroupFirstObjectWithExtensionHeaders = 0x0b,
-    kSubgroupExplicitNoExtensionHeaders = 0x0c,
-    kSubgroupExplicitWithExtensionHeaders = 0x0d,
-    kPadding = 0x26d3,
-  };
 
  private:
-  explicit MoqtDataStreamType(StreamType value) : value_(value) {}
-  const StreamType value_;
+  explicit MoqtDataStreamType(uint64_t value) : value_(value) {}
+  const uint64_t value_;
 };
 
 class QUICHE_EXPORT MoqtDatagramType {
diff --git a/quiche/quic/moqt/moqt_parser.cc b/quiche/quic/moqt/moqt_parser.cc
index c623693..eacc152 100644
--- a/quiche/quic/moqt/moqt_parser.cc
+++ b/quiche/quic/moqt/moqt_parser.cc
@@ -1421,6 +1421,7 @@
       break;
     case kStatus:
     case kData:
+    case kAwaitingNextByte:
       next_input_ = type_->IsFetch() ? kGroupId : kObjectId;
       break;
     case kExtensionSize:        // Either kExtensionBody or
@@ -1454,6 +1455,9 @@
         next_input_ = kPadding;
         return;
       }
+      if (type_->EndOfGroupInStream()) {
+        contains_end_of_group_ = true;
+      }
       AdvanceParserState();
       return;
     }
@@ -1540,6 +1544,12 @@
         }
 
         ++num_objects_read_;
+        // TODO(martinduke): If contains_end_of_group_ && fin_read, the track is
+        // malformed. There is no API to signal this to the session yet, but the
+        // contains_end_of_group_ logic is likely to substantially change in the
+        // spec. Don't bother to signal this for now; just ignore that the
+        // stream was supposed to conclude with kEndOfGroup and end it with the
+        // encoded status instead.
         visitor_.OnObjectMessage(metadata_, "", /*end_of_message=*/true);
         AdvanceParserState();
       }
@@ -1563,15 +1573,35 @@
         payload_length_remaining_ -= chunk_size;
         bool done = payload_length_remaining_ == 0;
         if (next_input_ == kData) {
+          no_more_data_ = peek_result.all_data_received &&
+                          chunk_size == peek_result.peeked_data.size();
+          if (!done && no_more_data_) {
+            ParseError("FIN received at an unexpected point in the stream");
+            return;
+          }
+          if (contains_end_of_group_) {
+            if (no_more_data_) {
+              metadata_.object_status = MoqtObjectStatus::kEndOfGroup;
+            } else if (done) {
+              // Don't signal done until the next byte arrives.
+              next_input_ = kAwaitingNextByte;
+              done = false;
+            }
+          }
           visitor_.OnObjectMessage(
               metadata_, peek_result.peeked_data.substr(0, chunk_size), done);
-          no_more_data_ = stream_.SkipBytes(chunk_size);
           if (done) {
             ++num_objects_read_;
             AdvanceParserState();
-          } else if (no_more_data_) {
-            ParseError("FIN received at an unexpected point in the stream");
-            return;
+          }
+          if (stream_.SkipBytes(chunk_size) && !no_more_data_) {
+            // Although there was no FIN, SkipBytes() can return true if the
+            // stream is reset, probably because OnObjectMessage() caused
+            // something to happen to the stream or the session.
+            no_more_data_ = true;
+            if (!done) {
+              ParseError("FIN received at an unexpected point in the stream");
+            }
           }
         } else {
           absl::StrAppend(&metadata_.extension_headers,
@@ -1589,6 +1619,11 @@
       return;
     }
 
+    case kAwaitingNextByte: {
+      QUICHE_NOTREACHED();  // CheckForFinWithoutData() should have handled it.
+      return;
+    }
+
     case kPadding:
       no_more_data_ |= stream_.SkipBytes(stream_.ReadableBytes());
       return;
@@ -1619,6 +1654,11 @@
 
 bool MoqtDataParser::CheckForFinWithoutData() {
   if (!stream_.PeekNextReadableRegion().fin_next) {
+    if (next_input_ == kAwaitingNextByte) {
+      // Data arrived; the last object was not EndOfGroup.
+      visitor_.OnObjectMessage(metadata_, "", /*end_of_message=*/true);
+      AdvanceParserState();
+    }
     return false;
   }
   no_more_data_ = true;
@@ -1630,6 +1670,10 @@
     ParseError("FIN received at an unexpected point in the stream");
     return true;
   }
+  if (next_input_ == kAwaitingNextByte) {
+    metadata_.object_status = MoqtObjectStatus::kEndOfGroup;
+    visitor_.OnObjectMessage(metadata_, "", /*end_of_message=*/true);
+  }
   return stream_.SkipBytes(0);
 }
 
diff --git a/quiche/quic/moqt/moqt_parser.h b/quiche/quic/moqt/moqt_parser.h
index 2df5cdf..d56c394 100644
--- a/quiche/quic/moqt/moqt_parser.h
+++ b/quiche/quic/moqt/moqt_parser.h
@@ -79,6 +79,8 @@
   // OBJECT payload. If not, there will be subsequent calls with further payload
   // data. The parser retains ownership of |message| and |payload|, so the
   // visitor needs to copy anything it wants to retain.
+  // If `message.object_status` == `kNormal`, the status must not be used until
+  // `end_of_message` is true, since a FIN can change the status.
   virtual void OnObjectMessage(const MoqtObject& message,
                                absl::string_view payload,
                                bool end_of_message) = 0;
@@ -224,6 +226,7 @@
     kObjectPayloadLength,
     kStatus,
     kData,
+    kAwaitingNextByte,  // Can't determine status until the next byte arrives.
     kPadding,
     kFailed,
   };
@@ -263,6 +266,8 @@
 
   bool no_more_data_ = false;  // Fatal error or fin. No more parsing.
   bool parsing_error_ = false;
+  bool contains_end_of_group_ = false;  // True if the stream contains an
+                                        // implied END_OF_GROUP object.
 
   std::string buffered_message_;
 
diff --git a/quiche/quic/moqt/moqt_parser_test.cc b/quiche/quic/moqt/moqt_parser_test.cc
index d93b3f0..dafe27c 100644
--- a/quiche/quic/moqt/moqt_parser_test.cc
+++ b/quiche/quic/moqt/moqt_parser_test.cc
@@ -92,7 +92,7 @@
       params.push_back(MoqtParserTestParams(message_type, true));
     }
   }
-  for (MoqtDataStreamType type : kMoqtDataStreamTypes) {
+  for (MoqtDataStreamType type : AllMoqtDataStreamTypes()) {
     params.push_back(MoqtParserTestParams(type));
   }
   return params;
@@ -161,6 +161,7 @@
 
 TEST_P(MoqtParserTest, OneMessage) {
   std::unique_ptr<TestMessageBase> message = MakeMessage();
+  message->MakeObjectEndOfStream();
   ProcessData(message->PacketSample(), true);
   ASSERT_EQ(visitor_.messages_received_, 1);
   EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_));
@@ -185,6 +186,7 @@
 
 TEST_P(MoqtParserTest, TwoPartMessage) {
   std::unique_ptr<TestMessageBase> message = MakeMessage();
+  message->MakeObjectEndOfStream();
   // The test Object message has payload for less then half the message length,
   // so splitting the message in half will prevent the first half from being
   // processed.
@@ -206,6 +208,7 @@
 
 TEST_P(MoqtParserTest, OneByteAtATime) {
   std::unique_ptr<TestMessageBase> message = MakeMessage();
+  message->MakeObjectEndOfStream();
   for (size_t i = 0; i < message->total_message_size(); ++i) {
     EXPECT_EQ(visitor_.messages_received_, 0);
     EXPECT_FALSE(visitor_.end_of_message_);
@@ -224,6 +227,7 @@
 TEST_P(MoqtParserTest, OneByteAtATimeLongerVarints) {
   std::unique_ptr<TestMessageBase> message = MakeMessage();
   message->ExpandVarints();
+  message->MakeObjectEndOfStream();
   for (size_t i = 0; i < message->total_message_size(); ++i) {
     EXPECT_EQ(visitor_.messages_received_, 0);
     EXPECT_FALSE(visitor_.end_of_message_);
@@ -241,10 +245,11 @@
 
 TEST_P(MoqtParserTest, TwoBytesAtATime) {
   std::unique_ptr<TestMessageBase> message = MakeMessage();
+  message->MakeObjectEndOfStream();
   for (size_t i = 0; i < message->total_message_size(); i += 3) {
     EXPECT_EQ(visitor_.messages_received_, 0);
     EXPECT_FALSE(visitor_.end_of_message_);
-    bool last = (i + 2) >= message->total_message_size();
+    bool last = (i + 3) >= message->total_message_size();
     ProcessData(message->PacketSample().substr(i, 3), last);
   }
   EXPECT_EQ(visitor_.messages_received_, 1);
@@ -1005,7 +1010,7 @@
   webtransport::test::InMemoryStream stream(/*stream_id=*/0);
   MoqtDataParser parser(&stream, &visitor_);
   char stream_header_subgroup[] = {
-      0x0d,                    // type field
+      0x15,                    // type field
       0x04, 0x05, 0x08,        // varints
       0x07,                    // publisher priority
       0x06, 0x00, 0x00, 0x0f,  // object middler; status = 0x0f
diff --git a/quiche/quic/moqt/moqt_session.cc b/quiche/quic/moqt/moqt_session.cc
index 1157031..b41d74e 100644
--- a/quiche/quic/moqt/moqt_session.cc
+++ b/quiche/quic/moqt/moqt_session.cc
@@ -2279,7 +2279,7 @@
       // Always include extension header length, because it's difficult to know
       // a priori if they're going to appear on a stream.
       stream_type_(MoqtDataStreamType::Subgroup(
-          index_.subgroup, parameters.first_object, false)),
+          index_.subgroup, parameters.first_object, false, false)),
       next_object_(parameters.first_object),
       session_liveness_(session->liveness_token_) {
   UpdateSendOrder(subscription);
diff --git a/quiche/quic/moqt/moqt_session_test.cc b/quiche/quic/moqt/moqt_session_test.cc
index 790db2a..c290db0 100644
--- a/quiche/quic/moqt/moqt_session_test.cc
+++ b/quiche/quic/moqt/moqt_session_test.cc
@@ -1248,7 +1248,7 @@
 
   // Verify first four message fields are sent correctly
   bool correct_message = false;
-  const std::string kExpectedMessage = {0x09, 0x02, 0x05, 0x7f};
+  const std::string kExpectedMessage = {0x11, 0x02, 0x05, 0x7f};
   EXPECT_CALL(mock_stream_, Writev(_, _))
       .WillOnce([&](absl::Span<quiche::QuicheMemSlice> data,
                     const quiche::StreamWriteOptions& options) {
@@ -1301,7 +1301,7 @@
 
   // Verify first four message fields are sent correctly
   bool correct_message = false;
-  const std::string kExpectedMessage = {0x09, 0x02, 0x05, 0x7f};
+  const std::string kExpectedMessage = {0x11, 0x02, 0x05, 0x7f};
   EXPECT_CALL(mock_stream_, Writev(_, _))
       .WillOnce([&](absl::Span<quiche::QuicheMemSlice> data,
                     const quiche::StreamWriteOptions& options) {
@@ -1352,7 +1352,7 @@
 
   // Verify first four message fields are sent correctly
   bool correct_message = false;
-  const std::string kExpectedMessage = {0x09, 0x02, 0x05, 0x7f};
+  const std::string kExpectedMessage = {0x11, 0x02, 0x05, 0x7f};
   EXPECT_CALL(mock_stream_, Writev(_, _))
       .WillOnce([&](absl::Span<quiche::QuicheMemSlice> data,
                     const quiche::StreamWriteOptions& options) {
@@ -1417,7 +1417,7 @@
 
   // Verify first four message fields are sent correctly
   bool correct_message = false;
-  const std::string kExpectedMessage = {0x09, 0x02, 0x05, 0x7f};
+  const std::string kExpectedMessage = {0x11, 0x02, 0x05, 0x7f};
   EXPECT_CALL(mock_stream_, Writev(_, _))
       .WillOnce([&](absl::Span<quiche::QuicheMemSlice> data,
                     const quiche::StreamWriteOptions& options) {
@@ -1484,7 +1484,7 @@
 
   // Verify first four message fields are sent correctly
   bool correct_message = false;
-  const std::string kExpectedMessage = {0x09, 0x02, 0x05, 0x7f};
+  const std::string kExpectedMessage = {0x11, 0x02, 0x05, 0x7f};
   EXPECT_CALL(mock_stream_, Writev(_, _))
       .WillOnce([&](absl::Span<quiche::QuicheMemSlice> data,
                     const quiche::StreamWriteOptions& options) {
@@ -1537,7 +1537,7 @@
 
   // Verify first four message fields are sent correctly
   bool correct_message = false;
-  const std::string kExpectedMessage = {0x09, 0x02, 0x05, 0x7f};
+  const std::string kExpectedMessage = {0x11, 0x02, 0x05, 0x7f};
   EXPECT_CALL(mock_stream_, Writev)
       .WillOnce([&](absl::Span<quiche::QuicheMemSlice> data,
                     const quiche::StreamWriteOptions& options) {
diff --git a/quiche/quic/moqt/test_tools/moqt_test_message.h b/quiche/quic/moqt/test_tools/moqt_test_message.h
index 850af54..211b5e0 100644
--- a/quiche/quic/moqt/test_tools/moqt_test_message.h
+++ b/quiche/quic/moqt/test_tools/moqt_test_message.h
@@ -39,16 +39,20 @@
     // Cannot have status and end_of_group both be true.
 };
 
-const MoqtDataStreamType kMoqtDataStreamTypes[] = {
-    MoqtDataStreamType::Fetch(),
-    MoqtDataStreamType::Subgroup(0, 1, true),
-    MoqtDataStreamType::Subgroup(0, 1, false),
-    MoqtDataStreamType::Subgroup(1, 1, true),
-    MoqtDataStreamType::Subgroup(1, 1, false),
-    MoqtDataStreamType::Subgroup(2, 1, true),
-    MoqtDataStreamType::Subgroup(2, 1, false),
-    // Padding omitted.
-};
+inline std::vector<MoqtDataStreamType> AllMoqtDataStreamTypes() {
+  std::vector<MoqtDataStreamType> types;
+  types.push_back(MoqtDataStreamType::Fetch());
+  uint64_t first_object_id = 1;
+  for (uint64_t subgroup_id : {0, 1, 2}) {
+    for (bool no_extension_headers : {true, false}) {
+      for (bool end_of_group : {false, true}) {
+        types.push_back(MoqtDataStreamType::Subgroup(
+            subgroup_id, first_object_id, no_extension_headers, end_of_group));
+      }
+    }
+  }
+  return types;
+}
 
 // Base class containing a wire image and the corresponding structured
 // representation of an example of each message. It allows parser and framer
@@ -104,6 +108,9 @@
     set_wire_image_size(wire_image_size_ + 1);
   }
 
+  // Objects might need a different status if at the end of the stream.
+  virtual void MakeObjectEndOfStream() {}
+
  protected:
   void SetWireImage(uint8_t* wire_image, size_t wire_image_size) {
     memcpy(wire_image_, wire_image, wire_image_size);
@@ -357,6 +364,12 @@
     return true;
   }
 
+  void MakeObjectEndOfStream() override {
+    if (type_.EndOfGroupInStream()) {
+      object_.object_status = MoqtObjectStatus::kEndOfGroup;
+    }
+  }
+
  private:
   MoqtDataStreamType type_;
   static constexpr absl::string_view kRawBeginning = "\x04\x05";