• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Audio Workgroup Management
2
3The audio workgroup is a set of APIs that help the system identify key audio threads within an application through tagging. By providing key audio threads and workgroup runtime information, the system can ensure healthier operation of audio threads.
4
5## How to Use
6
7For audio playback applications, you need to create an audio workgroup first and then periodically inform the system of the workgroup's runtime information. After the work is completed, it is necessary to clean up the audio workgroup.
8
9### Obtaining an AudioResourceManager Instance
10
11Before calling any API of OH_AudioWorkgroup, you must use [OH_AudioManager_GetAudioResourceManager](../../reference/apis-audio-kit/capi-native-audio-resource-manager-h.md#oh_audiomanager_getaudioresourcemanager) to obtain an OH_AudioResourceManager instance.
12
13  ```cpp
14  #include <ohaudio/native_audio_resource_manager.h>
15
16  OH_AudioResourceManager *resMgr;
17  OH_AudioManager_GetAudioResourceManager(&resMgr);
18  ```
19
20### Creating an Audio Workgroup and Adding Key Threads
21
22Call [OH_AudioResourceManager_CreateWorkgroup](../../reference/apis-audio-kit/capi-native-audio-resource-manager-h.md#oh_audioresourcemanager_createworkgroup) to create an audio workgroup, and then call [OH_AudioWorkgroup_AddCurrentThread](../../reference/apis-audio-kit/capi-native-audio-resource-manager-h.md#oh_audioworkgroup_addcurrentthread) to add key threads to the workgroup.
23
24  ```cpp
25  #include <chrono>
26
27  int32_t tokenId;
28  OH_AudioWorkgroup *grp = nullptr;
29
30  OH_AudioResourceManager_CreateWorkgroup(resMgr, "workgroup", &grp);
31  OH_AudioWorkgroup_AddCurrentThread(grp, &tokenId);
32  ```
33
34### Notifying the System of Workgroup Start and End
35
36When an audio workgroup begins a cycle of work, you can notify the system of the task's start time and expected end time. When the workgroup finishes its work for the current cycle, notify the system that the task has ended.
37
38  ```cpp
39  constexpr static uint64_t intervalMs = 20;
40  bool threadShouldRun = true;
41
42  while (threadShouldRun) {
43    auto now = std::chrono::system_clock::now().time_since_epoch();
44    auto startTimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
45
46    OH_AudioWorkgroup_Start(grp, startTimeMs, startTimeMs + intervalMs);
47
48    // Process audio data.
49
50    OH_AudioWorkgroup_Stop(grp);
51  }
52  ```
53
54### Cleanup After Workgroup Task Completion
55
56  ```cpp
57  // Remove the thread from the workgroup when it is no longer required.
58  OH_AudioWorkgroup_RemoveThread(grp, tokenId);
59
60  OH_AudioResourceManager_ReleaseWorkgroup(resMgr, grp);
61  grp = nullptr;
62  ```
63