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