3

Зашел в тупик с реализацией приложения для прослушивания онлайн радио. За основу взята библиотека NAudio. За небольшими правками реализация не отличается от данного примера, опубликованного вместе с библиотекой. Реализация парсера взята из этого примера.

Суть проблемы. Мне необходимо осуществить парсинг метаданных из потока для получения исполнителя и песни. В теории, при посыле запроса с заголовком:

_webRequest.Headers.Add("Icy-MetaData", "1");

нам должен прийти ответ с заголовком "icy-metaint" со значением (к примеру: 16000) позиция в потоке, содержащей метаданные исполнителя и песни. Парсинг (по идее) должен осуществляться в кастомной реализации Stream. Вот моя реализация:

public class RadioPlayerStream : Stream
    {
        private readonly Stream _sourceStream;
        private long _pos;
        private readonly byte[] _readAheadBuffer;
        private int _readAheadLength;
        private int _readAheadOffset;
        private int _icyMetaInt;

        public RadioPlayerStream(Stream sourceStream, int icyMetaInt)
        {
            _sourceStream = sourceStream;
            _icyMetaInt = icyMetaInt;
            _readAheadBuffer = new byte[4096];
        }
        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return false; }
        }

        public override bool CanWrite
        {
            get { return false; }
        }

        public override void Flush()
        {
            return;
        }

        public override long Length
        {
            get { return _pos; }
        }

        public override long Position
        {
            get
            {
                return _pos;
            }
            set
            {
                throw new InvalidOperationException();
            }
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new InvalidOperationException();
        }

        public override void SetLength(long value)
        {
            throw new InvalidOperationException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new InvalidOperationException();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int bytesRead = 0;
            while (bytesRead < count)
            {
                int readAheadAvailableBytes = _readAheadLength - _readAheadOffset;
                int bytesRequired = count - bytesRead;
                if (readAheadAvailableBytes > 0)
                {
                    int toCopy = Math.Min(readAheadAvailableBytes, bytesRequired);
                    Array.Copy(_readAheadBuffer, _readAheadOffset, buffer, offset + bytesRead, toCopy);
                    bytesRead += toCopy;
                    _readAheadOffset += toCopy;
                }
                else
                {
                    _readAheadOffset = 0;
                    _readAheadLength = _sourceStream.Read(_readAheadBuffer, 0, _readAheadBuffer.Length);
                    if (_readAheadLength == 0)
                    {
                        break;
                    }
                }
            }

            if (_icyMetaInt != 0)
            {
                StreamParser(_readAheadBuffer, _readAheadLength, _icyMetaInt);
            }

            _pos += bytesRead;
            return bytesRead;
        }


        // parser

        int count = 0;
        int metadataLength = 0;
        string metadataHeader = "";
        string oldMetadataHeader = null;

        private void StreamParser(byte[] buffer, int bufLen, int icyMetaInt)
        {
            for (var i = 0; i < bufLen; i++)
            {
                if (metadataLength != 0)
                {
                    metadataHeader += Convert.ToChar(buffer[i]);
                    metadataLength--;
                    if (metadataLength == 0) // all metadata informations were written to the 'metadataHeader' string
                    {
                        string fileName = "";

                        // if songtitle changes, create a new file
                        if (!metadataHeader.Equals(oldMetadataHeader))
                        {
                            var x = metadataHeader;

                            // extract songtitle from metadata header. Trim was needed, because some stations don't trim the songtitle
                            fileName = Regex.Match(metadataHeader, "(StreamTitle=')(.*)(';StreamUrl)").Groups[2].Value.Trim();

                            // save new header to 'oldMetadataHeader' string, to compare if there's a new song starting
                            oldMetadataHeader = metadataHeader;
                        }
                        metadataHeader = "";
                    }
                }
                else // write mp3 data to file or extract metadata headerlength
                {
                    if (count++ < icyMetaInt) // write bytes to filestream
                    {

                    }
                    else // get headerlength from lengthbyte and multiply by 16 to get correct headerlength
                    {
                        metadataLength = Convert.ToInt32(buffer[i]) * 16;
                        count = 0;
                    }
                }
            }
        }
    }

