Exemplo n.º 1
0
        protected override void OpenMediaAsync()
        {
            Dictionary <MediaSourceAttributesKeys, string> media_attributes = new Dictionary <MediaSourceAttributesKeys, string> ();
            List <MediaStreamDescription> media_streams = new List <MediaStreamDescription> ();
            Dictionary <MediaStreamAttributeKeys, string> stream_attributes = new Dictionary <MediaStreamAttributeKeys, string> ();
            MediaStreamDescription media_stream = new MediaStreamDescription(MediaStreamType.Audio, stream_attributes);
            long         duration = 60 * 10000;
            WaveFormatEx wave     = new WaveFormatEx();
            Mp3Frame     frame    = Mp3Frame.Read(stream);

            wave.FormatTag      = 85;
            wave.AvgBytesPerSec = (uint)frame.Bitrate / 8;
            wave.BitsPerSample  = 0;
            wave.BlockAlign     = 1;
            wave.Channels       = (ushort)frame.Channels;
            wave.SamplesPerSec  = (ushort)frame.SampleRate;
            wave.Size           = 12;

            media_attributes.Add(MediaSourceAttributesKeys.CanSeek, "0");
            media_attributes.Add(MediaSourceAttributesKeys.Duration, duration.ToString());
            stream_attributes [MediaStreamAttributeKeys.CodecPrivateData] = wave.Encoded;

            media_streams.Add(media_stream);

            try {
                this.frame       = frame;
                this.description = media_stream;
                ReportOpenMediaCompleted(media_attributes, media_streams);
                opened = DateTime.Now;
            } catch (Exception ex) {
                Console.WriteLine(ex);
            }
        }
Exemplo n.º 2
0
        protected override void GetSampleAsync(MediaStreamType mediaStreamType)
        {
            Mp3Frame          frame;
            MediaStreamSample sample;
            Dictionary <MediaSampleAttributeKeys, string> attribs = new Dictionary <MediaSampleAttributeKeys, string> ();

            //string format = "HH:mm:ss.ffff";
            //if (opened == DateTime.MinValue)
            //    opened = DateTime.Now;
            //Debug.WriteLine ("{0} GetSampleAsync stamp: {1}", (DateTime.Now - opened).ToString (), TimeSpan.FromMilliseconds (current_pts / 10000).ToString ());

            try {
                if (this.frame != null)
                {
                    frame      = this.frame;
                    this.frame = null;
                }
                else
                {
                    frame = Mp3Frame.Read(stream);
                }

                sample = new MediaStreamSample(description, new MemoryStream(frame.data), 0, frame.data.Length, current_pts, attribs);

                current_pts += frame.Duration;

                ReportGetSampleCompleted(sample);
            } catch (System.IO.EndOfStreamException ex) {
                Console.WriteLine(ex);
                sample = new MediaStreamSample(description, null, 0, 0, 0, attribs);
                ReportGetSampleCompleted(sample);
            } catch (Exception ex) {
                Console.WriteLine(ex);
                ReportGetSampleCompleted(null);
            }
        }