Пример #1
0
IMFTopologyNode* MediaSource::CreateTopologySourceNode()
{
  IMFTopologyNode* mfTopologyNode = nullptr;
  IMFStreamDescriptor* mfStreamDescriptor = nullptr;
  HRESULT hr;
  DWORD streamDescriptorCount;
  BOOL isSelected;

  do
  {
    // Iterate through the source streams to find the currently selected one

    if (!SUCCEEDED(hr = _mfPresentationDescriptor->GetStreamDescriptorCount(&streamDescriptorCount)))
      break;

    for (DWORD i = 0; i < streamDescriptorCount; i++)
    {
      if (!SUCCEEDED(hr = _mfPresentationDescriptor->GetStreamDescriptorByIndex(i, &isSelected, &mfStreamDescriptor)))
        break;

      if (isSelected)
      {
        break;
      }
      else
      {
        mfStreamDescriptor->Release();
        mfStreamDescriptor = nullptr;
      }
    }

    if (FAILED(hr) || !mfStreamDescriptor)
    {
      throw std::exception("Could not find selected stream in MediaSource");
    }

    // otherwise, we're in good shape :-)

    if (!SUCCEEDED(hr = MFCreateTopologyNode(MF_TOPOLOGY_SOURCESTREAM_NODE, &mfTopologyNode)))
      break;

    if (!SUCCEEDED(hr = mfTopologyNode->SetUnknown(MF_TOPONODE_SOURCE, _mfMediaSource)))
      break;

    if (!SUCCEEDED(hr = mfTopologyNode->SetUnknown(MF_TOPONODE_PRESENTATION_DESCRIPTOR, _mfPresentationDescriptor)))
      break;

    if (!SUCCEEDED(hr = mfTopologyNode->SetUnknown(MF_TOPONODE_STREAM_DESCRIPTOR, mfStreamDescriptor)))
      break;
  } while (0);

  if (FAILED(hr))
  {
    throw std::exception("Could not create topology source node");
  }

  mfStreamDescriptor->Release();
  
  return mfTopologyNode;
}
Пример #2
0
HRESULT CMFCamCapture::chooseCaptureFormats()
{
    IMFPresentationDescriptor *pPD = NULL;
    IMFStreamDescriptor *pSD = NULL;
    IMFMediaTypeHandler *pHandler = NULL;
    IMFMediaType *pType = NULL;
    DWORD i;

    HRESULT hr = m_spSource->CreatePresentationDescriptor(&pPD);
    if (FAILED(hr)) {
        goto done;
    }

    BOOL fSelected;
    hr = pPD->GetStreamDescriptorByIndex(0, &fSelected, &pSD);
    if (FAILED(hr)) {
        goto done;
    }

    hr = pSD->GetMediaTypeHandler(&pHandler);
    if (FAILED(hr)) {
        goto done;
    }

    DWORD cTypes = 0;
    hr = pHandler->GetMediaTypeCount(&cTypes);
    if (FAILED(hr)) {
        goto done;
    }

    for (i = 0; i < cTypes; i++) {
        hr = pHandler->GetMediaTypeByIndex(i, &pType);
        if (FAILED(hr)) {
            goto done;
        }

        bool found = selectMediaType(m_spSource, pType);
        if (found) {
            LogMediaType(pType);
            OutputDebugString(L"\n");
        }
        SafeRelease(&pType);
        if (found) {            
            hr = SetDeviceFormat(m_spSource, i);
            break;
        }
    }

    if (i >= cTypes) {
        hr = E_FAIL;
    }

done:
    SafeRelease(&pPD);
    SafeRelease(&pSD);
    SafeRelease(&pHandler);
    SafeRelease(&pType);
    return hr;
}
Пример #3
0
HRESULT SetMaxFrameRate(IMFMediaSource *pSource, DWORD dwTypeIndex)
{
  IMFPresentationDescriptor *pPD = NULL;
  IMFStreamDescriptor *pSD = NULL;
  IMFMediaTypeHandler *pHandler = NULL;
  IMFMediaType *pType = NULL;

  HRESULT hr = pSource->CreatePresentationDescriptor(&pPD);
  if (FAILED(hr))
  {
    goto done;
  }

  BOOL fSelected;
  hr = pPD->GetStreamDescriptorByIndex(dwTypeIndex, &fSelected, &pSD);
  if (FAILED(hr))
  {
    goto done;
  }

  hr = pSD->GetMediaTypeHandler(&pHandler);
  if (FAILED(hr))
  {
    goto done;
  }

  hr = pHandler->GetCurrentMediaType(&pType);
  if (FAILED(hr))
  {
    goto done;
  }

  // Get the maximum frame rate for the selected capture format.

  // Note: To get the minimum frame rate, use the 
  // MF_MT_FRAME_RATE_RANGE_MIN attribute instead.

  PROPVARIANT var;
  if (SUCCEEDED(pType->GetItem(MF_MT_FRAME_RATE_RANGE_MAX, &var)))
  {
    hr = pType->SetItem(MF_MT_FRAME_RATE, var);

    PropVariantClear(&var);

    if (FAILED(hr))
    {
      goto done;
    }

    hr = pHandler->SetCurrentMediaType(pType);
  }

done:
  SafeRelease(&pPD);
  SafeRelease(&pSD);
  SafeRelease(&pHandler);
  SafeRelease(&pType);
  return hr;
}
Пример #4
0
HRESULT EnumerateCaptureFormats(IMFMediaSource *pSource)
{
  IMFPresentationDescriptor *pPD = NULL;
  IMFStreamDescriptor *pSD = NULL;
  IMFMediaTypeHandler *pHandler = NULL;
  IMFMediaType *pType = NULL;

  HRESULT hr = pSource->CreatePresentationDescriptor(&pPD);
  if (FAILED(hr))
  {
    goto done;
  }

  BOOL fSelected;
  hr = pPD->GetStreamDescriptorByIndex(0, &fSelected, &pSD);
  if (FAILED(hr))
  {
    goto done;
  }

  hr = pSD->GetMediaTypeHandler(&pHandler);
  if (FAILED(hr))
  {
    goto done;
  }

  DWORD cTypes = 0;
  hr = pHandler->GetMediaTypeCount(&cTypes);
  if (FAILED(hr))
  {
    goto done;
  }

  for (DWORD i = 0; i < cTypes; i++)
  {
    hr = pHandler->GetMediaTypeByIndex(i, &pType);
    if (FAILED(hr))
    {
      goto done;
    }

    LogMediaType(pType);
    OutputDebugString(L"\n");

    SafeRelease(&pType);
  }

done:
  SafeRelease(&pPD);
  SafeRelease(&pSD);
  SafeRelease(&pHandler);
  SafeRelease(&pType);
  return hr;
}
Пример #5
0
// Create the topology.
HRESULT CreateTopology(IMFMediaSource *pSource, IMFActivate *pSinkActivate, IMFTopology **ppTopo)
{
    IMFTopology *pTopology = NULL;
    IMFPresentationDescriptor *pPD = NULL;
    IMFStreamDescriptor *pSD = NULL;
    IMFMediaTypeHandler *pHandler = NULL;
    IMFTopologyNode *pNode1 = NULL;
    IMFTopologyNode *pNode2 = NULL;

    HRESULT hr = S_OK;
    DWORD cStreams = 0;

    CHECK_HR(hr = MFCreateTopology(&pTopology));
    CHECK_HR(hr = pSource->CreatePresentationDescriptor(&pPD));
    CHECK_HR(hr = pPD->GetStreamDescriptorCount(&cStreams));

    for (DWORD i = 0; i < cStreams; i++) {
        // In this example, we look for audio streams and connect them to the sink.

        BOOL fSelected = FALSE;
        GUID majorType;

        CHECK_HR(hr = pPD->GetStreamDescriptorByIndex(i, &fSelected, &pSD));
        CHECK_HR(hr = pSD->GetMediaTypeHandler(&pHandler));
        CHECK_HR(hr = pHandler->GetMajorType(&majorType));

        if (majorType == MFMediaType_Video && fSelected) {
            CHECK_HR(hr = AddSourceNode(pTopology, pSource, pPD, pSD, &pNode1));
            CHECK_HR(hr = AddOutputNode(pTopology, pSinkActivate, 0, &pNode2));
            CHECK_HR(hr = pNode1->ConnectOutput(0, pNode2, 0));
            break;
        } else {
            CHECK_HR(hr = pPD->DeselectStream(i));
        }
        SafeRelease(&pSD);
        SafeRelease(&pHandler);
    }

    *ppTopo = pTopology;
    (*ppTopo)->AddRef();

done:
    SafeRelease(&pTopology);
    SafeRelease(&pNode1);
    SafeRelease(&pNode2);
    SafeRelease(&pPD);
    SafeRelease(&pSD);
    SafeRelease(&pHandler);
    return hr;
}
  //----------------------------------------------------------------------------
  long MediaFoundationVideoDevice::SetDeviceFormat(IMFMediaSource *pSource, DWORD streamIndex, DWORD dwFormatIndex)
  {
    IMFPresentationDescriptor *pPD = NULL;
    IMFStreamDescriptor *pSD = NULL;
    IMFMediaTypeHandler *pHandler = NULL;
    IMFMediaType *pType = NULL;

    HRESULT hr = pSource->CreatePresentationDescriptor(&pPD);
    if (FAILED(hr))
    {
      goto done;
    }

    BOOL fSelected=false;
    hr = pPD->GetStreamDescriptorByIndex(streamIndex, &fSelected, &pSD);
    if (FAILED(hr))
    {
      goto done;
    }
    if (!fSelected)
    {
      hr = pPD->SelectStream(streamIndex);
      if (FAILED(hr))
      {
        LOG_ERROR("Failed to select stream "<<streamIndex);
        goto done;
      }
    }

    hr = pSD->GetMediaTypeHandler(&pHandler);
    if (FAILED(hr))
    {
      goto done;
    }

    hr = pHandler->GetMediaTypeByIndex(dwFormatIndex, &pType);
    if (FAILED(hr))
    {
      goto done;
    }

    hr = pHandler->SetCurrentMediaType(pType);

done:
    SafeRelease(&pPD);
    SafeRelease(&pSD);
    SafeRelease(&pHandler);
    SafeRelease(&pType);
    return hr;
  }
