Ejemplo n.º 1
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);
			}
		}
Ejemplo n.º 2
0
		public static Mp3Frame Read (BinaryReader stream)
		{
			Mp3Frame result = new Mp3Frame ();
			uint header = 0;
			uint version, layer, bitrate, samplerate, padding, channel;

			do {
				header <<= 8;
				header += stream.ReadByte ();
			} while ((header & sync_mask) != sync_mask);

			version = (header & version_mask) >> version_shift;
			layer = (header & layer_mask) >> layer_shift;
			bitrate = (header & bitrate_mask) >> bitrate_shift;
			samplerate = (header & samplerate_mask) >> samplerate_shift;
			padding = (header & padding_mask) >> padding_shift;
			channel = (header & channel_mask) >> channel_shift;

			result.Padding = (int) padding;
			result.Channels = channel == 3 ? 1 : 2;

			switch (layer) {
			case 1:
				result.Layer = 3;
				break;
			case 2:
				result.Layer = 2;
				break;
			case 3:
				result.Layer = 1;
				break;
			default:
				throw new InvalidOperationException ();
			}

			switch (version) {
			case 1:
				result.Version = 3; // 2.5
				break;
			case 2:
				result.Version = 2; // 2
				break;
			case 3:
				result.Version = 1; // 1
				break;
			default: 
				throw new InvalidOperationException ();
			}

			result.SampleRate = sample_rates [result.Version - 1, samplerate];

			switch (result.Version) {
			case 1:
				result.Bitrate = mpeg1_bitrates [result.Layer - 1, bitrate];
				break;
			case 2:
			case 3:
				result.Bitrate = mpeg2_bitrates [result.Layer - 1, bitrate];
				break;
			default:
				throw new InvalidOperationException ();
			}

			switch (result.Layer) {
			case 1:
				result.FrameSize = ((12 * result.Bitrate / result.SampleRate) + result.Padding) * 4;
				break;
			case 2:
			case 3:
				result.FrameSize = (144 * result.Bitrate / result.SampleRate) + result.Padding;
				break;
            default:
				throw new InvalidOperationException ();
            }

			result.Duration = block_sizes [result.Version - 1, result.Layer - 1];
			result.Duration *= 10000000;
			result.Duration /= result.SampleRate;

			//System.Diagnostics.Debug.WriteLine ("Found header: {0:X8} at {7}, version: {1}, layer: {2}, bitrate: {3}, samplerate: {4}, padding: {5}, channel: {6} Duration: {8}",
			//	header, version, layer, bitrate, samplerate, padding, channel, stream.BaseStream.Position, result.Duration); 

			using (MemoryStream memory = new MemoryStream ()) {
				using (BinaryWriter writer = new BinaryWriter (memory)) {

					writer.Write ((byte) ((header & 0xFF000000) >> 24));
					writer.Write ((byte) ((header & 0x00FF0000) >> 16));
					writer.Write ((byte) ((header & 0x0000FF00) >> 8));
					writer.Write ((byte) ((header & 0x000000FF)));
					for (int i = 0; i < result.FrameSize - 4; i++)
						writer.Write (stream.ReadByte ());

					writer.Flush ();
					result.data = memory.ToArray ();
				}
			}

			return result;
		}
Ejemplo n.º 3
0
        public static Mp3Frame Read(BinaryReader stream)
        {
            Mp3Frame result = new Mp3Frame();
            uint     header = 0;
            uint     version, layer, bitrate, samplerate, padding, channel;

            do
            {
                header <<= 8;
                header  += stream.ReadByte();
            } while ((header & sync_mask) != sync_mask);

            version    = (header & version_mask) >> version_shift;
            layer      = (header & layer_mask) >> layer_shift;
            bitrate    = (header & bitrate_mask) >> bitrate_shift;
            samplerate = (header & samplerate_mask) >> samplerate_shift;
            padding    = (header & padding_mask) >> padding_shift;
            channel    = (header & channel_mask) >> channel_shift;

            result.Padding  = (int)padding;
            result.Channels = channel == 3 ? 1 : 2;

            switch (layer)
            {
            case 1:
                result.Layer = 3;
                break;

            case 2:
                result.Layer = 2;
                break;

            case 3:
                result.Layer = 1;
                break;

            default:
                throw new InvalidOperationException();
            }

            switch (version)
            {
            case 1:
                result.Version = 3;                 // 2.5
                break;

            case 2:
                result.Version = 2;                 // 2
                break;

            case 3:
                result.Version = 1;                 // 1
                break;

            default:
                throw new InvalidOperationException();
            }

            result.SampleRate = sample_rates [result.Version - 1, samplerate];

            switch (result.Version)
            {
            case 1:
                result.Bitrate = mpeg1_bitrates [result.Layer - 1, bitrate];
                break;

            case 2:
            case 3:
                result.Bitrate = mpeg2_bitrates [result.Layer - 1, bitrate];
                break;

            default:
                throw new InvalidOperationException();
            }

            switch (result.Layer)
            {
            case 1:
                result.FrameSize = ((12 * result.Bitrate / result.SampleRate) + result.Padding) * 4;
                break;

            case 2:
            case 3:
                result.FrameSize = (144 * result.Bitrate / result.SampleRate) + result.Padding;
                break;

            default:
                throw new InvalidOperationException();
            }

            result.Duration  = block_sizes [result.Version - 1, result.Layer - 1];
            result.Duration *= 10000000;
            result.Duration /= result.SampleRate;

            //System.Diagnostics.Debug.WriteLine ("Found header: {0:X8} at {7}, version: {1}, layer: {2}, bitrate: {3}, samplerate: {4}, padding: {5}, channel: {6} Duration: {8}",
            //	header, version, layer, bitrate, samplerate, padding, channel, stream.BaseStream.Position, result.Duration);

            using (MemoryStream memory = new MemoryStream()) {
                using (BinaryWriter writer = new BinaryWriter(memory)) {
                    writer.Write((byte)((header & 0xFF000000) >> 24));
                    writer.Write((byte)((header & 0x00FF0000) >> 16));
                    writer.Write((byte)((header & 0x0000FF00) >> 8));
                    writer.Write((byte)((header & 0x000000FF)));
                    for (int i = 0; i < result.FrameSize - 4; i++)
                    {
                        writer.Write(stream.ReadByte());
                    }

                    writer.Flush();
                    result.data = memory.ToArray();
                }
            }

            return(result);
        }
Ejemplo n.º 4
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);
			}
		}