# Hardware abstraction layer

The normal work of SDK depends on the hardware and software interfaces provided by the hardware platform, including the microphone, loudspeaker, camera, encryption library and other modules. We design a hardware abstraction layer to provide a consistent interface definition, and the specific interface implementation needs to be provided by the access side of the SDK.

# 1.Module

Header file:wmpf / module.h.

We abstract each module into awx_moduletype. There are currently several modules:

type Header file Introductions
wx_audio_module wmpf/hardware/audio.h Audio module for input / output audio stream from microphones and speakers
wx_camera_module wmpf/hardware/camera.h Camera module for obtaining video streaming from camera
wx_crypto_module wmpf/crypto.h The signature algorithm module. The SDK relies on the signature algorithm in the system's cryptographic library.
wx_video_module wmpf/hardware/video.h Video module for creating remote video streaming

When these Modules are implemented on demand, when you callwx_init (]]to initialize the SDK, the Module is provided to the SDK using the following function pointer:

typedef wx_error_t (*wx_get_module_t)(const char* id,
                                      struct wx_module** module);

When the SDK calls the implemented function, it passes in an ID that needs to return an instance ofwx_modulefor this ID.

parameter

attribute type Introductions
id char* ModuleID, defined in the following
module struct wx_module** The corresponding module instance

Currently supported module IDs are predefined in each header file:

ID Header file
WX_AUDIO_MODULE_ID wmpf/hardware/audio.h
WX_CAMERA_MODULE_ID wmpf/hardware/camera.h
WX_CRYPTO_MODULE_ID wmpf/crypto.h
WX_VIDEO_MODULE_ID wmpf/hardware/video.h

Return value

The wx_error_tSDK uses this return value to determine whether the requested module is valid.

For example, if a device does not have a camera, then when the SDK requests WX_CAMERA_MODULE_ID, it simply returnsWXERROR_UNIMPLEMENTED.

Error code Introductions
WXERROR_OK The requested module is valid.
WXERROR_UNIMPLEMENTED The requested module is invalid.

sample code

// Suppose you have implemented the following module:
extern struct wx_audio_module audio_module;
extern struct wx_camera_module camera_module;
extern struct wx_crypto_module crypto_module;
// The function of HAL getting Module is as follows:
wx_error_t hal_get_module(const char* id, struct wx_module** module_out) {
  if (!strcmp(id, WX_AUDIO_MODULE_ID)) {
    *module_out = (struct wx_module*)&audio_module;
    return WXERROR_OK;
  } else if (!strcmp(id, WX_CAMERA_MODULE_ID)) {
    *module_out = (struct wx_module*)&camera_module;
    return WXERROR_OK;
  } else if (!strcmp(id, WX_CRYPTO_MODULE_ID)) {
    *module_out = (struct wx_module*)&crypto_module;
    return WXERROR_OK;
  }
  // 假设当前设备没有屏幕,因此不需提供 wx_video_module,那么就返回 WXERROR_UNIMPLEMENTED
  return WXERROR_UNIMPLEMENTED;
}

# 2. Audio Module

Header file:wmpf / hardware / audio.h.

Audio module, you need to configure an instance of typewx_audio_module.This type mainly consists of several function pointers used to obtain information about the audio input / output device, or to operate the audio device.

At runtime, VoIP will call these four functions sequentially to complete the configuration of the audio input / output device.

# 2.1get_number_of_devices

Gets the number of audio input-output devices of the specified type.

wx_error_t (*get_number_of_devices)(struct wx_audio_module* module,
                                    wx_audio_device_type_t device_type,
                                    size_t* num_devices_out);

parameter

attribute type Introductions
module struct wx_audio_module* context
device_type wx_audio_device_type_t 设备类型,可选值

WX_AUDIO_DEVICE_IN(音频输入设备,如麦克风)

WX_AUDIO_DEVICE_OUT(音频输出设备,如扬声器)
num_devices_out size_t* Returns the number of audio devices of the corresponding type

Return value

Wx_error_tSDK determines whether the resulting number of devices is valid based on this return value

Error code Introductions
WXERROR_OK success
WXERROR_INVALID_ARGUMENT Num_devices_out is empty

# 2.2get_device_name

