/// <summary> /// Write a buffer of uncompressed PCM data to the buffer. /// </summary> /// <param name="buffer">Byte array defining the buffer to write.</param> /// <param name="index">Index of first value to write</param> /// <param name="count">NUmber of byte to write. Must be multiple of PCM sample size <see cref="Yeti.WMFSdk.WmaWriterConfig.Format.nBlockAlign"/></param> public override void Write(byte[] buffer, int index, int count) { if ((count % m_InputDataFormat.nBlockAlign) == 0) { INSSBuffer IBuff; NSSBuffer NssBuff; m_Writer.AllocateSample((uint)count, out IBuff); NssBuff = new NSSBuffer(IBuff); NssBuff.Write(buffer, index, count); NssBuff.Length = (uint)count; m_Writer.WriteSample(0, m_MsAudioTime * 10000, 0, IBuff); m_MsAudioTime += ((ulong)count * 1000) / (ulong)m_InputDataFormat.nAvgBytesPerSec; } else { throw new ArgumentException( string.Format("Invalid buffer size. Buffer size must be aligned to {0} bytes.", m_InputDataFormat.nBlockAlign), "count"); } }
public void Write(AudioBuffer buffer) { if (this.closed) { throw new InvalidOperationException("Writer already closed."); } if (!this.fileCreated) { this.m_pWriter.SetOutputFilename(outputPath); this.fileCreated = true; } if (!this.writingBegan) { this.m_pWriter.BeginWriting(); this.writingBegan = true; } buffer.Prepare(this); INSSBuffer pSample; m_pWriter.AllocateSample(buffer.ByteLength, out pSample); IntPtr pdwBuffer; pSample.GetBuffer(out pdwBuffer); pSample.SetLength(buffer.ByteLength); Marshal.Copy(buffer.Bytes, 0, pdwBuffer, buffer.ByteLength); long cnsSampleTime = sampleCount * 10000000L / Settings.PCM.SampleRate; m_pWriter.WriteSample(0, cnsSampleTime, SampleFlag.CleanPoint, pSample); Marshal.ReleaseComObject(pSample); sampleCount += buffer.Length; }
/// <summary> /// Add a frame to the output file /// </summary> /// <param name="hBitmap">Frame to add</param> public void AppendNewFrame(Bitmap hBitmap) { int hr = 0; INSSBuffer pSample = null; Rectangle r = new Rectangle(0, 0, hBitmap.Width, hBitmap.Height); BitmapData bmd; if (!m_Init) { // Only call this for the first frame Initialize(hBitmap); } // Lock the bitmap, which gets us a pointer to the raw bitmap data bmd = hBitmap.LockBits(r, ImageLockMode.ReadOnly, hBitmap.PixelFormat); try { // Compute size of bitmap in bytes. Strides may be negative. int iSize = Math.Abs(bmd.Stride * bmd.Height); IntPtr ip; // Get a sample interface hr = m_pWMWriter.AllocateSample(iSize, out pSample); Marshal.ThrowExceptionForHR(hr); // Get the buffer from the sample interface. This is // where we copy the bitmap data to hr = pSample.GetBuffer(out ip); Marshal.ThrowExceptionForHR(hr); // Copy the bitmap data into the sample buffer LoadSample(bmd, ip, iSize); // Write the sample to the output - Sometimes, for reasons I can't explain, // writing a sample fails. However, writing the same sample again // works. Go figure. int iRetry = 0; do { hr = m_pWMWriter.WriteSample(m_dwVideoInput, 10000 * m_msVideoTime, WriteFlags.CleanPoint, pSample); } while (hr == NSResults.E_InvalidData && iRetry++ < 3); Marshal.ThrowExceptionForHR(hr); // update the time based on the framerate m_msVideoTime = (++m_dwCurrentVideoSample * 1000) / m_iFrameRate; } finally { // Release the locals if (pSample != null) { Marshal.ReleaseComObject(pSample); pSample = null; } hBitmap.UnlockBits(bmd); } }
/// <summary> /// Write an audio sample. Sample time is in ticks. /// </summary> /// <param name="sampleSize"></param> /// <param name="inBuf"></param> /// <param name="sampleTime">in Ticks</param> /// <returns></returns> public bool WriteAudio(uint sampleSize, BufferChunk inBuf, ulong sampleTime) { INSSBuffer sample; IntPtr sampleBuf = IntPtr.Zero; //Debug.WriteLine("WMWriter.WriteAudio: time=" + sampleTime.ToString()); //return true; // + " size=" + sampleSize.ToString() + // " audio bytes " + inBuf[345].ToString() + " " + // inBuf[346].ToString() + " " + inBuf[347].ToString() + " " + inBuf[348].ToString()); try { lock (this) { writer.AllocateSample(sampleSize, out sample); sample.GetBuffer(out sampleBuf); Marshal.Copy(inBuf.Buffer, inBuf.Index, sampleBuf, (int)sampleSize); sample.SetLength(sampleSize); writer.WriteSample(audioInput, sampleTime, 0, sample); //Debug.WriteLine("Wrote audio. time=" + sampleTime.ToString()); Marshal.ReleaseComObject(sample); lastWriteTime = sampleTime; } } catch (Exception e) { //Debug.WriteLine("Exception while writing audio: " + // "audioInput=" + videoInput.ToString() + // " sampleTime=" + sampleTime.ToString() + // " sampleSize=" + sampleSize.ToString()); Debug.WriteLine("Exception while writing audio: " + e.ToString()); eventLog.WriteEntry("Exception while writing audio: " + e.ToString(), EventLogEntryType.Error, 1000); return(false); } //Debug.WriteLine("Audio write succeeded " + // "audioInput=" + videoInput.ToString() + // " sampleTime=" + sampleTime.ToString() + // " sampleSize=" + sampleSize.ToString()); return(true); }
private void TestWrite() { Bitmap b = new Bitmap(@"c:\sga.png"); Initialize(b); m_Writer.SetOutputFilename(sFileName); m_Writer.BeginWriting(); INSSBuffer pSample = WriteOne(b); m_Writer.WriteSample(0, 1, SampleFlag.CleanPoint, pSample); m_Writer.Flush(); TestAdvanced(b); m_Writer.EndWriting(); }
private void Test() { Bitmap b = new Bitmap(@"c:\sga.png"); Initialize(b); m_Writer.SetOutputFilename(sFileName); m_Writer.BeginWriting(); INSSBuffer pSample = WriteOne(b); m_Writer.WriteSample(0, 1, SampleFlag.CleanPoint, pSample); m_Writer.Flush(); m_Writer.EndWriting(); Debug.Assert(m_OnHeader && m_IsRealTime && m_AllocateDataUnit && m_OnDataUnit && m_OnEndWriting); }
private void TestSink() { IWMWriterAdvanced pWriterA; pWriterA = m_Writer as IWMWriterAdvanced; pWriterA.AddSink(this); Bitmap b = new Bitmap(@"c:\sga.png"); Initialize(b); m_Writer.SetOutputFilename(sFileName); m_Writer.BeginWriting(); INSSBuffer pSample = WriteOne(b); m_Writer.WriteSample(0, 1, SampleFlag.CleanPoint, pSample); m_Writer.Flush(); m_Writer.EndWriting(); }