-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[media] Create IamfMimeUtil #4061
Conversation
return false; | ||
} | ||
|
||
constexpr int kMaxIamfCodecIdLength = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although addition is a cheap operation, should we consider directly put the total number here, and then comment how it is calculated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
starboard/common/string.h
Outdated
|
||
std::stringstream stream(input); | ||
std::string token; | ||
while (std::getline(stream, token, delimiter)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious if this is the most efficient way to split strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's the most efficient, but I found it most readable. I replaced it with an implementation similar to Chromium's SplitString(), already in use in media code, so I think we can be confident it's efficient enough.
} | ||
|
||
std::vector<std::string> vec = SplitString(mime_type, '.'); | ||
if (vec.size() != 4 && vec.size() != 6) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should specify why 4
and 6
here as a comment. Same for other places.
If the format of IAMF is always iamf.<primary_profile>.<additional_profile>.<audio_format>
, then we can consider make SplitString()
as a special case, such as std::vector<std::string> SplitString(std::string& input, char delimiter, int num_fields=-1)
, so if num_fields=4
, this is used to parse iamf MIME strings.
So we can get rid of checking vec.size() != 6
here, and check vec.size() != 4 || vec[0] != "iamf"
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the comment. Seems that adding a special case is pretty specific to this use case, I'm not sure if it'd see any use outside of this function to justify adding it in. Happy to discuss it.
// The primary profile string should be three digits, and should be between 0 | ||
// and 255 inclusive. | ||
int primary_profile; | ||
std::stringstream stream(vec[1]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vec[1] is std::string, should we consider to use std::stoi()
to convert string to integer here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, std::isdigit()
can check if each character is a digit or not. Then, make this a helper function, so we don't need the similar codes in all other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue with std::stoi()
is that it throws an exception if it can't convert the field to an int. std::isdigit()
will help with that but I wonder if it's much more efficient than using stream.
} | ||
|
||
// The codec string should be one of "Opus", "mp4a", "fLaC", or "ipcm". | ||
std::string codec = vec[3]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe consider to refactor the following code to switch-case, so it is more readable.
For example:
switch(codec) {
case "Opus":
case "fLaC":
case "ipcm":
return true;
default:
// handle "mp4a.40.2" and check its format here
// return true if it meets
}
return false;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately switch statements can't work with strings, only values resolving to int.
kSbMediaAudioCodecIamf); | ||
} | ||
|
||
TEST(CodecUtilTest, IsIamfMimeType) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider to add cases where the length of resulting string vector is not 4, like iamf.xxx.Opus
, iamf.1xx.2yy
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -106,12 +107,100 @@ SbMediaAudioCodec GetAudioCodecFromString(const char* codec, | |||
if (is_wav && strcmp(codec, "1") == 0) { | |||
return kSbMediaAudioCodecPcm; | |||
} | |||
if (strcmp(codec, "iamf") == 0 || strncmp(codec, "iamf.", 5) == 0) { | |||
if (strcmp(codec, "iamf") == 0 || IsIamfMimeType(codec)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't check the validity of the codec string in this function.
The best way to check the validity of the iamf codec string I can think of is to check it in SbMediaIsAudioSupported(). We can add an iamf_util, and check the validity of codec string while deciding if the sub codec can be supported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
starboard/common/string.h
Outdated
@@ -101,6 +101,37 @@ static SB_C_FORCE_INLINE int strlcat(CHAR* dst, const CHAR* src, int dst_size) { | |||
dst_length; | |||
} | |||
|
|||
// Splits a string on a char delimiter. Returns an empty vector if the delimiter | |||
// is not found |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider double checking if returning an empty vector when the delimiter is not found is a common practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It now returns the full string if non empty, like the Chromium method does.
starboard/common/string.h
Outdated
@@ -101,6 +101,37 @@ static SB_C_FORCE_INLINE int strlcat(CHAR* dst, const CHAR* src, int dst_size) { | |||
dst_length; | |||
} | |||
|
|||
// Splits a string on a char delimiter. Returns an empty vector if the delimiter | |||
// is not found | |||
inline std::vector<std::string> SplitString(std::string& input, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const std::string&
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -263,6 +263,18 @@ TEST(CodecUtilTest, DoesNotParse1AsPcmForNonWavSubtypes) { | |||
EXPECT_EQ(GetAudioCodecFromString("1", "webm"), kSbMediaAudioCodecNone); | |||
} | |||
|
|||
TEST(CodecUtilTest, ParsesIamfCodec) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to double check if this makes use of IamfMimeUtil?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, IamfMimeUtil
is intended to be used in SbMediaIsAudioSupported()
implementations.
kIamfSubstreamCodecMp4a, | ||
kIamfSubstreamCodecFlac, | ||
kIamfSubstreamCodecIpcm, | ||
kIamfSubstreamCodecUnknown |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless this is a map of spec defined somewhere, consider moving kIamfSubstreamCodecUnknown to the first, so when the value is zero initialized, it's initialized to kIamfSubstreamCodecUnknown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
return profile_ <= kIamfProfileMax && | ||
substream_codec_ != kIamfSubstreamCodecUnknown; | ||
} | ||
int profile() const { return profile_; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add DCHECK(is_valid()), and the same to below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
if (mime_type.find("iamf") == std::string::npos) { | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably redundant to the check in line 57? And if we do want to check, we should check that the return value is 0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
// The codec string should be one of "Opus", "mp4a", "fLaC", or "ipcm". | ||
std::string codec = vec[3]; | ||
if (codec.size() != 4 || ((codec != "Opus") && (codec != "mp4a") && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check codec.size() != 4
is redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
stream = std::stringstream(vec[4]); | ||
int object_type_indication; | ||
stream >> object_type_indication; | ||
if (stream.fail() || vec[4].size() != 2 || object_type_indication != 40) { | ||
return; | ||
} | ||
|
||
stream = std::stringstream(vec[5]); | ||
int audio_object_type; | ||
stream >> audio_object_type; | ||
if (stream.fail() || vec[5].size() != 1 || audio_object_type != 2) { | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we just check vec[4] == '40' && vec[5] == '2'
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
The test will be implemented in a separate PR youtube#4188
Adds a test for GetAudioCodecFromString() to parse the codecs parameter of an IAMF MIME. `GetAudioCodecFromString()` will more strictly parse for valid values of the IAMF codec param in a follow up #4061. b/356704307
This PR will be close. The 25.lts PR is here: #4244 |
b/356704307