/// <summary> /// Write a block of video and audio data. /// </summary> /// <param name="block">The block of video and audio data to write.</param> public void Write(VideoAudio block) { try { // Write video data. if (block.Video != null) { // Write video data. _binaryWriter.Write((ushort)0); _binaryWriter.Write(block.Video.Length); // Get the images in the video. for (int v = 0; v < block.Video.Length; v++) { _binaryWriter.Write(block.Video[v].Data.Length); _binaryWriter.Write(block.Video[v].Data); } // Calculate the video frame count and duration. _totalVideoFrames += (long)block.Video.Length; _videoDuration = (_videoFrameRate > 0.0 ? (_totalVideoFrames / _videoFrameRate) : 0.0); } // Write audio data. if (block.Audio != null) { // Write audio data. _binaryWriter.Write((ushort)1); _binaryWriter.Write(block.Audio.Length); // Get the sounds in the audio. for (int s = 0; s < block.Audio.Length; s++) { _binaryWriter.Write(block.Audio[s].StartAtFrameIndex); _binaryWriter.Write(block.Audio[s].Data.Length); _binaryWriter.Write(block.Audio[s].Data); } // Calculate the audio frame count and duration. _totalAudioFrames += (long)block.Audio.Length; _audioDuration = (double)_totalAudioFrames; } } catch (Exception) { throw; } }
/// <summary> /// Read a block of video and audio data. /// </summary> /// <returns>The block of video and audio data read.</returns> public VideoAudio Read() { ImageModel[] video = null; SoundModel[] audio = null; VideoAudio block = new VideoAudio(); try { // If data exists. if (_binaryReader.PeekChar() > -1) { // Read the block type video or audio. ushort mediaType = _binaryReader.ReadUInt16(); int count = _binaryReader.ReadInt32(); // Read the block of data. switch (mediaType) { case 0: // Video block. video = new ImageModel[count]; // Get the images in the video. for (int v = 0; v < count; v++) { video[v].Size = _binaryReader.ReadInt32(); video[v].Data = _binaryReader.ReadBytes(video[v].Size); } // Calculate the video frame count and duration. _totalVideoFrames += (long)count; break; case 1: // Audio block. audio = new SoundModel[count]; // Get the sounds in the audio. for (int s = 0; s < count; s++) { audio[s].StartAtFrameIndex = _binaryReader.ReadInt32(); audio[s].Size = _binaryReader.ReadInt32(); audio[s].Data = _binaryReader.ReadBytes(audio[s].Size); } // Calculate the audio frame count and duration. _totalAudioFrames += (long)count; break; } } } catch (Exception) { throw; } // Assign the video and audio data. block.Video = video; block.Audio = audio; // Return the video audio block. return(block); }
/// <summary> /// Decode the raw media data. /// </summary> /// <param name="encodedData">The encoded media data.</param> /// <returns>The decoded media data.</returns> public VideoAudio[] DecodeRaw(byte[] encodedData) { ImageModel[] video = null; SoundModel[] audio = null; List <VideoAudio> videoAudios = new List <VideoAudio>(); 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); // While data exists. while (binaryReader.PeekChar() > -1) { // Video and audio. VideoAudio videoAudio = new VideoAudio(); // Read the block type video or audio. ushort mediaType = binaryReader.ReadUInt16(); int count = binaryReader.ReadInt32(); // Read the block of data. switch (mediaType) { case 0: // Video block. video = new ImageModel[count]; // Get the images in the video. for (int v = 0; v < count; v++) { video[v].Size = binaryReader.ReadInt32(); video[v].Data = binaryReader.ReadBytes(video[v].Size); } break; case 1: // Audio block. audio = new SoundModel[count]; // Get the sounds in the audio. for (int s = 0; s < count; s++) { audio[s].StartAtFrameIndex = binaryReader.ReadInt32(); audio[s].Size = binaryReader.ReadInt32(); audio[s].Data = binaryReader.ReadBytes(audio[s].Size); } break; } // Assign the video and audio data. videoAudio.Video = video; videoAudio.Audio = audio; // Add the video and audio. videoAudios.Add(videoAudio); } } catch (Exception) { throw; } finally { if (binaryReader != null) { binaryReader.Close(); } if (memoryStream != null) { memoryStream.Close(); } } // Return the video and audio data. return(videoAudios.ToArray()); }