AudioGetDelay returns signed value and cleanups.

This commit is contained in:
Johns 2012-03-10 15:00:58 +01:00
parent 1acdeee913
commit f59425ac57
3 changed files with 58 additions and 46 deletions

67
audio.c
View File

@ -105,20 +105,20 @@ typedef struct _audio_module_
{ {
const char *Name; ///< audio output module name const char *Name; ///< audio output module name
void (*Thread) (void); ///< module thread handler void (*const Thread) (void); ///< module thread handler
void (*Enqueue) (const void *, int); ///< enqueue samples for output void (*const Enqueue) (const void *, int); ///< enqueue samples for output
void (*VideoReady) (void); ///< video ready, start audio void (*const VideoReady) (void); ///< video ready, start audio
void (*FlushBuffers) (void); ///< flush sample buffers void (*const FlushBuffers) (void); ///< flush sample buffers
void (*Poller) (void); ///< output poller void (*const Poller) (void); ///< output poller
int (*FreeBytes) (void); ///< number of bytes free in buffer int (*const FreeBytes) (void); ///< number of bytes free in buffer
int (*UsedBytes) (void); ///< number of bytes used in buffer int (*const UsedBytes) (void); ///< number of bytes used in buffer
uint64_t(*GetDelay) (void); ///< get current audio delay int64_t(*const GetDelay) (void); ///< get current audio delay
void (*SetVolume) (int); ///< set output volume void (*const SetVolume) (int); ///< set output volume
int (*Setup) (int *, int *, int); ///< setup channels, samplerate int (*const Setup) (int *, int *, int); ///< setup channels, samplerate
void (*Play) (void); ///< play void (*const Play) (void); ///< play
void (*Pause) (void); ///< pause void (*const Pause) (void); ///< pause
void (*Init) (void); ///< initialize audio output module void (*const Init) (void); ///< initialize audio output module
void (*Exit) (void); ///< cleanup audio output module void (*const Exit) (void); ///< cleanup audio output module
} AudioModule; } AudioModule;
static const AudioModule NoopModule; ///< forward definition of noop module static const AudioModule NoopModule; ///< forward definition of noop module
@ -912,17 +912,17 @@ static void AlsaInitMixer(void)
** **
** @todo FIXME: handle the case no audio running ** @todo FIXME: handle the case no audio running
*/ */
static uint64_t AlsaGetDelay(void) static int64_t AlsaGetDelay(void)
{ {
int err; int err;
snd_pcm_sframes_t delay; snd_pcm_sframes_t delay;
uint64_t pts; int64_t pts;
if (!AlsaPCMHandle || !AudioSampleRate) { if (!AlsaPCMHandle || !AudioSampleRate) {
return 0UL; return 0L;
} }
if (!AudioRunning) { // audio not running if (!AudioRunning) { // audio not running
return 0UL; return 0L;
} }
// FIXME: thread safe? __assert_fail_base in snd_pcm_delay // FIXME: thread safe? __assert_fail_base in snd_pcm_delay
@ -940,8 +940,8 @@ static uint64_t AlsaGetDelay(void)
delay = 0L; delay = 0L;
} }
pts = ((uint64_t) delay * 90 * 1000) / AudioSampleRate; pts = ((int64_t) delay * 90 * 1000) / AudioSampleRate;
pts += ((uint64_t) RingBufferUsedBytes(AlsaRingBuffer) * 90 * 1000) pts += ((int64_t) RingBufferUsedBytes(AlsaRingBuffer) * 90 * 1000)
/ (AudioSampleRate * AudioChannels * AudioBytesProSample); / (AudioSampleRate * AudioChannels * AudioBytesProSample);
Debug(4, "audio/alsa: hw+sw delay %zd %" PRId64 " ms\n", Debug(4, "audio/alsa: hw+sw delay %zd %" PRId64 " ms\n",
RingBufferUsedBytes(AlsaRingBuffer), pts / 90); RingBufferUsedBytes(AlsaRingBuffer), pts / 90);
@ -1732,16 +1732,16 @@ static void OssInitMixer(void)
** **
** @returns audio delay in time stamps. ** @returns audio delay in time stamps.
*/ */
static uint64_t OssGetDelay(void) static int64_t OssGetDelay(void)
{ {
int delay; int delay;
uint64_t pts; int64_t pts;
if (OssPcmFildes == -1) { // setup failure if (OssPcmFildes == -1) { // setup failure
return 0UL; return 0L;
} }
if (!AudioRunning) { // audio not running if (!AudioRunning) { // audio not running
return 0UL; return 0L;
} }
// delay in bytes in kernel buffers // delay in bytes in kernel buffers
delay = -1; delay = -1;
@ -1754,7 +1754,7 @@ static uint64_t OssGetDelay(void)
delay = 0; delay = 0;
} }
pts = ((uint64_t) (delay + RingBufferUsedBytes(OssRingBuffer)) * 90 * 1000) pts = ((int64_t) (delay + RingBufferUsedBytes(OssRingBuffer)) * 90 * 1000)
/ (AudioSampleRate * AudioChannels * AudioBytesProSample); / (AudioSampleRate * AudioChannels * AudioBytesProSample);
Debug(4, "audio/oss: hw+sw delay %zd %" PRId64 " ms\n", Debug(4, "audio/oss: hw+sw delay %zd %" PRId64 " ms\n",
RingBufferUsedBytes(OssRingBuffer), pts / 90); RingBufferUsedBytes(OssRingBuffer), pts / 90);
@ -2003,9 +2003,9 @@ static int NoopUsedBytes(void)
** **
** @returns audio delay in time stamps. ** @returns audio delay in time stamps.
*/ */
static uint64_t NoopGetDelay(void) static int64_t NoopGetDelay(void)
{ {
return 0UL; return 0L;
} }
/** /**
@ -2080,9 +2080,8 @@ static void *AudioPlayHandlerThread(void *dummy)
// cond_wait can return, without signal! // cond_wait can return, without signal!
} while (!AudioRunning); } while (!AudioRunning);
Debug(3, "audio: ----> %d ms\n", (AudioUsedBytes() * 1000) Debug(3, "audio: ----> %d ms start\n", (AudioUsedBytes() * 1000)
/ (!AudioSampleRate / (!AudioSampleRate + !AudioChannels +
|| !AudioChannels +
AudioSampleRate * AudioChannels * AudioBytesProSample)); AudioSampleRate * AudioChannels * AudioBytesProSample));
pthread_mutex_unlock(&AudioMutex); pthread_mutex_unlock(&AudioMutex);
@ -2118,7 +2117,6 @@ static void *AudioPlayHandlerThread(void *dummy)
} }
#endif #endif
Debug(3, "audio: play start\n");
AudioUsedModule->Thread(); AudioUsedModule->Thread();
} }
@ -2192,7 +2190,7 @@ void AudioEnqueue(const void *samples, int count)
static uint32_t last; static uint32_t last;
static uint32_t tick; static uint32_t tick;
static uint32_t max = 101; static uint32_t max = 101;
uint64_t delay; int64_t delay;
delay = AudioGetDelay(); delay = AudioGetDelay();
tick = GetMsTicks(); tick = GetMsTicks();
@ -2208,7 +2206,7 @@ void AudioEnqueue(const void *samples, int count)
// Update audio clock (stupid gcc developers thinks INT64_C is unsigned) // Update audio clock (stupid gcc developers thinks INT64_C is unsigned)
if (AudioPTS != (int64_t) INT64_C(0x8000000000000000)) { if (AudioPTS != (int64_t) INT64_C(0x8000000000000000)) {
AudioPTS += AudioPTS +=
((int64_t) count * 90000) / (AudioSampleRate * AudioChannels * ((int64_t) count * 90 * 1000) / (AudioSampleRate * AudioChannels *
AudioBytesProSample); AudioBytesProSample);
} }
} }
@ -2259,7 +2257,7 @@ int AudioUsedBytes(void)
** **
** @returns audio delay in time stamps. ** @returns audio delay in time stamps.
*/ */
uint64_t AudioGetDelay(void) int64_t AudioGetDelay(void)
{ {
return AudioUsedModule->GetDelay(); return AudioUsedModule->GetDelay();
} }
@ -2275,7 +2273,6 @@ void AudioSetClock(int64_t pts)
if (AudioPTS != pts) { if (AudioPTS != pts) {
Debug(4, "audio: set clock to %#012" PRIx64 " %#012" PRIx64 " pts\n", Debug(4, "audio: set clock to %#012" PRIx64 " %#012" PRIx64 " pts\n",
AudioPTS, pts); AudioPTS, pts);
} }
#endif #endif
AudioPTS = pts; AudioPTS = pts;

