1
/* $Id: transport_adapter_sample.c 3841 2011-10-24 09:28:13Z ming $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pjmedia/transport_adapter_sample.h>
21
#include <pjmedia/endpoint.h>
22
#include <pj/assert.h>
26
/* Transport functions prototypes */
27
static pj_status_t transport_get_info (pjmedia_transport *tp,
28
pjmedia_transport_info *info);
29
static pj_status_t transport_attach (pjmedia_transport *tp,
31
const pj_sockaddr_t *rem_addr,
32
const pj_sockaddr_t *rem_rtcp,
37
void (*rtcp_cb)(void*,
40
static void transport_detach (pjmedia_transport *tp,
42
static pj_status_t transport_send_rtp( pjmedia_transport *tp,
45
static pj_status_t transport_send_rtcp(pjmedia_transport *tp,
48
static pj_status_t transport_send_rtcp2(pjmedia_transport *tp,
49
const pj_sockaddr_t *addr,
53
static pj_status_t transport_media_create(pjmedia_transport *tp,
56
const pjmedia_sdp_session *rem_sdp,
57
unsigned media_index);
58
static pj_status_t transport_encode_sdp(pjmedia_transport *tp,
60
pjmedia_sdp_session *local_sdp,
61
const pjmedia_sdp_session *rem_sdp,
62
unsigned media_index);
63
static pj_status_t transport_media_start (pjmedia_transport *tp,
65
const pjmedia_sdp_session *local_sdp,
66
const pjmedia_sdp_session *rem_sdp,
67
unsigned media_index);
68
static pj_status_t transport_media_stop(pjmedia_transport *tp);
69
static pj_status_t transport_simulate_lost(pjmedia_transport *tp,
72
static pj_status_t transport_destroy (pjmedia_transport *tp);
75
/* The transport operations */
76
static struct pjmedia_transport_op tp_adapter_op =
83
&transport_send_rtcp2,
84
&transport_media_create,
85
&transport_encode_sdp,
86
&transport_media_start,
87
&transport_media_stop,
88
&transport_simulate_lost,
93
/* The transport adapter instance */
96
pjmedia_transport base;
101
/* Stream information. */
102
void *stream_user_data;
103
void (*stream_rtp_cb)(void *user_data,
106
void (*stream_rtcp_cb)(void *user_data,
111
/* Add your own member here.. */
112
pjmedia_transport *slave_tp;
117
* Create the adapter.
119
PJ_DEF(pj_status_t) pjmedia_tp_adapter_create( pjmedia_endpt *endpt,
121
pjmedia_transport *transport,
123
pjmedia_transport **p_tp)
126
struct tp_adapter *adapter;
131
/* Create the pool and initialize the adapter structure */
132
pool = pjmedia_endpt_create_pool(endpt, name, 512, 512);
133
adapter = PJ_POOL_ZALLOC_T(pool, struct tp_adapter);
134
adapter->pool = pool;
135
pj_ansi_strncpy(adapter->base.name, pool->obj_name,
136
sizeof(adapter->base.name));
137
adapter->base.type = (pjmedia_transport_type)
138
(PJMEDIA_TRANSPORT_TYPE_USER + 1);
139
adapter->base.op = &tp_adapter_op;
141
/* Save the transport as the slave transport */
142
adapter->slave_tp = transport;
143
adapter->del_base = del_base;
146
*p_tp = &adapter->base;
152
* get_info() is called to get the transport addresses to be put
153
* in SDP c= line and a=rtcp line.
155
static pj_status_t transport_get_info(pjmedia_transport *tp,
156
pjmedia_transport_info *info)
158
struct tp_adapter *adapter = (struct tp_adapter*)tp;
160
/* Since we don't have our own connection here, we just pass
161
* this function to the slave transport.
163
return pjmedia_transport_get_info(adapter->slave_tp, info);
167
/* This is our RTP callback, that is called by the slave transport when it
168
* receives RTP packet.
170
static void transport_rtp_cb(void *user_data, void *pkt, pj_ssize_t size)
172
struct tp_adapter *adapter = (struct tp_adapter*)user_data;
174
pj_assert(adapter->stream_rtp_cb != NULL);
176
/* Call stream's callback */
177
adapter->stream_rtp_cb(adapter->stream_user_data, pkt, size);
180
/* This is our RTCP callback, that is called by the slave transport when it
181
* receives RTCP packet.
183
static void transport_rtcp_cb(void *user_data, void *pkt, pj_ssize_t size)
185
struct tp_adapter *adapter = (struct tp_adapter*)user_data;
187
pj_assert(adapter->stream_rtcp_cb != NULL);
189
/* Call stream's callback */
190
adapter->stream_rtcp_cb(adapter->stream_user_data, pkt, size);
195
* attach() is called by stream to register callbacks that we should
196
* call on receipt of RTP and RTCP packets.
198
static pj_status_t transport_attach(pjmedia_transport *tp,
200
const pj_sockaddr_t *rem_addr,
201
const pj_sockaddr_t *rem_rtcp,
203
void (*rtp_cb)(void*,
206
void (*rtcp_cb)(void*,
210
struct tp_adapter *adapter = (struct tp_adapter*)tp;
213
/* In this example, we will save the stream information and callbacks
214
* to our structure, and we will register different RTP/RTCP callbacks
217
pj_assert(adapter->stream_user_data == NULL);
218
adapter->stream_user_data = user_data;
219
adapter->stream_rtp_cb = rtp_cb;
220
adapter->stream_rtcp_cb = rtcp_cb;
222
status = pjmedia_transport_attach(adapter->slave_tp, adapter, rem_addr,
223
rem_rtcp, addr_len, &transport_rtp_cb,
225
if (status != PJ_SUCCESS) {
226
adapter->stream_user_data = NULL;
227
adapter->stream_rtp_cb = NULL;
228
adapter->stream_rtcp_cb = NULL;
236
* detach() is called when the media is terminated, and the stream is
237
* to be disconnected from us.
239
static void transport_detach(pjmedia_transport *tp, void *strm)
241
struct tp_adapter *adapter = (struct tp_adapter*)tp;
245
if (adapter->stream_user_data != NULL) {
246
pjmedia_transport_detach(adapter->slave_tp, adapter);
247
adapter->stream_user_data = NULL;
248
adapter->stream_rtp_cb = NULL;
249
adapter->stream_rtcp_cb = NULL;
255
* send_rtp() is called to send RTP packet. The "pkt" and "size" argument
256
* contain both the RTP header and the payload.
258
static pj_status_t transport_send_rtp( pjmedia_transport *tp,
262
struct tp_adapter *adapter = (struct tp_adapter*)tp;
264
/* You may do some processing to the RTP packet here if you want. */
266
/* Send the packet using the slave transport */
267
return pjmedia_transport_send_rtp(adapter->slave_tp, pkt, size);
272
* send_rtcp() is called to send RTCP packet. The "pkt" and "size" argument
273
* contain the RTCP packet.
275
static pj_status_t transport_send_rtcp(pjmedia_transport *tp,
279
struct tp_adapter *adapter = (struct tp_adapter*)tp;
281
/* You may do some processing to the RTCP packet here if you want. */
283
/* Send the packet using the slave transport */
284
return pjmedia_transport_send_rtcp(adapter->slave_tp, pkt, size);
289
* This is another variant of send_rtcp(), with the alternate destination
290
* address in the argument.
292
static pj_status_t transport_send_rtcp2(pjmedia_transport *tp,
293
const pj_sockaddr_t *addr,
298
struct tp_adapter *adapter = (struct tp_adapter*)tp;
299
return pjmedia_transport_send_rtcp2(adapter->slave_tp, addr, addr_len,
304
* The media_create() is called when the transport is about to be used for
307
static pj_status_t transport_media_create(pjmedia_transport *tp,
310
const pjmedia_sdp_session *rem_sdp,
311
unsigned media_index)
313
struct tp_adapter *adapter = (struct tp_adapter*)tp;
315
/* if "rem_sdp" is not NULL, it means we are UAS. You may do some
316
* inspections on the incoming SDP to verify that the SDP is acceptable
317
* for us. If the SDP is not acceptable, we can reject the SDP by
318
* returning non-PJ_SUCCESS.
321
/* Do your stuff.. */
324
/* Once we're done with our initialization, pass the call to the
325
* slave transports to let it do it's own initialization too.
327
return pjmedia_transport_media_create(adapter->slave_tp, sdp_pool, options,
328
rem_sdp, media_index);
332
* The encode_sdp() is called when we're about to send SDP to remote party,
333
* either as SDP offer or as SDP answer.
335
static pj_status_t transport_encode_sdp(pjmedia_transport *tp,
337
pjmedia_sdp_session *local_sdp,
338
const pjmedia_sdp_session *rem_sdp,
339
unsigned media_index)
341
struct tp_adapter *adapter = (struct tp_adapter*)tp;
343
/* If "rem_sdp" is not NULL, it means we're encoding SDP answer. You may
344
* do some more checking on the SDP's once again to make sure that
345
* everything is okay before we send SDP.
348
/* Do checking stuffs here.. */
351
/* You may do anything to the local_sdp, e.g. adding new attributes, or
352
* even modifying the SDP if you want.
355
/* Say we add a proprietary attribute here.. */
356
pjmedia_sdp_attr *my_attr;
358
my_attr = PJ_POOL_ALLOC_T(sdp_pool, pjmedia_sdp_attr);
359
pj_strdup2(sdp_pool, &my_attr->name, "X-adapter");
360
pj_strdup2(sdp_pool, &my_attr->value, "some value");
362
pjmedia_sdp_attr_add(&local_sdp->media[media_index]->attr_count,
363
local_sdp->media[media_index]->attr,
367
/* And then pass the call to slave transport to let it encode its
368
* information in the SDP. You may choose to call encode_sdp() to slave
369
* first before adding your custom attributes if you want.
371
return pjmedia_transport_encode_sdp(adapter->slave_tp, sdp_pool, local_sdp,
372
rem_sdp, media_index);
376
* The media_start() is called once both local and remote SDP have been
377
* negotiated successfully, and the media is ready to start. Here we can start
378
* committing our processing.
380
static pj_status_t transport_media_start(pjmedia_transport *tp,
382
const pjmedia_sdp_session *local_sdp,
383
const pjmedia_sdp_session *rem_sdp,
384
unsigned media_index)
386
struct tp_adapter *adapter = (struct tp_adapter*)tp;
390
/* And pass the call to the slave transport */
391
return pjmedia_transport_media_start(adapter->slave_tp, pool, local_sdp,
392
rem_sdp, media_index);
396
* The media_stop() is called when media has been stopped.
398
static pj_status_t transport_media_stop(pjmedia_transport *tp)
400
struct tp_adapter *adapter = (struct tp_adapter*)tp;
404
/* And pass the call to the slave transport */
405
return pjmedia_transport_media_stop(adapter->slave_tp);
409
* simulate_lost() is called to simulate packet lost
411
static pj_status_t transport_simulate_lost(pjmedia_transport *tp,
415
struct tp_adapter *adapter = (struct tp_adapter*)tp;
416
return pjmedia_transport_simulate_lost(adapter->slave_tp, dir, pct_lost);
420
* destroy() is called when the transport is no longer needed.
422
static pj_status_t transport_destroy (pjmedia_transport *tp)
424
struct tp_adapter *adapter = (struct tp_adapter*)tp;
426
/* Close the slave transport */
427
if (adapter->del_base) {
428
pjmedia_transport_close(adapter->slave_tp);
431
/* Self destruct.. */
432
pj_pool_release(adapter->pool);