/// <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); }
/// <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; } }
/// <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); }
/// <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); }
/// <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); }