- Timestamp:
- Jul 10, 2008 10:46:34 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/include/pjmedia/circbuf.h
r2098 r2114 18 18 */ 19 19 20 #ifndef pjmedia_circ_buf_H21 #define pjmedia_circ_buf_H20 #ifndef __PJMEDIA_CIRC_BUF_H__ 21 #define __PJMEDIA_CIRC_BUF_H__ 22 22 23 23 /** … … 151 151 * possitive number, in samples. 152 152 * 153 * @return PJ_SUCCESS when successful, otherwise the appropriate154 * error will be returned.153 * @return PJ_SUCCESS when successful, otherwise 154 * the appropriate error will be returned. 155 155 */ 156 156 PJ_INLINE(pj_status_t) pjmedia_circ_buf_adv_read_ptr(pjmedia_circ_buf *circbuf, … … 180 180 * possitive number, in samples. 181 181 * 182 * @return PJ_SUCCESS when successful, otherwise the appropriate183 * error will be returned.182 * @return PJ_SUCCESS when successful, otherwise 183 * the appropriate error will be returned. 184 184 */ 185 185 PJ_INLINE(pj_status_t) pjmedia_circ_buf_adv_write_ptr(pjmedia_circ_buf *circbuf, … … 223 223 } 224 224 225 PJMEDIA_CIRC_BUF_CHECK(*reg1_len != 0 || (*reg1_len == 0 && circbuf->len == 0)); 225 PJMEDIA_CIRC_BUF_CHECK(*reg1_len != 0 || (*reg1_len == 0 && 226 circbuf->len == 0)); 226 227 PJMEDIA_CIRC_BUF_CHECK(*reg1_len + *reg2_len == circbuf->len); 227 228 } … … 258 259 } 259 260 260 PJMEDIA_CIRC_BUF_CHECK(*reg1_len != 0 || (*reg1_len == 0 && circbuf->len == 0)); 261 PJMEDIA_CIRC_BUF_CHECK(*reg1_len + *reg2_len == circbuf->capacity - circbuf->len); 261 PJMEDIA_CIRC_BUF_CHECK(*reg1_len != 0 || (*reg1_len == 0 && 262 circbuf->len == 0)); 263 PJMEDIA_CIRC_BUF_CHECK(*reg1_len + *reg2_len == circbuf->capacity - 264 circbuf->len); 262 265 } 263 266 … … 270 273 * @param count Number of samples being read. 271 274 * 272 * @return PJ_SUCCESS when successful, otherwise the appropriate273 * error will be returned.275 * @return PJ_SUCCESS when successful, otherwise 276 * the appropriate error will be returned. 274 277 */ 275 278 PJ_INLINE(pj_status_t) pjmedia_circ_buf_read(pjmedia_circ_buf *circbuf, … … 304 307 * @param count Number of samples being written. 305 308 * 306 * @return PJ_SUCCESS when successful, otherwise the appropriate307 * error will be returned.309 * @return PJ_SUCCESS when successful, otherwise 310 * the appropriate error will be returned. 308 311 */ 309 312 PJ_INLINE(pj_status_t) pjmedia_circ_buf_write(pjmedia_circ_buf *circbuf, … … 330 333 } 331 334 335 336 /** 337 * Copy audio samples from the circular buffer without changing its state. 338 * 339 * @param circbuf The circular buffer. 340 * @param start_idx Starting sample index to be copied. 341 * @param data Buffer to store the read audio samples. 342 * @param count Number of samples being read. 343 * 344 * @return PJ_SUCCESS when successful, otherwise 345 * the appropriate error will be returned. 346 */ 347 PJ_INLINE(pj_status_t) pjmedia_circ_buf_copy(pjmedia_circ_buf *circbuf, 348 unsigned start_idx, 349 pj_int16_t *data, 350 unsigned count) 351 { 352 pj_int16_t *reg1, *reg2; 353 unsigned reg1cnt, reg2cnt; 354 355 /* Data in the buffer is less than requested */ 356 if (count + start_idx > circbuf->len) 357 return PJ_ETOOBIG; 358 359 pjmedia_circ_buf_get_read_regions(circbuf, ®1, ®1cnt, 360 ®2, ®2cnt); 361 if (reg1cnt > start_idx) { 362 unsigned tmp_len; 363 tmp_len = reg1cnt - start_idx; 364 if (tmp_len > count) 365 tmp_len = count; 366 pjmedia_copy_samples(data, reg1 + start_idx, tmp_len); 367 if (tmp_len < count) 368 pjmedia_copy_samples(data + tmp_len, reg2, count - tmp_len); 369 } else { 370 pjmedia_copy_samples(data, reg2 + start_idx - reg1cnt, count); 371 } 372 373 return PJ_SUCCESS; 374 } 375 376 377 /** 378 * Pack the buffer so the first sample will be in the beginning of the buffer. 379 * This will also make the buffer contiguous. 380 * 381 * @param circbuf The circular buffer. 382 * 383 * @return PJ_SUCCESS when successful, otherwise 384 * the appropriate error will be returned. 385 */ 386 PJ_INLINE(pj_status_t) pjmedia_circ_buf_pack_buffer(pjmedia_circ_buf *circbuf) 387 { 388 pj_int16_t *reg1, *reg2; 389 unsigned reg1cnt, reg2cnt; 390 unsigned gap; 391 392 pjmedia_circ_buf_get_read_regions(circbuf, ®1, ®1cnt, 393 ®2, ®2cnt); 394 395 /* Check if not contigue */ 396 if (reg2cnt != 0) { 397 /* Check if no space left to roll the buffer 398 * (or should this function provide temporary buffer?) 399 */ 400 gap = circbuf->capacity - pjmedia_circ_buf_get_len(circbuf); 401 if (gap == 0) 402 return PJ_ETOOBIG; 403 404 /* Roll buffer left using the gap until reg2cnt == 0 */ 405 do { 406 if (gap > reg2cnt) 407 gap = reg2cnt; 408 pjmedia_move_samples(reg1 - gap, reg1, reg1cnt); 409 pjmedia_copy_samples(reg1 + reg1cnt - gap, reg2, gap); 410 if (gap < reg2cnt) 411 pjmedia_move_samples(reg2, reg2 + gap, reg2cnt - gap); 412 reg1 -= gap; 413 reg1cnt += gap; 414 reg2cnt -= gap; 415 } while (reg2cnt > 0); 416 } 417 418 /* Finally, Shift samples to the left edge */ 419 if (reg1 != circbuf->buf) 420 pjmedia_move_samples(circbuf->buf, reg1, 421 pjmedia_circ_buf_get_len(circbuf)); 422 circbuf->start = circbuf->buf; 423 424 return PJ_SUCCESS; 425 } 426 427 332 428 PJ_END_DECL 333 429
Note: See TracChangeset
for help on using the changeset viewer.