| 1 | // Copyright 2025 LiveKit, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use super::VideoTrack; |
| 16 | use crate::{ |
| 17 | media_stream::{ |
| 18 | BiplanarYuv8Buffer, BiplanarYuvBuffer, PlanarYuv16BBuffer, PlanarYuv8Buffer, |
| 19 | PlanarYuvBuffer, VideoFrameBuffer, |
| 20 | }, |
| 21 | video_frame::{BiplanarYuv8Buffer, I420Buffer, SinkError, VideoFrame, VideoFrameBuffer}, |
| 22 | I010Buffer, I420ABuffer, I422Buffer, I444Buffer, NV12Buffer, |
| 23 | }; |
| 24 | use std::sync::mpsc; |
| 25 | use web_sys::{WebGlRenderingContext, WebGlTexture}; |
| 26 | |
| 27 | #[derive(Debug)] |
| 28 | pub struct WebGlVideoSink { |
| 29 | track: Arc<VideoTrack>, |
| 30 | gl_ctx: WebGlRenderingContext, |
| 31 | tex: WebGlTexture, |
| 32 | } |
| 33 | |
| 34 | /// Create a new WebGL texture and update it inside requestAnimationFrame |
| 35 | impl WebGlVideoSink { |
| 36 | pub fn new( |
| 37 | track: Arc<VideoTrack>, |
| 38 | gl_ctx: WebGlRenderingContext, |
| 39 | ) -> Result<(Self, mpsc::Receiver<VideoFrame<WebGlBuffer>>), SinkError> { |
| 40 | let (sender, receiver) = mpsc::channel(); |
| 41 | let tex = gl_ctx.create_texture()?; |
| 42 | Ok((Self { track, gl_ctx, tex }, receiver)) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | #[derive(Debug, Clone)] |
| 47 | pub struct WebGlBuffer { |
| 48 | width: i32, |
| 49 | height: i32, |
| 50 | tex: WebGlTexture, |
| 51 | } |
| 52 | |
| 53 | impl VideoFrameBuffer for WebGlBuffer { |
| 54 | fn width(&self) -> i32 { |
| 55 | self.width |
| 56 | } |
| 57 | |
| 58 | fn height(&self) -> i32 { |
| 59 | self.height |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// The following types could be implemented if we want |
| 64 | /// to support VideoFrame with WebCodecs |
| 65 | |
| 66 | #[derive(Debug)] |
| 67 | pub struct I420Buffer {} |
| 68 | |
| 69 | #[derive(Debug)] |
| 70 | pub struct I420ABuffer {} |
| 71 | |
| 72 | #[derive(Debug)] |
| 73 | pub struct I422Buffer {} |
| 74 | |
| 75 | #[derive(Debug)] |
| 76 | pub struct I444Buffer {} |
| 77 | |
| 78 | #[derive(Debug)] |
| 79 | pub struct I010Buffer {} |
| 80 | |
| 81 | #[derive(Debug)] |
| 82 | pub struct NV12Buffer {} |
| 83 | |
| 84 | impl VideoFrameBuffer for I420Buffer { |
| 85 | fn width(&self) -> i32 { |
| 86 | unimplemented!() |
| 87 | } |
| 88 | |
| 89 | fn height(&self) -> i32 { |
| 90 | unimplemented!() |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl VideoFrameBuffer for I420ABuffer { |
| 95 | fn width(&self) -> i32 { |
| 96 | unimplemented!() |
| 97 | } |
| 98 | |
| 99 | fn height(&self) -> i32 { |
| 100 | unimplemented!() |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | impl VideoFrameBuffer for I422Buffer { |
| 105 | fn width(&self) -> i32 { |
| 106 | unimplemented!() |
| 107 | } |
| 108 | |
| 109 | fn height(&self) -> i32 { |
| 110 | unimplemented!() |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl VideoFrameBuffer for I444Buffer { |
| 115 | fn width(&self) -> i32 { |
| 116 | unimplemented!() |
| 117 | } |
| 118 | |
| 119 | fn height(&self) -> i32 { |
| 120 | unimplemented!() |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | impl VideoFrameBuffer for I010Buffer { |
| 125 | fn width(&self) -> i32 { |
| 126 | unimplemented!() |
| 127 | } |
| 128 | |
| 129 | fn height(&self) -> i32 { |
| 130 | unimplemented!() |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | impl VideoFrameBuffer for NV12Buffer { |
| 135 | fn width(&self) -> i32 { |
| 136 | unimplemented!() |
| 137 | } |
| 138 | |
| 139 | fn height(&self) -> i32 { |
| 140 | unimplemented!() |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | impl PlanarYuvBuffer for I420Buffer { |
| 145 | fn chroma_width(&self) -> i32 { |
| 146 | unimplemented!() |
| 147 | } |
| 148 | |
| 149 | fn chroma_height(&self) -> i32 { |
| 150 | unimplemented!() |
| 151 | } |
| 152 | |
| 153 | fn stride_y(&self) -> i32 { |
| 154 | unimplemented!() |
| 155 | } |
| 156 | |
| 157 | fn stride_u(&self) -> i32 { |
| 158 | unimplemented!() |
| 159 | } |
| 160 | |
| 161 | fn stride_v(&self) -> i32 { |
| 162 | unimplemented!() |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | impl PlanarYuvBuffer for I420ABuffer { |
| 167 | fn chroma_width(&self) -> i32 { |
| 168 | unimplemented!() |
| 169 | } |
| 170 | |
| 171 | fn chroma_height(&self) -> i32 { |
| 172 | unimplemented!() |
| 173 | } |
| 174 | |
| 175 | fn stride_y(&self) -> i32 { |
| 176 | unimplemented!() |
| 177 | } |
| 178 | |
| 179 | fn stride_u(&self) -> i32 { |
| 180 | unimplemented!() |
| 181 | } |
| 182 | |
| 183 | fn stride_v(&self) -> i32 { |
| 184 | unimplemented!() |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | impl PlanarYuvBuffer for I422Buffer { |
| 189 | fn chroma_width(&self) -> i32 { |
| 190 | unimplemented!() |
| 191 | } |
| 192 | |
| 193 | fn chroma_height(&self) -> i32 { |
| 194 | unimplemented!() |
| 195 | } |
| 196 | |
| 197 | fn stride_y(&self) -> i32 { |
| 198 | unimplemented!() |
| 199 | } |
| 200 | |
| 201 | fn stride_u(&self) -> i32 { |
| 202 | unimplemented!() |
| 203 | } |
| 204 | |
| 205 | fn stride_v(&self) -> i32 { |
| 206 | unimplemented!() |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | impl PlanarYuvBuffer for I444Buffer { |
| 211 | fn chroma_width(&self) -> i32 { |
| 212 | unimplemented!() |
| 213 | } |
| 214 | |
| 215 | fn chroma_height(&self) -> i32 { |
| 216 | unimplemented!() |
| 217 | } |
| 218 | |
| 219 | fn stride_y(&self) -> i32 { |
| 220 | unimplemented!() |
| 221 | } |
| 222 | |
| 223 | fn stride_u(&self) -> i32 { |
| 224 | unimplemented!() |
| 225 | } |
| 226 | |
| 227 | fn stride_v(&self) -> i32 { |
| 228 | unimplemented!() |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | impl PlanarYuvBuffer for I010Buffer { |
| 233 | fn chroma_width(&self) -> i32 { |
| 234 | unimplemented!() |
| 235 | } |
| 236 | |
| 237 | fn chroma_height(&self) -> i32 { |
| 238 | unimplemented!() |
| 239 | } |
| 240 | |
| 241 | fn stride_y(&self) -> i32 { |
| 242 | unimplemented!() |
| 243 | } |
| 244 | |
| 245 | fn stride_u(&self) -> i32 { |
| 246 | unimplemented!() |
| 247 | } |
| 248 | |
| 249 | fn stride_v(&self) -> i32 { |
| 250 | unimplemented!() |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | impl PlanarYuvBuffer for NV12Buffer { |
| 255 | fn chroma_width(&self) -> i32 { |
| 256 | unimplemented!() |
| 257 | } |
| 258 | |
| 259 | fn chroma_height(&self) -> i32 { |
| 260 | unimplemented!() |
| 261 | } |
| 262 | |
| 263 | fn stride_y(&self) -> i32 { |
| 264 | unimplemented!() |
| 265 | } |
| 266 | |
| 267 | fn stride_u(&self) -> i32 { |
| 268 | unimplemented!() |
| 269 | } |
| 270 | |
| 271 | fn stride_v(&self) -> i32 { |
| 272 | unimplemented!() |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | impl PlanarYuv8Buffer for I420Buffer { |
| 277 | fn data_y(&self) -> &[u8] { |
| 278 | unimplemented!() |
| 279 | } |
| 280 | |
| 281 | fn data_u(&self) -> &[u8] { |
| 282 | unimplemented!() |
| 283 | } |
| 284 | |
| 285 | fn data_v(&self) -> &[u8] { |
| 286 | unimplemented!() |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | impl PlanarYuv8Buffer for I420ABuffer { |
| 291 | fn data_y(&self) -> &[u8] { |
| 292 | unimplemented!() |
| 293 | } |
| 294 | |
| 295 | fn data_u(&self) -> &[u8] { |
| 296 | unimplemented!() |
| 297 | } |
| 298 | |
| 299 | fn data_v(&self) -> &[u8] { |
| 300 | unimplemented!() |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | impl PlanarYuv8Buffer for I422Buffer { |
| 305 | fn data_y(&self) -> &[u8] { |
| 306 | unimplemented!() |
| 307 | } |
| 308 | |
| 309 | fn data_u(&self) -> &[u8] { |
| 310 | unimplemented!() |
| 311 | } |
| 312 | |
| 313 | fn data_v(&self) -> &[u8] { |
| 314 | unimplemented!() |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | impl PlanarYuv8Buffer for I444Buffer { |
| 319 | fn data_y(&self) -> &[u8] { |
| 320 | unimplemented!() |
| 321 | } |
| 322 | |
| 323 | fn data_u(&self) -> &[u8] { |
| 324 | unimplemented!() |
| 325 | } |
| 326 | |
| 327 | fn data_v(&self) -> &[u8] { |
| 328 | unimplemented!() |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | impl PlanarYuv16BBuffer for I010Buffer { |
| 333 | fn data_y(&self) -> &[u16] { |
| 334 | unimplemented!() |
| 335 | } |
| 336 | |
| 337 | fn data_u(&self) -> &[u16] { |
| 338 | unimplemented!() |
| 339 | } |
| 340 | |
| 341 | fn data_v(&self) -> &[u16] { |
| 342 | unimplemented!() |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | impl BiplanarYuvBuffer for NV12Buffer { |
| 347 | fn chroma_width(&self) -> i32 { |
| 348 | unimplemented!() |
| 349 | } |
| 350 | |
| 351 | fn chroma_height(&self) -> i32 { |
| 352 | unimplemented!() |
| 353 | } |
| 354 | |
| 355 | fn stride_y(&self) -> i32 { |
| 356 | unimplemented!() |
| 357 | } |
| 358 | |
| 359 | fn stride_uv(&self) -> i32 { |
| 360 | unimplemented!() |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | impl BiplanarYuv8Buffer for NV12Buffer { |
| 365 | fn data_y(&self) -> &[u8] { |
| 366 | unimplemented!() |
| 367 | } |
| 368 | |
| 369 | fn data_uv(&self) -> &[u8] { |
| 370 | unimplemented!() |
| 371 | } |
| 372 | } |