ews_capture.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include <locale.h>
  4. #include <portaudio.h>
  5. #include <memutil.h>
  6. #include <md5.h>
  7. #include <ipp.h>
  8. #include <intrin.h>
  9. #include "videoframework.h"
  10. #include "y2k_time.h"
  11. #include "libaudioqueue.h"
  12. #include "libvideoqueue.h"
  13. #include "rec_common.h"
  14. #include "ews_capture.h"
  15. #include "videohorflip.h"
  16. #include "Event.h"
  17. #include "EventCode.h"
  18. #include <cv.h>
  19. #include <cxcore.h>
  20. #include <highgui.h>
  21. #define av_always_inline __inline
  22. #define inline __inline
  23. #ifndef INT64_C
  24. #define INT64_C(c) (c##LL)
  25. #define UINT64_C(c) (c##UL)
  26. #endif
  27. #include <stdint.h>
  28. extern "C"
  29. {
  30. #include <libavutil\avutil.h>
  31. #include <libavcodec\avcodec.h>
  32. #include <libswscale\swscale.h>
  33. }
  34. #include "video_common/ffmpeg_api_cpp_adapter.h"
  35. #define CAPTURE_FRAME_TIME 20 // 20ms per frame
  36. #define CAPTURE_CLOCK 8000
  37. DeviceTypeEnum g_eDeviceType;
  38. typedef struct ews_audio_capture_t {
  39. PaStream *stream;
  40. Clibaudioqueue *shm_queue;
  41. ews_capture_t *parent;
  42. }ews_audio_capture_t;
  43. typedef struct ews_video_capture_t {
  44. videocap_t cap;
  45. Clibvideoqueue *snapshot_shm_queue;
  46. Clibvideoqueue *preview_shm_queue;
  47. Clibvideoqueue *rtp_shm_queue;
  48. Clibvideoqueue *sales_shm_queue;
  49. ews_capture_t *parent;
  50. int camera_type; // CAMERA_TYPE_xxx
  51. int frame_id;
  52. struct SwsContext *preview_sws_ctx;
  53. struct SwsContext *rtp_sws_ctx;
  54. }ews_video_capture_t;
  55. struct ews_capture_t
  56. {
  57. ews_capture_config_t config;
  58. ews_audio_capture_t *audio;
  59. ews_video_capture_t *video;
  60. };
  61. static int Bin2Str(unsigned char *x, int xlen, char *str, int str_size)
  62. {
  63. static const char *hex2char = "0123456789ABCDEF";
  64. int i, k = 0;
  65. if (str_size <= xlen * 2)
  66. return -1;
  67. for (i = 0; i < xlen; ++i) {
  68. int h = x[i] >> 4;
  69. int l = x[i] & 0xf;
  70. str[k++] = hex2char[h];
  71. str[k++] = hex2char[l];
  72. }
  73. str[k] = 0;
  74. return k;
  75. }
  76. static int translate_id(int in_direction, int idx)
  77. {
  78. int i, n, ii;
  79. n = Pa_GetDeviceCount();
  80. for (i = 0, ii = 0; i < n; ++i) {
  81. const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
  82. if (in_direction) {
  83. if (info->maxInputChannels) {
  84. if (ii == idx) {
  85. return i;
  86. }
  87. ii++;
  88. }
  89. } else {
  90. if (info->maxOutputChannels) {
  91. if (ii == idx) {
  92. return i;
  93. }
  94. ii++;
  95. }
  96. }
  97. }
  98. return -1;
  99. }
  100. static int StreamCallback(const void *input,
  101. void *output,
  102. unsigned long frameCount,
  103. const PaStreamCallbackTimeInfo* timeInfo,
  104. PaStreamCallbackFlags statusFlags,
  105. void *userData)
  106. {
  107. ews_audio_capture_t *audio_cap = (ews_audio_capture_t*)userData;
  108. if (input) {
  109. audio_frame frm;
  110. frm.bitspersample = 16;
  111. frm.format = 1;
  112. frm.data = (char*)const_cast<void*>(input);
  113. frm.framesize = frameCount << 1;
  114. frm.nchannels = 1;
  115. frm.samplespersec = audio_cap->parent->config.iaudiosamplerate;
  116. frm.iseriesnumber = 0;
  117. if (audio_cap && audio_cap->shm_queue){
  118. if (!audio_cap->shm_queue->InsertAudio(&frm)) {
  119. Dbg("Insert audio for surveillance record failed! frameCount:%d", frameCount);
  120. }
  121. }
  122. }
  123. if (output) {
  124. memset(output, 0, frameCount<<1);
  125. }
  126. return paContinue;
  127. }
  128. static ews_audio_capture_t *ews_audio_capture_create(ews_capture_t *cap)
  129. {
  130. ews_audio_capture_t *audio_cap = ZALLOC_T(ews_audio_capture_t);
  131. if (audio_cap) {
  132. audio_cap->parent = cap;
  133. audio_cap->shm_queue = new Clibaudioqueue(REC_COMMON_AUDIO_EWS_SHM_QUEUE);
  134. }
  135. return audio_cap;
  136. }
  137. static void ews_audio_capture_destroy(ews_audio_capture_t *audio_cap)
  138. {
  139. delete audio_cap->shm_queue;
  140. free(audio_cap);
  141. }
  142. static int ews_audio_capture_start(ews_audio_capture_t *audio_cap)
  143. {
  144. ews_capture_t *cap = audio_cap->parent;
  145. PaStreamParameters inParam = {0};
  146. PaStreamParameters outParam = {0};
  147. PaError paError;
  148. const PaDeviceInfo *info;
  149. int nId = ews_capture_get_audio_device_id(true, cap->config.strAudioIn);
  150. if (nId == -1)
  151. {
  152. //需要立即处理的告警使用Severity_High
  153. //LogWarn(Severity_High,Error_DevMedia,ERROR_MOD_MEDIACONTROLLER_HANDFREEIN_INITFAIL,"handfree in device config error,please check");
  154. return Error_AudioIN;
  155. }
  156. int in_dev_id = translate_id(TRUE, nId);
  157. if (in_dev_id < 0) {
  158. Dbg("audio in dev translate failed!");
  159. return Error_AudioIN;
  160. }
  161. info = Pa_GetDeviceInfo(in_dev_id);
  162. if (!info) {
  163. Dbg("get device info failed!");
  164. return Error_AudioIN;
  165. }
  166. inParam.channelCount = 1;
  167. inParam.device = in_dev_id;
  168. inParam.suggestedLatency = info->defaultLowInputLatency;
  169. inParam.sampleFormat = paInt16;
  170. inParam.hostApiSpecificStreamInfo = NULL;
  171. int iAudioCaptureSampleRate = cap->config.iaudiosamplerate;
  172. if (Pa_IsFormatSupported(&inParam, NULL, iAudioCaptureSampleRate) != paNoError) {
  173. Dbg("audio capture create error, cannot open audio input device, and current capture sample rate is %d.", iAudioCaptureSampleRate);
  174. return Error_AudioIN;
  175. }
  176. //打开流设备,可以用以下代码替换paError = Pa_OpenStream(&audio_cap->stream, &inParam, &outParam, CAPTURE_CLOCK,
  177. //CAPTURE_FRAME_TIME * CAPTURE_CLOCK/1000, paClipOff|paDitherOff, &StreamCallback, audio_cap);
  178. paError = Pa_OpenStream(&audio_cap->stream, &inParam, NULL, iAudioCaptureSampleRate,
  179. CAPTURE_FRAME_TIME * iAudioCaptureSampleRate/1000, paClipOff|paDitherOff, &StreamCallback, audio_cap);
  180. if (paError != paNoError) {
  181. Dbg("portaudio open stream failed! paError = %d", paError);
  182. return Error_AudioIN;
  183. }
  184. paError = Pa_StartStream(audio_cap->stream);
  185. if (paError != paNoError) {
  186. Dbg("portaudio start stream failed! paError = %d", paError);
  187. return Error_AudioIN;
  188. }
  189. return Error_Succeed;
  190. }
  191. static void ews_audio_capture_stop(ews_audio_capture_t *audio_cap)
  192. {
  193. if (audio_cap->stream) {
  194. Pa_AbortStream(audio_cap->stream);
  195. Pa_CloseStream(audio_cap->stream);
  196. audio_cap->stream = NULL;
  197. }
  198. }
  199. static int calc_capture_mode(int width, int height, int *mode)
  200. {
  201. const struct {
  202. int mode;
  203. int width;
  204. int height;
  205. } modes [] = {
  206. {VIDEOCAP_FRAME_SQCIF, VIDEOCAP_SQCIF_WIDTH, VIDEOCAP_SQCIF_HEIGHT},
  207. {VIDEOCAP_FRAME_QQVGA, VIDEOCAP_QQVGA_WIDTH, VIDEOCAP_QQVGA_HEIGHT},
  208. {VIDEOCAP_FRAME_QCIF, VIDEOCAP_QCIF_WIDTH, VIDEOCAP_QCIF_HEIGHT},
  209. {VIDEOCAP_FRAME_QVGA, VIDEOCAP_QVGA_WIDTH, VIDEOCAP_QVGA_HEIGHT},
  210. {VIDEOCAP_FRAME_CIF, VIDEOCAP_CIF_WIDTH, VIDEOCAP_CIF_HEIGHT},
  211. {VIDEOCAP_FRAME_VGA, VIDEOCAP_VGA_WIDTH, VIDEOCAP_VGA_HEIGHT},
  212. {VIDEOCAP_FRAME_4CIF, VIDEOCAP_4CIF_WIDTH, VIDEOCAP_4CIF_HEIGHT},
  213. {VIDEOCAP_FRAME_SVGA, VIDEOCAP_SVGA_WIDTH, VIDEOCAP_SVGA_HEIGHT},
  214. {VIDEOCAP_FRAME_NHD, VIDEOCAP_NHD_WIDTH, VIDEOCAP_NHD_HEIGHT},
  215. {VIDEOCAP_FRAME_SXGA, VIDEOCAP_SXGA_WIDTH, VIDEOCAP_SXGA_HEIGHT},
  216. {VIDEOCAP_FRAME_720P, VIDEOCAP_720P_WIDTH, VIDEOCAP_720P_HEIGHT},
  217. {VIDEOCAP_FRAME_1080P, VIDEOCAP_1080P_WIDTH, VIDEOCAP_1080P_HEIGHT},
  218. };
  219. int i;
  220. for (i = 0; i < array_size(modes); ++i) {
  221. if (modes[i].width == width && modes[i].height == height) {
  222. *mode = modes[i].mode;
  223. return 0;
  224. }
  225. }
  226. return Error_NotExist;
  227. }
  228. static int video_shm_enqueue(Clibvideoqueue *shm_queue, video_frame *frame, int flags)
  229. {
  230. videoq_frame tmp_frm;
  231. tmp_frm.data = frame->data[0];
  232. tmp_frm.framesize = frame->width * frame->height * 3;
  233. tmp_frm.format = VIDEOQ_FORMAT_RGB24;
  234. tmp_frm.width = frame->width;
  235. tmp_frm.height = frame->height;
  236. unsigned int nowtime = y2k_time_now();
  237. if (!shm_queue->InsertVideo(&tmp_frm, flags,nowtime)) {
  238. Dbg("caution: insert shm video failed!");
  239. return Error_Unexpect;
  240. } else {
  241. //Dbg("insert shm video ok!");
  242. return Error_Succeed;
  243. }
  244. }
  245. static void ews_cap_on_frame(void *user_data, video_frame *frame)
  246. {
  247. ews_video_capture_t *video_cap = (ews_video_capture_t *)user_data;
  248. ews_capture_t *cap = video_cap->parent;
  249. int rc;
  250. int flip = -1;
  251. if (cap->config.video_rotate == 0){
  252. flip = 0;
  253. }
  254. else if (cap->config.video_rotate == 180){
  255. flip = (VIDEOQUEUE_FLAG_VERTICAL_FLIP|VIDEOQUEUE_FLAG_HORIZONTAL_FLIP);
  256. }
  257. else {
  258. return;
  259. }
  260. video_cap->frame_id++;
  261. //Dbg("start ews on frame, id=%d, tick=%d", video_cap->frame_id, GetTickCount());
  262. //IplImage*img = NULL;
  263. //img = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3);
  264. //img->imageData = (char*)frame->data[0];
  265. //cvSaveImage("c:\\ews.jpg", img,0);
  266. //cvReleaseImageHeader(&img);
  267. rc = video_shm_enqueue(video_cap->snapshot_shm_queue, frame, flip==0?VIDEOQUEUE_FLAG_VERTICAL_FLIP:VIDEOQUEUE_FLAG_HORIZONTAL_FLIP);
  268. if (rc != Error_Succeed)
  269. {
  270. Dbg("ews snapshot queue enqueue shm failed! Error = %d, camera_type=%d", rc, video_cap->camera_type);
  271. }
  272. //// snapshot
  273. //if (rc==Error_Succeed)
  274. //{
  275. // if (*cap->config.ref_ews_capture_count)
  276. // {
  277. // Dbg("ews camera ref_env_capture_count=%d",*cap->config.ref_ews_capture_count);
  278. // InterlockedDecrement(cap->config.ref_ews_capture_count);
  279. // LogEvent(Severity_Middle, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENV, "agent capture env ok, and capture env finished!");
  280. // }
  281. //}
  282. // preview 320x240
  283. {
  284. video_frame preview_frame;
  285. video_frame_alloc(REC_COMMON_VIDEO_PREVIEW_WIDTH, REC_COMMON_VIDEO_PREVIEW_HEIGHT, VIDEO_FORMAT_RGB24, &preview_frame);
  286. memset(preview_frame.data[0], 0, preview_frame.height*preview_frame.linesize[0]);
  287. //uint8_t *src_data[4] = {frame->data[0] + 80*3, 0, 0, 0}; // cut for 480x360
  288. //sws_scale(video_cap->preview_sws_ctx, src_data, frame->linesize, 0, frame->height, preview_frame.data, preview_frame.linesize);
  289. uint8_t *dst_data[4] = {preview_frame.data[0] + 30 * preview_frame.linesize[0], 0, 0, 0}; // 320x180 paste to 320x240
  290. sws_scale(video_cap->preview_sws_ctx, frame->data, frame->linesize, 0, frame->height, dst_data, preview_frame.linesize);
  291. video_shm_enqueue(video_cap->preview_shm_queue, &preview_frame, flip);
  292. video_frame_free(&preview_frame);
  293. }
  294. // rtp 320x180
  295. {
  296. video_frame rtp_frame;
  297. video_frame_alloc(REC_COMMON_VIDEO_RTP_EWS_WIDTH, REC_COMMON_VIDEO_RTP_EWS_HEIGHT, VIDEO_FORMAT_RGB24, &rtp_frame);
  298. uint8_t *src_data[4] = {frame->data[0] + (frame->height-1) * frame->linesize[0], 0, 0, 0};
  299. int src_linesize[4] = {-frame->linesize[0], 0, 0, 0}; // 上下翻转并缩放
  300. sws_scale(video_cap->rtp_sws_ctx, src_data, src_linesize, 0, frame->height, rtp_frame.data, rtp_frame.linesize);
  301. video_shm_enqueue(video_cap->rtp_shm_queue, &rtp_frame, flip);
  302. video_shm_enqueue(video_cap->sales_shm_queue, &rtp_frame, flip);
  303. #if 0
  304. static int i = 0;
  305. if (i == 0 && 0) {
  306. video_frame tmp_frame;
  307. video_frame_alloc(320, 180, VIDEO_FORMAT_RGB24, &tmp_frame);
  308. video_frame_fill_black(&tmp_frame);
  309. videoq_frame frm;
  310. frm.data = tmp_frame.data[0];
  311. video_cap->rtp_shm_queue->GetVideo(&frm, 0);
  312. video_frame_save_bmpfile("d:\\abc.bmp", &tmp_frame);
  313. video_frame_free(&tmp_frame);
  314. //video_frame_save_bmpfile("d:\\ab.bmp", &rtp_frame);
  315. }
  316. #endif
  317. video_frame_free(&rtp_frame);
  318. }
  319. //Dbg("end ews on frame, id=%d, tick=%d", video_cap->frame_id, GetTickCount());;
  320. }
  321. static ews_video_capture_t *ews_video_capture_create(ews_capture_t *cap, int camera_type)
  322. {
  323. ews_video_capture_t *video_cap = ZALLOC_T(ews_video_capture_t);
  324. if (video_cap) {
  325. video_cap->parent = cap;
  326. video_cap->camera_type = camera_type;
  327. video_cap->frame_id = 0;
  328. if (camera_type == CAMERA_TYPE_EWS) { // need to be edited
  329. video_cap->snapshot_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_EWS_SHM_SNAPSHOT_QUEUE);
  330. video_cap->rtp_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_EWS_SHM_RTP_QUEUE);
  331. video_cap->sales_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_SALES_EWS_SHM_RTP_QUEUE);
  332. video_cap->rtp_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  333. REC_COMMON_VIDEO_SNAPSHOT_HEIGHT,
  334. PIX_FMT_BGR24,
  335. REC_COMMON_VIDEO_RTP_EWS_WIDTH,
  336. REC_COMMON_VIDEO_RTP_EWS_HEIGHT,
  337. PIX_FMT_BGR24,
  338. SWS_POINT, NULL, NULL, NULL);
  339. video_cap->preview_shm_queue = new Clibvideoqueue(REC_COMMON_VIDEO_EWS_SHM_PREVIEW_QUEUE);
  340. /*video_cap->preview_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_WIDTH,
  341. REC_COMMON_VIDEO_SNAPSHOT_PREVIEW_HEIGHT,
  342. PIX_FMT_BGR24,
  343. REC_COMMON_VIDEO_PREVIEW_WIDTH,
  344. REC_COMMON_VIDEO_PREVIEW_HEIGHT,
  345. PIX_FMT_BGR24,
  346. SWS_FAST_BILINEAR, NULL, NULL, NULL);*/
  347. video_cap->preview_sws_ctx = sws_getContext(REC_COMMON_VIDEO_SNAPSHOT_WIDTH,
  348. REC_COMMON_VIDEO_SNAPSHOT_HEIGHT,
  349. PIX_FMT_BGR24,
  350. REC_COMMON_VIDEO_RTP_EWS_WIDTH,
  351. REC_COMMON_VIDEO_RTP_EWS_HEIGHT,
  352. PIX_FMT_BGR24,
  353. SWS_FAST_BILINEAR, NULL, NULL, NULL);
  354. }
  355. }
  356. return video_cap;
  357. }
  358. static void ews_video_capture_destroy(ews_video_capture_t *video_cap)
  359. {
  360. if (video_cap) {
  361. if (video_cap->preview_sws_ctx) {
  362. sws_freeContext(video_cap->preview_sws_ctx);
  363. video_cap->preview_sws_ctx = NULL;
  364. }
  365. if (video_cap->rtp_sws_ctx) {
  366. sws_freeContext(video_cap->rtp_sws_ctx);
  367. video_cap->rtp_sws_ctx = NULL;
  368. }
  369. if (video_cap->snapshot_shm_queue) {
  370. delete video_cap->snapshot_shm_queue;
  371. video_cap->snapshot_shm_queue = NULL;
  372. }
  373. if (video_cap->rtp_shm_queue) {
  374. delete video_cap->rtp_shm_queue;
  375. video_cap->rtp_shm_queue = NULL;
  376. }
  377. if (video_cap->sales_shm_queue) {
  378. delete video_cap->sales_shm_queue;
  379. video_cap->sales_shm_queue = NULL;
  380. }
  381. if (video_cap->preview_shm_queue) {
  382. delete video_cap->preview_shm_queue;
  383. video_cap->preview_shm_queue = NULL;
  384. }
  385. free(video_cap);
  386. }
  387. }
  388. static int ews_video_capture_start(ews_video_capture_t *video_cap)
  389. {
  390. ews_capture_config_t *conf = &video_cap->parent->config;
  391. int dev_id;
  392. if (video_cap->camera_type == CAMERA_TYPE_EWS)
  393. {
  394. CSimpleStringA tmp;
  395. dev_id = ews_capture_get_video_device_id(conf->strVideo,tmp);
  396. if (dev_id == -1)
  397. {
  398. Dbg("No ews camera,please check config file or device!");
  399. return -1;
  400. }
  401. }
  402. videocap_param param = {0};
  403. int cap_mode;
  404. int rc = -1;
  405. rc = calc_capture_mode(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, &cap_mode);
  406. if (rc != 0)
  407. {
  408. Dbg("calc cap_mode failed!");
  409. return rc;
  410. }
  411. param.cap_mode = cap_mode;
  412. param.dev_id = dev_id;
  413. param.frame_fmt = VIDEO_FORMAT_RGB24;
  414. if ((ePadtype == g_eDeviceType)||(eDesk2SType == g_eDeviceType))
  415. {
  416. param.fps = REC_COMMON_VIDEO_FPS_MOBILE;
  417. }
  418. else
  419. {
  420. param.fps = REC_COMMON_VIDEO_RAW_FPS;
  421. }
  422. param.on_frame = &ews_cap_on_frame;
  423. param.user_data = video_cap;
  424. param.option = 0;
  425. rc = videocap_create(&video_cap->cap, &param);
  426. if (rc != 0)
  427. {
  428. Dbg("videocap create failed!");
  429. return rc;
  430. }
  431. rc = videocap_start(video_cap->cap);
  432. if (rc != 0)
  433. {
  434. Dbg("videocap start failed!");
  435. videocap_destroy(video_cap->cap);
  436. video_cap->cap = NULL;
  437. return rc;
  438. }
  439. return 0;
  440. }
  441. static void ews_video_capture_stop(ews_video_capture_t *video_cap)
  442. {
  443. if (video_cap->cap) {
  444. videocap_stop(video_cap->cap);
  445. videocap_destroy(video_cap->cap);
  446. video_cap->cap = NULL;
  447. }
  448. }
  449. int ews_capture_create( const ews_capture_config_t *config, ews_capture_t **p_cap )
  450. {
  451. ews_capture_t *cap = ZALLOC_T(ews_capture_t);
  452. cap->audio = NULL;
  453. cap->video = NULL;
  454. memcpy(&cap->config, config, sizeof(ews_capture_config_t));
  455. cap->audio = ews_audio_capture_create(cap);
  456. if (!cap->audio) {
  457. Dbg("create audio capture object failed!");
  458. return Error_Unexpect;
  459. }
  460. CSimpleStringA tmp;
  461. int dev_id = ews_capture_get_video_device_id(config->strVideo,tmp);
  462. if (dev_id != -1)
  463. {
  464. cap->video = ews_video_capture_create(cap, CAMERA_TYPE_EWS);
  465. if (!cap->video) {
  466. Dbg("create ews video object failed!");
  467. return Error_Unexpect;
  468. }
  469. }
  470. if(dev_id == -1)
  471. {
  472. Dbg("ews camera device id error!");
  473. ews_capture_destroy(cap);
  474. return Error_Unexpect;
  475. }
  476. else
  477. {
  478. *p_cap = cap;
  479. Dbg("create surveillance record audio capture object(%0x) success, capture sample rate is %d, and audio in device name is %s.",
  480. cap, cap->config.iaudiosamplerate, cap->config.strAudioIn.GetData());
  481. return 0;
  482. }
  483. }
  484. // 重启摄像头用到
  485. ErrorCodeEnum ews_capture_create( const ews_capture_config_t *config,ews_capture_t *cap )
  486. {
  487. CSimpleStringA tmp;
  488. int dev_id = ews_capture_get_video_device_id(config->strVideo,tmp);
  489. if((dev_id != -1)&&(cap->video == NULL))
  490. {
  491. cap->video = ews_video_capture_create(cap, CAMERA_TYPE_EWS);
  492. if (!cap->video)
  493. {
  494. Dbg("create ews video object failed!");
  495. return Error_Unexpect;
  496. }
  497. }
  498. else
  499. {
  500. return Error_Unexpect;
  501. }
  502. return Error_Succeed;
  503. }
  504. void ews_capture_destroy( ews_capture_t *cap )
  505. {
  506. if (cap) {
  507. if (cap->video) {
  508. ews_video_capture_destroy(cap->video);
  509. cap->video = NULL;
  510. }
  511. if (cap->audio) {
  512. ews_audio_capture_destroy(cap->audio);
  513. cap->audio = NULL;
  514. }
  515. free(cap);
  516. }
  517. }
  518. ErrorCodeEnum ews_capture_start( ews_capture_t *cap )
  519. {
  520. int rc = 0;
  521. if (cap->audio)
  522. {
  523. rc = ews_audio_capture_start(cap->audio);
  524. if (rc != Error_Succeed)
  525. {
  526. ErrorCodeEnum rslt = (ErrorCodeEnum)rc;
  527. if (rslt == Error_AudioIN)
  528. {
  529. Dbg("start audio In object failed! rc:%d", rc);
  530. //LogWarn(Severity_High,Error_NotInit,ERROR_MOD_MEDIACONTROLLER_HANDFREE_OPENFAIL,"open audio device fail,please check device");
  531. }
  532. return rslt;
  533. }
  534. }
  535. else
  536. {
  537. Dbg("start ews audio Error_Unexpect");
  538. return Error_Unexpect;
  539. }
  540. if (cap->video)
  541. {
  542. rc = ews_video_capture_start(cap->video);
  543. if (rc != Error_Succeed)
  544. {
  545. Dbg("start ews video capture object failed! rc:%d", rc);
  546. CSimpleStringA strCamFriendlyName;
  547. ews_capture_get_video_device_id(cap->config.strVideo.GetData(),strCamFriendlyName);
  548. char strMessage[MAX_PATH*2] = {0};
  549. get_external_camera_exception_message(strMessage, MAX_PATH*2, strCamFriendlyName, "open ews camera fail,please check device");
  550. LogError(Severity_Middle,Error_NotInit,ERROR_MOD_SURVEILLANCERECORDER_EWSCAM_OPEN,strMessage);
  551. return Error_EwsCamera;
  552. }
  553. }
  554. else
  555. {
  556. Dbg("start ews video Error_Unexpect");
  557. return Error_Unexpect;
  558. }
  559. return (ErrorCodeEnum)rc;
  560. }
  561. void ews_capture_stop( ews_capture_t *cap )
  562. {
  563. if (cap->audio) {
  564. ews_audio_capture_stop(cap->audio);
  565. }
  566. if (cap->video) {
  567. ews_video_capture_stop(cap->video);
  568. }
  569. }
  570. int ews_capture_detect_camera_bug( ews_capture_t *cap, int *ews_n )
  571. {
  572. *ews_n = 0;
  573. if (cap->video)
  574. {
  575. if (cap->video->rtp_shm_queue)
  576. {
  577. *ews_n = cap->video->rtp_shm_queue->GetVideoLens();
  578. }
  579. }
  580. else
  581. {
  582. *ews_n = -1;
  583. }
  584. return 0;
  585. }
  586. int ews_capture_get_last_frametime( ews_capture_t *cap, DWORD *ews_n )
  587. {
  588. *ews_n = 0;
  589. if (cap->video)
  590. {
  591. if (cap->video->rtp_shm_queue)
  592. {
  593. *ews_n = cap->video->rtp_shm_queue->GetLastFrameTime();
  594. }
  595. }
  596. else
  597. {
  598. *ews_n = 0;
  599. }
  600. return 0;
  601. }
  602. static int ews_audio_get_dev_count(int *in_cnt, int *out_cnt)
  603. {
  604. int icnt = 0, ocnt = 0;
  605. int cnt = Pa_GetDeviceCount();
  606. for (int i = 0; i < cnt; ++i) {
  607. const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
  608. if (info->maxInputChannels)
  609. icnt ++;
  610. if (info->maxOutputChannels)
  611. ocnt ++;
  612. }
  613. if (in_cnt)
  614. *in_cnt = icnt;
  615. if (out_cnt)
  616. *out_cnt = ocnt;
  617. return 0;
  618. }
  619. static CSimpleStringA ews_audio_get_dev_name(bool in_direction, int idx)
  620. {
  621. int cnt = Pa_GetDeviceCount();
  622. int ii, i;
  623. for (i = 0, ii = 0; i < cnt; ++i) {
  624. const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
  625. if (in_direction) {
  626. if (info->maxInputChannels) {
  627. if (idx == ii) {
  628. return CSimpleStringA(info->name);
  629. }
  630. ii++;
  631. }
  632. } else {
  633. if (info->maxOutputChannels) {
  634. if (idx == ii) {
  635. return CSimpleStringA(info->name);
  636. }
  637. ii++;
  638. }
  639. }
  640. }
  641. return CSimpleStringA();
  642. }
  643. int ews_capture_lib_init()
  644. {
  645. HRESULT hr = CoInitialize(NULL);
  646. int rc;
  647. {
  648. HMODULE hModule = GetModuleHandleA("MSVCR100.dll");
  649. if (hModule) {
  650. typedef char *(*f_setlocale)(int, const char*);
  651. f_setlocale f = (f_setlocale)GetProcAddress(hModule, "setlocale");
  652. (*f)(LC_ALL, "chs");
  653. }
  654. }
  655. if (SUCCEEDED(hr)) {
  656. PaError Error;
  657. Error = Pa_Initialize();
  658. if (Error == paNoError) {
  659. rc = videoframework_init();
  660. if (rc != 0) {
  661. Dbg("videoframework_init failed, rc=%d", rc);
  662. return Error_Resource;
  663. }
  664. } else {
  665. Dbg("PaInitialize failed, rc=%d", Error);
  666. return Error_Resource;
  667. }
  668. } else {
  669. Dbg("coinitialze failed! hr:%d", hr);
  670. return Error_Resource;
  671. }
  672. {
  673. int i, n;
  674. n = videocap_get_device_count();
  675. for (i = 0; i < n; ++i) {
  676. WCHAR tmp[256];
  677. char t[256];
  678. WCHAR tmp1[256];
  679. char t1[256];
  680. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  681. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  682. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  683. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  684. {
  685. unsigned char x[MD5_DIGESTSIZE];
  686. md5_ctx_t ctx;
  687. md5_init(&ctx);
  688. md5(x, t1, strlen(t1));
  689. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  690. }
  691. Dbg("%d = %s;%s", i, t, t1);
  692. }
  693. }
  694. {
  695. int icnt, ocnt;
  696. rc = ews_audio_get_dev_count(&icnt, &ocnt);
  697. if (rc == 0) {
  698. int i;
  699. Dbg("audio input devices(%d):", icnt);
  700. for (i = 0; i < icnt; ++i) {
  701. CSimpleStringA str = ews_audio_get_dev_name(true, i);
  702. Dbg("%d = %s", i, (LPCSTR)str);
  703. }
  704. Dbg("audio output devices(%d):", ocnt);
  705. for (i = 0; i < ocnt; ++i) {
  706. CSimpleStringA str = ews_audio_get_dev_name(false, i);
  707. Dbg("%d = %s", i, (LPCSTR)str);
  708. }
  709. }
  710. }
  711. return Error_Succeed;
  712. }
  713. void ews_capture_lib_term()
  714. {
  715. videoframework_term();
  716. Pa_Terminate();
  717. CoUninitialize();
  718. }
  719. int ews_capture_get_audio_device_id( bool in_direction, const char *dev_name )
  720. {
  721. int cnt = Pa_GetDeviceCount();
  722. int ii, i;
  723. for (i = 0, ii = 0; i < cnt; ++i) {
  724. const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
  725. if (in_direction) {
  726. if (info->maxInputChannels) {
  727. if (strstr(info->name, dev_name) != NULL) {
  728. return ii;
  729. }
  730. ii++;
  731. }
  732. } else {
  733. if (info->maxOutputChannels) {
  734. if (strstr(info->name, dev_name) != NULL) {
  735. return ii;
  736. }
  737. ii++;
  738. }
  739. }
  740. }
  741. return -1;
  742. }
  743. //int ews_capture_get_video_device_id( const char *dev_name )
  744. //{
  745. // int i, n;
  746. //
  747. // n = videocap_get_device_count();
  748. // for (i = 0; i < n; ++i) {
  749. // WCHAR tmp[256];
  750. // char t[256];
  751. // WCHAR tmp1[256];
  752. // char t1[256];
  753. // videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  754. // WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  755. // videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  756. // WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  757. // {
  758. // unsigned char x[MD5_DIGESTSIZE];
  759. // md5_ctx_t ctx;
  760. // md5_init(&ctx);
  761. // md5(x, t1, strlen(t1));
  762. // Bin2Str(x, sizeof(x), t1, sizeof(t1));
  763. // }
  764. // strcat(t, ";");
  765. // strcat(t, t1);
  766. // if (strcmp(dev_name, t) == 0)
  767. // return i;
  768. // }
  769. // return -1; // not found
  770. //}
  771. int ews_capture_get_video_device_id(const char *dev_inst_path, CSimpleStringA &ews_name)
  772. {
  773. int i, n;
  774. n = videocap_get_device_count();
  775. for (i = 0; i < n; ++i) {
  776. WCHAR tmp1[256];
  777. char t1[256];
  778. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  779. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  780. // save DevicePath
  781. for (int j = 0; j < strlen(t1); ++j)
  782. {
  783. t1[j] = toupper(t1[j]);
  784. if (t1[j] == '#') t1[j] = '\\';
  785. }
  786. if (strstr(t1,dev_inst_path) != NULL)
  787. {
  788. WCHAR tmp[256]={0};
  789. char t[256]={0};
  790. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  791. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  792. ews_name = t;
  793. Dbg("matched ews camera device: [%d] %s.", i, t1);
  794. return i;
  795. }
  796. else{
  797. WCHAR tmp[256] = {0};
  798. char t[256] = {0};
  799. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  800. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  801. Dbg("device list: [%d] %s.", i, t1);
  802. }
  803. }
  804. return -1; // not found
  805. }
  806. bool ews_capture_check_video_device_match( const char *dev_name, const char*dev_inst_path )
  807. {
  808. int i, n;
  809. n = videocap_get_device_count();
  810. for (i = 0; i < n; ++i) {
  811. WCHAR tmp[256];
  812. char t[256];
  813. WCHAR tmp1[256];
  814. char t1[256];
  815. videocap_get_device_name(i, tmp, ARRAYSIZE(tmp));
  816. WideCharToMultiByte(CP_ACP, 0, tmp, -1, t, sizeof(t), 0, NULL);
  817. videocap_get_device_path(i, tmp1, ARRAYSIZE(tmp1));
  818. WideCharToMultiByte(CP_ACP, 0, tmp1, -1, t1, sizeof(t1), 0, NULL);
  819. // save DevicePath (add by ly at 20160725)
  820. char t2[256];
  821. strcpy(t2,t1);
  822. for (int j = 0; j < strlen(t2); ++j)
  823. {
  824. t2[j] = toupper(t2[j]);
  825. if (t2[j] == '#') t2[j] = '\\';
  826. }
  827. {
  828. unsigned char x[MD5_DIGESTSIZE];
  829. md5_ctx_t ctx;
  830. md5_init(&ctx);
  831. md5(x, t1, strlen(t1));
  832. Bin2Str(x, sizeof(x), t1, sizeof(t1));
  833. }
  834. strcat(t, ";");
  835. strcat(t, t1);
  836. if (strstr(t2,dev_inst_path) != NULL)
  837. {
  838. Dbg("[dbg] %s founded in %d cameras.", dev_inst_path, n);
  839. if (strcmp(dev_name, t) == 0)
  840. return true;
  841. }
  842. }
  843. return false; // not match
  844. }
  845. bool ews_capture_adj_brightness( ews_capture_t *cap,int nvalue,ErrorCodeEnum nCode )
  846. {
  847. HRESULT rst = S_OK;
  848. if (cap->video&&(nCode!=Error_EwsCamera)&&(nCode!=Error_AllCamera))
  849. {
  850. rst = videocap_adj_brightness(cap->video->cap,nvalue);
  851. }
  852. if (SUCCEEDED(rst))
  853. return true;
  854. return false;
  855. }
  856. bool ews_capture_set_autobrightness( ews_capture_t *cap,ErrorCodeEnum nCode )
  857. {
  858. HRESULT rst = S_OK;
  859. if (cap->video&&(nCode!=Error_EwsCamera)&&(nCode!=Error_AllCamera))
  860. {
  861. rst = videocap_set_autobrightness(cap->video->cap);
  862. }
  863. if (SUCCEEDED(rst))
  864. return true;
  865. return false;
  866. }
  867. int ews_capture_get_brightness( ews_capture_t *cap,ErrorCodeEnum nCode )
  868. {
  869. int nValue=0;
  870. HRESULT rst = S_OK;
  871. if (cap->video&&(nCode!=Error_EwsCamera)&&(nCode!=Error_AllCamera))
  872. {
  873. HRESULT rst = videocap_get_brightness(cap->video->cap,&nValue);
  874. }
  875. else
  876. {
  877. return -1;
  878. }
  879. if (SUCCEEDED(rst))
  880. {
  881. return nValue;
  882. }
  883. else
  884. {
  885. return -1;
  886. }
  887. }
  888. int ews_stop_camera( ews_capture_t *cap )
  889. {
  890. if (cap->video)
  891. {
  892. ews_video_capture_stop(cap->video);
  893. ews_video_capture_destroy(cap->video);
  894. cap->video = NULL;
  895. return 0;
  896. }
  897. else
  898. {
  899. return -1;
  900. }
  901. }
  902. int get_external_camera_exception_message(char* pBuffer, size_t uLen, CSimpleStringA strDeviceName, const char* strErrorMessage)
  903. {
  904. int iRet = 0;
  905. if (strDeviceName.GetLength() > 0){
  906. const char* strCameraName = strDeviceName.GetData();
  907. char strBuffer[MAX_PATH] = {0};
  908. if (sprintf_s(strBuffer, MAX_PATH, "%s", strCameraName) > 0){
  909. char *pIndex = NULL;
  910. if (pIndex = (char*)strstr(strBuffer, ";")){
  911. *pIndex = '\0';
  912. }
  913. }
  914. if (NULL != strErrorMessage){
  915. size_t uDataLen = strlen(strBuffer);
  916. size_t uErrorLen = strlen(strErrorMessage);
  917. if (uLen > uDataLen + uErrorLen + 10){
  918. iRet = sprintf_s(pBuffer, uLen, "[%s] %s", strBuffer, strErrorMessage);
  919. }
  920. }
  921. }
  922. if (0 == iRet){
  923. if (NULL != strErrorMessage){
  924. iRet = sprintf_s(pBuffer, uLen, "%s", strErrorMessage);
  925. }
  926. }
  927. return iRet;
  928. }