/// <summary> /// Sample callback, NOT USED. /// </summary> /// <param name="SampleTime">The sample time.</param> /// <param name="pSample">The media sample.</param> /// <returns>The result.</returns> int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample pSample) { if (!_gotOneImage) { // Set bGotOne to prevent further calls until we // request a new bitmap. _gotOneImage = true; IntPtr pBuffer; pSample.GetPointer(out pBuffer); int iBufferLen = pSample.GetSize(); if (pSample.GetSize() > _strideImage * _videoHeightImage) { throw new Exception("Buffer is wrong size"); } Devices.CopyMemory(_handleImage, pBuffer, _strideImage * _videoHeightImage); // Picture is ready. if (_pictureReadyImage != null) { _pictureReadyImage.Set(); } } Marshal.ReleaseComObject(pSample); return(0); }
/// <summary> sample callback, NOT USED. </summary> int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample pSample) { if (!m_bGotOne) { // Set bGotOne to prevent further calls until we // request a new bitmap. m_bGotOne = true; IntPtr pBuffer; pSample.GetPointer(out pBuffer); int iBufferLen = pSample.GetSize(); if (pSample.GetSize() > m_stride * m_videoHeight) { throw new Exception("Buffer is wrong size"); } NativeMethods.CopyMemory(m_handle, pBuffer, m_stride * m_videoHeight); // Picture is ready. m_PictureReady.Set(); } Marshal.ReleaseComObject(pSample); return(0); }
/// <summary> /// Sample callback, NOT USED. /// </summary> /// <param name="SampleTime">The sample time.</param> /// <param name="pSample">The media sample.</param> /// <returns>The result.</returns> int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample pSample) { if (!_gotOneSound) { // Set bGotOne to prevent further calls until we // request a new bitmap. _gotOneSound = true; IntPtr pBuffer; pSample.GetPointer(out pBuffer); int iBufferLen = pSample.GetSize(); if (pSample.GetSize() > _soundMemorySize) { throw new Exception("Buffer is wrong size"); } Directx.Utility.Devices.CopyMemory(_handleSound, pBuffer, _soundMemorySize); // Picture is ready. if (_audoReadySound != null) { _audoReadySound.Set(); } } Marshal.ReleaseComObject(pSample); return(0); }
/// <summary> sample callback, NOT USED. </summary> int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample pSample) { if (!m_bGotOne) { // Set bGotOne to prevent further calls until we // request a new bitmap. m_bGotOne = true; IntPtr pBuffer; pSample.GetPointer(out pBuffer); int iBufferLen = pSample.GetSize(); if (iBufferLen > m_stride * m_videoHeight) { throw new Exception("Buffer is wrong size"); } #if !LIB unsafe { Buffer.MemoryCopy(pBuffer.ToPointer(), m_handle.ToPointer(), iBufferLen, iBufferLen); } #endif // Picture is ready. m_PictureReady.Set(); } Marshal.ReleaseComObject(pSample); return(0); }
/// <summary> /// コンストラクタ (初期値指定) /// </summary> /// <param name="sample_time">サンプルタイム</param> /// <param name="sample_data">サンプルデータ</param> public CxSampleGrabberEventArgs(double sample_time, IMediaSample sample_data) { SampleTime = sample_time; SampleData = sample_data; if (sample_data != null) { sample_data.GetPointer(ref m_Address); m_Length = sample_data.GetSize(); } }
public MediaSample(IMediaSample sample) { m_Size = sample.GetSize(); m_ActualDataLength = sample.GetActualDataLength(); m_IsSyncPoint = sample.IsSyncPoint() == 0; m_IsPreroll = sample.IsPreroll() == 0; m_IsDiscontinuity = sample.IsDiscontinuity() == 0; sample.GetTime(out m_TimeStart, out m_TimeEnd); sample.GetMediaTime(out m_MediaTimeStart, out m_MediaTimeEnd); m_Buffer = Marshal.AllocCoTaskMem(m_Size); }
/// <summary> sample callback, NOT USED. </summary> int ISampleGrabberCB.SampleCB(double sampleTime, IMediaSample sample) { if (!hasPicture) { hasPicture = true; IntPtr bufferPointer; sample.GetPointer(out bufferPointer); int iBufferLen = sample.GetSize(); if (sample.GetSize() > Stride * Height) { throw new Exception("Buffer is wrong size"); } CopyMemory(imageHandle, bufferPointer, Stride * Height); pictureReady.Set(); } Marshal.ReleaseComObject(sample); return(0); }
/// <summary> /// The callback from the GSSF to populate the sample. This class isn't intended /// to be overridden. Child classes should instead implement PopulateSample, /// which this method calls. /// </summary> /// <param name="pSample">The sample to populate</param> /// <returns>HRESULT</returns> public int SampleCallback(IMediaSample pSample) { int hr; IntPtr pData; try { // Get the buffer into which we will copy the data hr = pSample.GetPointer(out pData); if (hr >= 0) { // Find out the amount of space in the buffer int cbData = pSample.GetSize(); lock (this) { hr = SetTimeStamps(pSample); if (hr >= 0) { int iRead; // Populate the sample hr = PopulateSample(pData, cbData, out iRead); if (hr >= 0) { if (hr == S_Ok) // 1 == End of stream { // increment the frame number for next time m_iFrameNumber++; } else { m_iFrameNumber = 0; } pSample.SetActualDataLength(iRead); } } } } } finally { // Release our pointer the the media sample. THIS IS ESSENTIAL! If // you don't do this, the graph will stop after about 2 samples. Marshal.ReleaseComObject(pSample); } return(hr); }
private int PopulateMediaSample(IMediaSample pSample) { int hr; long tStart, tStop; // Read the file offsets they are seeking hr = pSample.GetTime(out tStart, out tStop); if (hr >= 0) { IntPtr pBuffer = IntPtr.Zero; // Turn the sample time into a file position and length long llPos = tStart / UNIT; int lLength = (int)((tStop - tStart) / UNIT); int iBufferSize = pSample.GetSize(); Debug.Write(string.Format(" pos: {0} len: {1} size: {2}", llPos, lLength, iBufferSize)); // Make sure the buffer they passed it big enough to hold // all the data they requested if (iBufferSize >= lLength) { // This is where we store the data hr = pSample.GetPointer(out pBuffer); } else { hr = E_BufferTooSmall; } if (hr >= 0) { int iBytesRead; // Read the data into the buffer hr = SyncReadInternal(llPos, lLength, pBuffer, out iBytesRead); if (hr >= 0) { // How much of the buffer we used. pSample.SetActualDataLength(iBytesRead); Debug.Write(string.Format(" read: {0}", iBytesRead)); } } } return(hr); }
/// <summary> /// Called by the GenericSampleSourceFilter. This routine populates the MediaSample. /// </summary> /// <param name="sample">Pointer to a sample</param> /// <returns>0 = success, 1 = end of stream, negative values for errors</returns> public virtual int SampleCallback(IMediaSample sample) { int hr; IntPtr dataPointer; try { // Get the buffer into which we will copy the data hr = sample.GetPointer(out dataPointer); if (hr >= 0) { // Set TRUE on every sample for uncompressed frames hr = sample.SetSyncPoint(true); if (hr >= 0) { // Find out the amount of space in the buffer int callbackData = sample.GetSize(); hr = this.SetTimeStamps(sample); if (hr >= 0) { int read; // Get copy the data into the sample hr = this.GetImage(this.frameNumber, dataPointer, callbackData, out read); // 1 == End of stream if (hr == 0) { sample.SetActualDataLength(read); // increment the frame number for next time this.frameNumber++; } } } } } finally { // Release our pointer the the media sample. THIS IS ESSENTIAL! If // you don't do this, the graph will stop after about 2 samples. Marshal.ReleaseComObject(sample); } return(hr); }
public MediaSample(IMediaSample sample) { m_Size = sample.GetSize(); m_ActualDataLength = sample.GetActualDataLength(); m_IsSyncPoint = sample.IsSyncPoint() == 0; m_IsPreroll = sample.IsPreroll() == 0; m_IsDiscontinuity = sample.IsDiscontinuity() == 0; m_HasTime = sample.GetTime(out m_TimeStart, out m_TimeEnd) == 0; m_HasMediaTime = sample.GetMediaTime(out m_MediaTimeStart, out m_MediaTimeEnd) == 0; m_Buffer = Marshal.AllocCoTaskMem(m_Size); // Copy the media type AMMediaType mediaType; if (sample.GetMediaType(out mediaType) == 0) { m_MediaType = mediaType; } }
void TestLength() { int hr; int iSize, iLen, iLen2; iSize = m_ims.GetSize(); iLen = m_ims.GetActualDataLength(); // Since we aren't using compression, size and datalen should // be the same. Debug.Assert(iSize == iLen, "GetSize, GetActualDatalen"); // Change the datalen hr = m_ims.SetActualDataLength(iLen - 1); Marshal.ThrowExceptionForHR(hr); // Make sure the change, changed iLen2 = m_ims.GetActualDataLength(); Debug.Assert(iLen - 1 == iLen2, "Get/Set ActualDataLen"); }
int ISampleGrabberCB.SampleCB(double sampleTime, IMediaSample sample) { if (!_gotPicture) { // Set bGotOne to prevent further calls until we // request a new bitmap. _gotPicture = true; IntPtr pBuffer; sample.GetPointer(out pBuffer); if (sample.GetSize() > _stride * _height) { throw new Exception("Buffer is wrong size"); } CopyMemory(_handle, pBuffer, _stride * _height); _pictureReadyResetEvent.Set(); } Marshal.ReleaseComObject(sample); return 0; }
/// <summary> sample callback, NOT USED. </summary> int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample pSample) { if (!m_bGotOne) { // Set bGotOne to prevent further calls until we // request a new bitmap. m_bGotOne = true; IntPtr pBuffer; pSample.GetPointer(out pBuffer); int iBufferLen = pSample.GetSize(); if (pSample.GetSize() > m_stride*m_videoHeight) { throw new Exception("Buffer is wrong size"); } NativeMethods.CopyMemory(m_handle, pBuffer, m_stride*m_videoHeight); // Picture is ready. m_PictureReady.Set(); } Marshal.ReleaseComObject(pSample); return 0; }
/// <summary> /// The callback from the GSSF to populate the sample. This class isn't intended /// to be overridden. Child classes should instead implement PopulateSample, /// which this method calls. /// </summary> /// <param name="pSample">The sample to populate</param> /// <returns>HRESULT</returns> public int SampleCallback(IMediaSample pSample) { int hr; IntPtr pData; try { // Get the buffer into which we will copy the data hr = pSample.GetPointer(out pData); if (hr >= 0) { // Find out the amount of space in the buffer int cbData = pSample.GetSize(); lock (this) { hr = SetTimeStamps(pSample); if (hr >= 0) { int iRead; // Populate the sample hr = PopulateSample(pData, cbData, out iRead); if (hr >= 0) { if (hr == S_Ok) // 1 == End of stream { // increment the frame number for next time m_iFrameNumber++; } else { m_iFrameNumber = 0; } pSample.SetActualDataLength(iRead); } } } } } finally { // Release our pointer the the media sample. THIS IS ESSENTIAL! If // you don't do this, the graph will stop after about 2 samples. Marshal.ReleaseComObject(pSample); } return hr; }
public int Request(IMediaSample pSample, IntPtr dwUser) { // queues up a request for a new sample Monitor.Enter(this); long start, end; pSample.GetTime(out start, out end); log.InfoFormat("Request ({0}, {1} - {2})", start, end, pSample.GetSize()); sampleList.Add(pSample); samplePtrList.Add(dwUser); Monitor.Exit(this); return S_OK; }
/// <summary> /// Called by the GenericSampleSourceFilter. This routine populates the MediaSample. /// </summary> /// <param name="pSample">Pointer to a sample</param> /// <returns>0 = success, 1 = end of stream, negative values for errors</returns> public virtual int SampleCallback(IMediaSample pSample) { int hr; IntPtr pData; try { // Get the buffer into which we will copy the data hr = pSample.GetPointer(out pData); if (hr >= 0) { // Set TRUE on every sample for uncompressed frames hr = pSample.SetSyncPoint(true); if (hr >= 0) { // Find out the amount of space in the buffer int cbData = pSample.GetSize(); hr = SetTimeStamps(pSample); if (hr >= 0) { int iRead; // Get copy the data into the sample hr = GetImage(m_iFrameNumber, pData, cbData, out iRead); if (hr == 0) // 1 == End of stream { pSample.SetActualDataLength(iRead); // increment the frame number for next time m_iFrameNumber++; } } } } } finally { // Release our pointer the the media sample. THIS IS ESSENTIAL! If // you don't do this, the graph will stop after about 2 samples. Marshal.ReleaseComObject(pSample); } return hr; }
/// <summary> /// �R���X�g���N�^ (�����l�w��) /// </summary> /// <param name="sample_time">�T���v���^�C��</param> /// <param name="sample_data">�T���v���f�[�^</param> public CxSampleGrabberEventArgs(double sample_time, IMediaSample sample_data) { SampleTime = sample_time; SampleData = sample_data; if (sample_data != null) { sample_data.GetPointer(ref m_Address); m_Length = sample_data.GetSize(); } }
public int SyncReadAligned(IMediaSample pSample) { Monitor.Enter(this); long start, end; long mediaStart, mediaEnd; int size; int offset; int hr; int ans = S_OK; IntPtr ptr = new IntPtr(); hr = pSample.GetPointer(out ptr); hr = pSample.GetTime(out start, out end); hr = pSample.GetMediaTime(out mediaStart, out mediaEnd); size = pSample.GetSize(); int rescale = DS_RESCALE_FACTOR; // (int)((end - start) / size); //start += startTime; offset = (int)(start / rescale); if ((offset + size) > reader.BufferSize) { Memory.Set(ptr, offset, size); log.InfoFormat("SyncReadAligned went off the end ({0}, {1}, {2})", offset, (offset + size), reader.BufferSize); size = (int)(reader.BufferSize - offset); ans = S_FALSE; } if ((offset + size) > reader.BufferEnd) { log.InfoFormat("SyncReadAligned wait for buffer ({0}, {1}, {2})", offset, (offset + size), reader.BufferEnd); BufferData(offset + size); } log.InfoFormat("SyncReadAligned ({0} / {1} ({2}), {3} / {4}) - {5}, {6}", start, mediaStart, offset, end, mediaEnd, size, rescale); byte[] buffer = reader.GetBuffer(); Marshal.Copy(buffer, offset, ptr, size); reader.ReleaseBuffer(); Monitor.Exit(this); return ans; }
int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample pSample) { if (!gotOne) { gotOne = true; IntPtr pBuffer; pSample.GetPointer(out pBuffer); pSample.GetSize(); if (pSample.GetSize() > Stride*Height) throw new Exception("Buffer is wrong size"); Kernel32.CopyMemory(handle, pBuffer, Stride * Height); pictureReady.Set(); } Marshal.ReleaseComObject(pSample); return 0; }
/// <summary> /// Implementation of ISampleGrabberCB. /// </summary> int ISampleGrabberCB.SampleCB(double sampleTime, IMediaSample pSample) { IntPtr pBuffer; int hr = pSample.GetPointer(out pBuffer); DsError.ThrowExceptionForHR(hr); Analyze(sampleTime, pBuffer, pSample.GetSize()); Marshal.ReleaseComObject(pSample); return 0; }
public int SampleCB(double SampleTime, IMediaSample pSample) { if (!bProcess) { lastSampleTime = SampleTime; return(WinAPI.E_FAIL); } // internal stats ++sampleIndex; long tStart, tEnd; pSample.GetMediaTime(out tStart, out tEnd); Debug.Assert(tStart < tEnd); Debug.Assert(tStart > lastMediaTime); sampleProcessed += tEnd - tStart; sampleDropped += tStart - lastMediaTime - 1; lastMediaTime = tEnd - 1; int dataLen = pSample.GetActualDataLength(); IntPtr bufPtr; int hr = pSample.GetPointer(out bufPtr); Debug.Assert(0 == hr); // BEGIN TRACE int bufSize = pSample.GetSize(); long timeStart, timeEnd; pSample.GetTime(out timeStart, out timeEnd); string msg = string.Format( "SampleCB ({0}) {1}, sampleTime:{2} datalen:{3} bufsize:{4} mediaTime:{5}-{6} time:{7}-{8}", name, sampleIndex, SampleTime, dataLen, bufSize, tStart, tEnd, timeStart, timeEnd); Trace.WriteLine(msg); if (tStart - lastMediaTime - 1 > 0) { msg = string.Format("!!! Frame drop: {0}", tStart - lastMediaTime - 1 > 0); Trace.WriteLine(msg); } //END TRACE byte[] buf = new byte[dataLen]; Marshal.Copy(bufPtr, buf, 0, dataLen); if (file != null) { file.Write(buf, 0, dataLen); } //DBG - simulate encoding error //if (sampleIndex > 100) // goto STOP_CAPTURE; if (mediaState != null && mediaState.mpeg2Enc != null) { PrimoSoftware.AVBlocks.Transcoder enc = mediaState.mpeg2Enc; MediaSample inputSample = new MediaSample(); inputSample.Buffer = new MediaBuffer(buf); inputSample.StartTime = Math.Max(SampleTime, 0); //TODO: end time try { bool pushed = false; // transcoder.Push() is not threads safe. // lock (enc){ } ensure that only one thread is calling transcoder.Push() lock (enc) { pushed = enc.Push(StreamNumber, inputSample); } if (pushed) { return(0); } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.ToString()); } Trace.WriteLine("PushSample FAILED"); } //STOP_CAPTURE: Trace.WriteLine("SampleCB: Before Post STOP_CAPTURE"); WinAPI.PostMessage(MainWindow, Util.WM_STOP_CAPTURE, new IntPtr(streamNumber), IntPtr.Zero); Trace.WriteLine("SampleCB: After Post STOP_CAPTURE"); bProcess = false; return(WinAPI.E_FAIL); } // end of SampleCB