diff options
Diffstat (limited to 'dom/media/fmp4')
-rw-r--r-- | dom/media/fmp4/MP4Decoder.cpp | 307 | ||||
-rw-r--r-- | dom/media/fmp4/MP4Decoder.h | 62 | ||||
-rw-r--r-- | dom/media/fmp4/MP4Demuxer.cpp | 502 | ||||
-rw-r--r-- | dom/media/fmp4/MP4Demuxer.h | 58 | ||||
-rw-r--r-- | dom/media/fmp4/MP4Stream.cpp | 103 | ||||
-rw-r--r-- | dom/media/fmp4/MP4Stream.h | 107 | ||||
-rw-r--r-- | dom/media/fmp4/moz.build | 25 |
7 files changed, 1164 insertions, 0 deletions
diff --git a/dom/media/fmp4/MP4Decoder.cpp b/dom/media/fmp4/MP4Decoder.cpp new file mode 100644 index 000000000..4cf07ddbd --- /dev/null +++ b/dom/media/fmp4/MP4Decoder.cpp @@ -0,0 +1,307 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "MP4Decoder.h" +#include "MediaContentType.h" +#include "MediaDecoderStateMachine.h" +#include "MP4Demuxer.h" +#include "mozilla/Preferences.h" +#include "nsCharSeparatedTokenizer.h" +#include "mozilla/CDMProxy.h" +#include "mozilla/Logging.h" +#include "mozilla/SharedThreadPool.h" +#include "nsMimeTypes.h" +#include "VideoUtils.h" + +#ifdef XP_WIN +#include "mozilla/WindowsVersion.h" +#endif +#ifdef MOZ_WIDGET_ANDROID +#include "nsIGfxInfo.h" +#endif +#include "mozilla/layers/LayersTypes.h" + +#include "PDMFactory.h" + +namespace mozilla { + +MP4Decoder::MP4Decoder(MediaDecoderOwner* aOwner) + : MediaDecoder(aOwner) +{ +} + +MediaDecoderStateMachine* MP4Decoder::CreateStateMachine() +{ + mReader = + new MediaFormatReader(this, + new MP4Demuxer(GetResource()), + GetVideoFrameContainer()); + + return new MediaDecoderStateMachine(this, mReader); +} + +static bool +IsWhitelistedH264Codec(const nsAString& aCodec) +{ + int16_t profile = 0, level = 0; + + if (!ExtractH264CodecDetails(aCodec, profile, level)) { + return false; + } + +#ifdef XP_WIN + // Disable 4k video on windows vista since it performs poorly. + if (!IsWin7OrLater() && + level >= H264_LEVEL_5) { + return false; + } +#endif + + // Just assume what we can play on all platforms the codecs/formats that + // WMF can play, since we don't have documentation about what other + // platforms can play... According to the WMF documentation: + // http://msdn.microsoft.com/en-us/library/windows/desktop/dd797815%28v=vs.85%29.aspx + // "The Media Foundation H.264 video decoder is a Media Foundation Transform + // that supports decoding of Baseline, Main, and High profiles, up to level + // 5.1.". We also report that we can play Extended profile, as there are + // bitstreams that are Extended compliant that are also Baseline compliant. + return level >= H264_LEVEL_1 && + level <= H264_LEVEL_5_1 && + (profile == H264_PROFILE_BASE || + profile == H264_PROFILE_MAIN || + profile == H264_PROFILE_EXTENDED || + profile == H264_PROFILE_HIGH); +} + +/* static */ +bool +MP4Decoder::CanHandleMediaType(const MediaContentType& aType, + DecoderDoctorDiagnostics* aDiagnostics) +{ + if (!IsEnabled()) { + return false; + } + + // Whitelist MP4 types, so they explicitly match what we encounter on + // the web, as opposed to what we use internally (i.e. what our demuxers + // etc output). + const bool isMP4Audio = aType.GetMIMEType().EqualsASCII("audio/mp4") || + aType.GetMIMEType().EqualsASCII("audio/x-m4a"); + const bool isMP4Video = + // On B2G, treat 3GPP as MP4 when Gonk PDM is available. +#ifdef MOZ_GONK_MEDIACODEC + aType.GetMIMEType().EqualsASCII(VIDEO_3GPP) || +#endif + aType.GetMIMEType().EqualsASCII("video/mp4") || + aType.GetMIMEType().EqualsASCII("video/quicktime") || + aType.GetMIMEType().EqualsASCII("video/x-m4v"); + if (!isMP4Audio && !isMP4Video) { + return false; + } + + nsTArray<UniquePtr<TrackInfo>> trackInfos; + if (aType.GetCodecs().IsEmpty()) { + // No codecs specified. Assume H.264 + if (isMP4Audio) { + trackInfos.AppendElement( + CreateTrackInfoWithMIMETypeAndContentTypeExtraParameters( + NS_LITERAL_CSTRING("audio/mp4a-latm"), aType)); + } else { + MOZ_ASSERT(isMP4Video); + trackInfos.AppendElement( + CreateTrackInfoWithMIMETypeAndContentTypeExtraParameters( + NS_LITERAL_CSTRING("video/avc"), aType)); + } + } else { + // Verify that all the codecs specified are ones that we expect that + // we can play. + nsTArray<nsString> codecs; + if (!ParseCodecsString(aType.GetCodecs(), codecs)) { + return false; + } + for (const nsString& codec : codecs) { + if (IsAACCodecString(codec)) { + trackInfos.AppendElement( + CreateTrackInfoWithMIMETypeAndContentTypeExtraParameters( + NS_LITERAL_CSTRING("audio/mp4a-latm"), aType)); + continue; + } + if (codec.EqualsLiteral("mp3")) { + trackInfos.AppendElement( + CreateTrackInfoWithMIMETypeAndContentTypeExtraParameters( + NS_LITERAL_CSTRING("audio/mpeg"), aType)); + continue; + } + if (codec.EqualsLiteral("opus")) { + trackInfos.AppendElement( + CreateTrackInfoWithMIMETypeAndContentTypeExtraParameters( + NS_LITERAL_CSTRING("audio/opus"), aType)); + continue; + } + if (codec.EqualsLiteral("flac")) { + trackInfos.AppendElement( + CreateTrackInfoWithMIMETypeAndContentTypeExtraParameters( + NS_LITERAL_CSTRING("audio/flac"), aType)); + continue; + } + // Note: Only accept H.264 in a video content type, not in an audio + // content type. + if (IsWhitelistedH264Codec(codec) && isMP4Video) { + trackInfos.AppendElement( + CreateTrackInfoWithMIMETypeAndContentTypeExtraParameters( + NS_LITERAL_CSTRING("video/avc"), aType)); + continue; + } + // Some unsupported codec. + return false; + } + } + + // Verify that we have a PDM that supports the whitelisted types. + RefPtr<PDMFactory> platform = new PDMFactory(); + for (const auto& trackInfo : trackInfos) { + if (!trackInfo || !platform->Supports(*trackInfo, aDiagnostics)) { + return false; + } + } + + return true; +} + +/* static */ +bool +MP4Decoder::IsH264(const nsACString& aMimeType) +{ + return aMimeType.EqualsLiteral("video/mp4") || + aMimeType.EqualsLiteral("video/avc"); +} + +/* static */ +bool +MP4Decoder::IsAAC(const nsACString& aMimeType) +{ + return aMimeType.EqualsLiteral("audio/mp4a-latm"); +} + +/* static */ +bool +MP4Decoder::IsEnabled() +{ + return Preferences::GetBool("media.mp4.enabled", true); +} + +// sTestH264ExtraData represents the content of the avcC atom found in +// an AVC1 h264 video. It contains the H264 SPS and PPS NAL. +// the structure of the avcC atom is as follow: +// write(0x1); // version, always 1 +// write(sps[0].data[1]); // profile +// write(sps[0].data[2]); // compatibility +// write(sps[0].data[3]); // level +// write(0xFC | 3); // reserved (6 bits), NULA length size - 1 (2 bits) +// write(0xE0 | 1); // reserved (3 bits), num of SPS (5 bits) +// write_word(sps[0].size); // 2 bytes for length of SPS +// for(size_t i=0 ; i < sps[0].size ; ++i) +// write(sps[0].data[i]); // data of SPS +// write(&b, pps.size()); // num of PPS +// for(size_t i=0 ; i < pps.size() ; ++i) { +// write_word(pps[i].size); // 2 bytes for length of PPS +// for(size_t j=0 ; j < pps[i].size ; ++j) +// write(pps[i].data[j]); // data of PPS +// } +// } +// here we have a h264 Baseline, 640x360 +// We use a 640x360 extradata, as some video framework (Apple VT) will never +// attempt to use hardware decoding for small videos. +static const uint8_t sTestH264ExtraData[] = { + 0x01, 0x42, 0xc0, 0x1e, 0xff, 0xe1, 0x00, 0x17, 0x67, 0x42, + 0xc0, 0x1e, 0xbb, 0x40, 0x50, 0x17, 0xfc, 0xb8, 0x08, 0x80, + 0x00, 0x00, 0x32, 0x00, 0x00, 0x0b, 0xb5, 0x07, 0x8b, 0x17, + 0x50, 0x01, 0x00, 0x04, 0x68, 0xce, 0x32, 0xc8 +}; + +static already_AddRefed<MediaDataDecoder> +CreateTestH264Decoder(layers::KnowsCompositor* aKnowsCompositor, + VideoInfo& aConfig, + TaskQueue* aTaskQueue) +{ + aConfig.mMimeType = "video/avc"; + aConfig.mId = 1; + aConfig.mDuration = 40000; + aConfig.mMediaTime = 0; + aConfig.mImage = aConfig.mDisplay = nsIntSize(640, 360); + aConfig.mExtraData = new MediaByteBuffer(); + aConfig.mExtraData->AppendElements(sTestH264ExtraData, + MOZ_ARRAY_LENGTH(sTestH264ExtraData)); + + RefPtr<PDMFactory> platform = new PDMFactory(); + RefPtr<MediaDataDecoder> decoder(platform->CreateDecoder({ aConfig, aTaskQueue, aKnowsCompositor })); + + return decoder.forget(); +} + +/* static */ already_AddRefed<dom::Promise> +MP4Decoder::IsVideoAccelerated(layers::KnowsCompositor* aKnowsCompositor, nsIGlobalObject* aParent) +{ + MOZ_ASSERT(NS_IsMainThread()); + + ErrorResult rv; + RefPtr<dom::Promise> promise; + promise = dom::Promise::Create(aParent, rv); + if (rv.Failed()) { + rv.SuppressException(); + return nullptr; + } + + RefPtr<TaskQueue> taskQueue = + new TaskQueue(GetMediaThreadPool(MediaThreadType::PLATFORM_DECODER)); + VideoInfo config; + RefPtr<MediaDataDecoder> decoder(CreateTestH264Decoder(aKnowsCompositor, config, taskQueue)); + if (!decoder) { + taskQueue->BeginShutdown(); + taskQueue->AwaitShutdownAndIdle(); + promise->MaybeResolve(NS_LITERAL_STRING("No; Failed to create H264 decoder")); + return promise.forget(); + } + + decoder->Init() + ->Then(AbstractThread::MainThread(), __func__, + [promise, decoder, taskQueue] (TrackInfo::TrackType aTrack) { + nsCString failureReason; + bool ok = decoder->IsHardwareAccelerated(failureReason); + nsAutoString result; + if (ok) { + result.AssignLiteral("Yes"); + } else { + result.AssignLiteral("No"); + } + if (failureReason.Length()) { + result.AppendLiteral("; "); + AppendUTF8toUTF16(failureReason, result); + } + decoder->Shutdown(); + taskQueue->BeginShutdown(); + taskQueue->AwaitShutdownAndIdle(); + promise->MaybeResolve(result); + }, + [promise, decoder, taskQueue] (MediaResult aError) { + decoder->Shutdown(); + taskQueue->BeginShutdown(); + taskQueue->AwaitShutdownAndIdle(); + promise->MaybeResolve(NS_LITERAL_STRING("No; Failed to initialize H264 decoder")); + }); + + return promise.forget(); +} + +void +MP4Decoder::GetMozDebugReaderData(nsAString& aString) +{ + if (mReader) { + mReader->GetMozDebugReaderData(aString); + } +} + +} // namespace mozilla diff --git a/dom/media/fmp4/MP4Decoder.h b/dom/media/fmp4/MP4Decoder.h new file mode 100644 index 000000000..ad5fba7b5 --- /dev/null +++ b/dom/media/fmp4/MP4Decoder.h @@ -0,0 +1,62 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#if !defined(MP4Decoder_h_) +#define MP4Decoder_h_ + +#include "MediaDecoder.h" +#include "MediaFormatReader.h" +#include "mozilla/dom/Promise.h" +#include "mozilla/layers/KnowsCompositor.h" + +namespace mozilla { + +class MediaContentType; + +// Decoder that uses a bundled MP4 demuxer and platform decoders to play MP4. +class MP4Decoder : public MediaDecoder +{ +public: + explicit MP4Decoder(MediaDecoderOwner* aOwner); + + MediaDecoder* Clone(MediaDecoderOwner* aOwner) override { + if (!IsEnabled()) { + return nullptr; + } + return new MP4Decoder(aOwner); + } + + MediaDecoderStateMachine* CreateStateMachine() override; + + // Returns true if aType is a type that we think we can render with the + // a MP4 platform decoder backend. + static bool CanHandleMediaType(const MediaContentType& aType, + DecoderDoctorDiagnostics* aDiagnostics); + + // Return true if aMimeType is a one of the strings used by our demuxers to + // identify H264. Does not parse general content type strings, i.e. white + // space matters. + static bool IsH264(const nsACString& aMimeType); + + // Return true if aMimeType is a one of the strings used by our demuxers to + // identify AAC. Does not parse general content type strings, i.e. white + // space matters. + static bool IsAAC(const nsACString& aMimeType); + + // Returns true if the MP4 backend is preffed on. + static bool IsEnabled(); + + static already_AddRefed<dom::Promise> + IsVideoAccelerated(layers::KnowsCompositor* aKnowsCompositor, nsIGlobalObject* aParent); + + void GetMozDebugReaderData(nsAString& aString) override; + +private: + RefPtr<MediaFormatReader> mReader; +}; + +} // namespace mozilla + +#endif diff --git a/dom/media/fmp4/MP4Demuxer.cpp b/dom/media/fmp4/MP4Demuxer.cpp new file mode 100644 index 000000000..70b176699 --- /dev/null +++ b/dom/media/fmp4/MP4Demuxer.cpp @@ -0,0 +1,502 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include <algorithm> +#include <limits> +#include <stdint.h> + +#include "MP4Demuxer.h" +#include "mp4_demuxer/MoofParser.h" +#include "mp4_demuxer/MP4Metadata.h" +#include "mp4_demuxer/ResourceStream.h" +#include "mp4_demuxer/BufferStream.h" +#include "mp4_demuxer/Index.h" +#include "nsPrintfCString.h" + +// Used for telemetry +#include "mozilla/Telemetry.h" +#include "mp4_demuxer/AnnexB.h" +#include "mp4_demuxer/H264.h" + +#include "nsAutoPtr.h" + +extern mozilla::LazyLogModule gMediaDemuxerLog; +mozilla::LogModule* GetDemuxerLog() { + return gMediaDemuxerLog; +} + +#define LOG(arg, ...) MOZ_LOG(gMediaDemuxerLog, mozilla::LogLevel::Debug, ("MP4Demuxer(%p)::%s: " arg, this, __func__, ##__VA_ARGS__)) + +namespace mozilla { + +class MP4TrackDemuxer : public MediaTrackDemuxer +{ +public: + MP4TrackDemuxer(MP4Demuxer* aParent, + UniquePtr<TrackInfo>&& aInfo, + const nsTArray<mp4_demuxer::Index::Indice>& indices); + + UniquePtr<TrackInfo> GetInfo() const override; + + RefPtr<SeekPromise> Seek(media::TimeUnit aTime) override; + + RefPtr<SamplesPromise> GetSamples(int32_t aNumSamples = 1) override; + + void Reset() override; + + nsresult GetNextRandomAccessPoint(media::TimeUnit* aTime) override; + + RefPtr<SkipAccessPointPromise> SkipToNextRandomAccessPoint(media::TimeUnit aTimeThreshold) override; + + media::TimeIntervals GetBuffered() override; + + void BreakCycles() override; + + void NotifyDataRemoved(); + +private: + friend class MP4Demuxer; + void NotifyDataArrived(); + already_AddRefed<MediaRawData> GetNextSample(); + void EnsureUpToDateIndex(); + void SetNextKeyFrameTime(); + RefPtr<MP4Demuxer> mParent; + RefPtr<mp4_demuxer::ResourceStream> mStream; + UniquePtr<TrackInfo> mInfo; + RefPtr<mp4_demuxer::Index> mIndex; + UniquePtr<mp4_demuxer::SampleIterator> mIterator; + Maybe<media::TimeUnit> mNextKeyframeTime; + // Queued samples extracted by the demuxer, but not yet returned. + RefPtr<MediaRawData> mQueuedSample; + bool mNeedReIndex; + bool mNeedSPSForTelemetry; + bool mIsH264 = false; +}; + + +// Returns true if no SPS was found and search for it should continue. +bool +AccumulateSPSTelemetry(const MediaByteBuffer* aExtradata) +{ + mp4_demuxer::SPSData spsdata; + if (mp4_demuxer::H264::DecodeSPSFromExtraData(aExtradata, spsdata)) { + uint8_t constraints = (spsdata.constraint_set0_flag ? (1 << 0) : 0) | + (spsdata.constraint_set1_flag ? (1 << 1) : 0) | + (spsdata.constraint_set2_flag ? (1 << 2) : 0) | + (spsdata.constraint_set3_flag ? (1 << 3) : 0) | + (spsdata.constraint_set4_flag ? (1 << 4) : 0) | + (spsdata.constraint_set5_flag ? (1 << 5) : 0); + Telemetry::Accumulate(Telemetry::VIDEO_DECODED_H264_SPS_CONSTRAINT_SET_FLAG, + constraints); + + // Collect profile_idc values up to 244, otherwise 0 for unknown. + Telemetry::Accumulate(Telemetry::VIDEO_DECODED_H264_SPS_PROFILE, + spsdata.profile_idc <= 244 ? spsdata.profile_idc : 0); + + // Make sure level_idc represents a value between levels 1 and 5.2, + // otherwise collect 0 for unknown level. + Telemetry::Accumulate(Telemetry::VIDEO_DECODED_H264_SPS_LEVEL, + (spsdata.level_idc >= 10 && spsdata.level_idc <= 52) ? + spsdata.level_idc : 0); + + // max_num_ref_frames should be between 0 and 16, anything larger will + // be treated as invalid. + Telemetry::Accumulate(Telemetry::VIDEO_H264_SPS_MAX_NUM_REF_FRAMES, + std::min(spsdata.max_num_ref_frames, 17u)); + + return false; + } + + return true; +} + +MP4Demuxer::MP4Demuxer(MediaResource* aResource) + : mResource(aResource) + , mStream(new mp4_demuxer::ResourceStream(aResource)) + , mInitData(new MediaByteBuffer) +{ +} + +RefPtr<MP4Demuxer::InitPromise> +MP4Demuxer::Init() +{ + AutoPinned<mp4_demuxer::ResourceStream> stream(mStream); + + // Check that we have enough data to read the metadata. + if (!mp4_demuxer::MP4Metadata::HasCompleteMetadata(stream)) { + return InitPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_DEMUXER_ERR, __func__); + } + + mInitData = mp4_demuxer::MP4Metadata::Metadata(stream); + if (!mInitData) { + // OOM + return InitPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_DEMUXER_ERR, __func__); + } + + RefPtr<mp4_demuxer::BufferStream> bufferstream = + new mp4_demuxer::BufferStream(mInitData); + + mMetadata = MakeUnique<mp4_demuxer::MP4Metadata>(bufferstream); + + if (!mMetadata->GetNumberTracks(mozilla::TrackInfo::kAudioTrack) && + !mMetadata->GetNumberTracks(mozilla::TrackInfo::kVideoTrack)) { + return InitPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_DEMUXER_ERR, __func__); + } + + return InitPromise::CreateAndResolve(NS_OK, __func__); +} + +bool +MP4Demuxer::HasTrackType(TrackInfo::TrackType aType) const +{ + return !!GetNumberTracks(aType); +} + +uint32_t +MP4Demuxer::GetNumberTracks(TrackInfo::TrackType aType) const +{ + return mMetadata->GetNumberTracks(aType); +} + +already_AddRefed<MediaTrackDemuxer> +MP4Demuxer::GetTrackDemuxer(TrackInfo::TrackType aType, uint32_t aTrackNumber) +{ + if (mMetadata->GetNumberTracks(aType) <= aTrackNumber) { + return nullptr; + } + UniquePtr<TrackInfo> info = mMetadata->GetTrackInfo(aType, aTrackNumber); + if (!info) { + return nullptr; + } + FallibleTArray<mp4_demuxer::Index::Indice> indices; + if (!mMetadata->ReadTrackIndex(indices, info->mTrackId)) { + return nullptr; + } + RefPtr<MP4TrackDemuxer> e = new MP4TrackDemuxer(this, Move(info), indices); + mDemuxers.AppendElement(e); + + return e.forget(); +} + +bool +MP4Demuxer::IsSeekable() const +{ + return mMetadata->CanSeek(); +} + +void +MP4Demuxer::NotifyDataArrived() +{ + for (uint32_t i = 0; i < mDemuxers.Length(); i++) { + mDemuxers[i]->NotifyDataArrived(); + } +} + +void +MP4Demuxer::NotifyDataRemoved() +{ + for (uint32_t i = 0; i < mDemuxers.Length(); i++) { + mDemuxers[i]->NotifyDataRemoved(); + } +} + +UniquePtr<EncryptionInfo> +MP4Demuxer::GetCrypto() +{ + const mp4_demuxer::CryptoFile& cryptoFile = mMetadata->Crypto(); + if (!cryptoFile.valid) { + return nullptr; + } + + const nsTArray<mp4_demuxer::PsshInfo>& psshs = cryptoFile.pssh; + nsTArray<uint8_t> initData; + for (uint32_t i = 0; i < psshs.Length(); i++) { + initData.AppendElements(psshs[i].data); + } + + if (initData.IsEmpty()) { + return nullptr; + } + + auto crypto = MakeUnique<EncryptionInfo>(); + crypto->AddInitData(NS_LITERAL_STRING("cenc"), Move(initData)); + + return crypto; +} + +MP4TrackDemuxer::MP4TrackDemuxer(MP4Demuxer* aParent, + UniquePtr<TrackInfo>&& aInfo, + const nsTArray<mp4_demuxer::Index::Indice>& indices) + : mParent(aParent) + , mStream(new mp4_demuxer::ResourceStream(mParent->mResource)) + , mInfo(Move(aInfo)) + , mIndex(new mp4_demuxer::Index(indices, + mStream, + mInfo->mTrackId, + mInfo->IsAudio())) + , mIterator(MakeUnique<mp4_demuxer::SampleIterator>(mIndex)) + , mNeedReIndex(true) +{ + EnsureUpToDateIndex(); // Force update of index + + VideoInfo* videoInfo = mInfo->GetAsVideoInfo(); + // Collect telemetry from h264 AVCC SPS. + if (videoInfo && + (mInfo->mMimeType.EqualsLiteral("video/mp4") || + mInfo->mMimeType.EqualsLiteral("video/avc"))) { + mIsH264 = true; + RefPtr<MediaByteBuffer> extraData = videoInfo->mExtraData; + mNeedSPSForTelemetry = AccumulateSPSTelemetry(extraData); + mp4_demuxer::SPSData spsdata; + if (mp4_demuxer::H264::DecodeSPSFromExtraData(extraData, spsdata) && + spsdata.pic_width > 0 && spsdata.pic_height > 0 && + mp4_demuxer::H264::EnsureSPSIsSane(spsdata)) { + videoInfo->mImage.width = spsdata.pic_width; + videoInfo->mImage.height = spsdata.pic_height; + videoInfo->mDisplay.width = spsdata.display_width; + videoInfo->mDisplay.height = spsdata.display_height; + } + } else { + // No SPS to be found. + mNeedSPSForTelemetry = false; + } +} + +UniquePtr<TrackInfo> +MP4TrackDemuxer::GetInfo() const +{ + return mInfo->Clone(); +} + +void +MP4TrackDemuxer::EnsureUpToDateIndex() +{ + if (!mNeedReIndex) { + return; + } + AutoPinned<MediaResource> resource(mParent->mResource); + MediaByteRangeSet byteRanges; + nsresult rv = resource->GetCachedRanges(byteRanges); + if (NS_FAILED(rv)) { + return; + } + mIndex->UpdateMoofIndex(byteRanges); + mNeedReIndex = false; +} + +RefPtr<MP4TrackDemuxer::SeekPromise> +MP4TrackDemuxer::Seek(media::TimeUnit aTime) +{ + int64_t seekTime = aTime.ToMicroseconds(); + mQueuedSample = nullptr; + + mIterator->Seek(seekTime); + + // Check what time we actually seeked to. + mQueuedSample = GetNextSample(); + if (mQueuedSample) { + seekTime = mQueuedSample->mTime; + } + + SetNextKeyFrameTime(); + + return SeekPromise::CreateAndResolve(media::TimeUnit::FromMicroseconds(seekTime), __func__); +} + +already_AddRefed<MediaRawData> +MP4TrackDemuxer::GetNextSample() +{ + RefPtr<MediaRawData> sample = mIterator->GetNext(); + if (!sample) { + return nullptr; + } + if (mInfo->GetAsVideoInfo()) { + sample->mExtraData = mInfo->GetAsVideoInfo()->mExtraData; + if (mIsH264) { + mp4_demuxer::H264::FrameType type = + mp4_demuxer::H264::GetFrameType(sample); + switch (type) { + case mp4_demuxer::H264::FrameType::I_FRAME: MOZ_FALLTHROUGH; + case mp4_demuxer::H264::FrameType::OTHER: + { + bool keyframe = type == mp4_demuxer::H264::FrameType::I_FRAME; + if (sample->mKeyframe != keyframe) { + NS_WARNING(nsPrintfCString("Frame incorrectly marked as %skeyframe @ pts:%lld dur:%u dts:%lld", + keyframe ? "" : "non-", + sample->mTime, + sample->mDuration, + sample->mTimecode).get()); + sample->mKeyframe = keyframe; + } + break; + } + case mp4_demuxer::H264::FrameType::INVALID: + NS_WARNING(nsPrintfCString("Invalid H264 frame @ pts:%lld dur:%u dts:%lld", + sample->mTime, + sample->mDuration, + sample->mTimecode).get()); + // We could reject the sample now, however demuxer errors are fatal. + // So we keep the invalid frame, relying on the H264 decoder to + // handle the error later. + // TODO: make demuxer errors non-fatal. + break; + } + } + } + if (sample->mCrypto.mValid) { + nsAutoPtr<MediaRawDataWriter> writer(sample->CreateWriter()); + writer->mCrypto.mMode = mInfo->mCrypto.mMode; + writer->mCrypto.mIVSize = mInfo->mCrypto.mIVSize; + writer->mCrypto.mKeyId.AppendElements(mInfo->mCrypto.mKeyId); + } + return sample.forget(); +} + +RefPtr<MP4TrackDemuxer::SamplesPromise> +MP4TrackDemuxer::GetSamples(int32_t aNumSamples) +{ + EnsureUpToDateIndex(); + RefPtr<SamplesHolder> samples = new SamplesHolder; + if (!aNumSamples) { + return SamplesPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_DEMUXER_ERR, __func__); + } + + if (mQueuedSample) { + MOZ_ASSERT(mQueuedSample->mKeyframe, + "mQueuedSample must be a keyframe"); + samples->mSamples.AppendElement(mQueuedSample); + mQueuedSample = nullptr; + aNumSamples--; + } + RefPtr<MediaRawData> sample; + while (aNumSamples && (sample = GetNextSample())) { + if (!sample->Size()) { + continue; + } + samples->mSamples.AppendElement(sample); + aNumSamples--; + } + + if (samples->mSamples.IsEmpty()) { + return SamplesPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_END_OF_STREAM, __func__); + } else { + for (const auto& sample : samples->mSamples) { + // Collect telemetry from h264 Annex B SPS. + if (mNeedSPSForTelemetry && mp4_demuxer::AnnexB::HasSPS(sample)) { + RefPtr<MediaByteBuffer> extradata = + mp4_demuxer::AnnexB::ExtractExtraData(sample); + mNeedSPSForTelemetry = AccumulateSPSTelemetry(extradata); + } + } + + if (mNextKeyframeTime.isNothing() || + samples->mSamples.LastElement()->mTime >= mNextKeyframeTime.value().ToMicroseconds()) { + SetNextKeyFrameTime(); + } + return SamplesPromise::CreateAndResolve(samples, __func__); + } +} + +void +MP4TrackDemuxer::SetNextKeyFrameTime() +{ + mNextKeyframeTime.reset(); + mp4_demuxer::Microseconds frameTime = mIterator->GetNextKeyframeTime(); + if (frameTime != -1) { + mNextKeyframeTime.emplace( + media::TimeUnit::FromMicroseconds(frameTime)); + } +} + +void +MP4TrackDemuxer::Reset() +{ + mQueuedSample = nullptr; + // TODO, Seek to first frame available, which isn't always 0. + mIterator->Seek(0); + SetNextKeyFrameTime(); +} + +nsresult +MP4TrackDemuxer::GetNextRandomAccessPoint(media::TimeUnit* aTime) +{ + if (mNextKeyframeTime.isNothing()) { + // There's no next key frame. + *aTime = + media::TimeUnit::FromMicroseconds(std::numeric_limits<int64_t>::max()); + } else { + *aTime = mNextKeyframeTime.value(); + } + return NS_OK; +} + +RefPtr<MP4TrackDemuxer::SkipAccessPointPromise> +MP4TrackDemuxer::SkipToNextRandomAccessPoint(media::TimeUnit aTimeThreshold) +{ + mQueuedSample = nullptr; + // Loop until we reach the next keyframe after the threshold. + uint32_t parsed = 0; + bool found = false; + RefPtr<MediaRawData> sample; + while (!found && (sample = GetNextSample())) { + parsed++; + if (sample->mKeyframe && sample->mTime >= aTimeThreshold.ToMicroseconds()) { + found = true; + mQueuedSample = sample; + } + } + SetNextKeyFrameTime(); + if (found) { + return SkipAccessPointPromise::CreateAndResolve(parsed, __func__); + } else { + SkipFailureHolder failure(NS_ERROR_DOM_MEDIA_END_OF_STREAM, parsed); + return SkipAccessPointPromise::CreateAndReject(Move(failure), __func__); + } +} + +media::TimeIntervals +MP4TrackDemuxer::GetBuffered() +{ + EnsureUpToDateIndex(); + AutoPinned<MediaResource> resource(mParent->mResource); + MediaByteRangeSet byteRanges; + nsresult rv = resource->GetCachedRanges(byteRanges); + + if (NS_FAILED(rv)) { + return media::TimeIntervals(); + } + + return mIndex->ConvertByteRangesToTimeRanges(byteRanges); +} + +void +MP4TrackDemuxer::NotifyDataArrived() +{ + mNeedReIndex = true; +} + +void +MP4TrackDemuxer::NotifyDataRemoved() +{ + AutoPinned<MediaResource> resource(mParent->mResource); + MediaByteRangeSet byteRanges; + nsresult rv = resource->GetCachedRanges(byteRanges); + if (NS_FAILED(rv)) { + return; + } + mIndex->UpdateMoofIndex(byteRanges, true /* can evict */); + mNeedReIndex = false; +} + +void +MP4TrackDemuxer::BreakCycles() +{ + mParent = nullptr; +} + +} // namespace mozilla + +#undef LOG diff --git a/dom/media/fmp4/MP4Demuxer.h b/dom/media/fmp4/MP4Demuxer.h new file mode 100644 index 000000000..ab3f87c4b --- /dev/null +++ b/dom/media/fmp4/MP4Demuxer.h @@ -0,0 +1,58 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#if !defined(MP4Demuxer_h_) +#define MP4Demuxer_h_ + +#include "mozilla/Maybe.h" +#include "mozilla/Monitor.h" +#include "MediaDataDemuxer.h" +#include "MediaResource.h" + +namespace mp4_demuxer { +class MP4Metadata; +class ResourceStream; +class SampleIterator; +} // namespace mp4_demuxer + +namespace mozilla { + +class MP4TrackDemuxer; + +class MP4Demuxer : public MediaDataDemuxer +{ +public: + explicit MP4Demuxer(MediaResource* aResource); + + RefPtr<InitPromise> Init() override; + + bool HasTrackType(TrackInfo::TrackType aType) const override; + + uint32_t GetNumberTracks(TrackInfo::TrackType aType) const override; + + already_AddRefed<MediaTrackDemuxer> GetTrackDemuxer(TrackInfo::TrackType aType, + uint32_t aTrackNumber) override; + + bool IsSeekable() const override; + + UniquePtr<EncryptionInfo> GetCrypto() override; + + void NotifyDataArrived() override; + + void NotifyDataRemoved() override; + +private: + friend class MP4TrackDemuxer; + RefPtr<MediaResource> mResource; + RefPtr<mp4_demuxer::ResourceStream> mStream; + RefPtr<MediaByteBuffer> mInitData; + UniquePtr<mp4_demuxer::MP4Metadata> mMetadata; + nsTArray<RefPtr<MP4TrackDemuxer>> mDemuxers; +}; + +} // namespace mozilla + +#endif diff --git a/dom/media/fmp4/MP4Stream.cpp b/dom/media/fmp4/MP4Stream.cpp new file mode 100644 index 000000000..615a7dc01 --- /dev/null +++ b/dom/media/fmp4/MP4Stream.cpp @@ -0,0 +1,103 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "MP4Stream.h" +#include "MediaResource.h" + +namespace mozilla { + +MP4Stream::MP4Stream(MediaResource* aResource) + : mResource(aResource) + , mPinCount(0) +{ + MOZ_COUNT_CTOR(MP4Stream); + MOZ_ASSERT(aResource); +} + +MP4Stream::~MP4Stream() +{ + MOZ_COUNT_DTOR(MP4Stream); + MOZ_ASSERT(mPinCount == 0); +} + +bool +MP4Stream::BlockingReadIntoCache(int64_t aOffset, size_t aCount, Monitor* aToUnlock) +{ + MOZ_ASSERT(mPinCount > 0); + CacheBlock block(aOffset, aCount); + if (!block.Init()) { + return false; + } + + uint32_t bytesRead = 0; + { + MonitorAutoUnlock unlock(*aToUnlock); + nsresult rv = mResource.ReadAt(aOffset, block.Buffer(), aCount, &bytesRead); + if (NS_FAILED(rv)) { + return false; + } + } + + MOZ_ASSERT(block.mCount >= bytesRead); + block.mCount = bytesRead; + + mCache.AppendElement(Move(block)); + return true; +} + +// We surreptitiously reimplement the supposedly-blocking ReadAt as a non- +// blocking CachedReadAt, and record when it fails. This allows MP4Reader +// to retry the read as an actual blocking read without holding the lock. +bool +MP4Stream::ReadAt(int64_t aOffset, void* aBuffer, size_t aCount, + size_t* aBytesRead) +{ + if (mFailedRead.isSome()) { + mFailedRead.reset(); + } + + if (!CachedReadAt(aOffset, aBuffer, aCount, aBytesRead)) { + mFailedRead.emplace(aOffset, aCount); + return false; + } + + return true; +} + +bool +MP4Stream::CachedReadAt(int64_t aOffset, void* aBuffer, size_t aCount, + size_t* aBytesRead) +{ + // First, check our local cache. + for (size_t i = 0; i < mCache.Length(); ++i) { + if (mCache[i].mOffset == aOffset && mCache[i].mCount >= aCount) { + memcpy(aBuffer, mCache[i].Buffer(), aCount); + *aBytesRead = aCount; + return true; + } + } + + nsresult rv = + mResource.GetResource()->ReadFromCache(reinterpret_cast<char*>(aBuffer), + aOffset, aCount); + if (NS_FAILED(rv)) { + *aBytesRead = 0; + return false; + } + *aBytesRead = aCount; + return true; +} + +bool +MP4Stream::Length(int64_t* aSize) +{ + if (mResource.GetLength() < 0) + return false; + *aSize = mResource.GetLength(); + return true; +} + +} // namespace mozilla diff --git a/dom/media/fmp4/MP4Stream.h b/dom/media/fmp4/MP4Stream.h new file mode 100644 index 000000000..d7d8e6afc --- /dev/null +++ b/dom/media/fmp4/MP4Stream.h @@ -0,0 +1,107 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef MP4_STREAM_H_ +#define MP4_STREAM_H_ + +#include "mp4_demuxer/Stream.h" + +#include "MediaResource.h" + +#include "mozilla/Maybe.h" +#include "mozilla/Monitor.h" +#include "mozilla/UniquePtrExtensions.h" + +namespace mozilla { + +class Monitor; + +class MP4Stream : public mp4_demuxer::Stream { +public: + explicit MP4Stream(MediaResource* aResource); + virtual ~MP4Stream(); + bool BlockingReadIntoCache(int64_t aOffset, size_t aCount, Monitor* aToUnlock); + bool ReadAt(int64_t aOffset, void* aBuffer, size_t aCount, + size_t* aBytesRead) override; + bool CachedReadAt(int64_t aOffset, void* aBuffer, size_t aCount, + size_t* aBytesRead) override; + bool Length(int64_t* aSize) override; + + struct ReadRecord { + ReadRecord(int64_t aOffset, size_t aCount) : mOffset(aOffset), mCount(aCount) {} + bool operator==(const ReadRecord& aOther) { return mOffset == aOther.mOffset && mCount == aOther.mCount; } + int64_t mOffset; + size_t mCount; + }; + bool LastReadFailed(ReadRecord* aOut) + { + if (mFailedRead.isSome()) { + *aOut = mFailedRead.ref(); + return true; + } + + return false; + } + + void ClearFailedRead() { mFailedRead.reset(); } + + void Pin() + { + mResource.GetResource()->Pin(); + ++mPinCount; + } + + void Unpin() + { + mResource.GetResource()->Unpin(); + MOZ_ASSERT(mPinCount); + --mPinCount; + if (mPinCount == 0) { + mCache.Clear(); + } + } + +private: + MediaResourceIndex mResource; + Maybe<ReadRecord> mFailedRead; + uint32_t mPinCount; + + struct CacheBlock { + CacheBlock(int64_t aOffset, size_t aCount) + : mOffset(aOffset), mCount(aCount), mBuffer(nullptr) {} + int64_t mOffset; + size_t mCount; + + CacheBlock(CacheBlock&& aOther) + : mOffset(aOther.mOffset) + , mCount(aOther.mCount) + , mBuffer(Move(aOther.mBuffer)) + {} + + bool Init() + { + mBuffer = MakeUniqueFallible<char[]>(mCount); + return !!mBuffer; + } + + char* Buffer() + { + MOZ_ASSERT(mBuffer.get()); + return mBuffer.get(); + } + + private: + CacheBlock(const CacheBlock&) = delete; + CacheBlock& operator=(const CacheBlock&) = delete; + + UniquePtr<char[]> mBuffer; + }; + nsTArray<CacheBlock> mCache; +}; + +} // namespace mozilla + +#endif diff --git a/dom/media/fmp4/moz.build b/dom/media/fmp4/moz.build new file mode 100644 index 000000000..6a249ae3e --- /dev/null +++ b/dom/media/fmp4/moz.build @@ -0,0 +1,25 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +EXPORTS += [ + 'MP4Decoder.h', + 'MP4Demuxer.h', + 'MP4Stream.h', +] + +UNIFIED_SOURCES += [ + 'MP4Decoder.cpp', + 'MP4Stream.cpp', +] + +SOURCES += [ + 'MP4Demuxer.cpp', +] + +FINAL_LIBRARY = 'xul' + +if CONFIG['MOZ_GONK_MEDIACODEC']: + DEFINES['MOZ_GONK_MEDIACODEC'] = True |