00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <qapplication.h>
00010
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <errno.h>
00014 #include <iostream>
00015
00016 #ifndef WIN32
00017 #include <netinet/in.h>
00018 #include <linux/soundcard.h>
00019 #include <unistd.h>
00020 #include <fcntl.h>
00021 #include <sys/ioctl.h>
00022 #include <net/if.h>
00023 #include <linux/sockios.h>
00024 #include <mythtv/mythcontext.h>
00025 #include "config.h"
00026 #else
00027 #include <io.h>
00028 #include <winsock2.h>
00029 #include <sstream>
00030 #include <Dsound.h>
00031 #include "gcontext.h"
00032 #endif
00033
00034 #include "rtp.h"
00035 #include "audiodrv.h"
00036
00037 using namespace std;
00038
00039
00040
00042
00044
00045 int AudioDriver::WriteSilence(int samples)
00046 {
00047 #define MAX_SILENCE_BUFFER 320
00048 int byteCount=0;
00049 short silenceBuffer[MAX_SILENCE_BUFFER];
00050 memset(silenceBuffer, 0, sizeof(silenceBuffer));
00051 while (samples>0)
00052 {
00053 if (samples < MAX_SILENCE_BUFFER)
00054 {
00055 byteCount += Write(silenceBuffer, samples);
00056 samples = 0;
00057 }
00058 else
00059 {
00060 byteCount += Write(silenceBuffer, MAX_SILENCE_BUFFER);
00061 samples -= MAX_SILENCE_BUFFER;
00062 }
00063 }
00064 return byteCount;
00065 }
00066
00067
00068
00069
00071
00073
00074 #ifdef WIN32
00075 waveAudioDriver::waveAudioDriver(QString s, QString m, int mCap) : AudioDriver(mCap)
00076 {
00077 spkIndex = 0;
00078 spkInBuffer = 0;
00079
00080 MicDevice = SpeakerDevice = WAVE_MAPPER;
00081 WAVEOUTCAPS AudioCap;
00082 int numAudioDevs = waveOutGetNumDevs();
00083 for (int i=0; i<=numAudioDevs; i++)
00084 {
00085 MMRESULT err = waveOutGetDevCaps(i, &AudioCap, sizeof(AudioCap));
00086 if ((err == MMSYSERR_NOERROR) && (s == AudioCap.szPname))
00087 SpeakerDevice = i;
00088 }
00089
00090 WAVEINCAPS AudioInCap;
00091 numAudioDevs = waveInGetNumDevs();
00092 for (i=0; i<=numAudioDevs; i++)
00093 {
00094 MMRESULT err = waveInGetDevCaps(i, &AudioInCap, sizeof(AudioInCap));
00095 if ((err == MMSYSERR_NOERROR) && (m == AudioInCap.szPname))
00096 MicDevice = i;
00097 }
00098 }
00099
00100
00101 waveAudioDriver::~waveAudioDriver()
00102 {
00103 }
00104
00105 void waveAudioDriver::Open()
00106 {
00107 openSpeaker();
00108 openMicrophone();
00109 }
00110
00111 void waveAudioDriver::Close()
00112 {
00113 closeSpeaker();
00114 closeMicrophone();
00115 }
00116
00117 void waveAudioDriver::StartSpeaker()
00118 {
00119 waveOutWrite(hSpeaker, &(spkBufferDescr[0]), sizeof(WAVEHDR));
00120 spkIndex = 0;
00121 }
00122
00123 int waveAudioDriver::Write(short *data, int samples)
00124 {
00125 int i = spkIndex % SPK_BUFFER_SIZE;
00126 int b = (spkIndex % (SPK_BUFFER_SIZE*NUM_SPK_BUFFERS))/SPK_BUFFER_SIZE;
00127
00128 if ((i + samples) <= SPK_BUFFER_SIZE)
00129 {
00130 memcpy(&SpkBuffer[b][i], data, samples*sizeof(short));
00131 }
00132 else
00133 {
00134 int temp = SPK_BUFFER_SIZE - i;
00135 memcpy(&SpkBuffer[b][i], data, temp*sizeof(short));
00136 b = (b+1)%NUM_SPK_BUFFERS;
00137 memcpy(&SpkBuffer[b][0], &data[temp], (samples-temp)*sizeof(short));
00138 }
00139
00140 spkIndex += samples;
00141
00142 return (samples*sizeof(short));
00143 }
00144
00145 bool waveAudioDriver::anyMicrophoneData()
00146 {
00147 if ((micBufferDescr[micCurrBuffer].dwFlags & WHDR_DONE) != 0)
00148 return true;
00149 return false;
00150 }
00151
00152 int waveAudioDriver::Read(short *buffer, int maxSamples)
00153 {
00154 if (maxSamples < micCaptureSamples)
00155 return 0;
00156 if ((micBufferDescr[micCurrBuffer].dwFlags & WHDR_DONE) != 0)
00157 {
00158 memcpy(buffer, MicBuffer[micCurrBuffer], micCaptureSamples*sizeof(short));
00159 int NextBuffer = ((micCurrBuffer+1)%NUM_MIC_BUFFERS);
00160 waveInAddBuffer(hMicrophone, &(micBufferDescr[micCurrBuffer]), sizeof(WAVEHDR));
00161 micCurrBuffer = NextBuffer;
00162 return micCaptureSamples*sizeof(short);
00163 }
00164 else
00165 return 0;
00166 }
00167
00168 int waveAudioDriver::msOutQueued()
00169 {
00170 int msDelay=0;
00171 MMTIME sPosn;
00172 sPosn.wType = TIME_SAMPLES;
00173 waveOutGetPosition(hSpeaker, &sPosn, sizeof(MMTIME));
00174 if (sPosn.u.sample <= spkIndex)
00175 msDelay = (spkIndex-sPosn.u.sample) / PCM_SAMPLES_PER_MS;
00176 else
00177 msDelay = 0;
00178 return msDelay;
00179 }
00180
00181 int waveAudioDriver::samplesOutSpaceRemaining()
00182 {
00183 int remainingSamples=0;
00184 MMTIME sPosn;
00185 sPosn.wType = TIME_SAMPLES;
00186 waveOutGetPosition(hSpeaker, &sPosn, sizeof(MMTIME));
00187 if (sPosn.u.sample <= spkIndex)
00188 remainingSamples = SPK_BUFFER_SIZE-(spkIndex-sPosn.u.sample);
00189 else
00190 remainingSamples = 0;
00191 return remainingSamples;
00192 }
00193
00194 bool waveAudioDriver::openMicrophone()
00195 {
00196 WAVEFORMATEX wfx;
00197 int b;
00198
00199
00200 micCurrBuffer = 0;
00201
00202 wfx.cbSize = 0;
00203 wfx.nAvgBytesPerSec = 16000;
00204 wfx.nBlockAlign = 2;
00205 wfx.nChannels = 1;
00206 wfx.nSamplesPerSec = 8000;
00207 wfx.wBitsPerSample = 16;
00208 wfx.wFormatTag = WAVE_FORMAT_PCM;
00209
00210
00211 if (waveInOpen(0, MicDevice, &wfx, 0, 0, WAVE_FORMAT_QUERY))
00212 return FALSE;
00213
00214
00215 if (waveInOpen(&hMicrophone, MicDevice, &wfx, 0, 0, CALLBACK_NULL))
00216 return FALSE;
00217
00218
00219 for (b=0; b<NUM_MIC_BUFFERS; b++)
00220 {
00221 micBufferDescr[b].lpData = (LPSTR)(MicBuffer[b]);
00222 micBufferDescr[b].dwBufferLength = micCaptureSamples * sizeof(short);
00223 micBufferDescr[b].dwFlags = 0L;
00224 if (waveInPrepareHeader(hMicrophone, &(micBufferDescr[b]), sizeof(WAVEHDR)))
00225 return FALSE;
00226 }
00227
00228
00229 if (waveInStart(hMicrophone))
00230 return FALSE;
00231
00232
00233 for (b=0; b<NUM_MIC_BUFFERS; b++)
00234 {
00235 if (waveInAddBuffer(hMicrophone, &(micBufferDescr[b]), sizeof(WAVEHDR)))
00236 return FALSE;
00237 }
00238
00239 return TRUE;
00240 }
00241
00242
00243 bool waveAudioDriver::openSpeaker()
00244 {
00245 int b;
00246
00247 spkInBuffer = 0;
00248
00249
00250 WAVEFORMATEX wfx;
00251 wfx.cbSize = 0;
00252 wfx.nAvgBytesPerSec = 16000;
00253 wfx.nBlockAlign = 2;
00254 wfx.nChannels = 1;
00255 wfx.nSamplesPerSec = 8000;
00256 wfx.wBitsPerSample = 16;
00257 wfx.wFormatTag = WAVE_FORMAT_PCM;
00258
00259 if (waveOutOpen(&hSpeaker, SpeakerDevice, &wfx, 0, 0L, WAVE_FORMAT_QUERY))
00260 return FALSE;
00261
00262 if (waveOutOpen(&hSpeaker, SpeakerDevice, &wfx, 0, 0L, CALLBACK_NULL))
00263 return FALSE;
00264
00265 for (b=0; b<NUM_SPK_BUFFERS; b++)
00266 {
00267 spkBufferDescr[b].lpData = (LPSTR)(SpkBuffer[b]);
00268 spkBufferDescr[b].dwBufferLength = SPK_BUFFER_SIZE * sizeof(short);
00269 spkBufferDescr[b].dwFlags = (b == 0 ? WHDR_BEGINLOOP : 0) |
00270 (b == NUM_SPK_BUFFERS-1 ? WHDR_ENDLOOP : 0);
00271 spkBufferDescr[b].dwLoops = (b == 0 ? 0xFFFF : 0);
00272 spkBufferDescr[b].dwUser = 0;
00273
00274 if (waveOutPrepareHeader(hSpeaker, &(spkBufferDescr[b]), sizeof(WAVEHDR)))
00275 return FALSE;
00276
00277 memset(SpkBuffer[b], 0, SPK_BUFFER_SIZE*sizeof(short));
00278 }
00279
00280 return true;
00281 }
00282
00283
00284 bool waveAudioDriver::closeMicrophone()
00285 {
00286 int b;
00287
00288 if (waveInReset(hMicrophone))
00289 return FALSE;
00290
00291 for (b=0; b<NUM_MIC_BUFFERS; b++)
00292 {
00293 if (waveInUnprepareHeader(hMicrophone, &(micBufferDescr[b]), sizeof(WAVEHDR)))
00294 return FALSE;
00295 }
00296
00297 if (waveInClose(hMicrophone))
00298 return FALSE;
00299
00300 return TRUE;
00301 }
00302
00303
00304 bool waveAudioDriver::closeSpeaker()
00305 {
00306 if (waveOutReset(hSpeaker))
00307 return FALSE;
00308
00309 for (int b=0; b<NUM_SPK_BUFFERS; b++)
00310 {
00311 if (waveOutUnprepareHeader(hSpeaker, &(spkBufferDescr[b]), sizeof(WAVEHDR)))
00312 return FALSE;
00313 }
00314
00315 if (waveOutClose(hSpeaker))
00316 return FALSE;
00317
00318 return TRUE;
00319 }
00320
00321
00322 #endif
00323
00324
00325
00327
00329
00330 #ifdef WIN32
00331
00332 bool CALLBACK enumerateCallback(LPGUID lpGUID, LPCTSTR lpszDesc, LPCTSTR lpszDrvName, LPVOID lpContext)
00333 {
00334 dsAudioDriver *audio = (dsAudioDriver *)lpContext;
00335 if (audio)
00336 audio->dsEnumerateCallback(lpGUID, lpszDesc, lpszDrvName);
00337 return true;
00338 }
00339
00340 dsAudioDriver::dsAudioDriver(QString s, QString m, int mCap, HWND hMainWindow) : AudioDriver(mCap)
00341 {
00342 spkName = s;
00343 micName = m;
00344 spkGuid = 0;
00345 micGuid = 0;
00346 spkDS = 0;
00347 micDS = 0;
00348 dsSpkBuffer = 0;
00349 dsMicBuffer = 0;
00350 lastPlayPos = -1;
00351 lastReadPos = 0;
00352 micBufferBytes = 0;
00353 playBufferWraps = 0;
00354 enumerateSpeaker = true;
00355 DirectSoundEnumerate((LPDSENUMCALLBACK) enumerateCallback, this);
00356 enumerateSpeaker = false;
00357 DirectSoundCaptureEnumerate((LPDSENUMCALLBACK) enumerateCallback, this);
00358 if (spkGuid != 0)
00359 {
00360 if (DirectSoundCreate(spkGuid, &spkDS, 0) == DS_OK)
00361 {
00362 spkDS->SetCooperativeLevel(hMainWindow, DSSCL_PRIORITY);
00363 }
00364 }
00365 if (micGuid != 0)
00366 {
00367 DirectSoundCaptureCreate(micGuid, &micDS, 0);
00368 }
00369 }
00370
00371 dsAudioDriver::~dsAudioDriver()
00372 {
00373 if (dsMicBuffer)
00374 closeMicrophone();
00375 if (dsSpkBuffer)
00376 closeSpeaker();
00377
00378 if (micDS)
00379 micDS->Release();
00380 if (spkDS)
00381 spkDS->Release();
00382 spkDS = 0;
00383 micDS = 0;
00384
00385 if (spkGuid)
00386 delete spkGuid;
00387 if (micGuid);
00388 delete micGuid;
00389 spkGuid = micGuid = 0;
00390 }
00391
00392 void dsAudioDriver::Open()
00393 {
00394 if (spkDS != 0)
00395 openSpeaker();
00396 if (micDS != 0)
00397 openMicrophone();
00398 }
00399
00400 void dsAudioDriver::Close()
00401 {
00402 if (spkDS != 0)
00403 closeSpeaker();
00404 if (micDS != 0)
00405 closeMicrophone();
00406 }
00407
00408 void dsAudioDriver::StartSpeaker()
00409 {
00410 spkByteIndex = 0;
00411 dsSpkBuffer->Play(0, 0, DSBPLAY_LOOPING);
00412 }
00413
00414 int dsAudioDriver::Write(short *data, int samples)
00415 {
00416 DWORD playPos, writePos, endPos;
00417 if (dsSpkBuffer->GetCurrentPosition(&playPos, &writePos) != DS_OK)
00418 return 0;
00419
00420 if ((int)playPos < lastPlayPos)
00421 playBufferWraps++;
00422 lastPlayPos = playPos;
00423
00424 int bytesToWrite = samples*sizeof(short);
00425
00426 int i = spkByteIndex % DS_SPK_BUFFER_BYTES;
00427
00428
00429
00430 if ((i + bytesToWrite) <= DS_SPK_BUFFER_BYTES)
00431 {
00432 memcpy(dsSpkMemory+i, data, bytesToWrite);
00433 }
00434 else
00435 {
00436 int temp = DS_SPK_BUFFER_BYTES - i;
00437 memcpy(dsSpkMemory+i, data, temp);
00438 memcpy(dsSpkMemory, ((uchar *)data)+temp, bytesToWrite-temp);
00439 }
00440
00441 spkByteIndex += bytesToWrite;
00442
00443 return bytesToWrite;
00444 }
00445
00446 bool dsAudioDriver::anyMicrophoneData()
00447 {
00448 DWORD capPosn, readPosn;
00449 if (dsMicBuffer->GetCurrentPosition(&capPosn, &readPosn) != DS_OK)
00450 return false;
00451
00452 int bytesAvailable = (int)readPosn - lastReadPos;
00453 if (bytesAvailable < 0)
00454 bytesAvailable += micBufferBytes;
00455
00456 return (bytesAvailable >= (micCaptureSamples * sizeof(short)) ? true : false);
00457 }
00458
00459 int dsAudioDriver::Read(short *buffer, int maxSamples)
00460 {
00461 if (maxSamples < micCaptureSamples)
00462 return 0;
00463 void *micBuffer1, *micBuffer2;
00464 DWORD len1, len2;
00465 HRESULT res;
00466 if ((res=dsMicBuffer->Lock(lastReadPos, micCaptureSamples*sizeof(short), &micBuffer1, &len1, &micBuffer2, &len2, 0)) == DS_OK)
00467 {
00468 if (micBuffer1)
00469 memcpy(buffer, micBuffer1, len1);
00470 if (micBuffer2)
00471 memcpy(((char *)buffer)+len1, micBuffer2, len2);
00472 dsMicBuffer->Unlock(micBuffer1, len1, micBuffer2, len2);
00473 lastReadPos = (lastReadPos+len1+len2) % micBufferBytes;
00474 return (len1+len2);
00475 }
00476 return 0;
00477 }
00478
00479 int dsAudioDriver::msOutQueued()
00480 {
00481 DWORD playPos, writePos, endPos;
00482 if (dsSpkBuffer->GetCurrentPosition(&playPos, &writePos) != DS_OK)
00483 return 0;
00484
00485 if ((int)playPos < lastPlayPos)
00486 playBufferWraps++;
00487 lastPlayPos = playPos;
00488 int totalPlayedBytes = (playBufferWraps * DS_SPK_BUFFER_BYTES) + playPos;
00489 int bytesQueued = spkByteIndex - totalPlayedBytes;
00490
00491
00492 if (bytesQueued < 0)
00493 bytesQueued = 0;
00494
00495 return (bytesQueued / sizeof(short) / PCM_SAMPLES_PER_MS);
00496 }
00497
00498 int dsAudioDriver::samplesOutSpaceRemaining()
00499 {
00500 DWORD playPos, writePos, endPos;
00501 if (dsSpkBuffer->GetCurrentPosition(&playPos, &writePos) != DS_OK)
00502 return 0;
00503
00504 if ((int)playPos < lastPlayPos)
00505 playBufferWraps++;
00506 lastPlayPos = playPos;
00507 int totalPlayedBytes = (playBufferWraps * DS_SPK_BUFFER_BYTES) + playPos;
00508 int bytesQueued = spkByteIndex - totalPlayedBytes;
00509
00510
00511 if (bytesQueued < 0)
00512 bytesQueued = 0;
00513
00514
00515
00516 return ((DS_SPK_BUFFER_BYTES-bytesQueued) / sizeof(short));
00517 }
00518
00519 void dsAudioDriver::dsEnumerateCallback(LPGUID lpGUID, LPCTSTR lpszDesc, LPCTSTR lpszDrvName)
00520 {
00521 if (enumerateSpeaker)
00522 {
00523 if ((spkName == lpszDesc) && (spkGuid == 0))
00524 {
00525 spkGuid = new GUID;
00526 memcpy(spkGuid, lpGUID, sizeof(GUID));
00527 }
00528 }
00529 else
00530 {
00531 if ((micName == lpszDesc) && (micGuid == 0))
00532 {
00533 micGuid = new GUID;
00534 memcpy(micGuid, lpGUID, sizeof(GUID));
00535 }
00536 }
00537 }
00538
00539 bool dsAudioDriver::openMicrophone()
00540 {
00541 DSCBUFFERDESC dscBufferDescr;
00542 WAVEFORMATEX wf;
00543
00544
00545 memset(&wf, 0, sizeof(WAVEFORMATEX));
00546 wf.wFormatTag = WAVE_FORMAT_PCM;
00547 wf.nChannels = 1;
00548 wf.nSamplesPerSec = 8000;
00549 wf.nBlockAlign = 2;
00550 wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nBlockAlign;
00551 wf.wBitsPerSample = 16;
00552
00553
00554 micBufferBytes = DS_NUM_MIC_BUFFERS * micCaptureSamples * sizeof(short);
00555 memset(&dscBufferDescr, 0, sizeof(DSCBUFFERDESC));
00556 dscBufferDescr.dwSize = sizeof(DSCBUFFERDESC);
00557 dscBufferDescr.dwFlags = 0;
00558 dscBufferDescr.dwBufferBytes = micBufferBytes;
00559 dscBufferDescr.lpwfxFormat = &wf;
00560
00561 if (micDS->CreateCaptureBuffer(&dscBufferDescr, &dsMicBuffer, 0) != DS_OK)
00562 return false;
00563
00564
00565 dsMicBuffer->Start(DSCBSTART_LOOPING);
00566
00567 return true;
00568 }
00569
00570
00571 bool dsAudioDriver::openSpeaker()
00572 {
00573 DSBUFFERDESC dsBufferDescr;
00574 WAVEFORMATEX wf;
00575
00576
00577 memset(&wf, 0, sizeof(WAVEFORMATEX));
00578 wf.wFormatTag = WAVE_FORMAT_PCM;
00579 wf.nChannels = 1;
00580 wf.nSamplesPerSec = 8000;
00581 wf.nBlockAlign = 2;
00582 wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nBlockAlign;
00583 wf.wBitsPerSample = 16;
00584
00585
00586 memset(&dsBufferDescr, 0, sizeof(DSBUFFERDESC));
00587 dsBufferDescr.dwSize = sizeof(DSBUFFERDESC);
00588 dsBufferDescr.dwFlags = DSBCAPS_CTRLPOSITIONNOTIFY |
00589 DSBCAPS_GETCURRENTPOSITION2 |
00590 DSBCAPS_GLOBALFOCUS;
00591 dsBufferDescr.dwBufferBytes = DS_SPK_BUFFER_BYTES;
00592 dsBufferDescr.lpwfxFormat = &wf;
00593
00594 if (spkDS->CreateSoundBuffer(&dsBufferDescr, &dsSpkBuffer, 0) != DS_OK)
00595 return false;
00596
00597 DWORD audioBytes1;
00598 void *audioPtr2;
00599 DWORD audioBytes2;
00600 if (dsSpkBuffer->Lock(0, 0, (void **)&dsSpkMemory, &audioBytes1, &audioPtr2, &audioBytes2, DSBLOCK_ENTIREBUFFER ) != DS_OK)
00601 return false;
00602
00603 memset(dsSpkMemory, 0, audioBytes1);
00604 if (audioPtr2)
00605 memset(audioPtr2, 0, audioBytes2);
00606
00607 if (dsSpkBuffer->Unlock(dsSpkMemory, audioBytes1, audioPtr2, audioBytes2) != DS_OK)
00608 return false;
00609
00610 return true;
00611 }
00612
00613
00614 bool dsAudioDriver::closeMicrophone()
00615 {
00616 if (dsMicBuffer)
00617 {
00618 dsMicBuffer->Stop();
00619 dsMicBuffer->Release();
00620 dsMicBuffer = 0;
00621 }
00622
00623 return true;
00624 }
00625
00626
00627 bool dsAudioDriver::closeSpeaker()
00628 {
00629 if (dsSpkBuffer)
00630 {
00631 dsSpkBuffer->Stop();
00632 dsSpkBuffer->Release();
00633 dsSpkBuffer = 0;
00634 }
00635 return true;
00636 }
00637
00638
00639 #endif
00640
00641
00642
00644
00646
00647 #ifndef WIN32
00648 ossAudioDriver::ossAudioDriver(QString s, QString m, int mCap) : AudioDriver(mCap)
00649 {
00650 speakerFd = -1;
00651 microphoneFd = -1;
00652 spkDevice = s;
00653 micDevice = m;
00654 readAnyData = false;
00655 }
00656
00657 ossAudioDriver::~ossAudioDriver()
00658 {
00659 }
00660
00661 void ossAudioDriver::Open()
00662 {
00663
00664 if (spkDevice == micDevice)
00665 microphoneFd = speakerFd = OpenAudioDevice(spkDevice, O_RDWR);
00666 else
00667 {
00668 if (spkDevice.length() > 0)
00669 speakerFd = OpenAudioDevice(spkDevice, O_WRONLY);
00670
00671 if ((micDevice.length() > 0) && (micDevice != "None"))
00672 microphoneFd = OpenAudioDevice(micDevice, O_RDONLY);
00673 }
00674 }
00675
00676 void ossAudioDriver::Close()
00677 {
00678 if (speakerFd > 0)
00679 close(speakerFd);
00680 if ((microphoneFd != speakerFd) && (microphoneFd > 0))
00681 close(microphoneFd);
00682
00683 speakerFd = -1;
00684 microphoneFd = -1;
00685 }
00686
00687 void ossAudioDriver::StartSpeaker()
00688 {
00689
00690 }
00691
00692 int ossAudioDriver::Write(short *data, int samples)
00693 {
00694 return write(speakerFd, data, samples*sizeof(short));
00695 }
00696
00697 bool ossAudioDriver::anyMicrophoneData()
00698 {
00699 if (!readAnyData)
00700 return true;
00701 audio_buf_info info;
00702 ioctl(microphoneFd, SNDCTL_DSP_GETISPACE, &info);
00703 if (info.bytes > (int)(micCaptureSamples*sizeof(short)))
00704 return true;
00705 return false;
00706 }
00707
00708 int ossAudioDriver::Read(short *buffer, int maxSamples)
00709 {
00710 if (maxSamples < micCaptureSamples)
00711 return 0;
00712 readAnyData = true;
00713 return read(microphoneFd, (char *)buffer, micCaptureSamples*sizeof(short));
00714 }
00715
00716 int ossAudioDriver::msOutQueued()
00717 {
00718
00719
00720 int bytesQueued;
00721 ioctl(speakerFd, SNDCTL_DSP_GETODELAY, &bytesQueued);
00722 return (bytesQueued / sizeof(short) / PCM_SAMPLES_PER_MS);
00723 }
00724
00725 int ossAudioDriver::samplesOutSpaceRemaining()
00726 {
00727 audio_buf_info info;
00728 ioctl(speakerFd, SNDCTL_DSP_GETOSPACE, &info);
00729 return (info.bytes / sizeof(short));
00730 }
00731
00732 int ossAudioDriver::OpenAudioDevice(QString devName, int mode)
00733 {
00734 int fd = open(devName, mode, 0);
00735 if (fd == -1)
00736 {
00737 cerr << "Cannot open device " << devName << endl;
00738 return -1;
00739 }
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749 int format = AFMT_S16_LE;
00750 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) == -1)
00751 {
00752 cerr << "Error setting audio driver format\n";
00753 close(fd);
00754 return -1;
00755 }
00756
00757 int channels = 1;
00758 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) == -1)
00759 {
00760 cerr << "Error setting audio driver num-channels\n";
00761 close(fd);
00762 return -1;
00763 }
00764
00765 int speed = 8000;
00766 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) == -1)
00767 {
00768 cerr << "Error setting audio driver speed\n";
00769 close(fd);
00770 return -1;
00771 }
00772
00773 if ((format != AFMT_S16_LE) || (channels != 1) || (speed != 8000))
00774 {
00775 cerr << "Error setting audio driver; " << format << ", " << channels << ", " << speed << endl;
00776 close(fd);
00777 return -1;
00778 }
00779
00780 uint frag_size = 0x7FFF0007;
00781 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag_size) == -1)
00782 {
00783 cerr << "Error setting audio fragment size\n";
00784 close(fd);
00785 return -1;
00786 }
00787
00788 int flags;
00789 if ((flags = fcntl(fd, F_GETFL, 0)) > 0)
00790 {
00791 flags &= O_NDELAY;
00792 fcntl(fd, F_SETFL, flags);
00793 }
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803 return fd;
00804 }
00805
00806 #endif
00807
00808
00809
00811
00813
00814 #ifndef WIN32
00815 mythAudioDriver::mythAudioDriver(QString s, QString m, int mCap) : AudioDriver(mCap)
00816 {
00817 mythOutput = 0;
00818 microphoneFd = -1;
00819 spkDevice = s;
00820 micDevice = m;
00821 readAnyData = false;
00822 }
00823
00824 mythAudioDriver::~mythAudioDriver()
00825 {
00826 if (mythOutput)
00827 delete mythOutput;
00828 mythOutput = 0;
00829 }
00830
00831 void mythAudioDriver::Open()
00832 {
00833
00834 if (spkDevice == micDevice)
00835 cerr << "Cannot have matching spk and mic devices in this mode, should have chosen OSS mode\n";
00836 else
00837 {
00838 mythOutput = AudioOutput::OpenAudio(spkDevice, "default", 16, 1, 8000,
00839 AUDIOOUTPUT_TELEPHONY, true,
00840 false );
00841 if (mythOutput)
00842 {
00843 mythOutput->SetBlocking(false);
00844 mythOutput->SetEffDsp(8000 * 100);
00845 }
00846
00847 if ((micDevice.length() > 0) && (micDevice != "None"))
00848 microphoneFd = OpenAudioDevice(micDevice, O_RDONLY);
00849 }
00850 }
00851
00852 void mythAudioDriver::Close()
00853 {
00854 if (mythOutput)
00855 delete mythOutput;
00856 mythOutput = 0;
00857
00858 if (microphoneFd > 0)
00859 close(microphoneFd);
00860 microphoneFd = -1;
00861 }
00862
00863 void mythAudioDriver::StartSpeaker()
00864 {
00865
00866 }
00867
00868 int mythAudioDriver::Write(short *data, int samples)
00869 {
00870 if (mythOutput)
00871 {
00872 mythOutput->AddSamples((char *)data, samples, 100);
00873 return samples*sizeof(short);
00874 }
00875 else
00876 return 0;
00877 }
00878
00879 bool mythAudioDriver::anyMicrophoneData()
00880 {
00881 if (!readAnyData)
00882 return true;
00883 audio_buf_info info;
00884 ioctl(microphoneFd, SNDCTL_DSP_GETISPACE, &info);
00885 if (info.bytes > (int)(micCaptureSamples*sizeof(short)))
00886 return true;
00887 return false;
00888 }
00889
00890 int mythAudioDriver::Read(short *buffer, int maxSamples)
00891 {
00892 if (maxSamples < micCaptureSamples)
00893 return 0;
00894 readAnyData = true;
00895 return read(microphoneFd, (char *)buffer, micCaptureSamples*sizeof(short));
00896 }
00897
00898 int mythAudioDriver::msOutQueued()
00899 {
00900 if (mythOutput)
00901 {
00902 int ms = mythOutput->GetAudiotime();
00903
00904
00905
00906
00907 return 120-ms;
00908 }
00909 else
00910 return 0xFFFF;
00911 }
00912
00913 int mythAudioDriver::samplesOutSpaceRemaining()
00914 {
00915 return micCaptureSamples*20;
00916 }
00917
00918 int mythAudioDriver::OpenAudioDevice(QString devName, int mode)
00919 {
00920 int fd = open(devName, mode, 0);
00921 if (fd == -1)
00922 {
00923 cerr << "Cannot open device " << devName << endl;
00924 return -1;
00925 }
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935 int format = AFMT_S16_LE;
00936 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) == -1)
00937 {
00938 cerr << "Error setting audio driver format\n";
00939 close(fd);
00940 return -1;
00941 }
00942
00943 int channels = 1;
00944 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) == -1)
00945 {
00946 cerr << "Error setting audio driver num-channels\n";
00947 close(fd);
00948 return -1;
00949 }
00950
00951 int speed = 8000;
00952 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) == -1)
00953 {
00954 cerr << "Error setting audio driver speed\n";
00955 close(fd);
00956 return -1;
00957 }
00958
00959 if ((format != AFMT_S16_LE) || (channels != 1) || (speed != 8000))
00960 {
00961 cerr << "Error setting audio driver; " << format << ", " << channels << ", " << speed << endl;
00962 close(fd);
00963 return -1;
00964 }
00965
00966 uint frag_size = 0x7FFF0007;
00967 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag_size) == -1)
00968 {
00969 cerr << "Error setting audio fragment size\n";
00970 close(fd);
00971 return -1;
00972 }
00973
00974 int flags;
00975 if ((flags = fcntl(fd, F_GETFL, 0)) > 0)
00976 {
00977 flags &= O_NDELAY;
00978 fcntl(fd, F_SETFL, flags);
00979 }
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989 return fd;
00990 }
00991
00992 #endif
00993
00994
00995
00996