C
QAndroidSurfaceStreamer Class
This class provides H.264 video streaming from Android surfaces over RTSP. More...
| Header: | #include <QAndroidSurfaceStreamer> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS androidautomotiveservice)target_link_libraries(mytarget PRIVATE Qt6::androidautomotiveservice) |
| Since: | QtAndroidAutomotive 6.10 |
| Inherits: | QObject |
Public Types
| enum class | Error { NoError, RtspPortFailed, StreamPortFailed, EncoderFailed, InternalError } |
Properties
- running : bool
Public Functions
| QAndroidSurfaceStreamer(QAndroidMediaFormat mediaFormat, QObject *parent = nullptr) | |
| virtual | ~QAndroidSurfaceStreamer() override |
| bool | isRunning() const |
| QAndroidSurfaceStreamer::Error | lastError() const |
| QAndroidMediaFormat | mediaFormat() const |
| bool | start(const QHostAddress &host, quint16 port) |
| void | stop() |
| ANativeWindow * | surface() const |
Signals
| void | error(QAndroidSurfaceStreamer::Error error, const QString &details = QString()) |
| void | runningChanged(bool running) |
Static Public Members
| QString | errorString(QAndroidSurfaceStreamer::Error error) |
Detailed Description
QAndroidSurfaceStreamer enables real-time video streaming from Android native surfaces using the H.264 codec and RTSP protocol. It manages the complete streaming pipeline including video encoding, RTP packet generation, and RTSP server management.
The class automatically handles:
- Initialization of the Android MediaCodec H.264 encoder
- RTP packet generation for H.264 video data
- RTSP session and server management
- Error propagation and recovery
- Graceful shutdown of all streaming components
To use QAndroidSurfaceStreamer, create an instance with the desired media format and call start() to begin streaming on a specific host and port:
QAndroidMediaFormat format; format.setWidth(1920); format.setHeight(1080); QAndroidSurfaceStreamer streamer(format); connect(&streamer, QOverload<QAndroidSurfaceStreamer::Error, const QString &>::of(&QAndroidSurfaceStreamer::error), this, &MyClass::onStreamingError); if (!streamer.start(QHostAddress::LocalHost, 5554)) { qWarning() << "Failed to start streaming:" << streamer.errorString(streamer.lastError()); } ANativeWindow *surface = streamer.surface(); // Use surface with Android graphics APIs... streamer.stop(); // Stop streaming when done
The streaming data can be accessed via the surface() method, which returns an ANativeWindow pointer that can be used with Android graphics APIs.
See also QAndroidMediaFormat.
Member Type Documentation
enum class QAndroidSurfaceStreamer::Error
Error codes that can occur during streaming operations.
| Constant | Value | Description |
|---|---|---|
QAndroidSurfaceStreamer::Error::NoError | 0 | No error occurred. |
QAndroidSurfaceStreamer::Error::RtspPortFailed | 1 | The RTSP server failed to start, typically because the specified port is unavailable or the application lacks required network permissions. |
QAndroidSurfaceStreamer::Error::StreamPortFailed | 2 | Failed to allocate UDP ports for RTP streaming. This may occur if the system is unable to bind to the requested ports or if no suitable port range is available. |
QAndroidSurfaceStreamer::Error::EncoderFailed | 3 | The H.264 video encoder failed to initialize. This may indicate the device does not support H.264 encoding, or the specified format parameters are not supported. |
QAndroidSurfaceStreamer::Error::InternalError | 4 | An internal error occurred during streaming. Check application logs for more details. |
See also error(), lastError(), and errorString().
Property Documentation
[read-only] running : bool
This property holds whether the streaming server is currently running.
This property holds true when the streaming server is actively running and false when it is stopped. The property is read-only; use start() and stop() to control the streaming state.
Access functions:
| bool | isRunning() const |
Notifier signal:
| void | runningChanged(bool running) |
See also start(), stop(), and runningChanged().
Member Function Documentation
[explicit] QAndroidSurfaceStreamer::QAndroidSurfaceStreamer(QAndroidMediaFormat mediaFormat, QObject *parent = nullptr)
Constructs a QAndroidSurfaceStreamer object with the given mediaFormat and the specified parent.
The media format specifies the video resolution, frame rate, and other encoding parameters. Call start() to initialize the encoder and begin streaming.
See also mediaFormat() and start().
[override virtual noexcept] QAndroidSurfaceStreamer::~QAndroidSurfaceStreamer()
Destroys the QAndroidSurfaceStreamer object. If the streamer is currently running, stop() is called automatically to ensure all resources are properly released.
See also stop().
[signal] void QAndroidSurfaceStreamer::error(QAndroidSurfaceStreamer::Error error, const QString &details = QString())
This signal is emitted when an error occurs during streaming operations.
The error parameter contains the error code indicating the type of error that occurred. The details parameter provides additional information about the error and may include session identifiers or other debugging information.
Common errors include:
- Error::RtspPortFailed - The RTSP server port is not available
- Error::StreamPortFailed - UDP ports for RTP streaming could not be allocated
- Error::EncoderFailed - The H.264 encoder failed to initialize
- Error::InternalError - An internal error occurred
When an error signal is emitted, the streamer is automatically stopped and the running property is set to false.
See also lastError(), errorString(), and runningChanged().
[static] QString QAndroidSurfaceStreamer::errorString(QAndroidSurfaceStreamer::Error error)
Returns a human-readable error message for the given error error.
This static function provides descriptive text for each Error code, suitable for displaying to users or logging. The returned string is localization-ready.
See also Error, lastError(), and error().
bool QAndroidSurfaceStreamer::isRunning() const
Returns true if the streaming server is currently running; otherwise returns false.
Note: Getter function for property running.
See also start(), stop(), running, and runningChanged().
QAndroidSurfaceStreamer::Error QAndroidSurfaceStreamer::lastError() const
Returns the last error that occurred during streaming operations.
When an error occurs during streaming (e.g., encoder failure, port allocation failure), this method returns the error code. Detailed error information is also provided via the error() signal.
See also error() and errorString().
QAndroidMediaFormat QAndroidSurfaceStreamer::mediaFormat() const
Returns the media format that was specified when constructing the streamer.
The returned format contains the video resolution, frame rate, and other encoding parameters used by the H.264 encoder.
See also QAndroidMediaFormat.
[signal] void QAndroidSurfaceStreamer::runningChanged(bool running)
This signal is emitted when the streaming server is started or stopped.
The running parameter is true when the server has been successfully started, and false when the server has been stopped or failed to start.
Note: Notifier signal for property running.
See also start(), stop(), isRunning(), and running.
bool QAndroidSurfaceStreamer::start(const QHostAddress &host, quint16 port)
Starts the streaming server on the specified host address and port.
This method initializes the H.264 encoder, creates the RTP packet source, and starts the RTSP server. All streaming components run in a dedicated worker thread.
Returns true if the streaming was started successfully; otherwise returns false and sets lastError() to indicate the reason for failure.
After successful start, the running property is set to true and the runningChanged() signal is emitted. The surface() method can then be used to obtain the video surface.
If the streamer is already running, this method returns false and does nothing.
host may be QHostAddress::LocalHost to accept connections only from the local machine, or QHostAddress::Any to accept connections from any network interface.
port is the RTSP server port. The method will also attempt to allocate UDP ports starting from port + 1 for RTP data streams.
Note: Errors that occur during streaming are reported via the error() signal, not the return value of this function.
See also stop(), isRunning(), surface(), error(), and lastError().
void QAndroidSurfaceStreamer::stop()
Stops the streaming server and releases all associated resources.
This method performs a graceful shutdown of all streaming components:
- Stops the RTSP server
- Stops the RTP packet source and H.264 encoder
- Terminates the worker thread and waits for it to finish
- Releases all allocated resources (ports, memory, etc.)
If the streamer is not running, this method does nothing.
After successful stop, the running property is set to false and the runningChanged() signal is emitted.
The method waits up to 5 seconds for the worker thread to finish. If the thread does not terminate within this timeout, it is left to finish naturally to avoid potential crashes.
See also start(), isRunning(), and running.
ANativeWindow *QAndroidSurfaceStreamer::surface() const
Returns a pointer to the native Android surface (ANativeWindow) used for video encoding.
The surface is created and managed by the H.264 RTP packet source. Video frames should be rendered to this surface using Android graphics APIs such as OpenGL ES or Vulkan.
Returns nullptr if the streamer is not running or if the encoder has not yet been initialized.
Note: The returned pointer is owned by the QAndroidSurfaceStreamer and must not be freed by the caller. The surface is automatically released when stop() is called.
Available under certain Qt licenses.
Find out more.