Skip to content

Commit aee8c6b

Browse files
authored
Check for fractional samples per frame (#4482)
1 parent a4d376d commit aee8c6b

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

pjmedia/include/pjmedia/conference.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ PJ_DECL(pj_status_t) pjmedia_conf_set_port0_name(pjmedia_conf *conf,
348348
*
349349
* This operation executes asynchronously, use the callback set from
350350
* #pjmedia_conf_set_op_cb() to receive notification upon completion.
351+
*
352+
* Note: Sample rate and ptime (frame duration) settings must be compatible.
353+
* Configurations resulting in a fractional number of samples per frame
354+
* are not supported and will cause the function to fail.
355+
* For example, a sample rate of 22050 Hz and a frame duration (ptime) of 10 ms
356+
* will result in 220.5 samples per frame, which is not an integer,
357+
* so port creation will fail.
351358
*
352359
* @param conf The conference bridge.
353360
* @param pool Pool to allocate buffers for this port.

pjmedia/include/pjmedia/wav_port.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ typedef struct pjmedia_wav_player_info
8383
* Create a media port to play streams from a WAV file. WAV player port
8484
* supports for reading WAV file with uncompressed 16 bit PCM format or
8585
* compressed G.711 A-law/U-law format.
86+
*
87+
* Note: The ptime value must be compatible with the WAV file's sample rate.
88+
* If the combination results in a fractional number of samples per frame,
89+
* port creation will fail.
90+
* For example, a sample rate of 22050 Hz and a frame duration (ptime) of 10 ms
91+
* will result in 220.5 samples per frame, which is not an integer,
92+
* so port creation will fail.
8693
*
8794
* @param pool Pool to create memory buffers for this port.
8895
* @param filename File name to open.

pjmedia/src/pjmedia/conference.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,22 @@ static pj_status_t create_conf_port( pj_pool_t *parent_pool,
531531
conf_ptime = conf->samples_per_frame / conf->channel_count *
532532
1000 / conf->clock_rate;
533533

534+
/* Check compatibility of sample rate and ptime.
535+
* Some combinations result in a fractional number of samples per frame
536+
* which we do not support.
537+
* One such case would be for example 10ms @ 22050Hz which would yield
538+
* 220.5 samples per frame.
539+
*/
540+
if (0 != (port_ptime * conf_port->clock_rate *
541+
conf_port->channel_count % 1000))
542+
{
543+
PJ_LOG(3,(THIS_FILE,
544+
"Cannot create conf port: incompatible sample rate/ptime"));
545+
status = PJMEDIA_ENOTCOMPATIBLE;
546+
goto on_return;
547+
}
548+
549+
534550
/* Calculate the size (in ptime) for the port buffer according to
535551
* this formula:
536552
* - if either ptime is an exact multiple of the other, then use
@@ -591,6 +607,13 @@ static pj_status_t create_conf_port( pj_pool_t *parent_pool,
591607

592608
on_return:
593609
if (status != PJ_SUCCESS) {
610+
/* Destroy resample if this conf port has it. */
611+
if (conf_port->rx_resample)
612+
pjmedia_resample_destroy(conf_port->rx_resample);
613+
614+
if (conf_port->tx_resample)
615+
pjmedia_resample_destroy(conf_port->tx_resample);
616+
594617
if (pool)
595618
pj_pool_release(pool);
596619
}

pjmedia/src/pjmedia/wav_player.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,22 @@ PJ_DEF(pj_status_t) pjmedia_wav_player_port_create( pj_pool_t *pool_,
416416

417417
/* It seems like we have a valid WAVE file. */
418418

419+
/* Check compatibility of sample rate and ptime.
420+
* Some combinations result in a fractional number of samples per frame
421+
* which we do not support.
422+
* One such case would be for example 10ms @ 22050Hz which would yield
423+
* 220.5 samples per frame.
424+
*/
425+
if (0 != (ptime * wave_hdr.fmt_hdr.sample_rate *
426+
wave_hdr.fmt_hdr.nchan % 1000))
427+
{
428+
pj_file_close(fport->fd);
429+
PJ_LOG(3,(THIS_FILE,
430+
"Cannot create wav player port: incompatible sample rate/ptime"));
431+
status = PJMEDIA_ENOTCOMPATIBLE;
432+
goto on_error;
433+
}
434+
419435
/* Initialize */
420436
fport->options = options;
421437

0 commit comments

Comments
 (0)