jvoipsounddevice.cpp
Upload User: zixun1918
Upload Date: 2013-06-07
Package Size: 742k
Code Size: 3k
Category:

VOIP program

Development Platform:

Visual C++

  1. /*
  2.     This file is a part of JVOIPLIB, a library designed to facilitate
  3.     the use of Voice over IP (VoIP).
  4.     Copyright (C) 2000-2005  Jori Liesenborgs (jori@lumumba.uhasselt.be)
  5.     This library (JVOIPLIB) is based upon work done for my thesis at
  6.     the School for Knowledge Technology (Belgium/The Netherlands)
  7.     The full GNU Library General Public License can be found in the
  8.     file LICENSE.LGPL which is included in the source code archive.
  9.     This library is free software; you can redistribute it and/or
  10.     modify it under the terms of the GNU Library General Public
  11.     License as published by the Free Software Foundation; either
  12.     version 2 of the License, or (at your option) any later version.
  13.     This library is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.     Library General Public License for more details.
  17.     You should have received a copy of the GNU Library General Public
  18.     License along with this library; if not, write to the Free
  19.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  20.     USA
  21. */
  22. #include "jvoipconfig.h"
  23. #include "jvoipsounddevice.h"
  24. #include "debugnew.h"
  25. JVOIPSoundDeviceDriver *JVOIPSoundDevice::firstsdevdriver = NULL;
  26. JVOIPSoundDevice::JVOIPSoundDevice(JVOIPSoundDeviceDriver *s,bool read)
  27. {
  28. devdrv = s;
  29. devdrv->Register(this,read);
  30. forreading = read;
  31. }
  32. JVOIPSoundDevice::~JVOIPSoundDevice()
  33. {
  34. CloseDevice();
  35. }
  36. int JVOIPSoundDevice::OpenDevice(JVOIPSoundDevice **sdev,const std::string &devicename,bool read,int requestedrate)
  37. {
  38. JVOIPSoundDeviceDriver *tmpdrv;
  39. JVOIPSoundDevice *sounddev;
  40. bool found;
  41. // look if the device is already opened
  42. found = false;
  43. tmpdrv = firstsdevdriver;
  44. while (!found && tmpdrv)
  45. {
  46. if (tmpdrv->IsSameDevice(devicename))
  47. found = true;
  48. else
  49. tmpdrv = tmpdrv->next;
  50. }
  51. if (found)
  52. {
  53. if (read && tmpdrv->IsSomeoneReading())
  54. return ERR_JVOIPLIB_SOUNDCARDIO_ALREADYOPENEDFORREADING;
  55. if (!read && tmpdrv->IsSomeoneWriting())
  56. return ERR_JVOIPLIB_SOUNDCARDIO_ALREADYOPENEDFORWRITING;
  57. sounddev = new JVOIPSoundDevice(tmpdrv,read);
  58. if (sounddev == NULL)
  59. return ERR_JVOIPLIB_GENERAL_OUTOFMEM;
  60. }
  61. else // open new device
  62. {
  63. int status;
  64. tmpdrv = new JVOIPSoundDeviceDriver();
  65. if (tmpdrv == NULL)
  66. return ERR_JVOIPLIB_GENERAL_OUTOFMEM;
  67. status = tmpdrv->Init(devicename,requestedrate);
  68. if (status < 0)
  69. {
  70. delete tmpdrv;
  71. return status;
  72. }
  73. sounddev = new JVOIPSoundDevice(tmpdrv,read);
  74. if (sounddev == NULL)
  75. {
  76. delete tmpdrv;
  77. return ERR_JVOIPLIB_GENERAL_OUTOFMEM;
  78. }
  79. // link the driver in the list
  80. tmpdrv->next = firstsdevdriver;
  81. firstsdevdriver = tmpdrv;
  82. }
  83. *sdev = sounddev;
  84. return 0;
  85. }
  86. void JVOIPSoundDevice::CloseDevice()
  87. {
  88. JVOIPSoundDeviceDriver *tmp,*prevtmp;
  89. bool found;
  90. if (devdrv == NULL)
  91. return;
  92. devdrv->Unregister(this);
  93. if (devdrv->IsReferenced())
  94. {
  95. devdrv = NULL;
  96. return;
  97. }
  98. // the driver is no longer used: remove it from the list
  99. tmp = firstsdevdriver;
  100. prevtmp = NULL;
  101. found = false;
  102. while (tmp && !found)
  103. {
  104. if (tmp == devdrv)
  105. found = true;
  106. else
  107. {
  108. prevtmp = tmp;
  109. tmp = tmp->next;
  110. }
  111. }
  112. if (!found)
  113. std::cerr << "JVOIPSoundDevice::CloseDevice -- Warning: device not found in list !n";
  114. else
  115. {
  116. if (prevtmp)
  117. prevtmp->next = tmp->next;
  118. else
  119. firstsdevdriver = tmp->next;
  120. }
  121. delete tmp;
  122. devdrv = NULL;
  123. }