使用多重位元速率互斥
[與此頁面相關聯的功能 Windows Media Format 11 SDK是舊版功能。 來源讀取器和接收寫入器已取代它。 來源讀取器和接收寫入器已針對Windows 10和Windows 11進行優化。 Microsoft 強烈建議新程式碼盡可能使用來源讀取器和接收寫入器,而不是Windows Media Format 11 SDK。 Microsoft 建議盡可能重寫使用舊版 API 的現有程式碼,以使用新的 API。]
當您想要編碼各種播放案例的內容時,多個位元速率 (MBR) 相互排除很有用。 MBR 視訊輸出是由單一輸入編碼多次所組成,每個輸入都有不同的位元速率設定。 讀取 MBR 編碼的檔案時,讀取器會根據可用的頻寬來判斷要使用的資料流程。
Windows 媒體格式 SDK 支援視訊和音訊資料流程的 MBR 編碼。 此外,您可以建立稱為多視訊大小 MBR 編碼的特殊 MBR 編碼類型。 多視訊大小的 MBR 視訊功能與一般 MBR 視訊相同,不同之處在于您可以在互斥中為視訊串流指定不同的影像大小。
下列範例示範如何設定具有多個視訊大小的 MBR 視訊設定檔。 它會建立新的設定檔,其中包含三個不同位元速率和大小的視訊資料流程,並將其包含在相互排除物件中。
IWMProfileManager* pProfileMgr = NULL;
IWMProfile* pProfile = NULL;
IWMMutualExclusion* pMutex = NULL;
IWMStreamConfig* pStream = NULL;
IWMMediaProps* pMediaProps = NULL;
WM_MEDIA_TYPE* pMediaType = NULL;
DWORD cbMediaType = 0;
HRESULT hr = S_OK;
// Initialize COM.
hr = CoInitialize(NULL);
// Create a profile manager object.
hr = WMCreateProfileManager(&pProfileMgr);
// Create an empty profile.
hr = pProfileMgr->CreateEmptyProfile(WMT_VER_9_0, &pProfile);
// Give the new profile a name and description.
hr = pProfile->SetName(L"MBR_Video_3_Stream_test");
hr = pProfile->SetDescription(L"Only for use with example code.");
// Create the first stream.
hr = pProfile->CreateNewStream(WMMEDIATYPE_Video, &pStream);
// Get the media properties interface for the new stream.
hr = pStream->QueryInterface(IID_IWMMediaProps, (void**)&pMediaProps);
// Get the media-type structure.
// First, get the size of the media-type structure.
hr = pMediaProps->GetMediaType(NULL, &cbMediaType);
// Allocate memory for the structure based on the retrieved size.
pMediaType = (WM_MEDIA_TYPE*) new BYTE[cbMediaType];
// Retrieve the media-type structure.
hr = pMediaProps->GetMediaType(pMediaType, &cbMediaType);
// Change the video size to 640 x 480.
pMediaType->pbFormat->bmiHeader.biWidth = 640;
pMediaType->pbFormat->bmiHeader.biHeight = 480;
// Replace the WM_MEDIA_TYPE in the profile with the one just changed.
hr = pMediaProps->SetMediaType(pMediaType);
// Release the media properties interface and delete the structure.
pMediaProps->Release();
pMediaProps = NULL;
delete[] pMediaType;
pMediaType = NULL;
// Set the bit rate to 200.
hr = pStream->SetBitrate(200000);
// Set the stream number.
hr = pStream->SetStreamNumber(1);
// Include the new stream in the profile.
hr = pProfile->AddStream(pStream);
// Release the stream configuration interface.
pStream->Release();
pStream = NULL;
// For the remaining two streams, leave the video at its default size of
// 320 x 240 <entity type="ndash"/>- just change the bit rates.
// Repeat for a 100K stream.
hr = pProfile->CreateNewStream(WMMEDIATYPE_Video, &pStream);
hr = pStream->SetBitrate(100000);
hr = pStream->SetStreamNumber(2);
hr = pProfile->AddStream(pStream);
pStream->Release();
pStream = NULL;
// Repeat for a 56K stream.
hr = pProfile->CreateNewStream(WMMEDIATYPE_Video, &pStream);
hr = pStream->SetBitrate(56000);
hr = pStream->SetStreamNumber(3);
hr = pProfile->AddStream(pStream);
pStream->Release();
pStream = NULL;
// Now that we have three streams, create a mutual exclusion object.
hr = pProfile->CreateNewMutualExclusion(&pMutex);
// Add the three streams to the mutual exclusion.
hr = pMutex->AddStream(1);
hr = pMutex->AddStream(2);
hr = pMutex->AddStream(3);
// Configure the mutual exclusion for MBR video.
hr = pMutex->SetType(CLSID_WMMUTEX_Bitrate);
// Add the mutual exclusion to the profile.
hr = pProfile->AddMutualExclusion(pMutex);
// Release the mutual exclusion object.
pMutex->Release();
pMutex = NULL;
// TODO: Save the profile to a string, and save the string to a file.
// For more information, see To Save a Custom Profile.
// Release remaining interfaces.
pProfile->Release();
pProfile = NULL;
pProfileMgr->Release();
pProfileMgr = NULL;
相關主題