summaryrefslogtreecommitdiffstats
path: root/bindings/_common/ProxyPluginMgr.cxx
blob: 2191b98d6ec7cff38ea408427438c0e02ee1b392 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.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.
*
*/
#include "ProxyPluginMgr.h"

#include "ProxyConsumer.h"
#include "ProxyProducer.h"

//
//	"twrap_proxy_plugin_t" Declarations
//
typedef struct twrap_proxy_plugin_s {
    TSK_DECLARE_OBJECT;
    ProxyPlugin* plugin;
}
twrap_proxy_plugin_t;
#define TWRAP_PROXY_PLUGIN(self) ((twrap_proxy_plugin_t*)(self))
static int pred_find_plugin_by_value(const tsk_list_item_t *item, const void *proxyPlugin);
static twrap_proxy_plugin_t* twrap_proxy_plugin_create(ProxyPlugin** plugin);


//
//	"ProxyPluginMgr" Class Implementation
//
ProxyPluginMgr* ProxyPluginMgr::instance = tsk_null;
static uint64_t __uniqueId = 0;

ProxyPluginMgr::ProxyPluginMgr(ProxyPluginMgrCallback* _callback)
    :callback(_callback)
{
    this->plugins = tsk_list_create();
    if(!this->callback) {
        TSK_DEBUG_WARN("Callback function is Null => You will have big problems as we won't check it before call");
    }
}

ProxyPluginMgr::~ProxyPluginMgr()
{
    if(this == ProxyPluginMgr::instance) {
        ProxyPluginMgr::instance = tsk_null;
    }
    TSK_OBJECT_SAFE_FREE(this->plugins);
}

ProxyPluginMgr* ProxyPluginMgr::createInstance(ProxyPluginMgrCallback* pCallback)
{
    if(!ProxyPluginMgr::instance) {
        ProxyPluginMgr::instance = new ProxyPluginMgr(pCallback);
    }
    else {
        TSK_DEBUG_WARN("Plugin instance already exist");
        ProxyPluginMgr::instance->callback = pCallback;
    }
    return ProxyPluginMgr::instance;
}

void ProxyPluginMgr::destroyInstance(ProxyPluginMgr** ppInstance)
{
    if(ppInstance && *ppInstance) {
        bool bMatch = ProxyPluginMgr::instance && (*ppInstance == ProxyPluginMgr::instance);
        delete *ppInstance, *ppInstance = tsk_null;
        if(bMatch) {
            ProxyPluginMgr::instance = tsk_null;
        }
    }
}

ProxyPluginMgr* ProxyPluginMgr::getInstance()
{
    if(!ProxyPluginMgr::instance) {
        TSK_DEBUG_ERROR("No instance of the manager could be found");
    }
    return ProxyPluginMgr::instance;
}

uint64_t ProxyPluginMgr::getUniqueId()
{
    return ++__uniqueId;
}

int ProxyPluginMgr::addPlugin(ProxyPlugin** plugin)
{
    twrap_proxy_plugin_t* twrap_plugin;
    int ret = -1;

    tsk_list_lock(this->plugins);

    if(!plugin || !*plugin) {
        TSK_DEBUG_ERROR("Invalid parameter");
        goto bail;
    }

    if(tsk_list_find_item_by_pred(this->plugins, pred_find_plugin_by_value, *plugin)) {
        TSK_DEBUG_ERROR("Plugin already exist");
        goto bail;
    }

    if((twrap_plugin = twrap_proxy_plugin_create(plugin))) {
        tsk_list_push_back_data(this->plugins, (void**)&twrap_plugin);
        ret = 0;
    }
    else {
        TSK_DEBUG_ERROR("Failed to create plugin");
        goto bail;
    }

bail:
    tsk_list_unlock(this->plugins);

    return ret;
}

int ProxyPluginMgr::removePlugin(ProxyPlugin** plugin)
{
    if(!plugin || !*plugin) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    return this->removePlugin((*plugin)->getId());
}

const ProxyPlugin* ProxyPluginMgr::findPlugin(uint64_t id)
{
    ProxyPlugin* ret = tsk_null;

    tsk_list_item_t* item;

    tsk_list_lock(this->plugins);
    tsk_list_foreach(item, this->plugins) {
        if(TWRAP_PROXY_PLUGIN(item->data)->plugin->getId() == id) {
            ret = TWRAP_PROXY_PLUGIN(item->data)->plugin;
            break;
        }
    }
    tsk_list_unlock(this->plugins);

    return ret;
}

const ProxyPlugin* ProxyPluginMgr::findPlugin(tsk_object_t* wrapped_plugin)
{
    ProxyPlugin* ret = tsk_null;

    tsk_list_item_t* item;

    if(!wrapped_plugin) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return tsk_null;
    }

    tsk_list_lock(this->plugins);
    tsk_list_foreach(item, this->plugins) {
        if(TWRAP_PROXY_PLUGIN(item->data)->plugin->isWrapping(wrapped_plugin)) {
            ret = TWRAP_PROXY_PLUGIN(item->data)->plugin;
            break;
        }
    }
    tsk_list_unlock(this->plugins);

    return ret;
}

