⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cxx

📁 这是一个自动应答机程序
💻 CXX
📖 第 1 页 / 共 5 页
字号:
    PString subStr = key.Left(len);

    PINDEX matches = 0;
    PINDEX lastMatch = P_MAX_INDEX;
    PINDEX i;

    // look for a match to the substring
    for (i = 0; i < list.GetSize(); i++) {
      if ((list[i].GetLength() >= keyLen) && MatchString(list[i].Left(len), subStr)) {
        matches++;
        lastMatch = i;
      }
    }

    // if we got ONE match, we have a winner
    if (matches == 1)
      return lastMatch+1;

    // if we have no matches, then there is no point continuing
    if (matches == 0)
      return P_MAX_INDEX;

    // if we have more than one match, try the next char
    len++;
  }

  // too many matches
  return 0;
}


MyH323Connection::MyH323Connection(MyH323EndPoint & _ep, unsigned callReference, unsigned _options)
  : H323Connection(_ep, callReference, _options), ep(_ep)
{
  basename = psprintf("%04i%02i%02i_%02i%02i%02i", callStartTime.GetYear(), callStartTime.GetMonth(),  callStartTime.GetDay(),
                                                   callStartTime.GetHour(), callStartTime.GetMinute(), callStartTime.GetSecond());
  recordFile = NULL;
  ogmChannel = NULL;

  receiveAudioCodecName = transmitAudioCodecName = "none";

#if OPENAM_VIDEO
  receiveVideoCodecName = transmitVideoCodecName = "none";
#endif

#ifndef DISABLE_COUT
  cout << "Opening connection" << endl;
#endif

  currentMenu = 0;
  digits = "";

  PConfig config;
  PStringList sections = config.GetSections();
  PINDEX i;
  for (i = 0; i < sections.GetSize(); i++) {
    if (sections[i].Find(MENU_PREFIX) == 0) 
      menuNames.AppendString(sections[i]);
  }

}

BOOL MyH323Connection::OnReceivedSignalSetup(const H323SignalPDU & setupPDU)
{
  if (!H323Connection::OnReceivedSignalSetup(setupPDU))
    return FALSE;

  // extract the called number
  const H225_Setup_UUIE & setup = setupPDU.m_h323_uu_pdu.m_h323_message_body;
  if (setup.HasOptionalField(H225_Setup_UUIE::e_destinationAddress) && setup.m_destinationAddress.GetSize() > 0) {
    calledParty = H323GetAliasAddressString(setup.m_destinationAddress[0]);
  }

  return TRUE;
}

MyH323Connection::~MyH323Connection()
{
  cout << "Call ended" << endl;

  PTime now;
  PTimeInterval interval = now - recordStartTime;
  PString addr = GetControlChannel().GetRemoteAddress();

  PString codecStr = receiveAudioCodecName + "/" + transmitAudioCodecName;
#if OPENAM_VIDEO
  codecStr += "/" + transmitVideoCodecName;
#endif

  unsigned duration = (unsigned)((interval.GetMilliSeconds()+999)/1000);

  LogCall(recordFn, addr, GetRemotePartyName(), duration, codecStr, product, calledParty);

  if ((recordFile!= NULL) && (recordFile->WasRecordStarted()) && !ep.GetRunCmd().IsEmpty()) {
    PString cmdStr = ep.GetRunCmd() &
                     recordFn &
                     "'" + addr + "'" &
                     "\"" + GetRemotePartyName() + "\"" &
                     PString(PString::Unsigned, duration) &
                     "\"" + codecStr + "\"" &
                     "\"" + product + "\"" &
                     "\"" + calledParty + "\"";
    PTRACE(1, "Executing : " << cmdStr);
    system((const char *)cmdStr);
  } else {
    PTRACE(1, "No action to perform at end of record");
  }

  if (ogmChannel != NULL)
    delete ogmChannel;

  if (recordFile != NULL)
    delete recordFile;

  if (ep.GetDeleteAfterRecord()) {
    PTRACE(1, "Removing " << recordFn << " as requested by option");
    PFile::Remove(recordFn);
  }
}

H323Connection::AnswerCallResponse
     MyH323Connection::OnAnswerCall(const PString & caller,
                                    const H323SignalPDU & setupPDU,
                                    H323SignalPDU & /*connectPDU*/)
{
  product = "Unknown";

  const H225_Setup_UUIE & setup = setupPDU.m_h323_uu_pdu.m_h323_message_body;
  const H225_EndpointType & epInfo = setup.m_sourceInfo;

  if (epInfo.HasOptionalField(H225_EndpointType::e_vendor)) {
    const H225_VendorIdentifier & vendorInfo = epInfo.m_vendor;
    if (vendorInfo.HasOptionalField(H225_VendorIdentifier::e_productId))
      product = vendorInfo.m_productId.AsString();
    if (vendorInfo.HasOptionalField(H225_VendorIdentifier::e_versionId))
      product = product + "/" + vendorInfo.m_versionId.AsString();
  }
  
  cout << "Accepting call from " << caller << " using " << product << endl;

  setupPDU.GetSourceE164(sourceno);
  sourceno.Delete(0,1);

  return AnswerCallNow;
}

