SDL2's SDL_OpenAudioDevice cannot play sound

Recently, I took the time to make a general video surveillance client. I obtained the media stream through the rtsp protocol and then decoded and rendered it. In the process, I found that the audio stream was about 4 to 5 seconds slower than the video stream. I initially suspected that it was caused by the audio playback module.

The old audio playback is implemented using SDL_OpenAudio, SDL_PauseAudio and callbacks. Because when the audio data is read in the callback is not controlled by my program, I suspect that the problem occurs here. After checking the SDL2 related information, I found that there is a new audio playback interface instead of SDL_OpenAudio, namely SDL_OpenAudioDevice. , but during use, it was found that there was no sound when switching to SDL_OpenAudioDevice to play audio. Through multiple analysis and tests, it was found that the problem occurred in the parameters of SDL_OpenAudioDevice. When the fifth parameter is SDL_AUDIO_ALLOW_ANY_CHANGE, the problem of being unable to play the sound will occur. After changing the value to SDL_AUDIO_ALLOW_FREQUENCY_CHANGE, the sound can be played normally. The code that can be played normally is as follows:

	int num = SDL_GetNumAudioDevices(0);
	const char* deviceName = SDL_GetAudioDeviceName(num, 0);
	m_deviceID = SDL_OpenAudioDevice(deviceName, 0, &m_audioSpec, NULL, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
	if (m_deviceID < 2)
	{
		LOG_DEBUG("SDL_OpenAudioDevice with error deviceID : %d", m_deviceID);
		return false;
	}

Guess you like

Origin blog.csdn.net/heibao111728/article/details/119010877