VideoSegment.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef MOZILLA_VIDEOSEGMENT_H_
  6. #define MOZILLA_VIDEOSEGMENT_H_
  7. #include "MediaSegment.h"
  8. #include "nsCOMPtr.h"
  9. #include "gfxPoint.h"
  10. #include "ImageContainer.h"
  11. namespace mozilla {
  12. namespace layers {
  13. class Image;
  14. } // namespace layers
  15. class VideoFrame {
  16. public:
  17. typedef mozilla::layers::Image Image;
  18. VideoFrame(already_AddRefed<Image>& aImage, const gfx::IntSize& aIntrinsicSize);
  19. VideoFrame();
  20. ~VideoFrame();
  21. bool operator==(const VideoFrame& aFrame) const
  22. {
  23. return mIntrinsicSize == aFrame.mIntrinsicSize &&
  24. mForceBlack == aFrame.mForceBlack &&
  25. ((mForceBlack && aFrame.mForceBlack) || mImage == aFrame.mImage) &&
  26. mPrincipalHandle == aFrame.mPrincipalHandle;
  27. }
  28. bool operator!=(const VideoFrame& aFrame) const
  29. {
  30. return !operator==(aFrame);
  31. }
  32. Image* GetImage() const { return mImage; }
  33. void SetForceBlack(bool aForceBlack) { mForceBlack = aForceBlack; }
  34. bool GetForceBlack() const { return mForceBlack; }
  35. void SetPrincipalHandle(const PrincipalHandle& aPrincipalHandle) { mPrincipalHandle = aPrincipalHandle; }
  36. PrincipalHandle GetPrincipalHandle() const { return mPrincipalHandle; }
  37. const gfx::IntSize& GetIntrinsicSize() const { return mIntrinsicSize; }
  38. void SetNull();
  39. void TakeFrom(VideoFrame* aFrame);
  40. // Create a planar YCbCr black image.
  41. static already_AddRefed<Image> CreateBlackImage(const gfx::IntSize& aSize);
  42. protected:
  43. // mImage can be null to indicate "no video" (aka "empty frame"). It can
  44. // still have an intrinsic size in this case.
  45. RefPtr<Image> mImage;
  46. // The desired size to render the video frame at.
  47. gfx::IntSize mIntrinsicSize;
  48. bool mForceBlack;
  49. // principalHandle for the image in this frame.
  50. // This can be compared to an nsIPrincipal when back on main thread.
  51. PrincipalHandle mPrincipalHandle;
  52. };
  53. struct VideoChunk {
  54. VideoChunk();
  55. ~VideoChunk();
  56. void SliceTo(StreamTime aStart, StreamTime aEnd)
  57. {
  58. NS_ASSERTION(aStart >= 0 && aStart < aEnd && aEnd <= mDuration,
  59. "Slice out of bounds");
  60. mDuration = aEnd - aStart;
  61. }
  62. StreamTime GetDuration() const { return mDuration; }
  63. bool CanCombineWithFollowing(const VideoChunk& aOther) const
  64. {
  65. return aOther.mFrame == mFrame;
  66. }
  67. bool IsNull() const { return !mFrame.GetImage(); }
  68. void SetNull(StreamTime aDuration)
  69. {
  70. mDuration = aDuration;
  71. mFrame.SetNull();
  72. mTimeStamp = TimeStamp();
  73. }
  74. void SetForceBlack(bool aForceBlack) { mFrame.SetForceBlack(aForceBlack); }
  75. size_t SizeOfExcludingThisIfUnshared(MallocSizeOf aMallocSizeOf) const
  76. {
  77. // Future:
  78. // - mFrame
  79. return 0;
  80. }
  81. PrincipalHandle GetPrincipalHandle() const { return mFrame.GetPrincipalHandle(); }
  82. StreamTime mDuration;
  83. VideoFrame mFrame;
  84. TimeStamp mTimeStamp;
  85. };
  86. class VideoSegment : public MediaSegmentBase<VideoSegment, VideoChunk> {
  87. public:
  88. typedef mozilla::layers::Image Image;
  89. typedef mozilla::gfx::IntSize IntSize;
  90. VideoSegment();
  91. ~VideoSegment();
  92. void AppendFrame(already_AddRefed<Image>&& aImage,
  93. StreamTime aDuration,
  94. const IntSize& aIntrinsicSize,
  95. const PrincipalHandle& aPrincipalHandle,
  96. bool aForceBlack = false,
  97. TimeStamp aTimeStamp = TimeStamp::Now());
  98. const VideoFrame* GetLastFrame(StreamTime* aStart = nullptr)
  99. {
  100. VideoChunk* c = GetLastChunk();
  101. if (!c) {
  102. return nullptr;
  103. }
  104. if (aStart) {
  105. *aStart = mDuration - c->mDuration;
  106. }
  107. return &c->mFrame;
  108. }
  109. // Override default impl
  110. void ReplaceWithDisabled() override {
  111. for (ChunkIterator i(*this);
  112. !i.IsEnded(); i.Next()) {
  113. VideoChunk& chunk = *i;
  114. chunk.SetForceBlack(true);
  115. }
  116. }
  117. // Segment-generic methods not in MediaSegmentBase
  118. static Type StaticType() { return VIDEO; }
  119. size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
  120. {
  121. return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  122. }
  123. bool IsEmpty() const
  124. {
  125. return mChunks.IsEmpty();
  126. }
  127. };
  128. } // namespace mozilla
  129. #endif /* MOZILLA_VIDEOSEGMENT_H_ */