Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/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
  {
30
    return mIntrinsicSize == aFrame.mIntrinsicSize &&
31
           mForceBlack == aFrame.mForceBlack &&
32
           ((mForceBlack && aFrame.mForceBlack) || mImage == aFrame.mImage) &&
33
           mPrincipalHandle == aFrame.mPrincipalHandle;
34
  }
35
  bool operator!=(const VideoFrame& aFrame) const
36
0
  {
37
0
    return !operator==(aFrame);
38
0
  }
39
40
  Image* GetImage() const { return mImage; }
41
  void SetForceBlack(bool aForceBlack) { mForceBlack = aForceBlack; }
42
  bool GetForceBlack() const { return mForceBlack; }
43
  void SetPrincipalHandle(PrincipalHandle aPrincipalHandle)
44
  {
45
    mPrincipalHandle = std::forward<PrincipalHandle>(aPrincipalHandle);
46
  }
47
  const PrincipalHandle& GetPrincipalHandle() const { return mPrincipalHandle; }
48
  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
  {
70
    NS_ASSERTION(aStart >= 0 && aStart < aEnd && aEnd <= mDuration,
71
                 "Slice out of bounds");
72
    mDuration = aEnd - aStart;
73
  }
74
  StreamTime GetDuration() const { return mDuration; }
75
  bool CanCombineWithFollowing(const VideoChunk& aOther) const
76
  {
77
    return aOther.mFrame == mFrame;
78
  }
79
  bool IsNull() const { return !mFrame.GetImage(); }
80
  void SetNull(StreamTime aDuration)
81
  {
82
    mDuration = aDuration;
83
    mFrame.SetNull();
84
    mTimeStamp = TimeStamp();
85
  }
86
  void SetForceBlack(bool aForceBlack) { mFrame.SetForceBlack(aForceBlack); }
87
88
  size_t SizeOfExcludingThisIfUnshared(MallocSizeOf aMallocSizeOf) const
89
  {
90
    // Future:
91
    // - mFrame
92
    return 0;
93
  }
94
95
  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
0
  {
123
0
    VideoChunk* c = GetLastChunk();
124
0
    if (!c) {
125
0
      return nullptr;
126
0
    }
127
0
    if (aStart) {
128
0
      *aStart = mDuration - c->mDuration;
129
0
    }
130
0
    return &c->mFrame;
131
0
  }
132
  // Override default impl
133
  void ReplaceWithDisabled() override {
134
    for (ChunkIterator i(*this);
135
         !i.IsEnded(); i.Next()) {
136
      VideoChunk& chunk = *i;
137
      chunk.SetForceBlack(true);
138
    }
139
  }
140
141
  // Segment-generic methods not in MediaSegmentBase
142
0
  static Type StaticType() { return VIDEO; }
143
144
  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
145
  {
146
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
147
  }
148
149
  bool IsEmpty() const
150
  {
151
    return mChunks.IsEmpty();
152
  }
153
154
};
155
156
} // namespace mozilla
157
158
#endif /* MOZILLA_VIDEOSEGMENT_H_ */