Skip to content

Commit

Permalink
Explicity check for supported IAMF codecs parameter strings
Browse files Browse the repository at this point in the history
b/356704307
  • Loading branch information
osagie98 committed Aug 28, 2024
1 parent 4efa101 commit de2e7ed
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
14 changes: 13 additions & 1 deletion starboard/shared/starboard/media/codec_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <algorithm>
#include <cctype>
#include <regex>
#include <string>

#include "starboard/common/log.h"
Expand Down Expand Up @@ -107,13 +108,24 @@ SbMediaAudioCodec GetAudioCodecFromString(const char* codec,
return kSbMediaAudioCodecPcm;
}
#if SB_API_VERSION >= 15
if (strcmp(codec, "iamf") == 0 || strncmp(codec, "iamf.", 5) == 0) {
if (strcmp(codec, "iamf") == 0 || IsIamfMimeType(codec)) {
return kSbMediaAudioCodecIamf;
}
#endif // SB_API_VERSION >= 15
return kSbMediaAudioCodecNone;
}

bool IsIamfMimeType(std::string mime_type) {
return std::regex_match(mime_type,
std::regex("iamf\\.\\d{3}\\.\\d{3}\\.Opus")) ||
std::regex_match(
mime_type, std::regex("iamf\\.\\d{3}\\.\\d{3}\\.mp4a\\.40\\.2")) ||
std::regex_match(mime_type,
std::regex("iamf\\.\\d{3}\\.\\d{3}\\.fLaC")) ||
std::regex_match(mime_type,
std::regex("iamf\\.\\d{3}\\.\\d{3}\\.ipcm"));
}

} // namespace media
} // namespace starboard
} // namespace shared
Expand Down
3 changes: 3 additions & 0 deletions starboard/shared/starboard/media/codec_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class VideoConfig {
SbMediaAudioCodec GetAudioCodecFromString(const char* codec,
const char* subtype);

#if SB_API_VERSION >= 15
bool IsIamfMimeType(std::string mime_type);
#endif // SB_API_VERSION >= 15
} // namespace media
} // namespace starboard
} // namespace shared
Expand Down
35 changes: 35 additions & 0 deletions starboard/shared/starboard/media/codec_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,41 @@ TEST(CodecUtilTest, DoesNotParse1AsPcmForNonWavSubtypes) {
EXPECT_EQ(GetAudioCodecFromString("1", "webm"), kSbMediaAudioCodecNone);
}

TEST(CodecUtilTest, ParsesIamfCodec) {
#if SB_API_VERSION < 15
GTEST_SKIP() << "IAMF is unsupported on Starboard " << SB_API_VERSION;
#endif // SB_API_VERSION < 15
EXPECT_EQ(GetAudioCodecFromString("iamf", ""), kSbMediaAudioCodecIamf);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.000.Opus", ""),
kSbMediaAudioCodecIamf);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.000.mp4a.40.2", ""),
kSbMediaAudioCodecIamf);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.000.fLaC", ""),
kSbMediaAudioCodecIamf);
EXPECT_EQ(GetAudioCodecFromString("iamf.000.000.ipcm", ""),
kSbMediaAudioCodecIamf);
}

TEST(CodecUtilTest, IamfRegex) {
#if SB_API_VERSION < 15
GTEST_SKIP() << "IAMF is unsupported on Starboard " << SB_API_VERSION;
#endif // SB_API_VERSION < 15
EXPECT_TRUE(IsIamfMimeType("iamf.000.000.Opus"));
EXPECT_TRUE(IsIamfMimeType("iamf.999.999.Opus"));
EXPECT_TRUE(IsIamfMimeType("iamf.000.000.mp4a.40.2"));
EXPECT_TRUE(IsIamfMimeType("iamf.000.000.fLaC"));
EXPECT_TRUE(IsIamfMimeType("iamf.000.000.ipcm"));

EXPECT_FALSE(IsIamfMimeType("iamf.000.000.Opu"));
EXPECT_FALSE(IsIamfMimeType("iamf,000.000.Opus"));
EXPECT_FALSE(IsIamfMimeType("Iamf.000.000.Opus"));
EXPECT_FALSE(IsIamfMimeType("iamf.000.000.0pus"));
EXPECT_FALSE(IsIamfMimeType("iamf.xxx.yyy.Opus"));

EXPECT_FALSE(IsIamfMimeType("iamf.000.000.mp4a.40.3"));
EXPECT_FALSE(IsIamfMimeType("iamf.000.000.flac"));
}

} // namespace
} // namespace media
} // namespace starboard
Expand Down

0 comments on commit de2e7ed

Please sign in to comment.