| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 |
* You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#ifndef AUDIO_THREAD_PRIORITY_H |
| 6 |
#define AUDIO_THREAD_PRIORITY_H |
| 7 |
|
| 8 |
#include <stdint.h> |
| 9 |
#include <stdlib.h> |
| 10 |
|
| 11 |
/** |
| 12 |
* An opaque structure containing information about a thread that was promoted |
| 13 |
* to real-time priority. |
| 14 |
*/ |
| 15 |
struct atp_handle; |
| 16 |
struct atp_thread_info; |
| 17 |
extern size_t ATP_THREAD_INFO_SIZE; |
| 18 |
|
| 19 |
#ifdef __cplusplus |
| 20 |
extern "C" { |
| 21 |
#endif // __cplusplus |
| 22 |
|
| 23 |
/** |
| 24 |
* Promotes the current thread to real-time priority. |
| 25 |
* |
| 26 |
* audio_buffer_frames: number of frames per audio buffer. If unknown, passing 0 |
| 27 |
* will choose an appropriate number, conservatively. If variable, either pass 0 |
| 28 |
* or an upper bound. |
| 29 |
* audio_samplerate_hz: sample-rate for this audio stream, in Hz |
| 30 |
* |
| 31 |
* Returns an opaque handle in case of success, NULL otherwise. |
| 32 |
*/ |
| 33 |
atp_handle *atp_promote_current_thread_to_real_time(uint32_t audio_buffer_frames, |
| 34 |
uint32_t audio_samplerate_hz); |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Demotes the current thread promoted to real-time priority via |
| 39 |
* `atp_demote_current_thread_from_real_time` to its previous priority. |
| 40 |
* |
| 41 |
* Returns 0 in case of success, non-zero otherwise. |
| 42 |
*/ |
| 43 |
int32_t atp_demote_current_thread_from_real_time(atp_handle *handle); |
| 44 |
|
| 45 |
/** |
| 46 |
* Frees an atp_handle. This is useful when it impractical to call |
| 47 |
*`atp_demote_current_thread_from_real_time` on the right thread. Access to the |
| 48 |
* handle must be synchronized externaly (or the related thread must have |
| 49 |
* exited). |
| 50 |
* |
| 51 |
* Returns 0 in case of success, non-zero otherwise. |
| 52 |
*/ |
| 53 |
int32_t atp_free_handle(atp_handle *handle); |
| 54 |
|
| 55 |
/* |
| 56 |
* Linux-only API. |
| 57 |
* |
| 58 |
* The Linux backend uses DBUS to promote a thread to real-time priority. In |
| 59 |
* environment where this is not possible (due to sandboxing), this set of |
| 60 |
* functions allow remoting the call to a process that can make DBUS calls. |
| 61 |
* |
| 62 |
* To do so: |
| 63 |
* - Set the real-time limit from within the process where a |
| 64 |
* thread will be promoted. This is a `setrlimit` call, that can be done |
| 65 |
* before the sandbox lockdown. |
| 66 |
* - Then, gather information on the thread that will be promoted. |
| 67 |
* - Serialize this info. |
| 68 |
* - Send over the serialized data via an IPC mechanism |
| 69 |
* - Deserialize the inf |
| 70 |
* - Call `atp_promote_thread_to_real_time` |
| 71 |
*/ |
| 72 |
|
| 73 |
#ifdef __linux__ |
| 74 |
/** |
| 75 |
* Promotes a thread, possibly in another process, to real-time priority. |
| 76 |
* |
| 77 |
* thread_info: info on the thread to promote, gathered with |
| 78 |
* `atp_get_current_thread_info()`, called on the thread itself. |
| 79 |
* audio_buffer_frames: number of frames per audio buffer. If unknown, passing 0 |
| 80 |
* will choose an appropriate number, conservatively. If variable, either pass 0 |
| 81 |
* or an upper bound. |
| 82 |
* audio_samplerate_hz: sample-rate for this audio stream, in Hz |
| 83 |
* |
| 84 |
* Returns an opaque handle in case of success, NULL otherwise. |
| 85 |
* |
| 86 |
* This call is useful on Linux desktop only, when the process is sandboxed and |
| 87 |
* cannot promote itself directly. |
| 88 |
*/ |
| 89 |
atp_handle *atp_promote_thread_to_real_time(atp_thread_info *thread_info); |
| 90 |
|
| 91 |
/** |
| 92 |
* Demotes a thread promoted to real-time priority via |
| 93 |
* `atp_demote_thread_from_real_time` to its previous priority. |
| 94 |
* |
| 95 |
* Returns 0 in case of success, non-zero otherwise. |
| 96 |
* |
| 97 |
* This call is useful on Linux desktop only, when the process is sandboxed and |
| 98 |
* cannot promote itself directly. |
| 99 |
*/ |
| 100 |
int32_t atp_demote_thread_from_real_time(atp_thread_info* thread_info); |
| 101 |
|
| 102 |
/** |
| 103 |
* Gather informations from the calling thread, to be able to promote it from |
| 104 |
* another thread and/or process. |
| 105 |
* |
| 106 |
* Returns a non-null pointer to an `atp_thread_info` structure in case of |
| 107 |
* sucess, to be freed later with `atp_free_thread_info`, and NULL otherwise. |
| 108 |
* |
| 109 |
* This call is useful on Linux desktop only, when the process is sandboxed and |
| 110 |
* cannot promote itself directly. |
| 111 |
*/ |
| 112 |
atp_thread_info *atp_get_current_thread_info(); |
| 113 |
|
| 114 |
/** |
| 115 |
* Free an `atp_thread_info` structure. |
| 116 |
* |
| 117 |
* Returns 0 in case of success, non-zero in case of error (because thread_info |
| 118 |
* was NULL). |
| 119 |
*/ |
| 120 |
int32_t atp_free_thread_info(atp_thread_info *thread_info); |
| 121 |
|
| 122 |
/** |
| 123 |
* Serialize an `atp_thread_info` to a byte buffer that is |
| 124 |
* sizeof(atp_thread_info) long. |
| 125 |
*/ |
| 126 |
void atp_serialize_thread_info(atp_thread_info *thread_info, uint8_t *bytes); |
| 127 |
|
| 128 |
/** |
| 129 |
* Deserialize a byte buffer of sizeof(atp_thread_info) to an `atp_thread_info` |
| 130 |
* pointer. It can be then freed using atp_free_thread_info. |
| 131 |
* */ |
| 132 |
atp_thread_info* atp_deserialize_thread_info(uint8_t *bytes); |
| 133 |
|
| 134 |
/** |
| 135 |
* Set real-time limit for the calling process. |
| 136 |
* |
| 137 |
* This is useful only on Linux desktop, and allows remoting the rtkit DBUS call |
| 138 |
* to a process that has access to DBUS. This function has to be called before |
| 139 |
* attempting to promote threads from another process. |
| 140 |
* |
| 141 |
* This sets the real-time computation limit. For actually promoting the thread |
| 142 |
* to a real-time scheduling class, see `atp_promote_thread_to_real_time`. |
| 143 |
*/ |
| 144 |
int32_t atp_set_real_time_limit(uint32_t audio_buffer_frames, |
| 145 |
uint32_t audio_samplerate_hz); |
| 146 |
|
| 147 |
#endif // __linux__ |
| 148 |
|
| 149 |
#ifdef __cplusplus |
| 150 |
} // extern "C" |
| 151 |
#endif // __cplusplus |
| 152 |
|
| 153 |
#endif // AUDIO_THREAD_PRIORITY_H |