OsStatus MpidAndroid::disableDevice() { OsStatus status = OS_SUCCESS; LOGI("MpidAndroid::disableDevice()"); if (!isDeviceValid() || !isEnabled()) { LOGW("Invalid device (valid=%d) or already disabled (enable=%d).", isDeviceValid(), isEnabled()); return OS_FAILED; } // Start accessing non-atomic member variables AutoMutex autoLock(mLock); if (mState == DRIVER_RECORDING || mState == DRIVER_STARTING) { mState = DRIVER_STOPPING; LOGV("MpidAndroid::disableDevice() waiting cond"); status_t lStatus = mWaitCbkCond.waitRelative(mLock, seconds(3)); if (lStatus == NO_ERROR) { LOGV("MpidAndroid::disableDevice() track stop complete, time %d", (unsigned int)(systemTime()/1000000)); } else { LOGW("MpidAndroid::disableDevice() Stop timed out"); mState = DRIVER_IDLE; } } if (mpAudioRecord != NULL) { mpAudioRecord->stop(); delete mpAudioRecord; mpAudioRecord = NULL; #ifdef MPID_ANDROID_CLEAN_EXIT // [ sgAudioRecord = NULL; #endif // MPID_ANDROID_CLEAN_EXIT ] } // Clear out all the audio stream information. mSamplesPerFrame = 0; mSamplesPerSec = 0; mCurrentFrameTime = 0; // Free internal buffer delete[] mpBufInternal; mBufInternalSamples = 0; // Free resampler delete mpResampler; mpResampler = NULL; delete[] mpResampleBuf; mpResampleBuf = NULL; // Indicate driver is no longer enabled mIsEnabled = FALSE; return status; }
OsStatus MpidOss::disableDevice() { OsStatus ret; if (!isEnabled()) { return OS_FAILED; } // If the device is not valid, let the user know it's bad. if (!isDeviceValid()) { return OS_INVALID_STATE; } ret = pDevWrapper->detachReader(); if (ret != OS_SUCCESS) { return ret; } delete[] mAudioFrame; mIsEnabled = FALSE; pDevWrapper->freeInputDevice(); return ret; }
void *NclLayoutConverter::createRegionBase( DOMElement* parentElement ) { bptnl::RegionBase *layout; std::string mapId = ""; bptcf::XMLChHandler* xmlHndl = getXmlHandler(); layout = new bptnl::RegionBase( xmlHndl->getStr( parentElement->getAttribute( xmlHndl->getXMLCh( "id" ) ) ).c_str() ); bptnl::Device device; // device attribute if (parentElement->hasAttribute( xmlHndl->getXMLCh( "device" ) )) { // region for output bit map attribute if (parentElement->hasAttribute( xmlHndl->getXMLCh( "region" ) )) { mapId = xmlHndl->getStr( parentElement->getAttribute( xmlHndl->getXMLCh( "region" ) ) ); } std::string deviceStr = xmlHndl->getStr( parentElement->getAttribute( xmlHndl->getXMLCh( "device" ) ) ).c_str(); if ( !isDeviceValid( deviceStr, device ) ){ LERROR("NclLayoutConverter", "invalid device, value=%s", deviceStr.c_str()); return NULL; } } else { device.name = "systemScreen(0)"; device.number = 0; } layout->setDevice( device, mapId ); return layout; }
/* ============================ MANIPULATORS ============================== */ OsStatus MpidOss::enableDevice(unsigned samplesPerFrame, unsigned samplesPerSec, MpFrameTime currentFrameTime) { OsSysLog::add(FAC_MP, PRI_DEBUG, "MpidOss::enableDevice(samplesPerFrame: %d, samplesPerSec: %d, currentFrameTime: %d)", samplesPerFrame, samplesPerSec, currentFrameTime); OsStatus ret; if (isEnabled()) { return OS_FAILED; } if (pDevWrapper) { OsStatus res = pDevWrapper->setInputDevice(this); if (res != OS_SUCCESS) { pDevWrapper = NULL; } else if (!pDevWrapper->mbReadCap) { //Device dosen't support input pDevWrapper->freeInputDevice(); pDevWrapper = NULL; } } // If the device is not valid, let the user know it's bad. if (!isDeviceValid()) { OsSysLog::add(FAC_MP, PRI_ERR, "MpidOss::enableDevice not valid"); return OS_INVALID_STATE; } // Set some wave header stat information. mSamplesPerFrame = samplesPerFrame; mSamplesPerSec = samplesPerSec; mCurrentFrameTime = currentFrameTime; // Get buffer and fill it with silence mAudioFrame = new MpAudioSample[samplesPerFrame]; if (mAudioFrame == NULL) { return OS_LIMIT_REACHED; } memset(mAudioFrame, 0, samplesPerFrame * sizeof(MpAudioSample)); ret = pDevWrapper->attachReader(); if (ret != OS_SUCCESS) { return ret; } mIsEnabled = TRUE; return ret; }
OsStatus MpodOss::enableDevice(unsigned samplesPerFrame, unsigned samplesPerSec, MpFrameTime currentFrameTime, OsCallback &frameTicker) { OsStatus ret; if (isEnabled()) { return OS_FAILED; } //Opening OSS device if (pDevWrapper) { OsStatus res = pDevWrapper->setOutputDevice(this); if (res != OS_SUCCESS) { pDevWrapper = NULL; } else if (!pDevWrapper->mbWriteCap) { //Device dosen't support output pDevWrapper->freeOutputDevice(); pDevWrapper = NULL; } } // If the device is not valid, let the user know it's bad. if (!isDeviceValid()) { return OS_INVALID_STATE; } // Set some wave header stat information. mSamplesPerFrame = samplesPerFrame; mSamplesPerSec = samplesPerSec; mCurrentFrameTime = currentFrameTime; mpTickerNotification = &frameTicker; // Get buffer for audio data and fill it with silence mAudioFrame = new MpAudioSample[samplesPerFrame]; if (mAudioFrame == NULL) { return OS_LIMIT_REACHED; } memset(mAudioFrame, 0, samplesPerFrame * sizeof(MpAudioSample)); ret = pDevWrapper->attachWriter(); if (ret != OS_SUCCESS) { return ret; } mIsEnabled = TRUE; return ret; }
MpodOss::~MpodOss() { // OSS Device must be freed assert (!isDeviceValid()); if (mpCont != NULL) { MpOssContainer::releaseContainer(mpCont); } }
OsStatus MpidWinMM::disableDevice() { OsStatus status = OS_SUCCESS; MMRESULT res; if (!isDeviceValid() || !isEnabled()) { return OS_FAILED; } // Indicate we are no longer enabled -- Do this first, // since we'll be partially disabled from here on out. // It is very important that this happen *before* waveInReset, // as the callback will continue to add and process buffers // while waveInReset is called causing a deadlock. mIsEnabled = FALSE; // Cleanup if (mDevHandle == NULL) { return OS_INVALID_STATE; } // Reset performs a stop, resets the buffers, and marks them // for being sent to the callback. // The remaining data in the windows buffers *IS* sent to the callback, // So be sure to watch for it and drop it on the floor. res = waveInReset(mDevHandle); if (res != MMSYSERR_NOERROR) { showWaveError("waveInReset", res, -1, __LINE__); } // Must unprepare the headers after a reset, but before the device is closed // (if this is done after waveInClose, mDevHandle will be invalid and // MMSYSERR_INVALHANDLE will be returned. unsigned i; for (i=0; i < mNumInBuffers; i++) { res = waveInUnprepareHeader(mDevHandle, &mpWaveHeaders[i], sizeof(WAVEHDR)); if (res != MMSYSERR_NOERROR) { showWaveError("waveInUnprepareHeader", res, i, __LINE__); } } res = waveInClose(mDevHandle); if (res != MMSYSERR_NOERROR) { showWaveError("waveInClose", res, -1, __LINE__); } // Delete the buffers that were allocated in enableDevice() for (i = 0; i < mNumInBuffers; i++) { delete[] mpWaveBuffers[i]; mpWaveBuffers[i] = NULL; } // set the device handle to NULL, since it no longer is valid. mDevHandle = NULL; // Clear out all the wave header information. mSamplesPerFrame = 0; mSamplesPerSec = 0; mCurrentFrameTime = 0; return status; }
OsStatus MpidWinMM::enableDevice(unsigned samplesPerFrame, unsigned samplesPerSec, MpFrameTime currentFrameTime) { OsStatus status = OS_SUCCESS; // reset the number of addBuffer failures, as we're starting fresh now. mnAddBufferFailures = 0; // If the device is not valid, let the user know it's bad. if (!isDeviceValid()) { return OS_INVALID_STATE; // perhaps new OsState of OS_RESOURCE_INVALID? } if (isEnabled()) { return OS_FAILED; } // Set some wave header stat information. mSamplesPerFrame = samplesPerFrame; mSamplesPerSec = samplesPerSec; mCurrentFrameTime = currentFrameTime; // Do stuff to enable device. int nChannels = 1; WAVEFORMATEX wavFormat; wavFormat.wFormatTag = WAVE_FORMAT_PCM; wavFormat.nChannels = nChannels; wavFormat.nSamplesPerSec = mSamplesPerSec; wavFormat.nAvgBytesPerSec = nChannels * mSamplesPerSec * sizeof(MpAudioSample); wavFormat.nBlockAlign = nChannels * sizeof(MpAudioSample); wavFormat.wBitsPerSample = sizeof(MpAudioSample) * 8; wavFormat.cbSize = 0; // Tell windows to open the input audio device. This doesn't // tell it to send the data to our callback yet, just to get it ready // to do so.. MMRESULT res = waveInOpen(&mDevHandle, mWinMMDeviceId, &wavFormat, (CBTYPE)waveInCallbackStatic, (CBTYPE)this, CALLBACK_FUNCTION); if (res != MMSYSERR_NOERROR) { // If waveInOpen failed, print out the error info, // invalidate the handle, and the device driver itself, status = OS_FAILED; showWaveError("MpidWinMM::enableDevice", res, -1, __LINE__); waveInClose(mDevHandle); mDevHandle = NULL; // Open didn't work, reset device handle to NULL mWinMMDeviceId = -1; // Make device invalid. // and return OS_FAILED. return status; } // Allocate the buffers we are going to use to receive audio data from // the windows audio input callback. // Calculate the buffer length we're going to use. // number of samples per frame * sample size in bytes mWaveBufSize = mSamplesPerFrame * sizeof(MpAudioSample); unsigned i; for (i = 0; i < mNumInBuffers; i++) { mpWaveBuffers[i] = new char[mWaveBufSize]; } // Setup the buffers so windows can stuff them full of audio // when it becomes available from this audio input device. WAVEHDR* pWaveHdr = NULL; for (i=0; i < mNumInBuffers; i++) { pWaveHdr = initWaveHeader(i); res = waveInPrepareHeader(mDevHandle, pWaveHdr, sizeof(WAVEHDR)); if (res != MMSYSERR_NOERROR) { showWaveError("waveInPrepareHeader", res, i, __LINE__); waveInClose(mDevHandle); mDevHandle = NULL; mWinMMDeviceId = -1; // and return OS_FAILED. return status; } res = waveInAddBuffer(mDevHandle, pWaveHdr, sizeof(WAVEHDR)); if (res != MMSYSERR_NOERROR) { showWaveError("waveInAddBuffer", res, i, __LINE__); waveInClose(mDevHandle); mDevHandle = NULL; mWinMMDeviceId = -1; // and return OS_FAILED. return status; } } // Tell windows to start sending audio data to the callback. res = waveInStart(mDevHandle); if (res != MMSYSERR_NOERROR) { // If waveInStart failed, print out the error info, // invalidate the handle and the device driver itself, status = OS_FAILED; showWaveError("waveInStart", res, -1, __LINE__); waveInClose(mDevHandle); mDevHandle = NULL; mWinMMDeviceId = -1; // and return OS_FAILED. return status; } // If enableDevice failed, return indicating failure. if (status == OS_SUCCESS) { mIsEnabled = TRUE; } return status; }
OsStatus MpidAndroid::enableDevice(unsigned samplesPerFrame, unsigned samplesPerSec, MpFrameTime currentFrameTime) { OsStatus status = OS_SUCCESS; // If the device is not valid, let the user know it's bad. if (!isDeviceValid()) { return OS_INVALID; // perhaps new OsState of OS_RESOURCE_INVALID? } if (isEnabled()) { return OS_NAME_IN_USE; } LOGI("MpidAndroid::enableDevice()"); // Start accessing non-atomic member variables AutoMutex autoLock(mLock); if (mState != DRIVER_IDLE) { LOGW("MpidAndroid::enableDevice() wrong state %d\n", mState); return OS_INVALID_STATE; } // Set some wave header stat information. mSamplesPerFrame = samplesPerFrame; mSamplesPerSec = samplesPerSec; mCurrentFrameTime = currentFrameTime; if (probeSampleRate(mSamplesPerSec, mSamplesPerFrame, mpSampleRatesList, mSampleRatesListLen)) { LOGV("MpidAndroid::enableDevice() INIT OK, time: %"PRId64"\n", ns2ms(systemTime())); } else { LOGW("MpidAndroid::enableDevice() INIT FAILED!!!\n"); return OS_INVALID_ARGUMENT; } if (mState != DRIVER_INIT) { LOGW("MpidAndroid::enableDevice() wrong state: %d\n", mState); return OS_INVALID_STATE; } // Allocate internal buffer assert(mSamplesPerFrameInternal > 0); mpBufInternal = new MpAudioSample[mSamplesPerFrameInternal]; assert(mpBufInternal != NULL); mBufInternalSamples = 0; // Create resampler if (mSamplesPerSecInternal != mSamplesPerSec) { OsSysLog::add(FAC_MP, PRI_ERR, "mSamplesPerSecInternal: %d mSamplesPerSec: %d\n", mSamplesPerSecInternal, mSamplesPerSec); LOGV("mSamplesPerSecInternal: %d mSamplesPerSec: %d\n", mSamplesPerSecInternal, mSamplesPerSec); mpResampler = MpResamplerBase::createResampler(1, mSamplesPerSecInternal, mSamplesPerSec); mpResampleBuf = new MpAudioSample[mSamplesPerFrame]; assert(mpResampler != NULL && mpResampleBuf != NULL); } mState = DRIVER_STARTING; mLock.unlock(); int startStatus = mpAudioRecord->start(); if(startStatus != NO_ERROR) { OsSysLog::add(FAC_MP, PRI_ERR, "MpidAndroid::enableDevice AudioRecord::start returned error: %d", startStatus); LOGE("MpidAndroid::enableDevice AudioRecord::start returned error: %d", startStatus); switch(startStatus) { case INVALID_OPERATION: status = OS_LIMIT_REACHED; break; default: status = OS_FAILED; break; } mState = DRIVER_IDLE; mIsEnabled = FALSE; return(status); } mLock.lock(); if (mState == DRIVER_STARTING) { LOGV("MpidAndroid::enableDevice() waiting for start callback"); status_t lStatus = mWaitCbkCond.waitRelative(mLock, seconds(3)); if (lStatus != NO_ERROR) { LOGE("MpidAndroid::enableDevice() callback timed out, status %d", lStatus); mState = DRIVER_IDLE; mIsEnabled = FALSE; return OS_WAIT_TIMEOUT; } } else { LOGW("MpidAndroid::enableDevice() state %d\n", mState); } LOGV("MpidAndroid::enableDevice() started, time %"PRId64"\n", ns2ms(systemTime())); // Indicate driver has been started. mIsEnabled = TRUE; return status; }