Esempio n. 1
0
        /// <summary>
        /// Encode the media data.
        /// </summary>
        /// <param name="videoAudio">The video and audio content.</param>
        /// <returns>The encoded media data.</returns>
        public byte[] Encode(VideoAudioModel videoAudio)
        {
            byte[] data = null;

            VideoModel?video = videoAudio.Video;
            AudioModel?audio = videoAudio.Audio;

            MemoryStream memoryStream = null;
            BinaryWriter binaryWriter = null;

            try
            {
                // Create the encoding stream.
                memoryStream = new MemoryStream();

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

                // Write the media format.
                if (videoAudio.MediaFormat > 0)
                {
                    binaryWriter.Write(videoAudio.MediaFormat);
                }
                else
                {
                    binaryWriter.Write(Nequeo.Media.Streaming.MediaFormat);
                }

                // If there is video data.
                if (video != null && video.Value.Header.ContainsVideo)
                {
                    // Write each pice of binary data to the stream.
                    binaryWriter.Write(video.Value.Header.ContainsVideo);
                    binaryWriter.Write(video.Value.Header.FrameRate);
                    binaryWriter.Write(video.Value.Header.FrameSizeWidth);
                    binaryWriter.Write(video.Value.Header.FrameSizeHeight);
                    binaryWriter.Write(Helper.GetImageTypeInt32(video.Value.Header.ImageType));
                    binaryWriter.Write(CompressionAlgorithmHelper.GetAlgorithmInt32(video.Value.Header.CompressionAlgorithm));
                    binaryWriter.Write(video.Value.Header.Duration);
                    binaryWriter.Write((ushort)0);
                    binaryWriter.Write(video.Value.Video.Length);

                    // Get the images in the video.
                    for (int v = 0; v < video.Value.Video.Length; v++)
                    {
                        binaryWriter.Write(video.Value.Video[v].Data.Length);
                        binaryWriter.Write(video.Value.Video[v].Data);
                    }
                }
                else
                {
                    // Let the stream know there is no video data.
                    binaryWriter.Write(false);
                }

                // If there is audio data.
                if (audio != null && audio.Value.Header.ContainsAudio)
                {
                    // Write the audio data.
                    binaryWriter.Write(audio.Value.Header.ContainsAudio);
                    binaryWriter.Write(audio.Value.Header.Channels);
                    binaryWriter.Write(audio.Value.Header.SamplingRate);
                    binaryWriter.Write(audio.Value.Header.SampleSize);
                    binaryWriter.Write(Helper.GetSoundTypeInt32(audio.Value.Header.SoundType));
                    binaryWriter.Write(CompressionAlgorithmHelper.GetAlgorithmInt32(audio.Value.Header.CompressionAlgorithm));
                    binaryWriter.Write(audio.Value.Header.Duration);
                    binaryWriter.Write((ushort)1);
                    binaryWriter.Write(audio.Value.Audio.Length);

                    // Get the sounds in the audio.
                    for (int s = 0; s < audio.Value.Audio.Length; s++)
                    {
                        binaryWriter.Write(audio.Value.Audio[s].StartAtFrameIndex);
                        binaryWriter.Write(audio.Value.Audio[s].Data.Length);
                        binaryWriter.Write(audio.Value.Audio[s].Data);
                    }
                }
                else
                {
                    // Let the stream know there is no audio data.
                    binaryWriter.Write(false);
                }

                // Assign the data.
                data = memoryStream.ToArray();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (binaryWriter != null)
                {
                    binaryWriter.Close();
                }

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

            // Return the data.
            return(data);
        }
Esempio n. 2
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);
        }