private void InternalStopStream() { if (playbackState != StreamingPlaybackState.Stopped) { if (!fullyDownloaded) { try { webRequest.Abort(); } catch { } } playbackState = StreamingPlaybackState.Stopped; if (waveOut != null) { try { waveOut.Stop(); waveOut.Dispose(); } finally { waveOut = null; } } if (processorWaveProvider != null) { try { processorWaveProvider.Dispose(); } finally { processorWaveProvider = null; } } FirePropertyChangedAction(false); //SmartSleep(1500, "InternalStopStream"); } }
public WaveInChannelProcessor(string streamName, string waveInSourceName, int waveInChannel, Common.ProcessRadioSignalingItemDelegate sigDelegate, Action <bool> propertyChangedAction, float initialVolume, bool recordingEnabled, Common.SignalRecordingType recordingType, int recordingKickTime, Common.NoiseFloor noiseFloor, int customNoiseFloor, bool removeNoise, bool decodeMDC1200, bool decodeGEStar, bool decodeFleetSync, bool decodeP25, string waveOutDevName) { _streamShouldPlay = true; _streamName = streamName; _waveInSourceName = waveInSourceName; _waveInChannel = waveInChannel; _sigDelegate = sigDelegate; _propertyChangedAction = propertyChangedAction; sourceActive = false; _recordingEnabled = recordingEnabled; _recordingType = recordingType; _recordingKickTime = recordingKickTime; _noiseFloor = noiseFloor; _customNoiseFloor = customNoiseFloor; _removeNoise = removeNoise; _decodeMDC1200 = decodeMDC1200; _decodeGEStar = decodeGEStar; _decodeFleetSync = decodeFleetSync; _decodeP25 = decodeP25; _waveOutDevName = waveOutDevName; bufferedWaveProvider = new BufferedWaveProvider(AudioProcessingGlobals.GetWaveFormatForChannels(1)); bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(3); processorWaveProvider = new ProcessorWaveProvider(streamName, bufferedWaveProvider, ProcessRadioSignalingItem, propertyChangedAction, recordingEnabled, recordingType, recordingKickTime, noiseFloor, customNoiseFloor, removeNoise, decodeMDC1200, decodeGEStar, decodeFleetSync, decodeP25); volumeProvider = new VolumeWaveProvider16(processorWaveProvider); volumeProvider.Volume = initialVolume; FirePropertyChangedAction(true); WaveInManager.Instance.SetupForProcessor(this); sourceActive = true; waveOut = CreateWaveOut(); waveOut.Init(volumeProvider); waveOut.Play(); }
private void WaveStreamProc() { string icyStreamName = string.Empty; while (_streamShouldPlay) { ResetFeedInactivityTimeout(); SetFeedActive(true, false); System.Threading.Thread.Sleep(new System.Random().Next(100, 1500)); try { Common.ConsoleHelper.ColorWriteLine(ConsoleColor.DarkCyan, "Initiating connection to {0}", _streamName); playbackState = StreamingPlaybackState.Buffering; FirePropertyChangedAction(false); bufferedWaveProvider = null; fullyDownloaded = false; webRequest = (HttpWebRequest)WebRequest.Create(Common.UrlHelper.GetCorrectedStreamURL(_streamURL)); //webRequest.Timeout = webRequest.Timeout * 20; /*test - start*/ webRequest.Headers.Clear(); webRequest.Headers.Add("Icy-MetaData", "1"); SetupWebRequestSettings(webRequest); /*test - end*/ HttpWebResponse resp; try { resp = SmartGetWebResponse(webRequest);// (HttpWebResponse)webRequest.GetResponse(); string newUrl = string.Empty; if (!ValidateResponse(resp, out newUrl, out icyStreamName)) { if (!string.IsNullOrWhiteSpace(newUrl)) { webRequest = (HttpWebRequest)WebRequest.Create(newUrl); //webRequest.Timeout = webRequest.Timeout * 20; /*test - start*/ webRequest.Headers.Clear(); webRequest.Headers.Add("Icy-MetaData", "1"); SetupWebRequestSettings(webRequest); /*test - end*/ resp = SmartGetWebResponse(webRequest);// (HttpWebResponse)webRequest.GetResponse(); PrintHttpWebResponseHeader(resp, "WaveStreamProc.Redirected", out icyStreamName); } } } catch (WebException e) { if (e.Status == WebExceptionStatus.ProtocolError) { Common.DebugHelper.WriteExceptionToLog("WaveStreamProcessor.WebRequest.GetResponse", e, false, _streamURL); try { System.Net.HttpWebResponse httpRes = e.Response as System.Net.HttpWebResponse; if (httpRes != null && httpRes.StatusCode == HttpStatusCode.NotFound) { SetFeedActive(false, false); } } catch { } } if (e.Status != WebExceptionStatus.RequestCanceled) { //ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "WaveStreamProcessor Error: {0}", e.Message); //Common.DebugHelper.WriteExceptionToLog("WaveStreamProcessor Error", e, false); InternalStopStream(); ClearStreamTitle(); SmartSleep(30 * 1000, string.Format("WebRequest.GetResponse: {0}", e.Status)); } continue; } var buffer = new byte[16348 * 4]; IMp3FrameDecompressor decompressor = null; try { int metaInt = -1; bool bIsIcy = IsIcecastStream(resp, out metaInt); if (!bIsIcy) { metaInt = -1; } using (var responseStream = resp.GetResponseStream()) { Stream readFullyStream = null; if (!string.IsNullOrWhiteSpace(icyStreamName)) { UpdateStreamTitle(icyStreamName, false); FirePropertyChangedAction(true); } if (bIsIcy && metaInt > 0) { readFullyStream = new ReadFullyStream(new IcyStream(responseStream, metaInt, ProcessIcyMetadata)); } else { readFullyStream = new ReadFullyStream(responseStream); } do { if (IsBufferNearlyFull) { SmartSleep(500); //ConsoleHelper.ColorWriteLine(ConsoleColor.DarkCyan, "Buffer is getting full, taking a break, {0}sec...", bufferedWaveProvider.BufferedDuration.TotalSeconds); //ConsoleHelper.ColorWriteLine(ConsoleColor.DarkCyan, " {0}", _streamURL); } else { Mp3Frame frame; try { frame = Mp3Frame.LoadFromStream(readFullyStream); } catch (EndOfStreamException eose) { fullyDownloaded = true; SmartSleep(1500, "EndOfStreamException"); if (playbackState != StreamingPlaybackState.Stopped) { Common.DebugHelper.WriteExceptionToLog("Mp3Frame.LoadFromStream", eose, false, _streamURL); } continue; } catch (WebException wex) { InternalStopStream(); SmartSleep(3 * 60 * 1000, "WebException"); if (playbackState != StreamingPlaybackState.Stopped) { Common.DebugHelper.WriteExceptionToLog("Mp3Frame.LoadFromStream", wex, false, _streamURL); } continue; } if (frame != null) { KickFeedInactivityTimeout(); SetFeedActive(true, true); if (decompressor == null) { try { Common.ConsoleHelper.ColorWriteLine(ConsoleColor.DarkGray, "Creating MP3 Decompressor for {0}...", _streamURL); decompressor = CreateFrameDecompressor(frame); bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat); bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(20); processorWaveProvider = new ProcessorWaveProvider(_streamName, bufferedWaveProvider, _sigDelegate, _propertyChangedAction, _recordingEnabled, _recordingType, _recordingKickTime, _noiseFloor, _customNoiseFloor, _removeNoise, _decodeMDC1200, _decodeGEStar, _decodeFleetSync, _decodeP25); //volumeProvider = new VolumeWaveProvider16(bufferedWaveProvider); volumeProvider = new VolumeWaveProvider16(processorWaveProvider); volumeProvider.Volume = _initialVolume; waveOut = CreateWaveOut(); waveOut.Init(volumeProvider); FirePropertyChangedAction(false); } catch (Exception ex) { Common.ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "Excpetion in stream {0}: {1}", _streamURL, ex.Message); Common.DebugHelper.WriteExceptionToLog("WaveStreamProcessor", ex, false, string.Format("Exception in stream {0}", _streamURL)); InternalStopStream(); SmartSleep(3 * 60 * 1000, "Exception:CreateFrameDecompressor"); continue; } } int decompressed = decompressor.DecompressFrame(frame, buffer, 0); bufferedWaveProvider.AddSamples(buffer, 0, decompressed); var bufferedSeconds = bufferedWaveProvider.BufferedDuration.TotalSeconds; if (bufferedSeconds < 0.5 && playbackState == StreamingPlaybackState.Playing && !fullyDownloaded) { playbackState = StreamingPlaybackState.Buffering; FirePropertyChangedAction(false); waveOut.Pause(); Common.ConsoleHelper.ColorWriteLine(ConsoleColor.DarkRed, "Stream Paused... Buffering... {0}", _streamURL); } else if (bufferedSeconds > 4 && playbackState == StreamingPlaybackState.Buffering) { waveOut.Play(); playbackState = StreamingPlaybackState.Playing; FirePropertyChangedAction(false); Common.ConsoleHelper.ColorWriteLine(ConsoleColor.DarkGreen, "Stream Playing... {0}", _streamURL); } } else { if (IsFeedInactive()) { try { Common.ConsoleHelper.ColorWriteLine(ConsoleColor.DarkYellow, "Restarting {0} due to inactivity...", _streamName); InternalStopStream(); } finally { LongSmartSleep(FEED_NOT_ACTIVE_SLEEP_SECS, "WaveStreamProc.IsFeedInactive"); } } } } }while (playbackState != StreamingPlaybackState.Stopped); if (decompressor != null) { try { decompressor.Dispose(); } finally { decompressor = null; } } } } finally { if (decompressor != null) { try { decompressor.Dispose(); } finally { decompressor = null; } } } } catch (Exception ex) { #if DEBUG Common.DebugHelper.WriteExceptionToLog("WaveStreamProc", ex, false, "General Catch"); #endif try { InternalStopStream(); } finally { LongSmartSleep(15, "WaveStreamProc"); } } } }