Вместо распарсенной строки мне приходит набор символов:

lØ|ÊB\u0003\u0014Qä¢7BW\u0093`Á\u0087+[§cÔ\a8àNyåÿû\u0090`¾\0\u0003@HÞc\u00033jTÅ\u009c\r\u001c£[\rÉ!w¬\u0014k\u0089P\u001ap4±\tlv×\u0014\u0012H\u0013e\u0011 \u0015\u0002\u00944\u000eÙâ¤\u0096\u0010\u0085Öá°Ñ ,A&]æ­Ìü\u008d¤û<Ëo¹\u001f\tIÖ\u0003upF\b\u0002\f,cC\b,%\u0001+\u008aw|,j\u0080Ê\u000e

Вот метод получения данных:

private void StreamMp3(object state)
        {
            _buffer = new byte[BufferSize]; // needs to be big enough to hold a decompressed frame
            bool riseMp3FrameInfoEvent = false;
            _fullyDownloaded = false;
            var url = (string)state;
            int metaInt = 0;

            #region metadata

            // create web request
            _webRequest = (HttpWebRequest)WebRequest.Create(url);

            if (MetaDataEnabled)
            {
                // clear old request header and build own header to receive ICY-metadata
                var serverPath = "/";
                _webRequest.Headers.Clear();
                _webRequest.Headers.Add("GET", serverPath + " HTTP/1.0");
                _webRequest.Headers.Add("Icy-MetaData", "1");

                // needed to receive metadata informations
                _webRequest.UserAgent = "WinampMPEG/5.09";
            }

            #endregion

            #region make HttpWebRequest

            HttpWebResponse resp;
            try
            {
                resp = (HttpWebResponse)_webRequest.GetResponse();
                if (resp.Headers.AllKeys.Contains("icy-metaint"))
                    metaInt = Convert.ToInt32(resp.GetResponseHeader("icy-metaint"));
                StreamInfoUpdated?.Invoke(StreamInfo = new StreamInfoModel(resp));
            }
            catch (WebException e)
            {
                if (e.Status != WebExceptionStatus.RequestCanceled)
                {
                    ShowError(e.Message);
                }
                return;
            }

            #endregion

            try
            {
                using (var responseStream = resp.GetResponseStream())
                {
                    using (_stream = new RadioPlayerStream(responseStream, metaInt))
                    {
                        do
                        {
                            if (IsBufferNearlyFull)
                            {
                                DebugEvent?.Invoke("Buffer getting full, taking a break");
                                Thread.Sleep(500);
                            }
                            else
                            {
                                Mp3Frame frame;

                                #region get mp3frame
                                try
                                {
                                    frame = Mp3Frame.LoadFromStream(_stream);
                                    Mp3FrameInfo = new Mp3FrameInfoModel(frame);
                                    if (!riseMp3FrameInfoEvent)
                                    {
                                        Mp3FrameInfoUpdated?.Invoke(Mp3FrameInfo);
                                        riseMp3FrameInfoEvent = true;
                                    }
                                }
                                catch (EndOfStreamException) // reached the end of the MP3 file / stream
                                {
                                    _fullyDownloaded = true;
                                    EndOfStream.Invoke(this, EventArgs.Empty);
                                    break;
                                }
                                catch (WebException) // probably we have aborted download from the GUI thread
                                {
                                    ShowError("probably we have aborted download from the GUI thread");
                                    break;
                                }
                                #endregion

                                #region create decompressor
                                if (_decompressor == null)
                                {
                                    // don't think these details matter too much - just help ACM select the right codec
                                    // however, the buffered provider doesn't know what sample rate it is working at
                                    // until we have a frame
                                    _decompressor = CreateFrameDecompressor(frame);
                                    _bufferedWaveProvider = new BufferedWaveProvider(_decompressor.OutputFormat);
                                    _bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(WaveProviderBufferDuration); // allow us to get well ahead of ourselves             
                                    WaveInfoUpdated?.Invoke(WaveInfo = new WaveInfoModel(_bufferedWaveProvider));
                                }
                                #endregion

                                // decompress
                                int decompressed = _decompressor.DecompressFrame(frame, _buffer, 0);
                                _bufferedWaveProvider.AddSamples(_buffer, 0, decompressed);
                                //DebugEvent?.Invoke($"Decompressed a frame {decompressed}");
                            }
                        }
                        while (_playbackState != StreamingPlaybackState.Stopped);
                    }

                    // was doing this in a finally block, but for some reason
                    // we are hanging on response stream .Dispose so never get there
                    _decompressor?.Dispose();
                    DebugEvent?.Invoke("Exiting...");
                }
            }
            finally
            {
                _decompressor?.Dispose();
            }
        }

