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; }
private INSSBuffer WriteOne(Bitmap hBitmap) { INSSBuffer pSample; Rectangle r = new Rectangle(0, 0, hBitmap.Width, hBitmap.Height); // Lock the bitmap, which gets us a pointer to the raw bitmap data BitmapData 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 m_Writer.AllocateSample(iSize, out pSample); // Get the buffer from the sample interface. is // where we copy the bitmap data to pSample.GetBuffer(out ip); // Copy the bitmap data into the sample buffer LoadSample(bmd, ip, iSize); } finally { hBitmap.UnlockBits(bmd); } return(pSample); }
/// <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"); } }
/// <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); }
public INSSBuffer GetSampleFromBitmap(Bitmap bitmap) { INSSBuffer sample = null; IntPtr sampleBuffer; uint length = 0; #if DEBUG && DEBUG_SAMPLES Logger.WriteLogMessage("Grabbing sample from bitmap."); #endif BitmapData bitmapData = null; uint actualLength = 0; try { // // assume incoming bitmap was screen-oriented // bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat); length = (uint)(bitmapData.Height * bitmapData.Stride); #if DEBUG && DEBUG_SAMPLES Logger.WriteLogMessage("Locked bits on bitmap, rect [" + rect + "], length [" + length + "], width [" + bitmapData.Width + "], height [" + bitmapData.Height + "], stride [" + bitmapData.Stride + "], pixelFormat [" + bitmap.PixelFormat + "]."); #endif #if DEBUG_BITMAP_IMAGE // SaveBitmapAsImage(bitmap, bitmapData); #endif // // allocate new sample and grab sample buffer // _writer.AllocateSample(length, out sample); #if DEBUG && DEBUG_SAMPLES Logger.WriteLogMessage("Allocated sample, [" + length + "] bytes."); #endif sample.GetBufferAndLength(out sampleBuffer, out actualLength); #if DEBUG && DEBUG_SAMPLES Logger.WriteLogMessage("Grabbed buffer from sample, [" + actualLength + "] bytes."); #endif Helpers.CopyMemory(sampleBuffer, bitmapData.Scan0, length); #if DEBUG && DEBUG_SAMPLES Logger.WriteLogMessage("Copied [" + length + "] bytes from bitmap to sample buffer."); #endif } catch (Exception) { // error handle throw; //Logger.WriteLogError("Failed converting bitmap to sample.", e); } finally { if (bitmapData != null) { bitmap.UnlockBits(bitmapData); #if DEBUG && DEBUG_SAMPLES Logger.WriteLogMessage("Unlocked bits on bitmap."); #endif } } return(sample); }