VideoFrameData.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace fis.media.Library.Players
  7. {
  8. public class VideoFrameData : IDisposable
  9. {
  10. private readonly Action<object> _dispose;
  11. private bool _disposed;
  12. public int Width { get; set; }
  13. public int Height { get; set; }
  14. public byte[] Data { get; set; }
  15. public object? ImageData { get; set; }
  16. public string? Key { get; set; }
  17. public VideoFrameData(int width, int height, byte[] data, Action<object> dispose = null!)
  18. {
  19. Width = width;
  20. Height = height;
  21. Data = data;
  22. _dispose = dispose;
  23. }
  24. public string ToBase64()
  25. {
  26. return Convert.ToBase64String(Data, 0, Data.Length);
  27. }
  28. ~VideoFrameData()
  29. {
  30. Dispose();
  31. }
  32. public void Dispose()
  33. {
  34. if (!_disposed)
  35. {
  36. _dispose?.Invoke(ImageData!);
  37. _disposed = true;
  38. }
  39. }
  40. }
  41. public class ImageDataBuffer : IDisposable
  42. {
  43. private uint _width;
  44. private uint _height;
  45. private int _dataSize;
  46. private byte[]? _buffer;
  47. public void SetBufferSize(uint width, uint height, int dataSize)
  48. {
  49. if (width != _width || height != _height || dataSize != _dataSize)
  50. {
  51. _buffer = new byte[width * height * dataSize];
  52. _width = width;
  53. _height = height;
  54. _dataSize = dataSize;
  55. }
  56. }
  57. public void SetBuffer(byte[] image)
  58. {
  59. if (_buffer != null)
  60. {
  61. _buffer = image;
  62. }
  63. }
  64. public byte[]? GetBuffer()
  65. {
  66. return _buffer;
  67. }
  68. public void Dispose()
  69. {
  70. _buffer = null;
  71. }
  72. }
  73. public class RemoteVideoFrameData
  74. {
  75. public string UserId { get; }
  76. public VideoFrameData? Data { get; set; }
  77. public ImageDataBuffer ImageDataBuffer { get; }
  78. public RemoteVideoFrameData(string userId)
  79. {
  80. UserId = userId;
  81. ImageDataBuffer = new ImageDataBuffer();
  82. }
  83. }
  84. public class LocalPreviewVideoFrameData
  85. {
  86. public string UserId { get; }
  87. public VideoFrameData? Data { get; set; }
  88. public LocalPreviewVideoFrameData(string userId)
  89. {
  90. UserId = userId;
  91. }
  92. }
  93. }