/// <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> /// 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> /// 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> /// Encode the header media data. /// </summary> /// <param name="videoAudio">The video and audio header content.</param> /// <returns>The encoded media data.</returns> public byte[] EncodeHeader(VideoAudioHeader videoAudio) { byte[] data = null; VideoHeader?video = videoAudio.Video; AudioHeader?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.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); } 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 the audio data. 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); } 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); }