#pragma warning restore CS0067 public AudioExtrasSource() { _audioEncoder = new AudioEncoder(); _audioOpts = new AudioSourceOptions { AudioSource = AudioSourcesEnum.None }; }
#pragma warning restore CS0067 public AudioExtrasSource() { _audioEncoder = new AudioEncoder(); _audioFormatManager = new MediaFormatManager <AudioFormat>(_audioEncoder.SupportedFormats); _audioOpts = new AudioSourceOptions { AudioSource = AudioSourcesEnum.None }; }
/// <summary> /// Instantiates an audio source that can generate output samples from a variety of different /// non-live sources. /// </summary> /// <param name="audioOptions">Optional. The options that determine the type of audio to stream to the remote party. /// Example type of audio sources are music, silence, white noise etc.</param> public AudioExtrasSource( AudioEncoder audioEncoder, AudioSourceOptions audioOptions = null) { _audioEncoder = audioEncoder; _audioOpts = audioOptions ?? new AudioSourceOptions { AudioSource = AudioSourcesEnum.None }; }
/// <summary> /// Creates an audio only RTP session that can supply an audio stream to the caller. /// </summary> /// <param name="audioOptions">The options that determine the type of audio to stream to the remote party. Example /// type of audio sources are music, silence, white noise etc.</param> /// <param name="audioCodecs">The audio codecs to support.</param> /// <param name="bindAddress">Optional. If specified this address will be used as the bind address for any RTP /// and control sockets created. Generally this address does not need to be set. The default behaviour /// is to bind to [::] or 0.0.0.0,d depending on system support, which minimises network routing /// causing connection issues.</param> public RtpAudioSession(AudioSourceOptions audioOptions, List <SDPMediaFormatsEnum> audioCodecs, IPAddress bindAddress = null) : base(false, false, false, bindAddress) { if (audioCodecs == null || audioCodecs.Count() == 0) { _audioCodecs = new List <SDPMediaFormatsEnum> { SDPMediaFormatsEnum.PCMU, SDPMediaFormatsEnum.PCMA, SDPMediaFormatsEnum.G722 }; } else if (audioCodecs.Any(x => !(x == SDPMediaFormatsEnum.PCMU || x == SDPMediaFormatsEnum.PCMA || x == SDPMediaFormatsEnum.G722))) { throw new ApplicationException("Only PCMA, PCMU and G722 audio codecs are supported."); } _audioOpts = audioOptions; _audioCodecs = audioCodecs ?? _audioCodecs; var audioCapabilities = new List <SDPMediaFormat>(); foreach (var codec in _audioCodecs) { audioCapabilities.Add(new SDPMediaFormat(codec)); } // RTP event support. SDPMediaFormat rtpEventFormat = new SDPMediaFormat(DTMF_EVENT_PAYLOAD_ID); rtpEventFormat.SetFormatAttribute($"{SDP.TELEPHONE_EVENT_ATTRIBUTE}/{RTP_TIMESTAMP_RATE}"); rtpEventFormat.SetFormatParameterAttribute("0-16"); audioCapabilities.Add(rtpEventFormat); // Add a local audio track to the RTP session. MediaStreamTrack audioTrack = new MediaStreamTrack(SDPMediaTypesEnum.audio, false, audioCapabilities); base.addTrack(audioTrack); }
/// <summary> /// Sets the source for the session. Overrides any existing source. /// </summary> /// <param name="sourceOptions">The new audio source.</param> public void SetSource(AudioSourceOptions sourceOptions) { // If required start the audio source. if (sourceOptions != null) { _audioStreamTimer?.Dispose(); _audioStreamReader?.Close(); StopSendFromAudioStream(); if (sourceOptions.AudioSource == AudioSourcesEnum.None) { // Do nothing, all other sources have already been stopped. } else if (sourceOptions.AudioSource == AudioSourcesEnum.Silence) { _audioStreamTimer = new Timer(SendSilenceSample, null, 0, AUDIO_SAMPLE_PERIOD_MILLISECONDS); } else if (sourceOptions.AudioSource == AudioSourcesEnum.PinkNoise || sourceOptions.AudioSource == AudioSourcesEnum.WhiteNoise || sourceOptions.AudioSource == AudioSourcesEnum.SineWave) { int sourceSampleRate = _sourceAudioSampleRate == AudioSamplingRatesEnum.Rate8KHz ? 8000 : 16000; _signalGenerator = new SignalGenerator(sourceSampleRate, 1); switch (sourceOptions.AudioSource) { case AudioSourcesEnum.PinkNoise: _signalGenerator.Type = SignalGeneratorType.Pink; break; case AudioSourcesEnum.SineWave: _signalGenerator.Type = SignalGeneratorType.Sin; break; case AudioSourcesEnum.WhiteNoise: default: _signalGenerator.Type = SignalGeneratorType.White; break; } _audioStreamTimer = new Timer(SendSignalGeneratorSample, null, 0, AUDIO_SAMPLE_PERIOD_MILLISECONDS); } else if (sourceOptions.AudioSource == AudioSourcesEnum.Music) { if (string.IsNullOrWhiteSpace(sourceOptions.MusicFile) || !File.Exists(sourceOptions.MusicFile)) { if (!string.IsNullOrWhiteSpace(sourceOptions.MusicFile)) { Log.LogWarning($"Music file not set or not found, using default music resource."); } EmbeddedFileProvider efp = new EmbeddedFileProvider(Assembly.GetExecutingAssembly()); var audioStreamFileInfo = efp.GetFileInfo(MUSIC_RESOURCE_PATH); _audioStreamReader = new StreamReader(audioStreamFileInfo.CreateReadStream()); } else { _audioStreamReader = new StreamReader(sourceOptions.MusicFile); } _audioStreamTimer = new Timer(SendMusicSample, null, 0, AUDIO_SAMPLE_PERIOD_MILLISECONDS); } _audioOpts = sourceOptions; } }