Commit d367adba authored by Chun-Min Chang's avatar Chun-Min Chang
Browse files

Bug 1801818 - Make VideoFrame.timestamp non-null r=padenot,emilio

parent ab4009c7
Loading
Loading
Loading
Loading
+19 −26
Original line number Diff line number Diff line
@@ -1054,7 +1054,7 @@ static Result<RefPtr<VideoFrame>, nsCString> CreateVideoFrameFromBuffer(
  return MakeRefPtr<VideoFrame>(
      aGlobal, data, aInit.mFormat, codedSize, parsedRect,
      displaySize ? *displaySize : parsedRect.Size(), std::move(duration),
      Some(aInit.mTimestamp), colorSpace);
      aInit.mTimestamp, colorSpace);
}

template <class T>
@@ -1138,14 +1138,14 @@ InitializeFrameWithResourceAndSize(
  return MakeAndAddRef<VideoFrame>(aGlobal, image, format->PixelFormat(),
                                   image->GetSize(), visibleRect.value(),
                                   displaySize.value(), std::move(duration),
                                   Some(aInit.mTimestamp.Value()), colorSpace);
                                   aInit.mTimestamp.Value(), colorSpace);
}

// https://w3c.github.io/webcodecs/#videoframe-initialize-frame-from-other-frame
struct VideoFrameData {
  VideoFrameData(layers::Image* aImage, const VideoPixelFormat& aFormat,
                 gfx::IntRect aVisibleRect, gfx::IntSize aDisplaySize,
                 Maybe<uint64_t> aDuration, Maybe<int64_t> aTimestamp,
                 Maybe<uint64_t> aDuration, int64_t aTimestamp,
                 const VideoColorSpaceInit& aColorSpace)
      : mImage(aImage),
        mFormat(aFormat),
@@ -1160,7 +1160,7 @@ struct VideoFrameData {
  const gfx::IntRect mVisibleRect;
  const gfx::IntSize mDisplaySize;
  const Maybe<uint64_t> mDuration;
  const Maybe<int64_t> mTimestamp;
  const int64_t mTimestamp;
  const VideoColorSpaceInit mColorSpace;
};
static Result<already_AddRefed<VideoFrame>, nsCString>
@@ -1186,14 +1186,13 @@ InitializeFrameFromOtherFrame(nsIGlobalObject* aGlobal, VideoFrameData&& aData,
  Maybe<uint64_t> duration = aInit.mDuration.WasPassed()
                                 ? Some(aInit.mDuration.Value())
                                 : aData.mDuration;
  Maybe<int64_t> timestamp = aInit.mTimestamp.WasPassed()
                                 ? Some(aInit.mTimestamp.Value())
  int64_t timestamp = aInit.mTimestamp.WasPassed() ? aInit.mTimestamp.Value()
                                                   : aData.mTimestamp;

  return MakeAndAddRef<VideoFrame>(
      aGlobal, aData.mImage, aData.mFormat.PixelFormat(),
      aData.mImage->GetSize(), *visibleRect, *displaySize, std::move(duration),
      std::move(timestamp), aData.mColorSpace);
      timestamp, aData.mColorSpace);
}

