Debian Codegraph @

Xpjproject/pjsip/src/pjsua-lib/pjsua_txt.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2024-2025 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include <pjsua-lib/pjsua.h>
20 #include <pjsua-lib/pjsua_internal.h>
21
22 #if defined(PJSUA_MEDIA_HAS_PJMEDIA) && PJSUA_MEDIA_HAS_PJMEDIA != 0
23
24 #define THIS_FILE "pjsua_txt.c"
25
26
27 /* Send text to remote. */
28 PJ_DEF(pj_status_t)
29 pjsua_call_send_text(pjsua_call_id call_id,
30 const pjsua_call_send_text_param *param)
31 {
32 pjsua_call *call;
33 pjsua_call_media *call_med;
34 pjsip_dialog *dlg = NULL;
35 int med_idx;
36 pj_status_t status;
37
38 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls &&
39 param, PJ_EINVAL);
40
41 pj_log_push_indent();
42
43 status = acquire_call("pjsua_call_send_text()", call_id, &call, &dlg);
44 if (status != PJ_SUCCESS)
45 goto on_return;
46
47 /* Verify and normalize media index */
48 if ((med_idx = param->med_idx) == -1) {
49 unsigned i;
50
51 for (i = 0; i < call->med_cnt; ++i) {
52 if (call->media[i].type == PJMEDIA_TYPE_TEXT &&
53 (call->media[i].dir & PJMEDIA_DIR_ENCODING))
54 {
55 med_idx = i;
56 break;
57 }
58 }
59
60 if (med_idx == -1) {
61 status = PJ_ENOTFOUND;
62 goto on_return;
63 }
64 }
65
66 PJ_ASSERT_ON_FAIL(med_idx >= 0 && med_idx < (int)call->med_cnt,
67 {status = PJ_EINVAL; goto on_return;});
68
69 /* Verify if the stream is transmitting text */
70 call_med = &call->media[med_idx];
71 if (call_med->type != PJMEDIA_TYPE_TEXT ||
72 (call_med->dir & PJMEDIA_DIR_ENCODING) == 0 ||
73 !call_med->strm.t.stream)
74 {
75 status = PJ_EINVALIDOP;
76 goto on_return;
77 }
78
79 status = pjmedia_txt_stream_send_text(call_med->strm.t.stream,
80 &param->text);
81
82 on_return:
83 if (dlg) pjsip_dlg_dec_lock(dlg);
84 pj_log_pop_indent();
85 return status;
86 }
87
88 /*
89 * Incoming text callback from the stream.
90 */
91 static void rx_text_cb(pjmedia_txt_stream *strm, void *user_data,
92 const pjmedia_txt_stream_data *data)
93 {
94 pjsua_call_id call_id;
95 pjsua_txt_stream_data txt;
96
97 PJ_UNUSED_ARG(strm);
98
99 call_id = (pjsua_call_id)(pj_ssize_t)user_data;
100 if (pjsua_var.calls[call_id].hanging_up)
101 return;
102
103 pj_log_push_indent();
104
105 if (pjsua_var.ua_cfg.cb.on_call_rx_text) {
106 txt.seq = data->seq;
107 txt.ts = data->ts;
108 txt.text = data->text;
109 (*pjsua_var.ua_cfg.cb.on_call_rx_text)(call_id, &txt);
110 }
111
112 pj_log_pop_indent();
113 }
114
115 /* Internal function to stop text stream */
116 void pjsua_txt_stop_stream(pjsua_call_media *call_med)
117 {
118 pjmedia_txt_stream *strm = call_med->strm.t.stream;
119 pjmedia_stream_common *c_strm = (pjmedia_stream_common *)strm;
120 pjmedia_rtcp_stat stat;
121 pjmedia_txt_stream_info prev_txt_si;
122
123 pj_assert(call_med->type == PJMEDIA_TYPE_TEXT);
124
125 if (!strm)
126 return;
127
128 PJ_LOG(4,(THIS_FILE, "Stopping text stream.."));
129 pj_log_push_indent();
130
131 pjmedia_txt_stream_get_info(strm, &prev_txt_si);
132 call_med->prev_local_addr = prev_txt_si.local_addr;
133 call_med->prev_rem_addr = prev_txt_si.rem_addr;
134
135 pjmedia_stream_common_send_rtcp_bye(c_strm);
136
137 if (pjmedia_stream_common_get_stat(c_strm, &stat) == PJ_SUCCESS)
138 {
139 /* Save RTP timestamp & sequence, so when media session is
140 * restarted, those values will be restored as the initial
141 * RTP timestamp & sequence of the new media session. So in
142 * the same call session, RTP timestamp and sequence are
143 * guaranteed to be contigue.
144 */
145 call_med->rtp_tx_seq_ts_set = 1 | (1 << 1);
146 call_med->rtp_tx_seq = stat.rtp_tx_last_seq;
147 call_med->rtp_tx_ts = stat.rtp_tx_last_ts;
148 }
149
150 pjmedia_txt_stream_destroy(strm);
151 call_med->strm.t.stream = NULL;
152
153 pj_log_pop_indent();
154 }
155
156 pj_status_t pjsua_txt_channel_update(pjsua_call_media *call_med,
157 pj_pool_t *tmp_pool,
158 pjmedia_txt_stream_info *si,
159 const pjmedia_sdp_session *local_sdp,
160 const pjmedia_sdp_session *remote_sdp)
161 {
162 pjsua_call *call = call_med->call;
163 unsigned strm_idx = call_med->idx;
164 pj_status_t status = PJ_SUCCESS;
165
166 PJ_UNUSED_ARG(tmp_pool);
167 PJ_UNUSED_ARG(local_sdp);
168 PJ_UNUSED_ARG(remote_sdp);
169
170 PJ_LOG(4,(THIS_FILE,"Text channel update for index %d for call %d...",
171 call_med->idx, call_med->call->index));
172 pj_log_push_indent();
173
174 si->rtcp_sdes_bye_disabled = pjsua_var.media_cfg.no_rtcp_sdes_bye;
175
176 /* Check if no media is active */
177 if (local_sdp->media[strm_idx]->desc.port != 0) {
178
179 /* Optionally, application may modify other stream settings here
180 * (such as jitter buffer parameters, codec ptime, etc.)
181 */
182 si->jb_init = pjsua_var.media_cfg.jb_init;
183 si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
184 si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
185 si->jb_max = pjsua_var.media_cfg.jb_max;
186 si->jb_discard_algo = pjsua_var.media_cfg.jb_discard_algo;
187
188 /* Set SSRC and CNAME */
189 si->ssrc = call_med->ssrc;
190 si->cname = call->cname;
191
192 /* Set RTP timestamp & sequence, normally these value are intialized
193 * automatically when stream session created, but for some cases (e.g:
194 * call reinvite, call update) timestamp and sequence need to be kept
195 * contigue.
196 */
197 si->rtp_ts = call_med->rtp_tx_ts;
198 si->rtp_seq = call_med->rtp_tx_seq;
199 si->rtp_seq_ts_set = call_med->rtp_tx_seq_ts_set;
200
201 #if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0
202 /* Enable/disable stream keep-alive and NAT hole punch. */
203 si->use_ka = pjsua_var.acc[call->acc_id].cfg.use_stream_ka;
204
205 si->ka_cfg = pjsua_var.acc[call->acc_id].cfg.stream_ka_cfg;
206 #endif
207
208 if (!call->hanging_up && pjsua_var.ua_cfg.cb.on_stream_precreate) {
209 pjsua_on_stream_precreate_param prm;
210 prm.stream_idx = strm_idx;
211 prm.stream_info.type = PJMEDIA_TYPE_TEXT;
212 prm.stream_info.info.txt = *si;
213 (*pjsua_var.ua_cfg.cb.on_stream_precreate)(call->index, &prm);
214
215 /* Copy back only the fields which are allowed to be changed. */
216 si->jb_init = prm.stream_info.info.aud.jb_init;
217 si->jb_min_pre = prm.stream_info.info.aud.jb_min_pre;
218 si->jb_max_pre = prm.stream_info.info.aud.jb_max_pre;
219 si->jb_max = prm.stream_info.info.aud.jb_max;
220 si->jb_discard_algo = prm.stream_info.info.aud.jb_discard_algo;
221 #if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
222 si->use_ka = prm.stream_info.info.aud.use_ka;
223
224 si->ka_cfg = prm.stream_info.info.aud.ka_cfg;
225 #endif
226 si->rtcp_sdes_bye_disabled =
227 prm.stream_info.info.aud.rtcp_sdes_bye_disabled;
228 }
229
230 /* Create session based on session info. */
231 status = pjmedia_txt_stream_create(pjsua_var.med_endpt, NULL, si,
232 call_med->tp, NULL,
233 &call_med->strm.t.stream);
234 if (status != PJ_SUCCESS) {
235 goto on_return;
236 }
237
238 /* Start stream. */
239 status = pjmedia_txt_stream_start(call_med->strm.t.stream);
240 if (status != PJ_SUCCESS) {
241 goto on_return;
242 }
243
244 if (call_med->prev_state == PJSUA_CALL_MEDIA_NONE) {
245 pjmedia_stream_common_send_rtcp_sdes(
246 (pjmedia_stream_common *)call_med->strm.t.stream);
247 }
248
249 /* Set incoming text callback. */
250 if (!call->hanging_up && pjsua_var.ua_cfg.cb.on_call_rx_text) {
251 pjmedia_txt_stream_set_rx_callback(
252 call_med->strm.t.stream, &rx_text_cb,
253 (void *)(pj_ssize_t)(call->index), 0);
254 }
255 }
256
257 on_return:
258 pj_log_pop_indent();
259 if (status != PJ_SUCCESS)
260 pjsua_perror(THIS_FILE, "pjsua_txt_channel_update failed", status);
261 return status;
262 }
263
264 #endif /* PJSUA_MEDIA_HAS_PJMEDIA */