Add audio view classes

From the new header file:
* MonoView<>: A single channel contiguous buffer of samples.
* InterleavedView<>: Channel samples are interleaved (side-by-side) in
  the buffer. A single channel InterleavedView<> is the same thing as a
  MonoView<>
* DeinterleavedView<>: Each channel's samples are contiguous within the
  buffer. Channels can be enumerated and accessing the
  individual channel data is done via MonoView<>.

There are also a few utility functions that offer a unified way to check
the properties regardless of what view type is in use.

Bug: chromium:335805780
Change-Id: I28196f8f4ded4fadc72ee32b62af304c62f4fc47
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/349300
Reviewed-by: Per Ã…hgren <peah@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42377}
diff --git a/audio/remix_resample.cc b/audio/remix_resample.cc
index 06752c2..26d0c85 100644
--- a/audio/remix_resample.cc
+++ b/audio/remix_resample.cc
@@ -50,11 +50,10 @@
         << "dst_frame->num_channels_: " << dst_frame->num_channels_;
 
     AudioFrameOperations::DownmixChannels(
-        rtc::ArrayView<const int16_t>(src_data,
-                                      num_channels * samples_per_channel),
-        num_channels, samples_per_channel, dst_frame->num_channels_,
-        rtc::ArrayView<int16_t>(&downmixed_audio[0], dst_frame->num_channels_ *
-                                                         samples_per_channel));
+        InterleavedView<const int16_t>(src_data, samples_per_channel,
+                                       num_channels),
+        InterleavedView<int16_t>(&downmixed_audio[0], samples_per_channel,
+                                 dst_frame->num_channels_));
     audio_ptr = downmixed_audio;
     audio_ptr_num_channels = dst_frame->num_channels_;
   }
@@ -71,30 +70,32 @@
   // resampler to return output length without doing the resample, so we know
   // how much to zero here; or 2) make resampler accept a hint that the input is
   // zeroed.
-  const size_t src_length = samples_per_channel * audio_ptr_num_channels;
+
   // Ensure the `samples_per_channel_` member is set correctly based on the
   // destination sample rate, number of channels and assumed 10ms buffer size.
   // TODO(tommi): Could we rather assume that this has been done by the caller?
   dst_frame->SetSampleRateAndChannelSize(dst_frame->sample_rate_hz_);
 
+  InterleavedView<const int16_t> src_view(audio_ptr, samples_per_channel,
+                                          audio_ptr_num_channels);
+  // Stash away the originally requested number of channels. Then provide
+  // `dst_frame` as a target buffer with the same number of channels as the
+  // source.
+  auto original_dst_number_of_channels = dst_frame->num_channels_;
   int out_length = resampler->Resample(
-      rtc::ArrayView<const int16_t>(audio_ptr, src_length),
-      dst_frame->mutable_data(dst_frame->samples_per_channel_,
-                              dst_frame->num_channels_));
-  if (out_length == -1) {
-    RTC_FATAL() << "Resample failed: audio_ptr = " << audio_ptr
-                << ", src_length = " << src_length
-                << ", dst_frame->mutable_data() = "
-                << dst_frame->mutable_data();
-  }
+      src_view, dst_frame->mutable_data(dst_frame->samples_per_channel_,
+                                        src_view.num_channels()));
+  RTC_CHECK_NE(out_length, -1) << "Resample failed: audio_ptr = " << audio_ptr
+                               << ", src_length = " << src_view.data().size();
 
-  dst_frame->samples_per_channel_ = out_length / audio_ptr_num_channels;
+  RTC_DCHECK_EQ(dst_frame->samples_per_channel(),
+                out_length / audio_ptr_num_channels);
 
   // Upmix after resampling.
-  if (num_channels == 1 && dst_frame->num_channels_ == 2) {
+  if (num_channels == 1 && original_dst_number_of_channels == 2) {
     // The audio in dst_frame really is mono at this point; MonoToStereo will
     // set this back to stereo.
-    dst_frame->num_channels_ = 1;
+    RTC_DCHECK_EQ(dst_frame->num_channels_, 1);
     AudioFrameOperations::UpmixChannels(2, dst_frame);
   }
 }