fork/rust-sdks

git clone https://git.julia.blue/fork/rust-sdks

Max BrunsfeldAdd APIs for converting between native VideoFrameBuffers and their underlying `CVPixelBuffer` on macOS (#355)2487755

main
6.6 KiB247 linesraw
1/*
2 * Copyright 2023 LiveKit
3 *
4 * Licensed under the Apache License, Version 2.0 (the “License”);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an “AS IS” BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <memory>
20
21#include "api/video/i420_buffer.h"
22#include "api/video/i422_buffer.h"
23#include "api/video/i444_buffer.h"
24#include "api/video/i010_buffer.h"
25#include "api/video/nv12_buffer.h"
26#include "api/video/video_frame_buffer.h"
27
28namespace livekit {
29class VideoFrameBuffer;
30class PlanarYuvBuffer;
31class PlanarYuv8Buffer;
32class PlanarYuv16BBuffer;
33class BiplanarYuvBuffer;
34class BiplanarYuv8Buffer;
35class I420Buffer;
36class I420ABuffer;
37class I422Buffer;
38class I444Buffer;
39class I010Buffer;
40class NV12Buffer;
41}  // namespace livekit
42
43#ifdef __APPLE__
44#include <CoreVideo/CoreVideo.h>
45namespace livekit {
46typedef __CVBuffer PlatformImageBuffer;
47}  // namespace livekit
48#else
49namespace livekit {
50typedef void PlatformImageBuffer;
51}  // namespace livekit
52#endif
53
54#include "webrtc-sys/src/video_frame_buffer.rs.h"
55
56namespace livekit {
57
58class VideoFrameBuffer {
59 public:
60  explicit VideoFrameBuffer(
61      rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer);
62
63  VideoFrameBufferType buffer_type() const;
64
65  unsigned int width() const;
66  unsigned int height() const;
67
68  std::unique_ptr<I420Buffer> to_i420() const;
69
70  // Requires ownership
71  std::unique_ptr<I420Buffer> get_i420();
72  std::unique_ptr<I420ABuffer> get_i420a();
73  std::unique_ptr<I422Buffer> get_i422();
74  std::unique_ptr<I444Buffer> get_i444();
75  std::unique_ptr<I010Buffer> get_i010();
76  std::unique_ptr<NV12Buffer> get_nv12();
77  rtc::scoped_refptr<webrtc::VideoFrameBuffer> get() const;
78
79 protected:
80  rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer_;
81};
82
83class PlanarYuvBuffer : public VideoFrameBuffer {
84 public:
85  explicit PlanarYuvBuffer(rtc::scoped_refptr<webrtc::PlanarYuvBuffer> buffer);
86
87  unsigned int chroma_width() const;
88  unsigned int chroma_height() const;
89
90  unsigned int stride_y() const;
91  unsigned int stride_u() const;
92  unsigned int stride_v() const;
93
94 private:
95  webrtc::PlanarYuvBuffer* buffer() const;
96};
97
98class PlanarYuv8Buffer : public PlanarYuvBuffer {
99 public:
100  explicit PlanarYuv8Buffer(
101      rtc::scoped_refptr<webrtc::PlanarYuv8Buffer> buffer);
102
103  const uint8_t* data_y() const;
104  const uint8_t* data_u() const;
105  const uint8_t* data_v() const;
106
107 private:
108  webrtc::PlanarYuv8Buffer* buffer() const;
109};
110
111class PlanarYuv16BBuffer : public PlanarYuvBuffer {
112 public:
113  explicit PlanarYuv16BBuffer(
114      rtc::scoped_refptr<webrtc::PlanarYuv16BBuffer> buffer);
115
116  const uint16_t* data_y() const;
117  const uint16_t* data_u() const;
118  const uint16_t* data_v() const;
119
120 private:
121  webrtc::PlanarYuv16BBuffer* buffer() const;
122};
123
124class BiplanarYuvBuffer : public VideoFrameBuffer {
125 public:
126  explicit BiplanarYuvBuffer(
127      rtc::scoped_refptr<webrtc::BiplanarYuvBuffer> buffer);
128
129  unsigned int chroma_width() const;
130  unsigned int chroma_height() const;
131
132  unsigned int stride_y() const;
133  unsigned int stride_uv() const;
134
135 private:
136  webrtc::BiplanarYuvBuffer* buffer() const;
137};
138
139class BiplanarYuv8Buffer : public BiplanarYuvBuffer {
140 public:
141  explicit BiplanarYuv8Buffer(
142      rtc::scoped_refptr<webrtc::BiplanarYuv8Buffer> buffer);
143
144  const uint8_t* data_y() const;
145  const uint8_t* data_uv() const;
146
147 private:
148  webrtc::BiplanarYuv8Buffer* buffer() const;
149};
150
151class I420Buffer : public PlanarYuv8Buffer {
152 public:
153  explicit I420Buffer(rtc::scoped_refptr<webrtc::I420BufferInterface> buffer);
154};
155
156class I420ABuffer : public I420Buffer {
157 public:
158  explicit I420ABuffer(rtc::scoped_refptr<webrtc::I420ABufferInterface> buffer);
159
160  unsigned int stride_a() const;
161  const uint8_t* data_a() const;
162
163 private:
164  webrtc::I420ABufferInterface* buffer() const;
165};
166
167class I422Buffer : public PlanarYuv8Buffer {
168 public:
169  explicit I422Buffer(rtc::scoped_refptr<webrtc::I422BufferInterface> buffer);
170};
171
172class I444Buffer : public PlanarYuv8Buffer {
173 public:
174  explicit I444Buffer(rtc::scoped_refptr<webrtc::I444BufferInterface> buffer);
175};
176
177class I010Buffer : public PlanarYuv16BBuffer {
178 public:
179  explicit I010Buffer(rtc::scoped_refptr<webrtc::I010BufferInterface> buffer);
180};
181
182class NV12Buffer : public BiplanarYuv8Buffer {
183 public:
184  explicit NV12Buffer(rtc::scoped_refptr<webrtc::NV12BufferInterface> buffer);
185};
186
187std::unique_ptr<I420Buffer> copy_i420_buffer(
188    const std::unique_ptr<I420Buffer>& i420);
189std::unique_ptr<I420Buffer> new_i420_buffer(int width, int height, int stride_y, int stride_u, int stride_v);
190std::unique_ptr<I422Buffer> new_i422_buffer(int width, int height, int stride_y, int stride_u, int stride_v);
191std::unique_ptr<I444Buffer> new_i444_buffer(int width, int height, int stride_y, int stride_u, int stride_v);
192std::unique_ptr<I010Buffer> new_i010_buffer(int width, int height, int stride_y, int stride_u, int stride_v);
193std::unique_ptr<NV12Buffer> new_nv12_buffer(int width, int height, int stride_y, int stride_uv);
194
195std::unique_ptr<VideoFrameBuffer> new_native_buffer_from_platform_image_buffer(PlatformImageBuffer *buffer);
196PlatformImageBuffer* native_buffer_to_platform_image_buffer(const std::unique_ptr<VideoFrameBuffer> &);
197
198static const VideoFrameBuffer* yuv_to_vfb(const PlanarYuvBuffer* yuv) {
199  return yuv;
200}
201
202static const VideoFrameBuffer* biyuv_to_vfb(const BiplanarYuvBuffer* biyuv) {
203  return biyuv;
204}
205
206static const PlanarYuvBuffer* yuv8_to_yuv(const PlanarYuv8Buffer* yuv8) {
207  return yuv8;
208}
209
210static const PlanarYuvBuffer* yuv16b_to_yuv(const PlanarYuv16BBuffer* yuv16) {
211  return yuv16;
212}
213
214static const BiplanarYuvBuffer* biyuv8_to_biyuv(
215    const BiplanarYuv8Buffer* biyuv8) {
216  return biyuv8;
217}
218
219static const PlanarYuv8Buffer* i420_to_yuv8(const I420Buffer* i420) {
220  return i420;
221}
222
223static const PlanarYuv8Buffer* i420a_to_yuv8(const I420ABuffer* i420a) {
224  return i420a;
225}
226
227static const PlanarYuv8Buffer* i422_to_yuv8(const I422Buffer* i422) {
228  return i422;
229}
230
231static const PlanarYuv8Buffer* i444_to_yuv8(const I444Buffer* i444) {
232  return i444;
233}
234
235static const PlanarYuv16BBuffer* i010_to_yuv16b(const I010Buffer* i010) {
236  return i010;
237}
238
239static const BiplanarYuv8Buffer* nv12_to_biyuv8(const NV12Buffer* nv12) {
240  return nv12;
241}
242
243static std::unique_ptr<VideoFrameBuffer> _unique_video_frame_buffer() {
244  return nullptr;
245}
246
247}  // namespace livekit