// Copyright (c) 2010-2011 SharpDX - Alexandre Mutel // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.Globalization; using System.Runtime.InteropServices; using System.Reflection; namespace SharpDX.MediaFoundation { public partial class MediaAttributes { /// /// Initializes a new instance of the class. /// /// The initial number of elements allocated for the attribute store. The attribute store grows as needed. Default is 0 /// ///

Attributes are used throughout Microsoft Media Foundation to configure objects, describe media formats, query object properties, and other purposes. For more information, see Attributes in Media Foundation.

For a complete list of all the defined attribute GUIDs in Media Foundation, see Media Foundation Attributes.

///
/// ms701878 /// HRESULT MFCreateAttributes([Out] IMFAttributes** ppMFAttributes,[In] unsigned int cInitialSize) /// MFCreateAttributes public MediaAttributes(int initialSizeInBytes = 0) : base(IntPtr.Zero) { MediaFactory.CreateAttributes(this, initialSizeInBytes); } /// /// Gets an item value /// /// GUID of the key. /// The value associated to this key. /// ms704598 /// HRESULT IMFAttributes::GetItem([In] const GUID& guidKey,[In] void* pValue) /// IMFAttributes::GetItem public object Get(System.Guid guidKey) { var itemType = GetItemType(guidKey); switch (itemType) { case AttributeType.UInt32: return Get(guidKey); case AttributeType.UInt64: return Get(guidKey); case AttributeType.Double: return Get(guidKey); case AttributeType.Guid: return Get(guidKey); case AttributeType.Blob: return Get(guidKey); case AttributeType.String: return Get(guidKey); case AttributeType.IUnknown: return Get(guidKey); } throw new ArgumentException("The type of the value is not supported"); } /// ///

Applies to: desktop apps | Metro style apps

Retrieves an attribute at the specified index.

///
///

Index of the attribute to retrieve. To get the number of attributes, call .

///

Receives the that identifies this attribute.

/// The value associated to this index /// ///

To enumerate all of an object's attributes in a thread-safe way, do the following:

  1. Call to prevent another thread from adding or deleting attributes.

  2. Call to find the number of attributes.

  3. Call GetItemByIndex to get each attribute by index.

  4. Call to unlock the attribute store.

This interface is available on the following platforms if the Windows Media Format 11 SDK redistributable components are installed:

  • Windows?XP with Service Pack?2 (SP2) and later.
  • Windows?XP Media Center Edition?2005 with KB900325 (Windows?XP Media Center Edition?2005) and KB925766 (October 2006 Update Rollup for Windows?XP Media Center Edition) installed.
///
/// bb970331 /// HRESULT IMFAttributes::GetItemByIndex([In] unsigned int unIndex,[Out] GUID* pguidKey,[InOut, Optional] PROPVARIANT* pValue) /// IMFAttributes::GetItemByIndex public object GetByIndex(int index, out System.Guid guidKey) { GetItemByIndex(index, out guidKey, IntPtr.Zero); return Get(guidKey); } /// /// Gets an item value /// /// GUID of the key. /// The value associated to this key. /// ms704598 /// HRESULT IMFAttributes::GetItem([In] const GUID& guidKey,[In] void* pValue) /// IMFAttributes::GetItem public unsafe T Get(System.Guid guidKey) { // Perform conversions to supported types // int // long // string // byte[] // double // ComObject // Guid if (typeof(T) == typeof(int) || typeof(T) == typeof(bool) || typeof(T) == typeof(byte) || typeof(T) == typeof(uint) || typeof(T) == typeof(short) || typeof(T) == typeof(ushort) || typeof(T) == typeof(byte) || typeof(T) == typeof(sbyte)) { return (T)Convert.ChangeType(GetInt(guidKey), typeof(T)); } if (typeof(T).GetTypeInfo().IsEnum ) { return (T)Enum.ToObject(typeof(T), GetInt(guidKey)); } if (typeof(T) == typeof(IntPtr)) { return (T)(object)new IntPtr(GetLong(guidKey)); } if (typeof(T) == typeof(long) || typeof(T) == typeof(ulong)) { return (T)Convert.ChangeType(GetLong(guidKey), typeof(T)); } if (typeof(T) == typeof(System.Guid)) { return (T)(object)GetGUID(guidKey); } if (typeof(T) == typeof(string)) { int length = GetStringLength(guidKey); char* wstr = stackalloc char[length + 1]; GetString(guidKey, new IntPtr(wstr), length + 1, IntPtr.Zero); return (T)(object)Marshal.PtrToStringUni(new IntPtr(wstr)); } if (typeof(T) == typeof(double) || typeof(T) == typeof(float)) { return (T)Convert.ChangeType(GetDouble(guidKey), typeof(T)); } if (typeof(T) == typeof(byte[])) { int length = GetBlobSize(guidKey); var buffer = new byte[length]; fixed (void *pBuffer = buffer) GetBlob(guidKey, (IntPtr)pBuffer, buffer.Length, IntPtr.Zero); return (T)(object)buffer; } if (typeof(T).GetTypeInfo().IsValueType) { int length = GetBlobSize(guidKey); if ( length != SharpDX.Interop.SizeOf()) { throw new ArgumentException("Size of the structure doesn't match the size of stored value"); } var value = default(T); GetBlob(guidKey, (IntPtr)Interop.Fixed(ref value), SharpDX.Interop.SizeOf(), IntPtr.Zero); return value; } if (typeof(T) == typeof(ComObject)) { IntPtr ptr; GetUnknown(guidKey, Utilities.GetGuidFromType(typeof(IUnknown)), out ptr); return (T)(object)new ComObject(ptr); } if (typeof(T).GetTypeInfo().IsSubclassOf(typeof(ComObject))) { IntPtr ptr; GetUnknown(guidKey, Utilities.GetGuidFromType(typeof(T)), out ptr); return AsUnsafe(ptr); } throw new ArgumentException("The type of the value is not supported"); } /// /// Gets an item value /// /// GUID of the key. /// The value associated to this key. /// ms704598 /// HRESULT IMFAttributes::GetItem([In] const GUID& guidKey,[In] void* pValue) /// IMFAttributes::GetItem public unsafe T Get(MediaAttributeKey guidKey) { return Get(guidKey.Guid); } /// ///

