void testEnabledWithData() { MprToSpkr* pToSpkr = NULL; OsMsgQ* pSpkQ = NULL; OsMsgQ* pEchoQ = NULL; MpBufferMsg* pSpkMsg = NULL; MpBufferMsg* pEchoMsg = NULL; MpBufPtr pBuf; OsStatus res; // Create message queues to get data from MprToSpkr pSpkQ = new OsMsgQ(MSG_Q_LEN); CPPUNIT_ASSERT(pSpkQ != NULL); pEchoQ = new OsMsgQ(MSG_Q_LEN); CPPUNIT_ASSERT(pEchoQ != NULL); pToSpkr = new MprToSpkr("MprToSpkr", pSpkQ, pEchoQ); CPPUNIT_ASSERT(pToSpkr != NULL); setupFramework(pToSpkr); // pToSpkr enabled, there are buffers on the input 0, message queue // is not full. CPPUNIT_ASSERT(mpSourceResource->enable()); CPPUNIT_ASSERT(pToSpkr->enable()); res = mpFlowGraph->processNextFrame(); CPPUNIT_ASSERT(res == OS_SUCCESS); // Get messages from the queues (wait for 1 second) res = pSpkQ->receive((OsMsg*&)pSpkMsg, OsTime(1000)); CPPUNIT_ASSERT(res == OS_SUCCESS); res = pEchoQ->receive((OsMsg*&)pEchoMsg, OsTime(1000)); CPPUNIT_ASSERT(res == OS_SUCCESS); // Store output buffer for convenience pBuf = mpSourceResource->mLastDoProcessArgs.outBufs[0]; // Buffer is sent to queues and to output CPPUNIT_ASSERT( (mpSinkResource->mLastDoProcessArgs.inBufs[0] == pBuf) && (pSpkMsg->getBuffer().isValid()) && (pEchoMsg->getBuffer() == pBuf) ); // Free received message and stored buffer pSpkMsg->releaseMsg(); pEchoMsg->releaseMsg(); pBuf.release(); // Stop flowgraph haltFramework(); // Free message queue delete pSpkQ; delete pEchoQ; }
UtlBoolean MpResource::handleMessages(OsMsgQ& msgQ) { UtlBoolean handledAllMsgs = FALSE; while(!msgQ.isEmpty()) { OsMsg* msg; OsStatus recvStat = msgQ.receive(msg, OsTime::NO_WAIT_TIME); UtlBoolean curMsgHandled = FALSE; if (recvStat == OS_SUCCESS) { if (msg->getMsgType() == OsMsg::MP_FLOWGRAPH_MSG) { MpFlowGraphMsg* fgMsg = static_cast<MpFlowGraphMsg*>(msg); if (fgMsg != NULL) { curMsgHandled = handleMessage(*fgMsg); } } else if (msg->getMsgType() == OsMsg::MP_RESOURCE_MSG) { MpResourceMsg* rMsg = static_cast<MpResourceMsg*>(msg); if (rMsg != NULL) { curMsgHandled = handleMessage(*rMsg); } } // TODO: If message received is not handled, it might be good // to stuff the message back into the front of the queue // with msgQ.sendUrgent(msg, OsTime::NO_WAIT_TIME); // I haven't thought through the ramifications of this, // so I haven't implemented it yet. } // Stop at the first message we encounter that is not handled. if (curMsgHandled == FALSE) { break; } // If the queue is empty at this point, // then all messages have been handled. if (msgQ.isEmpty()) { handledAllMsgs = TRUE; } } return handledAllMsgs; }
UtlBoolean removeMessage(OsMsgQ& messageQueue, int waitMilliSeconds, const SipMessage*& message) { UtlBoolean gotMessage = FALSE; message = NULL; OsTime messageTimeout(0, waitMilliSeconds * 1000); OsMsg* osMessage = NULL; messageQueue.receive(osMessage, messageTimeout); if(osMessage) { int msgType = osMessage->getMsgType(); int msgSubType = osMessage->getMsgSubType(); int messageType = ((SipMessageEvent*)osMessage)->getMessageStatus(); if(msgType == OsMsg::PHONE_APP && msgSubType == SipMessage::NET_SIP_MESSAGE && messageType == SipMessageEvent::APPLICATION) { message = ((SipMessageEvent*)osMessage)->getMessage(); gotMessage = TRUE; #ifdef TEST_PRINT if(message) { UtlString messageBytes; int len; message->getBytes(&messageBytes, &len); printf("%s", messageBytes.data()); } else { printf("removeMessage: messageBytes: <null>\n"); } #endif } } return(gotMessage); }
UtlBoolean MprFromMic::doProcessFrame(MpBufPtr inBufs[], MpBufPtr outBufs[], int inBufsSize, int outBufsSize, UtlBoolean isEnabled, int samplesPerFrame, int samplesPerSecond) { MpBufPtr out = NULL ; MpBufferMsg* pMsg; if (0 == outBufsSize) { return FALSE; } // Clear the the number of empty frames every 512 frames mNumFrames++; if (0 == (mNumFrames & 0x1ff)) { mNumEmpties = 0; } if (isEnabled) { // If the microphone queue (holds unprocessed mic data) has more then // the max_mic_buffers threshold, drain the queue until in range) OsMsgQ* pMicOutQ; pMicOutQ = MpMisc.pMicQ; while (pMicOutQ && MpMisc.max_mic_buffers < pMicOutQ->numMsgs()) { if (OS_SUCCESS == pMicOutQ->receive((OsMsg*&) pMsg, OsTime::NO_WAIT)) { MpBuf_delRef(pMsg->getTag()); MpBuf_delRef(pMsg->getTag(1)); pMsg->releaseMsg(); } } if (pMicOutQ && pMicOutQ->numMsgs() <= 0) { // osPrintf("MprFromMic: No data available (total frames=%d, starved frames=%d)\n", // mNumFrames, mNumEmpties); } else { if (pMicOutQ && OS_SUCCESS == pMicOutQ->receive((OsMsg*&) pMsg, OsTime::NO_WAIT)) { out = pMsg->getTag(); pMsg->releaseMsg(); if (NULL != out) { #ifdef REAL_SILENCE_DETECTION /* [ */ Sample* shpTmpFrame; MpBufPtr tpBuf; int n; #endif /* REAL_SILENCE_DETECTION ] */ switch(MpBuf_getSpeech(out)) { case MP_SPEECH_TONE: break; case MP_SPEECH_MUTED: MpBuf_setSpeech(out, MP_SPEECH_SILENT); break; default: #ifdef REAL_SILENCE_DETECTION /* [ */ Sample *shpSamples; n = MpBuf_getNumSamples(out); shpSamples = MpBuf_getSamples(out); tpBuf = MpBuf_getBuf(MpMisc.UcbPool, n, 0, MP_FMT_T12); assert(NULL != tpBuf); shpTmpFrame = MpBuf_getSamples(tpBuf); highpass_filter800(shpSamples, shpTmpFrame, n); if(0 == speech_detected(shpTmpFrame,n)) { MpBuf_setSpeech(out, MP_SPEECH_SILENT); } else { MpBuf_setSpeech(out, MP_SPEECH_ACTIVE); } MpBuf_delRef(tpBuf); #else /* REAL_SILENCE_DETECTION ] [ */ // 24 April 2001 (HZM) I am disabling this because it takes // too long to recognize the beginning of a talk spurt, and // causes the bridge mixer to drop the start of each word. MpBuf_isActiveAudio(out); #endif /* REAL_SILENCE_DETECTION ] */ break; } } } } #ifdef INSERT_SAWTOOTH /* [ */ if (NULL == out) { out = MpBuf_getBuf(MpMisc.UcbPool, MpMisc.frameSamples, 0, MP_FMT_T12); } MpBuf_insertSawTooth(out); MpBuf_setSpeech(out, MP_SPEECH_ACTIVE); #endif /* INSERT_SAWTOOTH ] */ if (s_fnMicDataHook) { // // Allow an external identity to source microphone data. Ideally, // this should probably become a different resource, but abstracting // a new CallFlowGraph is a lot of work. // if (NULL == out) { out = MpBuf_getBuf(MpMisc.UcbPool, MpMisc.frameSamples, 0, MP_FMT_T12); } if (NULL != out) { int n = 0; Sample* s = NULL; s = MpBuf_getSamples(out); n = MpBuf_getNumSamples(out); s_fnMicDataHook(n, s) ; MpBuf_setSpeech(out, MP_SPEECH_UNKNOWN); MpBuf_isActiveAudio(out); } } if (NULL == out) { out = MpBuf_getFgSilence(); } } *outBufs = out; return TRUE; }
//! Send a pager style instant message to the given destination UtlBoolean SipPimClient::sendPagerMessage(Url& destinationAor, const char* messageText, int& responseCode, UtlString& responseCodeText) { UtlBoolean returnCode = FALSE; responseCode = -1; responseCodeText.remove(0); if(messageText && *messageText) { // Construct the text body HttpBody* textBody = new HttpBody(messageText, strlen(messageText), CONTENT_TYPE_TEXT_PLAIN); // Construct the MESSAGE request UtlString toAddress = destinationAor.toString(); UtlString requestUri; destinationAor.getUri(requestUri); UtlString callId; CallId::getNewCallId(callId); SipMessage messageRequest; messageRequest.setRequestData(SIP_MESSAGE_METHOD, requestUri, mFromField, toAddress, callId, 1, // sequenceNumber NULL); // contactUrl // Attache the body messageRequest.setBody(textBody); messageRequest.setContentType(CONTENT_TYPE_TEXT_PLAIN); // Set the queue to which the response will be deposited // for this specific request. OsMsgQ responseQueue; messageRequest.setResponseListenerQueue(&responseQueue); // Send the request returnCode = mpUserAgent->send(messageRequest); // wait for the response OsMsg* qMessage = NULL; // For now we will block forever. Theoretically this should // always get a response (e.g. worst case a 408 timed out). // If we do not wait forever, we need to be sure to wait the // the maximum transaction timeout period so that the qMessage // exists when the SipUserAgent queues the response. responseQueue.receive(qMessage); // If we got a response, get the response status code and text if(qMessage) { int msgType = qMessage->getMsgType(); int msgSubType = qMessage->getMsgSubType(); // SIP message if(msgType == OsMsg::PHONE_APP && msgSubType == SipMessage::NET_SIP_MESSAGE) { const SipMessage* messageResponse = ((SipMessageEvent*)qMessage)->getMessage(); if(messageResponse && messageResponse->isResponse()) { responseCode = messageResponse->getResponseStatusCode(); messageResponse->getResponseStatusText(&responseCodeText); } } } } return(returnCode); }