/*
@@ -1204,7 +1203,7 @@ VideoFrame::VideoFrame(nsIGlobalObject* aParent,
                       const RefPtr<layers::Image>& aImage,
                       const VideoPixelFormat& aFormat, gfx::IntSize aCodedSize,
                       gfx::IntRect aVisibleRect, gfx::IntSize aDisplaySize,
                       Maybe<uint64_t>&& aDuration, Maybe<int64_t>&& aTimestamp,
                       Maybe<uint64_t>&& aDuration, int64_t aTimestamp,
                       const VideoColorSpaceInit& aColorSpace)
    : mParent(aParent),
      mResource(Some(Resource(aImage, VideoFrame::Format(aFormat)))),
@@ -1212,7 +1211,7 @@ VideoFrame::VideoFrame(nsIGlobalObject* aParent,
      mVisibleRect(aVisibleRect),
      mDisplaySize(aDisplaySize),
      mDuration(std::move(aDuration)),
      mTimestamp(std::move(aTimestamp)),
      mTimestamp(aTimestamp),
      mColorSpace(aColorSpace) {
  MOZ_ASSERT(mParent);
}
@@ -1451,11 +1450,12 @@ already_AddRefed<VideoFrame> VideoFrame::Constructor(
    return nullptr;
  }

  // TODO: Retrive/infer the duration, timestamp, and colorspace.
  // TODO: Retrive/infer the duration, and colorspace.
  auto r = InitializeFrameFromOtherFrame(
      global.get(),
      VideoFrameData(image.get(), format.ref(), image->GetPictureRect(),
                     image->GetSize(), Nothing(), Nothing(), {}),
                     image->GetSize(), Nothing(),
                     static_cast<int64_t>(aVideoElement.CurrentTime()), {}),
      aInit);
  if (r.isErr()) {
    aRv.ThrowTypeError(r.unwrapErr());
@@ -1691,10 +1691,10 @@ Nullable<uint64_t> VideoFrame::GetDuration() const {
}

// https://w3c.github.io/webcodecs/#dom-videoframe-timestamp
Nullable<int64_t> VideoFrame::GetTimestamp() const {
int64_t VideoFrame::Timestamp() const {
  AssertIsOnOwningThread();

  return mTimestamp ? Nullable<int64_t>(*mTimestamp) : Nullable<int64_t>();
  return mTimestamp;
}

// https://w3c.github.io/webcodecs/#dom-videoframe-colorspace
@@ -1840,7 +1840,6 @@ void VideoFrame::Close() {
  mVisibleRect = gfx::IntRect();
  mDisplaySize = gfx::IntSize();
  mDuration.reset();
  mTimestamp.reset();
}

// https://w3c.github.io/webcodecs/#ref-for-deserialization-steps%E2%91%A0
@@ -1890,16 +1889,12 @@ JSObject* VideoFrame::ReadStructuredClone(JSContext* aCx,
      hasDuration ? Some(uint64_t(durationHigh) << 32 | durationLow)
                  : Nothing();

  uint8_t hasTimestamp = 0;
  uint32_t timestampLow = 0;
  uint32_t timestampHigh = 0;
  if (NS_WARN_IF(!JS_ReadBytes(aReader, &hasTimestamp, 1)) ||
      NS_WARN_IF(!JS_ReadUint32Pair(aReader, &timestampLow, &timestampHigh))) {
  if (NS_WARN_IF(!JS_ReadUint32Pair(aReader, &timestampLow, &timestampHigh))) {
    return nullptr;
  }
  Maybe<uint64_t> timestamp =
      hasTimestamp ? Some(uint64_t(timestampHigh) << 32 | timestampLow)
                   : Nothing();
  int64_t timestamp = int64_t(timestampHigh) << 32 | timestampLow;

  uint8_t colorSpaceFullRange = 0;
  uint8_t colorSpaceMatrix = 0;
@@ -1942,7 +1937,7 @@ JSObject* VideoFrame::ReadStructuredClone(JSContext* aCx,
        aGlobal, aData.mImage, format, gfx::IntSize(codedWidth, codedHeight),
        gfx::IntRect(visibleX, visibleY, visibleWidth, visibleHeight),
        gfx::IntSize(displayWidth, displayHeight), std::move(duration),
        std::move(timestamp), colorSpace);
        timestamp, colorSpace);
    if (!GetOrCreateDOMReflector(aCx, frame, &value) || !value.isObject()) {
      return nullptr;
    }
@@ -1978,9 +1973,8 @@ bool VideoFrame::WriteStructuredClone(JSStructuredCloneWriter* aWriter,
  const uint32_t durationLow = mDuration ? uint32_t(*mDuration) : 0;
  const uint32_t durationHigh = mDuration ? uint32_t(*mDuration >> 32) : 0;

  const uint8_t hasTimestamp = mTimestamp ? 1 : 0;
  const uint32_t timestampLow = mTimestamp ? uint32_t(*mTimestamp) : 0;
  const uint32_t timestampHigh = mTimestamp ? uint32_t(*mTimestamp >> 32) : 0;
  const uint32_t timestampLow = uint32_t(mTimestamp);
  const uint32_t timestampHigh = uint32_t(mTimestamp >> 32);

  const uint8_t colorSpaceFullRange =
      mColorSpace.mFullRange.WasPassed() ? mColorSpace.mFullRange.Value() : 2;
@@ -2014,7 +2008,6 @@ bool VideoFrame::WriteStructuredClone(JSStructuredCloneWriter* aWriter,
      NS_WARN_IF(!JS_WriteUint32Pair(aWriter, displayWidth, displayHeight)) ||
      NS_WARN_IF(!JS_WriteBytes(aWriter, &hasDuration, 1)) ||
      NS_WARN_IF(!JS_WriteUint32Pair(aWriter, durationLow, durationHigh)) ||
      NS_WARN_IF(!JS_WriteBytes(aWriter, &hasTimestamp, 1)) ||
      NS_WARN_IF(!JS_WriteUint32Pair(aWriter, timestampLow, timestampHigh)) ||
      NS_WARN_IF(!JS_WriteBytes(aWriter, &colorSpaceFullRange, 1)) ||
      NS_WARN_IF(!JS_WriteBytes(aWriter, &colorSpaceMatrix, 1)) ||
+3 −3
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ class VideoFrame final : public nsISupports, public nsWrapperCache {
  VideoFrame(nsIGlobalObject* aParent, const RefPtr<layers::Image>& aImage,
             const VideoPixelFormat& aFormat, gfx::IntSize aCodedSize,
             gfx::IntRect aVisibleRect, gfx::IntSize aDisplaySize,
             Maybe<uint64_t>&& aDuration, Maybe<int64_t>&& aTimestamp,
             Maybe<uint64_t>&& aDuration, int64_t aTimestamp,
             const VideoColorSpaceInit& aColorSpace);

  VideoFrame(const VideoFrame& aOther);
@@ -128,7 +128,7 @@ class VideoFrame final : public nsISupports, public nsWrapperCache {

  Nullable<uint64_t> GetDuration() const;

  Nullable<int64_t> GetTimestamp() const;
  int64_t Timestamp() const;

  already_AddRefed<VideoColorSpace> ColorSpace() const;

@@ -206,7 +206,7 @@ class VideoFrame final : public nsISupports, public nsWrapperCache {
  gfx::IntSize mDisplaySize;

  Maybe<uint64_t> mDuration;  // Nothing() after `Close()`d
  Maybe<int64_t> mTimestamp;  // Nothing() after `Close()`d
  int64_t mTimestamp;
  VideoColorSpaceInit mColorSpace;
};

+1 −1
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ interface VideoFrame {
  readonly attribute unsigned long displayWidth;
  readonly attribute unsigned long displayHeight;
  readonly attribute unsigned long long? duration;  // microseconds
  readonly attribute long long? timestamp;          // microseconds
  readonly attribute long long timestamp;           // microseconds
  readonly attribute VideoColorSpace colorSpace;

  [Throws]