summaryrefslogtreecommitdiff
path: root/protocols/Tox/toxcore/toxav/msi.h
blob: 64fa08819abb0032f024e7a8a3513de63775cbfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**  msi.h
 *
 *   Copyright (C) 2013 Tox project All Rights Reserved.
 *
 *   This file is part of Tox.
 *
 *   Tox is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Tox is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Tox. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef __TOXMSI
#define __TOXMSI

#include <inttypes.h>
#include <pthread.h>

#include "../toxcore/Messenger.h"

typedef uint8_t MSICallIDType[12];
typedef uint8_t MSIReasonStrType[255];
typedef void ( *MSICallbackType ) ( void *agent, int32_t call_idx, void *arg );


/**
 * @brief Call type identifier. Also used as rtp callback prefix.
 */
typedef enum {
    type_audio = 192,
    type_video
} MSICallType;


/**
 * @brief Call state identifiers.
 */
typedef enum {
    call_inviting, /* when sending call invite */
    call_starting, /* when getting call invite */
    call_active,
    call_hold,
    call_hanged_up

} MSICallState;


/**
 * @brief Encoding settings.
 */
typedef struct _MSICodecSettings {
    MSICallType call_type;

    uint32_t video_bitrate; /* In kbits/s */
    uint16_t max_video_width; /* In px */
    uint16_t max_video_height; /* In px */

    uint32_t audio_bitrate; /* In bits/s */
    uint16_t audio_frame_duration; /* In ms */
    uint32_t audio_sample_rate; /* In Hz */
    uint32_t audio_channels;
} MSICSettings;


/**
 * @brief Callbacks ids that handle the states
 */
typedef enum {
    /* Requests */
    MSI_OnInvite,
    MSI_OnStart,
    MSI_OnCancel,
    MSI_OnReject,
    MSI_OnEnd,

    /* Responses */
    MSI_OnRinging,
    MSI_OnStarting,
    MSI_OnEnding,

    /* Protocol */
    MSI_OnRequestTimeout,
    MSI_OnPeerTimeout,
    MSI_OnMediaChange
} MSICallbackID;

/**
 * @brief Callbacks container
 */
typedef struct _MSICallbackCont {
    MSICallbackType function;
    void *data;
} MSICallbackCont;

/**
 * @brief The call struct.
 *
 */
typedef struct _MSICall {                  /* Call info structure */
    struct _MSISession *session;           /* Session pointer */

    MSICallState        state;

    MSICSettings    csettings_local;   /* Local call settings */
    MSICSettings   *csettings_peer;    /* Peers call settings */

    MSICallIDType       id;                /* Random value identifying the call */

    int                 ringing_tout_ms;   /* Ringing timeout in ms */

    int                 request_timer_id;  /* Timer id for outgoing request/action */
    int                 ringing_timer_id;  /* Timer id for ringing timeout */


    pthread_mutex_t     mutex;             /* */
    uint32_t           *peers;
    uint16_t            peer_count;

    int32_t             call_idx;          /* Index of this call in MSISession */
} MSICall;


/**
 * @brief Control session struct
 *
 */
typedef struct _MSISession {

    /* Call handlers */
    MSICall       **calls;
    int32_t         max_calls;

    void           *agent_handler;
    Messenger      *messenger_handle;

    uint32_t        frequ;
    uint32_t        call_timeout; /* Time of the timeout for some action to end; 0 if infinite */

    pthread_mutex_t mutex;

    void           *timer_handler;
    MSICallbackCont callbacks[11];     /* Callbacks used by this session */
} MSISession;

/**
 * @brief Callback setter.
 *
 * @param session The container.
 * @param callback The callback.
 * @param id The id.
 * @return void
 */
void msi_register_callback(MSISession *session, MSICallbackType callback, MSICallbackID id, void *userdata);


/**
 * @brief Start the control session.
 *
 * @param messenger Tox* object.
 * @param max_calls Amount of calls possible
 * @return MSISession* The created session.
 * @retval NULL Error occurred.
 */
MSISession *msi_init_session ( Messenger *messenger, int32_t max_calls );


/**
 * @brief Terminate control session.
 *
 * @param session The session
 * @return int
 */
int msi_terminate_session ( MSISession *session );


/**
 * @brief Send invite request to friend_id.
 *
 * @param session Control session.
 * @param call_index Set to new call index.
 * @param call_type Type of the call. Audio or Video(both audio and video)
 * @param rngsec Ringing timeout.
 * @param friend_id The friend.
 * @return int
 */
int msi_invite ( MSISession *session, int32_t *call_index, MSICSettings csettings, uint32_t rngsec,
                 uint32_t friend_id );


/**
 * @brief Hangup active call.
 *
 * @param session Control session.
 * @param call_index To which call is this action handled.
 * @return int
 * @retval -1 Error occurred.
 * @retval 0 Success.
 */
int msi_hangup ( MSISession *session, int32_t call_index );


/**
 * @brief Answer active call request.
 *
 * @param session Control session.
 * @param call_index To which call is this action handled.
 * @param call_type Answer with Audio or Video(both).
 * @return int
 */
int msi_answer ( MSISession *session, int32_t call_index, MSICSettings csettings );


/**
 * @brief Cancel request.
 *
 * @param session Control session.
 * @param call_index To which call is this action handled.
 * @param peer To which peer.
 * @param reason Set optional reason header. Pass NULL if none.
 * @return int
 */
int msi_cancel ( MSISession *session, int32_t call_index, uint32_t peer, const char *reason );


/**
 * @brief Reject request.
 *
 * @param session Control session.
 * @param call_index To which call is this action handled.
 * @param reason Set optional reason header. Pass NULL if none.
 * @return int
 */
int msi_reject ( MSISession *session, int32_t call_index, const char *reason );


/**
 * @brief Send invite request to friend_id.
 *
 * @param session Control session.
 * @param call_index Call index.
 * @param call_type Type of the call. Audio or Video(both audio and video)
 * @param rngsec Ringing timeout.
 * @param friend_id The friend.
 * @return int
 */
int msi_change_csettings ( MSISession *session, int32_t call_index, MSICSettings csettings );


/**
 * @brief Terminate the current call.
 *
 * @param session Control session.
 * @param call_index To which call is this action handled.
 * @return int
 */
int msi_stopcall ( MSISession *session, int32_t call_index );

#endif /* __TOXMSI */