SourceBufferTask.h (4064B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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 #ifndef MOZILLA_SOURCEBUFFERTASK_H_ 8 #define MOZILLA_SOURCEBUFFERTASK_H_ 9 10 #include <utility> 11 12 #include "MediaResult.h" 13 #include "SourceBufferAttributes.h" 14 #include "TimeUnits.h" 15 #include "mozilla/Maybe.h" 16 #include "mozilla/MozPromise.h" 17 18 namespace mozilla { 19 20 class SourceBufferTask { 21 public: 22 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SourceBufferTask); 23 enum class Type { 24 AppendBuffer, 25 Abort, 26 Reset, 27 RangeRemoval, 28 EvictData, 29 Detach, 30 ChangeType 31 }; 32 33 typedef std::pair<bool, SourceBufferAttributes> AppendBufferResult; 34 typedef MozPromise<AppendBufferResult, MediaResult, /* IsExclusive = */ true> 35 AppendPromise; 36 typedef MozPromise<bool, nsresult, /* IsExclusive = */ true> 37 RangeRemovalPromise; 38 39 virtual Type GetType() const = 0; 40 virtual const char* GetTypeName() const = 0; 41 42 template <typename ReturnType> 43 ReturnType* As() { 44 MOZ_ASSERT(this->GetType() == ReturnType::sType); 45 return static_cast<ReturnType*>(this); 46 } 47 48 protected: 49 virtual ~SourceBufferTask() = default; 50 }; 51 52 class AppendBufferTask : public SourceBufferTask { 53 public: 54 AppendBufferTask(already_AddRefed<MediaByteBuffer> aData, 55 const SourceBufferAttributes& aAttributes) 56 : mBuffer(aData), mAttributes(aAttributes) {} 57 58 static const Type sType = Type::AppendBuffer; 59 Type GetType() const override { return Type::AppendBuffer; } 60 const char* GetTypeName() const override { return "AppendBuffer"; } 61 62 RefPtr<MediaByteBuffer> mBuffer; 63 SourceBufferAttributes mAttributes; 64 MozPromiseHolder<AppendPromise> mPromise; 65 }; 66 67 class AbortTask : public SourceBufferTask { 68 public: 69 static const Type sType = Type::Abort; 70 Type GetType() const override { return Type::Abort; } 71 const char* GetTypeName() const override { return "Abort"; } 72 }; 73 74 class ResetTask : public SourceBufferTask { 75 public: 76 static const Type sType = Type::Reset; 77 Type GetType() const override { return Type::Reset; } 78 const char* GetTypeName() const override { return "Reset"; } 79 }; 80 81 class RangeRemovalTask : public SourceBufferTask { 82 public: 83 explicit RangeRemovalTask(const media::TimeInterval& aRange) 84 : mRange(aRange) {} 85 86 static const Type sType = Type::RangeRemoval; 87 Type GetType() const override { return Type::RangeRemoval; } 88 const char* GetTypeName() const override { return "RangeRemoval"; } 89 90 media::TimeInterval mRange; 91 MozPromiseHolder<RangeRemovalPromise> mPromise; 92 }; 93 94 class EvictDataTask : public SourceBufferTask { 95 public: 96 EvictDataTask(const media::TimeUnit& aPlaybackTime, int64_t aSizetoEvict) 97 : mPlaybackTime(aPlaybackTime), mSizeToEvict(Some(aSizetoEvict)) {} 98 99 explicit EvictDataTask(const media::TimeUnit& aPlaybackTime) 100 : mPlaybackTime(aPlaybackTime), mSizeToEvict(Nothing()) {} 101 102 static const Type sType = Type::EvictData; 103 Type GetType() const override { return Type::EvictData; } 104 const char* GetTypeName() const override { return "EvictData"; } 105 106 media::TimeUnit mPlaybackTime; 107 // If not present, that means the size of eviction will be determined 108 // automatically depends on the current buffer condition. 109 Maybe<int64_t> mSizeToEvict; 110 }; 111 112 class DetachTask : public SourceBufferTask { 113 public: 114 static const Type sType = Type::Detach; 115 Type GetType() const override { return Type::Detach; } 116 const char* GetTypeName() const override { return "Detach"; } 117 }; 118 119 class ChangeTypeTask : public SourceBufferTask { 120 public: 121 explicit ChangeTypeTask(const MediaContainerType& aType) : mType(aType) {} 122 123 static const Type sType = Type::ChangeType; 124 Type GetType() const override { return Type::ChangeType; } 125 const char* GetTypeName() const override { return "ChangeType"; } 126 127 const MediaContainerType mType; 128 }; 129 130 } // namespace mozilla 131 132 #endif