/// <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; } }
/// <summary> /// Initialises the audio source as required. /// </summary> public override Task Start() { lock (this) { if (!IsStarted) { if (AudioLocalTrack == null || AudioLocalTrack.Capabilities == null || AudioLocalTrack.Capabilities.Count == 0) { throw new ApplicationException( "Cannot start audio session without a local audio track being available."); } else if (AudioRemoteTrack == null || AudioRemoteTrack.Capabilities == null || AudioRemoteTrack.Capabilities.Count == 0) { throw new ApplicationException( "Cannot start audio session without a remote audio track being available."); } _sendingFormat = base.GetSendingFormat(SDPMediaTypesEnum.audio); _sendingAudioSampleRate = SDPMediaFormatInfo.GetClockRate(_sendingFormat.FormatCodec); _sendingAudioRtpRate = SDPMediaFormatInfo.GetRtpClockRate(_sendingFormat.FormatCodec); Log.LogDebug($"RTP audio session selected sending codec {_sendingFormat.FormatCodec}."); if (_sendingFormat.FormatCodec == SDPMediaFormatsEnum.G722) { _g722Codec = new G722Codec(); _g722CodecState = new G722CodecState(G722_BIT_RATE, G722Flags.None); _g722Decoder = new G722Codec(); _g722DecoderState = new G722CodecState(G722_BIT_RATE, G722Flags.None); } // If required start the audio source. if (_audioOpts != null && _audioOpts.AudioSource != AudioSourcesEnum.None) { if (_audioOpts.AudioSource == AudioSourcesEnum.Silence) { _audioStreamTimer = new Timer(SendSilenceSample, null, 0, AUDIO_SAMPLE_PERIOD_MILLISECONDS); } else if (_audioOpts.AudioSource == AudioSourcesEnum.PinkNoise || _audioOpts.AudioSource == AudioSourcesEnum.WhiteNoise || _audioOpts.AudioSource == AudioSourcesEnum.SineWave) { _signalGenerator = new SignalGenerator(_sendingAudioSampleRate, 1); switch (_audioOpts.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 (_audioOpts.AudioSource == AudioSourcesEnum.Music) { if (_audioOpts.SourceFiles == null || !_audioOpts.SourceFiles.ContainsKey(_sendingFormat.FormatCodec)) { Log.LogWarning($"Source file not set for codec {_sendingFormat.FormatCodec}."); } else { string sourceFile = _audioOpts.SourceFiles[_sendingFormat.FormatCodec]; if (String.IsNullOrEmpty(sourceFile) || !File.Exists(sourceFile)) { Log.LogWarning( "Could not start audio music source as the source file does not exist."); } else { _audioStreamReader = new StreamReader(sourceFile); _audioStreamTimer = new Timer(SendMusicSample, null, 0, AUDIO_SAMPLE_PERIOD_MILLISECONDS); } } } } base.OnRtpPacketReceived += RtpPacketReceived; } return(base.Start()); } }
/// <summary> /// Initialises the audio source as required. /// </summary> public override Task Start() { lock (this) { if (!_isStarted) { _isStarted = true; if (AudioLocalTrack == null || AudioLocalTrack.Capabilties == null || AudioLocalTrack.Capabilties.Count == 0) { throw new ApplicationException("Cannot start audio session without a local audio track being available."); } else if (AudioRemoteTrack == null || AudioRemoteTrack.Capabilties == null || AudioRemoteTrack.Capabilties.Count == 0) { throw new ApplicationException("Cannot start audio session without a remote audio track being available."); } // Choose which codec to use. //_sendingFormat = AudioLocalTrack.Capabilties // .Where(x => x.FormatID != DTMF_EVENT_PAYLOAD_ID.ToString() && int.TryParse(x.FormatID, out _)) // .OrderBy(x => int.Parse(x.FormatID)).First(); _sendingFormat = base.GetSendingFormat(SDPMediaTypesEnum.audio); Log.LogDebug($"RTP audio session selected sending codec {_sendingFormat.FormatCodec}."); if (_sendingFormat.FormatCodec == SDPMediaFormatsEnum.G722) { _g722Codec = new G722Codec(); _g722CodecState = new G722CodecState(64000, G722Flags.None); } // If required start the audio source. if (_audioOpts != null && _audioOpts.AudioSource != DummyAudioSourcesEnum.None) { if (_audioOpts.AudioSource == DummyAudioSourcesEnum.Silence) { _audioStreamTimer = new Timer(SendSilenceSample, null, 0, AUDIO_SAMPLE_PERIOD_MILLISECONDS); } else if (_audioOpts.AudioSource == DummyAudioSourcesEnum.PinkNoise || _audioOpts.AudioSource == DummyAudioSourcesEnum.WhiteNoise || _audioOpts.AudioSource == DummyAudioSourcesEnum.SineWave) { _signalGenerator = new SignalGenerator(SAMPLE_RATE, 1); switch (_audioOpts.AudioSource) { case DummyAudioSourcesEnum.PinkNoise: _signalGenerator.Type = SignalGeneratorType.Pink; break; case DummyAudioSourcesEnum.SineWave: _signalGenerator.Type = SignalGeneratorType.Sin; break; case DummyAudioSourcesEnum.WhiteNoise: default: _signalGenerator.Type = SignalGeneratorType.White; break; } _audioStreamTimer = new Timer(SendNoiseSample, null, 0, AUDIO_SAMPLE_PERIOD_MILLISECONDS); } else if (_audioOpts.AudioSource == DummyAudioSourcesEnum.Music) { if (_audioOpts.SourceFiles == null || !_audioOpts.SourceFiles.ContainsKey(_sendingFormat.FormatCodec)) { Log.LogWarning($"Source file not set for codec {_sendingFormat.FormatCodec}."); } else { string sourceFile = _audioOpts.SourceFiles[_sendingFormat.FormatCodec]; if (String.IsNullOrEmpty(sourceFile) || !File.Exists(sourceFile)) { Log.LogWarning("Could not start audio music source as the source file does not exist."); } else { _audioStreamReader = new StreamReader(sourceFile); _audioStreamTimer = new Timer(SendMusicSample, null, 0, AUDIO_SAMPLE_PERIOD_MILLISECONDS); } } } } } return(base.Start()); } }