On this page

C

QAndroidMediaFormat Class

The QAndroidMediaFormat class encapsulates media format settings for video streaming. More...

Header: #include <qandroidmediaformat.h>
CMake: find_package(Qt6 REQUIRED COMPONENTS androidautomotiveservice)
target_link_libraries(mytarget PRIVATE Qt6::androidautomotiveservice)
Since: QtAndroidAutomotive 6.10

Public Types

enum class CodecType { H264, H265 }

Properties

Public Functions

int avcLevel() const
int avcProfile() const
int bitrate() const
QAndroidMediaFormat::CodecType codecType() const
QStringList formatKeys() const
float framerate() const
bool hasFormatKey(const QString &key) const
float iFrameInterval() const
QVariant keyValue(const QString &key, const QVariant &defaultValue = {}) const
float operatingRate() const
void removeFormatKey(const QString &key)
void setAvcLevel(int avcLevel)
void setAvcProfile(int avcProfile)
void setBitrate(int bitrate)
void setCodecType(QAndroidMediaFormat::CodecType codecType)
void setFramerate(float framerate)
void setIFrameInterval(float iFrameInterval)
void setKeyValue(const QString &key, const QVariant &value)
void setOperatingRate(float operatingRate)
void setSize(const QSize &size)
QSize size() const

Detailed Description

QAndroidMediaFormat is used to configure video codec parameters for streaming operations. It allows specifying the video codec type, resolution, bitrate, frame rate, and codec-specific parameters like AVC profile and level.

Example usage:

QAndroidMediaFormat format;
format.setCodecType(QAndroidMediaFormat::CodecType::H264);
format.setSize(QSize(1920, 1080));
format.setBitrate(5000000);  // 5 Mbps
format.setFramerate(30.0f);
format.setAvcProfile(8);     // PROFILE_HIGH
format.setAvcLevel(40);      // LEVEL_4

Generic Format Keys

In addition to the standard properties (codecType(), size(), bitrate(), etc.), QAndroidMediaFormat supports setting arbitrary Android MediaFormat keys through the generic setKeyValue() API. This allows configuring advanced codec parameters that are not exposed through dedicated property methods.

Generic format keys are applied to the underlying Android MediaCodec when the encoder is initialized. Available format keys are defined in Android's MediaFormat class documentation.

Example:

QAndroidMediaFormat format;
// Standard properties
format.setBitrate(8000000);

// Generic format keys for advanced configuration
format.setKeyValue("priority", 1);                     // qint32
format.setKeyValue("max-input-size", qint64(1024000)); // qint64 for sizes
format.setKeyValue("color-range", 2);                  // qint32

Integer Type Selection

Warning: Integer literals in C++ are of type qint32. When setting size values or other parameters that require 64-bit integers, you must explicitly cast to qint64. See setKeyValue() for details.

Override Behavior

Warning: When a generic format key has the same name as a standard property (e.g., "bitrate"), the generic key value takes precedence when configuring the MediaCodec. However, the standard property getter methods continue to return the standard property value, not the generic key value.

Example of override behavior:

format.setBitrate(5000000);
format.setKeyValue("bitrate", 8000000);

format.bitrate();  // Returns 5000000 (standard property value)
// But MediaCodec receives 8000000 (custom key value)

To avoid confusion, it is recommended to use either standard properties or custom keys for a given setting, but not both.

See also QAndroidSurfaceStreamer and setKeyValue().

Member Type Documentation

enum class QAndroidMediaFormat::CodecType

Video codec types for streaming.

ConstantValueDescription
QAndroidMediaFormat::CodecType::H2640H.264 (AVC) video codec.
QAndroidMediaFormat::CodecType::H2651H.265 (HEVC) video codec. Not currently supported.

Property Documentation

avcLevel : int

This property holds the AVC level for H.264 encoding.

Specifies the H.264 level (e.g., AVCLevel31, AVCLevel41, AVCLevel5). Only applicable when codecType is H264.

Access functions:

int avcLevel() const
void setAvcLevel(int avcLevel)

avcProfile : int

This property holds the AVC profile level for H.264 encoding.

Specifies the H.264 profile (e.g., AVCProfileBaseline, AVCProfileMain, AVCProfileHigh). Only applicable when codecType is H264.

Access functions:

int avcProfile() const
void setAvcProfile(int avcProfile)

bitrate : int

This property holds the target bitrate for video encoding in bits per second.

Specifies the target bitrate for video compression. Higher values result in better quality but higher bandwidth requirements.

Access functions:

int bitrate() const
void setBitrate(int bitrate)

codecType : CodecType

This property holds the video codec type used for encoding.

Specifies whether H.264 or H.265 codec should be used for video streaming. Default is H264.

Access functions:

QAndroidMediaFormat::CodecType codecType() const
void setCodecType(QAndroidMediaFormat::CodecType codecType)

framerate : float

This property holds the frame rate of the video format in frames per second.

Specifies the output frame rate for the video stream. This value describes how many frames per second the encoded video will contain. The value can be specified as either an integer or floating-point value. Common values are 24, 25, 30, and 60 fps.

Access functions:

float framerate() const
void setFramerate(float framerate)

See also operatingRate().

iFrameInterval : float

