Skip to content

Commit

Permalink
cli_test_utils: Use absl::Span passing substream IDs to create au…
Browse files Browse the repository at this point in the history
…dio elements.

  - Convenient to use with `constexpr` array-based values when many substreams are required (e.g. 4OA).
  - Especially useful to name a type when it enhances readability (e.g. to distinguish between 1OA or 4OA ambisonics).
  - When substreams are tangential to the test the initializer list pattern still reads better (e.g. `{kFirstSubstreamId}`).
  - Drive-by: Remove some incidental unused args.

PiperOrigin-RevId: 697644158
  • Loading branch information
jwcullen committed Nov 19, 2024
1 parent 2b6f3a1 commit 9fbe55d
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 47 deletions.
1 change: 1 addition & 0 deletions iamf/cli/proto_to_obu/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ cc_test(
"//iamf/obu:types",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/types:span",
"@com_google_googletest//:gtest_main",
"@com_google_protobuf//:protobuf",
],
Expand Down
15 changes: 9 additions & 6 deletions iamf/cli/proto_to_obu/tests/parameter_block_generator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
*/
#include "iamf/cli/proto_to_obu/parameter_block_generator.h"

#include <array>
#include <cstdint>
#include <list>
#include <memory>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/status/status_matchers.h"
#include "absl/types/span.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "iamf/cli/audio_element_with_data.h"
Expand All @@ -44,11 +46,12 @@ using ::absl_testing::IsOk;

constexpr DecodedUleb128 kCodecConfigId = 200;
constexpr DecodedUleb128 kAudioElementId = 300;
constexpr DecodedUleb128 kMixPresentationId = 1337;
constexpr DecodedUleb128 kParameterId = 100;
constexpr DecodedUleb128 kParameterRate = 48000;
constexpr DecodedUleb128 kDuration = 8;
constexpr bool kOverrideComputedReconGains = false;
constexpr std::array<DecodedUleb128, 1> kOneSubstreamId{0};
constexpr std::array<DecodedUleb128, 4> kFourSubtreamIds{0, 1, 2, 3};

