a6de266806f955d9335bb90013b6bde6eb1075b9
[profile/ivi/audiomanager.git] / AudioManagerDaemon / src / RoutingSender.cpp
1 /**
2 * Copyright (C) 2011, BMW AG
3 *
4 * GeniviAudioMananger AudioManagerDaemon
5 *
6 * \file RoutingSender.h
7 *
8 * \date 20-Oct-2011 3:42:04 PM
9 * \author Christian Mueller (christian.ei.mueller@bmw.de)
10 *
11 * \section License
12 * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13 * Copyright (C) 2011, BMW AG Christian Mueller  Christian.ei.mueller@bmw.de
14 *
15 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
16 * This program 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 Lesser General Public License, version 2.1, for more details.
17 * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
18 * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
19 * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
20 * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
21 * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
22 *
23 */
24
25 #include "RoutingSender.h"
26 #include <utility>
27 #include <dirent.h>
28 #include <dlfcn.h>
29 #include <dlt/dlt.h>
30 #include "PluginTemplate.h"
31
32 using namespace am;
33
34 #define CALL_ALL_INTERFACES(...)                                                                                                                 \
35                 std::vector<InterfaceNamePairs>::iterator iter = mListInterfaces.begin();                \
36                 std::vector<InterfaceNamePairs>::iterator iterEnd = mListInterfaces.end();               \
37                 for (; iter<iterEnd;++iter)                                                                                                      \
38                 {                                                                                                                                                                \
39                         (*iter).routingInterface->__VA_ARGS__;                                                                           \
40                 }
41
42 RoutingSender::RoutingSender(const std::vector<std::string>& listOfPluginDirectories)
43
44         :mHandleCount(0),
45          mlistActiveHandles(),
46          mListInterfaces(),
47          mMapConnectionInterface(),
48          mMapCrossfaderInterface(),
49          mMapDomainInterface(),
50          mMapSinkInterface(),
51          mMapSourceInterface(),
52          mMapHandleInterface()
53 {
54         std::vector<std::string> sharedLibraryNameList;
55     std::vector<std::string>::const_iterator dirIter = listOfPluginDirectories.begin();
56     std::vector<std::string>::const_iterator dirIterEnd = listOfPluginDirectories.end();
57
58     // search communicator plugins in configured directories
59     for (; dirIter < dirIterEnd; ++dirIter)
60     {
61                 const char* directoryName = dirIter->c_str();
62                 //DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("Searching for HookPlugins in"),DLT_STRING(directoryName));
63                 DIR *directory = opendir(directoryName);
64
65                 if (!directory)
66                 {
67                         //DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("Error opening directory "),DLT_STRING(dirName.c_str()));
68                 }
69
70         // iterate content of directory
71         struct dirent *itemInDirectory = 0;
72         while ((itemInDirectory = readdir(directory)))
73         {
74                         unsigned char entryType = itemInDirectory->d_type;
75                         std::string entryName = itemInDirectory->d_name;
76
77                         bool regularFile = (entryType == DT_REG);
78                         bool sharedLibExtension = ("so" == entryName.substr(entryName.find_last_of(".") + 1));
79
80                         if (regularFile && sharedLibExtension)
81                         {
82                         //      DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("PluginSearch adding file "),DLT_STRING(entryName.c_str()));
83                           std::string name(directoryName);
84                           sharedLibraryNameList.push_back(name + "/" + entryName);
85                         }
86                         else
87                         {
88                         //DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("PluginSearch ignoring file "),DLT_STRING(entryName.c_str()));
89                         }
90         }
91
92           closedir(directory);
93     }
94
95     // iterate all communicator plugins and start them
96     std::vector<std::string>::iterator iter = sharedLibraryNameList.begin();
97     std::vector<std::string>::iterator iterEnd = sharedLibraryNameList.end();
98
99     for (; iter != iterEnd; ++iter)
100     {
101         //DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("Loading Hook plugin"),DLT_STRING(iter->c_str()));
102
103         RoutingSendInterface* (*createFunc)();
104         void* tempLibHandle=NULL;
105         createFunc = getCreateFunction<RoutingSendInterface*()>(*iter,tempLibHandle);
106
107         if (!createFunc)
108         {
109            // DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("Entry point of Communicator not found"));
110             continue;
111         }
112
113         RoutingSendInterface* router = createFunc();
114
115         if (!router)
116         {
117                 //DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("HookPlugin initialization failed. Entry Function not callable"));
118             continue;
119         }
120
121         InterfaceNamePairs routerInterface;
122         routerInterface.routingInterface = router;
123
124         //here, the busname is saved together with the interface. Later The domains will register with the name and sinks, sources etc with the domain....
125         router->returnBusName(routerInterface.busName);
126         mListInterfaces.push_back(routerInterface);
127         mListLibraryHandles.push_back(tempLibHandle);
128     }
129     //now all plugins are loaded, so the interface is ready
130 }
131
132 RoutingSender::~RoutingSender()
133 {
134         unloadLibraries();
135 }
136
137 void RoutingSender::routingInterfacesReady()
138 {
139         CALL_ALL_INTERFACES(routingInterfacesReady())
140 }
141
142 void RoutingSender::routingInterfacesRundown()
143 {
144         CALL_ALL_INTERFACES(routingInterfacesRundown())
145 }
146
147 void RoutingSender::startupRoutingInterface(RoutingReceiveInterface *routingreceiveinterface)
148 {
149         CALL_ALL_INTERFACES(startupRoutingInterface(routingreceiveinterface))
150 }
151
152 am_Error_e RoutingSender::asyncAbort(const am_Handle_s& handle)
153 {
154         HandleInterfaceMap::iterator iter = mMapHandleInterface.begin();
155         iter=mMapHandleInterface.find(handle.handle);
156     if (iter != mMapHandleInterface.end())
157     {
158         return iter->second->asyncAbort(handle);
159     }
160
161     return E_NON_EXISTENT;
162 }
163
164
165
166 am_Error_e RoutingSender::asyncConnect(am_Handle_s& handle, const am_connectionID_t connectionID, const am_sourceID_t sourceID, const am_sinkID_t sinkID, const am_ConnectionFormat_e connectionFormat)
167 {
168         am_handleData_c handleData;
169         SinkInterfaceMap::iterator iter = mMapSinkInterface.begin();
170         iter=mMapSinkInterface.find(sinkID);
171     if (iter != mMapSinkInterface.end())
172     {
173         handleData.connectionID=connectionID;
174         handle=createHandle(handleData,H_CONNECT);
175         mMapConnectionInterface.insert(std::make_pair(connectionID,iter->second));
176         mMapHandleInterface.insert(std::make_pair(handle.handle,iter->second));
177         return iter->second->asyncConnect(handle,connectionID,sourceID,sinkID,connectionFormat);
178     }
179
180     return E_NON_EXISTENT;
181 }
182
183
184
185 am_Error_e RoutingSender::asyncDisconnect(am_Handle_s& handle, const am_connectionID_t connectionID)
186 {
187         am_handleData_c handleData;
188         ConnectionInterfaceMap::iterator iter = mMapConnectionInterface.begin();
189         mMapConnectionInterface.find(connectionID);
190     if (iter != mMapConnectionInterface.end())
191     {
192         handleData.connectionID=connectionID;
193         handle=createHandle(handleData,H_DISCONNECT);
194         mMapHandleInterface.insert(std::make_pair(handle.handle,iter->second));
195         am_Error_e returnVal=iter->second->asyncDisconnect(handle,connectionID);
196         mMapConnectionInterface.erase(iter);
197         return returnVal;
198     }
199
200     return E_NON_EXISTENT;
201 }
202
203
204
205 am_Error_e RoutingSender::asyncSetSinkVolume(am_Handle_s& handle, const am_sinkID_t sinkID, const am_volume_t volume, const am_RampType_e ramp, const am_time_t time)
206 {
207         am_handleData_c handleData;
208         SinkInterfaceMap::iterator iter = mMapSinkInterface.begin();
209         iter=mMapSinkInterface.find(sinkID);
210     if (iter != mMapSinkInterface.end())
211         handleData.sinkID=sinkID;
212         handleData.volume=volume;
213         handle=createHandle(handleData,H_SETSINKVOLUME);
214                 mMapHandleInterface.insert(std::make_pair(handle.handle,iter->second));
215         return iter->second->asyncSetSinkVolume(handle,sinkID,volume,ramp,time);
216     return E_NON_EXISTENT;
217 }
218
219
220
221 am_Error_e RoutingSender::asyncSetSourceVolume(am_Handle_s& handle, const am_sourceID_t sourceID, const am_volume_t volume, const am_RampType_e ramp, const am_time_t time)
222 {
223         am_handleData_c handleData;
224         SourceInterfaceMap::iterator iter = mMapSourceInterface.begin();
225         iter=mMapSourceInterface.find(sourceID);
226     if (iter != mMapSourceInterface.end())
227         handleData.sourceID=sourceID;
228         handleData.volume=volume;
229         handle=createHandle(handleData,H_SETSOURCEVOLUME);
230                 mMapHandleInterface.insert(std::make_pair(handle.handle,iter->second));
231         return iter->second->asyncSetSourceVolume(handle,sourceID,volume,ramp,time);
232     return E_NON_EXISTENT;
233 }
234
235
236
237 am_Error_e RoutingSender::asyncSetSourceState(am_Handle_s& handle, const am_sourceID_t sourceID, const am_SourceState_e state)
238 {
239         am_handleData_c handleData;
240         SourceInterfaceMap::iterator iter = mMapSourceInterface.begin();
241         iter=mMapSourceInterface.find(sourceID);
242     if (iter != mMapSourceInterface.end())
243         handleData.sourceID=sourceID;
244         handleData.sourceState=state;
245         handle=createHandle(handleData,H_SETSOURCESTATE);
246                 mMapHandleInterface.insert(std::make_pair(handle.handle,iter->second));
247         return iter->second->asyncSetSourceState(handle,sourceID,state);
248     return E_NON_EXISTENT;
249 }
250
251
252
253 am_Error_e RoutingSender::asyncSetSinkSoundProperty(am_Handle_s& handle, const am_sinkID_t sinkID, const am_SoundProperty_s & soundProperty)
254 {
255         am_handleData_c handleData;
256         SinkInterfaceMap::iterator iter = mMapSinkInterface.begin();
257         iter=mMapSinkInterface.find(sinkID);
258     if (iter != mMapSinkInterface.end())
259         handleData.sinkID=sinkID;
260         handleData.soundPropery=soundProperty;
261         handle=createHandle(handleData,H_SETSINKSOUNDPROPERTY);
262                 mMapHandleInterface.insert(std::make_pair(handle.handle,iter->second));
263         return iter->second->asyncSetSinkSoundProperty(handle,soundProperty,sinkID);
264     return E_NON_EXISTENT;
265 }
266
267
268
269 am_Error_e RoutingSender::asyncSetSourceSoundProperty(am_Handle_s& handle, const am_sourceID_t sourceID, const am_SoundProperty_s & soundProperty)
270 {
271         am_handleData_c handleData;
272         SourceInterfaceMap::iterator iter = mMapSourceInterface.begin();
273         iter=mMapSourceInterface.find(sourceID);
274     if (iter != mMapSourceInterface.end())
275         handleData.sourceID=sourceID;
276         handleData.soundPropery=soundProperty;
277         handle=createHandle(handleData,H_SETSOURCESOUNDPROPERTY);
278                 mMapHandleInterface.insert(std::make_pair(handle.handle,iter->second));
279         return iter->second->asyncSetSourceSoundProperty(handle,soundProperty,sourceID);
280     return E_NON_EXISTENT;
281 }
282
283
284
285 am_Error_e RoutingSender::asyncCrossFade(am_Handle_s& handle, const am_crossfaderID_t crossfaderID, const am_HotSink_e hotSink, const am_RampType_e rampType, const am_time_t time)
286 {
287         am_handleData_c handleData;
288         CrossfaderInterfaceMap::iterator iter = mMapCrossfaderInterface.begin();
289         iter=mMapCrossfaderInterface.find(crossfaderID);
290     if (iter != mMapCrossfaderInterface.end())
291         handleData.crossfaderID=crossfaderID;
292         handleData.hotSink=hotSink;
293         handle=createHandle(handleData,H_CROSSFADE);
294                 mMapHandleInterface.insert(std::make_pair(handle.handle,iter->second));
295         return iter->second->asyncCrossFade(handle,crossfaderID,hotSink,rampType,time);
296     return E_NON_EXISTENT;
297 }
298
299
300
301 am_Error_e RoutingSender::setDomainState(const am_domainID_t domainID, const am_DomainState_e domainState)
302 {
303         DomainInterfaceMap::iterator iter = mMapDomainInterface.begin();
304         iter=mMapDomainInterface.find(domainID);
305     if (iter != mMapDomainInterface.end())
306         return iter->second->setDomainState(domainID,domainState);
307     return E_NON_EXISTENT;
308 }
309
310 am_Error_e RoutingSender::addDomainLookup(const am_Domain_s& domainData)
311 {
312         std::vector<InterfaceNamePairs>::iterator iter = mListInterfaces.begin();
313         std::vector<InterfaceNamePairs>::iterator iterEnd = mListInterfaces.end();
314         for (; iter<iterEnd;++iter)
315         {
316                 if((*iter).busName.compare(domainData.busname) == 0)
317                 {
318                         mMapDomainInterface.insert(std::make_pair(domainData.domainID,(*iter).routingInterface));
319                         return E_OK;
320                 }
321         }
322
323         return E_UNKNOWN;
324 }
325
326
327
328 am_Error_e RoutingSender::addSourceLookup(const am_Source_s& sourceData)
329 {
330         DomainInterfaceMap::iterator iter = mMapDomainInterface.begin();
331         iter=mMapDomainInterface.find(sourceData.domainID);
332     if (iter != mMapDomainInterface.end())
333     {
334         mMapSourceInterface.insert(std::make_pair(sourceData.sourceID,iter->second));
335         return E_OK;
336     }
337
338     return E_UNKNOWN;
339 }
340
341
342
343 am_Error_e RoutingSender::addSinkLookup(const am_Sink_s& sinkData)
344 {
345         DomainInterfaceMap::iterator iter = mMapDomainInterface.begin();
346         iter=mMapDomainInterface.find(sinkData.domainID);
347     if (iter != mMapDomainInterface.end())
348     {
349         mMapSinkInterface.insert(std::make_pair(sinkData.sinkID,iter->second));
350         return E_OK;
351     }
352
353     return E_UNKNOWN;
354 }
355
356
357
358 am_Error_e RoutingSender::addCrossfaderLookup(const am_Crossfader_s& crossfaderData)
359 {
360         DomainInterfaceMap::iterator iter = mMapSourceInterface.begin();
361         iter=mMapSourceInterface.find(crossfaderData.sourceID);
362     if (iter != mMapSourceInterface.end())
363     {
364         mMapSourceInterface.insert(std::make_pair(crossfaderData.crossfaderID,iter->second));
365         return E_OK;
366     }
367
368     return E_UNKNOWN;
369 }
370
371 am_Error_e RoutingSender::removeDomainLookup(const am_domainID_t domainID)
372 {
373         DomainInterfaceMap::iterator iter = mMapDomainInterface.begin();
374         iter=mMapDomainInterface.find(domainID);
375     if (iter != mMapDomainInterface.end())
376     {
377         mMapDomainInterface.erase(iter);
378         return E_OK;
379     }
380
381     return E_NON_EXISTENT;
382 }
383
384
385
386 am_Error_e RoutingSender::removeSourceLookup(const am_sourceID_t sourceID)
387 {
388         SourceInterfaceMap::iterator iter = mMapSourceInterface.begin();
389         iter=mMapSourceInterface.find(sourceID);
390     if (iter != mMapSourceInterface.end())
391     {
392         mMapSourceInterface.erase(iter);
393         return E_OK;
394     }
395
396     return E_NON_EXISTENT;
397 }
398
399
400
401 am_Error_e RoutingSender::removeSinkLookup(const am_sinkID_t sinkID)
402 {
403         SinkInterfaceMap::iterator iter = mMapSinkInterface.begin();
404         iter=mMapSinkInterface.find(sinkID);
405     if (iter != mMapSinkInterface.end())
406     {
407         mMapSinkInterface.erase(iter);
408         return E_OK;
409     }
410
411     return E_NON_EXISTENT;
412 }
413
414
415
416 am_Error_e RoutingSender::removeCrossfaderLookup(const am_crossfaderID_t crossfaderID)
417 {
418         CrossfaderInterfaceMap::iterator iter = mMapCrossfaderInterface.begin();
419         iter=mMapCrossfaderInterface.find(crossfaderID);
420     if (iter != mMapCrossfaderInterface.end())
421     {
422         mMapCrossfaderInterface.erase(iter);
423         return E_OK;
424     }
425
426     return E_NON_EXISTENT;
427 }
428
429
430 am_Error_e RoutingSender::removeHandle(const am_Handle_s& handle)
431 {
432         if(mlistActiveHandles.erase(handle)) return E_OK;
433         return E_UNKNOWN;
434 }
435
436 am_Error_e RoutingSender::getListHandles(std::vector<am_Handle_s> & listHandles) const
437 {
438         listHandles.clear();
439         HandlesMap::const_iterator it=mlistActiveHandles.begin();
440         for(;it!=mlistActiveHandles.end();++it)
441         {
442                 listHandles.push_back(it->first);
443         }
444         return E_OK;
445 }
446
447 am_Handle_s RoutingSender::createHandle(const am_handleData_c& handleData, const am_Handle_e type)
448 {
449         am_Handle_s handle;
450         handle.handle=++mHandleCount; //todo: handle overflows here...
451         handle.handleType=type;
452         mlistActiveHandles.insert(std::make_pair(handle,handleData));
453         return handle;
454 }
455
456 RoutingSender::am_handleData_c RoutingSender::returnHandleData(am_Handle_s handle)
457 {
458         HandlesMap::iterator it=mlistActiveHandles.begin();
459         it=mlistActiveHandles.find(handle);
460         return (it->second);
461 }
462
463 void RoutingSender::unloadLibraries(void)
464 {
465         std::vector<void*>::iterator iterator=mListLibraryHandles.begin();
466         for(;iterator<mListLibraryHandles.end();++iterator)
467         {
468                 dlclose(*iterator);
469         }
470         mListLibraryHandles.clear();
471 }
472
473
474
475
476
477
478
479
480
481
482