StreamReceiverBase.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using Unity.WebRTC;
  3. using UnityEngine;
  4. namespace Unity.RenderStreaming
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public abstract class StreamReceiverBase : MonoBehaviour, IStreamReceiver
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public RTCRtpTransceiver Transceiver => m_transceiver;
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. public OnStartedStreamHandler OnStartedStream { get; set; }
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. public OnStoppedStreamHandler OnStoppedStream { get; set; }
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. public MediaStreamTrack Track => m_track;
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. public bool isPlaying
  31. {
  32. get
  33. {
  34. if (string.IsNullOrEmpty(Transceiver.Mid))
  35. return false;
  36. if (Transceiver.Sender.Track.ReadyState == TrackState.Ended)
  37. return false;
  38. return true;
  39. }
  40. }
  41. private RTCRtpTransceiver m_transceiver;
  42. private MediaStreamTrack m_track;
  43. /// <summary>
  44. ///
  45. /// </summary>
  46. /// <param name="connectionId"></param>
  47. /// <param name="receiver"></param>
  48. public virtual void SetTransceiver(string connectionId, RTCRtpTransceiver transceiver)
  49. {
  50. if (connectionId == null)
  51. throw new ArgumentNullException("connectionId", "connectionId is null");
  52. m_transceiver = transceiver;
  53. m_track = m_transceiver?.Receiver.Track;
  54. if (m_transceiver == null)
  55. OnStoppedStream?.Invoke(connectionId);
  56. else
  57. OnStartedStream?.Invoke(connectionId);
  58. }
  59. protected virtual void OnDestroy()
  60. {
  61. m_track?.Dispose();
  62. m_track = null;
  63. }
  64. }
  65. }