int ProxyPluginMgr::removePlugin(uint64_t id)
{
    tsk_list_item_t* item;

    tsk_list_lock(this->plugins);

    tsk_list_foreach(item, this->plugins) {
        if(TWRAP_PROXY_PLUGIN(item->data)->plugin->getId() == id) {
            tsk_list_remove_item(this->plugins, item);
            break;
        }
    }

    tsk_list_unlock(this->plugins);
    return 0;
}

const ProxyAudioConsumer* ProxyPluginMgr::findAudioConsumer(uint64_t id)
{
    const ProxyPlugin* audioConsumer = this->findPlugin(id);
    if(audioConsumer && audioConsumer->getType() == twrap_proxy_plugin_audio_consumer) {
        return dyn_cast<const ProxyAudioConsumer*>(audioConsumer);
    }
    return tsk_null;
}

const ProxyVideoConsumer* ProxyPluginMgr::findVideoConsumer(uint64_t id)
{
    const ProxyPlugin* videoConsumer = this->findPlugin(id);
    if(videoConsumer && videoConsumer->getType() == twrap_proxy_plugin_video_consumer) {
        return dyn_cast<const ProxyVideoConsumer*>(videoConsumer);
    }
    return tsk_null;
}

const ProxyAudioProducer* ProxyPluginMgr::findAudioProducer(uint64_t id)
{
    const ProxyPlugin* audioProducer = this->findPlugin(id);
    if(audioProducer && audioProducer->getType() == twrap_proxy_plugin_audio_producer) {
        return dyn_cast<const ProxyAudioProducer*>(audioProducer);
    }
    return tsk_null;
}

const ProxyVideoProducer* ProxyPluginMgr::findVideoProducer(uint64_t id)
{
    const ProxyPlugin* videoProducer = this->findPlugin(id);
    if(videoProducer && videoProducer->getType() == twrap_proxy_plugin_video_producer) {
        return dyn_cast<const ProxyVideoProducer*>(videoProducer);
    }
    return tsk_null;
}


//
//	"twrap_proxy_plugin_t" Implementations
//
static tsk_object_t* twrap_proxy_plugin_ctor(tsk_object_t * self, va_list * app)
{
    twrap_proxy_plugin_t *_self = dyn_cast<twrap_proxy_plugin_t *>(TWRAP_PROXY_PLUGIN(self));
    if(_self) {
    }
    return self;
}

static tsk_object_t* twrap_proxy_plugin_dtor(tsk_object_t * self)
{
    twrap_proxy_plugin_t *_self = dyn_cast<twrap_proxy_plugin_t *>(TWRAP_PROXY_PLUGIN(self));
    if(_self) {
        if(_self->plugin) {
            delete _self->plugin, _self->plugin = tsk_null;
        }
    }

    return self;
}

static int twrap_proxy_plugin_cmp(const tsk_object_t *_c1, const tsk_object_t *_c2)
{
    const twrap_proxy_plugin_t *c1 = dyn_cast<const twrap_proxy_plugin_t *>(TWRAP_PROXY_PLUGIN(_c1));
    const twrap_proxy_plugin_t *c2 = dyn_cast<const twrap_proxy_plugin_t *>(TWRAP_PROXY_PLUGIN(_c2));

    if(c1 && c2) {
        return (c1->plugin == c2->plugin); // See "ProxyPlugin::operator =="
    }
    else if(!c1 && !c2) {
        return 0;
    }
    else {
        return -1;
    }
}

static const tsk_object_def_t twrap_proxy_plugin_def_s = {
    sizeof(twrap_proxy_plugin_t),
    twrap_proxy_plugin_ctor,
    twrap_proxy_plugin_dtor,
    twrap_proxy_plugin_cmp,
};
const tsk_object_def_t *twrap_proxy_plugin_def_t = &twrap_proxy_plugin_def_s;

static int pred_find_plugin_by_value(const tsk_list_item_t *item, const void *proxyPlugin)
{
    if(item && item->data) {
        const twrap_proxy_plugin_t *twrap_plugin = dyn_cast<const twrap_proxy_plugin_t *>(TWRAP_PROXY_PLUGIN(item->data));
        return (twrap_plugin->plugin == dyn_cast<const ProxyPlugin *>((const ProxyPlugin*)proxyPlugin)) ? 0 : -1;
    }
    return -1;
}

static twrap_proxy_plugin_t* twrap_proxy_plugin_create(ProxyPlugin** plugin)
{
    if(!plugin || !*plugin) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return tsk_null;
    }

    twrap_proxy_plugin_t* twrap_plugin = (twrap_proxy_plugin_t*)tsk_object_new(twrap_proxy_plugin_def_t);
    if(!twrap_plugin) {
        TSK_DEBUG_ERROR("Failed to create new instance of 'twrap_proxy_plugin_t'");
        return tsk_null;
    }

    twrap_plugin->plugin = *plugin,
                  *plugin = tsk_null;
    return twrap_plugin;
}

OpenPOWER on IntegriCloud