Changeset 35402 in vbox
- Timestamp:
- Jan 4, 2011 2:59:31 PM (14 years ago)
- svn:sync-xref-src-repo-rev:
- 69266
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Devices/Audio/audiosniffer.c
r35353 r35402 141 141 void *rate; 142 142 143 /* Temporary buffer for st_sample_t representation of the input audio data. */ 144 void *pvSamplesBuffer; 145 uint32_t cbSamplesBufferAllocated; 146 147 /* Temporary buffer for frequency conversion. */ 148 void *pvRateBuffer; 149 uint32_t cbRateBufferAllocated; 150 143 151 } SnifferInputCtx; 152 153 static void ictxDelete(SnifferInputCtx *pCtx) 154 { 155 /* The caller will not use this context anymore. */ 156 if (pCtx->rate) 157 { 158 st_rate_stop (pCtx->rate); 159 } 160 161 RTMemFree(pCtx->pvSamplesBuffer); 162 RTMemFree(pCtx->pvRateBuffer); 163 164 memset(pCtx, 0, sizeof(*pCtx)); 165 RTMemFree(pCtx); 166 } 167 168 static void ictxReallocSamplesBuffer(SnifferInputCtx *pCtx, uint32_t cs) 169 { 170 uint32_t cbBuffer = cs * sizeof(st_sample_t); 171 172 if (cbBuffer > pCtx->cbSamplesBufferAllocated) 173 { 174 RTMemFree(pCtx->pvSamplesBuffer); 175 176 pCtx->pvSamplesBuffer = RTMemAlloc(cbBuffer); 177 if (pCtx->pvSamplesBuffer) 178 { 179 pCtx->cbSamplesBufferAllocated = cbBuffer; 180 } 181 else 182 { 183 pCtx->cbSamplesBufferAllocated = 0; 184 } 185 } 186 } 187 188 static void ictxReallocRateBuffer(SnifferInputCtx *pCtx, uint32_t cs) 189 { 190 uint32_t cbBuffer = cs * sizeof(st_sample_t); 191 192 if (cbBuffer > pCtx->cbRateBufferAllocated) 193 { 194 RTMemFree(pCtx->pvRateBuffer); 195 196 pCtx->pvRateBuffer = RTMemAlloc(cbBuffer); 197 if (pCtx->pvRateBuffer) 198 { 199 pCtx->cbRateBufferAllocated = cbBuffer; 200 } 201 else 202 { 203 pCtx->cbRateBufferAllocated = 0; 204 } 205 } 206 } 207 144 208 145 209 /* … … 209 273 pCtx->conv = NULL; 210 274 pCtx->rate = NULL; 275 pCtx->pvSamplesBuffer = NULL; 276 pCtx->cbSamplesBufferAllocated = 0; 277 pCtx->pvRateBuffer = NULL; 278 pCtx->cbRateBufferAllocated = 0; 211 279 212 280 rc = g_pData->pDrv->pfnAudioInputBegin (g_pData->pDrv, … … 247 315 if (c == 0) 248 316 { 249 /* The caller will not use this context anymore. */ 250 if (pCtx->rate) 251 { 252 st_rate_stop (pCtx->rate); 253 } 254 RTMemFree(pCtx); 317 ictxDelete(pCtx); 255 318 pCtx = NULL; 256 319 } … … 352 415 { 353 416 /* Convert PCM samples to st_sample_t. 354 * And then apply rate conver tion if necessary.417 * And then apply rate conversion if necessary. 355 418 */ 356 /* @todo Optimization: allocate ps buffer once per context and reallocate if cbData changes. */ 357 uint32_t cs = cbData / pCtx->cBytesPerFrame; 358 st_sample_t *ps = (st_sample_t *)RTMemAlloc(cs * sizeof(st_sample_t)); 419 420 /* Optimization: allocate 'ps' buffer once per context and reallocate if cbData changes. 421 * Usually size of packets is constant. 422 */ 423 st_sample_t *ps; 424 uint32_t cs = cbData / pCtx->cBytesPerFrame; /* How many samples. */ 425 426 ictxReallocSamplesBuffer(pCtx, cs); 427 428 ps = (st_sample_t *)pCtx->pvSamplesBuffer; 359 429 if (ps) 360 430 { 361 void *pvSamplesRateDst = NULL;362 363 431 void *pvSamples = NULL; 364 432 uint32_t cbSamples = 0; 365 433 434 Assert(pCtx->cbSamplesBufferAllocated >= cs * sizeof(st_sample_t)); 435 366 436 pCtx->conv(ps, pvData, cs, &nominal_volume); 367 437 368 438 if (pCtx->rate) 369 439 { 440 st_sample_t *psConverted; 370 441 uint32_t csConverted = (cs * pCtx->phw->info.freq) / pCtx->iFreq; 371 pvSamplesRateDst = RTMemAlloc(csConverted * sizeof(st_sample_t)); 372 373 if (pvSamplesRateDst) 442 443 ictxReallocRateBuffer(pCtx, csConverted); 444 445 psConverted = (st_sample_t *)pCtx->pvRateBuffer; 446 if (psConverted) 374 447 { 375 448 int csSrc = cs; 376 449 int csDst = csConverted; 377 450 451 Assert(pCtx->cbRateBufferAllocated >= csConverted * sizeof(st_sample_t)); 452 378 453 st_rate_flow (pCtx->rate, 379 ps, (st_sample_t *)pvSamplesRateDst,454 ps, psConverted, 380 455 &csSrc, &csDst); 381 456 382 pvSamples = p vSamplesRateDst;383 cbSamples = csDst * sizeof(st_sample_t); 457 pvSamples = psConverted; 458 cbSamples = csDst * sizeof(st_sample_t); /* Use csDst as it may be != csConverted */ 384 459 } 385 460 else … … 398 473 rc = pCtx->pfnFilterCallback(pCtx->pvFilterCallback, cbSamples, pvSamples); 399 474 } 400 401 RTMemFree(pvSamplesRateDst);402 RTMemFree(ps);403 475 } 404 476 else … … 428 500 if (c == 0) 429 501 { 430 /* The caller will not use this context anymore. */ 431 if (pCtx->rate) 432 { 433 st_rate_stop (pCtx->rate); 434 } 435 RTMemFree(pCtx); 436 } 437 } 438 502 ictxDelete(pCtx); 503 pCtx = NULL; 504 } 505 } 439 506 440 507 static DECLCALLBACK(int) iface_Setup (PPDMIAUDIOSNIFFERPORT pInterface, bool fEnable, bool fKeepHostAudio)
Note:
See TracChangeset
for help on using the changeset viewer.