Beispiel #1
0
        /// <summary>
        /// Write the video and audio headers.
        /// </summary>
        /// <param name="header">The video and audo header information.</param>
        public void WriteHeader(VideoAudioHeader header)
        {
            VideoHeader?video = header.Video;
            AudioHeader?audio = header.Audio;

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

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

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

                    // Get the frame rate.
                    _videoFrameRate = video.Value.FrameRate;
                    _videoDuration  = video.Value.Duration;
                }
                else
                {
                    // Let the stream know there is no video data.
                    _binaryWriter.Write(false);
                }

                // If there is audio data.
                if (audio != null && audio.Value.ContainsAudio)
                {
                    // Write each pice of binary data to the stream.
                    _binaryWriter.Write(audio.Value.ContainsAudio);
                    _binaryWriter.Write(audio.Value.Channels);
                    _binaryWriter.Write(audio.Value.SamplingRate);
                    _binaryWriter.Write(audio.Value.SampleSize);
                    _binaryWriter.Write(Helper.GetSoundTypeInt32(audio.Value.SoundType));
                    _binaryWriter.Write(CompressionAlgorithmHelper.GetAlgorithmInt32(audio.Value.CompressionAlgorithm));
                    _binaryWriter.Write(audio.Value.Duration);

                    // Get the frame rate.
                    _audioDuration = audio.Value.Duration;
                }
                else
                {
                    // Let the stream know there is no audio data.
                    _binaryWriter.Write(false);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #2
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);
        }