Вот класс переопределенного потока:

public class RadioPlayerStream : Stream
    {
        private readonly Stream _sourceStream;
        private long _pos;
        private readonly byte[] _readAheadBuffer;
        private int _readAheadLength;
        private int _readAheadOffset;
        private int _icyMetaInt;

        public RadioPlayerStream(Stream sourceStream, int icyMetaInt)
        {
            _sourceStream = sourceStream;
            _icyMetaInt = icyMetaInt;
            _readAheadBuffer = new byte[4096];
        }
        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return false; }
        }

        public override bool CanWrite
        {
            get { return false; }
        }

        public override void Flush()
        {
            return;
        }

        public override long Length
        {
            get { return _pos; }
        }

        public override long Position
        {
            get
            {
                return _pos;
            }
            set
            {
                throw new InvalidOperationException();
            }
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new InvalidOperationException();
        }

        public override void SetLength(long value)
        {
            throw new InvalidOperationException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new InvalidOperationException();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int bytesRead = 0;
            while (bytesRead < count)
            {
                int readAheadAvailableBytes = _readAheadLength - _readAheadOffset;
                int bytesRequired = count - bytesRead;
                if (readAheadAvailableBytes > 0)
                {
                    int toCopy = Math.Min(readAheadAvailableBytes, bytesRequired);
                    Array.Copy(_readAheadBuffer, _readAheadOffset, buffer, offset + bytesRead, toCopy);
                    bytesRead += toCopy;
                    _readAheadOffset += toCopy;
                }
                else
                {
                    _readAheadOffset = 0;
                    _readAheadLength = _sourceStream.Read(_readAheadBuffer, 0, _readAheadBuffer.Length);
                    if (_readAheadLength == 0)
                    {
                        break;
                    }
                }
            }

            if (_icyMetaInt != 0)
            {
                StreamParser(_readAheadBuffer, _readAheadLength, _icyMetaInt);
            }

            _pos += bytesRead;
            return bytesRead;
        }


        // parser

        int count = 0;
        int metadataLength = 0;
        string metadataHeader = "";
        public string oldMetadataHeader = "";

        private void StreamParser(byte[] buffer, int bufLen, int icyMetaInt)
        {
            for (var i = 0; i < bufLen; i++)
            {
                if (metadataLength != 0)
                {
                    metadataHeader += Convert.ToChar(buffer[i]);
                    metadataLength--;
                    if (metadataLength == 0) // all metadata informations were written to the 'metadataHeader' string
                    {
                        string fileName = "";

                        // if songtitle changes, create a new file
                        if (!metadataHeader.Equals(oldMetadataHeader))
                        {
                            var x = metadataHeader;

                            // extract songtitle from metadata header. Trim was needed, because some stations don't trim the songtitle
                            fileName = Regex.Match(metadataHeader, "(StreamTitle=')(.*)(';StreamUrl)").Groups[2].Value.Trim();

                            // save new header to 'oldMetadataHeader' string, to compare if there's a new song starting
                            oldMetadataHeader = metadataHeader;
                        }
                        metadataHeader = "";
                    }
                }
                else // write mp3 data to file or extract metadata headerlength
                {
                    if (count++ < icyMetaInt) // write bytes to filestream
                    {

                    }
                    else // get headerlength from lengthbyte and multiply by 16 to get correct headerlength
                    {
                        metadataLength = Convert.ToInt32(buffer[i]) * 16;
                        count = 0;
                    }
                }
            }
        }
    }