Queries for the ID of the index device in the list of audio input / output devices of the specified type (e.g., WX_AUDIO_DEVICE_IN for microphone)

wx_error_t (*get_device_name)(struct wx_audio_module* module,
                              size_t index,
                              wx_audio_device_type_t device_type,
                              char** device_name_out);

parameter

attribute type Introductions
module struct wx_audio_module* context
index size_t The device specified in the device list.Value range\ [0, num_devices_out)
device_type wx_audio_device_type_t Device type, same asget_number_of_devices
device_name_out char** Returns the ID of the corresponding device

Return value

Wx_error_tSDK determines whether the resulting device ID is valid based on this return value

Error code Introductions
WXERROR_OK success
WXERROR_INVALID_ARGUMENT Num_devices_out is empty
WXERROR_OUT_OF_RANGE Index out of range

# 2.3get_device_info

Queries the details of a device that specifies the type and device ID. This function is used to return some audio device information that can be obtained without opening the audio device, such as the supported audio encoding format.

wx_error_t (*get_device_info)(struct wx_audio_module* module,
                              const char* id,
                              wx_audio_device_type_t device_type,
                              const struct wx_metadata** metadata_out);

parameter

attribute type Introductions
module struct wx_audio_module* context
id const char* Get_device_nameThe device ID found, or the macroWX_AUDIO_DEVICE_PRIMARYindicates any valid device.
device_type wx_audio_device_type_t Same asget_number_of_devicesdevice_type
metadata_out const struct wx_metadata** Return details of the corresponding device. The object pointed to by a secondary pointer can be a local variable.

Return value

Wx_error_tSDK determines whether the resulting device details are valid based on this return value

Error code Introductions
WXERROR_OK success

# 2.4open

Invokes the specified audio input / output device.

wx_error_t (*open)(struct wx_audio_module* module,
                   const char* id,
                   wx_audio_device_type_t device_type,
                   struct wx_audio_device** device_out);

parameter

attribute type Introductions
module struct wx_audio_module* context
id const char* id withget_device_info``
device_type wx_audio_device_type_t Same asget_number_of_devicesdevice_type
device_out struct wx_audio_device** Returns the corresponding device instance.Seewx_audio_deviceType Dxplaination

Return value

Wx_error_tSDK determines whether the resulting device instance is valid based on this return value

Error code Introductions
WXERROR_OK success

# 2.5wx_audio_device

attribute type Introductions
common wx_device A common base class of device types.
metadata wx_metadata Descriptive information about the device.

Struct wx_audio_deviceitself does not have much function, mainly for the description of the device.What really needs to be focused on are its two extension types:wx_audio_device_inandwx_audio_device_out

# 2.5.1wx_audio_device_in

Abstraction of input devices (e.g. microphones).The key to this type is to provide theopen_input_streamfunction implementation, which the SDK will call at runtime to create an audio input stream object [[]]wx_audio_stream_in。The SDK further controls audio capture using an instance of thewx_audio_stream_inobject.

Microphone devices are created when a VoIP call is dialed and destroyed when a VoIP call is over.

attribute type Introductions
device wx_audio_device Base classes.
open_input_stream Functional pointer The SDK uses this function pointer to turn on / use the microphone device.
# Parameters for open_input_stream
parameter type Introductions
dev wx_audio_device_in * context
config wx_audio_config * Microphone configuration desired by the SDK.
listener wx_audio_stream_in_listener * The SDK listens for a callback function for microphone events. Developers need to call these functions correctly at the right time.
user_data void * Other context information that the SDK relies on. You need to bring this pointer when you call the microphone event callback function.
stream_out wx_audio_stream_in ** Returns the newly created audio input stream object. The definition is as follows.
# wx_audio_stream_in
parameter type Introductions
common wx_audio_stream * context
pause Functional pointer The SDK calls this function to pause audio data input.
resume Functional pointer The SDK calls this function to restore audio data input.
# wx_audio_stream_in_listener
parameter type Introductions
common wx_struct Base classes
data Functional pointer The system calls this function to provide the audio data collected by the microphone to the SDK.
error Functional pointer By calling this function, the system informs the SDK that the microphone is collecting audio data in error.

