summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/libwebrtcglue/WebrtcImageBuffer.h
blob: 305f4df5770e7472dc8693db012a145920ad66bb (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 WebrtcImageBuffer_h__
#define WebrtcImageBuffer_h__

#include "common_video/include/video_frame_buffer.h"

namespace mozilla {
namespace layers {
class Image;
}

class ImageBuffer : public webrtc::VideoFrameBuffer {
 public:
  explicit ImageBuffer(RefPtr<layers::Image>&& aImage)
      : mImage(std::move(aImage)) {}

  rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override {
    RefPtr<layers::PlanarYCbCrImage> image = mImage->AsPlanarYCbCrImage();
    MOZ_ASSERT(image);
    if (!image) {
      // TODO. YUV420 ReadBack, Image only provides a RGB readback.
      return nullptr;
    }
    const layers::PlanarYCbCrData* data = image->GetData();
    rtc::scoped_refptr<webrtc::I420BufferInterface> buf =
        webrtc::WrapI420Buffer(
            data->mPictureRect.width, data->mPictureRect.height,
            data->mYChannel, data->mYStride, data->mCbChannel,
            data->mCbCrStride, data->mCrChannel, data->mCbCrStride,
            [image] { /* keep reference alive*/ });
    return buf;
  }

  Type type() const override { return Type::kNative; }

  int width() const override { return mImage->GetSize().width; }

  int height() const override { return mImage->GetSize().height; }

  RefPtr<layers::Image> GetNativeImage() const { return mImage; }

 private:
  const RefPtr<layers::Image> mImage;
};

}  // namespace mozilla

#endif  // WebrtcImageBuffer_h__