Загвоздка состоит в том, как имея на руках значение MetaInt (позиция метаданных в потоке) вытащить данные о текущей композиции в эфире.

А именно метод Read:

public override int Read(byte[] buffer, int offset, int count)
        {
            int bytesRead = 0;
            while (bytesRead < count)
            {
                int readAheadAvailableBytes = _readAheadLength - _readAheadOffset;
                int bytesRequired = count - bytesRead;
                if (readAheadAvailableBytes > 0)
                {
                    int toCopy = Math.Min(readAheadAvailableBytes, bytesRequired);
                    Array.Copy(_readAheadBuffer, _readAheadOffset, buffer, offset + bytesRead, toCopy);
                    bytesRead += toCopy;
                    _readAheadOffset += toCopy;
                }
                else
                {
                    _readAheadOffset = 0;
                    _readAheadLength = _sourceStream.Read(_readAheadBuffer, 0, _readAheadBuffer.Length);
                    if (_readAheadLength == 0)
                    {
                        break;
                    }
                }
            }

            if (_icyMetaInt != 0)
            {
                StreamParser(_readAheadBuffer, _readAheadLength, _icyMetaInt);
            }

            _pos += bytesRead;
            return bytesRead;
        }

И метод для парсинга:

private void StreamParser(byte[] buffer, int bufLen, int icyMetaInt)
        {
            for (var i = 0; i < bufLen; i++)
            {
                if (metadataLength != 0)
                {
                    metadataHeader += Convert.ToChar(buffer[i]);
                    metadataLength--;
                    if (metadataLength == 0) // all metadata informations were written to the 'metadataHeader' string
                    {
                        string fileName = "";

                        // if songtitle changes, create a new file
                        if (!metadataHeader.Equals(oldMetadataHeader))
                        {
                            var x = metadataHeader;

                            // extract songtitle from metadata header. Trim was needed, because some stations don't trim the songtitle
                            fileName = Regex.Match(metadataHeader, "(StreamTitle=')(.*)(';StreamUrl)").Groups[2].Value.Trim();

                            // save new header to 'oldMetadataHeader' string, to compare if there's a new song starting
                            oldMetadataHeader = metadataHeader;
                        }
                        metadataHeader = "";
                    }
                }
                else // write mp3 data to file or extract metadata headerlength
                {
                    if (count++ < icyMetaInt) // write bytes to filestream
                    {

                    }
                    else // get headerlength from lengthbyte and multiply by 16 to get correct headerlength
                    {
                        metadataLength = Convert.ToInt32(buffer[i]) * 16;
                        count = 0;
                    }
                }
            }
        }
2
  • 1
    какие-то странные у вас методы, откуда они переопределены? где точка входа в программу? Как вы вообще что-то получаете от этого кода?
    – Garrus_En
    Commented 2 мар. 2017 в 3:38
  • @АнтонЛихацкий вопросы, которые возникают у меня каждый день на работе Commented 2 мар. 2017 в 3:44

2 ответа 2

0

Написано для UWP, есть небольшие недочёты в плане разрыва соединений, но этого вам будет исчерпывающе.

using Portable.Text;
using ShoutcastMSS.Enums;
using ShoutcastMSS.Exceptions;
using ShoutcastStream.EventArguments;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Windows.Media.Core;
using Windows.Media.MediaProperties;
using Windows.Storage;
using Windows.Storage.Streams;

