Esempio n. 1
0
        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");
                    }
                }
            }
        }
Esempio n. 2
0
        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");
                    }
                }
            }
        }