summaryrefslogtreecommitdiffstats
path: root/dom/media/VideoSegment.h
blob: 5421112c9f59252608fb123d8d16077583229ca1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 MOZILLA_VIDEOSEGMENT_H_
#define MOZILLA_VIDEOSEGMENT_H_

#include "MediaSegment.h"
#include "nsCOMPtr.h"
#include "gfxPoint.h"
#include "ImageContainer.h"

namespace mozilla {

namespace layers {
class Image;
}  // namespace layers

class VideoFrame {
 public:
  typedef mozilla::layers::Image Image;

  VideoFrame(already_AddRefed<Image> aImage,
             const gfx::IntSize& aIntrinsicSize);
  VideoFrame();
  ~VideoFrame();

  bool operator==(const VideoFrame& aFrame) const {
    return mIntrinsicSize == aFrame.mIntrinsicSize &&
           mForceBlack == aFrame.mForceBlack &&
           ((mForceBlack && aFrame.mForceBlack) || mImage == aFrame.mImage) &&
           mPrincipalHandle == aFrame.mPrincipalHandle;
  }
  bool operator!=(const VideoFrame& aFrame) const {
    return !operator==(aFrame);
  }

  Image* GetImage() const { return mImage; }
  void SetForceBlack(bool aForceBlack) { mForceBlack = aForceBlack; }
  bool GetForceBlack() const { return mForceBlack; }
  void SetPrincipalHandle(PrincipalHandle aPrincipalHandle) {
    mPrincipalHandle = std::forward<PrincipalHandle>(aPrincipalHandle);
  }
  const PrincipalHandle& GetPrincipalHandle() const { return mPrincipalHandle; }
  const gfx::IntSize& GetIntrinsicSize() const { return mIntrinsicSize; }
  void SetNull();
  void TakeFrom(VideoFrame* aFrame);

  // Create a planar YCbCr black image.
  static already_AddRefed<Image> CreateBlackImage(const gfx::IntSize& aSize);

 protected:
  // mImage can be null to indicate "no video" (aka "empty frame"). It can
  // still have an intrinsic size in this case.
  RefPtr<Image> mImage;
  // The desired size to render the video frame at.
  gfx::IntSize mIntrinsicSize;
  bool mForceBlack;
  // principalHandle for the image in this frame.
  // This can be compared to an nsIPrincipal when back on main thread.
  PrincipalHandle mPrincipalHandle;
};

struct VideoChunk {
  void SliceTo(TrackTime aStart, TrackTime aEnd) {
    NS_ASSERTION(aStart >= 0 && aStart < aEnd && aEnd <= mDuration,
                 "Slice out of bounds");
    mDuration = aEnd - aStart;
  }
  TrackTime GetDuration() const { return mDuration; }
  bool CanCombineWithFollowing(const VideoChunk& aOther) const {
    return aOther.mFrame == mFrame;
  }
  bool IsNull() const { return !mFrame.GetImage(); }
  void SetNull(TrackTime aDuration) {
    mDuration = aDuration;
    mFrame.SetNull();
    mTimeStamp = TimeStamp();
  }
  void SetForceBlack(bool aForceBlack) { mFrame.SetForceBlack(aForceBlack); }

  size_t SizeOfExcludingThisIfUnshared(MallocSizeOf aMallocSizeOf) const {
    // Future:
    // - mFrame
    return 0;
  }

  const PrincipalHandle& GetPrincipalHandle() const {
    return mFrame.GetPrincipalHandle();
  }

  TrackTime mDuration;
  VideoFrame mFrame;
  TimeStamp mTimeStamp;
};

class VideoSegment : public MediaSegmentBase<VideoSegment, VideoChunk> {
 public:
  typedef mozilla::layers::Image Image;
  typedef mozilla::gfx::IntSize IntSize;

  VideoSegment();
  VideoSegment(VideoSegment&& aSegment);

  VideoSegment(const VideoSegment&) = delete;
  VideoSegment& operator=(const VideoSegment&) = delete;

  ~VideoSegment();

  void AppendFrame(already_AddRefed<Image>&& aImage,
                   const IntSize& aIntrinsicSize,
                   const PrincipalHandle& aPrincipalHandle,
                   bool aForceBlack = false,
                   TimeStamp aTimeStamp = TimeStamp::Now());
  void ExtendLastFrameBy(TrackTime aDuration) {
    if (aDuration <= 0) {
      return;
    }
    if (mChunks.IsEmpty()) {
      mChunks.AppendElement()->SetNull(aDuration);
    } else {
      mChunks[mChunks.Length() - 1].mDuration += aDuration;
    }
    mDuration += aDuration;
  }
  const VideoFrame* GetLastFrame(TrackTime* aStart = nullptr) {
    VideoChunk* c = GetLastChunk();
    if (!c) {
      return nullptr;
    }
    if (aStart) {
      *aStart = mDuration - c->mDuration;
    }
    return &c->mFrame;
  }
  VideoChunk* FindChunkContaining(const TimeStamp& aTime) {
    VideoChunk* previousChunk = nullptr;
    for (VideoChunk& c : mChunks) {
      if (c.mTimeStamp.IsNull()) {
        continue;
      }
      if (c.mTimeStamp > aTime) {
        return previousChunk;
      }
      previousChunk = &c;
    }
    return previousChunk;
  }
  void ForgetUpToTime(const TimeStamp& aTime) {
    VideoChunk* chunk = FindChunkContaining(aTime);
    if (!chunk) {
      return;
    }
    TrackTime duration = 0;
    size_t chunksToRemove = 0;
    for (const VideoChunk& c : mChunks) {
      if (c.mTimeStamp >= chunk->mTimeStamp) {
        break;
      }
      duration += c.GetDuration();
      ++chunksToRemove;
    }
    mChunks.RemoveElementsAt(0, chunksToRemove);
    mDuration -= duration;
    MOZ_ASSERT(mChunks.Capacity() >= DEFAULT_SEGMENT_CAPACITY,
               "Capacity must be retained after removing chunks");
  }
  // Override default impl
  void ReplaceWithDisabled() override {
    for (ChunkIterator i(*this); !i.IsEnded(); i.Next()) {
      VideoChunk& chunk = *i;
      chunk.SetForceBlack(true);
    }
  }

  // Segment-generic methods not in MediaSegmentBase
  static Type StaticType() { return VIDEO; }

  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override {
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  }
};

}  // namespace mozilla

#endif /* MOZILLA_VIDEOSEGMENT_H_ */