namespace ShoutcastStream
{
    public class ShoutcastMSSManager
    {
        #region Public Fields
        public event EventHandler<SMSSMetadataChangedEventArgs> MetadataChanged;
        public event EventHandler<SMSSConnectionFailedEventArgs> ConnectionFailed;
        public event EventHandler<SMSSRecordStateChangedEventArgs> RecordStateChanged;
        public MediaStreamSource MediaStreamSource { get; private set; }
        public ShoutcastStationInfo StationInfo = new ShoutcastStationInfo();
        public Uri StreamUrl { get; }
        #endregion

        #region Private Fields
        private IRandomAccessStream _recordStream = null;
        private bool _isStreamRecords = false;
        private readonly uint _sampleSize = 1152;
        private uint _metadataInterval = 16000;
        private uint _metadataPosition = 0;
        private TimeSpan _timeOffSet = new TimeSpan();
        private readonly TimeSpan _sampleDuration = TimeSpan.FromMilliseconds(70);
        private DataReader _shoutcastDataReader = null;
        private readonly int _connectionTimeout;
        #endregion

        public ShoutcastMSSManager(string url, int connectionTimeout = 4000)
        {
            StreamUrl = new Uri(url);
            _connectionTimeout = connectionTimeout;
        }
        public async void StartRecord()
        {
            try
            {
                string fileName = MediaStreamSource.MusicProperties.AlbumArtist + "-" + MediaStreamSource.MusicProperties.Title + ".mp3";
                StorageFile recordFile = await KnownFolders.MusicLibrary.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);
                _recordStream = await recordFile.OpenAsync(FileAccessMode.ReadWrite);
                _isStreamRecords = true;
                RecordStateChanged?.Invoke(this, new SMSSRecordStateChangedEventArgs(RecordState.Records, fileName));
            }
            catch { }
        }
        public void StopRecord()
        {
            try
            {
                if (_recordStream != null)
                {
                    _recordStream.Dispose();
                    _recordStream = null;
                }
                _isStreamRecords = false;
                RecordStateChanged?.Invoke(this, new SMSSRecordStateChangedEventArgs(RecordState.Stopped, null));
            }
            catch { }
        }
        public void Disconnect()
        {
            try
            {
                if (_isStreamRecords)
                {
                    StopRecord();
                }
                MediaStreamSource.SampleRequested -= MediaStreamSource_SampleRequested;
                MediaStreamSource.Starting -= MediaStreamSource_Starting;
                MediaStreamSource.Closed -= MediaStreamSource_Closed;
                MediaStreamSource.Paused -= MediaStreamSource_Paused;
            }
            catch { }

            if (_shoutcastDataReader != null)
            {
                _shoutcastDataReader.Dispose();
            }

        }
        public async Task ConnectAsync()
        {
            HttpClient shoutcastHttpClient = new HttpClient(new HttpClientHandler { Proxy = null, UseProxy = false });
            shoutcastHttpClient.DefaultRequestHeaders.Add("icy-metadata", "1");
            MediaEncodingProfile audioProfile = null;

            try
            {
                HttpResponseMessage responseMessage = await shoutcastHttpClient.GetAsync(StreamUrl, HttpCompletionOption.ResponseHeadersRead);
                ParseResponse(responseMessage.Headers);
                _shoutcastDataReader = new DataReader((await responseMessage.Content.ReadAsStreamAsync()).AsInputStream())
                {
                    UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8,
                    InputStreamOptions = InputStreamOptions.None

                };

                if (_metadataInterval < 16000)
                {
                    using (InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream())
                    {
                        await _shoutcastDataReader.LoadAsync(_metadataInterval);
                        _metadataPosition += _metadataInterval;
                        IBuffer tempBufferOne = _shoutcastDataReader.ReadBuffer(_metadataInterval);
                        await memoryStream.WriteAsync(tempBufferOne);
                        await HandleMetadataAsync();
                        await _shoutcastDataReader.LoadAsync(_metadataInterval);
                        _metadataPosition += _metadataInterval;
                        IBuffer tempBufferTwo = _shoutcastDataReader.ReadBuffer(_metadataInterval);
                        await memoryStream.WriteAsync(tempBufferTwo);
                        audioProfile = await MediaEncodingProfile.CreateFromStreamAsync(memoryStream);
                        await HandleMetadataAsync();
                    }
                }
                else
                {
                    await _shoutcastDataReader.LoadAsync(16000);
                    using (IRandomAccessStream stream = _shoutcastDataReader.ReadBuffer(16000).AsStream().AsRandomAccessStream())
                    {
                        audioProfile = await MediaEncodingProfile.CreateFromStreamAsync(stream);
                    }
                    _metadataPosition += 16000;
                }
                AudioEncodingProperties audioProperties = AudioEncodingProperties.CreateMp3(audioProfile.Audio.SampleRate, audioProfile.Audio.ChannelCount, audioProfile.Audio.Bitrate / 1000);
                AudioStreamDescriptor streamDescriptor = new AudioStreamDescriptor(audioProperties);
                MediaStreamSource = new MediaStreamSource(streamDescriptor)
                {
                    BufferTime = TimeSpan.FromSeconds(3),
                };

                MediaStreamSource.SampleRequested += MediaStreamSource_SampleRequested;
                MediaStreamSource.CanSeek = false;
                MediaStreamSource.Starting += MediaStreamSource_Starting;
                MediaStreamSource.Closed += MediaStreamSource_Closed;
                MediaStreamSource.Paused += MediaStreamSource_Paused;
            }
            catch (Exception ex)
            {
                throw new ConnectionException(ex.Message, ExceptionCode.ConnectionError);
            }
        }
        private void MediaStreamSource_Paused(MediaStreamSource sender, object args)
        {

        }
        private void MediaStreamSource_Closed(MediaStreamSource sender, MediaStreamSourceClosedEventArgs args)
        {

        }
        private void MediaStreamSource_Starting(MediaStreamSource sender, MediaStreamSourceStartingEventArgs args)
        {

        }
        private void ParseResponse(HttpResponseHeaders headers)
        {
            foreach (var header in headers)
            {
                switch (header.Key)
                {
                    case "icy-name":
                        StationInfo.StationName = header.Value.FirstOrDefault();
                        break;
                    case "icy-genre":
                        StationInfo.StationGenre = header.Value.FirstOrDefault();
                        break;
                    case "icy-metaint":
                        _metadataInterval = Convert.ToUInt16(header.Value.FirstOrDefault());
                        break;
                }
            }
        }
        private async void MediaStreamSource_SampleRequested(MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args)
        {
            MediaStreamSourceSampleRequest sampleRequest = args.Request;
            MediaStreamSourceSampleRequestDeferral requestDeferral = sampleRequest.GetDeferral();
            MediaStreamSample sample = null;
            try
            {
                sampleRequest.ReportSampleProgress(25);
                if (_metadataInterval - _metadataPosition <= _sampleSize && _metadataInterval - _metadataPosition > 0)
                {
                    uint partial = _metadataInterval - _metadataPosition;
                    DataReaderLoadOperation loadOperation = _shoutcastDataReader.LoadAsync(partial);
                    await loadOperation.AsTask(new CancellationTokenSource(_connectionTimeout).Token);
                    //await _shoutcastDataReader.LoadAsync(partial);
                    _metadataPosition += _metadataInterval - _metadataPosition;
                    IBuffer buffer = _shoutcastDataReader.ReadBuffer(partial);
                    sample = await ParseSampleAsync(buffer);
                }
                else
                {
                    await HandleMetadataAsync();
                    sampleRequest.ReportSampleProgress(50);
                    DataReaderLoadOperation loadOperation = _shoutcastDataReader.LoadAsync(_sampleSize);
                    await loadOperation.AsTask(new CancellationTokenSource(_connectionTimeout).Token);
                    //await _shoutcastDataReader.LoadAsync(_sampleSize);
                    _metadataPosition += _sampleSize;
                    IBuffer buffer = _shoutcastDataReader.ReadBuffer(_sampleSize);
                    sample = await ParseSampleAsync(buffer);
                }
                sampleRequest.Sample = sample;
                sampleRequest.ReportSampleProgress(100);
            }
            catch (Exception)
            {
                MediaStreamSource.NotifyError(MediaStreamSourceErrorStatus.ConnectionToServerLost);
                //throw new ConnectionException(ex.Message, ExceptionCode.ConnectionLost);
            }
            finally
            {
                requestDeferral.Complete();
            }
        }