NOTE:

  1. The SDK currently only collects audio data in PCM format, and other audio formats may be added in the future.
  2. Config is currently configured with a sampling rate of 16000, a sampling length of 20ms, and 16 bits by default, and may be dynamically adjusted. If the device does not support these audio configurations, the device manufacturer should transform the PCM data themselves.
  3. Data callbacks can be understood as non-blocking, that is, network-independent.
  4. The data callback provides one frame of audio data that needs to be provided according to the parameters set by theconfig.For example: PCM sampling rate 16000, sampling format 16Bit, sampling length 20ms single channel audio data, including16000 / 1000*20*1*(16 / 8)Data in bytes (sampling rate / 1000*sampling length*] channels * sampling format / 8Bit [).

# 2.5.2 wx_audio_device_out type

Abstraction of a loudspeaker device.The key to this type is to provide an implementation of theopen_output_streamfunction that the runtime SDK will call to create an audio output stream objectwx_audio_stream_out。The SDK further controls audio playback using an instance of thewx_audio_stream_outobject.

A loudspeaker device is created at the beginning of a VoIP call, plays the ringtone first, starts playing the voice at the beginning of the VoIP call, and is destroyed at the end of the VoIP call.

attribute type Introductions
device wx_audio_device Base classes.
open_output_stream Functional pointer The SDK uses this function pointer to open / use the speaker device.
# Parameters for open_output_stream:
parameter type Introductions
dev wx_audio_device_out * context
config wx_audio_config * The speaker configuration desired by the SDK.
listener wx_audio_stream_out_listener * The SDK listens for a callback function for speaker events. These functions should be called correctly at the right time.
user_data void * Other context information that the SDK relies on. You need to bring this pointer when you call the speaker event callback function.
stream_out wx_audio_stream_out ** Returns the newly created audio output stream object. The definition is as follows.
# Wx_audio_stream_outType
parameter type Introductions
common wx_audio_stream * context
pause Functional pointer The SDK calls this function to pause audio data playback.
resume Functional pointer The SDK calls this function to restore audio data playback.
flush Functional pointer The SDK calls this function to request the audio playback cache and immediately plays the audio data in the cache.
# Wx_audio_stream_out_listenerType
parameter type Introductions
common wx_struct Base classes
data Functional pointer By calling this function, the system pulls audio data from the SDK to be played (a ringtone or a received voice stream).
error Functional pointer The system notifies the SDK of an audio playback error by calling this function.

NOTE:

  1. The SDK currently only provides audio data in PCM format, and other audio formats may be added in the future.
  2. Config is currently configured with a sampling rate of 16000, a sampling length of 20ms, and 16 bits by default, and may be dynamically adjusted. If the device does not support these audio configurations, the device manufacturer should transform the PCM data themselves.
  3. Data callbacks can be understood as non-blocking, that is, network-independent.
  4. The buffer passed in by the data callback needs to be large enough to hold a frame of audio data set by the config. For example: PCM sampling rate 16000, sampling format 16Bit, sampling length 20ms single-channel audio data, containing 16000 / 1000_20_1_(16 / 8) bytes of data (sampling rate / 1000_sampling length_channel number_sampling format / 8Bit).

# 3. Signature Algorithm Module

Header file:wmpf / crypto.h.

The SDK relies on the system's built-in signature algorithm, and we provide Demo implementations of OpenSSL, MbedTLS, and WolfSSL in the example directory. If you use the above crypto implementation, you can usually directly use the example code implementation, otherwise refer to the example implementation for adaptation.

Demo implementation:

  • example/crypto_mbedtls.c
  • example/crypto_openssl.c
  • example/crypto_wolfssl.c

# 4. (Optional) Camera module

Header file:wmpf / hardware / camera.h.

Camera module, you need to configure an instance of typewx_camera_module.This type mainly requires you to provide the following function pointers to generate a video stream sent by the device to the phone using the camera device object provided by the device

At runtime, the SDK will call these three functions sequentially to complete the configuration of the camera device.

# 4.1get_number_of_devices

Number of camera devices obtained.

wx_error_t (*get_number_of_devices)(struct wx_camera_module* module,
                                    size_t* num_devices_out);

parameter

attribute type Introductions
module struct wx_camera_module* context
num_devices_out size_t* Return the number of camera devices of the corresponding type

Return value

Wx_error_tSDK determines whether the resulting number of devices is valid based on this return value

