diff options
Diffstat (limited to 'toxav/toxav_new.h')
-rw-r--r-- | toxav/toxav_new.h | 481 |
1 files changed, 481 insertions, 0 deletions
diff --git a/toxav/toxav_new.h b/toxav/toxav_new.h new file mode 100644 index 00000000..78e79357 --- /dev/null +++ b/toxav/toxav_new.h | |||
@@ -0,0 +1,481 @@ | |||
1 | #pragma once | ||
2 | #include <stdbool.h> | ||
3 | #include <stddef.h> | ||
4 | #include <stdint.h> | ||
5 | /** \page av Public audio/video API for Tox clients. | ||
6 | * | ||
7 | * Unlike the Core API, this API is fully thread-safe. The library will ensure | ||
8 | * the proper synchronisation of parallel calls. | ||
9 | */ | ||
10 | /** | ||
11 | * The type of the Tox Audio/Video subsystem object. | ||
12 | */ | ||
13 | typedef struct toxAV ToxAV; | ||
14 | #ifndef TOX_DEFINED | ||
15 | #define TOX_DEFINED | ||
16 | /** | ||
17 | * The type of a Tox instance. Repeated here so this file does not have a direct | ||
18 | * dependency on the Core interface. | ||
19 | */ | ||
20 | typedef struct Tox Tox; | ||
21 | #endif | ||
22 | /******************************************************************************* | ||
23 | * | ||
24 | * :: Creation and destruction | ||
25 | * | ||
26 | ******************************************************************************/ | ||
27 | typedef enum TOXAV_ERR_NEW { | ||
28 | TOXAV_ERR_NEW_OK, | ||
29 | TOXAV_ERR_NEW_NULL, | ||
30 | /** | ||
31 | * Memory allocation failure while trying to allocate structures required for | ||
32 | * the A/V session. | ||
33 | */ | ||
34 | TOXAV_ERR_NEW_MALLOC, | ||
35 | /** | ||
36 | * Attempted to create a second session for the same Tox instance. | ||
37 | */ | ||
38 | TOXAV_ERR_NEW_MULTIPLE | ||
39 | } TOXAV_ERR_NEW; | ||
40 | /** | ||
41 | * Start new A/V session. There can only be only one session per Tox instance. | ||
42 | */ | ||
43 | ToxAV *toxav_new(Tox *tox, TOXAV_ERR_NEW *error); | ||
44 | /** | ||
45 | * Releases all resources associated with the A/V session. | ||
46 | * | ||
47 | * If any calls were ongoing, these will be forcibly terminated without | ||
48 | * notifying peers. After calling this function, no other functions may be | ||
49 | * called and the av pointer becomes invalid. | ||
50 | */ | ||
51 | void toxav_kill(ToxAV *av); | ||
52 | /** | ||
53 | * Returns the Tox instance the A/V object was created for. | ||
54 | */ | ||
55 | Tox *toxav_get_tox(ToxAV *av); | ||
56 | /******************************************************************************* | ||
57 | * | ||
58 | * :: A/V event loop | ||
59 | * | ||
60 | ******************************************************************************/ | ||
61 | /** | ||
62 | * Returns the interval in milliseconds when the next toxav_iteration should be | ||
63 | * called. If no call is active at the moment, this function returns 200. | ||
64 | */ | ||
65 | uint32_t toxav_iteration_interval(ToxAV const *av); | ||
66 | /** | ||
67 | * Main loop for the session. This function needs to be called in intervals of | ||
68 | * toxav_iteration_interval() milliseconds. It is best called in the same loop | ||
69 | * as tox_iteration. | ||
70 | */ | ||
71 | void toxav_iteration(ToxAV *av); | ||
72 | /******************************************************************************* | ||
73 | * | ||
74 | * :: Call setup | ||
75 | * | ||
76 | ******************************************************************************/ | ||
77 | typedef enum TOXAV_ERR_CALL { | ||
78 | TOXAV_ERR_CALL_OK, | ||
79 | /** | ||
80 | * A resource allocation error occurred while trying to create the structures | ||
81 | * required for the call. | ||
82 | */ | ||
83 | TOXAV_ERR_CALL_MALLOC, | ||
84 | /** | ||
85 | * The friend number did not designate a valid friend. | ||
86 | */ | ||
87 | TOXAV_ERR_CALL_FRIEND_NOT_FOUND, | ||
88 | /** | ||
89 | * The friend was valid, but not currently connected. | ||
90 | */ | ||
91 | TOXAV_ERR_CALL_FRIEND_NOT_CONNECTED, | ||
92 | /** | ||
93 | * Attempted to call a friend while already in an audio or video call with | ||
94 | * them. | ||
95 | */ | ||
96 | TOXAV_ERR_CALL_FRIEND_ALREADY_IN_CALL, | ||
97 | /** | ||
98 | * Audio or video bit rate is invalid. | ||
99 | */ | ||
100 | TOXAV_ERR_CALL_INVALID_BIT_RATE | ||
101 | } TOXAV_ERR_CALL; | ||
102 | /** | ||
103 | * Call a friend. This will start ringing the friend. | ||
104 | * | ||
105 | * It is the client's responsibility to stop ringing after a certain timeout, | ||
106 | * if such behaviour is desired. If the client does not stop ringing, the A/V | ||
107 | * library will not stop until the friend is disconnected. | ||
108 | * | ||
109 | * @param friend_number The friend number of the friend that should be called. | ||
110 | * @param audio_bit_rate Audio bit rate in Kb/sec. Set this to 0 to disable | ||
111 | * audio sending. | ||
112 | * @param video_bit_rate Video bit rate in Kb/sec. Set this to 0 to disable | ||
113 | * video sending. | ||
114 | */ | ||
115 | bool toxav_call(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_CALL *error); | ||
116 | /** | ||
117 | * The function type for the `call` callback. | ||
118 | */ | ||
119 | typedef void toxav_call_cb(ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_enabled, void *user_data); | ||
120 | /** | ||
121 | * Set the callback for the `call` event. Pass NULL to unset. | ||
122 | * | ||
123 | * This event is triggered when a call is received from a friend. | ||
124 | */ | ||
125 | void toxav_callback_call(ToxAV *av, toxav_call_cb *function, void *user_data); | ||
126 | typedef enum TOXAV_ERR_ANSWER { | ||
127 | TOXAV_ERR_ANSWER_OK, | ||
128 | /** | ||
129 | * A resource allocation error occurred while trying to create the structures | ||
130 | * required for the call. | ||
131 | */ | ||
132 | TOXAV_ERR_ANSWER_MALLOC, | ||
133 | /** | ||
134 | * The friend number did not designate a valid friend. | ||
135 | */ | ||
136 | TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND, | ||
137 | /** | ||
138 | * The friend was valid, but they are not currently trying to initiate a call. | ||
139 | * This is also returned if this client is already in a call with the friend. | ||
140 | */ | ||
141 | TOXAV_ERR_ANSWER_FRIEND_NOT_CALLING, | ||
142 | /** | ||
143 | * Audio or video bit rate is invalid. | ||
144 | */ | ||
145 | TOXAV_ERR_ANSWER_INVALID_BIT_RATE | ||
146 | } TOXAV_ERR_ANSWER; | ||
147 | /** | ||
148 | * Accept an incoming call. | ||
149 | * | ||
150 | * If an allocation error occurs while answering a call, both participants will | ||
151 | * receive TOXAV_CALL_STATE_ERROR and the call will end. | ||
152 | * | ||
153 | * @param friend_number The friend number of the friend that is calling. | ||
154 | * @param audio_bit_rate Audio bit rate in Kb/sec. Set this to 0 to disable | ||
155 | * audio sending. | ||
156 | * @param video_bit_rate Video bit rate in Kb/sec. Set this to 0 to disable | ||
157 | * video sending. | ||
158 | */ | ||
159 | bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_ANSWER *error); | ||
160 | /******************************************************************************* | ||
161 | * | ||
162 | * :: Call state graph | ||
163 | * | ||
164 | ******************************************************************************/ | ||
165 | typedef enum TOXAV_CALL_STATE { | ||
166 | /** | ||
167 | * The friend's client is aware of the call. This happens after calling | ||
168 | * toxav_call and the initial call request has been received. | ||
169 | */ | ||
170 | TOXAV_CALL_STATE_RINGING, | ||
171 | /** | ||
172 | * Not sending anything. Either the friend requested that this client stops | ||
173 | * sending anything, or the client turned off both audio and video by setting | ||
174 | * the respective bit rates to 0. | ||
175 | * | ||
176 | * If both sides are in this state, the call is effectively on hold, but not | ||
177 | * in the PAUSED state. | ||
178 | */ | ||
179 | TOXAV_CALL_STATE_NOT_SENDING, | ||
180 | /** | ||
181 | * Sending audio only. Either the friend requested that this client stops | ||
182 | * sending video, or the client turned off video by setting the video bit rate | ||
183 | * to 0. | ||
184 | */ | ||
185 | TOXAV_CALL_STATE_SENDING_A, | ||
186 | /** | ||
187 | * Sending video only. Either the friend requested that this client stops | ||
188 | * sending audio (muted), or the client turned off audio by setting the audio | ||
189 | * bit rate to 0. | ||
190 | */ | ||
191 | TOXAV_CALL_STATE_SENDING_V, | ||
192 | /** | ||
193 | * Sending both audio and video. | ||
194 | */ | ||
195 | TOXAV_CALL_STATE_SENDING_AV, | ||
196 | /** | ||
197 | * The call is on hold. Both sides stop sending and receiving. | ||
198 | */ | ||
199 | TOXAV_CALL_STATE_PAUSED, | ||
200 | /** | ||
201 | * The call has finished. This is the final state after which no more state | ||
202 | * transitions can occur for the call. | ||
203 | */ | ||
204 | TOXAV_CALL_STATE_END, | ||
205 | /** | ||
206 | * Sent by the AV core if an error occurred on the remote end. | ||
207 | */ | ||
208 | TOXAV_CALL_STATE_ERROR | ||
209 | } TOXAV_CALL_STATE; | ||
210 | /** | ||
211 | * The function type for the `call_state` callback. | ||
212 | * | ||
213 | * @param friend_number The friend number for which the call state changed. | ||
214 | * @param state The new call state. | ||
215 | */ | ||
216 | typedef void toxav_call_state_cb(ToxAV *av, uint32_t friend_number, TOXAV_CALL_STATE state, void *user_data); | ||
217 | /** | ||
218 | * Set the callback for the `call_state` event. Pass NULL to unset. | ||
219 | * | ||
220 | * This event is triggered when a call state transition occurs. | ||
221 | */ | ||
222 | void toxav_callback_call_state(ToxAV *av, toxav_call_state_cb *function, void *user_data); | ||
223 | /******************************************************************************* | ||
224 | * | ||
225 | * :: Call control | ||
226 | * | ||
227 | ******************************************************************************/ | ||
228 | typedef enum TOXAV_CALL_CONTROL { | ||
229 | /** | ||
230 | * Resume a previously paused call. Only valid if the pause was caused by this | ||
231 | * client. Not valid before the call is accepted. | ||
232 | */ | ||
233 | TOXAV_CALL_CONTROL_RESUME, | ||
234 | /** | ||
235 | * Put a call on hold. Not valid before the call is accepted. | ||
236 | */ | ||
237 | TOXAV_CALL_CONTROL_PAUSE, | ||
238 | /** | ||
239 | * Reject a call if it was not answered, yet. Cancel a call after it was | ||
240 | * answered. | ||
241 | */ | ||
242 | TOXAV_CALL_CONTROL_CANCEL, | ||
243 | /** | ||
244 | * Request that the friend stops sending audio. Regardless of the friend's | ||
245 | * compliance, this will cause the `receive_audio_frame` event to stop being | ||
246 | * triggered on receiving an audio frame from the friend. | ||
247 | */ | ||
248 | TOXAV_CALL_CONTROL_MUTE_AUDIO, | ||
249 | /** | ||
250 | * Request that the friend stops sending video. Regardless of the friend's | ||
251 | * compliance, this will cause the `receive_video_frame` event to stop being | ||
252 | * triggered on receiving an video frame from the friend. | ||
253 | */ | ||
254 | TOXAV_CALL_CONTROL_MUTE_VIDEO | ||
255 | } TOXAV_CALL_CONTROL; | ||
256 | typedef enum TOXAV_ERR_CALL_CONTROL { | ||
257 | TOXAV_ERR_CALL_CONTROL_OK, | ||
258 | /** | ||
259 | * The friend_number passed did not designate a valid friend. | ||
260 | */ | ||
261 | TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_FOUND, | ||
262 | /** | ||
263 | * This client is currently not in a call with the friend. Before the call is | ||
264 | * answered, only CANCEL is a valid control. | ||
265 | */ | ||
266 | TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL, | ||
267 | /** | ||
268 | * Attempted to resume a call that was not paused. | ||
269 | */ | ||
270 | TOXAV_ERR_CALL_CONTROL_NOT_PAUSED, | ||
271 | /** | ||
272 | * Attempted to resume a call that was paused by the other party. Also set if | ||
273 | * the client attempted to send a system-only control. | ||
274 | */ | ||
275 | TOXAV_ERR_CALL_CONTROL_DENIED, | ||
276 | /** | ||
277 | * The call was already paused on this client. It is valid to pause if the | ||
278 | * other party paused the call. The call will resume after both parties sent | ||
279 | * the RESUME control. | ||
280 | */ | ||
281 | TOXAV_ERR_CALL_CONTROL_ALREADY_PAUSED | ||
282 | } TOXAV_ERR_CALL_CONTROL; | ||
283 | /** | ||
284 | * Sends a call control command to a friend. | ||
285 | * | ||
286 | * @param friend_number The friend number of the friend this client is in a call | ||
287 | * with. | ||
288 | * @param control The control command to send. | ||
289 | * | ||
290 | * @return true on success. | ||
291 | */ | ||
292 | bool toxav_call_control(ToxAV *av, uint32_t friend_number, TOXAV_CALL_CONTROL control, TOXAV_ERR_CALL_CONTROL *error); | ||
293 | /******************************************************************************* | ||
294 | * | ||
295 | * :: Controlling bit rates | ||
296 | * | ||
297 | ******************************************************************************/ | ||
298 | typedef enum TOXAV_ERR_BIT_RATE { | ||
299 | TOXAV_ERR_BIT_RATE_OK, | ||
300 | /** | ||
301 | * The bit rate passed was not one of the supported values. | ||
302 | */ | ||
303 | TOXAV_ERR_BIT_RATE_INVALID | ||
304 | } TOXAV_ERR_BIT_RATE; | ||
305 | /** | ||
306 | * Set the audio bit rate to be used in subsequent audio frames. | ||
307 | * | ||
308 | * @param friend_number The friend number of the friend for which to set the | ||
309 | * audio bit rate. | ||
310 | * @param audio_bit_rate The new audio bit rate in Kb/sec. Set to 0 to disable | ||
311 | * audio sending. | ||
312 | * | ||
313 | * @see toxav_call for the valid bit rates. | ||
314 | */ | ||
315 | bool toxav_set_audio_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, TOXAV_ERR_BIT_RATE *error); | ||
316 | /** | ||
317 | * Set the video bit rate to be used in subsequent video frames. | ||
318 | * | ||
319 | * @param friend_number The friend number of the friend for which to set the | ||
320 | * video bit rate. | ||
321 | * @param video_bit_rate The new video bit rate in Kb/sec. Set to 0 to disable | ||
322 | * video sending. | ||
323 | * | ||
324 | * @see toxav_call for the valid bit rates. | ||
325 | */ | ||
326 | bool toxav_set_video_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t video_bit_rate, TOXAV_ERR_BIT_RATE *error); | ||
327 | /******************************************************************************* | ||
328 | * | ||
329 | * :: A/V sending | ||
330 | * | ||
331 | ******************************************************************************/ | ||
332 | /** | ||
333 | * Common error codes for the send_*_frame functions. | ||
334 | */ | ||
335 | typedef enum TOXAV_ERR_SEND_FRAME { | ||
336 | TOXAV_ERR_SEND_FRAME_OK, | ||
337 | /** | ||
338 | * In case of video, one of Y, U, or V was NULL. In case of audio, the samples | ||
339 | * data pointer was NULL. | ||
340 | */ | ||
341 | TOXAV_ERR_SEND_FRAME_NULL, | ||
342 | /** | ||
343 | * The friend_number passed did not designate a valid friend. | ||
344 | */ | ||
345 | TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND, | ||
346 | /** | ||
347 | * This client is currently not in a call with the friend. | ||
348 | */ | ||
349 | TOXAV_ERR_SEND_FRAME_FRIEND_NOT_IN_CALL, | ||
350 | /** | ||
351 | * No video frame had been requested through the `request_video_frame` event, | ||
352 | * but the client tried to send one, anyway. | ||
353 | */ | ||
354 | TOXAV_ERR_SEND_FRAME_NOT_REQUESTED, | ||
355 | /** | ||
356 | * One of the frame parameters was invalid. E.g. the resolution may be too | ||
357 | * small or too large, or the audio sampling rate may be unsupported. | ||
358 | */ | ||
359 | TOXAV_ERR_SEND_FRAME_INVALID | ||
360 | } TOXAV_ERR_SEND_FRAME; | ||
361 | /** | ||
362 | * The function type for the `request_video_frame` callback. | ||
363 | * | ||
364 | * @param friend_number The friend number of the friend for which the next video | ||
365 | * frame should be sent. | ||
366 | */ | ||
367 | typedef void toxav_request_video_frame_cb(ToxAV *av, uint32_t friend_number, void *user_data); | ||
368 | /** | ||
369 | * Set the callback for the `request_video_frame` event. Pass NULL to unset. | ||
370 | */ | ||
371 | void toxav_callback_request_video_frame(ToxAV *av, toxav_request_video_frame_cb *function, void *user_data); | ||
372 | /** | ||
373 | * Send a video frame to a friend. | ||
374 | * | ||
375 | * This is called in response to receiving the `request_video_frame` event. | ||
376 | * | ||
377 | * Each plane should contain (width * height) pixels. The Alpha plane can be | ||
378 | * NULL, in which case every pixel is assumed fully opaque. | ||
379 | * | ||
380 | * @param friend_number The friend number of the friend to which to send a video | ||
381 | * frame. | ||
382 | * @param width Width of the frame in pixels. | ||
383 | * @param height Height of the frame in pixels. | ||
384 | * @param y Y (Luminance) plane data. | ||
385 | * @param u U (Chroma) plane data. | ||
386 | * @param v V (Chroma) plane data. | ||
387 | * @param a A (Alpha) plane data. | ||
388 | */ | ||
389 | bool toxav_send_video_frame(ToxAV *av, uint32_t friend_number, | ||
390 | uint16_t width, uint16_t height, | ||
391 | uint8_t const *y, uint8_t const *u, uint8_t const *v, uint8_t const *a, | ||
392 | TOXAV_ERR_SEND_FRAME *error); | ||
393 | /** | ||
394 | * The function type for the `request_audio_frame` callback. | ||
395 | * | ||
396 | * @param friend_number The friend number of the friend for which the next audio | ||
397 | * frame should be sent. | ||
398 | */ | ||
399 | typedef void toxav_request_audio_frame_cb(ToxAV *av, uint32_t friend_number, void *user_data); | ||
400 | /** | ||
401 | * Set the callback for the `request_audio_frame` event. Pass NULL to unset. | ||
402 | */ | ||
403 | void toxav_callback_request_audio_frame(ToxAV *av, toxav_request_audio_frame_cb *function, void *user_data); | ||
404 | /** | ||
405 | * Send an audio frame to a friend. | ||
406 | * | ||
407 | * This is called in response to receiving the `request_audio_frame` event. | ||
408 | * | ||
409 | * The expected format of the PCM data is: [s1c1][s1c2][...][s2c1][s2c2][...]... | ||
410 | * Meaning: sample 1 for channel 1, sample 1 for channel 2, ... | ||
411 | * For mono audio, this has no meaning, every sample is subsequent. For stereo, | ||
412 | * this means the expected format is LRLRLR... with samples for left and right | ||
413 | * alternating. | ||
414 | * | ||
415 | * @param friend_number The friend number of the friend to which to send an | ||
416 | * audio frame. | ||
417 | * @param pcm An array of audio samples. The size of this array must be | ||
418 | * sample_count * channels. | ||
419 | * @param sample_count Number of samples in this frame. Valid numbers here are | ||
420 | * ((sample rate) * (audio length) / 1000), where audio length can be | ||
421 | * 2.5, 5, 10, 20, 40 or 60 millseconds. | ||
422 | * @param channels Number of audio channels. Must be at least 1 for mono. | ||
423 | * For voice over IP, more than 2 channels (stereo) typically doesn't make | ||
424 | * sense, but up to 255 channels are supported. | ||
425 | * @param sampling_rate Audio sampling rate used in this frame. Valid sampling | ||
426 | * rates are 8000, 12000, 16000, 24000, or 48000. | ||
427 | */ | ||
428 | bool toxav_send_audio_frame(ToxAV *av, uint32_t friend_number, | ||
429 | int16_t const *pcm, | ||
430 | size_t sample_count, | ||
431 | uint8_t channels, | ||
432 | uint32_t sampling_rate, | ||
433 | TOXAV_ERR_SEND_FRAME *error); | ||
434 | /******************************************************************************* | ||
435 | * | ||
436 | * :: A/V receiving | ||
437 | * | ||
438 | ******************************************************************************/ | ||
439 | /** | ||
440 | * The function type for the `receive_video_frame` callback. | ||
441 | * | ||
442 | * Each plane contains (width * height) pixels. The Alpha plane can be NULL, in | ||
443 | * which case every pixel should be assumed fully opaque. | ||
444 | * | ||
445 | * @param friend_number The friend number of the friend who sent a video frame. | ||
446 | * @param width Width of the frame in pixels. | ||
447 | * @param height Height of the frame in pixels. | ||
448 | * @param y Y (Luminance) plane data. | ||
449 | * @param u U (Chroma) plane data. | ||
450 | * @param v V (Chroma) plane data. | ||
451 | * @param a A (Alpha) plane data. | ||
452 | */ | ||
453 | typedef void toxav_receive_video_frame_cb(ToxAV *av, uint32_t friend_number, | ||
454 | uint16_t width, uint16_t height, | ||
455 | uint8_t const *y, uint8_t const *u, uint8_t const *v, uint8_t const *a, | ||
456 | void *user_data); | ||
457 | /** | ||
458 | * Set the callback for the `receive_video_frame` event. Pass NULL to unset. | ||
459 | */ | ||
460 | void toxav_callback_receive_video_frame(ToxAV *av, toxav_receive_video_frame_cb *function, void *user_data); | ||
461 | /** | ||
462 | * The function type for the `receive_audio_frame` callback. | ||
463 | * | ||
464 | * @param friend_number The friend number of the friend who sent an audio frame. | ||
465 | * @param pcm An array of audio samples (sample_count * channels elements). | ||
466 | * @param sample_count The number of audio samples per channel in the PCM array. | ||
467 | * @param channels Number of audio channels. | ||
468 | * @param sampling_rate Sampling rate used in this frame. | ||
469 | * | ||
470 | * @see toxav_send_audio_frame for the audio format. | ||
471 | */ | ||
472 | typedef void toxav_receive_audio_frame_cb(ToxAV *av, uint32_t friend_number, | ||
473 | int16_t const *pcm, | ||
474 | size_t sample_count, | ||
475 | uint8_t channels, | ||
476 | uint32_t sampling_rate, | ||
477 | void *user_data); | ||
478 | /** | ||
479 | * Set the callback for the `receive_audio_frame` event. Pass NULL to unset. | ||
480 | */ | ||
481 | void toxav_callback_receive_audio_frame(ToxAV *av, toxav_receive_audio_frame_cb *function, void *user_data); \ No newline at end of file | ||