View File

@ -32,7 +32,7 @@ extern void AudioFlushBuffers(void); ///< flush audio buffers
extern void AudioPoller(void); ///< poll audio events/handling extern void AudioPoller(void); ///< poll audio events/handling
extern int AudioFreeBytes(void); ///< free bytes in audio output extern int AudioFreeBytes(void); ///< free bytes in audio output
extern int AudioUsedBytes(void); ///< used bytes in audio output extern int AudioUsedBytes(void); ///< used bytes in audio output
extern uint64_t AudioGetDelay(void); ///< get current audio delay extern int64_t AudioGetDelay(void); ///< get current audio delay
extern void AudioSetClock(int64_t); ///< set audio clock base extern void AudioSetClock(int64_t); ///< set audio clock base
extern int64_t AudioGetClock(); ///< get current audio clock extern int64_t AudioGetClock(); ///< get current audio clock
extern void AudioSetVolume(int); ///< set volume extern void AudioSetVolume(int); ///< set volume

35
video.c
View File

@ -543,9 +543,9 @@ static void VideoUpdateOutput(AVRational input_aspect_ratio, int input_width,
/// ///
/// Reduce output. /// Reduce output.
/// ///
static void VideoMessage(void) //static void VideoMessage(const char *message)
{ //{
} //}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
// GLX // GLX
@ -1723,6 +1723,8 @@ static VaapiDecoder *VaapiNewHwDecoder(void)
decoder->OutputWidth = VideoWindowWidth; decoder->OutputWidth = VideoWindowWidth;
decoder->OutputHeight = VideoWindowHeight; decoder->OutputHeight = VideoWindowHeight;
decoder->PTS = AV_NOPTS_VALUE;
// get/put still not working // get/put still not working
//decoder->GetPutImage = !VaapiBuggyIntel || VaapiNewIntel; //decoder->GetPutImage = !VaapiBuggyIntel || VaapiNewIntel;
decoder->GetPutImage = !VaapiBuggyIntel; decoder->GetPutImage = !VaapiBuggyIntel;
@ -4345,10 +4347,12 @@ static void VaapiDisplayFrame(void)
/// ///
static void VaapiSyncDisplayFrame(VaapiDecoder * decoder) static void VaapiSyncDisplayFrame(VaapiDecoder * decoder)
{ {
int err;
int filled; int filled;
int64_t audio_clock; int64_t audio_clock;
int64_t video_clock; int64_t video_clock;
err = 0;
if (Video60HzMode && !(decoder->FramesDisplayed % 6)) { if (Video60HzMode && !(decoder->FramesDisplayed % 6)) {
// FIXME: drop next frame? // FIXME: drop next frame?
decoder->DupNextFrame++; decoder->DupNextFrame++;
@ -4372,6 +4376,7 @@ static void VaapiSyncDisplayFrame(VaapiDecoder * decoder)
|| video_clock > audio_clock + VideoAudioDelay + 120 * 90)) { || video_clock > audio_clock + VideoAudioDelay + 120 * 90)) {
Debug(3, "video: initial slow down %d\n", decoder->StartCounter); Debug(3, "video: initial slow down %d\n", decoder->StartCounter);
decoder->DupNextFrame = 2; decoder->DupNextFrame = 2;
err = 1;
} }
if (decoder->DupNextFrame) { if (decoder->DupNextFrame) {
@ -4383,22 +4388,25 @@ static void VaapiSyncDisplayFrame(VaapiDecoder * decoder)
if (abs(video_clock - audio_clock) > 5000 * 90) { if (abs(video_clock - audio_clock) > 5000 * 90) {
Debug(3, "video: pts difference too big\n"); Debug(3, "video: pts difference too big\n");
err = 1;
} else if (video_clock > audio_clock + VideoAudioDelay + 100 * 90) { } else if (video_clock > audio_clock + VideoAudioDelay + 100 * 90) {
Debug(3, "video: slow down video\n"); Debug(3, "video: slow down video\n");
err = 1;
decoder->DupNextFrame += 2; decoder->DupNextFrame += 2;
} else if (video_clock > audio_clock + VideoAudioDelay + 45 * 90) { } else if (video_clock > audio_clock + VideoAudioDelay + 45 * 90) {
Debug(3, "video: slow down video\n"); Debug(3, "video: slow down video\n");
err = 1;
decoder->DupNextFrame++; decoder->DupNextFrame++;
} else if (audio_clock + VideoAudioDelay > video_clock + 15 * 90 } else if (audio_clock + VideoAudioDelay > video_clock + 15 * 90
&& filled > 1) { && filled > 1) {
Debug(3, "video: speed up video\n"); Debug(3, "video: speed up video\n");
err = 1;
decoder->DropNextFrame = 1; decoder->DropNextFrame = 1;
} }
} }
#if defined(DEBUG) || defined(AV_INFO) #if defined(DEBUG) || defined(AV_INFO)
// debug audio/video sync // debug audio/video sync
if (decoder->DupNextFrame || decoder->DropNextFrame if (err || !(decoder->FramesDisplayed % AV_INFO_TIME)) {
|| !(decoder->FramesDisplayed % AV_INFO_TIME)) {
Info("video: %s%+5" PRId64 " %4" PRId64 " %3d/\\ms %3d v-buf\n", Info("video: %s%+5" PRId64 " %4" PRId64 " %3d/\\ms %3d v-buf\n",
Timestamp2String(video_clock), Timestamp2String(video_clock),
abs((video_clock - audio_clock) / 90) < abs((video_clock - audio_clock) / 90) <
@ -7411,10 +7419,12 @@ static void VdpauDisplayFrame(void)
/// ///
static void VdpauSyncDisplayFrame(VdpauDecoder * decoder) static void VdpauSyncDisplayFrame(VdpauDecoder * decoder)
{ {
int err;
int filled; int filled;
int64_t audio_clock; int64_t audio_clock;
int64_t video_clock; int64_t video_clock;
err = 0;
if (Video60HzMode && !(decoder->FramesDisplayed % 6)) { if (Video60HzMode && !(decoder->FramesDisplayed % 6)) {
// FIXME: drop next frame? // FIXME: drop next frame?
decoder->DupNextFrame++; decoder->DupNextFrame++;
@ -7437,6 +7447,7 @@ static void VdpauSyncDisplayFrame(VdpauDecoder * decoder)
&& (audio_clock == (int64_t) AV_NOPTS_VALUE && (audio_clock == (int64_t) AV_NOPTS_VALUE
|| video_clock > audio_clock + VideoAudioDelay + 120 * 90)) { || video_clock > audio_clock + VideoAudioDelay + 120 * 90)) {
Debug(3, "video: initial slow down %d\n", decoder->StartCounter); Debug(3, "video: initial slow down %d\n", decoder->StartCounter);
err = 1;
decoder->DupNextFrame = 2; decoder->DupNextFrame = 2;
} }
@ -7449,22 +7460,25 @@ static void VdpauSyncDisplayFrame(VdpauDecoder * decoder)
if (abs(video_clock - audio_clock) > 5000 * 90) { if (abs(video_clock - audio_clock) > 5000 * 90) {
Debug(3, "video: pts difference too big\n"); Debug(3, "video: pts difference too big\n");
err = 1;
} else if (video_clock > audio_clock + VideoAudioDelay + 100 * 90) { } else if (video_clock > audio_clock + VideoAudioDelay + 100 * 90) {
Debug(3, "video: slow down video\n"); Debug(3, "video: slow down video\n");
err = 1;
decoder->DupNextFrame += 2; decoder->DupNextFrame += 2;
} else if (video_clock > audio_clock + VideoAudioDelay + 45 * 90) { } else if (video_clock > audio_clock + VideoAudioDelay + 45 * 90) {
Debug(3, "video: slow down video\n"); Debug(3, "video: slow down video\n");
err = 1;
decoder->DupNextFrame++; decoder->DupNextFrame++;
} else if (audio_clock + VideoAudioDelay > video_clock + 15 * 90 } else if (audio_clock + VideoAudioDelay > video_clock + 15 * 90
&& filled > 1 + 2 * decoder->Interlaced) { && filled > 1 + 2 * decoder->Interlaced) {
Debug(3, "video: speed up video\n"); Debug(3, "video: speed up video\n");
err = 1;
decoder->DropNextFrame = 1; decoder->DropNextFrame = 1;
} }
} }
#if defined(DEBUG) || defined(AV_INFO) #if defined(DEBUG) || defined(AV_INFO)
// debug audio/video sync // debug audio/video sync
if (decoder->DupNextFrame || decoder->DropNextFrame if (err || !(decoder->FramesDisplayed % AV_INFO_TIME)) {
|| !(decoder->FramesDisplayed % AV_INFO_TIME)) {
Info("video: %s%+5" PRId64 " %4" PRId64 " %3d/\\ms %3d v-buf\n", Info("video: %s%+5" PRId64 " %4" PRId64 " %3d/\\ms %3d v-buf\n",
Timestamp2String(video_clock), Timestamp2String(video_clock),
abs((video_clock - audio_clock) / 90) < abs((video_clock - audio_clock) / 90) <
@ -7486,7 +7500,8 @@ static void VdpauSyncRenderFrame(VdpauDecoder * decoder,
{ {
// FIXME: temp debug // FIXME: temp debug
if (0 && frame->pkt_pts != (int64_t) AV_NOPTS_VALUE) { if (0 && frame->pkt_pts != (int64_t) AV_NOPTS_VALUE) {
Info("render frame pts %s\n", Timestamp2String(frame->pkt_pts)); Debug(3, "video: render frame pts %s\n",
Timestamp2String(frame->pkt_pts));
} }
VideoSetPts(&decoder->PTS, decoder->Interlaced, frame); VideoSetPts(&decoder->PTS, decoder->Interlaced, frame);
@ -9699,9 +9714,9 @@ void VideoExit(void)
int SysLogLevel; ///< show additional debug informations int SysLogLevel; ///< show additional debug informations
uint32_t VideoSwitch; ///< required uint32_t VideoSwitch; ///< required
uint64_t AudioGetDelay(void) ///< required int64_t AudioGetDelay(void) ///< required
{ {
return 0UL; return 0L;
} }
int64_t AudioGetClock(void) ///< required int64_t AudioGetClock(void) ///< required