Share via


Protecting Content

Windows Media Encoder SDK banner art

After you have set up an account with a license provider and you have a DRM profile, protect your content as follows:

  1. Through the WMEncoder object, retrieve an IWMDRMContentAuthor object.

  2. Through the IWMDRMContentAuthor object, retrieve an IWMDRMProfileCollection object.

  3. Create an IWMDRMProfile object, iterate through the DRM profile collection, and retrieve the DRM profile you want to use.

  4. Use the SetSessionDRMProfile method in the IWMDRMContentAuthor object to use that profile. This method also generates a key ID, which is a string that is used to generate the key for encrypting content. The key ID can also be specified rather than generated. For example, you can reuse a key ID. However, every piece of content that is encrypted with the same key ID can be decrypted with the same license.

  5. Optionally, generate a content ID using the GenerateContentID method of the IWMDRMContentAuthor object, and set it into the current encoding session using the ContentID property. The content ID is used to uniquely identify each file or stream you protect. For example, you could use the content ID in a database to track the content you protect.

  6. Configure the encoding session as usual, and then start the encoding process.

    Note   Protected content that is encoded using the Windows Media Audio 9 Professional codec cannot be played back on portable devices.

This DRM technology is based on the Windows Media Rights Manager SDK, which you can use if you want full control over the entire process of protecting content and issuing licenses. For more information, see the Windows Media Digital Rights Management page on the Microsoft Web site.

The following examples show how to protect content, and assume you have at least one DRM profile. For end-to-end code examples, see Complete Code Examples.

Visual Basic Example

' Create a WMEncoder object.
  Dim Encoder As WMEncoder
  Set Encoder = New WMEncoder

' Create an IWMDRMContentAuthor object.
  Dim DRM As IWMDRMContentAuthor
  Set DRM = Encoder.EncoderDRMContentAuthor

' Retrieve the collection of DRM profiles.
  Dim DRMProColl As IWMDRMProfileCollection
  Set DRMProColl = DRM.DRMProfileCollection

' Create an IWMDRMProfile object and retrieve the first DRM profile
' in the collection.
  Dim DRMPro As IWMDRMProfile
  Set DRMPro = DRMProColl.Item(0)

' Set the DRM profile into the current encoding session. You can specify
' a key ID. If you don't, one is generated.
  Dim vKeyID As Variant
  DRM.SetSessionDRMProfile DRMPro.ID, vKeyID

' Configure the encoding session, including the sources, the output,
' and the profile, and then start encoding.
  Encoder.Start

C++ Example

// Include libraries.
#include <windows.h>
#include <atlbase.h>
#include <comdef.h>
#include "C:\WMSDK\WMEncSDK9\include\wmencode.h"
#include "C:\WMSDK\WMEncSDK9\include\wmdrmprf.h" // for DRM features

    HRESULT hr;
    IWMEncoder2* pEncoder;
    IWMDRMContentAuthor* pDRM;
    IWMDRMProfileCollection* pDRMProColl;
    IWMDRMProfile* pDRMPro;
    IWMDRMAttributes* pProAttr;

    // Initialize the COM library and retrieve a pointer
    // to an IWMEncoder interface.
    hr = CoInitialize(NULL);

    if ( SUCCEEDED( hr ) )
    {
        hr = CoCreateInstance(CLSID_WMEncoder,
            NULL,
            CLSCTX_INPROC_SERVER,
            IID_IWMEncoder2,
            (void**) &pEncoder);
    }

    // Create an IWMDRMContentAuthor object.
    if ( SUCCEEDED( hr ) )
    {
        hr = pEncoder->get_EncoderDRMContentAuthor(&pDRM);
    }

    // Retrieve the collection of DRM profiles.
    if ( SUCCEEDED( hr ) )
    {
        hr = pDRM->get_DRMProfileCollection(&pDRMProColl);
    }

    // Create an IWMDRMProfile object and retrieve a profile.
    CComVariant vIndex(0);
    if ( SUCCEEDED( hr ) )
    {
        hr = pDRMProColl->Item(vIndex, &pDRMPro);
    }

    // Set the DRM profile into the current encoding session. 
    // You can specify a key ID. If you don't, one is generated.
    CComVariant vKeyID;
    CComBSTR sProID;
    if ( SUCCEEDED( hr ) )
    {
        hr = pDRMPro->get_ID(&sProID);
    }
    if ( SUCCEEDED( hr ) )
    {
        hr = pDRM->SetSessionDRMProfile(sProID, &vKeyID);
    }

    // Configure the encoding session, including the sources, the output,
    // and the profile, and then start encoding.

    // Release pointers.
    if ( pDRM )
    {
        pDRM->Release();
        pDRM = NULL;
    }
    if ( pDRMProColl )
    {
        pDRMProColl->Release();
        pDRMProColl = NULL;
    }
    if ( pDRMPro )
    {
        pDRMPro->Release();
        pDRMPro = NULL;
    }
    if ( pProAttr )
    {
        pProAttr->Release();
        pProAttr = NULL;
    }
    if ( pEncoder )
    {
        pEncoder->Release();
        pEncoder = NULL;
    }

C# Example

using WMEncoderLib;

try
{
// Create a WMEncoder object.
  WMEncoder Encoder = new WMEncoder();

// Create an IWMDRMContentAuthor object.
  IWMDRMContentAuthor DRM = Encoder.EncoderDRMContentAuthor;

// Retrieve the collection of DRM profiles.
  IWMDRMProfileCollection DRMProColl = DRM.DRMProfileCollection;

// Create an IWMDRMProfile object and retrieve the first DRM profile
// in the collection.
  IWMDRMProfile DRMPro = DRMProColl.Item(0);

// Set the DRM profile into the current encoding session. You can specify
// a key ID. Otherwise, a key ID is generated.
  object vKeyID = 0;
  DRM.SetSessionDRMProfile(DRMPro.ID, ref vKeyID);

// Configure the encoding session, including the sources, the output,
// and the profile, and then start encoding.
} 

catch (Exception e) 
{  
   // TODO: Handle exceptions.
}

See Also