BOOL MyH323Connection::OpenAudioChannel(BOOL isEncoding, 
                                        unsigned /* bufferSize */, 
                                        H323AudioCodec & codec)
{
  codec.SetSilenceDetectionMode(H323AudioCodec::NoSilenceDetection);
  PString codecName = codec.GetMediaFormat();

  PString ogm;
  BOOL isPCM = FALSE;

  if (codecName == OPAL_G7231_6k3) {
    ogm   = ep.GetG7231OGM();
    isPCM = FALSE;
  } else {
    static OpalMediaFormat::List list = H323PluginCodecManager::GetMediaFormats();
    if (list.GetValuesIndex(codecName) == P_MAX_INDEX) {
      cerr << "Unknown codec \"" << codecName << endl;
      return FALSE;
    }
    isPCM = TRUE;
    if (codecName == OPAL_G711_ULAW_64K || codecName == OPAL_G711_ALAW_64K)
      ogm = ep.GetG711OGM();
    else if (codecName == OPAL_GSM0610)
      ogm = ep.GetGSMOGM();
    else if (codecName.Find("iLBC") != P_MAX_INDEX)
      ogm = ep.GetiLBCOGM();
    else if (codecName.Find("Speex") != P_MAX_INDEX)
      ogm = ep.GetSPEEXOGM();
    else
      ogm = ep.GetG711OGM();
  }

  PWaitAndSignal mutex(connMutex);

  if ((recordFile == NULL) && (isEncoding == FALSE)) {
    if (isPCM) {
      if (ep.GetRecordWav() == TRUE)
	      recordFn = ep.GetDirectory() + (basename + ".wav");
      else
	      recordFn = ep.GetDirectory() + (basename + ".sw");
      recordFile = new PCM_RecordFile  (*this, recordFn, ep.GetCallLimit());
    } else {
      if (ep.GetRecordWav() == TRUE)
	      recordFn = ep.GetDirectory() + (basename + ".wav");
      else
	      recordFn = ep.GetDirectory() + (basename + ".g723");
      recordFile = new G7231_RecordFile(*this, recordFn, ep.GetCallLimit());
    }
  }

  if ((ogmChannel == NULL) && (isEncoding == TRUE)) {
    if (isPCM)
      ogmChannel = new PCM_OGMChannel(*this);
    else
      ogmChannel = new G7231_OGMChannel(*this);
  }

  if (isEncoding) {

    if (ep.GetHangupAfterPlay())
      ogmChannel->SetPlayOnce();

	if (ep.GetLoopMessage())
	  ogmChannel->SetLoopMessage();

    if (ogm.Find("%s"))
      ogm.Replace("%s", e164Number);

    transmitAudioCodecName = codecName;
    if (!StartMenu(0)) {
      if (!PFile::Exists(ogm)) {
        cerr << "error: cannot find OGM \"" << ogm << "\"" << endl;
        return FALSE;
      }
      else
        ogmChannel->QueueFile(ogm);
      if (!(ep.GetNoRecordG7231() || ep.GetLoopMessage()))
        ogmChannel->SetRecordTrigger();
    }
    codec.AttachChannel(ogmChannel, FALSE);
  } else {
    receiveAudioCodecName = codecName;
    codec.AttachChannel(recordFile, FALSE);
  }

  return TRUE;
}

#if OPENAM_VIDEO

