fork/rust-sdks
git clone https://git.julia.blue/fork/rust-sdks
2487755
main
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#include "livekit/video_frame_buffer.h" 18 19#include "api/make_ref_counted.h" 20 21namespace livekit { 22 23VideoFrameBuffer ::VideoFrameBuffer ( 24 rtc::scoped_refptr < webrtc::VideoFrameBuffer > buffer ) 25 :buffer_ (std::move (buffer )) {} 26 27VideoFrameBufferType VideoFrameBuffer ::buffer_type ()const { 28return static_cast < VideoFrameBufferType > (buffer_ -> type ()); 29} 30 31unsigned int VideoFrameBuffer ::width ()const { 32return buffer_ -> width (); 33} 34 35unsigned int VideoFrameBuffer ::height ()const { 36return buffer_ -> height (); 37} 38 39std::unique_ptr < I420Buffer > VideoFrameBuffer ::to_i420 ()const { 40return std::make_unique < I420Buffer > (buffer_ -> ToI420 ()); 41} 42 43// const_cast is valid here because we take the ownership on the rust side 44std::unique_ptr < I420Buffer > VideoFrameBuffer ::get_i420 () { 45return std::make_unique < I420Buffer > ( 46 rtc::scoped_refptr < webrtc::I420BufferInterface > ( 47const_cast < webrtc::I420BufferInterface *> (buffer_ -> GetI420 ()))); 48} 49 50std::unique_ptr < I420ABuffer > VideoFrameBuffer ::get_i420a () { 51return std::make_unique < I420ABuffer > ( 52 rtc::scoped_refptr < webrtc::I420ABufferInterface > ( 53const_cast < webrtc::I420ABufferInterface *> (buffer_ -> GetI420A ()))); 54} 55 56std::unique_ptr < I422Buffer > VideoFrameBuffer ::get_i422 () { 57return std::make_unique < I422Buffer > ( 58 rtc::scoped_refptr < webrtc::I422BufferInterface > ( 59const_cast < webrtc::I422BufferInterface *> (buffer_ -> GetI422 ()))); 60} 61 62std::unique_ptr < I444Buffer > VideoFrameBuffer ::get_i444 () { 63return std::make_unique < I444Buffer > ( 64 rtc::scoped_refptr < webrtc::I444BufferInterface > ( 65const_cast < webrtc::I444BufferInterface *> (buffer_ -> GetI444 ()))); 66} 67 68std::unique_ptr < I010Buffer > VideoFrameBuffer ::get_i010 () { 69return std::make_unique < I010Buffer > ( 70 rtc::scoped_refptr < webrtc::I010BufferInterface > ( 71const_cast < webrtc::I010BufferInterface *> (buffer_ -> GetI010 ()))); 72} 73 74std::unique_ptr < NV12Buffer > VideoFrameBuffer ::get_nv12 () { 75return std::make_unique < NV12Buffer > ( 76 rtc::scoped_refptr < webrtc::NV12BufferInterface > ( 77const_cast < webrtc::NV12BufferInterface *> (buffer_ -> GetNV12 ()))); 78} 79 80rtc::scoped_refptr < webrtc::VideoFrameBuffer > VideoFrameBuffer ::get ()const { 81return buffer_ ; 82} 83 84PlanarYuvBuffer ::PlanarYuvBuffer ( 85 rtc::scoped_refptr < webrtc::PlanarYuvBuffer > buffer ) 86 :VideoFrameBuffer (buffer ) {} 87 88unsigned int PlanarYuvBuffer ::chroma_width ()const { 89return buffer ()-> ChromaWidth (); 90} 91 92unsigned int PlanarYuvBuffer ::chroma_height ()const { 93return buffer ()-> ChromaHeight (); 94} 95 96unsigned int PlanarYuvBuffer ::stride_y ()const { 97return buffer ()-> StrideY (); 98} 99 100unsigned int PlanarYuvBuffer ::stride_u ()const { 101return buffer ()-> StrideU (); 102} 103 104unsigned int PlanarYuvBuffer ::stride_v ()const { 105return buffer ()-> StrideV (); 106} 107 108webrtc::PlanarYuvBuffer * PlanarYuvBuffer ::buffer ()const { 109return static_cast < webrtc::PlanarYuvBuffer *> (buffer_ .get ()); 110} 111 112PlanarYuv8Buffer ::PlanarYuv8Buffer ( 113 rtc::scoped_refptr < webrtc::PlanarYuv8Buffer > buffer ) 114 :PlanarYuvBuffer (buffer ) {} 115 116const uint8_t * PlanarYuv8Buffer ::data_y ()const { 117return buffer ()-> DataY (); 118} 119 120const uint8_t * PlanarYuv8Buffer ::data_u ()const { 121return buffer ()-> DataU (); 122} 123 124const uint8_t * PlanarYuv8Buffer ::data_v ()const { 125return buffer ()-> DataV (); 126} 127 128webrtc::PlanarYuv8Buffer * PlanarYuv8Buffer ::buffer ()const { 129return static_cast < webrtc::PlanarYuv8Buffer *> (buffer_ .get ()); 130} 131 132PlanarYuv16BBuffer ::PlanarYuv16BBuffer ( 133 rtc::scoped_refptr < webrtc::PlanarYuv16BBuffer > buffer ) 134 :PlanarYuvBuffer (buffer ) {} 135 136const uint16_t * PlanarYuv16BBuffer ::data_y ()const { 137return buffer ()-> DataY (); 138} 139 140const uint16_t * PlanarYuv16BBuffer ::data_u ()const { 141return buffer ()-> DataU (); 142} 143 144const uint16_t * PlanarYuv16BBuffer ::data_v ()const { 145return buffer ()-> DataV (); 146} 147 148webrtc::PlanarYuv16BBuffer * PlanarYuv16BBuffer ::buffer ()const { 149return static_cast < webrtc::PlanarYuv16BBuffer *> (buffer_ .get ()); 150} 151 152BiplanarYuvBuffer ::BiplanarYuvBuffer ( 153 rtc::scoped_refptr < webrtc::BiplanarYuvBuffer > buffer ) 154 :VideoFrameBuffer (buffer ) {} 155 156unsigned int BiplanarYuvBuffer ::chroma_width ()const { 157return buffer ()-> ChromaWidth (); 158} 159 160unsigned int BiplanarYuvBuffer ::chroma_height ()const { 161return buffer ()-> ChromaHeight (); 162} 163 164unsigned int BiplanarYuvBuffer ::stride_y ()const { 165return buffer ()-> StrideY (); 166} 167 168unsigned int BiplanarYuvBuffer ::stride_uv ()const { 169return buffer ()-> StrideUV (); 170} 171 172webrtc::BiplanarYuvBuffer * BiplanarYuvBuffer ::buffer ()const { 173return static_cast < webrtc::BiplanarYuvBuffer *> (buffer_ .get ()); 174} 175 176BiplanarYuv8Buffer ::BiplanarYuv8Buffer ( 177 rtc::scoped_refptr < webrtc::BiplanarYuv8Buffer > buffer ) 178 :BiplanarYuvBuffer (buffer ) {} 179 180const uint8_t * BiplanarYuv8Buffer ::data_y ()const { 181return buffer ()-> DataY (); 182} 183 184const uint8_t * BiplanarYuv8Buffer ::data_uv ()const { 185return buffer ()-> DataUV (); 186} 187 188webrtc::BiplanarYuv8Buffer * BiplanarYuv8Buffer ::buffer ()const { 189return static_cast < webrtc::BiplanarYuv8Buffer *> (buffer_ .get ()); 190} 191 192I420Buffer ::I420Buffer (rtc::scoped_refptr < webrtc::I420BufferInterface > buffer ) 193 :PlanarYuv8Buffer (buffer ) {} 194 195I420ABuffer ::I420ABuffer ( 196 rtc::scoped_refptr < webrtc::I420ABufferInterface > buffer ) 197 :I420Buffer (buffer ) {} 198 199unsigned int I420ABuffer ::stride_a ()const { 200return buffer ()-> StrideA (); 201} 202 203const uint8_t * I420ABuffer ::data_a ()const { 204return buffer ()-> DataA (); 205} 206 207webrtc::I420ABufferInterface * I420ABuffer ::buffer ()const { 208return static_cast < webrtc::I420ABufferInterface *> (buffer_ .get ()); 209} 210 211I422Buffer ::I422Buffer (rtc::scoped_refptr < webrtc::I422BufferInterface > buffer ) 212 :PlanarYuv8Buffer (buffer ) {} 213 214I444Buffer ::I444Buffer (rtc::scoped_refptr < webrtc::I444BufferInterface > buffer ) 215 :PlanarYuv8Buffer (buffer ) {} 216 217I010Buffer ::I010Buffer (rtc::scoped_refptr < webrtc::I010BufferInterface > buffer ) 218 :PlanarYuv16BBuffer (buffer ) {} 219 220NV12Buffer ::NV12Buffer (rtc::scoped_refptr < webrtc::NV12BufferInterface > buffer ) 221 :BiplanarYuv8Buffer (buffer ) {} 222 223std::unique_ptr < I420Buffer > copy_i420_buffer ( 224const std::unique_ptr < I420Buffer >& i420 ) { 225return std::make_unique < I420Buffer > (webrtc::I420Buffer ::Copy (* i420 -> get ())); 226} 227 228std::unique_ptr < I420Buffer > new_i420_buffer (int width , 229int height , 230int stride_y , 231int stride_u , 232int stride_v ) { 233return std::make_unique < I420Buffer > ( 234 webrtc::I420Buffer ::Create (width ,height ,stride_y ,stride_u ,stride_v )); 235} 236 237std::unique_ptr < I422Buffer > new_i422_buffer (int width , 238int height , 239int stride_y , 240int stride_u , 241int stride_v ) { 242return std::make_unique < I422Buffer > ( 243 webrtc::I422Buffer ::Create (width ,height ,stride_y ,stride_u ,stride_v )); 244} 245 246std::unique_ptr < I444Buffer > new_i444_buffer (int width , 247int height , 248int stride_y , 249int stride_u , 250int stride_v ) { 251return std::make_unique < I444Buffer > ( 252 webrtc::I444Buffer ::Create (width ,height ,stride_y ,stride_u ,stride_v )); 253} 254 255std::unique_ptr < I010Buffer > new_i010_buffer (int width , 256int height , 257int stride_y , 258int stride_u , 259int stride_v ) { 260return std::make_unique < I010Buffer > (rtc::make_ref_counted < webrtc::I010Buffer > ( 261width ,height ,stride_y ,stride_u ,stride_v )); 262} 263 264std::unique_ptr < NV12Buffer > new_nv12_buffer (int width , 265int height , 266int stride_y , 267int stride_uv ) { 268return std::make_unique < NV12Buffer > ( 269 webrtc::NV12Buffer ::Create (width ,height ,stride_y ,stride_uv )); 270} 271 272#ifndef __APPLE__ 273 274std::unique_ptr < VideoFrameBuffer > new_native_buffer_from_platform_image_buffer ( 275PlatformImageBuffer * buffer 276) { 277return nullptr ; 278} 279 280PlatformImageBuffer * native_buffer_to_platform_image_buffer ( 281const std::unique_ptr < VideoFrameBuffer > & buffer 282) { 283return nullptr ; 284} 285 286#endif 287 288}// namespace livekit