harmony 鸿蒙Concurrently Creating a Video Decoder and Initializing NativeWindow
Concurrently Creating a Video Decoder and Initializing NativeWindow
When to Use
To ensure that a video decoder can be created and run properly in surface mode, you can create an empty surface before the XComponent is created or the OpenGL post-processing (NativeImage) is initialized.
How to Develop
The following describes how to concurrently create a video decoder and initialize NativeWindow. This approach ensures that the video decoder can be set up and executed properly, even before the surface consumer is ready.
Linking Dynamic Libraries
target_link_libraries(sample PUBLIC libnative_image.so)
target_link_libraries(sample PUBLIC libnative_window.so)
target_link_libraries(sample PUBLIC libnative_buffer.so)
target_link_libraries(sample PUBLIC libnative_media_vdec.so)
NOTE
The word sample in the preceding code snippet is only an example. Use the actual project directory name.
Including Header Files
#include <iostream>
#include <string>
#include <native_image/native_image.h>
#include <native_window/external_window.h>
#include <native_buffer/native_buffer.h>
#include <multimedia/player_framework/native_avcodec_videodecoder.h>
Create an OH_NativeImage instance.
// Create a NativeImage instance as the surface consumer. OH_NativeImage* image = OH_ConsumerSurface_Create();
Obtain the NativeWindow instance that functions as the producer.
// Obtain a NativeWindow instance. OHNativeWindow* nativeImageWindow = OH_NativeImage_AcquireNativeWindow(image);
Set the width and height of the NativeWindow instance.
int code = SET_BUFFER_GEOMETRY; int32_t width = 800; int32_t height = 600; int32_t ret = OH_NativeWindow_NativeWindowHandleOpt(nativeImageWindow, code, width, height); if (ret != AV_ERR_OK) { // Handle exceptions. }
Register a callback function for the NativeImage instance.
Register the OH_OnFrameAvailableListener, which contains the following parameters:
- context: user-defined context information.
- onFrameAvailable: callback function triggered when a frame is available.
// Implement onFrameAvailable. static void onFrameAvailable() { OHNativeWindowBuffer *buffer = nullptr; int fenceFd; // Obtain an OHNativeWindowBuffer instance through the OH_NativeImage instance on the consumer side. OH_NativeImage_AcquireNativeWindowBuffer(image, &buffer, &fenceFd); // Release the OHNativeWindowBuffer instance through the OH_NativeImage instance. OH_NativeImage_ReleaseNativeWindowBuffer(image, &buffer, &fenceFd); } static void context() { // Customize the context information. } // Set a listener. OH_OnFrameAvailableListener listener = {&onFrameAvailable, &context}; // Register the listener to listen for frame availability events. ret = OH_NativeImage_SetOnFrameAvailableListener(image, listener); if (ret != AV_ERR_OK) { // Handle exceptions. }
NOTE
In this example, the callback function just retrieves and releases the buffer. You can customize and expand the callback function based on service requirements.
Configure the decoder.
For details, see step 5 in Video Decoding in Surface Mode.
Set the surface.
Before the actual surface consumer is created, you can use the temporarily created consumer to connect to the decoder.
In the code snippet below, the following variables are used:
- videoDec: pointer to the video decoder instance. For details, see step 2 in Video Decoding Surface Mode.
ret = OH_VideoDecoder_SetSurface(videoDec, nativeImageWindow); if (ret != AV_ERR_OK) { // Handle exceptions. }
Start the decoder.
For details, see step 8 in Video Decoding Surface Mode.
Set the surface.
After the actual surface consumer is created, call OH_VideoDecoder_SetSurface to redirect the decoded output to the new surface.
You can obtain NativeWindow in either of the following ways:
- If the image is directly displayed after being decoded, obtain NativeWindow from the XComponent. For details about the operation, see XComponent.
- If OpenGL post-processing is performed after decoding, obtain NativeWindow from NativeImage. For details about the operation, see NativeImage.
ret = OH_VideoDecoder_SetSurface(videoDec, nativeWindow); if (ret != AV_ERR_OK) { // Handle exceptions. }
Destroy the OH_NativeImage instance.
After calling OH_VideoDecoder_Destroy, call OH_NativeImage_Destroy to destroy the OH_NativeImage instance.
// Destroy the OH_NativeImage instance.
OH_NativeImage_Destroy(&image);
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Media Data Demultiplexing
harmony 鸿蒙Media Data Multiplexing
harmony 鸿蒙Introduction to AVCodec Kit
harmony 鸿蒙AVCodec Supported Formats
harmony 鸿蒙Obtaining Supported Codecs
harmony 鸿蒙Video Encoding Configurations for Typical Scenarios
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