BOOL MyH323Connection::OpenVideoChannel(BOOL isEncoding, H323VideoCodec & codec)
{
  PString capture_filename(ep.GetDirectory() + (basename + ".yuv"));
  if (!isEncoding) {
    receiveVideoCodecName = codec.GetMediaFormat(); 
    PVideoOutputDevice * display = NULL;
	if (ep.GetLoopMessage()) {	// just loop message, no recording
		// TODO: this would be better, but fails when building with 'make optnoshared'
		// PTRACE(3, "Try to create NULLOutput device");
		// display = PVideoOutputDevice::CreateDevice("NULLOutput");
		display = new PVideoOutputDevice_YUVFile();
		PTRACE(3, "Try to create YUVFile device on NULL device");
#ifdef _WIN32
		capture_filename = "NUL:";
#else
		capture_filename = "/dev/null";
#endif
	} else {
		PTRACE(3, "Try to create YUVFile device");
		display = new PVideoOutputDevice_YUVFile();
	}   
    if (display == NULL) {
      PTRACE(3, "Cannot create video output device");
      return FALSE;
    }

    if (!display->Open(capture_filename)) {
      delete display;
      return FALSE;
    }
     
    display->SetFrameSize(codec.GetWidth(), codec.GetHeight()); // needed to enable resize
    display->SetColourFormatConverter("YUV420P");

	PTRACE(1,"Video device opened IN: " << codec.GetMediaFormat() << " : " << codec.GetWidth() << "x" << codec.GetHeight());
    PVideoChannel * channel = new TimeLimitedVideoChannel(*this, ep.GetCallLimit()); 
    channel->AttachVideoPlayer(display); 
    return codec.AttachChannel(channel,TRUE);
  }

  PString nameStr = isEncoding ? PString("Local") : GetRemotePartyName();
  transmitVideoCodecName = codec.GetMediaFormat();

  //if (0 != videoTxMinQuality) // set MinQuality first so TxQuality cannot be set lower
  //  codec.SetTxMinQuality(videoTxMinQuality);

  //if (0 != videoTxQuality)
  //  codec.SetTxQualityLevel(videoTxQuality);

  //codec.SetBackgroundFill(videoFill);

  //if (0 != videoBitRate) {
//    codec.SetMaxBitRate(videoBitRate);
//    codec.SetVideoMode(
//    H323VideoCodec::DynamicVideoQuality | 
//    H323VideoCodec::AdaptivePacketDelay |
//    codec.GetVideoMode());
  //}

  //if (0 != frameTimeMs) {
  //  codec.SetTargetFrameTimeMs(frameTimeMs);
  //  codec.SetVideoMode(
  //    H323VideoCodec::DynamicVideoQuality | 
  //    H323VideoCodec::AdaptivePacketDelay |
  //    codec.GetVideoMode());
  //}

  // create the device using the file driver name
  PVideoChannel      * channel = new TimeLimitedVideoChannel(*this, ep.GetCallLimit());
  PVideoInputDevice  * grabber = PVideoInputDevice::CreateDevice(VideoGrabberDriverName);
  if (grabber == NULL) {
    PTRACE(3, "Cannot create video input device for driver " << VideoGrabberDriverName);
    return FALSE;
  }

  if (!InitGrabber(grabber, codec.GetWidth(), codec.GetHeight())) {
    delete grabber;
    return FALSE;
  }

  PTRACE(1,"Video device opened OUT: " << codec.GetMediaFormat() << " : " << codec.GetWidth() << "x" << codec.GetHeight());
  grabber->Start();
  channel->AttachVideoReader(grabber);

  return codec.AttachChannel(channel,TRUE);
}

BOOL MyH323Connection::InitGrabber(PVideoInputDevice * grabber, unsigned codecFrameWidth, unsigned codecFrameHeight)
{
  unsigned int grabberFrameWidth  =  352 >> (1 - ep.GetVideoSize());                
  unsigned int grabberFrameHeight = (ep.GetVideoIsPal() ? 288 : 240) >> (1 - ep.GetVideoSize());                

  //if (!(pfdColourFormat.IsEmpty()))
  //  grabber->SetPreferredColourFormat(pfdColourFormat);

  PTRACE(3, "Attempt to open file " << ep.GetVideoOGM() << " for reading."); 
  if (!grabber->Open(ep.GetVideoOGM(), FALSE)) {
    PTRACE(3, "Failed to open the video input device");
    return FALSE;
  }

  if (!grabber->SetChannel(ep.GetVideoPlayMode())) {
    PTRACE(3, "Failed to set channel to " << ep.GetVideoPlayMode());
    return FALSE;
  }

  if (!grabber->SetVideoFormat(
      ep.GetVideoIsPal() ? PVideoDevice::PAL : PVideoDevice::NTSC)) {
    PTRACE(3, "Failed to set format to " << (ep.GetVideoIsPal() ? "PAL" : "NTSC"));
    return FALSE;
  }

  if (!grabber->SetColourFormatConverter("YUV420P") ) {
    PTRACE(3,"Failed to set format to YUV420P");
    return FALSE;
  }

  if (ep.GetVideoFrameRate() != 0) {
    if (!grabber->SetFrameRate(ep.GetVideoFrameRate())) {
      PTRACE(3, "Failed to set framerate to " << ep.GetVideoFrameRate());
      return FALSE;
    }
  }

  // set the grabber to grab the size specified by the command line
  grabber->SetFrameSizeConverter(grabberFrameWidth, grabberFrameHeight, FALSE);

  // if the codec wants a different size, then change to that size
  if (grabberFrameWidth != codecFrameWidth || grabberFrameHeight != codecFrameHeight) {
    if (!grabber->SetFrameSizeConverter(codecFrameWidth, codecFrameHeight,FALSE)) {
      PTRACE(3, "Failed to set frame size to " << codecFrameWidth << "x" << codecFrameHeight);
      return FALSE;
    }
  }

  return TRUE;
}

#endif // OPENAM_VIDEO


BOOL MyH323Connection::OnStartLogicalChannel(H323Channel & channel)
{
  if (!H323Connection::OnStartLogicalChannel(channel))
    return FALSE;

#ifndef DISABLE_COUT
  cout << "Started logical channel: ";

  switch (channel.GetDirection()) {
    case H323Channel::IsTransmitter :
      cout << "sending ";
      break;

    case H323Channel::IsReceiver :
      cout << "receiving ";
      break;

    default :
      break;
  }

  cout << channel.GetCapability() << endl;
#endif

  return TRUE;
}

void MyH323Connection::StartRecording()
{
  recordFile->StartRecording();
}

void MyH323Connection::OnUserInputString(const PString & value)
{
  PINDEX i;
  for (i = 0; i < value.GetLength(); i++) {
    OnUserInputChar(value[i]);
  }
}

BOOL MyH323Connection::StartMenu(int menuNumber)
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -