Voice recording messages

Last modified by Sylvain Berfini on 2022/01/06 15:09

This feature has been added in 5.1.0 release of the SDK.

How to use it

First of all, you need a recorder to record the voice message. To create one, you need some parameters:

/**
* Create a recorder params that will hold parameters.
* This recorder support WAVE and MATROSKA formats.
* @param lc A #LinphoneCore object @notnil
* @return A pointer on the newly created instance. @notnil
*/

LINPHONE_PUBLIC LinphoneRecorderParams *linphone_core_create_recorder_params(const LinphoneCore *lc);

The main parameter you will have to set is the format. MKV format (with Opus audio encoding) has the advantage of creating lighter files but WAV format will be more easily played by other device if you choose to export it.

Once configured, create a LinphoneRecorder:

/**
* Create a media file recorder.
* This recorder support WAVE and MATROSKA formats.
* @param lc A #LinphoneCore object. @notnil
* @param params The #LinphoneRecorderParams that will contains all recorder parameters. @notnil
* @return A pointer on the new instance. NULL if failed. @notnil
*/

LINPHONE_PUBLIC LinphoneRecorder *linphone_core_create_recorder(LinphoneCore *lc, LinphoneRecorderParams *params);

Set the path to where you want the voice message to be recorded and start the recording:

/**
 * Open a file for recording.
 * @param recorder The #LinphoneRecorder object. @notnil
 * @param file The path to the file to open. @notnil
 */

LINPHONE_PUBLIC LinphoneStatus linphone_recorder_open(LinphoneRecorder *recorder, const char *file);

/**
 * Start the recording into the opened file.
 * @param recorder The #LinphoneRecorder object. @notnil
 */

LINPHONE_PUBLIC LinphoneStatus linphone_recorder_start(LinphoneRecorder *recorder);

Don't forget to close the recorder when you have finished!

/**
 * Close the opened file.
 * @param recorder The #LinphoneRecorder object. @notnil
 */

LINPHONE_PUBLIC void linphone_recorder_close(LinphoneRecorder *recorder);

Once your message has been recorded, you can create the LinphoneChatMessage from your LinphoneChatRoom:

 /**
 * Creates a chat message with a voice recording attached to the given chat room.
 * @warning If the recorder isn't in Closed state, it will return an empty message!
 * @param chat_room the #LinphoneChatRoom object. @notnil
 * @param recorder the #LinphoneRecorder object used to record the voice message. @notnil
 * @return a new #LinphoneChatMessage @notnil
 */

LINPHONE_PUBLIC LinphoneChatMessage *linphone_chat_room_create_voice_recording_message (LinphoneChatRoom *chat_room, LinphoneRecorder *recorder);

And because it is a chat message like any other, you can add other contents like text or files, respond to/with it, forward it, etc...

Requirements

The voice recording will be sent out as any chat message with a file attachment, so you must have configured a file transfer server URL.

How it works

We use a specific content-type to distinguish a voice recording sent by Linphone than a simple audio file transfer: "audio/wav;voice-recording=yes".
This way the app knows that it must display it as a voice recording.
And in our SDK that allows us to automatically download it to improve the user-experience, but you can disable it if you don't want to:

/**
 * Auto download files attach to a chat message if it's content type matches the one we use for voice recordings.
 * @param core #LinphoneCore object @notnil
 * @param auto_download_voice_recordings TRUE to automatically download incoming voice recordings, FALSE to disable it.
 * @ingroup chat
**/

LINPHONE_PUBLIC void linphone_core_enable_auto_download_voice_recordings(LinphoneCore *core, bool_t auto_download_voice_recordings);

We also add a new entry in the file transfer XML description, following RCS specs (RCC.07-v11.0, section 3.2.7.1 Audio Messaging): "<am:playing-length></am:playing-length>" that contains the duration of the voice message.