0

I'm using a SampleGrabber to get audio data, however my BufferCB method is not being executed. What am I doing wrong ?

//add Sample Grabber
            IBaseFilter pSampleGrabber = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_SampleGrabber));
            hr = pGraph.AddFilter(pSampleGrabber, "SampleGrabber");
            checkHR(hr, "Can't add Sample Grabber");

        AMMediaType pSampleGrabber_pmt = new AMMediaType();
        //pSampleGrabber_pmt.majorType = MediaType.Audio;
        pSampleGrabber_pmt.subType = MediaSubType.PCM;
        pSampleGrabber_pmt.formatType = FormatType.WaveEx;
        pSampleGrabber_pmt.fixedSizeSamples = true;
        pSampleGrabber_pmt.formatSize = 18;
        pSampleGrabber_pmt.sampleSize = 2;

        WaveFormatEx pSampleGrabber_Format = new WaveFormatEx();
        pSampleGrabber_Format.wFormatTag = 1;
        pSampleGrabber_Format.nChannels = 1;
        pSampleGrabber_Format.nSamplesPerSec = 48000;
        pSampleGrabber_Format.nAvgBytesPerSec = 96000;
        pSampleGrabber_Format.nBlockAlign = 2;
        pSampleGrabber_Format.wBitsPerSample = 16;
        pSampleGrabber_pmt.formatPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(pSampleGrabber_Format));
        Marshal.StructureToPtr(pSampleGrabber_Format, pSampleGrabber_pmt.formatPtr, false);
        hr = ((ISampleGrabber)pSampleGrabber).SetMediaType(pSampleGrabber_pmt);
        DsUtils.FreeAMMediaType(pSampleGrabber_pmt);
        checkHR(hr, "Can't set media type to sample grabber");

        ISampleGrabber pGrabber = new SampleGrabber() as ISampleGrabber; 
        pGrabber = (ISampleGrabber)pSampleGrabber;
        pGrabber.SetCallback(null, 1);

My BufferCB method is like

public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
        {

            return 0;
        }
CharlesBryan
  • 181
  • 1
  • 16

1 Answers1

1

You created and configured one instance pSampleGrabber and then you are attaching your callback to another unused idling instance pGrabber.

You need

pSampleGrabber as ISampleGrabber

instead of

new SampleGrabber() as ISampleGrabber
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • I get the error "An object reference is required for the non-static field, method, or property" – CharlesBryan May 31 '12 at 08:52
  • Oh wait, you are doing even more weird things. `pGrabber = new ...` is completely useless as you are are immeately releasing this instance. `SetCallback(null` - what do you expect here? You are passing `null` where your callback is expected... – Roman R. May 31 '12 at 08:56
  • Oh,I see.. So I need to implement ISampleGrabberCB and then put my implementation of BufferCB there ? – CharlesBryan May 31 '12 at 09:09
  • Yes this is how SG finds your `BufferCB`. Actually, I personally always prefer `SampleCB` to `BufferCB`. – Roman R. May 31 '12 at 09:25