        private async Task HandleMetadataAsync()
        {
            if (_metadataPosition == _metadataInterval)
            {
                _metadataPosition = 0;
                try
                {
                    await _shoutcastDataReader.LoadAsync(1);
                    uint lengthByte = _shoutcastDataReader.ReadByte();
                    if (lengthByte > 0)
                    {
                        uint metadataRange = lengthByte * 16;
                        await _shoutcastDataReader.LoadAsync(metadataRange);
                        byte[] metadataBytes = new byte[metadataRange];
                        _shoutcastDataReader.ReadBytes(metadataBytes);

                        string[] encodedMetadata = new string[]
                        {
                            Encoding.GetEncoding("windows-1251").GetString(metadataBytes, 0, metadataBytes.Length - 1),
                            Encoding.UTF8.GetString(metadataBytes, 0, metadataBytes.Length - 1)
                        };
                        ParseSongMetadata(encodedMetadata.FirstOrDefault(x => (x.Contains(";"))));
                    }
                }
                catch { }
            }
        }
        private void ParseSongMetadata(string metadata)
        {
            string track = "";
            string artist = "";
            string[] semiColonSplit = metadata.Split(';');
            KeyValuePair<string, string>[] headers = semiColonSplit.Where(line => line.Contains("=")).Select(line =>
            {
                string header = line.Substring(0, line.IndexOf("="));
                string value = line.Substring(line.IndexOf("=") + 1);
                var pair = new KeyValuePair<string, string>(header.ToUpper(), value.Trim('\'').Trim());
                return pair;
            }).ToArray();

            string songInfo = headers.First(x => x.Key == "STREAMTITLE").Value;

            if (songInfo.Split('-').Count() >= 2)
            {
                artist = songInfo.Split(new string[] { " - " }, StringSplitOptions.None)[0].Trim();
                track = songInfo.Split(new string[] { " - " }, StringSplitOptions.None)[1].Trim();
            }
            else
            {
                track = songInfo.Trim();
                artist = "Unknown Artist";
            }
            MetadataChanged?.Invoke(this, new SMSSMetadataChangedEventArgs(artist, track));
        }
        //============MP3Parser==============================================================================
        private async Task<MediaStreamSample> ParseSampleAsync(IBuffer bufferBytes)
        {
            MediaStreamSample sample = null;
            if (_isStreamRecords)
            {
                await _recordStream.WriteAsync(bufferBytes);
            }
            sample = MediaStreamSample.CreateFromBuffer(bufferBytes, _timeOffSet);
            sample.KeyFrame = true;
            _timeOffSet = _timeOffSet.Add(_sampleDuration);
            return sample;
        }
    }
}
0

