1 /*
2 * Copyright 2008-2014 Arsen Chaloyan
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * $Id: umcscenario.cpp 2232 2014-11-12 01:33:37Z achaloyan@gmail.com $
17 */
18
19 #include <stdlib.h>
20 #include "umcscenario.h"
21 #include "apt_log.h"
22
UmcScenario()23 UmcScenario::UmcScenario() :
24 m_pName(NULL),
25 m_pMrcpProfile("uni2"),
26 m_pDirLayout(NULL),
27 m_ResourceDiscovery(false),
28 m_pCapabilities(NULL),
29 m_pRtpDescriptor(NULL)
30 {
31 }
32
~UmcScenario()33 UmcScenario::~UmcScenario()
34 {
35 }
36
Load(const apr_xml_elem * pElem,apr_pool_t * pool)37 bool UmcScenario::Load(const apr_xml_elem* pElem, apr_pool_t* pool)
38 {
39 const apr_xml_elem* pChildElem;
40 /* Load Child Elements */
41 for(pChildElem = pElem->first_child; pChildElem; pChildElem = pChildElem->next)
42 {
43 if(!LoadElement(pChildElem,pool))
44 apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Load Child Element %s",pChildElem->name);
45 }
46 return true;
47 }
48
Destroy()49 void UmcScenario::Destroy()
50 {
51 }
52
LoadElement(const apr_xml_elem * pElem,apr_pool_t * pool)53 bool UmcScenario::LoadElement(const apr_xml_elem* pElem, apr_pool_t* pool)
54 {
55 if(strcasecmp(pElem->name,"resource-discovery") == 0)
56 {
57 LoadDiscovery(pElem,pool);
58 return true;
59 }
60 else if(strcasecmp(pElem->name,"termination") == 0)
61 {
62 LoadTermination(pElem,pool);
63 return true;
64 }
65 else if(strcasecmp(pElem->name,"rtp-termination") == 0)
66 {
67 LoadRtpTermination(pElem,pool);
68 return true;
69 }
70
71 return false;
72 }
73
LoadDiscovery(const apr_xml_elem * pElem,apr_pool_t * pool)74 bool UmcScenario::LoadDiscovery(const apr_xml_elem* pElem, apr_pool_t* pool)
75 {
76 m_ResourceDiscovery = IsElementEnabled(pElem);
77 return true;
78 }
79
LoadTermination(const apr_xml_elem * pElem,apr_pool_t * pool)80 bool UmcScenario::LoadTermination(const apr_xml_elem* pElem, apr_pool_t* pool)
81 {
82 if(!IsElementEnabled(pElem))
83 return true;
84
85 const apr_xml_elem* pChildElem;
86 /* Load Child Elements */
87 for(pChildElem = pElem->first_child; pChildElem; pChildElem = pChildElem->next)
88 {
89 if(strcasecmp(pChildElem->name,"capabilities") == 0)
90 return LoadCapabilities(pChildElem,pool);
91 }
92 return true;
93 }
94
LoadCapabilities(const apr_xml_elem * pElem,apr_pool_t * pool)95 bool UmcScenario::LoadCapabilities(const apr_xml_elem* pElem, apr_pool_t* pool)
96 {
97 const apr_xml_elem* pChildElem;
98 /* Load Child Elements */
99 m_pCapabilities = (mpf_codec_capabilities_t*) apr_palloc(pool,sizeof(mpf_codec_capabilities_t));
100 mpf_codec_capabilities_init(m_pCapabilities,1,pool);
101 for(pChildElem = pElem->first_child; pChildElem; pChildElem = pChildElem->next)
102 {
103 if(strcasecmp(pChildElem->name,"codec") != 0)
104 continue;
105
106 const char* pName = NULL;
107 const char* pRates = NULL;
108 const apr_xml_attr* pAttr;
109 for(pAttr = pChildElem->attr; pAttr; pAttr = pAttr->next)
110 {
111 if(strcasecmp(pAttr->name,"name") == 0)
112 {
113 pName = pAttr->value;
114 }
115 else if(strcasecmp(pAttr->name,"rates") == 0)
116 {
117 pRates = pAttr->value;
118 }
119 }
120
121 if(pName)
122 {
123 int rates = ParseRates(pRates,pool);
124 mpf_codec_capabilities_add(m_pCapabilities,rates,pName);
125 }
126 }
127 return true;
128 }
129
ParseRates(const char * pStr,apr_pool_t * pool)130 int UmcScenario::ParseRates(const char* pStr, apr_pool_t* pool)
131 {
132 int rates = 0;
133 if(pStr)
134 {
135 char* pRateStr;
136 char* pState;
137 char* pRateListStr = apr_pstrdup(pool,pStr);
138 do
139 {
140 pRateStr = apr_strtok(pRateListStr, " ", &pState);
141 if(pRateStr)
142 {
143 apr_uint16_t rate = (apr_uint16_t)atoi(pRateStr);
144 rates |= mpf_sample_rate_mask_get(rate);
145 }
146 pRateListStr = NULL; /* make sure we pass NULL on subsequent calls of apr_strtok() */
147 }
148 while(pRateStr);
149 }
150 return rates;
151 }
152
LoadRtpTermination(const apr_xml_elem * pElem,apr_pool_t * pool)153 bool UmcScenario::LoadRtpTermination(const apr_xml_elem* pElem, apr_pool_t* pool)
154 {
155 return true;
156 }
157
InitCapabilities(mpf_stream_capabilities_t * pCapabilities) const158 bool UmcScenario::InitCapabilities(mpf_stream_capabilities_t* pCapabilities) const
159 {
160 if(m_pCapabilities)
161 {
162 int i;
163 mpf_codec_attribs_t *pAttribs;
164 for(i=0; i<m_pCapabilities->attrib_arr->nelts; i++)
165 {
166 pAttribs = &APR_ARRAY_IDX(m_pCapabilities->attrib_arr,i,mpf_codec_attribs_t);
167 mpf_codec_capabilities_add(
168 &pCapabilities->codecs,
169 pAttribs->sample_rates,
170 pAttribs->name.buf);
171 }
172 }
173 else
174 {
175 /* add default codec capabilities (Linear PCM) */
176 mpf_codec_capabilities_add(
177 &pCapabilities->codecs,
178 MPF_SAMPLE_RATE_8000 | MPF_SAMPLE_RATE_16000,
179 "LPCM");
180 }
181
182 return true;
183 }
184
IsElementEnabled(const apr_xml_elem * pElem)185 bool UmcScenario::IsElementEnabled(const apr_xml_elem* pElem)
186 {
187 const apr_xml_attr* pAttr;
188 for(pAttr = pElem->attr; pAttr; pAttr = pAttr->next)
189 {
190 if(strcasecmp(pAttr->name,"enable") == 0)
191 {
192 return atoi(pAttr->value) > 0;
193 }
194 }
195 return true;
196 }
197
LoadFileContent(const char * pFileName,apr_size_t & size,apr_pool_t * pool) const198 const char* UmcScenario::LoadFileContent(const char* pFileName, apr_size_t& size, apr_pool_t* pool) const
199 {
200 if(!m_pDirLayout || !pFileName)
201 return NULL;
202
203 char* pFilePath = apt_datadir_filepath_get(m_pDirLayout,pFileName,pool);
204 if(!pFilePath)
205 return NULL;
206
207 apr_file_t *pFile;
208 if(apr_file_open(&pFile,pFilePath,APR_FOPEN_READ|APR_FOPEN_BINARY,0,pool) != APR_SUCCESS)
209 {
210 apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Open File %s",pFilePath);
211 return NULL;
212 }
213
214 apr_finfo_t finfo;
215 if(apr_file_info_get(&finfo,APR_FINFO_SIZE,pFile) != APR_SUCCESS)
216 {
217 apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Get File Info %s",pFilePath);
218 apr_file_close(pFile);
219 return NULL;
220 }
221
222 size = (apr_size_t)finfo.size;
223 char* pContent = (char*) apr_palloc(pool,size+1);
224 apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Load File Content size [%" APR_SIZE_T_FMT" bytes] %s",size,pFilePath);
225 if(apr_file_read(pFile,pContent,&size) != APR_SUCCESS)
226 {
227 apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Read Content %s",pFilePath);
228 apr_file_close(pFile);
229 return NULL;
230 }
231 pContent[size] = '\0';
232 apr_file_close(pFile);
233 return pContent;
234 }
235