ImageTrack.cpp (4192B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/ImageTrack.h" 8 9 #include "ImageContainer.h" 10 #include "mozilla/dom/ImageTrackList.h" 11 #include "mozilla/dom/WebCodecsUtils.h" 12 #include "mozilla/gfx/2D.h" 13 #include "mozilla/image/ImageUtils.h" 14 15 extern mozilla::LazyLogModule gWebCodecsLog; 16 17 namespace mozilla::dom { 18 19 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(ImageTrack, mParent, mTrackList, 20 mDecodedFrames) 21 NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageTrack) 22 NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageTrack) 23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageTrack) 24 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 25 NS_INTERFACE_MAP_ENTRY(nsISupports) 26 NS_INTERFACE_MAP_END 27 28 ImageTrack::ImageTrack(ImageTrackList* aTrackList, int32_t aIndex, 29 nsTArray<ImageSize>&& aNativeSizes, bool aSelected, 30 bool aAnimated, uint32_t aFrameCount, 31 bool aFrameCountComplete, float aRepetitionCount) 32 : mParent(aTrackList->GetParentObject()), 33 mTrackList(aTrackList), 34 mNativeSizes(std::move(aNativeSizes)), 35 mFramesTimestamp(image::FrameTimeout::Zero()), 36 mIndex(aIndex), 37 mRepetitionCount(aRepetitionCount), 38 mFrameCount(aFrameCount), 39 mFrameCountComplete(aFrameCountComplete), 40 mAnimated(aAnimated), 41 mSelected(aSelected) {} 42 43 ImageTrack::~ImageTrack() = default; 44 45 void ImageTrack::Destroy() { mTrackList = nullptr; } 46 47 JSObject* ImageTrack::WrapObject(JSContext* aCx, 48 JS::Handle<JSObject*> aGivenProto) { 49 AssertIsOnOwningThread(); 50 return ImageTrack_Binding::Wrap(aCx, this, aGivenProto); 51 } 52 53 void ImageTrack::SetSelected(bool aSelected) { 54 if (mTrackList) { 55 mTrackList->SetSelectedIndex(mIndex, aSelected); 56 } 57 } 58 59 void ImageTrack::GetSizes(nsTArray<ImageSize>& aSizes) { 60 aSizes = mNativeSizes.Clone(); 61 } 62 63 void ImageTrack::OnFrameCountSuccess( 64 const image::DecodeFrameCountResult& aResult) { 65 MOZ_ASSERT_IF(mFrameCountComplete, mFrameCount == aResult.mFrameCount); 66 MOZ_ASSERT_IF(!aResult.mFinished, !mFrameCountComplete); 67 MOZ_ASSERT_IF(!mAnimated, aResult.mFrameCount <= 1); 68 MOZ_ASSERT(aResult.mFrameCount >= mFrameCount); 69 mFrameCount = aResult.mFrameCount; 70 mFrameCountComplete = aResult.mFinished; 71 } 72 73 void ImageTrack::OnDecodeFramesSuccess( 74 const image::DecodeFramesResult& aResult) { 75 MOZ_LOG(gWebCodecsLog, LogLevel::Debug, 76 ("ImageTrack %p OnDecodeFramesSuccess -- decoded %zu frames " 77 "(finished %d), already had %zu frames (finished %d)", 78 this, aResult.mFrames.Length(), aResult.mFinished, 79 mDecodedFrames.Length(), mDecodedFramesComplete)); 80 81 mDecodedFramesComplete = aResult.mFinished; 82 mDecodedFrames.SetCapacity(mDecodedFrames.Length() + 83 aResult.mFrames.Length()); 84 85 for (const auto& f : aResult.mFrames) { 86 VideoColorSpaceInternal colorSpace; 87 gfx::IntSize size = f.mSurface->GetSize(); 88 gfx::IntRect rect(gfx::IntPoint(0, 0), size); 89 90 Maybe<VideoPixelFormat> format = 91 SurfaceFormatToVideoPixelFormat(f.mSurface->GetFormat()); 92 MOZ_ASSERT(format, "Unexpected format for image!"); 93 94 Maybe<uint64_t> duration; 95 if (f.mTimeout != image::FrameTimeout::Forever()) { 96 duration = 97 Some(static_cast<uint64_t>(f.mTimeout.AsMilliseconds()) * 1000); 98 } 99 100 uint64_t timestamp = UINT64_MAX; 101 if (mFramesTimestamp != image::FrameTimeout::Forever()) { 102 timestamp = 103 static_cast<uint64_t>(mFramesTimestamp.AsMilliseconds()) * 1000; 104 } 105 106 mFramesTimestamp += f.mTimeout; 107 108 auto image = MakeRefPtr<layers::SourceSurfaceImage>(size, f.mSurface); 109 auto frame = MakeRefPtr<VideoFrame>(mParent, image, format, size, rect, 110 size, duration, timestamp, colorSpace); 111 mDecodedFrames.AppendElement(std::move(frame)); 112 } 113 } 114 115 } // namespace mozilla::dom