Возможно будет кому-то полезно, так как сам столкнулся с этой же проблемой. Сам код, который модифицировался, взял отсюда

Создал новый stream reader, который вычитывает meta данные (metadataHeader будет содержать актуальные данные):

public class IcyMetaReaderStream : Stream
{
    protected readonly Stream _sourceStream;
    protected long _pos;
    private int _icyMetaInt;
    public IcyMetaReaderStream(Stream sourceStream, int icyMetaInt)
    {
        _sourceStream = sourceStream;
        _icyMetaInt = icyMetaInt;
    }
    public override bool CanRead => true;
    public override bool CanSeek => true;
    public override bool CanWrite => true;
    public override void Flush()
    {
        return;
    }
    public override long Length => _pos;
    public override long Position
    {
        get => _pos;
        set => throw new InvalidOperationException();
    }
    public override long Seek(long offset, SeekOrigin origin)
        => throw new InvalidOperationException();
    public override void SetLength(long value)
        => throw new InvalidOperationException();
    public override void Write(byte[] buffer, int offset, int count)
        => throw new InvalidOperationException();
    private int receivedBytes;
    public override int Read(byte[] buffer, int offset, int count)
    {
        if (receivedBytes == _icyMetaInt)
        {
            var metaLen = _sourceStream.ReadByte();
            if (metaLen > 0)
            {
                var metaInfo = new byte[metaLen * 16];
                var len = 0;
                while ((len += _sourceStream.Read(metaInfo, len, metaInfo.Length - len)) < metaInfo.Length);
                ParseMetaInfo(metaInfo);
            }
            receivedBytes  = 0;
        }
        var bytesLeft = _icyMetaInt - receivedBytes > count
            ? count
            : _icyMetaInt - receivedBytes;
        var result = _sourceStream.Read(buffer, offset, bytesLeft);
        _pos += result;
        receivedBytes += result;
        return result;
    }
    // parser
    private string metadataHeader;
    private string oldMetadataHeader;
    private void ParseMetaInfo(byte[] buffer)
    {
        metadataHeader = "";
        for (var i = 0; i < buffer.Length; i++)
        {
            metadataHeader += Convert.ToChar(buffer[i]);
        }
        if (!metadataHeader.Equals(oldMetadataHeader))
        {
            // extract songtitle from metadata header. Trim was needed, because some stations don't trim the songtitle
            //var fileName = Regex.Match(metadataHeader, "(StreamTitle=')(.*)(';StreamUrl)").Groups[2].Value.Trim();
            // save new header to 'oldMetadataHeader' string, to compare if there's a new song starting
            Console.WriteLine(metadataHeader);
            oldMetadataHeader = metadataHeader;
            metadataHeader = "";
        }
    }
}

