• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Concurrently Creating a Video Decoder and Initializing NativeWindow
2
3## When to Use
4
5To 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.
6
7
8## How to Develop
9
10The 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.
11
12**Linking Dynamic Libraries**
13
14``` cmake
15target_link_libraries(sample PUBLIC libnative_image.so)
16target_link_libraries(sample PUBLIC libnative_window.so)
17target_link_libraries(sample PUBLIC libnative_buffer.so)
18target_link_libraries(sample PUBLIC libnative_media_vdec.so)
19```
20
21> **NOTE**
22>
23> The word **sample** in the preceding code snippet is only an example. Use the actual project directory name.
24>
25
26**Including Header Files**
27
28```c++
29#include <iostream>
30#include <string>
31#include <native_image/native_image.h>
32#include <native_window/external_window.h>
33#include <native_buffer/native_buffer.h>
34#include <multimedia/player_framework/native_avcodec_videodecoder.h>
35```
36
371. Create an OH_NativeImage instance.
38
39    ```c++
40    // Create a NativeImage instance as the surface consumer.
41    OH_NativeImage* image = OH_ConsumerSurface_Create();
42    ```
43
442. Obtain the NativeWindow instance that functions as the producer.
45
46    ```c++
47    // Obtain a NativeWindow instance.
48    OHNativeWindow* nativeImageWindow = OH_NativeImage_AcquireNativeWindow(image);
49    ```
50
513. Set the width and height of the NativeWindow instance.
52
53    ```c++
54    int code = SET_BUFFER_GEOMETRY;
55    int32_t width = 800;
56    int32_t height = 600;
57    int32_t ret = OH_NativeWindow_NativeWindowHandleOpt(nativeImageWindow, code, width, height);
58    if (ret != AV_ERR_OK) {
59        // Handle exceptions.
60    }
61    ```
62
634. Register a callback function for the NativeImage instance.
64
65    Register the **OH_OnFrameAvailableListener**, which contains the following parameters:
66
67    - **context**: user-defined context information.
68    - **onFrameAvailable**: callback function triggered when a frame is available.
69
70    ```c++
71    // Implement onFrameAvailable.
72    static void onFrameAvailable()
73    {
74      OHNativeWindowBuffer *buffer = nullptr;
75      int fenceFd;
76      // Obtain an OHNativeWindowBuffer instance through the OH_NativeImage instance on the consumer side.
77      OH_NativeImage_AcquireNativeWindowBuffer(image, &buffer, &fenceFd);
78      // Release the OHNativeWindowBuffer instance through the OH_NativeImage instance.
79      OH_NativeImage_ReleaseNativeWindowBuffer(image, &buffer, &fenceFd);
80    }
81
82    static void context()
83    {
84      // Customize the context information.
85    }
86
87    // Set a listener.
88    OH_OnFrameAvailableListener listener = {&onFrameAvailable, &context};
89    // Register the listener to listen for frame availability events.
90    ret = OH_NativeImage_SetOnFrameAvailableListener(image, listener);
91    if (ret != AV_ERR_OK) {
92        // Handle exceptions.
93    }
94    ```
95
96    > **NOTE**
97    >
98    > In this example, the callback function just retrieves and releases the buffer. You can customize and expand the callback function based on service requirements.
99    >
100
1015. Configure the decoder.
102
103    For details, see step 5 in [Video Decoding in Surface Mode](video-decoding.md#surface-output).
104
1056. Set the surface.
106
107    Before the actual surface consumer is created, you can use the temporarily created consumer to connect to the decoder.
108
109    In the code snippet below, the following variables are used:
110    - **videoDec**: pointer to the video decoder instance. For details, see step 2 in [Video Decoding Surface Mode](video-decoding.md#surface-output).
111
112    ```c++
113
114    ret = OH_VideoDecoder_SetSurface(videoDec, nativeImageWindow);
115    if (ret != AV_ERR_OK) {
116        // Handle exceptions.
117    }
118    ```
119
1207. Start the decoder.
121
122    For details, see step 9 in [Video Decoding Surface Mode](video-decoding.md#surface-output).
123
124
1258. Set the surface.
126
127    After the actual surface consumer is created, call **OH_VideoDecoder_SetSurface** to redirect the decoded output to the new surface.
128
129    You can obtain NativeWindow in either of the following ways:
130    1. If the image is directly displayed after being decoded, obtain NativeWindow from the **XComponent**. For details about the operation, see [XComponent](../../reference/apis-arkui/arkui-ts/ts-basic-components-xcomponent.md).
131    2. If OpenGL post-processing is performed after decoding, obtain NativeWindow from NativeImage. For details about the operation, see [NativeImage](../../graphics/native-image-guidelines.md).
132
133    ```c++
134
135    ret = OH_VideoDecoder_SetSurface(videoDec, nativeWindow);
136    if (ret != AV_ERR_OK) {
137        // Handle exceptions.
138    }
139    ```
140
1419. Destroy the OH_NativeImage instance.
142
143   After calling **OH_VideoDecoder_Destroy**, call **OH_NativeImage_Destroy** to destroy the OH_NativeImage instance.
144   ```c++
145   // Destroy the OH_NativeImage instance.
146   OH_NativeImage_Destroy(&image);
147   ```
148