Error code Introductions
WXERROR_OK success
WXERROR_INVALID_ARGUMENT Num_devices_out is empty

# 4.2get_device_info

Query for details of the specified camera device.

wx_error_t (*get_device_info)(struct wx_camera_module* module,
                              size_t index,
                              struct wx_camera_device_info* device_info);

parameter

attribute type Introductions
module struct wx_camera_module* context
index size_t The device specified in the device list.Value range\ [0, num_devices_out)
device_info struct wx_camera_device_info* The SDK passes in this device_info, which needs to fill in the conformation.

Return value

Wx_error_tSDK determines whether the resulting device details are valid based on this return value

Error code Introductions
WXERROR_OK The requested device details are valid.

# 4.3open

Call the specified camera device.

wx_error_t (*open)(struct wx_camera_module* module,
                   const char* id,
                   struct wx_camera_device** device_out);

parameter

attribute type Introductions
module struct wx_camera_module* context
id char * Get_device_infoThe device ID found, or the macroWX_CAMERA_DEVICE_PRIMARYrepresents any valid device.
device_out struct wx_camera_device ** Returns the corresponding device instance.

Return value

Wx_error_tSDK determines whether the resulting device instance is valid based on this return value

Error code Introductions
WXERROR_OK success

# 4.4wx_camera_device

Abstraction of camera devices.The key to this type is the need to provide an implementation of theopen_streamfunction, which the runtime SDK will call to create a video input stream objectwx_camera_stream。The SDK further controls video capture using an instance of thewx_camera_streamobject.

attribute type Introductions
common wx_device A common base class of device types.
metadata wx_metadata Descriptive information about the device.
open_stream Functional pointer The SDK uses this function pointer to open / use the camera device.

Parameters for open_stream:

parameter type Introductions
dev wx_camera_device * context
config wx_camera_stream_config * The SDK requires the camera configuration, including the data flow type.
listener wx_camera_stream_listener * The SDK listens to the callback function for the Camera event. These functions should be called correctly at the right time.
user_data void * Other context information that the SDK relies on. You need to bring this pointer when you call the camera event callback function.
stream_out wx_camera_stream ** Returns the newly created video input stream object. The definition is as follows.

# Wx_camera_stream type

parameter type Introductions
common wx_struct Base classes
update Functional pointer The SDK calls this function to update the camera recording parameters.
make_i_frame Functional pointer The SDK calls this function to request that an H264 / H265 I frame be received immediately.

# Wx_camera_stream_listener type

parameter type Introductions
common wx_struct Base classes
data Functional pointer The system calls this function to provide the video data collected by the camera to the SDK.
error Functional pointer The system calls this function to inform the SDK camera capture video data error.

NOTE:

  1. The SDK currently only collects video data in H264 / H265 format.
  2. Data callbacks can be understood as non-blocking, that is, network-independent.
  3. The device needs to provide PPS, SPS, I frames on a regular basis (1 second recommended, maximum 5 seconds).
  4. Device incoming rotation is only valid on Linux SDK 0xD5000084 and above, WeChat Guest 8.0.41 and above,For older WeChat releases, call the set UIConfig] of the VoIP plug-in on the Weixin Mini Program side to set the angle of swing.

# 5. (Optional) Video Module

Header file:wmpf / hardware / video.h.

Camera module, you need to configure an instance of typewx_video_module.

At runtime, the SDK will call thecreate_output_streamfunction to create a video output stream that outputs the received video recorded by the remote (phone) camera through the stream object.

# 5.1create_output_stream

Create a video output stream.

wx_error_t (*create_output_stream)(struct wx_video_module* module,
                                   const struct wx_video_stream_config* config,
                                   struct wx_video_stream** stream);

parameter

attribute type Introductions
module struct wx_video_module* context
config const struct wx_video_stream_config* Video output streaming parameters
stream struct wx_video_stream** Returns a video output stream object

Return value

Wx_error_tSDK determines whether the video output stream is valid based on this return value

Error code Introductions
WXERROR_OK success

# 5.2wx_video_stream

parameter type Introductions
common wx_struct Base classes
write Functional pointer The SDK calls this function to write to the video output stream.
close Functional pointer The SDK calls this function to turn off the video output stream.