public IMediaTrack this[CodecTypes type, int id] { get { if (MediaTracks.Count == 0) { return(null); } GenericMediaTrack track = null; if (id == 0) // get the first track with the given type { if (MediaTracks.Any(trk => trk.Codec.CodecType == type)) { track = (GenericMediaTrack)MediaTracks.First(trk => trk.Codec.CodecType == type); } } else // get specific track with the given type and track ID { if (MediaTracks.Any(trk => trk.Codec.CodecType == type && trk.TrackID == id)) { track = (GenericMediaTrack)MediaTracks.First(trk => trk.Codec.CodecType == type && trk.TrackID == id); } } return(track); } }
/// <summary> /// WriteSamples /// Write several samples to the output file. /// </summary> /// <param name="slices"></param> /// <param name="codecType"></param> public override void WriteSamples(IEnumerable <Slice> slices, CodecTypes codecType) { if (codecType == CodecTypes.Audio) { // ignore audio } else if (codecType == CodecTypes.Video) { IMediaTrack trak = MediaTracks.First(t => t.Codec.CodecType == CodecTypes.Video); foreach (Slice sample in slices) { // convert to bit-stream format (prefix with 0001) Stream mstrm = new MemoryStream(sample.SliceBytes); Stream ostr = H264.H264Utilities.H264Stream(firstSample, trak.Codec.PrivateCodecData, mstrm, 0, (uint)sample.SliceBytes.Length); firstSample = false; mstrm.Close(); ostr.Position = 0L; byte[] buf = new byte[ostr.Length]; ostr.Read(buf, 0, (int)ostr.Length); ostr.Close(); sample.SliceBytes = buf; Stream.Write(sample.SliceBytes, 0, sample.SliceBytes.Length); } } else { throw new Exception("WriteSamples: unknown codec type"); } }
/// <summary> /// WriteSamples /// Overloaded method for writing slices directly. /// </summary> /// <param name="slices">A List of Sample(s)</param> /// <param name="codecType">Member of CodecTypes enum</param> public override void WriteSamples(IEnumerable <Slice> slices, CodecTypes codecType) { if (codecType == CodecTypes.Audio) { GenericAudioTrack audioTrack = (GenericAudioTrack)MediaTracks.First(tr => tr.Codec.CodecType == codecType); QBoxTrackFormat trackFormat = (QBoxTrackFormat)audioTrack.TrackFormat; trackFormat.WriteSamples(_binaryWriter, slices); } else if (codecType == CodecTypes.Video) { GenericVideoTrack videoTrack = (GenericVideoTrack)MediaTracks.First(tr => tr.Codec.CodecType == codecType); QBoxTrackFormat trackFormat = (QBoxTrackFormat)videoTrack.TrackFormat; trackFormat.WriteSamples(_binaryWriter, slices); } else { throw new Exception("WriteSamples: unknown codec type"); } }