From d5368090a6a7dd39689e2c02f98baa79a2cfc80c Mon Sep 17 00:00:00 2001 From: "acolwell@chromium.org" Date: Thu, 5 Sep 2013 18:33:50 +0000 Subject: [PATCH] Implement experimental MP3 support for Media Source API. BUG=280550 TEST=PipelineIntegrationTest.MediaSource_MP3 Review URL: https://chromiumcodereview.appspot.com/23454006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221471 0039d316-1c4b-4281-b951-d872f2087c98 --- .../renderer_host/render_process_host_impl.cc | 1 + media/DEPS | 1 + media/base/media_switches.cc | 3 + media/base/media_switches.h | 2 + media/base/run_all_unittests.cc | 2 + media/filters/pipeline_integration_test.cc | 49 +- media/filters/stream_parser_factory.cc | 43 +- media/filters/stream_parser_factory.h | 2 +- media/media.gyp | 3 + media/mp3/mp3_stream_parser.cc | 565 ++++++++++++++++++ media/mp3/mp3_stream_parser.h | 119 ++++ tools/metrics/histograms/histograms.xml | 1 + 12 files changed, 784 insertions(+), 7 deletions(-) create mode 100644 media/mp3/mp3_stream_parser.cc create mode 100644 media/mp3/mp3_stream_parser.h diff --git a/content/browser/renderer_host/render_process_host_impl.cc b/content/browser/renderer_host/render_process_host_impl.cc index a3bec9b93faf44..3082af3a87fafe 100644 --- a/content/browser/renderer_host/render_process_host_impl.cc +++ b/content/browser/renderer_host/render_process_host_impl.cc @@ -900,6 +900,7 @@ void RenderProcessHostImpl::PropagateBrowserCommandLineToRenderer( switches::kEnableGPUClientLogging, switches::kEnableGpuClientTracing, switches::kEnableGpuBenchmarking, + switches::kEnableMP3StreamParser, switches::kEnableMemoryBenchmarking, switches::kEnableOverlayScrollbars, switches::kEnableSkiaBenchmarking, diff --git a/media/DEPS b/media/DEPS index 495c8049c7d891..61ad8cf657dac8 100644 --- a/media/DEPS +++ b/media/DEPS @@ -1,6 +1,7 @@ include_rules = [ "+gpu", "+jni", + "+net/http", "+third_party/ffmpeg", "+third_party/libvpx", "+third_party/opus", diff --git a/media/base/media_switches.cc b/media/base/media_switches.cc index 567d779b3e9df2..a4149fa1ec372d 100644 --- a/media/base/media_switches.cc +++ b/media/base/media_switches.cc @@ -25,6 +25,9 @@ const char kVideoThreads[] = "video-threads"; const char kOverrideEncryptedMediaCanPlayType[] = "override-encrypted-media-canplaytype"; +// Enables MP3 stream parser for Media Source Extensions. +const char kEnableMP3StreamParser[] = "enable-mp3-stream-parser"; + #if defined(GOOGLE_TV) // Use external video surface for video with more than or equal pixels to // specified value. For example, value of 0 will enable external video surface diff --git a/media/base/media_switches.h b/media/base/media_switches.h index 3009f60f0ea02f..de7b7dd4b952e4 100644 --- a/media/base/media_switches.h +++ b/media/base/media_switches.h @@ -24,6 +24,8 @@ MEDIA_EXPORT extern const char kVideoThreads[]; MEDIA_EXPORT extern const char kOverrideEncryptedMediaCanPlayType[]; +MEDIA_EXPORT extern const char kEnableMP3StreamParser[]; + #if defined(GOOGLE_TV) MEDIA_EXPORT extern const char kUseExternalVideoSurfaceThresholdInPixels[]; #endif diff --git a/media/base/run_all_unittests.cc b/media/base/run_all_unittests.cc index a9a64231905c47..c63f1dcb5d3dd0 100644 --- a/media/base/run_all_unittests.cc +++ b/media/base/run_all_unittests.cc @@ -39,6 +39,8 @@ void TestSuiteNoAtExit::Initialize() { // Run this here instead of main() to ensure an AtExitManager is already // present. media::InitializeMediaLibraryForTesting(); + CommandLine* cmd_line = CommandLine::ForCurrentProcess(); + cmd_line->AppendSwitch(switches::kEnableMP3StreamParser); } int main(int argc, char** argv) { diff --git a/media/filters/pipeline_integration_test.cc b/media/filters/pipeline_integration_test.cc index e79f631b72b307..a767b7f44057f2 100644 --- a/media/filters/pipeline_integration_test.cc +++ b/media/filters/pipeline_integration_test.cc @@ -11,6 +11,7 @@ #include "build/build_config.h" #include "media/base/decoder_buffer.h" #include "media/base/media_keys.h" +#include "media/base/media_switches.h" #include "media/base/test_data_util.h" #include "media/cdm/aes_decryptor.h" #include "media/filters/chunk_demuxer.h" @@ -33,6 +34,7 @@ static const char kMP4Video[] = "video/mp4; codecs=\"avc1.4D4041\""; static const char kMP4Audio[] = "audio/mp4; codecs=\"mp4a.40.2\""; static const char kMP4AudioType[] = "audio/mp4"; static const char kMP4VideoType[] = "video/mp4"; +static const char kMP3[] = "audio/mpeg"; // Key used to encrypt test files. static const uint8 kSecretKey[] = { @@ -284,13 +286,29 @@ class MockMediaSource { } void DemuxerOpenedTask() { + // This code assumes that |mimetype_| is one of the following forms. + // 1. audio/mpeg + // 2. video/webm;codec="vorbis,vp8". size_t semicolon = mimetype_.find(";"); - std::string type = mimetype_.substr(0, semicolon); - size_t quote1 = mimetype_.find("\""); - size_t quote2 = mimetype_.find("\"", quote1 + 1); - std::string codecStr = mimetype_.substr(quote1 + 1, quote2 - quote1 - 1); + std::string type = mimetype_; std::vector codecs; - Tokenize(codecStr, ",", &codecs); + if (semicolon != std::string::npos) { + type = mimetype_.substr(0, semicolon); + size_t codecs_param_start = mimetype_.find("codecs=\"", semicolon); + + CHECK_NE(codecs_param_start, std::string::npos); + + codecs_param_start += 8; // Skip over the codecs=". + + size_t codecs_param_end = mimetype_.find("\"", codecs_param_start); + + CHECK_NE(codecs_param_end, std::string::npos); + + std::string codecs_param = + mimetype_.substr(codecs_param_start, + codecs_param_end - codecs_param_start); + Tokenize(codecs_param, ",", &codecs); + } CHECK_EQ(chunk_demuxer_->AddId(kSourceId, type, codecs), ChunkDemuxer::kOk); AppendData(initial_append_size_); @@ -627,6 +645,27 @@ TEST_F(PipelineIntegrationTest, } #if defined(USE_PROPRIETARY_CODECS) +TEST_F(PipelineIntegrationTest, MediaSource_MP3) { + MockMediaSource source("sfx.mp3", kMP3, kAppendWholeFile); + StartPipelineWithMediaSource(&source); + source.EndOfStream(); + + Play(); + + EXPECT_TRUE(WaitUntilOnEnded()); +} + + +TEST_F(PipelineIntegrationTest, MediaSource_MP3_Icecast) { + MockMediaSource source("icy_sfx.mp3", kMP3, kAppendWholeFile); + StartPipelineWithMediaSource(&source); + source.EndOfStream(); + + Play(); + + EXPECT_TRUE(WaitUntilOnEnded()); +} + TEST_F(PipelineIntegrationTest, MediaSource_ConfigChange_MP4) { MockMediaSource source("bear-640x360-av_frag.mp4", kMP4, kAppendWholeFile); StartPipelineWithMediaSource(&source); diff --git a/media/filters/stream_parser_factory.cc b/media/filters/stream_parser_factory.cc index 5a7a0302c8a1f8..2b2ec9cbcee42a 100644 --- a/media/filters/stream_parser_factory.cc +++ b/media/filters/stream_parser_factory.cc @@ -10,6 +10,7 @@ #include "base/strings/string_util.h" #include "media/base/media_log.h" #include "media/base/media_switches.h" +#include "media/mp3/mp3_stream_parser.h" #include "media/webm/webm_stream_parser.h" #if defined(USE_PROPRIETARY_CODECS) @@ -28,6 +29,8 @@ struct CodecInfo { AUDIO, VIDEO }; + + // Update tools/metrics/histograms/histograms.xml if new values are added. enum HistogramTag { HISTOGRAM_UNKNOWN, HISTOGRAM_VP8, @@ -37,6 +40,7 @@ struct CodecInfo { HISTOGRAM_MPEG2AAC, HISTOGRAM_MPEG4AAC, HISTOGRAM_EAC3, + HISTOGRAM_MP3, HISTOGRAM_MAX // Must be the last entry. }; @@ -151,6 +155,7 @@ static const CodecInfo* kAudioMP4Codecs[] = { static StreamParser* BuildMP4Parser( const std::vector& codecs, const LogCB& log_cb) { std::set audio_object_types; + bool has_sbr = false; #if defined(ENABLE_EAC3_PLAYBACK) bool enable_eac3 = CommandLine::ForCurrentProcess()->HasSwitch( @@ -179,12 +184,28 @@ static StreamParser* BuildMP4Parser( return new mp4::MP4StreamParser(audio_object_types, has_sbr); } + +static const CodecInfo kMP3CodecInfo = { NULL, CodecInfo::AUDIO, NULL, + CodecInfo::HISTOGRAM_MP3 }; + +static const CodecInfo* kAudioMP3Codecs[] = { + &kMP3CodecInfo, + NULL +}; + +static StreamParser* BuildMP3Parser( + const std::vector& codecs, const LogCB& log_cb) { + return new MP3StreamParser(); +} + #endif + static const SupportedTypeInfo kSupportedTypeInfo[] = { { "video/webm", &BuildWebMParser, kVideoWebMCodecs }, { "audio/webm", &BuildWebMParser, kAudioWebMCodecs }, #if defined(USE_PROPRIETARY_CODECS) + { "audio/mpeg", &BuildMP3Parser, kAudioMP3Codecs }, { "video/mp4", &BuildMP4Parser, kVideoMP4Codecs }, { "audio/mp4", &BuildMP4Parser, kAudioMP4Codecs }, #endif @@ -212,6 +233,7 @@ static bool VerifyCodec( return false; } #endif + if (audio_codecs) audio_codecs->push_back(codec_info->tag); return true; @@ -253,8 +275,26 @@ static bool CheckTypeAndCodecs( for (size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) { const SupportedTypeInfo& type_info = kSupportedTypeInfo[i]; if (type == type_info.type) { + if (codecs.empty()) { + +#if defined(USE_PROPRIETARY_CODECS) + if (type_info.codecs == kAudioMP3Codecs && + !CommandLine::ForCurrentProcess()->HasSwitch( + switches::kEnableMP3StreamParser)) { + DVLOG(1) << "MP3StreamParser is not enabled."; + return false; + } +#endif + + const CodecInfo* codec_info = type_info.codecs[0]; + if (codec_info && !codec_info->pattern && + VerifyCodec(codec_info, audio_codecs, video_codecs)) { + + if (factory_function) + *factory_function = type_info.factory_function; + return true; + } - if (codecs.size() == 0u) { MEDIA_LOG(log_cb) << "A codecs parameter must be provided for '" << type << "'"; return false; @@ -275,6 +315,7 @@ static bool CheckTypeAndCodecs( break; // Since only 1 pattern will match, no need to check others. } } + if (!found_codec) { MEDIA_LOG(log_cb) << "Codec '" << codec_id << "' is not supported for '" << type << "'"; diff --git a/media/filters/stream_parser_factory.h b/media/filters/stream_parser_factory.h index ccf394150bcd73..1f9ad347d12e21 100644 --- a/media/filters/stream_parser_factory.h +++ b/media/filters/stream_parser_factory.h @@ -32,7 +32,7 @@ class MEDIA_EXPORT StreamParserFactory { // |has_video| is true if a video codec was specified. // Returns NULL otherwise. The values of |has_audio| and |has_video| are // undefined. - static scoped_ptr Create( + static scoped_ptr Create( const std::string& type, const std::vector& codecs, const LogCB& log_cb, bool* has_audio, bool* has_video); }; diff --git a/media/media.gyp b/media/media.gyp index ab2701881dd76e..fca53baa00ac34 100644 --- a/media/media.gyp +++ b/media/media.gyp @@ -41,6 +41,7 @@ 'dependencies': [ '../base/base.gyp:base', '../crypto/crypto.gyp:crypto', + '../net/net.gyp:net', '../skia/skia.gyp:skia', '../third_party/opus/opus.gyp:opus', '../ui/ui.gyp:ui', @@ -382,6 +383,8 @@ 'midi/midi_manager_mac.h', 'midi/midi_port_info.cc', 'midi/midi_port_info.h', + 'mp3/mp3_stream_parser.cc', + 'mp3/mp3_stream_parser.h', 'video/capture/android/video_capture_device_android.cc', 'video/capture/android/video_capture_device_android.h', 'video/capture/fake_video_capture_device.cc', diff --git a/media/mp3/mp3_stream_parser.cc b/media/mp3/mp3_stream_parser.cc new file mode 100644 index 00000000000000..86348ffd2ce237 --- /dev/null +++ b/media/mp3/mp3_stream_parser.cc @@ -0,0 +1,565 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "media/mp3/mp3_stream_parser.h" + +#include "base/bind.h" +#include "base/callback_helpers.h" +#include "base/message_loop/message_loop.h" +#include "media/base/bit_reader.h" +#include "media/base/buffers.h" +#include "media/base/stream_parser_buffer.h" +#include "media/base/video_decoder_config.h" +#include "net/http/http_util.h" + +namespace media { + +static const uint32 kMP3StartCodeMask = 0xffe00000; +static const uint32 kICYStartCode = 0x49435920; // 'ICY ' + +// Arbitrary upper bound on the size of an IceCast header before it +// triggers an error. +static const int kMaxIcecastHeaderSize = 4096; + +static const uint32 kID3StartCodeMask = 0xffffff00; +static const uint32 kID3v1StartCode = 0x54414700; // 'TAG\0' +static const int kID3v1Size = 128; +static const int kID3v1ExtendedSize = 227; +static const uint32 kID3v2StartCode = 0x49443300; // 'ID3\0' + +// Map that determines which bitrate_index & channel_mode combinations +// are allowed. +// Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html +static const bool kIsAllowed[17][4] = { + { true, true, true, true }, // free + { true, false, false, false }, // 32 + { true, false, false, false }, // 48 + { true, false, false, false }, // 56 + { true, true, true, true }, // 64 + { true, false, false, false }, // 80 + { true, true, true, true }, // 96 + { true, true, true, true }, // 112 + { true, true, true, true }, // 128 + { true, true, true, true }, // 160 + { true, true, true, true }, // 192 + { false, true, true, true }, // 224 + { false, true, true, true }, // 256 + { false, true, true, true }, // 320 + { false, true, true, true }, // 384 + { false, false, false, false } // bad +}; + +// Maps version and layer information in the frame header +// into an index for the |kBitrateMap|. +// Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html +static const int kVersionLayerMap[4][4] = { + // { reserved, L3, L2, L1 } + { 5, 4, 4, 3 }, // MPEG 2.5 + { 5, 5, 5, 5 }, // reserved + { 5, 4, 4, 3 }, // MPEG 2 + { 5, 2, 1, 0 } // MPEG 1 +}; + +// Maps the bitrate index field in the header and an index +// from |kVersionLayerMap| to a frame bitrate. +// Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html +static const int kBitrateMap[16][6] = { + // { V1L1, V1L2, V1L3, V2L1, V2L2 & V2L3, reserved } + { 0, 0, 0, 0, 0, 0 }, + { 32, 32, 32, 32, 8, 0 }, + { 64, 48, 40, 48, 16, 0 }, + { 96, 56, 48, 56, 24, 0 }, + { 128, 64, 56, 64, 32, 0 }, + { 160, 80, 64, 80, 40, 0 }, + { 192, 96, 80, 96, 48, 0 }, + { 224, 112, 96, 112, 56, 0 }, + { 256, 128, 112, 128, 64, 0 }, + { 288, 160, 128, 144, 80, 0 }, + { 320, 192, 160, 160, 96, 0 }, + { 352, 224, 192, 176, 112, 0 }, + { 384, 256, 224, 192, 128, 0 }, + { 416, 320, 256, 224, 144, 0 }, + { 448, 384, 320, 256, 160, 0 }, + { 0, 0, 0, 0, 0} +}; + +// Maps the sample rate index and version fields from the frame header +// to a sample rate. +// Derived from: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html +static const int kSampleRateMap[4][4] = { + // { V2.5, reserved, V2, V1 } + { 11025, 0, 22050, 44100 }, + { 12000, 0, 24000, 48000 }, + { 8000, 0, 16000, 32000 }, + { 0, 0, 0, 0 } +}; + +// Frame header field constants. +static const int kVersion1 = 3; +static const int kVersion2 = 2; +static const int kVersionReserved = 1; +static const int kVersion2_5 = 0; +static const int kLayerReserved = 0; +static const int kLayer1 = 3; +static const int kLayer2 = 2; +static const int kLayer3 = 1; +static const int kBitrateFree = 0; +static const int kBitrateBad = 0xf; +static const int kSampleRateReserved = 3; + +MP3StreamParser::MP3StreamParser() + : state_(UNINITIALIZED), + in_media_segment_(false) { +} + +MP3StreamParser::~MP3StreamParser() {} + +void MP3StreamParser::Init(const InitCB& init_cb, + const NewConfigCB& config_cb, + const NewBuffersCB& new_buffers_cb, + const NewTextBuffersCB& text_cb, + const NeedKeyCB& need_key_cb, + const AddTextTrackCB& add_text_track_cb, + const NewMediaSegmentCB& new_segment_cb, + const base::Closure& end_of_segment_cb, + const LogCB& log_cb) { + DVLOG(1) << __FUNCTION__; + DCHECK_EQ(state_, UNINITIALIZED); + init_cb_ = init_cb; + config_cb_ = config_cb; + new_buffers_cb_ = new_buffers_cb; + new_segment_cb_ = new_segment_cb; + end_of_segment_cb_ = end_of_segment_cb; + log_cb_ = log_cb; + + ChangeState(INITIALIZED); +} + +void MP3StreamParser::Flush() { + DVLOG(1) << __FUNCTION__; + DCHECK_NE(state_, UNINITIALIZED); + queue_.Reset(); + timestamp_helper_->SetBaseTimestamp(base::TimeDelta()); + in_media_segment_ = false; +} + +bool MP3StreamParser::Parse(const uint8* buf, int size) { + DVLOG(1) << __FUNCTION__ << "(" << size << ")"; + DCHECK(buf); + DCHECK_GT(size, 0); + DCHECK_NE(state_, UNINITIALIZED); + + if (state_ == PARSE_ERROR) + return false; + + DCHECK_EQ(state_, INITIALIZED); + + queue_.Push(buf, size); + + for (;;) { + const uint8* data; + int data_size; + queue_.Peek(&data, &data_size); + + if (size < 4) + return true; + + uint32 start_code = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; + int bytes_read = 0; + if ((start_code & kMP3StartCodeMask) == kMP3StartCodeMask) { + bytes_read = ParseMP3Frame(data, data_size); + } else if (start_code == kICYStartCode) { + bytes_read = ParseIcecastHeader(data, data_size); + } else if ((start_code & kID3StartCodeMask) == kID3v1StartCode) { + bytes_read = ParseID3v1(data, data_size); + } else if ((start_code & kID3StartCodeMask) == kID3v2StartCode) { + bytes_read = ParseID3v2(data, data_size); + } else { + bytes_read = FindNextValidStartCode(data, data_size); + + if (bytes_read > 0) { + DVLOG(1) << "Unexpected start code 0x" << std::hex << start_code; + DVLOG(1) << "SKIPPING " << bytes_read << " bytes of garbage."; + } + } + + CHECK_LE(bytes_read, data_size); + + if (bytes_read < 0) { + ChangeState(PARSE_ERROR); + return false; + } else if (bytes_read == 0) { + // Need more data. + return true; + } + + queue_.Pop(bytes_read); + } + + return true; +} + +void MP3StreamParser::ChangeState(State state) { + DVLOG(1) << __FUNCTION__ << "() : " << state_ << " -> " << state; + state_ = state; +} + +int MP3StreamParser::ParseFrameHeader(const uint8* data, int size, + int* frame_size, + int* sample_rate, + ChannelLayout* channel_layout, + int* sample_count) const { + DCHECK(data); + DCHECK_GE(size, 0); + DCHECK(frame_size); + + if (size < 4) + return 0; + + BitReader reader(data, size); + int sync; + int version; + int layer; + int is_protected; + int bitrate_index; + int sample_rate_index; + int has_padding; + int is_private; + int channel_mode; + int other_flags; + + if (!reader.ReadBits(11, &sync) || + !reader.ReadBits(2, &version) || + !reader.ReadBits(2, &layer) || + !reader.ReadBits(1, &is_protected) || + !reader.ReadBits(4, &bitrate_index) || + !reader.ReadBits(2, &sample_rate_index) || + !reader.ReadBits(1, &has_padding) || + !reader.ReadBits(1, &is_private) || + !reader.ReadBits(2, &channel_mode) || + !reader.ReadBits(6, &other_flags)) { + return -1; + } + + DVLOG(2) << "Header data :" << std::hex + << " sync 0x" << sync + << " version 0x" << version + << " layer 0x" << layer + << " bitrate_index 0x" << bitrate_index + << " sample_rate_index 0x" << sample_rate_index + << " channel_mode 0x" << channel_mode; + + if (sync != 0x7ff || + version == kVersionReserved || + layer == kLayerReserved || + bitrate_index == kBitrateFree || bitrate_index == kBitrateBad || + sample_rate_index == kSampleRateReserved) { + MEDIA_LOG(log_cb_) << "Invalid header data :" << std::hex + << " sync 0x" << sync + << " version 0x" << version + << " layer 0x" << layer + << " bitrate_index 0x" << bitrate_index + << " sample_rate_index 0x" << sample_rate_index + << " channel_mode 0x" << channel_mode; + return -1; + } + + if (layer == kLayer2 && kIsAllowed[bitrate_index][channel_mode]) { + MEDIA_LOG(log_cb_) << "Invalid (bitrate_index, channel_mode) combination :" + << std::hex + << " bitrate_index " << bitrate_index + << " channel_mode " << channel_mode; + return -1; + } + + int bitrate = kBitrateMap[bitrate_index][kVersionLayerMap[version][layer]]; + + if (bitrate == 0) { + MEDIA_LOG(log_cb_) << "Invalid bitrate :" << std::hex + << " version " << version + << " layer " << layer + << " bitrate_index " << bitrate_index; + return -1; + } + + DVLOG(2) << " bitrate " << bitrate; + + int frame_sample_rate = kSampleRateMap[sample_rate_index][version]; + if (frame_sample_rate == 0) { + MEDIA_LOG(log_cb_) << "Invalid sample rate :" << std::hex + << " version " << version + << " sample_rate_index " << sample_rate_index; + return -1; + } + + if (sample_rate) + *sample_rate = frame_sample_rate; + + // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf + // Table 2.1.5 + int samples_per_frame; + switch (layer) { + case kLayer1: + samples_per_frame = 384; + break; + + case kLayer2: + samples_per_frame = 1152; + break; + + case kLayer3: + if (version == kVersion2 || version == kVersion2_5) + samples_per_frame = 576; + else + samples_per_frame = 1152; + break; + + default: + return -1; + } + + if (sample_count) + *sample_count = samples_per_frame; + + // http://teslabs.com/openplayer/docs/docs/specs/mp3_structure2.pdf + // Text just below Table 2.1.5. + if (layer == kLayer1) { + // This formulation is a slight variation on the equation below, + // but has slightly different truncation characteristics to deal + // with the fact that Layer 1 has 4 byte "slots" instead of single + // byte ones. + *frame_size = 4 * (12 * bitrate * 1000 / frame_sample_rate); + } else { + *frame_size = + ((samples_per_frame / 8) * bitrate * 1000) / frame_sample_rate; + } + + if (has_padding) + *frame_size += (layer == kLayer1) ? 4 : 1; + + if (channel_layout) { + // Map Stereo(0), Joint Stereo(1), and Dual Channel (2) to + // CHANNEL_LAYOUT_STEREO and Single Channel (3) to CHANNEL_LAYOUT_MONO. + *channel_layout = + (channel_mode == 3) ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; + } + + return 4; +} + +int MP3StreamParser::ParseMP3Frame(const uint8* data, int size) { + DVLOG(2) << __FUNCTION__ << "(" << size << ")"; + + int sample_rate; + ChannelLayout channel_layout; + int frame_size; + int sample_count; + int bytes_read = ParseFrameHeader( + data, size, &frame_size, &sample_rate, &channel_layout, &sample_count); + + if (bytes_read <= 0) + return bytes_read; + + // Make sure data contains the entire frame. + if (size < frame_size) + return 0; + + DVLOG(2) << " sample_rate " << sample_rate + << " channel_layout " << channel_layout + << " frame_size " << frame_size; + + if (config_.IsValidConfig() && + (config_.samples_per_second() != sample_rate || + config_.channel_layout() != channel_layout)) { + // Clear config data so that a config change is initiated. + config_ = AudioDecoderConfig(); + } + + if (!config_.IsValidConfig()) { + config_.Initialize(kCodecMP3, kSampleFormatF32, channel_layout, + sample_rate, NULL, 0, false, false); + + base::TimeDelta base_timestamp; + if (timestamp_helper_) + base_timestamp = timestamp_helper_->GetTimestamp(); + + timestamp_helper_.reset(new AudioTimestampHelper(sample_rate)); + timestamp_helper_->SetBaseTimestamp(base_timestamp); + + VideoDecoderConfig video_config; + bool success = config_cb_.Run(config_, video_config); + + if (!init_cb_.is_null()) + base::ResetAndReturn(&init_cb_).Run(success, kInfiniteDuration()); + + if (!success) + return -1; + } + + if (!in_media_segment_) { + in_media_segment_ = true; + new_segment_cb_.Run(); + } + + BufferQueue audio_buffers; + BufferQueue video_buffers; + + // TODO(acolwell): Change this code to parse as many frames as + // possible before calling |new_buffers_cb_|. + scoped_refptr buffer = + StreamParserBuffer::CopyFrom(data, frame_size, true); + audio_buffers.push_back(buffer); + + if (!new_buffers_cb_.Run(audio_buffers, video_buffers)) + return -1; + + timestamp_helper_->AddFrames(sample_count); + + return frame_size; +} + +int MP3StreamParser::ParseIcecastHeader(const uint8* data, int size) { + DVLOG(1) << __FUNCTION__ << "(" << size << ")"; + + if (size < 4) + return 0; + + if (memcmp("ICY ", data, 4)) + return -1; + + int locate_size = std::min(size, kMaxIcecastHeaderSize); + int offset = net::HttpUtil::LocateEndOfHeaders( + reinterpret_cast(data), locate_size, 4); + if (offset < 0) { + if (locate_size == kMaxIcecastHeaderSize) { + MEDIA_LOG(log_cb_) << "Icecast header is too large."; + return -1; + } + + return 0; + } + + return offset; +} + +int MP3StreamParser::ParseID3v1(const uint8* data, int size) { + DVLOG(1) << __FUNCTION__ << "(" << size << ")"; + + if (size < kID3v1Size) + return 0; + + // TODO(acolwell): Add code to actually validate ID3v1 data and + // expose it as a metadata text track. + return !memcmp(data, "TAG+", 4) ? kID3v1ExtendedSize : kID3v1Size; +} + +int MP3StreamParser::ParseID3v2(const uint8* data, int size) { + DVLOG(1) << __FUNCTION__ << "(" << size << ")"; + + if (size < 10) + return 0; + + BitReader reader(data, size); + int32 id; + int version; + uint8 flags; + int32 id3_size; + + if (!reader.ReadBits(24, &id) || + !reader.ReadBits(16, &version) || + !reader.ReadBits(8, &flags) || + !ParseSyncSafeInt(&reader, &id3_size)) { + return -1; + } + + int32 actual_tag_size = 10 + id3_size; + + // Increment size if 'Footer present' flag is set. + if (flags & 0x10) + actual_tag_size += 10; + + // Make sure we have the entire tag. + if (size < actual_tag_size) + return 0; + + // TODO(acolwell): Add code to actually validate ID3v2 data and + // expose it as a metadata text track. + return actual_tag_size; +} + +bool MP3StreamParser::ParseSyncSafeInt(BitReader* reader, int32* value) { + *value = 0; + for (int i = 0; i < 4; ++i) { + uint8 tmp; + if (!reader->ReadBits(1, &tmp) || tmp != 0) { + MEDIA_LOG(log_cb_) << "ID3 syncsafe integer byte MSb is not 0!"; + return false; + } + + if (!reader->ReadBits(7, &tmp)) + return false; + + *value <<= 7; + *value += tmp; + } + + return true; +} + +int MP3StreamParser::FindNextValidStartCode(const uint8* data, int size) const { + const uint8* start = data; + const uint8* end = data + size; + + while (start < end) { + int bytes_left = end - start; + const uint8* candidate_start_code = + static_cast(memchr(start, 0xff, bytes_left)); + + if (!candidate_start_code) + return 0; + + bool parse_header_failed = false; + const uint8* sync = candidate_start_code; + // Try to find 3 valid frames in a row. 3 was selected to decrease + // the probability of false positives. + for (int i = 0; i < 3; ++i) { + int sync_size = end - sync; + int frame_size; + int sync_bytes = ParseFrameHeader( + sync, sync_size, &frame_size, NULL, NULL, NULL); + + if (sync_bytes == 0) + return 0; + + if (sync_bytes > 0) { + DCHECK_LT(sync_bytes, sync_size); + + // Skip over this frame so we can check the next one. + sync += frame_size; + + // Make sure the next frame starts inside the buffer. + if (sync >= end) + return 0; + } else { + DVLOG(1) << "ParseFrameHeader() " << i << " failed @" << (sync - data); + parse_header_failed = true; + break; + } + } + + if (parse_header_failed) { + // One of the frame header parses failed so |candidate_start_code| + // did not point to the start of a real frame. Move |start| forward + // so we can find the next candidate. + start = candidate_start_code + 1; + continue; + } + + return candidate_start_code - data; + } + + return 0; +} + +} // namespace media diff --git a/media/mp3/mp3_stream_parser.h b/media/mp3/mp3_stream_parser.h new file mode 100644 index 00000000000000..a4b40576a798a6 --- /dev/null +++ b/media/mp3/mp3_stream_parser.h @@ -0,0 +1,119 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MEDIA_MP3_MP3_STREAM_PARSER_H_ +#define MEDIA_MP3_MP3_STREAM_PARSER_H_ + +#include +#include + +#include "base/basictypes.h" +#include "base/callback.h" +#include "media/base/audio_decoder_config.h" +#include "media/base/audio_timestamp_helper.h" +#include "media/base/byte_queue.h" +#include "media/base/media_export.h" +#include "media/base/stream_parser.h" + +namespace media { + +class BitReader; + +class MEDIA_EXPORT MP3StreamParser : public StreamParser { + public: + MP3StreamParser(); + virtual ~MP3StreamParser(); + + // StreamParser implementation. + virtual void Init(const InitCB& init_cb, const NewConfigCB& config_cb, + const NewBuffersCB& new_buffers_cb, + const NewTextBuffersCB& text_cb, + const NeedKeyCB& need_key_cb, + const AddTextTrackCB& add_text_track_cb, + const NewMediaSegmentCB& new_segment_cb, + const base::Closure& end_of_segment_cb, + const LogCB& log_cb) OVERRIDE; + virtual void Flush() OVERRIDE; + virtual bool Parse(const uint8* buf, int size) OVERRIDE; + + private: + enum State { + UNINITIALIZED, + INITIALIZED, + PARSE_ERROR + }; + + State state_; + + InitCB init_cb_; + NewConfigCB config_cb_; + NewBuffersCB new_buffers_cb_; + NewMediaSegmentCB new_segment_cb_; + base::Closure end_of_segment_cb_; + LogCB log_cb_; + + ByteQueue queue_; + + AudioDecoderConfig config_; + scoped_ptr timestamp_helper_; + bool in_media_segment_; + + void ChangeState(State state); + + // Parsing functions for various byte stream elements. + // |data| & |size| describe the data available for parsing. + // These functions are expected to consume an entire frame/header. + // It should only return a value greater than 0 when |data| has + // enough bytes to successfully parse & consume the entire element. + // + // |frame_size| - Required parameter that is set to the size of the frame, in + // bytes, including the frame header if the function returns a value > 0. + // |sample_rate| - Optional parameter that is set to the sample rate + // of the frame if this function returns a value > 0. + // |channel_layout| - Optional parameter that is set to the channel_layout + // of the frame if this function returns a value > 0. + // |sample_count| - Optional parameter that is set to the number of samples + // in the frame if this function returns a value > 0. + // + // |sample_rate|, |channel_layout|, |sample_count| may be NULL if the caller + // is not interested in receiving these values from the frame header. + // + // Returns: + // > 0 : The number of bytes parsed. + // 0 : If more data is needed to parse the entire element. + // < 0 : An error was encountered during parsing. + int ParseFrameHeader(const uint8* data, int size, + int* frame_size, + int* sample_rate, + ChannelLayout* channel_layout, + int* sample_count) const; + int ParseMP3Frame(const uint8* data, int size); + int ParseIcecastHeader(const uint8* data, int size); + int ParseID3v1(const uint8* data, int size); + int ParseID3v2(const uint8* data, int size); + + // Parses an ID3v2 "sync safe" integer. + // |reader| - A BitReader to read from. + // |value| - Set to the integer value read, if true is returned. + // + // Returns true if the integer was successfully parsed and |value| + // was set. + // Returns false if an error was encountered. The state of |value| is + // undefined when false is returned. + bool ParseSyncSafeInt(BitReader* reader, int32* value); + + // Scans |data| for the next valid start code. + // Returns: + // > 0 : The number of bytes that should be skipped to reach the + // next start code.. + // 0 : If a valid start code was not found and more data is needed. + // < 0 : An error was encountered during parsing. + int FindNextValidStartCode(const uint8* data, int size) const; + + DISALLOW_COPY_AND_ASSIGN(MP3StreamParser); +}; + +} // namespace media + +#endif // MEDIA_MP3_MP3_STREAM_PARSER_H_ diff --git a/tools/metrics/histograms/histograms.xml b/tools/metrics/histograms/histograms.xml index 92a3e21e82371f..53ed0b04d79926 100644 --- a/tools/metrics/histograms/histograms.xml +++ b/tools/metrics/histograms/histograms.xml @@ -22606,6 +22606,7 @@ other types of suffix sets. +