# 平台适配层 (PAL)

Source: [https://docs.qualcomm.com/doc/80-70014-16Y/topic/platform_adaptation_layer_pal.html](https://docs.qualcomm.com/doc/80-70014-16Y/topic/platform_adaptation_layer_pal.html)

平台适配层 (PAL) 提供更高级别的音频特定 API，用于访问底层音频硬件和驱动程序，以实现功能丰富的音频用例。

PulseAudio 对不同的音频用例使用相同的 PAL API。PAL 模块的源代码位于：

build-qcom-wayland/workspace/sources/pal/opensource/arpal-lx

PAL 模块公开的所有 API 都声明位于以下位置：

build-qcom-wayland/workspace/sources/pal/opensource/arpal-lx/inc/PalApi.h

下面介绍了一些常用的 PAL API。

## pal\_init

初始化 PAL，解析相关的配置文件，并将它们存储在本地架构中，以便在用例中使用。

    int32_t pal_init( )
    Copy to clipboard

**参数**

无

**返回值**
- 成功时为 0
- 失败时为错误代码

## pal\_deinit

反初始化 PAL 并释放初始化期间分配的资源。

    void pal_deinit()
    Copy to clipboard

**参数**

无

**返回值**
- 成功时为 0
- 失败时为错误代码

## pal\_stream\_open

使用指定的配置（如Source/Sink设备、媒体配置等）打开流。成功执行时返回流句柄。

    int32_t pal_stream_open(
         struct pal_stream_attributes *attributes, 
         uint32_t no_of_devices, 
         struct pal_device *devices, 
         uint32_t no_of_modifiers, 
         struct modifier_kv *modifiers, 
         pal_stream_callback cb, 
         uint64_t cookie, 
         pal_stream_handle_t **stream_handle)Copy to clipboard

**参数**

| attributes | 从 pal\_stream\_open 获取的有效流属性 |
| --- | --- |
| no\_of\_devices | 最初用于启动流的音频设备数 |
| devices | pal\_devices 的数组。数组的大小基于客户端指定的 no\_of\_devices。 |
| no\_of\_modifiers | 修饰符数量 |
| modifiers | 修饰符的数组。修饰符用于添加其他键值对。 |
| cb | 与流关联的回调函数。任何事件都将通过此回调函数进行通知。 |
| cookie | 与流关联的客户端数据。此 cookie 将在回调函数中返回。 |
| stream\_handle | 如果操作成功，则使用有效的流句柄进行更新。 |

**返回值**
- 成功时为 0
- 失败时为错误代码

## pal\_stream\_start

启动流。

    int32_t pal_stream_start(
         pal_stream_handle_t *stream_handle)Copy to clipboard

**参数**

| stream\_handle | 从 pal\_stream\_open 获取的有效流句柄 |
| --- | --- |

**返回值**
- 成功时为 0
- 失败时为错误代码

## pal\_stream\_read

读取从音频源设备捕获的音频缓冲区。

    ssize_t pal_stream_read(
         pal_stream_handle_t *stream_handle, 
         struct pal_buffer *buf)Copy to clipboard

**参数**

| stream\_handle | 从 pal\_stream\_open 获取的有效流句柄 |
| --- | --- |
| buf | 指向包含音频示例和元数据的 pal\_buffer 指针 |

**返回值**
- 读取的字节数
- 失败时为错误代码

## pal\_stream\_write

写入音频缓冲区，以便通过Sink设备进行流渲染。

    ssize_t pal_stream_write(
         pal_stream_handle_t *stream_handle, 
         struct pal_buffer *buf)Copy to clipboard

**参数**

| stream\_handle | 从 pal\_stream\_open 获取的有效流句柄 |
| --- | --- |
| buf | 指向包含音频示例和元数据的 pal\_buffer 指针 |

**返回值**
- 读取的字节数
- 失败时为错误代码

## pal\_stream\_stop

停止流。

    int32_t pal_stream_stop(
         pal_stream_handle_t *stream_handle)Copy to clipboard

**参数**

| stream\_handle | 从 pal\_stream\_open 获取的有效流句柄 |
| --- | --- |

**返回值**
- 成功时为 0
- 失败时为错误代码

## pal\_stream\_close

关闭流。

    int32_t pal_stream_close(
         pal_stream_handle_t *stream_handle)Copy to clipboard

**参数**

| stream\_handle | 从 pal\_stream\_open 获取的有效流句柄 |
| --- | --- |

**返回值**
- `0` 成功时
- 失败时为错误代码

**Parent Topic:** [API 和示例程序](https://docs.qualcomm.com/doc/80-70014-16Y/topic/apis_and_sample_apps.html)

Last Published: Aug 27, 2024

[Previous Topic
PulseAudio](https://docs.qualcomm.com/bundle/publicresource/80-70014-16Y/topics/pulseaudio.md) [Next Topic
TinyALSA](https://docs.qualcomm.com/bundle/publicresource/80-70014-16Y/topics/tinyalsa_apis.md)