summaryrefslogtreecommitdiffstats
path: root/tinyMEDIA/src/tmedia.c
blob: daa907fd9c92f0710de35493b8abdf5932a558c4 (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO 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.
*
* DOUBANGO 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 DOUBANGO.
*
*/

/**@file tmedia.c
 * @brief Media.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
 *
 */
#include "tinymedia/tmedia.h"

#if 0

#include "tsk_string.h"
#include "tsk_memory.h"
#include "tsk_debug.h"

#define TMED_MAX_PLUGINS			10
const tmedia_plugin_def_t* __tmedia_plugins[TMED_MAX_PLUGINS] = {0};


tmedia_t* tmedia_create(const char* name, const char* host, tnet_socket_type_t socket_type)
{
    return tsk_object_new(TMEDIA_VA_ARGS(name, host, socket_type));
}

tmedia_t* tmedia_create_null()
{
    return tmedia_create(tsk_null, TNET_SOCKET_HOST_ANY, tnet_socket_type_invalid);
}

int tmedia_init(tmedia_t* self, const char* name)
{
    if(!self) {
        return -1;
    }

    tsk_strupdate(&self->name, name);

    return 0;
}

int tmedia_deinit(tmedia_t* self)
{
    if(!self) {
        return -1;
    }

    TSK_FREE(self->name);
    TSK_FREE(self->protocol);

    return 0;
}


int tmedia_plugin_register(const tmedia_plugin_def_t* plugin)
{
    tsk_size_t i;
    if(!plugin) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }

    for(i=0; i<TMED_MAX_PLUGINS; i++) {
        if(!__tmedia_plugins[i] || __tmedia_plugins[i] == plugin) {
            __tmedia_plugins[i] = plugin;
            return 0;
        }
    }

    TSK_DEBUG_ERROR("There are already %d plugins.", TMED_MAX_PLUGINS);
    return -2;
}

tmedia_t* tmedia_factory_create(const char* name, const char* host, tnet_socket_type_t socket_type)
{
    tmedia_t* ret =  tsk_null;
    const tmedia_plugin_def_t* plugin;
    tsk_size_t i = 0;

    while((i < TMED_MAX_PLUGINS) && (plugin = __tmedia_plugins[i++])) {
        if(plugin->objdef && tsk_strequals(plugin->name, name)) {
            if((ret = tsk_object_new(plugin->objdef, name, host, socket_type))) {
                ret->plugin = plugin;
                break;
            }
        }
    }

    return ret;
}

int tmedia_start(tmedia_t* self)
{
    if(!self || !self->plugin) {
        return -1;
    }

    if(!self->plugin->start) {
        return -2;
    }
    else {
        return self->plugin->start(self);
    }
}
int tmedia_pause(tmedia_t*self )
{
    if(!self || !self->plugin) {
        return -1;
    }

    if(!self->plugin->pause) {
        return -2;
    }
    else {
        return self->plugin->pause(self);
    }
}
int tmedia_stop(tmedia_t* self)
{
    if(!self || !self->plugin) {
        return -1;
    }

    if(!self->plugin->stop) {
        return -2;
    }
    else {
        return self->plugin->stop(self);
    }
}

// Only SDP headers
const tsdp_header_M_t* tmedia_get_local_offer(tmedia_t* self, ...)
{

    if(!self || !self->plugin) {
        return tsk_null;
    }

    if(!self->plugin->get_local_offer) {
        return tsk_null;
    }
    else {
        va_list ap;
        const tsdp_header_M_t* M;
        va_start(ap, self);
        M = self->plugin->get_local_offer(self, &ap);
        va_end(ap);
        return M;
    }
}

const tsdp_header_M_t* tmedia_get_negotiated_offer(tmedia_t* self)
{
    if(!self || !self->plugin) {
        return tsk_null;
    }

    if(!self->plugin->get_negotiated_offer) {
        return tsk_null;
    }
    else {
        return self->plugin->get_negotiated_offer(self);
    }
}

int tmedia_set_remote_offer(tmedia_t* self, const tsdp_message_t* offer)
{
    if(!self || !self->plugin) {
        return -1;
    }

    if(!self->plugin->set_remote_offer) {
        return -2;
    }
    else {
        return self->plugin->set_remote_offer(self, offer);
    }
}

int tmedia_perform(tmedia_t* self, tmedia_action_t action, ... )
{
    int ret = -1;

    if(!self || !self->plugin) {
        return -1;
    }

    if(!self->plugin->perform) {
        return -2;
    }
    else {
        const tsk_object_def_t* objdef;
        tsk_param_t *param;
        tsk_params_L_t* params;
        va_list ap;

        va_start(ap, action);
        params = tsk_list_create();
        while((objdef = va_arg(ap, const tsk_object_def_t*))) {
            if(objdef != tsk_param_def_t) { // sanity check
                break;
            }
            if((param = tsk_object_new_2(objdef, &ap))) {
                tsk_params_add_param_2(&params, param);
                TSK_OBJECT_SAFE_FREE(param);
            }
        }

        // Perform
        ret = self->plugin->perform(self, action, params);

        TSK_OBJECT_SAFE_FREE(params);
        va_end(ap);

        return ret;
    }
}

//========================================================
//	Media object definition
//

static void* tmedia_ctor(tsk_object_t *self, va_list * app)
{
    tmedia_t *media = self;
    if(media) {
        const char* name = va_arg(*app, const char*);
        const char* host = va_arg(*app, const char*);
        tnet_socket_type_t socket_type = va_arg(*app, tnet_socket_type_t);

        tmedia_init(media, name);
    }
    else {
        TSK_DEBUG_ERROR("Failed to create new media.");
    }
    return self;
}

static void* tmedia_dtor(tsk_object_t *self)
{
    tmedia_t *media = self;
    if(media) {
        tmedia_deinit(media);
    }
    else {
        TSK_DEBUG_ERROR("Null media.");
    }

    return self;
}

static int tmedia_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
{
    return -1;
}

static const tsk_object_def_t tmedia_def_s = {
    sizeof(tmedia_t),
    tmedia_ctor,
    tmedia_dtor,
    tmedia_cmp
};

const void *tmedia_def_t = &tmedia_def_s;

#endif /* if 0 => FIXME: Remove this file */

OpenPOWER on IntegriCloud