mainform.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "mainform.h"
  2. #include "ui_mainform.h"
  3. #include <portaudio.h>
  4. #include <QMessageBox>
  5. #include <QDebug>
  6. #include <QCoreApplication>
  7. #include "imediadeviceinfo.h"
  8. #ifndef MAX_STR_LEN
  9. #define MAX_STR_LEN 512
  10. #endif // !MAX_STR_LEN
  11. #ifndef MAX_PATH
  12. #define MAX_PATH 260
  13. #endif // !MAX_PATH
  14. typedef int (*lpfn_get_cameracountfun)();
  15. typedef int (*lpfn_get_videodevice_namefun)(int device_id, char* buf, int len);
  16. typedef int (*lpfn_get_videodevice_infofun)(int device_id, char* namebuf, int namelen, char* pathbuf, int pathlen);
  17. typedef int (*lpfn_get_videodeviceid)(const char* dev_name);
  18. static lpfn_get_cameracountfun get_cameracount = NULL;
  19. static lpfn_get_videodevice_namefun get_videodevice_name = NULL;
  20. static lpfn_get_videodevice_infofun get_videodevice_info = NULL;
  21. static lpfn_get_videodevice_namefun get_device_fullpathname = NULL;
  22. static lpfn_get_videodeviceid get_videodeviceid = NULL;
  23. static int audio_translate_id(int in_direction, int idx)
  24. {
  25. int i, n, ii;
  26. //audio_log_set_func(NULL);
  27. n = Pa_GetDeviceCount();
  28. for (i = 0, ii = 0; i < n; ++i) {
  29. const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
  30. if (in_direction) {
  31. if (info->maxInputChannels) {
  32. if (ii == idx) {
  33. //audio_log_set_func(__audio_log_func);
  34. return i;
  35. }
  36. ii++;
  37. }
  38. } else {
  39. if (info->maxOutputChannels) {
  40. if (ii == idx) {
  41. //audio_log_set_func(__audio_log_func);
  42. return i;
  43. }
  44. ii++;
  45. }
  46. }
  47. }
  48. //audio_log_set_func(__audio_log_func);
  49. return -1;
  50. }
  51. int audio_get_dev_count(int* in_cnt, int* out_cnt)
  52. {
  53. int i;
  54. int icnt = 0, ocnt = 0;
  55. int cnt = Pa_GetDeviceCount();
  56. printf("\n\ndevice count is %d.\n", cnt);
  57. for (i = 0; i < cnt; ++i) {
  58. const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
  59. if (info->maxInputChannels)
  60. icnt++;
  61. if (info->maxOutputChannels)
  62. ocnt++;
  63. }
  64. if (in_cnt)
  65. *in_cnt = icnt;
  66. if (out_cnt)
  67. *out_cnt = ocnt;
  68. return 0;
  69. }
  70. static char* audio_get_dev_name(char* buf, bool in_direction, int idx)
  71. {
  72. int cnt = Pa_GetDeviceCount();
  73. int ii, i;
  74. for (i = 0, ii = 0; i < cnt; ++i) {
  75. const PaDeviceInfo* info = Pa_GetDeviceInfo(i);
  76. if (in_direction) {
  77. if (info->maxInputChannels) {
  78. if (idx == ii) {
  79. strcpy(buf, info->name);
  80. return buf;
  81. }
  82. ii++;
  83. }
  84. } else {
  85. if (info->maxOutputChannels) {
  86. if (idx == ii) {
  87. strcpy(buf, info->name);
  88. return buf;
  89. }
  90. ii++;
  91. }
  92. }
  93. }
  94. return NULL;
  95. }
  96. static void show_audio_dev()
  97. {
  98. int icnt, ocnt;
  99. int rc = audio_get_dev_count(&icnt, &ocnt);
  100. if (rc == 0) {
  101. int i;
  102. char tmp[128];
  103. printf("audio input devices(%d):\n", icnt);
  104. for (i = 0; i < icnt; ++i) {
  105. audio_get_dev_name(tmp, true, i);
  106. printf("%d = %s\n", i, tmp);
  107. }
  108. printf("audio output devices(%d):\n", ocnt);
  109. for (i = 0; i < ocnt; ++i) {
  110. audio_get_dev_name(tmp, false, i);
  111. printf("%d = %s\n", i, tmp);
  112. }
  113. printf("\n");
  114. }
  115. }
  116. static void show_video_dev()
  117. {
  118. int icount = get_cameracount();
  119. printf("video devices(%d):\n", icount);
  120. int inumber = 0;
  121. for (int i = 0; i < 2 * icount; ++i) {
  122. char strcamera[2 * MAX_PATH] = { 0 };
  123. char strpath[MAX_PATH] = { 0 };
  124. if (0 == get_device_fullpathname(i, strcamera, 2 * MAX_PATH)) {
  125. printf("%d = %s\n", inumber++, strcamera);
  126. }
  127. }
  128. }
  129. MainForm::MainForm(QWidget *parent) :
  130. QWidget(parent),
  131. ui(new Ui::MainForm), deviceInfoLib(nullptr)
  132. {
  133. Pa_Initialize();
  134. ui->setupUi(this);
  135. }
  136. MainForm::~MainForm()
  137. {
  138. delete ui;
  139. if (deviceInfoLib != nullptr) {
  140. deviceInfoLib->unload();
  141. delete deviceInfoLib;
  142. }
  143. Pa_Terminate();
  144. }
  145. bool MainForm::loadExportFunctions()
  146. {
  147. QString appDir = QCoreApplication::applicationDirPath();
  148. QString libAbsolutePath = appDir + "/libmediadeviceinfo.so";
  149. qDebug() << "Enter loadExportFunctions" << " " << libAbsolutePath << endl;
  150. deviceInfoLib = new QLibrary(libAbsolutePath);
  151. deviceInfoLib->load();
  152. if (!deviceInfoLib->isLoaded()) {
  153. qDebug() << "Load libmediadeviceinfo.so failed: " << deviceInfoLib->errorString() << endl;
  154. return false;
  155. }
  156. get_cameracount = (lpfn_get_cameracountfun)deviceInfoLib->resolve("rvc_videocap_get_device_count");
  157. if (!get_cameracount) {
  158. qDebug() << "Load rvc_videocap_get_device_count failed: " << deviceInfoLib->errorString() << endl;
  159. deviceInfoLib->unload();
  160. return false;
  161. }
  162. get_videodevice_name = (lpfn_get_videodevice_namefun)deviceInfoLib->resolve("rvc_videocap_get_device_name");
  163. if (!get_videodevice_name) {
  164. qDebug() << "Load rvc_videocap_get_device_name failed: " << deviceInfoLib->errorString() << endl;
  165. deviceInfoLib->unload();
  166. return false;
  167. }
  168. get_videodevice_info = (lpfn_get_videodevice_infofun)deviceInfoLib->resolve("rvc_videocap_get_device_info");
  169. if (!get_videodevice_info) {
  170. qDebug() << "Load rvc_videocap_get_device_info failed: " << deviceInfoLib->errorString() << endl;
  171. deviceInfoLib->unload();
  172. return false;
  173. }
  174. get_device_fullpathname = (lpfn_get_videodevice_namefun)deviceInfoLib->resolve("rvc_videocap_get_device_fullpathname");
  175. if (!get_device_fullpathname) {
  176. qDebug() << "Load rvc_videocap_get_device_fullpathname failed: " << deviceInfoLib->errorString() << endl;
  177. deviceInfoLib->unload();
  178. return false;
  179. }
  180. get_videodeviceid = (lpfn_get_videodeviceid)deviceInfoLib->resolve("rvc_videocap_get_video_device_id");
  181. if (!get_videodeviceid) {
  182. qDebug() << "Load rvc_videocap_get_video_device_id failed: " << deviceInfoLib->errorString() << endl;
  183. deviceInfoLib->unload();
  184. return false;
  185. }
  186. return true;
  187. }
  188. void MainForm::on_pushButton_clicked()
  189. {
  190. QMessageBox::information(this, "Hello", tst);
  191. if (!loadExportFunctions()) {
  192. qDebug() << "load library failed!" << endl;
  193. return;
  194. }
  195. if (deviceInfoLib != nullptr) {
  196. show_audio_dev();
  197. show_video_dev();
  198. }
  199. }