TEST(ParameterBlockGeneratorTest, NoParameterBlocks) {
absl::flat_hash_map<DecodedUleb128, PerIdParameterMetadata>
Expand Down Expand Up @@ -109,7 +112,7 @@ void ConfigureDemixingParameterBlocks(
}

void InitializePrerequisiteObus(
const std::vector<DecodedUleb128>& substream_ids,
absl::Span<const DecodedUleb128> substream_ids,
absl::flat_hash_map<DecodedUleb128, CodecConfigObu>& codec_config_obus,
absl::flat_hash_map<DecodedUleb128, AudioElementWithData>& audio_elements) {
constexpr uint32_t kSampleRate = 48000;
Expand Down Expand Up @@ -153,7 +156,7 @@ TEST(ParameterBlockGeneratorTest, GenerateTwoDemixingParameterBlocks) {
// Initialize pre-requisite OBUs.
absl::flat_hash_map<DecodedUleb128, CodecConfigObu> codec_config_obus;
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> audio_elements;
InitializePrerequisiteObus(/*substream_ids=*/{0}, codec_config_obus,
InitializePrerequisiteObus(kOneSubstreamId, codec_config_obus,
audio_elements);

// Add a demixing parameter definition inside the Audio Element OBU.
Expand Down Expand Up @@ -268,7 +271,7 @@ TEST(ParameterBlockGeneratorTest, GenerateMixGainParameterBlocks) {
// Initialize pre-requisite OBUs.
absl::flat_hash_map<DecodedUleb128, CodecConfigObu> codec_config_obus;
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> audio_elements;
InitializePrerequisiteObus(/*substream_ids=*/{0}, codec_config_obus,
InitializePrerequisiteObus(kOneSubstreamId, codec_config_obus,
audio_elements);

// Add param definition. It would normally be owned by a Mix Presentation OBU.
Expand Down Expand Up @@ -428,7 +431,7 @@ TEST(ParameterBlockGeneratorTest, GenerateReconGainParameterBlocks) {
// Initialize pre-requisite OBUs.
absl::flat_hash_map<DecodedUleb128, CodecConfigObu> codec_config_obus;
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> audio_elements;
InitializePrerequisiteObus(/*substream_ids=*/{0, 1, 2, 3}, codec_config_obus,
InitializePrerequisiteObus(kFourSubtreamIds, codec_config_obus,
audio_elements);

// Extra data needed to compute recon gain.
Expand Down Expand Up @@ -486,7 +489,7 @@ TEST(Initialize, FailsWhenThereAreStrayParameterBlocks) {
ConfigureDemixingParameterBlocks(user_metadata);
absl::flat_hash_map<DecodedUleb128, CodecConfigObu> codec_config_obus;
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> audio_elements;
InitializePrerequisiteObus(/*substream_ids=*/{0, 1, 2, 3}, codec_config_obus,
InitializePrerequisiteObus(kFourSubtreamIds, codec_config_obus,
audio_elements);

// Construct and initialize.
Expand Down
10 changes: 5 additions & 5 deletions iamf/cli/tests/cli_test_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void AddAacCodecConfigWithId(

void AddAmbisonicsMonoAudioElementWithSubstreamIds(
DecodedUleb128 audio_element_id, uint32_t codec_config_id,
const std::vector<DecodedUleb128>& substream_ids,
absl::Span<const DecodedUleb128> substream_ids,
const absl::flat_hash_map<uint32_t, CodecConfigObu>& codec_config_obus,
absl::flat_hash_map<DecodedUleb128, AudioElementWithData>& audio_elements) {
// Check the `codec_config_id` is known and this is a new
Expand All @@ -187,10 +187,9 @@ void AddAmbisonicsMonoAudioElementWithSubstreamIds(
AudioElementObu obu = AudioElementObu(
ObuHeader(), audio_element_id, AudioElementObu::kAudioElementSceneBased,
0, codec_config_id);
obu.audio_substream_ids_ = substream_ids;
obu.InitializeParams(0);
obu.InitializeAudioSubstreams(substream_ids.size());
obu.audio_substream_ids_ = substream_ids;
obu.audio_substream_ids_.assign(substream_ids.begin(), substream_ids.end());

// Initialize to n-th order ambisonics. Choose the lowest order that can fit
// all `substream_ids`. This may result in mixed-order ambisonics.
Expand Down Expand Up @@ -226,7 +225,7 @@ void AddAmbisonicsMonoAudioElementWithSubstreamIds(
// Adds a scalable Audio Element OBU based on the input arguments.
void AddScalableAudioElementWithSubstreamIds(
DecodedUleb128 audio_element_id, uint32_t codec_config_id,
const std::vector<DecodedUleb128>& substream_ids,
absl::Span<const DecodedUleb128> substream_ids,
const absl::flat_hash_map<uint32_t, CodecConfigObu>& codec_config_obus,
absl::flat_hash_map<DecodedUleb128, AudioElementWithData>& audio_elements) {
// Check the `codec_config_id` is known and this is a new
Expand All @@ -239,7 +238,8 @@ void AddScalableAudioElementWithSubstreamIds(
AudioElementObu obu(ObuHeader(), audio_element_id,
AudioElementObu::kAudioElementChannelBased, 0,
codec_config_id);
obu.audio_substream_ids_ = substream_ids;
obu.InitializeAudioSubstreams(substream_ids.size());
obu.audio_substream_ids_.assign(substream_ids.begin(), substream_ids.end());
obu.InitializeParams(0);

EXPECT_THAT(obu.InitializeScalableChannelLayout(1, 0), IsOk());
Expand Down
5 changes: 2 additions & 3 deletions iamf/cli/tests/cli_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "iamf/cli/audio_element_with_data.h"
Expand Down Expand Up @@ -92,7 +91,7 @@ void AddAacCodecConfigWithId(
*/
void AddAmbisonicsMonoAudioElementWithSubstreamIds(
DecodedUleb128 audio_element_id, uint32_t codec_config_id,
const std::vector<DecodedUleb128>& substream_ids,
absl::Span<const DecodedUleb128> substream_ids,
const absl::flat_hash_map<uint32_t, CodecConfigObu>& codec_config_obus,
absl::flat_hash_map<DecodedUleb128, AudioElementWithData>& audio_elements);

Expand All @@ -106,7 +105,7 @@ void AddAmbisonicsMonoAudioElementWithSubstreamIds(
*/
void AddScalableAudioElementWithSubstreamIds(
DecodedUleb128 audio_element_id, uint32_t codec_config_id,
const std::vector<DecodedUleb128>& substream_ids,
absl::Span<const DecodedUleb128> substream_ids,
const absl::flat_hash_map<uint32_t, CodecConfigObu>& codec_config_obus,
absl::flat_hash_map<DecodedUleb128, AudioElementWithData>& audio_elements);

Expand Down
11 changes: 7 additions & 4 deletions iamf/cli/tests/cli_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
#include "iamf/cli/cli_util.h"

#include <array>
#include <cstddef>
#include <cstdint>
#include <list>
Expand Down Expand Up @@ -55,6 +56,8 @@ constexpr DecodedUleb128 kParameterId = 99999;
constexpr DecodedUleb128 kParameterRate = 48000;
constexpr DecodedUleb128 kFirstSubstreamId = 31;
constexpr DecodedUleb128 kSecondSubstreamId = 32;
constexpr std::array<DecodedUleb128, 1> kZerothOrderAmbisonicsSubstreamId{
kFirstSubstreamId};

TEST(WritePcmFrameToBuffer, ResizesOutputBuffer) {
const size_t kExpectedSize = 12; // 3 bytes per sample * 4 samples.
Expand Down Expand Up @@ -525,8 +528,8 @@ TEST(CollectAndValidateParamDefinitions,
const std::list<MixPresentationObu> kNoMixPresentationObus = {};
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> audio_elements;
AddAmbisonicsMonoAudioElementWithSubstreamIds(
kAudioElementId, kCodecConfigId, {kFirstSubstreamId}, input_codec_configs,
audio_elements);
kAudioElementId, kCodecConfigId, kZerothOrderAmbisonicsSubstreamId,
input_codec_configs, audio_elements);
auto& audio_element = audio_elements.at(kAudioElementId);
audio_element.obu.InitializeParams(1);
audio_element.obu.audio_element_params_[0] = AudioElementParam{
Expand All @@ -547,8 +550,8 @@ TEST(CollectAndValidateParamDefinitions,
const std::list<MixPresentationObu> kNoMixPresentationObus = {};
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> audio_elements;
AddAmbisonicsMonoAudioElementWithSubstreamIds(
kAudioElementId, kCodecConfigId, {kFirstSubstreamId}, input_codec_configs,
audio_elements);
kAudioElementId, kCodecConfigId, kZerothOrderAmbisonicsSubstreamId,
input_codec_configs, audio_elements);

// Add an extension param definition to the audio element. It is not possible
// to determine the ID to store it or to use further processing.
Expand Down
5 changes: 3 additions & 2 deletions iamf/cli/tests/demixing_module_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,13 @@ TEST(InitializeForReconstruction, CreatesNoDemixersForSingleLayerChannelBased) {

TEST(InitializeForReconstruction, CreatesNoDemixersForAmbisonics) {
const DecodedUleb128 kCodecConfigId = 0;
constexpr std::array<DecodedUleb128, 4> kAmbisonicsSubstreamIds{0, 1, 2, 3};
absl::flat_hash_map<DecodedUleb128, CodecConfigObu> codec_configs;
AddLpcmCodecConfigWithIdAndSampleRate(kCodecConfigId, 48000, codec_configs);
absl::flat_hash_map<DecodedUleb128, AudioElementWithData> audio_elements;
AddAmbisonicsMonoAudioElementWithSubstreamIds(kAudioElementId, kCodecConfigId,
{0, 1, 2, 3}, codec_configs,
audio_elements);
kAmbisonicsSubstreamIds,
codec_configs, audio_elements);
DemixingModule demixing_module;
EXPECT_THAT(demixing_module.InitializeForReconstruction(audio_elements),
IsOk());
Expand Down
25 changes: 13 additions & 12 deletions iamf/cli/tests/global_timing_module_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
Expand All @@ -39,16 +38,15 @@ const DecodedUleb128 kSampleRate = 48000;
const DecodedUleb128 kFirstAudioElementId = 0;
const DecodedUleb128 kFirstAudioFrameId = 1000;
const DecodedUleb128 kFirstParameterId = 0;
const DecodedUleb128 kParameterIdForLoggingPurposes = kFirstParameterId;

class GlobalTimingModuleTest : public ::testing::Test {
protected:
void InitializeForTestingValidateParameterBlockCoverage() {
AddLpcmCodecConfigWithIdAndSampleRate(kCodecConfigId, kSampleRate,
codec_config_obus_);
AddAmbisonicsMonoAudioElementWithSubstreamIds(
kFirstAudioElementId, kCodecConfigId, {1000}, codec_config_obus_,
audio_elements_);
kFirstAudioElementId, kCodecConfigId, {kFirstAudioFrameId},
codec_config_obus_, audio_elements_);
EXPECT_THAT(Initialize(), IsOk());

TestGetNextAudioFrameStamps(kFirstAudioFrameId, 512, 0, 512);
Expand Down Expand Up @@ -128,14 +126,15 @@ TEST_F(GlobalTimingModuleTest, OneSubstream) {
}

TEST_F(GlobalTimingModuleTest, InvalidUnknownSubstreamId) {
constexpr DecodedUleb128 kSubstreamId = 9999;
constexpr DecodedUleb128 kUnknownSubstreamId = 10000;
AddLpcmCodecConfigWithIdAndSampleRate(kCodecConfigId, kSampleRate,
codec_config_obus_);
AddAmbisonicsMonoAudioElementWithSubstreamIds(
kFirstAudioElementId, kCodecConfigId, {0}, codec_config_obus_,
kFirstAudioElementId, kCodecConfigId, {kSubstreamId}, codec_config_obus_,
audio_elements_);
EXPECT_THAT(Initialize(), IsOk());

const DecodedUleb128 kUnknownSubstreamId = 9999;
TestGetNextAudioFrameStamps(kUnknownSubstreamId, 128, 0, 128,
absl::StatusCode::kInvalidArgument);
}
Expand All @@ -152,22 +151,24 @@ TEST_F(GlobalTimingModuleTest, InvalidDuplicateSubstreamIds) {
}

TEST_F(GlobalTimingModuleTest, TwoAudioElements) {
constexpr DecodedUleb128 kFirstSubstreamId = kFirstAudioFrameId;
constexpr DecodedUleb128 kSecondSubstreamId = 2000;
AddLpcmCodecConfigWithIdAndSampleRate(kCodecConfigId, kSampleRate,
codec_config_obus_);
AddAmbisonicsMonoAudioElementWithSubstreamIds(
kFirstAudioElementId, kCodecConfigId, {kFirstAudioFrameId},
kFirstAudioElementId, kCodecConfigId, {kFirstSubstreamId},
codec_config_obus_, audio_elements_);
const DecodedUleb128 kSecondAudioElementId = 1;
ASSERT_NE(kFirstAudioElementId, kSecondAudioElementId);
AddAmbisonicsMonoAudioElementWithSubstreamIds(
kSecondAudioElementId, kCodecConfigId, {2000}, codec_config_obus_,
audio_elements_);
kSecondAudioElementId, kCodecConfigId, {kSecondSubstreamId},
codec_config_obus_, audio_elements_);
EXPECT_THAT(Initialize(), IsOk());

// All subtreams have separate time keeping functionality.
TestGetNextAudioFrameStamps(kFirstAudioFrameId, 128, 0, 128);
TestGetNextAudioFrameStamps(kFirstAudioFrameId, 128, 128, 256);
TestGetNextAudioFrameStamps(kFirstAudioFrameId, 128, 256, 384);
TestGetNextAudioFrameStamps(kFirstSubstreamId, 128, 0, 128);
TestGetNextAudioFrameStamps(kFirstSubstreamId, 128, 128, 256);
TestGetNextAudioFrameStamps(kFirstSubstreamId, 128, 256, 384);

TestGetNextAudioFrameStamps(2000, 256, 0, 256);
TestGetNextAudioFrameStamps(2000, 256, 256, 512);
Expand Down
10 changes: 6 additions & 4 deletions iamf/cli/tests/parameters_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ using ::absl_testing::IsOk;
constexpr DecodedUleb128 kCodecConfigId = 1450;
constexpr DecodedUleb128 kSampleRate = 16000;
constexpr DecodedUleb128 kAudioElementId = 157;
constexpr DecodedUleb128 kFirstSubstreamId = 0;
constexpr DecodedUleb128 kSecondSubstreamId = 1;
constexpr DecodedUleb128 kParameterId = 995;
constexpr DecodedUleb128 kSecondParameterId = 996;
constexpr DecodedUleb128 kDuration = 8;
Expand Down Expand Up @@ -112,8 +114,8 @@ class ParametersManagerTest : public testing::Test {
AddLpcmCodecConfigWithIdAndSampleRate(kCodecConfigId, kSampleRate,
codec_config_obus_);
AddAmbisonicsMonoAudioElementWithSubstreamIds(
kAudioElementId, kCodecConfigId,
/*substream_ids=*/{100}, codec_config_obus_, audio_elements_);
kAudioElementId, kCodecConfigId, {kFirstSubstreamId},
codec_config_obus_, audio_elements_);

auto& audio_element_obu = audio_elements_.at(kAudioElementId).obu;
AddDemixingParamDefinition(kParameterId, kSampleRate, kDuration,
Expand Down Expand Up @@ -554,8 +556,8 @@ TEST_F(ParametersManagerTest,
// Add a second audio element sharing the same demixing parameter.
constexpr DecodedUleb128 kAudioElementId2 = kAudioElementId + 1;
AddAmbisonicsMonoAudioElementWithSubstreamIds(
kAudioElementId2, kCodecConfigId,
/*substream_ids=*/{200}, codec_config_obus_, audio_elements_);
kAudioElementId2, kCodecConfigId, {kSecondSubstreamId},
codec_config_obus_, audio_elements_);
auto& second_audio_element_obu = audio_elements_.at(kAudioElementId2).obu;
AddDemixingParamDefinition(kParameterId, kSampleRate, kDuration,
second_audio_element_obu,
Expand Down
10 changes: 6 additions & 4 deletions iamf/cli/tests/profile_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
#include "iamf/cli/profile_filter.h"

#include <array>
#include <cstdint>
#include <initializer_list>
#include <list>
Expand Down Expand Up @@ -54,7 +55,8 @@ const uint32_t kCommonMixGainParameterRate = kSampleRate;
const uint8_t kAudioElementReserved = 0;
const int kOneLayer = 1;

const std::vector<DecodedUleb128> kSubstreamIdsForFourthOrderAmbisonics = {
constexpr std::array<DecodedUleb128, 1> kZerothOrderAmbisonicsSubstreamId{100};
constexpr std::array<DecodedUleb128, 25> kFourthOrderAmbisonicsSubstreamIds = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};

Expand Down Expand Up @@ -369,7 +371,7 @@ void InitializeDescriptorObusForOneMonoAmbisonicsAudioElement(
AddLpcmCodecConfigWithIdAndSampleRate(kCodecConfigId, kSampleRate,
codec_config_obus);
AddAmbisonicsMonoAudioElementWithSubstreamIds(
kFirstAudioElementId, kCodecConfigId, {kFirstSubstreamId},
kFirstAudioElementId, kCodecConfigId, kZerothOrderAmbisonicsSubstreamId,
codec_config_obus, audio_elements);
AddMixPresentationObuWithAudioElementIds(
kFirstMixPresentationId, {kFirstAudioElementId},
Expand All @@ -384,8 +386,8 @@ void InitializeDescriptorObusForOneFourthOrderAmbisonicsAudioElement(
AddLpcmCodecConfigWithIdAndSampleRate(kCodecConfigId, kSampleRate,
codec_config_obus);
AddAmbisonicsMonoAudioElementWithSubstreamIds(
kFirstAudioElementId, kCodecConfigId,
kSubstreamIdsForFourthOrderAmbisonics, codec_config_obus, audio_elements);
kFirstAudioElementId, kCodecConfigId, kFourthOrderAmbisonicsSubstreamIds,
codec_config_obus, audio_elements);

AddMixPresentationObuWithAudioElementIds(
kFirstMixPresentationId, {kFirstAudioElementId},
Expand Down
9 changes: 5 additions & 4 deletions iamf/cli/tests/rendering_mix_presentation_finalizer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
#include "iamf/cli/rendering_mix_presentation_finalizer.h"

#include <array>
#include <cstddef>
#include <cstdint>
#include <filesystem>
Expand Down Expand Up @@ -175,12 +176,12 @@ constexpr uint8_t kCodecConfigBitDepth = 16;
void InitPrerequisiteObusForStereoInput(
absl::flat_hash_map<DecodedUleb128, CodecConfigObu>& codec_configs,
absl::flat_hash_map<DecodedUleb128, AudioElementWithData>& audio_elements) {
const std::vector<DecodedUleb128> kStereoSubstreamIds = {0};
constexpr std::array<DecodedUleb128, 1> kStereoSubstreamId{0};

AddLpcmCodecConfigWithIdAndSampleRate(kCodecConfigId, kSampleRate,
codec_configs);
AddScalableAudioElementWithSubstreamIds(kAudioElementId, kCodecConfigId,
kStereoSubstreamIds, codec_configs,
kStereoSubstreamId, codec_configs,
audio_elements);

// Fill in the first layer correctly for stereo input.
Expand All @@ -195,12 +196,12 @@ void InitPrerequisiteObusForStereoInput(
void InitPrerequisiteObusForMonoInput(
absl::flat_hash_map<DecodedUleb128, CodecConfigObu>& codec_configs,
absl::flat_hash_map<DecodedUleb128, AudioElementWithData>& audio_elements) {
const std::vector<DecodedUleb128> kMonoSubstreamIds = {0};
constexpr std::array<DecodedUleb128, 1> kMonoSubstreamId{0};

AddLpcmCodecConfigWithIdAndSampleRate(kCodecConfigId, kSampleRate,
codec_configs);
AddScalableAudioElementWithSubstreamIds(kAudioElementId, kCodecConfigId,
kMonoSubstreamIds, codec_configs,
kMonoSubstreamId, codec_configs,
audio_elements);

// Fill in the first layer correctly for mono input.
Expand Down
Loading

0 comments on commit 9fbe55d

Please sign in to comment.