comparison utilities/vampipe-server.cpp @ 34:ba58fe5ee2dd

Check channel count & block size
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 25 May 2016 11:09:18 +0100
parents 0b48b10140bb
children 55d69b26d4db
comparison
equal deleted inserted replaced
33:0b48b10140bb 34:ba58fe5ee2dd
44 if (m_plugins.find(h) == m_plugins.end()) { 44 if (m_plugins.find(h) == m_plugins.end()) {
45 throw NotFound(); 45 throw NotFound();
46 } 46 }
47 Plugin *p = m_plugins[h]; 47 Plugin *p = m_plugins[h];
48 m_plugins.erase(h); 48 m_plugins.erase(h);
49 if (isConfigured(h)) {
50 m_configuredPlugins.erase(h);
51 m_channelCounts.erase(h);
52 }
49 m_rplugins.erase(p); 53 m_rplugins.erase(p);
50 } 54 }
51 55
52 int32_t pluginToHandle(Plugin *p) { 56 int32_t pluginToHandle(Plugin *p) {
53 if (m_rplugins.find(p) == m_rplugins.end()) { 57 if (m_rplugins.find(p) == m_rplugins.end()) {
65 69
66 bool isConfigured(int32_t h) { 70 bool isConfigured(int32_t h) {
67 return m_configuredPlugins.find(h) != m_configuredPlugins.end(); 71 return m_configuredPlugins.find(h) != m_configuredPlugins.end();
68 } 72 }
69 73
70 void markConfigured(int32_t h) { 74 void markConfigured(int32_t h, int channelCount, int blockSize) {
71 m_configuredPlugins.insert(h); 75 m_configuredPlugins.insert(h);
76 m_channelCounts[h] = channelCount;
77 m_blockSizes[h] = blockSize;
78 }
79
80 int getChannelCount(int32_t h) {
81 if (m_channelCounts.find(h) == m_channelCounts.end()) {
82 throw NotFound();
83 }
84 return m_channelCounts[h];
85 }
86
87 int getBlockSize(int32_t h) {
88 if (m_blockSizes.find(h) == m_blockSizes.end()) {
89 throw NotFound();
90 }
91 return m_blockSizes[h];
72 } 92 }
73 93
74 private: 94 private:
75 int32_t m_nextHandle; // NB plugin handle type must fit in JSON number 95 int32_t m_nextHandle; // NB plugin handle type must fit in JSON number
76 map<uint32_t, Plugin *> m_plugins; 96 map<uint32_t, Plugin *> m_plugins;
77 map<Plugin *, uint32_t> m_rplugins; 97 map<Plugin *, uint32_t> m_rplugins;
78 set<uint32_t> m_configuredPlugins; 98 set<uint32_t> m_configuredPlugins;
99 map<uint32_t, int> m_channelCounts;
100 map<uint32_t, int> m_blockSizes;
79 }; 101 };
80 102
81 static Mapper mapper; 103 static Mapper mapper;
82 104
83 RequestOrResponse 105 RequestOrResponse
153 175
154 writeMessageToFd(1, message); 176 writeMessageToFd(1, message);
155 } 177 }
156 178
157 RequestOrResponse 179 RequestOrResponse
158 processRequest(const RequestOrResponse &request) 180 handleRequest(const RequestOrResponse &request)
159 { 181 {
160 RequestOrResponse response; 182 RequestOrResponse response;
161 response.direction = RequestOrResponse::Response; 183 response.direction = RequestOrResponse::Response;
162 response.type = request.type; 184 response.type = request.type;
163 185
178 } 200 }
179 break; 201 break;
180 202
181 case RRType::Configure: 203 case RRType::Configure:
182 { 204 {
183 auto h = mapper.pluginToHandle(request.configurationRequest.plugin); 205 auto &creq = request.configurationRequest;
206 auto h = mapper.pluginToHandle(creq.plugin);
184 if (mapper.isConfigured(h)) { 207 if (mapper.isConfigured(h)) {
185 throw runtime_error("plugin has already been configured"); 208 throw runtime_error("plugin has already been configured");
186 } 209 }
187 210
188 response.configurationResponse = 211 response.configurationResponse = loader->configurePlugin(creq);
189 loader->configurePlugin(request.configurationRequest);
190 212
191 if (!response.configurationResponse.outputs.empty()) { 213 if (!response.configurationResponse.outputs.empty()) {
192 mapper.markConfigured(h); 214 mapper.markConfigured
215 (h, creq.configuration.channelCount, creq.configuration.blockSize);
193 response.success = true; 216 response.success = true;
194 } 217 }
195 break; 218 break;
196 } 219 }
197 220
198 case RRType::Process: 221 case RRType::Process:
199 { 222 {
200 auto &preq = request.processRequest; 223 auto &preq = request.processRequest;
224 auto h = mapper.pluginToHandle(preq.plugin);
225 if (!mapper.isConfigured(h)) {
226 throw runtime_error("plugin has not been configured");
227 }
228
201 int channels = int(preq.inputBuffers.size()); 229 int channels = int(preq.inputBuffers.size());
230 if (channels != mapper.getChannelCount(h)) {
231 throw runtime_error("wrong number of channels supplied to process");
232 }
233
202 const float **fbuffers = new const float *[channels]; 234 const float **fbuffers = new const float *[channels];
203 for (int i = 0; i < channels; ++i) { 235 for (int i = 0; i < channels; ++i) {
236 if (int(preq.inputBuffers[i].size()) != mapper.getBlockSize(h)) {
237 delete[] fbuffers;
238 throw runtime_error("wrong block size supplied to process");
239 }
204 fbuffers[i] = preq.inputBuffers[i].data(); 240 fbuffers[i] = preq.inputBuffers[i].data();
205 } 241 }
206 242
207 response.processResponse.features = 243 response.processResponse.features =
208 preq.plugin->process(fbuffers, preq.timestamp); 244 preq.plugin->process(fbuffers, preq.timestamp);
252 if (request.type == RRType::NotValid) { 288 if (request.type == RRType::NotValid) {
253 cerr << "vampipe-server: eof reached" << endl; 289 cerr << "vampipe-server: eof reached" << endl;
254 break; 290 break;
255 } 291 }
256 292
257 RequestOrResponse response = processRequest(request); 293 RequestOrResponse response = handleRequest(request);
258 294
259 cerr << "vampipe-server: request processed, writing response" 295 cerr << "vampipe-server: request handled, writing response"
260 << endl; 296 << endl;
261 297
262 writeResponseCapnp(response); 298 writeResponseCapnp(response);
263 299
264 cerr << "vampipe-server: response written" << endl; 300 cerr << "vampipe-server: response written" << endl;