Keep spatial_idx=kNoSpatialIdx(255) if there is no layer indices.

spatial_idx is not present in RTP header if there is no temporal or
spatial layering. But the parser sets spatial_idx to 0 in this case.
When reflector repacketizes such packets it writes layering indices
into outgoing packets. When packets arrive to receiver it thinks that
it deals with multi layer stream and passes it through special path
in Vp9 reference frame finder which never outputs inter frames.

I modified the parser such that it keeps spatial_idx=kNoSpatialIdx(255)
when there is no layer indices in RTP header. Related unit tests have
been modified as well.

Bug: none
Change-Id: I14498cafb4e57797577dc873298c35b243479f88
Reviewed-on: https://webrtc-review.googlesource.com/17980
Commit-Queue: Sergey Silkin <ssilkin@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Åsa Persson <asapersson@webrtc.org>
Reviewed-by: Rasmus Brandt <brandtr@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20560}
diff --git a/modules/rtp_rtcp/source/rtp_format_vp9.cc b/modules/rtp_rtcp/source/rtp_format_vp9.cc
index 89a9848..2623790 100644
--- a/modules/rtp_rtcp/source/rtp_format_vp9.cc
+++ b/modules/rtp_rtcp/source/rtp_format_vp9.cc
@@ -721,7 +721,6 @@
   vp9->beginning_of_frame = b_bit ? true : false;
   vp9->end_of_frame = e_bit ? true : false;
   vp9->ss_data_available = v_bit ? true : false;
-  vp9->spatial_idx = 0;
 
   // Parse fields that are present.
   if (i_bit && !ParsePictureId(&parser, vp9)) {
diff --git a/modules/rtp_rtcp/source/rtp_format_vp9_unittest.cc b/modules/rtp_rtcp/source/rtp_format_vp9_unittest.cc
index 468f77b..c522c4d 100644
--- a/modules/rtp_rtcp/source/rtp_format_vp9_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_format_vp9_unittest.cc
@@ -30,8 +30,7 @@
   EXPECT_EQ(expected.picture_id, actual.picture_id);
   EXPECT_EQ(expected.max_picture_id, actual.max_picture_id);
   EXPECT_EQ(expected.temporal_idx, actual.temporal_idx);
-  EXPECT_EQ(expected.spatial_idx == kNoSpatialIdx ? 0 : expected.spatial_idx,
-      actual.spatial_idx);
+  EXPECT_EQ(expected.spatial_idx, actual.spatial_idx);
   EXPECT_EQ(expected.gof_idx, actual.gof_idx);
   EXPECT_EQ(expected.tl0_pic_idx, actual.tl0_pic_idx);
   EXPECT_EQ(expected.temporal_up_switch, actual.temporal_up_switch);
diff --git a/video/video_send_stream_tests.cc b/video/video_send_stream_tests.cc
index f417dde..93df93f 100644
--- a/video/video_send_stream_tests.cc
+++ b/video/video_send_stream_tests.cc
@@ -3114,8 +3114,23 @@
     EXPECT_EQ(kMaxTwoBytePictureId, vp9.max_picture_id);       // M:1
     EXPECT_NE(kNoPictureId, vp9.picture_id);                   // I:1
     EXPECT_EQ(vp9_settings_.flexibleMode, vp9.flexible_mode);  // F
-    EXPECT_GE(vp9.spatial_idx, 0);                             // S
-    EXPECT_LT(vp9.spatial_idx, vp9_settings_.numberOfSpatialLayers);
+
+    if (vp9_settings_.numberOfSpatialLayers > 1) {
+      EXPECT_LT(vp9.spatial_idx, vp9_settings_.numberOfSpatialLayers);
+    } else if (vp9_settings_.numberOfTemporalLayers > 1) {
+      EXPECT_EQ(vp9.spatial_idx, 0);
+    } else {
+      EXPECT_EQ(vp9.spatial_idx, kNoSpatialIdx);
+    }
+
+    if (vp9_settings_.numberOfTemporalLayers > 1) {
+      EXPECT_LT(vp9.temporal_idx, vp9_settings_.numberOfTemporalLayers);
+    } else if (vp9_settings_.numberOfSpatialLayers > 1) {
+      EXPECT_EQ(vp9.temporal_idx, 0);
+    } else {
+      EXPECT_EQ(vp9.temporal_idx, kNoTemporalIdx);
+    }
+
     if (vp9.ss_data_available)  // V
       VerifySsData(vp9);
 
@@ -3257,16 +3272,27 @@
     }
 
     void InspectHeader(const RTPVideoHeaderVP9& vp9) override {
-      bool ss_data_expected = !vp9.inter_pic_predicted &&
-                              vp9.beginning_of_frame && vp9.spatial_idx == 0;
+      bool ss_data_expected =
+          !vp9.inter_pic_predicted && vp9.beginning_of_frame &&
+          (vp9.spatial_idx == 0 || vp9.spatial_idx == kNoSpatialIdx);
       EXPECT_EQ(ss_data_expected, vp9.ss_data_available);
-      EXPECT_EQ(vp9.spatial_idx > 0, vp9.inter_layer_predicted);  // D
+      if (num_spatial_layers_ > 1) {
+        EXPECT_EQ(vp9.spatial_idx > 0, vp9.inter_layer_predicted);
+      } else {
+        EXPECT_FALSE(vp9.inter_layer_predicted);
+      }
+
       EXPECT_EQ(!vp9.inter_pic_predicted,
                 frames_sent_ % kKeyFrameInterval == 0);
 
       if (IsNewPictureId(vp9)) {
-        EXPECT_EQ(0, vp9.spatial_idx);
-        EXPECT_EQ(num_spatial_layers_ - 1, last_vp9_.spatial_idx);
+        if (num_temporal_layers_ == 1 && num_spatial_layers_ == 1) {
+          EXPECT_EQ(kNoSpatialIdx, vp9.spatial_idx);
+        } else {
+          EXPECT_EQ(0, vp9.spatial_idx);
+        }
+        if (num_spatial_layers_ > 1)
+          EXPECT_EQ(num_spatial_layers_ - 1, last_vp9_.spatial_idx);
       }
 
       VerifyFixedTemporalLayerStructure(vp9,