diff options
Diffstat (limited to 'toxav/toxav.h')
-rw-r--r-- | toxav/toxav.h | 765 |
1 files changed, 587 insertions, 178 deletions
diff --git a/toxav/toxav.h b/toxav/toxav.h index 7285f45c..5c5195b3 100644 --- a/toxav/toxav.h +++ b/toxav/toxav.h | |||
@@ -1,285 +1,695 @@ | |||
1 | /** toxav.h | 1 | /* toxav.h |
2 | * | ||
3 | * Copyright (C) 2013-2015 Tox project All Rights Reserved. | ||
2 | * | 4 | * |
3 | * Copyright (C) 2013 Tox project All Rights Reserved. | 5 | * This file is part of Tox. |
4 | * | 6 | * |
5 | * This file is part of Tox. | 7 | * Tox is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or | ||
10 | * (at your option) any later version. | ||
6 | * | 11 | * |
7 | * Tox is free software: you can redistribute it and/or modify | 12 | * Tox is distributed in the hope that it will be useful, |
8 | * it under the terms of the GNU General Public License as published by | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | * the Free Software Foundation, either version 3 of the License, or | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | * (at your option) any later version. | 15 | * GNU General Public License for more details. |
11 | * | ||
12 | * Tox is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | 16 | * |
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | */ | 20 | */ |
21 | 21 | ||
22 | #ifndef TOXAV_H | ||
23 | #define TOXAV_H | ||
22 | 24 | ||
23 | #ifndef __TOXAV | 25 | #include <stdbool.h> |
24 | #define __TOXAV | 26 | #include <stddef.h> |
25 | #include <inttypes.h> | 27 | #include <stdint.h> |
26 | 28 | ||
27 | #ifdef __cplusplus | 29 | #ifdef __cplusplus |
28 | extern "C" { | 30 | extern "C" { |
29 | #endif | 31 | #endif |
30 | 32 | ||
31 | typedef struct _ToxAv ToxAv; | 33 | /** \page av Public audio/video API for Tox clients. |
32 | 34 | * | |
33 | /* vpx_image_t */ | 35 | * This API can handle multiple calls. Each call has its state, in very rare |
34 | #include <vpx/vpx_image.h> | 36 | * occasions the library can change the state of the call without apps knowledge. |
35 | 37 | * | |
36 | typedef void ( *ToxAVCallback ) ( void *agent, int32_t call_idx, void *arg ); | 38 | */ |
37 | typedef void ( *ToxAvAudioCallback ) (void *agent, int32_t call_idx, const int16_t *PCM, uint16_t size, void *data); | 39 | /** \subsection events Events and callbacks |
38 | typedef void ( *ToxAvVideoCallback ) (void *agent, int32_t call_idx, const vpx_image_t *img, void *data); | 40 | * |
39 | 41 | * As in Core API, events are handled by callbacks. One callback can be | |
42 | * registered per event. All events have a callback function type named | ||
43 | * `toxav_{event}_cb` and a function to register it named `toxav_callback_{event}`. | ||
44 | * Passing a NULL callback will result in no callback being registered for that | ||
45 | * event. Only one callback per event can be registered, so if a client needs | ||
46 | * multiple event listeners, it needs to implement the dispatch functionality | ||
47 | * itself. Unlike Core API, lack of some event handlers will cause the the | ||
48 | * library to drop calls before they are started. Hanging up call from a | ||
49 | * callback causes undefined behaviour. | ||
50 | * | ||
51 | */ | ||
52 | /** \subsection threading Threading implications | ||
53 | * | ||
54 | * Unlike the Core API, this API is fully thread-safe. The library will ensure | ||
55 | * the proper synchronization of parallel calls. | ||
56 | * | ||
57 | * A common way to run ToxAV (multiple or single instance) is to have a thread, | ||
58 | * separate from tox instance thread, running a simple toxav_iterate loop, | ||
59 | * sleeping for toxav_iteration_interval * milliseconds on each iteration. | ||
60 | * | ||
61 | * An important thing to note is that events are triggered from both tox and | ||
62 | * toxav thread (see above). Audio and video receive frame events are triggered | ||
63 | * from toxav thread while all the other events are triggered from tox thread. | ||
64 | * | ||
65 | * Tox thread has priority with mutex mechanisms. Any api function can | ||
66 | * fail if mutexes are held by tox thread in which case they will set SYNC | ||
67 | * error code. | ||
68 | */ | ||
69 | /** | ||
70 | * External Tox type. | ||
71 | */ | ||
40 | #ifndef TOX_DEFINED | 72 | #ifndef TOX_DEFINED |
41 | #define TOX_DEFINED | 73 | #define TOX_DEFINED |
42 | typedef struct Tox Tox; | 74 | typedef struct Tox Tox; |
43 | #endif | 75 | #endif /* TOX_DEFINED */ |
44 | |||
45 | #define RTP_PAYLOAD_SIZE 65535 | ||
46 | |||
47 | 76 | ||
48 | /** | 77 | /** |
49 | * Callbacks ids that handle the call states. | 78 | * ToxAV. |
50 | */ | 79 | */ |
51 | typedef enum { | ||
52 | av_OnInvite, /* Incoming call */ | ||
53 | av_OnRinging, /* When peer is ready to accept/reject the call */ | ||
54 | av_OnStart, /* Call (RTP transmission) started */ | ||
55 | av_OnCancel, /* The side that initiated call canceled invite */ | ||
56 | av_OnReject, /* The side that was invited rejected the call */ | ||
57 | av_OnEnd, /* Call that was active ended */ | ||
58 | av_OnRequestTimeout, /* When the requested action didn't get response in specified time */ | ||
59 | av_OnPeerTimeout, /* Peer timed out; stop the call */ | ||
60 | av_OnPeerCSChange, /* Peer changing Csettings. Prepare for changed AV */ | ||
61 | av_OnSelfCSChange /* Csettings change confirmation. Once triggered peer is ready to recv changed AV */ | ||
62 | } ToxAvCallbackID; | ||
63 | |||
64 | |||
65 | /** | 80 | /** |
66 | * Call type identifier. | 81 | * The ToxAV instance type. Each ToxAV instance can be bound to only one Tox |
82 | * instance, and Tox instance can have only one ToxAV instance. One must make | ||
83 | * sure to close ToxAV instance prior closing Tox instance otherwise undefined | ||
84 | * behaviour occurs. Upon closing of ToxAV instance, all active calls will be | ||
85 | * forcibly terminated without notifying peers. | ||
86 | * | ||
67 | */ | 87 | */ |
68 | typedef enum { | 88 | #ifndef TOXAV_DEFINED |
69 | av_TypeAudio = 192, | 89 | #define TOXAV_DEFINED |
70 | av_TypeVideo | 90 | typedef struct ToxAV ToxAV; |
71 | } ToxAvCallType; | 91 | #endif /* TOXAV_DEFINED */ |
72 | |||
73 | 92 | ||
74 | typedef enum { | ||
75 | av_CallNonExistent = -1, | ||
76 | av_CallInviting, /* when sending call invite */ | ||
77 | av_CallStarting, /* when getting call invite */ | ||
78 | av_CallActive, | ||
79 | av_CallHold, | ||
80 | av_CallHungUp | ||
81 | } ToxAvCallState; | ||
82 | 93 | ||
94 | /******************************************************************************* | ||
95 | * | ||
96 | * :: API version | ||
97 | * | ||
98 | ******************************************************************************/ | ||
83 | /** | 99 | /** |
84 | * Error indicators. Values under -20 are reserved for toxcore. | 100 | * The major version number. Incremented when the API or ABI changes in an |
101 | * incompatible way. | ||
85 | */ | 102 | */ |
86 | typedef enum { | 103 | #define TOXAV_VERSION_MAJOR 0u |
87 | av_ErrorNone = 0, | ||
88 | av_ErrorUnknown = -1, /* Unknown error */ | ||
89 | av_ErrorNoCall = -20, /* Trying to perform call action while not in a call */ | ||
90 | av_ErrorInvalidState = -21, /* Trying to perform call action while in invalid state*/ | ||
91 | av_ErrorAlreadyInCallWithPeer = -22, /* Trying to call peer when already in a call with peer */ | ||
92 | av_ErrorReachedCallLimit = -23, /* Cannot handle more calls */ | ||
93 | av_ErrorInitializingCodecs = -30, /* Failed creating CSSession */ | ||
94 | av_ErrorSettingVideoResolution = -31, /* Error setting resolution */ | ||
95 | av_ErrorSettingVideoBitrate = -32, /* Error setting bitrate */ | ||
96 | av_ErrorSplittingVideoPayload = -33, /* Error splitting video payload */ | ||
97 | av_ErrorEncodingVideo = -34, /* vpx_codec_encode failed */ | ||
98 | av_ErrorEncodingAudio = -35, /* opus_encode failed */ | ||
99 | av_ErrorSendingPayload = -40, /* Sending lossy packet failed */ | ||
100 | av_ErrorCreatingRtpSessions = -41, /* One of the rtp sessions failed to initialize */ | ||
101 | av_ErrorNoRtpSession = -50, /* Trying to perform rtp action on invalid session */ | ||
102 | av_ErrorInvalidCodecState = -51, /* Codec state not initialized */ | ||
103 | av_ErrorPacketTooLarge = -52, /* Split packet exceeds it's limit */ | ||
104 | } ToxAvError; | ||
105 | |||
106 | 104 | ||
107 | /** | 105 | /** |
108 | * Locally supported capabilities. | 106 | * The minor version number. Incremented when functionality is added without |
107 | * breaking the API or ABI. Set to 0 when the major version number is | ||
108 | * incremented. | ||
109 | */ | 109 | */ |
110 | typedef enum { | 110 | #define TOXAV_VERSION_MINOR 0u |
111 | av_AudioEncoding = 1 << 0, | ||
112 | av_AudioDecoding = 1 << 1, | ||
113 | av_VideoEncoding = 1 << 2, | ||
114 | av_VideoDecoding = 1 << 3 | ||
115 | } ToxAvCapabilities; | ||
116 | |||
117 | 111 | ||
118 | /** | 112 | /** |
119 | * Encoding settings. | 113 | * The patch or revision number. Incremented when bugfixes are applied without |
114 | * changing any functionality or API or ABI. | ||
120 | */ | 115 | */ |
121 | typedef struct _ToxAvCSettings { | 116 | #define TOXAV_VERSION_PATCH 0u |
122 | ToxAvCallType call_type; | ||
123 | |||
124 | uint32_t video_bitrate; /* In kbits/s */ | ||
125 | uint16_t max_video_width; /* In px */ | ||
126 | uint16_t max_video_height; /* In px */ | ||
127 | |||
128 | uint32_t audio_bitrate; /* In bits/s */ | ||
129 | uint16_t audio_frame_duration; /* In ms */ | ||
130 | uint32_t audio_sample_rate; /* In Hz */ | ||
131 | uint32_t audio_channels; | ||
132 | } ToxAvCSettings; | ||
133 | |||
134 | extern const ToxAvCSettings av_DefaultSettings; | ||
135 | 117 | ||
136 | /** | 118 | /** |
137 | * Start new A/V session. There can only be one session at the time. | 119 | * A macro to check at preprocessing time whether the client code is compatible |
120 | * with the installed version of ToxAV. | ||
138 | */ | 121 | */ |
139 | ToxAv *toxav_new(Tox *messenger, int32_t max_calls); | 122 | #define TOXAV_VERSION_IS_API_COMPATIBLE(MAJOR, MINOR, PATCH) \ |
123 | (TOXAV_VERSION_MAJOR == MAJOR && \ | ||
124 | (TOXAV_VERSION_MINOR > MINOR || \ | ||
125 | (TOXAV_VERSION_MINOR == MINOR && \ | ||
126 | TOXAV_VERSION_PATCH >= PATCH))) | ||
140 | 127 | ||
141 | /** | 128 | /** |
142 | * Remove A/V session. | 129 | * A macro to make compilation fail if the client code is not compatible with |
130 | * the installed version of ToxAV. | ||
143 | */ | 131 | */ |
144 | void toxav_kill(ToxAv *av); | 132 | #define TOXAV_VERSION_REQUIRE(MAJOR, MINOR, PATCH) \ |
133 | typedef char toxav_required_version[TOXAV_IS_COMPATIBLE(MAJOR, MINOR, PATCH) ? 1 : -1] | ||
145 | 134 | ||
146 | /** | 135 | /** |
147 | * Returns the interval in milliseconds when the next toxav_do() should be called. | 136 | * A convenience macro to call toxav_version_is_compatible with the currently |
148 | * If no call is active at the moment returns 200. | 137 | * compiling API version. |
149 | */ | 138 | */ |
150 | uint32_t toxav_do_interval(ToxAv *av); | 139 | #define TOXAV_VERSION_IS_ABI_COMPATIBLE() \ |
140 | toxav_version_is_compatible(TOXAV_VERSION_MAJOR, TOXAV_VERSION_MINOR, TOXAV_VERSION_PATCH) | ||
151 | 141 | ||
152 | /** | 142 | /** |
153 | * Main loop for the session. Best called right after tox_do(); | 143 | * Return the major version number of the library. Can be used to display the |
144 | * ToxAV library version or to check whether the client is compatible with the | ||
145 | * dynamically linked version of ToxAV. | ||
154 | */ | 146 | */ |
155 | void toxav_do(ToxAv *av); | 147 | uint32_t toxav_version_major(void); |
156 | 148 | ||
157 | /** | 149 | /** |
158 | * Register callback for call state. | 150 | * Return the minor version number of the library. |
159 | */ | 151 | */ |
160 | void toxav_register_callstate_callback (ToxAv *av, ToxAVCallback cb, ToxAvCallbackID id, void *userdata); | 152 | uint32_t toxav_version_minor(void); |
161 | 153 | ||
162 | /** | 154 | /** |
163 | * Register callback for audio data. | 155 | * Return the patch number of the library. |
164 | */ | 156 | */ |
165 | void toxav_register_audio_callback (ToxAv *av, ToxAvAudioCallback cb, void *userdata); | 157 | uint32_t toxav_version_patch(void); |
166 | 158 | ||
167 | /** | 159 | /** |
168 | * Register callback for video data. | 160 | * Return whether the compiled library version is compatible with the passed |
161 | * version numbers. | ||
169 | */ | 162 | */ |
170 | void toxav_register_video_callback (ToxAv *av, ToxAvVideoCallback cb, void *userdata); | 163 | bool toxav_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch); |
164 | |||
165 | |||
166 | /******************************************************************************* | ||
167 | * | ||
168 | * :: Creation and destruction | ||
169 | * | ||
170 | ******************************************************************************/ | ||
171 | typedef enum TOXAV_ERR_NEW { | ||
172 | /** | ||
173 | * The function returned successfully. | ||
174 | */ | ||
175 | TOXAV_ERR_NEW_OK, | ||
176 | /** | ||
177 | * One of the arguments to the function was NULL when it was not expected. | ||
178 | */ | ||
179 | TOXAV_ERR_NEW_NULL, | ||
180 | /** | ||
181 | * Memory allocation failure while trying to allocate structures required for | ||
182 | * the A/V session. | ||
183 | */ | ||
184 | TOXAV_ERR_NEW_MALLOC, | ||
185 | /** | ||
186 | * Attempted to create a second session for the same Tox instance. | ||
187 | */ | ||
188 | TOXAV_ERR_NEW_MULTIPLE, | ||
189 | } TOXAV_ERR_NEW; | ||
171 | 190 | ||
172 | /** | 191 | /** |
173 | * Call user. Use its friend_id. | 192 | * Start new A/V session. There can only be only one session per Tox instance. |
174 | */ | 193 | */ |
175 | int toxav_call(ToxAv *av, | 194 | ToxAV *toxav_new(Tox *tox, TOXAV_ERR_NEW *error); |
176 | int32_t *call_index, | ||
177 | int friend_id, | ||
178 | const ToxAvCSettings *csettings, | ||
179 | int ringing_seconds); | ||
180 | 195 | ||
181 | /** | 196 | /** |
182 | * Hangup active call. | 197 | * Releases all resources associated with the A/V session. |
198 | * | ||
199 | * If any calls were ongoing, these will be forcibly terminated without | ||
200 | * notifying peers. After calling this function, no other functions may be | ||
201 | * called and the av pointer becomes invalid. | ||
183 | */ | 202 | */ |
184 | int toxav_hangup(ToxAv *av, int32_t call_index); | 203 | void toxav_kill(ToxAV *toxAV); |
185 | 204 | ||
186 | /** | 205 | /** |
187 | * Answer incoming call. Pass the csettings that you will use. | 206 | * Returns the Tox instance the A/V object was created for. |
188 | */ | 207 | */ |
189 | int toxav_answer(ToxAv *av, int32_t call_index, const ToxAvCSettings *csettings ); | 208 | Tox *toxav_get_tox(const ToxAV *toxAV); |
209 | |||
190 | 210 | ||
211 | /******************************************************************************* | ||
212 | * | ||
213 | * :: A/V event loop | ||
214 | * | ||
215 | ******************************************************************************/ | ||
191 | /** | 216 | /** |
192 | * Reject incoming call. | 217 | * Returns the interval in milliseconds when the next toxav_iterate call should |
218 | * be. If no call is active at the moment, this function returns 200. | ||
193 | */ | 219 | */ |
194 | int toxav_reject(ToxAv *av, int32_t call_index, const char *reason); | 220 | uint32_t toxav_iteration_interval(const ToxAV *toxAV); |
195 | 221 | ||
196 | /** | 222 | /** |
197 | * Cancel outgoing request. | 223 | * Main loop for the session. This function needs to be called in intervals of |
224 | * toxav_iteration_interval() milliseconds. It is best called in the separate | ||
225 | * thread from tox_iterate. | ||
198 | */ | 226 | */ |
199 | int toxav_cancel(ToxAv *av, int32_t call_index, int peer_id, const char *reason); | 227 | void toxav_iterate(ToxAV *toxAV); |
228 | |||
229 | |||
230 | /******************************************************************************* | ||
231 | * | ||
232 | * :: Call setup | ||
233 | * | ||
234 | ******************************************************************************/ | ||
235 | typedef enum TOXAV_ERR_CALL { | ||
236 | /** | ||
237 | * The function returned successfully. | ||
238 | */ | ||
239 | TOXAV_ERR_CALL_OK, | ||
240 | /** | ||
241 | * A resource allocation error occurred while trying to create the structures | ||
242 | * required for the call. | ||
243 | */ | ||
244 | TOXAV_ERR_CALL_MALLOC, | ||
245 | /** | ||
246 | * Synchronization error occurred. | ||
247 | */ | ||
248 | TOXAV_ERR_CALL_SYNC, | ||
249 | /** | ||
250 | * The friend number did not designate a valid friend. | ||
251 | */ | ||
252 | TOXAV_ERR_CALL_FRIEND_NOT_FOUND, | ||
253 | /** | ||
254 | * The friend was valid, but not currently connected. | ||
255 | */ | ||
256 | TOXAV_ERR_CALL_FRIEND_NOT_CONNECTED, | ||
257 | /** | ||
258 | * Attempted to call a friend while already in an audio or video call with | ||
259 | * them. | ||
260 | */ | ||
261 | TOXAV_ERR_CALL_FRIEND_ALREADY_IN_CALL, | ||
262 | /** | ||
263 | * Audio or video bit rate is invalid. | ||
264 | */ | ||
265 | TOXAV_ERR_CALL_INVALID_BIT_RATE, | ||
266 | } TOXAV_ERR_CALL; | ||
200 | 267 | ||
201 | /** | 268 | /** |
202 | * Notify peer that we are changing codec settings. | 269 | * Call a friend. This will start ringing the friend. |
270 | * | ||
271 | * It is the client's responsibility to stop ringing after a certain timeout, | ||
272 | * if such behaviour is desired. If the client does not stop ringing, the | ||
273 | * library will not stop until the friend is disconnected. Audio and video | ||
274 | * receiving are both enabled by default. | ||
275 | * | ||
276 | * @param friend_number The friend number of the friend that should be called. | ||
277 | * @param audio_bit_rate Audio bit rate in Kb/sec. Set this to 0 to disable | ||
278 | * audio sending. | ||
279 | * @param video_bit_rate Video bit rate in Kb/sec. Set this to 0 to disable | ||
280 | * video sending. | ||
203 | */ | 281 | */ |
204 | int toxav_change_settings(ToxAv *av, int32_t call_index, const ToxAvCSettings *csettings); | 282 | bool toxav_call(ToxAV *toxAV, uint32_t friend_number, uint32_t audio_bit_rate, |
283 | uint32_t video_bit_rate, TOXAV_ERR_CALL *error); | ||
205 | 284 | ||
206 | /** | 285 | /** |
207 | * Terminate transmission. Note that transmission will be | 286 | * The function type for the call callback. |
208 | * terminated without informing remote peer. Usually called when we can't inform peer. | 287 | * |
288 | * @param friend_number The friend number from which the call is incoming. | ||
289 | * @param audio_enabled True if friend is sending audio. | ||
290 | * @param video_enabled True if friend is sending video. | ||
209 | */ | 291 | */ |
210 | int toxav_stop_call(ToxAv *av, int32_t call_index); | 292 | typedef void toxav_call_cb(ToxAV *toxAV, uint32_t friend_number, bool audio_enabled, |
293 | bool video_enabled, void *user_data); | ||
211 | 294 | ||
212 | /** | 295 | /** |
213 | * Allocates transmission data. Must be call before calling toxav_prepare_* and toxav_send_*. | 296 | * Set the callback for the `call` event. Pass NULL to unset. |
214 | * Also, it must be called when call is started | 297 | * |
215 | */ | 298 | */ |
216 | int toxav_prepare_transmission(ToxAv *av, int32_t call_index, int support_video); | 299 | void toxav_callback_call(ToxAV *toxAV, toxav_call_cb *callback, void *user_data); |
300 | |||
301 | typedef enum TOXAV_ERR_ANSWER { | ||
302 | /** | ||
303 | * The function returned successfully. | ||
304 | */ | ||
305 | TOXAV_ERR_ANSWER_OK, | ||
306 | /** | ||
307 | * Synchronization error occurred. | ||
308 | */ | ||
309 | TOXAV_ERR_ANSWER_SYNC, | ||
310 | /** | ||
311 | * Failed to initialize codecs for call session. Note that codec initiation | ||
312 | * will fail if there is no receive callback registered for either audio or | ||
313 | * video. | ||
314 | */ | ||
315 | TOXAV_ERR_ANSWER_CODEC_INITIALIZATION, | ||
316 | /** | ||
317 | * The friend number did not designate a valid friend. | ||
318 | */ | ||
319 | TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND, | ||
320 | /** | ||
321 | * The friend was valid, but they are not currently trying to initiate a call. | ||
322 | * This is also returned if this client is already in a call with the friend. | ||
323 | */ | ||
324 | TOXAV_ERR_ANSWER_FRIEND_NOT_CALLING, | ||
325 | /** | ||
326 | * Audio or video bit rate is invalid. | ||
327 | */ | ||
328 | TOXAV_ERR_ANSWER_INVALID_BIT_RATE, | ||
329 | } TOXAV_ERR_ANSWER; | ||
217 | 330 | ||
218 | /** | 331 | /** |
219 | * Clears transmission data. Call this at the end of the transmission. | 332 | * Accept an incoming call. |
333 | * | ||
334 | * If answering fails for any reason, the call will still be pending and it is | ||
335 | * possible to try and answer it later. Audio and video receiving are both | ||
336 | * enabled by default. | ||
337 | * | ||
338 | * @param friend_number The friend number of the friend that is calling. | ||
339 | * @param audio_bit_rate Audio bit rate in Kb/sec. Set this to 0 to disable | ||
340 | * audio sending. | ||
341 | * @param video_bit_rate Video bit rate in Kb/sec. Set this to 0 to disable | ||
342 | * video sending. | ||
220 | */ | 343 | */ |
221 | int toxav_kill_transmission(ToxAv *av, int32_t call_index); | 344 | bool toxav_answer(ToxAV *toxAV, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_ANSWER *error); |
345 | |||
346 | |||
347 | /******************************************************************************* | ||
348 | * | ||
349 | * :: Call state graph | ||
350 | * | ||
351 | ******************************************************************************/ | ||
352 | enum TOXAV_FRIEND_CALL_STATE { | ||
353 | /** | ||
354 | * Set by the AV core if an error occurred on the remote end or if friend | ||
355 | * timed out. This is the final state after which no more state | ||
356 | * transitions can occur for the call. This call state will never be triggered | ||
357 | * in combination with other call states. | ||
358 | */ | ||
359 | TOXAV_FRIEND_CALL_STATE_ERROR = 1, | ||
360 | /** | ||
361 | * The call has finished. This is the final state after which no more state | ||
362 | * transitions can occur for the call. This call state will never be | ||
363 | * triggered in combination with other call states. | ||
364 | */ | ||
365 | TOXAV_FRIEND_CALL_STATE_FINISHED = 2, | ||
366 | /** | ||
367 | * The flag that marks that friend is sending audio. | ||
368 | */ | ||
369 | TOXAV_FRIEND_CALL_STATE_SENDING_A = 4, | ||
370 | /** | ||
371 | * The flag that marks that friend is sending video. | ||
372 | */ | ||
373 | TOXAV_FRIEND_CALL_STATE_SENDING_V = 8, | ||
374 | /** | ||
375 | * The flag that marks that friend is receiving audio. | ||
376 | */ | ||
377 | TOXAV_FRIEND_CALL_STATE_ACCEPTING_A = 16, | ||
378 | /** | ||
379 | * The flag that marks that friend is receiving video. | ||
380 | */ | ||
381 | TOXAV_FRIEND_CALL_STATE_ACCEPTING_V = 32, | ||
382 | }; | ||
222 | 383 | ||
223 | /** | 384 | /** |
224 | * Encode video frame. | 385 | * The function type for the call_state callback. |
386 | * | ||
387 | * @param friend_number The friend number for which the call state changed. | ||
388 | * @param state The bitmask of the new call state which is guaranteed to be | ||
389 | * different than the previous state. The state is set to 0 when the call is | ||
390 | * paused. The bitmask represents all the activities currently performed by the | ||
391 | * friend. | ||
225 | */ | 392 | */ |
226 | int toxav_prepare_video_frame ( ToxAv *av, | 393 | typedef void toxav_call_state_cb(ToxAV *toxAV, uint32_t friend_number, uint32_t state, void *user_data); |
227 | int32_t call_index, | ||
228 | uint8_t *dest, | ||
229 | int dest_max, | ||
230 | vpx_image_t *input); | ||
231 | 394 | ||
232 | /** | 395 | /** |
233 | * Send encoded video packet. | 396 | * Set the callback for the `call_state` event. Pass NULL to unset. |
397 | * | ||
234 | */ | 398 | */ |
235 | int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *frame, uint32_t frame_size); | 399 | void toxav_callback_call_state(ToxAV *toxAV, toxav_call_state_cb *callback, void *user_data); |
400 | |||
401 | /******************************************************************************* | ||
402 | * | ||
403 | * :: Call control | ||
404 | * | ||
405 | ******************************************************************************/ | ||
406 | typedef enum TOXAV_CALL_CONTROL { | ||
407 | /** | ||
408 | * Resume a previously paused call. Only valid if the pause was caused by this | ||
409 | * client, if not, this control is ignored. Not valid before the call is accepted. | ||
410 | */ | ||
411 | TOXAV_CALL_CONTROL_RESUME, | ||
412 | /** | ||
413 | * Put a call on hold. Not valid before the call is accepted. | ||
414 | */ | ||
415 | TOXAV_CALL_CONTROL_PAUSE, | ||
416 | /** | ||
417 | * Reject a call if it was not answered, yet. Cancel a call after it was | ||
418 | * answered. | ||
419 | */ | ||
420 | TOXAV_CALL_CONTROL_CANCEL, | ||
421 | /** | ||
422 | * Request that the friend stops sending audio. Regardless of the friend's | ||
423 | * compliance, this will cause the audio_receive_frame event to stop being | ||
424 | * triggered on receiving an audio frame from the friend. | ||
425 | */ | ||
426 | TOXAV_CALL_CONTROL_MUTE_AUDIO, | ||
427 | /** | ||
428 | * Calling this control will notify client to start sending audio again. | ||
429 | */ | ||
430 | TOXAV_CALL_CONTROL_UNMUTE_AUDIO, | ||
431 | /** | ||
432 | * Request that the friend stops sending video. Regardless of the friend's | ||
433 | * compliance, this will cause the video_receive_frame event to stop being | ||
434 | * triggered on receiving a video frame from the friend. | ||
435 | */ | ||
436 | TOXAV_CALL_CONTROL_HIDE_VIDEO, | ||
437 | /** | ||
438 | * Calling this control will notify client to start sending video again. | ||
439 | */ | ||
440 | TOXAV_CALL_CONTROL_SHOW_VIDEO, | ||
441 | } TOXAV_CALL_CONTROL; | ||
442 | |||
443 | typedef enum TOXAV_ERR_CALL_CONTROL { | ||
444 | /** | ||
445 | * The function returned successfully. | ||
446 | */ | ||
447 | TOXAV_ERR_CALL_CONTROL_OK, | ||
448 | /** | ||
449 | * Synchronization error occurred. | ||
450 | */ | ||
451 | TOXAV_ERR_CALL_CONTROL_SYNC, | ||
452 | /** | ||
453 | * The friend_number passed did not designate a valid friend. | ||
454 | */ | ||
455 | TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_FOUND, | ||
456 | /** | ||
457 | * This client is currently not in a call with the friend. Before the call is | ||
458 | * answered, only CANCEL is a valid control. | ||
459 | */ | ||
460 | TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL, | ||
461 | /** | ||
462 | * Happens if user tried to pause an already paused call or if trying to | ||
463 | * resume a call that is not paused. | ||
464 | */ | ||
465 | TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION, | ||
466 | } TOXAV_ERR_CALL_CONTROL; | ||
236 | 467 | ||
237 | /** | 468 | /** |
238 | * Encode audio frame. | 469 | * Sends a call control command to a friend. |
470 | * | ||
471 | * @param friend_number The friend number of the friend this client is in a call | ||
472 | * with. | ||
473 | * @param control The control command to send. | ||
474 | * | ||
475 | * @return true on success. | ||
239 | */ | 476 | */ |
240 | int toxav_prepare_audio_frame ( ToxAv *av, | 477 | bool toxav_call_control(ToxAV *toxAV, uint32_t friend_number, TOXAV_CALL_CONTROL control, TOXAV_ERR_CALL_CONTROL *error); |
241 | int32_t call_index, | 478 | |
242 | uint8_t *dest, | 479 | |
243 | int dest_max, | 480 | /******************************************************************************* |
244 | const int16_t *frame, | 481 | * |
245 | int frame_size); | 482 | * :: Controlling bit rates |
483 | * | ||
484 | ******************************************************************************/ | ||
485 | typedef enum TOXAV_ERR_BIT_RATE_SET { | ||
486 | /** | ||
487 | * The function returned successfully. | ||
488 | */ | ||
489 | TOXAV_ERR_BIT_RATE_SET_OK, | ||
490 | /** | ||
491 | * Synchronization error occurred. | ||
492 | */ | ||
493 | TOXAV_ERR_BIT_RATE_SET_SYNC, | ||
494 | /** | ||
495 | * The audio bit rate passed was not one of the supported values. | ||
496 | */ | ||
497 | TOXAV_ERR_BIT_RATE_SET_INVALID_AUDIO_BIT_RATE, | ||
498 | /** | ||
499 | * The video bit rate passed was not one of the supported values. | ||
500 | */ | ||
501 | TOXAV_ERR_BIT_RATE_SET_INVALID_VIDEO_BIT_RATE, | ||
502 | /** | ||
503 | * The friend_number passed did not designate a valid friend. | ||
504 | */ | ||
505 | TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND, | ||
506 | /** | ||
507 | * This client is currently not in a call with the friend. | ||
508 | */ | ||
509 | TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_IN_CALL, | ||
510 | } TOXAV_ERR_BIT_RATE_SET; | ||
246 | 511 | ||
247 | /** | 512 | /** |
248 | * Send encoded audio frame. | 513 | * Set the bit rate to be used in subsequent audio/video frames. |
514 | * | ||
515 | * @param friend_number The friend number of the friend for which to set the | ||
516 | * bit rate. | ||
517 | * @param audio_bit_rate The new audio bit rate in Kb/sec. Set to 0 to disable | ||
518 | * audio sending. Set to -1 to leave unchanged. | ||
519 | * @param video_bit_rate The new video bit rate in Kb/sec. Set to 0 to disable | ||
520 | * video sending. Set to -1 to leave unchanged. | ||
521 | * | ||
249 | */ | 522 | */ |
250 | int toxav_send_audio ( ToxAv *av, int32_t call_index, const uint8_t *frame, unsigned int size); | 523 | bool toxav_bit_rate_set(ToxAV *toxAV, uint32_t friend_number, int32_t audio_bit_rate, |
524 | int32_t video_bit_rate, TOXAV_ERR_BIT_RATE_SET *error); | ||
251 | 525 | ||
252 | /** | 526 | /** |
253 | * Get codec settings from the peer. These were exchanged during call initialization | 527 | * The function type for the bit_rate_status callback. The event is triggered |
254 | * or when peer send us new csettings. | 528 | * when the network becomes too saturated for current bit rates at which |
529 | * point core suggests new bit rates. | ||
530 | * | ||
531 | * @param friend_number The friend number of the friend for which to set the | ||
532 | * bit rate. | ||
533 | * @param audio_bit_rate Suggested maximum audio bit rate in Kb/sec. | ||
534 | * @param video_bit_rate Suggested maximum video bit rate in Kb/sec. | ||
255 | */ | 535 | */ |
256 | int toxav_get_peer_csettings ( ToxAv *av, int32_t call_index, int peer, ToxAvCSettings *dest ); | 536 | typedef void toxav_bit_rate_status_cb(ToxAV *toxAV, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, void *user_data); |
257 | 537 | ||
258 | /** | 538 | /** |
259 | * Get friend id of peer participating in conversation. | 539 | * Set the callback for the `bit_rate_status` event. Pass NULL to unset. |
540 | * | ||
260 | */ | 541 | */ |
261 | int toxav_get_peer_id ( ToxAv *av, int32_t call_index, int peer ); | 542 | void toxav_callback_bit_rate_status(ToxAV *toxAV, toxav_bit_rate_status_cb *callback, void *user_data); |
543 | |||
544 | |||
545 | /******************************************************************************* | ||
546 | * | ||
547 | * :: A/V sending | ||
548 | * | ||
549 | ******************************************************************************/ | ||
550 | typedef enum TOXAV_ERR_SEND_FRAME { | ||
551 | /** | ||
552 | * The function returned successfully. | ||
553 | */ | ||
554 | TOXAV_ERR_SEND_FRAME_OK, | ||
555 | /** | ||
556 | * In case of video, one of Y, U, or V was NULL. In case of audio, the samples | ||
557 | * data pointer was NULL. | ||
558 | */ | ||
559 | TOXAV_ERR_SEND_FRAME_NULL, | ||
560 | /** | ||
561 | * The friend_number passed did not designate a valid friend. | ||
562 | */ | ||
563 | TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND, | ||
564 | /** | ||
565 | * This client is currently not in a call with the friend. | ||
566 | */ | ||
567 | TOXAV_ERR_SEND_FRAME_FRIEND_NOT_IN_CALL, | ||
568 | /** | ||
569 | * Synchronization error occurred. | ||
570 | */ | ||
571 | TOXAV_ERR_SEND_FRAME_SYNC, | ||
572 | /** | ||
573 | * One of the frame parameters was invalid. E.g. the resolution may be too | ||
574 | * small or too large, or the audio sampling rate may be unsupported. | ||
575 | */ | ||
576 | TOXAV_ERR_SEND_FRAME_INVALID, | ||
577 | /** | ||
578 | * Either friend turned off audio or video receiving or we turned off sending | ||
579 | * for the said payload. | ||
580 | */ | ||
581 | TOXAV_ERR_SEND_FRAME_PAYLOAD_TYPE_DISABLED, | ||
582 | /** | ||
583 | * Failed to push frame through rtp interface. | ||
584 | */ | ||
585 | TOXAV_ERR_SEND_FRAME_RTP_FAILED, | ||
586 | } TOXAV_ERR_SEND_FRAME; | ||
262 | 587 | ||
263 | /** | 588 | /** |
264 | * Get current call state. | 589 | * Send an audio frame to a friend. |
265 | */ | 590 | * |
266 | ToxAvCallState toxav_get_call_state ( ToxAv *av, int32_t call_index ); | 591 | * The expected format of the PCM data is: [s1c1][s1c2][...][s2c1][s2c2][...]... |
592 | * Meaning: sample 1 for channel 1, sample 1 for channel 2, ... | ||
593 | * For mono audio, this has no meaning, every sample is subsequent. For stereo, | ||
594 | * this means the expected format is LRLRLR... with samples for left and right | ||
595 | * alternating. | ||
596 | * | ||
597 | * @param friend_number The friend number of the friend to which to send an | ||
598 | * audio frame. | ||
599 | * @param pcm An array of audio samples. The size of this array must be | ||
600 | * sample_count * channels. | ||
601 | * @param sample_count Number of samples in this frame. Valid numbers here are | ||
602 | * ((sample rate) * (audio length) / 1000), where audio length can be | ||
603 | * 2.5, 5, 10, 20, 40 or 60 millseconds. | ||
604 | * @param channels Number of audio channels. Supported values are 1 and 2. | ||
605 | * @param sampling_rate Audio sampling rate used in this frame. Valid sampling | ||
606 | * rates are 8000, 12000, 16000, 24000, or 48000. | ||
607 | */ | ||
608 | bool toxav_audio_send_frame(ToxAV *toxAV, uint32_t friend_number, const int16_t *pcm, | ||
609 | size_t sample_count, uint8_t channels, uint32_t sampling_rate, | ||
610 | TOXAV_ERR_SEND_FRAME *error); | ||
267 | 611 | ||
268 | /** | 612 | /** |
269 | * Is certain capability supported. Used to determine if encoding/decoding is ready. | 613 | * Send a video frame to a friend. |
614 | * | ||
615 | * Y - plane should be of size: height * width | ||
616 | * U - plane should be of size: (height/2) * (width/2) | ||
617 | * V - plane should be of size: (height/2) * (width/2) | ||
618 | * | ||
619 | * @param friend_number The friend number of the friend to which to send a video | ||
620 | * frame. | ||
621 | * @param width Width of the frame in pixels. | ||
622 | * @param height Height of the frame in pixels. | ||
623 | * @param y Y (Luminance) plane data. | ||
624 | * @param u U (Chroma) plane data. | ||
625 | * @param v V (Chroma) plane data. | ||
626 | */ | ||
627 | bool toxav_video_send_frame(ToxAV *toxAV, uint32_t friend_number, uint16_t width, | ||
628 | uint16_t height, const uint8_t *y, const uint8_t *u, const uint8_t *v, | ||
629 | TOXAV_ERR_SEND_FRAME *error); | ||
630 | |||
631 | |||
632 | /******************************************************************************* | ||
633 | * | ||
634 | * :: A/V receiving | ||
635 | * | ||
636 | ******************************************************************************/ | ||
637 | /** | ||
638 | * The function type for the audio_receive_frame callback. The callback can be | ||
639 | * called multiple times per single iteration depending on the amount of queued | ||
640 | * frames in the buffer. The received format is the same as in send function. | ||
641 | * | ||
642 | * @param friend_number The friend number of the friend who sent an audio frame. | ||
643 | * @param pcm An array of audio samples (sample_count * channels elements). | ||
644 | * @param sample_count The number of audio samples per channel in the PCM array. | ||
645 | * @param channels Number of audio channels. | ||
646 | * @param sampling_rate Sampling rate used in this frame. | ||
647 | * | ||
270 | */ | 648 | */ |
271 | int toxav_capability_supported ( ToxAv *av, int32_t call_index, ToxAvCapabilities capability ); | 649 | typedef void toxav_audio_receive_frame_cb(ToxAV *toxAV, uint32_t friend_number, const int16_t *pcm, |
650 | size_t sample_count, uint8_t channels, uint32_t sampling_rate, | ||
651 | void *user_data); | ||
272 | 652 | ||
273 | /** | 653 | /** |
274 | * Returns tox reference. | 654 | * Set the callback for the `audio_receive_frame` event. Pass NULL to unset. |
655 | * | ||
275 | */ | 656 | */ |
276 | Tox *toxav_get_tox (ToxAv *av); | 657 | void toxav_callback_audio_receive_frame(ToxAV *toxAV, toxav_audio_receive_frame_cb *callback, void *user_data); |
658 | |||
659 | /** | ||
660 | * The function type for the video_receive_frame callback. | ||
661 | * | ||
662 | * @param friend_number The friend number of the friend who sent a video frame. | ||
663 | * @param width Width of the frame in pixels. | ||
664 | * @param height Height of the frame in pixels. | ||
665 | * @param y | ||
666 | * @param u | ||
667 | * @param v Plane data. | ||
668 | * The size of plane data is derived from width and height where | ||
669 | * Y = MAX(width, abs(ystride)) * height, | ||
670 | * U = MAX(width/2, abs(ustride)) * (height/2) and | ||
671 | * V = MAX(width/2, abs(vstride)) * (height/2). | ||
672 | * @param ystride | ||
673 | * @param ustride | ||
674 | * @param vstride Strides data. Strides represent padding for each plane | ||
675 | * that may or may not be present. You must handle strides in | ||
676 | * your image processing code. Strides are negative if the | ||
677 | * image is bottom-up hence why you MUST abs() it when | ||
678 | * calculating plane buffer size. | ||
679 | */ | ||
680 | typedef void toxav_video_receive_frame_cb(ToxAV *toxAV, uint32_t friend_number, uint16_t width, | ||
681 | uint16_t height, const uint8_t *y, const uint8_t *u, const uint8_t *v, | ||
682 | int32_t ystride, int32_t ustride, int32_t vstride, void *user_data); | ||
277 | 683 | ||
278 | /** | 684 | /** |
279 | * Returns number of active calls or -1 on error. | 685 | * Set the callback for the `video_receive_frame` event. Pass NULL to unset. |
686 | * | ||
280 | */ | 687 | */ |
281 | int toxav_get_active_count (ToxAv *av); | 688 | void toxav_callback_video_receive_frame(ToxAV *toxAV, toxav_video_receive_frame_cb *callback, void *user_data); |
282 | 689 | ||
690 | /** | ||
691 | * NOTE Compatibility with old toxav group calls TODO remove | ||
692 | */ | ||
283 | /* Create a new toxav group. | 693 | /* Create a new toxav group. |
284 | * | 694 | * |
285 | * return group number on success. | 695 | * return group number on success. |
@@ -290,7 +700,7 @@ int toxav_get_active_count (ToxAv *av); | |||
290 | * | 700 | * |
291 | * Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)). | 701 | * Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)). |
292 | */ | 702 | */ |
293 | int toxav_add_av_groupchat(Tox *tox, void (*audio_callback)(Tox *, int, int, const int16_t *, unsigned int, uint8_t, | 703 | int toxav_add_av_groupchat(Tox *tox, void (*audio_callback)(void*, int, int, const int16_t *, unsigned int, uint8_t, |
294 | unsigned int, void *), void *userdata); | 704 | unsigned int, void *), void *userdata); |
295 | 705 | ||
296 | /* Join a AV group (you need to have been invited first.) | 706 | /* Join a AV group (you need to have been invited first.) |
@@ -304,7 +714,7 @@ int toxav_add_av_groupchat(Tox *tox, void (*audio_callback)(Tox *, int, int, con | |||
304 | * Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)). | 714 | * Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)). |
305 | */ | 715 | */ |
306 | int toxav_join_av_groupchat(Tox *tox, int32_t friendnumber, const uint8_t *data, uint16_t length, | 716 | int toxav_join_av_groupchat(Tox *tox, int32_t friendnumber, const uint8_t *data, uint16_t length, |
307 | void (*audio_callback)(Tox *, int, int, const int16_t *, unsigned int, uint8_t, unsigned int, void *), void *userdata); | 717 | void (*audio_callback)(void*, int, int, const int16_t *, unsigned int, uint8_t, unsigned int, void *), void *userdata); |
308 | 718 | ||
309 | /* Send audio to the group chat. | 719 | /* Send audio to the group chat. |
310 | * | 720 | * |
@@ -325,5 +735,4 @@ int toxav_group_send_audio(Tox *tox, int groupnumber, const int16_t *pcm, unsign | |||
325 | #ifdef __cplusplus | 735 | #ifdef __cplusplus |
326 | } | 736 | } |
327 | #endif | 737 | #endif |
328 | 738 | #endif /* TOXAV_H */ | |
329 | #endif /* __TOXAV */ | ||