コード例 #1
0
 public void TooSmallData()
 {
     this.dataArray = new byte[2] {
         128, 64
     };
     int result = BitTools.ConvertSyncSafeToInt32(this.dataArray, 0);
 }
コード例 #2
0
 public void ReadPastId3V2Tags(Action <MpegFrame> callback)
 {
     byte[] numArray = new byte[10];
     if (this.audioStream.Read(numArray, 0, 3) == 3)
     {
         if ((int)numArray[0] == 73 && (int)numArray[1] == 68 && (int)numArray[2] == 51)
         {
             if (this.audioStream.Read(numArray, 3, 7) == 7)
             {
                 int       id3Size   = BitTools.ConvertSyncSafeToInt32(numArray, 6);
                 int       bytesRead = 0;
                 MpegFrame mpegFrame;
                 ThreadPool.QueueUserWorkItem((WaitCallback)(state =>
                 {
                     while (id3Size > 0)
                     {
                         bytesRead = id3Size - Mp3MediaStreamSourceX.buffer.Length > 0 ? this.audioStream.Read(Mp3MediaStreamSourceX.buffer, 0, Mp3MediaStreamSourceX.buffer.Length) : this.audioStream.Read(Mp3MediaStreamSourceX.buffer, 0, id3Size);
                         id3Size -= bytesRead;
                     }
                     this._offsetFirstFrame = this.audioStream.Position;
                     mpegFrame = new MpegFrame(this.audioStream);
                     callback(mpegFrame);
                 }));
                 return;
             }
         }
         else if (this.audioStream.Read(numArray, 3, 1) == 1)
         {
             MpegFrame mpegFrame = new MpegFrame(this.audioStream, numArray);
             callback(mpegFrame);
             return;
         }
     }
     throw new Exception("Could not read intial audio stream data");
 }
コード例 #3
0
        public void InvalidStartIndexTooCloseToEndOfArray()
        {
            int result;

            try
            {
                result = BitTools.ConvertSyncSafeToInt32(this.dataArray, 2);
            }
            catch (Exception)
            {
                Assert.Fail();
            }

            try
            {
                result = BitTools.ConvertSyncSafeToInt32(this.dataArray, 3);
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            catch (Exception)
            {
                Assert.Fail();
            }
        }
コード例 #4
0
        public void LargestValidSyncSafeIntValue()
        {
            this.dataArray = new byte[4] {
                127, 127, 127, 127
            };
            int result = BitTools.ConvertSyncSafeToInt32(this.dataArray, 0);

            Assert.AreEqual(268435455, result);
        }
コード例 #5
0
        /// <summary>
        /// Read off the Id3Data from the stream and return the first MpegFrame of the audio stream.
        /// This assumes that the first bit of data is either an ID3 segment or an MPEG segment. Should
        /// probably do something a bit more robust at some point.
        /// </summary>
        /// <returns>
        /// The first MpegFrame in the audio stream.
        /// </returns>
        public MpegFrame ReadPastId3V2Tags(Action <MpegFrame> callback)
        {
            /*
             * Since this code assumes that the first bit of data is either an ID3 segment or an MPEG segment it could
             * get into trouble. Should probably do something a bit more robust at some point.
             */

            MpegFrame mpegFrame;

            // Read and (throw out) any Id3 data if present.
            byte[] data = new byte[10];
            if (this.audioStream.Read(data, 0, 3) != 3)
            {
                goto cleanup;
            }

            if (data[0] == 73 /* I */ &&
                data[1] == 68 /* D */ &&
                data[2] == 51 /* 3 */)
            {
                // Need to update to read the is footer present flag and account for its 10 bytes if needed.
                if (this.audioStream.Read(data, 3, 7) != 7)
                {
                    goto cleanup;
                }

                int id3Size   = BitTools.ConvertSyncSafeToInt32(data, 6);
                int bytesRead = 0;

                ThreadPool.QueueUserWorkItem(o =>
                {
                    // Read through the ID3 Data tossing it out.)
                    while (id3Size > 0)
                    {
                        bytesRead = (id3Size - buffer.Length > 0)
                                                                         ? this.audioStream.Read(buffer, 0,
                                                                                                 buffer.Length)
                                                                         : this.audioStream.Read(buffer, 0, id3Size);
                        id3Size -= bytesRead;
                    }

                    mpegFrame = new MpegFrame(this.audioStream);
                    callback(mpegFrame);
                });
            }
            else
            {
                // No ID3 tag present, presumably this is streaming and we are starting right at the Mp3 data.
                // Assume the stream isn't seekable.
                if (this.audioStream.Read(data, 3, 1) != 1)
                {
                    goto cleanup;
                }

                mpegFrame = new MpegFrame(this.audioStream, data);
                callback(mpegFrame);
            }

            return(null);

            // Cleanup and quit if you couldn't even read the initial data for some reason.
cleanup:
            throw new Exception("Could not read intial audio stream data");
        }
コード例 #6
0
 public void InvalidStartIndexTooBig()
 {
     int result = BitTools.ConvertSyncSafeToInt32(this.dataArray, this.dataArray.Length);
 }
コード例 #7
0
 public void InvalidStartIndexTooSmall()
 {
     int result = BitTools.ConvertSyncSafeToInt32(this.dataArray, -1);
 }
コード例 #8
0
        public void Simple()
        {
            int result = BitTools.ConvertSyncSafeToInt32(this.dataArray, 1);

            Assert.AreEqual(27721787, result);
        }
コード例 #9
0
 public void NullData()
 {
     this.dataArray = null;
     BitTools.ConvertSyncSafeToInt32(null, 0);
 }