Applies to: desktop apps | Metro style apps

Adds an attribute value with a specified key.

///
///

A that identifies the value to set. If this key already exists, the method overwrites the old value.

///

A that contains the attribute value. The method copies the value. The type must be one of the types listed in the enumeration.

///

The method returns an . Possible values include, but are not limited to, those in the following table.

Return codeDescription

The method succeeded.

E_OUTOFMEMORY

Insufficient memory.

MF_E_INVALIDTYPE

Invalid attribute type.

?

/// ///

This method checks whether the type is one of the attribute types defined in , and fails if an unsupported type is used. However, this method does not check whether the is the correct type for the specified attribute . (There is no programmatic way to associate attribute GUIDs with property types.) For a list of Media Foundation attributes and their data types, see Media Foundation Attributes.

This interface is available on the following platforms if the Windows Media Format 11 SDK redistributable components are installed:

  • Windows?XP with Service Pack?2 (SP2) and later.
  • Windows?XP Media Center Edition?2005 with KB900325 (Windows?XP Media Center Edition?2005) and KB925766 (October 2006 Update Rollup for Windows?XP Media Center Edition) installed.
///
/// bb970346 /// HRESULT IMFAttributes::SetItem([In] const GUID& guidKey,[In] const PROPVARIANT& Value) /// IMFAttributes::SetItem public unsafe void Set(System.Guid guidKey, T value) { // Perform conversions to supported types // int // long // string // byte[] // double // ComObject // Guid if (typeof(T) == typeof(int) || typeof(T) == typeof(bool) || typeof(T) == typeof(byte) || typeof(T) == typeof(uint) || typeof(T) == typeof(short) || typeof(T) == typeof(ushort) || typeof(T) == typeof(byte) || typeof(T) == typeof(sbyte) || typeof(T).GetTypeInfo().IsEnum ) { Set(guidKey, Convert.ToInt32(value)); return; } if (typeof(T) == typeof(long) || typeof(T) == typeof(ulong)) { Set(guidKey, Convert.ToInt64(value)); return; } if (typeof(T) == typeof(IntPtr)) { Set(guidKey, ((IntPtr)(object)value).ToInt64()); return; } if (typeof(T) == typeof(System.Guid)) { Set(guidKey, (System.Guid)(object)value); return; } if (typeof(T) == typeof(string)) { Set(guidKey, value.ToString()); return; } if (typeof(T) == typeof(double) || typeof(T) == typeof(float)) { Set(guidKey, Convert.ToDouble(value)); return; } if (typeof(T) == typeof(byte[])) { var arrayValue = ((byte[])(object)value); fixed (void* pBuffer = arrayValue) SetBlob(guidKey, (IntPtr)pBuffer, arrayValue.Length); return; } if (typeof(T).GetTypeInfo().IsValueType) { SetBlob(guidKey, (IntPtr)Interop.Fixed(ref value), SharpDX.Interop.SizeOf()); return; } if (typeof(T) == typeof(ComObject) || typeof(IUnknown).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo())) { Set(guidKey, ((IUnknown)(object)value)); return; } throw new ArgumentException("The type of the value is not supported"); } /// ///

Applies to: desktop apps | Metro style apps

Adds an attribute value with a specified key.

///
///

A that identifies the value to set. If this key already exists, the method overwrites the old value.

///

A that contains the attribute value. The method copies the value. The type must be one of the types listed in the enumeration.

///

The method returns an . Possible values include, but are not limited to, those in the following table.

Return codeDescription

The method succeeded.

E_OUTOFMEMORY

Insufficient memory.

MF_E_INVALIDTYPE

Invalid attribute type.

?

/// ///

This method checks whether the type is one of the attribute types defined in , and fails if an unsupported type is used. However, this method does not check whether the is the correct type for the specified attribute . (There is no programmatic way to associate attribute GUIDs with property types.) For a list of Media Foundation attributes and their data types, see Media Foundation Attributes.

This interface is available on the following platforms if the Windows Media Format 11 SDK redistributable components are installed:

  • Windows?XP with Service Pack?2 (SP2) and later.
  • Windows?XP Media Center Edition?2005 with KB900325 (Windows?XP Media Center Edition?2005) and KB925766 (October 2006 Update Rollup for Windows?XP Media Center Edition) installed.
///
/// bb970346 /// HRESULT IMFAttributes::SetItem([In] const GUID& guidKey,[In] const PROPVARIANT& Value) /// IMFAttributes::SetItem public unsafe void Set(MediaAttributeKey guidKey, T value) { Set(guidKey.Guid, value); } } }