после этого RadioPlayerStream, который добавляет буферизацию, уже наследует созданный класс:

public class RadioPlayerStream: IcyMetaReaderStream
{
    private readonly byte[] readAheadBuffer;
    private int readAheadLength;
    private int readAheadOffset;
    public ReadFullyStream(Stream sourceStream, int icyMetaInt) : base(sourceStream, icyMetaInt)
    {
        readAheadBuffer = new byte[4096];
    }
    public override int Read(byte[] buffer, int offset, int count)
    {
        int bytesRead = 0;
        while (bytesRead < count)
        {
            int readAheadAvailableBytes = readAheadLength - readAheadOffset;
            int bytesRequired = count - bytesRead;
            if (readAheadAvailableBytes > 0)
            {
                int toCopy = Math.Min(readAheadAvailableBytes, bytesRequired);
                Array.Copy(readAheadBuffer, readAheadOffset, buffer, offset + bytesRead, toCopy);
                bytesRead += toCopy;
                readAheadOffset += toCopy;
            }
            else
            {
                readAheadOffset = 0;
                readAheadLength = base.Read(readAheadBuffer, 0, readAheadBuffer.Length);
                if (readAheadLength == 0)
                {
                    break;
                }
            }
        }
        return bytesRead;
    }
}

Хорошее описание, как работает протокол - Другой вопрос на эту же тему

Ваш ответ

Нажимая «Отправить ответ», вы соглашаетесь с условиями пользования и подтверждаете, что прочитали политику конфиденциальности.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.