Skip to content

Commit

Permalink
Ensure that MP3, FLAC, and PCM are not supported for MediaSource. (#1359
Browse files Browse the repository at this point in the history
)

This change makes the logic of CanPlayAdaptive more like
CanPlayProgressive, wherein unsupported containers are immediately
filtered out.

Bug: b/297060983

Test: Added unit tests. Also manually ran Cobalt and verified that
MediaSource.isTypeSupported returns false for these MIME strings:
'audio/mpeg; codecs="mp3"'
'audio/mpeg'
'audio/ogg; codecs="flac"'
'audio/flac; codecs="flac"'
'audio/wav; codecs="1"'
  • Loading branch information
antoniori-eng authored Nov 17, 2023
1 parent fbe1ba5 commit 9fd3a9c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
1 change: 1 addition & 0 deletions cobalt/media/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ target(gtest_target_type, "media_test") {
"base/decoder_buffer_cache_test.cc",
"bidirectional_fit_reuse_allocator_test.cc",
"file_data_source_test.cc",
"media_module_test.cc",
"progressive/demuxer_extension_wrapper_test.cc",
"progressive/mock_data_source_reader.h",
"progressive/mp4_map_unittest.cc",
Expand Down
14 changes: 11 additions & 3 deletions cobalt/media/media_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ class CanPlayTypeHandlerStarboard : public CanPlayTypeHandler {
// progressive.
SbMediaSupportType support_type;
media::FormatSupportQueryMetrics metrics;
if (strstr(mime_type.c_str(), "video/mp4") == 0 &&
strstr(mime_type.c_str(), "application/x-mpegURL") == 0) {
if (strstr(mime_type.c_str(), "video/mp4") == NULL &&
strstr(mime_type.c_str(), "application/x-mpegURL") == NULL) {
support_type = kSbMediaSupportTypeNotSupported;
} else {
support_type = CanPlayType(mime_type, "");
Expand All @@ -135,8 +135,16 @@ class CanPlayTypeHandlerStarboard : public CanPlayTypeHandler {
SbMediaSupportType CanPlayAdaptive(
const std::string& mime_type,
const std::string& key_system) const override {
SbMediaSupportType support_type;
media::FormatSupportQueryMetrics metrics;
SbMediaSupportType support_type = CanPlayType(mime_type, key_system);
// Only mp4 and webm videos are supported for adaptive playback in Cobalt;
// any other containers can be immediately rejected.
if (strstr(mime_type.c_str(), "video/mp4") == NULL &&
strstr(mime_type.c_str(), "video/webm") == NULL) {
support_type = kSbMediaSupportTypeNotSupported;
} else {
support_type = CanPlayType(mime_type, key_system);
}
metrics.RecordAndLogQuery("MediaSource::IsTypeSupported", mime_type,
key_system, support_type);
return support_type;
Expand Down
81 changes: 81 additions & 0 deletions cobalt/media/media_module_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2023 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/media/media_module.h"

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cobalt {
namespace media {
namespace {

using ::testing::NotNull;

#if SB_API_VERSION >= 14
TEST(MediaModuleTest, DoesNotSupportMp3Adaptive) {
std::unique_ptr<CanPlayTypeHandler> handler =
MediaModule::CreateCanPlayTypeHandler();
ASSERT_THAT(handler, NotNull());
EXPECT_FALSE(handler->CanPlayAdaptive("audio/mpeg; codecs=\"mp3\"",
/*key_system=*/""));
EXPECT_FALSE(handler->CanPlayAdaptive("audio/mpeg", /*key_system=*/""));
}

TEST(MediaModuleTest, DoesNotSupportMp3Progressive) {
std::unique_ptr<CanPlayTypeHandler> handler =
MediaModule::CreateCanPlayTypeHandler();
ASSERT_THAT(handler, NotNull());
EXPECT_FALSE(handler->CanPlayProgressive("audio/mpeg; codecs=\"mp3\""));
EXPECT_FALSE(handler->CanPlayProgressive("audio/mpeg"));
}

TEST(MediaModuleTest, DoesNotSupportFlacAdaptive) {
std::unique_ptr<CanPlayTypeHandler> handler =
MediaModule::CreateCanPlayTypeHandler();
ASSERT_THAT(handler, NotNull());
EXPECT_FALSE(handler->CanPlayAdaptive("audio/ogg; codecs=\"flac\"",
/*key_system=*/""));
EXPECT_FALSE(handler->CanPlayAdaptive("audio/flac; codecs=\"flac\"",
/*key_system=*/""));
}

TEST(MediaModuleTest, DoesNotSupportFlacProgressive) {
std::unique_ptr<CanPlayTypeHandler> handler =
MediaModule::CreateCanPlayTypeHandler();
ASSERT_THAT(handler, NotNull());
EXPECT_FALSE(handler->CanPlayProgressive("audio/ogg; codecs=\"flac\""));
EXPECT_FALSE(handler->CanPlayProgressive("audio/flac; codecs=\"flac\""));
}

TEST(MediaModuleTest, DoesNotSupportPcmAdaptive) {
std::unique_ptr<CanPlayTypeHandler> handler =
MediaModule::CreateCanPlayTypeHandler();
ASSERT_THAT(handler, NotNull());
EXPECT_FALSE(handler->CanPlayAdaptive("audio/wav; codecs=\"1\"",
/*key_system=*/""));
}

TEST(MediaModuleTest, DoesNotSupportPcmProgressive) {
std::unique_ptr<CanPlayTypeHandler> handler =
MediaModule::CreateCanPlayTypeHandler();
ASSERT_THAT(handler, NotNull());
EXPECT_FALSE(handler->CanPlayProgressive("audio/wav; codecs=\"1\""));
}

#endif // SB_API_VERSION >= 14

} // namespace
} // namespace media
} // namespace cobalt

0 comments on commit 9fd3a9c

Please sign in to comment.