This property holds the interval between I-frames (key frames) in seconds.

Specifies how frequently key frames should be inserted in the video stream. Smaller intervals increase file size but improve stream startup time and error resilience.

Access functions:

float iFrameInterval() const
void setIFrameInterval(float iFrameInterval)

operatingRate : float

This property holds the desired operating frame rate for the codec in frames per second.

Specifies the rate at which the codec should operate, which may differ from the output frame rate specified by framerate(). This is used for resource planning and codec optimization.

This value is a hint to guide codec resource allocation and does not directly affect the encoded output frame rate.

Access functions:

float operatingRate() const
void setOperatingRate(float operatingRate)

See also framerate().

size : QSize

This property holds the video resolution (width and height).

Specifies the output video resolution in pixels.

Access functions:

QSize size() const
void setSize(const QSize &size)

Member Function Documentation

QStringList QAndroidMediaFormat::formatKeys() const

Returns a list of all generic format key names that have been set.

The returned list contains only the keys that were set using setKeyValue(), not the standard property names.

See also setKeyValue(), hasFormatKey(), and removeFormatKey().

bool QAndroidMediaFormat::hasFormatKey(const QString &key) const

Returns true if a generic format key key exists, false otherwise.

This method can be used to check whether a key has been set before attempting to retrieve its value.

See also setKeyValue(), removeFormatKey(), and formatKeys().

QVariant QAndroidMediaFormat::keyValue(const QString &key, const QVariant &defaultValue = {}) const

Returns the value for generic format key key, or defaultValue if the key does not exist.

The returned QVariant contains the value with its original type as it was set using setKeyValue().

Example:

format.setKeyValue("priority", 1);
format.setKeyValue("max-input-size", qint64(1024000));

qint32 priority = format.keyValue("priority").toInt();                // qint32
qint64 maxSize = format.keyValue("max-input-size").toLongLong();      // qint64

See also setKeyValue() and hasFormatKey().

void QAndroidMediaFormat::removeFormatKey(const QString &key)

Removes the generic format key key if it exists.

If the key does not exist, this function does nothing.

See also setKeyValue(), hasFormatKey(), and formatKeys().

void QAndroidMediaFormat::setAvcLevel(int avcLevel)

Sets the H.264 AVC level to avcLevel.

Note: Setter function for property avcLevel.

See also avcLevel().

void QAndroidMediaFormat::setAvcProfile(int avcProfile)

Sets the H.264 AVC profile to avcProfile.

Note: Setter function for property avcProfile.

See also avcProfile().

void QAndroidMediaFormat::setBitrate(int bitrate)

Sets the target bitrate (in bits per second) to bitrate.

Note: Setter function for property bitrate.

See also bitrate().

void QAndroidMediaFormat::setCodecType(QAndroidMediaFormat::CodecType codecType)

Sets the video codec type to codecType.

Note: Setter function for property codecType.

See also codecType().

void QAndroidMediaFormat::setFramerate(float framerate)

Sets the video frame rate (in frames per second) to framerate.

Note: Setter function for property framerate.

See also framerate().

void QAndroidMediaFormat::setIFrameInterval(float iFrameInterval)

Sets the interval between I-frames (in seconds) to iFrameInterval.

Note: Setter function for property iFrameInterval.

See also iFrameInterval().

void QAndroidMediaFormat::setKeyValue(const QString &key, const QVariant &value)

Sets a generic format key key with value.

Generic format keys allow configuring Android MediaFormat parameters that are not exposed through dedicated properties. The value is stored as a QVariant and its type determines how it will be applied to the Android MediaCodec.

Format key names are defined in Android's MediaFormat class documentation.

Supported Types

The following types are supported:

  • qint32 - 32-bit integers (e.g., priority, color-format)
  • qint64 - 64-bit integers and size values (e.g., max-input-size, duration)
  • float - Single-precision floating-point
  • double - Double-precision floating-point
  • QString - String values (e.g., MIME types)
  • QByteArray - Binary buffers (e.g., codec-specific data)
  • QRect - Rectangle values (e.g., crop rectangles)

Integer Type Selection

Warning: Integer literals in C++ are of type qint32. When setting size values or other parameters that require 64-bit integers, you must explicitly cast to qint64:

// Wrong - literal is qint32
format.setKeyValue("max-input-size", 1024000);

// Correct - explicit qint64 for size values
format.setKeyValue("max-input-size", qint64(1024000));

// Small values use qint32
format.setKeyValue("priority", 1);
format.setKeyValue("color-range", 2);

Common keys requiring qint64:

  • max-input-size - Maximum input buffer size
  • duration - Duration in microseconds

Warning: Generic format keys with the same name as standard properties will override those properties when the MediaCodec is configured. See the class documentation for details on override behavior.

See also keyValue(), hasFormatKey(), and removeFormatKey().

void QAndroidMediaFormat::setOperatingRate(float operatingRate)

Sets the operating rate hint for the hardware encoder to operatingRate.

Note: Setter function for property operatingRate.

See also operatingRate().

void QAndroidMediaFormat::setSize(const QSize &size)

Sets the video resolution to size.

Note: Setter function for property size.

See also size().

Available under certain Qt licenses.
Find out more.