Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/VideoSegment.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef MOZILLA_VIDEOSEGMENT_H_
7
#define MOZILLA_VIDEOSEGMENT_H_
8
9
#include "MediaSegment.h"
10
#include "nsCOMPtr.h"
11
#include "gfxPoint.h"
12
#include "ImageContainer.h"
13
14
namespace mozilla {
15
16
namespace layers {
17
class Image;
18
} // namespace layers
19
20
class VideoFrame {
21
public:
22
  typedef mozilla::layers::Image Image;
23
24
  VideoFrame(already_AddRefed<Image>& aImage, const gfx::IntSize& aIntrinsicSize);
25
  VideoFrame();
26
  ~VideoFrame();
27
28
  bool operator==(const VideoFrame& aFrame) const
29
0
  {
30
0
    return mIntrinsicSize == aFrame.mIntrinsicSize &&
31
0
           mForceBlack == aFrame.mForceBlack &&
32
0
           ((mForceBlack && aFrame.mForceBlack) || mImage == aFrame.mImage) &&
33
0
           mPrincipalHandle == aFrame.mPrincipalHandle;
34
0
  }
35
  bool operator!=(const VideoFrame& aFrame) const
36
  {
37
    return !operator==(aFrame);
38
  }
39
40
0
  Image* GetImage() const { return mImage; }
41
0
  void SetForceBlack(bool aForceBlack) { mForceBlack = aForceBlack; }
42
0
  bool GetForceBlack() const { return mForceBlack; }
43
  void SetPrincipalHandle(PrincipalHandle aPrincipalHandle)
44
0
  {
45
0
    mPrincipalHandle = std::forward<PrincipalHandle>(aPrincipalHandle);
46
0
  }
47
0
  const PrincipalHandle& GetPrincipalHandle() const { return mPrincipalHandle; }
48
0
  const gfx::IntSize& GetIntrinsicSize() const { return mIntrinsicSize; }
49
  void SetNull();
50
  void TakeFrom(VideoFrame* aFrame);
51
52
  // Create a planar YCbCr black image.
53
  static already_AddRefed<Image> CreateBlackImage(const gfx::IntSize& aSize);
54
55
protected:
56
  // mImage can be null to indicate "no video" (aka "empty frame"). It can
57
  // still have an intrinsic size in this case.
58
  RefPtr<Image> mImage;
59
  // The desired size to render the video frame at.
60
  gfx::IntSize mIntrinsicSize;
61
  bool mForceBlack;
62
  // principalHandle for the image in this frame.
63
  // This can be compared to an nsIPrincipal when back on main thread.
64
  PrincipalHandle mPrincipalHandle;
65
};
66
67
struct VideoChunk {
68
  void SliceTo(StreamTime aStart, StreamTime aEnd)
69
0
  {
70
0
    NS_ASSERTION(aStart >= 0 && aStart < aEnd && aEnd <= mDuration,
71
0
                 "Slice out of bounds");
72
0
    mDuration = aEnd - aStart;
73
0
  }
74
0
  StreamTime GetDuration() const { return mDuration; }
75
  bool CanCombineWithFollowing(const VideoChunk& aOther) const
76
0
  {
77
0
    return aOther.mFrame == mFrame;
78
0
  }
79
0
  bool IsNull() const { return !mFrame.GetImage(); }
80
  void SetNull(StreamTime aDuration)
81
0
  {
82
0
    mDuration = aDuration;
83
0
    mFrame.SetNull();
84
0
    mTimeStamp = TimeStamp();
85
0
  }
86
0
  void SetForceBlack(bool aForceBlack) { mFrame.SetForceBlack(aForceBlack); }
87
88
  size_t SizeOfExcludingThisIfUnshared(MallocSizeOf aMallocSizeOf) const
89
0
  {
90
0
    // Future:
91
0
    // - mFrame
92
0
    return 0;
93
0
  }
94
95
0
  const PrincipalHandle& GetPrincipalHandle() const { return mFrame.GetPrincipalHandle(); }
96
97
  StreamTime mDuration;
98
  VideoFrame mFrame;
99
  TimeStamp mTimeStamp;
100
};
101
102
class VideoSegment : public MediaSegmentBase<VideoSegment, VideoChunk> {
103
public:
104
  typedef mozilla::layers::Image Image;
105
  typedef mozilla::gfx::IntSize IntSize;
106
107
  VideoSegment();
108
  VideoSegment(VideoSegment&& aSegment);
109
110
  VideoSegment(const VideoSegment&)=delete;
111
  VideoSegment& operator= (const VideoSegment&)=delete;
112
113
  ~VideoSegment();
114
115
  void AppendFrame(already_AddRefed<Image>&& aImage,
116
                   StreamTime aDuration,
117
                   const IntSize& aIntrinsicSize,
118
                   const PrincipalHandle& aPrincipalHandle,
119
                   bool aForceBlack = false,
120
                   TimeStamp aTimeStamp = TimeStamp::Now());
121
  const VideoFrame* GetLastFrame(StreamTime* aStart = nullptr)
122
  {
123
    VideoChunk* c = GetLastChunk();
124
    if (!c) {
125
      return nullptr;
126
    }
127
    if (aStart) {
128
      *aStart = mDuration - c->mDuration;
129
    }
130
    return &c->mFrame;
131
  }
132
  // Override default impl
133
0
  void ReplaceWithDisabled() override {
134
0
    for (ChunkIterator i(*this);
135
0
         !i.IsEnded(); i.Next()) {
136
0
      VideoChunk& chunk = *i;
137
0
      chunk.SetForceBlack(true);
138
0
    }
139
0
  }
140
141
  // Segment-generic methods not in MediaSegmentBase
142
  static Type StaticType() { return VIDEO; }
143
144
  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
145
0
  {
146
0
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
147
0
  }
148
149
  bool IsEmpty() const
150
0
  {
151
0
    return mChunks.IsEmpty();
152
0
  }
153
154
};
155
156
} // namespace mozilla
157
158
#endif /* MOZILLA_VIDEOSEGMENT_H_ */