| 1 |
/********************************************************* |
| 2 |
|
| 3 |
SunAudioRenderer implementation |
| 4 |
|
| 5 |
*********************************************************/ |
| 6 |
|
| 7 |
#include "SunAudioRenderer.h" |
| 8 |
|
| 9 |
#ifdef HAVE_SUNAUDIO |
| 10 |
|
| 11 |
#include "avifile.h" |
| 12 |
#include "except.h" |
| 13 |
#include "cpuinfo.h" |
| 14 |
#include "utils.h" |
| 15 |
#include "avm_output.h" |
| 16 |
|
| 17 |
#include <sys/ioctl.h> |
| 18 |
#include <sys/audioio.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include <stropts.h> |
| 21 |
#include <errno.h> |
| 22 |
#include <fcntl.h> |
| 23 |
#include <stdio.h> |
| 24 |
|
| 25 |
#define Debug |
| 26 |
#define __MODULE__ "SunAudioRenderer" |
| 27 |
|
| 28 |
AVM_BEGIN_NAMESPACE; |
| 29 |
|
| 30 |
int SunAudioMix::Mix(void* data, const void* src, uint_t n) const |
| 31 |
{ |
| 32 |
return ::write(m_iFd, src, n); |
| 33 |
} |
| 34 |
|
| 35 |
SunAudioRenderer::SunAudioRenderer(IAviReadStream* as, WAVEFORMATEX& Owf, const char* privcname) |
| 36 |
:IAudioRenderer(as, Owf), m_iAudioFd(-1), m_pAudioMix(0) |
| 37 |
{ |
| 38 |
try |
| 39 |
{ |
| 40 |
int audio_status = m_pAudiostream->StartStreaming(); |
| 41 |
if(audio_status != 0) |
| 42 |
throw FATAL("Failed to start streaming"); |
| 43 |
|
| 44 |
m_pAudiostream->GetOutputFormat(&m_Owf, sizeof m_Owf); |
| 45 |
|
| 46 |
// Debug std::cout << "Audio format " << m_Owf.nSamplesPerSec |
| 47 |
// << "/" << m_Owf.wBitsPerSample |
| 48 |
// << "/" << ((m_Owf.nChannels != 1) ? "stereo" : "mono") |
| 49 |
// << endl; |
| 50 |
|
| 51 |
m_iAudioFd = open("/dev/audio", O_WRONLY|O_NDELAY); |
| 52 |
if(m_iAudioFd < 0) |
| 53 |
throw FATAL("Can't open audio device"); |
| 54 |
|
| 55 |
// m_bForce44KHz = RegAccess::ReadForce44KHz(); |
| 56 |
// if(m_Owf.nSamplesPerSec == 44100 && m_bForce44KHz) |
| 57 |
// m_bForce44KHz = false; |
| 58 |
|
| 59 |
audio_info_t audio_info; |
| 60 |
AUDIO_INITINFO(&audio_info); |
| 61 |
audio_info.play.channels = m_Owf.nChannels; |
| 62 |
audio_info.play.precision = m_Owf.wBitsPerSample; |
| 63 |
audio_info.play.sample_rate = 0 // m_bForce44KHz |
| 64 |
? 44100 : m_Owf.nSamplesPerSec; |
| 65 |
audio_info.play.encoding = m_Owf.wBitsPerSample == 8 |
| 66 |
? AUDIO_ENCODING_LINEAR8 |
| 67 |
: AUDIO_ENCODING_LINEAR; |
| 68 |
audio_info.play.samples = 0; |
| 69 |
if(ioctl(m_iAudioFd, AUDIO_SETINFO, &audio_info) < 0) |
| 70 |
{ |
| 71 |
char failure_msg[80]; |
| 72 |
sprintf(failure_msg, |
| 73 |
"ioctl(AUDIO_SETINFO) failed: chnl=%d, prec=%d, rate=%d, enc=%d", |
| 74 |
audio_info.play.channels, |
| 75 |
audio_info.play.precision, |
| 76 |
audio_info.play.sample_rate, |
| 77 |
audio_info.play.encoding); |
| 78 |
throw FATAL(failure_msg); |
| 79 |
} |
| 80 |
m_pAudioMix = new SunAudioMix(m_iAudioFd); |
| 81 |
} |
| 82 |
catch(FatalError& error) |
| 83 |
{ |
| 84 |
if(m_iAudioFd > 0) |
| 85 |
::close(m_iAudioFd); |
| 86 |
m_iAudioFd = -1; |
| 87 |
m_pAudiostream = 0; |
| 88 |
throw; |
| 89 |
} |
| 90 |
|
| 91 |
m_iSamplesSent = 0; |
| 92 |
|
| 93 |
m_pAudioThread = new PthreadTask(0, doAudioOut, (void*)this); |
| 94 |
} |
| 95 |
|
| 96 |
SunAudioRenderer::~SunAudioRenderer() |
| 97 |
{ |
| 98 |
//Debug cout<<"Destroying audio renderer"<<endl; |
| 99 |
m_pQueue->Lock(); |
| 100 |
m_bQuit = true; |
| 101 |
m_pQueue->Broadcast(); |
| 102 |
m_pQueue->Unlock(); |
| 103 |
delete m_pAudioThread; |
| 104 |
delete m_pAudioMix; |
| 105 |
reset(); |
| 106 |
::close(m_iAudioFd); |
| 107 |
//Debug cout<<"Destroy() successful"<<endl; |
| 108 |
} |
| 109 |
|
| 110 |
int SunAudioRenderer::SetVolume(int volume) |
| 111 |
{ |
| 112 |
int r = IAudioRenderer::SetVolume(volume); |
| 113 |
if (r == 0) |
| 114 |
{ |
| 115 |
audio_info_t audio_info; |
| 116 |
AUDIO_INITINFO(&audio_info); |
| 117 |
audio_info.play.gain = int(volume * AUDIO_MAX_GAIN / VOL_MAX); |
| 118 |
// not yet, initial 100% volume setting is much too loud |
| 119 |
// (at least on my SB16pci :-) |
| 120 |
//ioctl(m_iAudioFd, AUDIO_SETINFO, &audio_info); |
| 121 |
} |
| 122 |
return r; |
| 123 |
} |
| 124 |
|
| 125 |
double SunAudioRenderer::getRendererBufferTime() const |
| 126 |
{ |
| 127 |
double rate = m_Owf.nSamplesPerSec; |
| 128 |
double frame_time = 0; |
| 129 |
audio_info_t audio_info; |
| 130 |
if (m_bInitialized && ::ioctl(m_iAudioFd, AUDIO_GETINFO, &audio_info) == 0) |
| 131 |
{ |
| 132 |
frame_time += (m_iSamplesSent - audio_info.play.samples); |
| 133 |
AVM_WRITE("audio renderer", 1, "SAMPLES queue %d, device %d - %d = %d, time = %f\n", |
| 134 |
m_pQueue->GetSize(), |
| 135 |
m_iSamplesSent, audio_info.play.samples, |
| 136 |
m_iSamplesSent - audio_info.play.samples, |
| 137 |
m_pQueue->GetBufferTime()); |
| 138 |
} |
| 139 |
return frame_time / rate; |
| 140 |
} |
| 141 |
|
| 142 |
void* SunAudioRenderer::doAudioOut(void* arg) |
| 143 |
{ |
| 144 |
SunAudioRenderer& a = *(SunAudioRenderer*) arg; |
| 145 |
int reset_dev = 0; |
| 146 |
while (!a.m_bQuit) |
| 147 |
{ |
| 148 |
if (!a.m_bInitialized) |
| 149 |
{ |
| 150 |
a.m_pQueue->Lock(); |
| 151 |
if (reset_dev) |
| 152 |
{ |
| 153 |
a.m_pQueue->Clear(); |
| 154 |
a.reset(); |
| 155 |
reset_dev = 0; |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
AVM_WRITE("audio renderer", 0, "not yet m_bInitialized\n"); |
| 160 |
a.m_pQueue->Wait(); |
| 161 |
} |
| 162 |
a.m_pQueue->Unlock(); |
| 163 |
continue; |
| 164 |
} else |
| 165 |
reset_dev=1; |
| 166 |
|
| 167 |
|
| 168 |
a.m_pQueue->Lock(); |
| 169 |
if (a.m_pQueue->GetSize() < a.m_uiWriteSize |
| 170 |
|| a.m_bPaused ) |
| 171 |
{ |
| 172 |
if (!a.m_bQuit) |
| 173 |
a.m_pQueue->Wait(); |
| 174 |
a.m_pQueue->Unlock(); |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
int result = a.m_pQueue->Read(0, 8192, a.m_pAudioMix); |
| 179 |
|
| 180 |
if (result < 0) |
| 181 |
perror("AudioQueue::write"); |
| 182 |
else |
| 183 |
a.updateTimer(); |
| 184 |
a.m_pQueue->Unlock(); |
| 185 |
} |
| 186 |
AVM_WRITE("audio renderer", 1, "Exiting audio thread\n"); |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
void SunAudioRenderer::pause(int state) |
| 191 |
{ |
| 192 |
AVM_WRITE("audio renderer", 1, "SunAudioRenderer::pause %d\n", state); |
| 193 |
|
| 194 |
audio_info_t audio_info; |
| 195 |
AUDIO_INITINFO(&audio_info); |
| 196 |
audio_info.play.pause = state; |
| 197 |
::ioctl(m_iAudioFd, AUDIO_SETINFO, &audio_info); |
| 198 |
|
| 199 |
AVM_WRITE("audio renderer", 1, "Samples buffered in audio_queue %f\n", |
| 200 |
m_pQueue->GetBufferTime()); |
| 201 |
} |
| 202 |
|
| 203 |
int SunAudioRenderer::reset() |
| 204 |
{ |
| 205 |
if (m_iAudioFd) |
| 206 |
{ |
| 207 |
/* |
| 208 |
* clear data in the STREAMS queue, and wait for |
| 209 |
* playback end. Make sure the audio device is not |
| 210 |
* 'paused' state, else the AUDIO_DRAIN ioctl hangs |
| 211 |
* forever. |
| 212 |
*/ |
| 213 |
ioctl(m_iAudioFd, I_FLUSH, FLUSHW); |
| 214 |
|
| 215 |
audio_info_t audio_info; |
| 216 |
AUDIO_INITINFO(&audio_info); |
| 217 |
audio_info.play.pause = 0; |
| 218 |
ioctl(m_iAudioFd, AUDIO_SETINFO, &audio_info); |
| 219 |
|
| 220 |
ioctl(m_iAudioFd, AUDIO_DRAIN, 0); |
| 221 |
|
| 222 |
AUDIO_INITINFO(&audio_info); |
| 223 |
audio_info.play.samples = 0; |
| 224 |
ioctl(m_iAudioFd, AUDIO_SETINFO, &audio_info); |
| 225 |
m_iSamplesSent = 0; |
| 226 |
return 0; |
| 227 |
} |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
AVM_END_NAMESPACE; |
| 232 |
|
| 233 |
#endif // HAVE_SUNAUDIO |