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