/** Set playback topology */
FIntPoint FImfVideoPlayer::SetPlaybackTopology( FImfSampleGrabberCallback* SampleGrabberCallback )
{
	FIntPoint OutDimensions = FIntPoint( ForceInit );
	HRESULT HResult = S_OK;

	IMFPresentationDescriptor* PresentationDesc = NULL;
	HResult = MediaSource->CreatePresentationDescriptor( &PresentationDesc );
	check( SUCCEEDED( HResult ) );

	IMFTopology* Topology = NULL;
	HResult = MFCreateTopology( &Topology );
	check( SUCCEEDED( HResult ) );

	DWORD StreamCount = 0;
	HResult = PresentationDesc->GetStreamDescriptorCount( &StreamCount );
	check( SUCCEEDED( HResult ) );

	for( uint32 i = 0; i < StreamCount; i++ )
	{
		BOOL bSelected = 0;

		IMFStreamDescriptor* StreamDesc = NULL;
		HResult = PresentationDesc->GetStreamDescriptorByIndex( i, &bSelected, &StreamDesc );
		check( SUCCEEDED( HResult ) );

		if( bSelected )
		{
			FIntPoint VideoDimensions = AddStreamToTopology( Topology, PresentationDesc, StreamDesc, SampleGrabberCallback );
			if( VideoDimensions != FIntPoint( ForceInit ) )
				OutDimensions = VideoDimensions;
		}

		StreamDesc->Release( );
	}

	HResult = MediaSession->SetTopology( 0, Topology );
	check( SUCCEEDED( HResult ) );

	Topology->Release( );
	PresentationDesc->Release( );

	return OutDimensions;
}
Пример #8
0
HRESULT SetDeviceFormat(IMFMediaSource *pSource, DWORD dwFormatIndex)
{
  IMFPresentationDescriptor *pPD = NULL;
  IMFStreamDescriptor *pSD = NULL;
  IMFMediaTypeHandler *pHandler = NULL;
  IMFMediaType *pType = NULL;

  HRESULT hr = pSource->CreatePresentationDescriptor(&pPD);
  if (FAILED(hr))
  {
    goto done;
  }

  BOOL fSelected;
  hr = pPD->GetStreamDescriptorByIndex(0, &fSelected, &pSD);
  if (FAILED(hr))
  {
    goto done;
  }

  hr = pSD->GetMediaTypeHandler(&pHandler);
  if (FAILED(hr))
  {
    goto done;
  }

  hr = pHandler->GetMediaTypeByIndex(dwFormatIndex, &pType);
  if (FAILED(hr))
  {
    goto done;
  }

  hr = pHandler->SetCurrentMediaType(pType);

done:
  SafeRelease(&pPD);
  SafeRelease(&pSD);
  SafeRelease(&pHandler);
  SafeRelease(&pType);
  return hr;
}
Пример #9
0
HRESULT WavSource::ValidatePresentationDescriptor(IMFPresentationDescriptor *pPD)
{
    HRESULT hr;

    assert(pPD != NULL);

    IMFStreamDescriptor *pStreamDescriptor = NULL;
    IMFMediaTypeHandler *pHandler = NULL;
    IMFMediaType        *pMediaType = NULL;
    WAVEFORMATEX        *pFormat = NULL;

    DWORD   cStreamDescriptors = 0;
    BOOL    fSelected = FALSE;
    UINT32  cbWaveFormat = 0;

    // Make sure there is only one stream.
    hr = pPD->GetStreamDescriptorCount(&cStreamDescriptors);

    if (SUCCEEDED(hr))
    {   
        if (cStreamDescriptors != 1)
        {
            hr = MF_E_UNSUPPORTED_REPRESENTATION;
        }
    }

    // Get the stream descriptor.
    if (SUCCEEDED(hr))
    {   
        hr = pPD->GetStreamDescriptorByIndex(0, &fSelected, &pStreamDescriptor);
    }

    // Make sure it's selected. (This media source has only one stream, so it
    // is not useful to deselect the only stream.)
    if (SUCCEEDED(hr))
    {   
        if (!fSelected)
        {
            hr = MF_E_UNSUPPORTED_REPRESENTATION;
        }
    }

    // Get the media type handler, so that we can get the media type.
    if (SUCCEEDED(hr))
    {   
        hr = pStreamDescriptor->GetMediaTypeHandler(&pHandler);
    }

    if (SUCCEEDED(hr))
    {   
        hr = pHandler->GetCurrentMediaType(&pMediaType);
    }

    if (SUCCEEDED(hr))
    {   
        hr = MFCreateWaveFormatExFromMFMediaType(
            pMediaType, 
            &pFormat,
            &cbWaveFormat);
    }

    if (SUCCEEDED(hr))
    {   
        assert(this->WaveFormat() != NULL);

        if (cbWaveFormat < this->WaveFormatSize())
        {
            hr = MF_E_INVALIDMEDIATYPE;
        }
    }

    if (SUCCEEDED(hr))
    {   
        if (memcmp(pFormat, WaveFormat(), WaveFormatSize()) != 0)
        {
            hr = MF_E_INVALIDMEDIATYPE;
        }
    }


    SafeRelease(&pStreamDescriptor);
    SafeRelease(&pHandler);
    SafeRelease(&pMediaType);
    CoTaskMemFree(pFormat);

    return hr;
}
Пример #10
0
HRESULT WavSource::CreatePresentationDescriptor()
{
    HRESULT hr = S_OK;
    MFTIME duration = 0;

    IMFMediaType *pMediaType = NULL;
    IMFStreamDescriptor *pStreamDescriptor = NULL;
    IMFMediaTypeHandler *pHandler = NULL;

    assert(WaveFormat() != NULL);

    // Create an empty media type.
    hr = MFCreateMediaType(&pMediaType);

    // Initialize the media type from the WAVEFORMATEX structure.
    if (SUCCEEDED(hr))
    {   
        hr = MFInitMediaTypeFromWaveFormatEx(pMediaType, WaveFormat(), WaveFormatSize());
    }

    // Create the stream descriptor.
    if (SUCCEEDED(hr))
    {   
        hr = MFCreateStreamDescriptor(
            0,          // stream identifier
            1,          // Number of media types.
            &pMediaType, // Array of media types
            &pStreamDescriptor
            );
    }

    // Set the default media type on the media type handler.
    if (SUCCEEDED(hr))
    {   
        hr = pStreamDescriptor->GetMediaTypeHandler(&pHandler);
    }

    if (SUCCEEDED(hr))
    {   
        hr = pHandler->SetCurrentMediaType(pMediaType);
    }

    // Create the presentation descriptor.
    if (SUCCEEDED(hr))
    {   
        hr = MFCreatePresentationDescriptor(
            1,                      // Number of stream descriptors
            &pStreamDescriptor,     // Array of stream descriptors
            &m_pPresentationDescriptor
            );
    }

    // Select the first stream
    if (SUCCEEDED(hr))
    {   
        hr = m_pPresentationDescriptor->SelectStream(0);
    }

    // Set the file duration as an attribute on the presentation descriptor.
    if (SUCCEEDED(hr))
    {   
        duration = m_pRiff->FileDuration();
        hr = m_pPresentationDescriptor->SetUINT64(MF_PD_DURATION, (UINT64)duration);
    }

    SafeRelease(&pMediaType);
    SafeRelease(&pStreamDescriptor);
    SafeRelease(&pHandler);
    return hr;
}
  //----------------------------------------------------------------------------
  long MediaFoundationVideoDevice::EnumerateCaptureFormats(IMFMediaSource *pSource)
  {
    IMFPresentationDescriptor *pPD = NULL;
    IMFStreamDescriptor *pSD = NULL;
    IMFMediaTypeHandler *pHandler = NULL;

    HRESULT hr=-1;
    if (pSource==NULL)
    {
      goto done;
    }

    hr = pSource->CreatePresentationDescriptor(&pPD);
    if (FAILED(hr))
    {
      goto done;
    }

    DWORD descriptorCount = 0;
    hr = pPD->GetStreamDescriptorCount(&descriptorCount);
    if (FAILED(hr))
    {
      goto done;
    }

    for (DWORD streamIndex = 0; streamIndex<descriptorCount; streamIndex++)
    {
      std::vector<MediaType> formats;

      BOOL fSelected = false;
      hr = pPD->GetStreamDescriptorByIndex(streamIndex, &fSelected, &pSD);
      if (FAILED(hr))
      {
        goto done;
      }
      DWORD streamId=0;
      hr = pSD->GetStreamIdentifier(&streamId);
      if (FAILED(hr))
      {
        goto done;
      }
      hr = pSD->GetMediaTypeHandler(&pHandler);
      if (FAILED(hr))
      {
        goto done;
      }
      DWORD cTypes = 0;
      hr = pHandler->GetMediaTypeCount(&cTypes);
      if (FAILED(hr))
      {
        continue;
      }
      for (DWORD i = 0; i < cTypes; i++)
      {
        IMFMediaType *pType = NULL;
        hr = pHandler->GetMediaTypeByIndex(i, &pType);
        if (SUCCEEDED(hr))
        {
          MediaType mt = FormatReader::Read(pType);\
          mt.StreamId = streamId;
          formats.push_back(mt);
        }
        SafeRelease(&pType);
      }
      this->CurrentFormats.push_back(formats);
    }

done:
    SafeRelease(&pPD);
    SafeRelease(&pSD);
    SafeRelease(&pHandler);
    return hr;
  }