你好!这里是风筝的博客,
欢迎和我一起交流。
最近同事问我snd_pcm_drain和snd_pcm_drop的区别和使用场景,我去文档里翻了下:PCM Interface
int snd_pcm_drain(snd_pcm_t *pcm)
Stop a PCM preserving pending frames.
Parameters:
pcm PCM handle
Returns:
0 on success otherwise a negative error code
Return values:
-ESTRPIPE a suspend event occurred
For playback wait for all pending frames to be played and then stop the PCM. For capture stop PCM permitting to retrieve residual frames.
For stopping the PCM stream immediately, use ::snd_pcm_drop() instead.
Examples:
/test/latency.c.
int snd_pcm_drop(snd_pcm_t *pcm)
Stop a PCM dropping pending frames.
Parameters:
pcm PCM handle
Returns:
0 on success otherwise a negative error code
This function stops the PCM immediately. The pending samples on the buffer are ignored.
For processing all pending samples, use ::snd_pcm_drain() instead.
Examples:
/test/latency.c.
可以看出,
snd_pcm_drain:对于播放,会先等待所有挂起没有传输完的数据帧先播完,才会去关闭PCM。
snd_pcm_drop:对于播放,会立即停止PCM,剩余的数据帧则直接丢弃不要。
在alsa-lib/test/pcm_mian.c文件里有个实例:
/* pass the remaining samples, otherwise they're dropped in close */
err = snd_pcm_drain(handle);
if (err < 0)
printf("snd_pcm_drain failed: %s\n", snd_strerror(err));
snd_pcm_close(handle);
在关闭pcm之前,先调用snd_pcm_drain将剩余当数据帧播完。