internal ShoutcastStream(Uri serverUrl, ShoutcastStreamFactoryConnectionSettings settings, SocketWrapper socketWrapper)
        {
            this.socket         = socketWrapper;
            this.serverUrl      = serverUrl;
            this.serverSettings = settings;

            StationInfo       = new ServerStationInfo();
            AudioInfo         = new ServerAudioInfo();
            cancelTokenSource = new CancellationTokenSource();
            //MediaStreamSource = new MediaStreamSource(null);
            streamProcessor = new ShoutcastStreamProcessor(this, socket);
        }
        public ShoutcastMediaSourceStream(Uri url, ShoutcastServerType stationServerType = ShoutcastServerType.Shoutcast, string relativePath = ";", bool getMetadata = true)
        {
            StationInfo = new ShoutcastStationInfo();

            streamUrl = url;

            serverType = stationServerType;

            socket = new StreamSocket();

            AudioInfo = new ServerAudioInfo();

            ShouldGetMetadata = getMetadata;

            this.relativePath = relativePath;
        }
        private async Task <AudioEncodingProperties> ParseEncodingFromMediaAsync()
        {
            //grab the first frame and strip it for information

            AudioEncodingProperties obtainedProperties = null;
            IBuffer buffer = null;

            if (AudioInfo.AudioFormat == StreamAudioFormat.AAC)
            {
                //obtainedProperties = AudioEncodingProperties.CreateAac(0, 2, 0);
                throw new Exception("Not supported.");
            }

            var provider = AudioProviderFactory.GetAudioProvider(AudioInfo.AudioFormat);

            ServerAudioInfo firstFrame = await provider.GrabFrameInfoAsync(streamProcessor, AudioInfo).ConfigureAwait(false);

            //loop until we receive a few "frames" with identical information.
            while (true)
            {
                cancelTokenSource.Token.ThrowIfCancellationRequested();

                ServerAudioInfo secondFrame = await provider.GrabFrameInfoAsync(streamProcessor, AudioInfo).ConfigureAwait(false);

                if (firstFrame.BitRate == secondFrame.BitRate &&
                    firstFrame.SampleRate == secondFrame.SampleRate)
                {
                    //both frames are identical, use one of them and escape the loop.
                    AudioInfo = firstFrame;
                    break;
                }
                else
                {
                    //frames aren't identical, get rid of the first one using the second frame and loop back.
                    firstFrame = secondFrame;
                    continue;
                }
            }

            cancelTokenSource.Token.ThrowIfCancellationRequested();

            if (AudioInfo.AudioFormat == StreamAudioFormat.MP3)
            {
                //skip the entire first frame/sample to get back on track
                await socket.LoadAsync(MP3Parser.mp3_sampleSize - MP3Parser.HeaderLength);

                buffer = await socket.ReadBufferAsync(MP3Parser.mp3_sampleSize - MP3Parser.HeaderLength);

                //streamProcessor.byteOffset += MP3Parser.mp3_sampleSize - MP3Parser.HeaderLength;


                obtainedProperties = AudioEncodingProperties.CreateMp3((uint)AudioInfo.SampleRate, (uint)AudioInfo.ChannelCount, AudioInfo.BitRate);
            }
            else if (AudioInfo.AudioFormat == StreamAudioFormat.AAC_ADTS)
            {
                //skip the entire first frame/sample to get back on track
                await socket.LoadAsync(AAC_ADTSParser.aac_adts_sampleSize - AAC_ADTSParser.HeaderLength);

                buffer = await socket.ReadBufferAsync(AAC_ADTSParser.aac_adts_sampleSize - AAC_ADTSParser.HeaderLength);

                //streamProcessor.byteOffset += AAC_ADTSParser.aac_adts_sampleSize - AAC_ADTSParser.HeaderLength;

                obtainedProperties = AudioEncodingProperties.CreateAacAdts((uint)AudioInfo.SampleRate, (uint)AudioInfo.ChannelCount, AudioInfo.BitRate);
            }
            else
            {
                throw new Exception("Unsupported format.");
            }

            if (serverSettings.RequestSongMetdata)
            {
                streamProcessor.metadataPos += buffer.Length; //very important or it will throw everything off!
            }
            return(obtainedProperties);
        }