Exemplo n.º 1
0
        /// <summary>
        /// Decode the header media data.
        /// </summary>
        /// <param name="encodedData">The encoded media data.</param>
        /// <returns>The decoded media data.</returns>
        public VideoAudioHeader DecodeHeader(byte[] encodedData)
        {
            VideoHeader      videoHeader = new VideoHeader();
            AudioHeader      audioHeader = new AudioHeader();
            VideoAudioHeader videoAudio  = new VideoAudioHeader();

            MemoryStream memoryStream = null;
            BinaryReader binaryReader = null;

            try
            {
                // Load the encoded data into the memory stream.
                memoryStream = new MemoryStream(encodedData);

                // Create a new binary reader from the stream
                // set the starting position at the begining
                binaryReader = new BinaryReader(memoryStream);
                binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);

                // Read the media format.
                videoAudio.MediaFormat = binaryReader.ReadInt32();

                // Is there video data.
                videoHeader.ContainsVideo = binaryReader.ReadBoolean();

                // If there is video data.
                if (videoHeader.ContainsVideo)
                {
                    // Read each pice of binary data.
                    videoHeader.FrameRate            = binaryReader.ReadDouble();
                    videoHeader.FrameSizeWidth       = binaryReader.ReadInt32();
                    videoHeader.FrameSizeHeight      = binaryReader.ReadInt32();
                    videoHeader.ImageType            = Helper.GetImageType(binaryReader.ReadInt32());
                    videoHeader.CompressionAlgorithm = CompressionAlgorithmHelper.GetAlgorithm(binaryReader.ReadInt32());
                    videoHeader.Duration             = binaryReader.ReadDouble();
                }

                // Is there audio data.
                audioHeader.ContainsAudio = binaryReader.ReadBoolean();

                // If there is audio data.
                if (audioHeader.ContainsAudio)
                {
                    // Read the audio data.
                    audioHeader.Channels             = binaryReader.ReadInt16();
                    audioHeader.SamplingRate         = binaryReader.ReadInt32();
                    audioHeader.SampleSize           = binaryReader.ReadInt16();
                    audioHeader.SoundType            = Helper.GetSoundType(binaryReader.ReadInt32());
                    audioHeader.CompressionAlgorithm = CompressionAlgorithmHelper.GetAlgorithm(binaryReader.ReadInt32());
                    audioHeader.Duration             = binaryReader.ReadDouble();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (binaryReader != null)
                {
                    binaryReader.Close();
                }

                if (memoryStream != null)
                {
                    memoryStream.Close();
                }
            }

            // Assign the video and audio data.
            videoAudio.Video = videoHeader;
            videoAudio.Audio = audioHeader;

            // Return the video and audio data.
            return(videoAudio);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read the video and audio headers.
        /// </summary>
        /// <returns>The video and audo header information.</returns>
        public VideoAudioHeader ReadHeader()
        {
            VideoHeader      videoHeader = new VideoHeader();
            AudioHeader      audioHeader = new AudioHeader();
            VideoAudioHeader header      = new VideoAudioHeader();

            try
            {
                // Create a new binary reader from the stream
                // set the starting position at the begining
                _binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);

                // Read the media format.
                header.MediaFormat = _binaryReader.ReadInt32();

                // Is there video data.
                videoHeader.ContainsVideo = _binaryReader.ReadBoolean();

                // If there is video data.
                if (videoHeader.ContainsVideo)
                {
                    // Read each pice of binary data.
                    videoHeader.FrameRate            = _binaryReader.ReadDouble();
                    videoHeader.FrameSizeWidth       = _binaryReader.ReadInt32();
                    videoHeader.FrameSizeHeight      = _binaryReader.ReadInt32();
                    videoHeader.ImageType            = Helper.GetImageType(_binaryReader.ReadInt32());
                    videoHeader.CompressionAlgorithm = CompressionAlgorithmHelper.GetAlgorithm(_binaryReader.ReadInt32());
                    videoHeader.Duration             = _binaryReader.ReadDouble();

                    // Get the frame rate.
                    _videoFrameRate = videoHeader.FrameRate;
                    _videoDuration  = videoHeader.Duration;
                }

                // Assign the video header.
                header.Video = videoHeader;

                // Is there audio data.
                audioHeader.ContainsAudio = _binaryReader.ReadBoolean();

                // If there is audio data.
                if (audioHeader.ContainsAudio)
                {
                    // Read the audio data.
                    audioHeader.Channels             = _binaryReader.ReadInt16();
                    audioHeader.SamplingRate         = _binaryReader.ReadInt32();
                    audioHeader.SampleSize           = _binaryReader.ReadInt16();
                    audioHeader.SoundType            = Helper.GetSoundType(_binaryReader.ReadInt32());
                    audioHeader.CompressionAlgorithm = CompressionAlgorithmHelper.GetAlgorithm(_binaryReader.ReadInt32());
                    audioHeader.Duration             = _binaryReader.ReadDouble();

                    // Get the frame rate.
                    _audioDuration = audioHeader.Duration;
                }

                // Assign the audio header.
                header.Audio = audioHeader;
            }
            catch (Exception)
            {
                throw;
            }

            // Return the video and audio header.
            return(header);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Decode the media data.
        /// </summary>
        /// <param name="encodedData">The encoded media data.</param>
        /// <returns>The decoded media data.</returns>
        public VideoAudioModel Decode(byte[] encodedData)
        {
            VideoModel      video       = new VideoModel();
            AudioModel      audio       = new AudioModel();
            VideoHeader     videoHeader = new VideoHeader();
            AudioHeader     audioHeader = new AudioHeader();
            VideoAudioModel videoAudio  = new VideoAudioModel();

            MemoryStream memoryStream = null;
            BinaryReader binaryReader = null;

            try
            {
                // Load the encoded data into the memory stream.
                memoryStream = new MemoryStream(encodedData);

                // Create a new binary reader from the stream
                // set the starting position at the begining
                binaryReader = new BinaryReader(memoryStream);
                binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);

                // Read the media format.
                videoAudio.MediaFormat = binaryReader.ReadInt32();

                // Is there video data.
                videoHeader.ContainsVideo = binaryReader.ReadBoolean();

                // If there is video data.
                if (videoHeader.ContainsVideo)
                {
                    // Read each pice of binary data.
                    videoHeader.FrameRate            = binaryReader.ReadDouble();
                    videoHeader.FrameSizeWidth       = binaryReader.ReadInt32();
                    videoHeader.FrameSizeHeight      = binaryReader.ReadInt32();
                    videoHeader.ImageType            = Helper.GetImageType(binaryReader.ReadInt32());
                    videoHeader.CompressionAlgorithm = CompressionAlgorithmHelper.GetAlgorithm(binaryReader.ReadInt32());
                    videoHeader.Duration             = binaryReader.ReadDouble();
                    ushort mediaVideoType = binaryReader.ReadUInt16();
                    video.ImageCount = binaryReader.ReadInt32();

                    // Create the image collection.
                    ImageModel[] images = new ImageModel[video.ImageCount];

                    // Get the images in the video.
                    for (int v = 0; v < video.ImageCount; v++)
                    {
                        // Create the image.
                        ImageModel image = new ImageModel();
                        image.Size = binaryReader.ReadInt32();
                        image.Data = binaryReader.ReadBytes(image.Size);

                        // Assign this image.
                        images[v] = image;
                    }

                    // Assign the image collection.
                    video.Video = images;
                }

                // Assign the video header.
                video.Header = videoHeader;

                // Is there audio data.
                audioHeader.ContainsAudio = binaryReader.ReadBoolean();

                // If there is audio data.
                if (audioHeader.ContainsAudio)
                {
                    // Read the audio data.
                    audioHeader.Channels             = binaryReader.ReadInt16();
                    audioHeader.SamplingRate         = binaryReader.ReadInt32();
                    audioHeader.SampleSize           = binaryReader.ReadInt16();
                    audioHeader.SoundType            = Helper.GetSoundType(binaryReader.ReadInt32());
                    audioHeader.CompressionAlgorithm = CompressionAlgorithmHelper.GetAlgorithm(binaryReader.ReadInt32());
                    audioHeader.Duration             = binaryReader.ReadDouble();
                    ushort mediaAudioType = binaryReader.ReadUInt16();
                    audio.SoundCount = binaryReader.ReadInt32();

                    // Create the sound collection.
                    SoundModel[] sounds = new SoundModel[audio.SoundCount];

                    // Get the sounds in the audio.
                    for (int s = 0; s < audio.SoundCount; s++)
                    {
                        // Create the sound.
                        SoundModel sound = new SoundModel();
                        sound.StartAtFrameIndex = binaryReader.ReadInt32();
                        sound.Size = binaryReader.ReadInt32();
                        sound.Data = binaryReader.ReadBytes(sound.Size);

                        // Assign this sound.
                        sounds[s] = sound;
                    }

                    // Assign the sound collection.
                    audio.Audio = sounds;
                }

                // Assign the audio header.
                audio.Header = audioHeader;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (binaryReader != null)
                {
                    binaryReader.Close();
                }

                if (memoryStream != null)
                {
                    memoryStream.Close();
                }
            }

            // Assign the video and audio data.
            videoAudio.Video = video;
            videoAudio.Audio = audio;

            // Return the video and audio data.
            return(videoAudio);
        }