AudioStreamReceiver.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using Unity.WebRTC;
  5. using UnityEngine;
  6. namespace Unity.RenderStreaming
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. [AddComponentMenu("Render Streaming/Audio Stream Receiver")]
  12. public class AudioStreamReceiver : StreamReceiverBase
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. /// <param name="source"></param>
  18. public delegate void OnUpdateReceiveAudioSourceHandler(AudioSource source);
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. public OnUpdateReceiveAudioSourceHandler OnUpdateReceiveAudioSource;
  23. [SerializeField]
  24. private AudioSource m_TargetAudioSource;
  25. [SerializeField, Codec]
  26. private AudioCodecInfo m_Codec;
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. public AudioCodecInfo codec
  31. {
  32. get { return m_Codec; }
  33. }
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. public AudioSource targetAudioSource
  38. {
  39. get { return m_TargetAudioSource; }
  40. set { m_TargetAudioSource = value; }
  41. }
  42. /// <summary>
  43. ///
  44. /// </summary>
  45. /// <returns></returns>
  46. static public IEnumerable<AudioCodecInfo> GetAvailableCodecs()
  47. {
  48. var excludeCodecMimeType = new[] { "audio/CN", "audio/telephone-event" };
  49. var capabilities = RTCRtpReceiver.GetCapabilities(TrackKind.Audio);
  50. return capabilities.codecs.Where(codec => !excludeCodecMimeType.Contains(codec.mimeType)).Select(codec => AudioCodecInfo.Create(codec));
  51. }
  52. /// <summary>
  53. ///
  54. /// </summary>
  55. /// <param name="mimeType"></param>
  56. public void SetCodec(AudioCodecInfo codec)
  57. {
  58. m_Codec = codec;
  59. if (Transceiver == null)
  60. return;
  61. if (!string.IsNullOrEmpty(Transceiver.Mid))
  62. throw new InvalidOperationException("Transceiver is streaming. This operation is invalid during the track is in use.");
  63. if (Transceiver.Sender.Track.ReadyState == TrackState.Ended)
  64. throw new InvalidOperationException("Track has already been ended.");
  65. var codecs = new AudioCodecInfo[] { m_Codec };
  66. RTCErrorType error = Transceiver.SetCodecPreferences(SelectCodecCapabilities(codecs).ToArray());
  67. if (error != RTCErrorType.None)
  68. throw new InvalidOperationException($"Set codec is failed. errorCode={error}");
  69. }
  70. internal IEnumerable<RTCRtpCodecCapability> SelectCodecCapabilities(IEnumerable<AudioCodecInfo> codecs)
  71. {
  72. return RTCRtpReceiver.GetCapabilities(TrackKind.Audio).SelectCodecCapabilities(codecs);
  73. }
  74. private protected virtual void Start()
  75. {
  76. OnStartedStream += StartedStream;
  77. OnStoppedStream += StoppedStream;
  78. }
  79. private void StartedStream(string connectionId)
  80. {
  81. if (Track is AudioStreamTrack audioTrack)
  82. {
  83. m_TargetAudioSource?.SetTrack(audioTrack);
  84. OnUpdateReceiveAudioSource?.Invoke(m_TargetAudioSource);
  85. }
  86. }
  87. private void StoppedStream(string connectionId)
  88. {
  89. }
  90. }
  91. }