You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

529 lines
20 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #pragma once
  2. #include <fstream>
  3. #include <functional>
  4. #include <iostream>
  5. #include <list>
  6. #include <map>
  7. #include <memory>
  8. #include <mutex>
  9. #include <set>
  10. #include <sstream>
  11. #include <string>
  12. #include <vector>
  13. //
  14. #include "i_xsync_udp.hpp"
  15. #include "xsync_errcode.hpp"
  16. #include "xsync_packet.hpp"
  17. #include "xsync_regs.hpp"
  18. namespace xsync {
  19. using namespace std;
  20. typedef enum {
  21. kxsync_net_state_disconnect,
  22. kxsync_net_state_connecting,
  23. kxsync_net_state_connected,
  24. } xsync_net_state_t;
  25. typedef enum {
  26. TIMECODE_FPS2398 = 0,
  27. TIMECODE_FPS2400 = 1,
  28. TIMECODE_FPS2500 = 2,
  29. TIMECODE_FPS2997 = 3,
  30. TIMECODE_FPS2997Drop = 4,
  31. TIMECODE_FPS3000 = 5,
  32. } TimecodeFormat_t;
  33. typedef enum {
  34. GENLOCK_FPS2397 = 0,
  35. GENLOCK_FPS2398 = 1,
  36. GENLOCK_FPS2400 = 2,
  37. GENLOCK_FPS2500 = 3,
  38. GENLOCK_FPS2997 = 4,
  39. GENLOCK_FPS3000 = 5,
  40. GENLOCK_FPS5000 = 6,
  41. GENLOCK_FPS5994 = 7,
  42. GENLOCK_FPS6000 = 8,
  43. } GenlockFormat_t;
  44. string GenlockFormatToStr(GenlockFormat_t fomrat);
  45. string TimecodeFormatToStr(TimecodeFormat_t fomrat);
  46. GenlockFormat_t Str2GenlockFormat(string format);
  47. TimecodeFormat_t Str2TimecodeFormat(string format);
  48. list<string> GenlockFormatStrSet();
  49. list<string> TimecodeFormatStrSet();
  50. typedef struct {
  51. uint8_t hour;
  52. uint8_t minute;
  53. uint8_t second;
  54. uint8_t frame;
  55. } XsyncTimecode_t;
  56. string XsyncTimecodeToStr(XsyncTimecode_t timecode);
  57. XsyncTimecode_t Str2XsyncTimecode(string timecode);
  58. typedef struct {
  59. uint32_t tc0;
  60. uint32_t tc1;
  61. } Timecode64_t;
  62. typedef struct {
  63. uint32_t frameIndex;
  64. } xysnc_camera_sync_data_t;
  65. typedef function<void(XsyncTimecode_t *timecode_msg)> xsync_on_timecode_msg_t;
  66. typedef function<void(uint32_t workstate)> xsync_on_workstate_change_msg_t;
  67. typedef function<void(xysnc_camera_sync_data_t *timecode_msg)> xsync_on_camera_sync_msg_t;
  68. /*******************************************************************************
  69. * TTL输出模块相关枚举 *
  70. *******************************************************************************/
  71. namespace ttlout_module {
  72. typedef enum {
  73. tri_logic0 = 0, // 逻辑0
  74. tri_logic1 = 1, // 逻辑1
  75. tri_ttlin1_module_ext = 2, // ttl1输入模块原始信号
  76. tri_ttlin1_module_divide = 3, // ttl1输入模块分频信号
  77. tri_ttlin2_module_ext = 4, // ttl2输入模块原始信号
  78. tri_ttlin2_module_divide = 5, // ttl2输入模块分频信号
  79. tri_ttlin3_module_ext = 6, // ttl3输入模块原始信号
  80. tri_ttlin3_module_divide = 7, // ttl3输入模块分频信号
  81. tri_ttlin4_module_ext = 8, // ttl4输入模块原始信号
  82. tri_ttlin4_module_divide = 9, // ttl4输入模块分频信号
  83. tri_recorded_sig = 10, // 录制信号
  84. tri_genlock_frame_sync_ext = 11, // 外部genlock帧同步信号
  85. tri_genlock_frame_sync_internal = 12, // 内部genlock帧同步信号
  86. tri_timecode_frame_sync_ext = 13, // 外部timecode帧同步信号
  87. tri_timecode_frame_sync_internal = 14, // 内部timecode帧同步信号
  88. tri_timecode_serial_data_ext = 15, // 外部timecode串行数据输入
  89. tri_timecode_serial_data_internal = 16, // 内部timecode串行数据输入
  90. tri_internal_clk_1hz = 17, // 内部
  91. tri_internal_100hz = 31 // 100hz测试信号
  92. } TriggerSigType_t;
  93. string TriggerSigType2Str(TriggerSigType_t type);
  94. TriggerSigType_t Str2TriggerSigType(string type);
  95. list<string> TriggerSigTypeStrSet();
  96. typedef enum {
  97. OutSigType_logic0 = 0, // 0
  98. OutSigType_logic1 = 1, // 1
  99. OutSigType_test_signal = 2, // 测试信号,信号为ID*1000HZ方波信号
  100. OutSigType_input_signal = 3, // 输入信号
  101. OutSigType_input_signal_mirror = 4, // 翻转后的输入信号
  102. OutSigType_trigger_mode_signal = 5, // 触发模式下的触发信号
  103. OutSigType_trigger_mode_signal_mirror = 6, // 触发模式下的触发信号翻转
  104. } OutputSigType_t;
  105. string OutputSigType2Str(OutputSigType_t type);
  106. OutputSigType_t Str2OutputSigType(string type);
  107. list<string> OutputSigTypeStrSet();
  108. } // namespace ttlout_module
  109. /*******************************************************************************
  110. * *
  111. *******************************************************************************/
  112. namespace sig_generator_module {
  113. typedef enum {
  114. kControlMode_manualTrigger, // 0.手动,启动停止
  115. kControlMode_externalTimecodeTrigger, // 1.外部TIMECODE触发启动
  116. kControlMode_externalTTL1Trigger, // 2.外部TTL输入1_高电平触发,低电平停止
  117. kControlMode_externalTTL2Trigger, // 3.外部TTL输入2_高电平触发,低电平停止
  118. kControlMode_externalTTL3Trigger, // 4.外部TTL输入3_高电平触发,低电平停止
  119. kControlMode_externalTTL4Trigger, // 5.外部TTL输入4_高电平触发,低电平停止
  120. } ControlMode_t;
  121. string ControlMode2Str(ControlMode_t mode);
  122. ControlMode_t Str2ControlMode(string mode);
  123. list<string> ControlModeStrSet();
  124. } // namespace sig_generator_module
  125. /*******************************************************************************
  126. * *
  127. *******************************************************************************/
  128. namespace timecode_output_module {
  129. typedef enum {
  130. koff = 0, // 关闭输出
  131. kinternal_timecode_sig = 1, // 外部timecode信号
  132. kext_timecode_sig = 2, // 内部timecode信号
  133. } TriggerSigType_t;
  134. typedef enum {
  135. kline,
  136. kmic,
  137. } OutputSigLevelType_t;
  138. string TriggerSigType2Str(TriggerSigType_t type);
  139. TriggerSigType_t Str2TriggerSigType(string type);
  140. list<string> TriggerSigTypeStrSet();
  141. string OutputSigLevelType2Str(OutputSigLevelType_t type);
  142. OutputSigLevelType_t Str2OutputSigLevelType(string type);
  143. list<string> OutputSigLevelTypeStrSet();
  144. } // namespace timecode_output_module
  145. /*******************************************************************************
  146. * *
  147. *******************************************************************************/
  148. namespace camera_sync_packet_generator_module {
  149. typedef enum {
  150. koff = 0, // 关闭输出
  151. kinternal_genlock_sig = 1, // 外部genlock信号
  152. kext_genlock_sig = 2, // 内部genlock信号
  153. } TriggerSigType_t;
  154. string TriggerSigType2Str(TriggerSigType_t type);
  155. TriggerSigType_t Str2TriggerSigType(string type);
  156. list<string> TriggerSigTypeStrSet();
  157. } // namespace camera_sync_packet_generator_module
  158. /*******************************************************************************
  159. * *
  160. *******************************************************************************/
  161. namespace timecode_input_module {
  162. typedef enum {
  163. koff = 0,
  164. kbnc_timecode,
  165. kheadphone_timecode,
  166. } TriggerSigType_t;
  167. string TriggerSigType2Str(TriggerSigType_t type);
  168. TriggerSigType_t Str2TriggerSigType(string type);
  169. list<string> TriggerSigTypeStrSet();
  170. } // namespace timecode_input_module
  171. class Xsync {
  172. public:
  173. private:
  174. /* data */
  175. I_XSUDPFactory *m_xsync_udp_factory = nullptr;
  176. shared_ptr<I_XSUDP> m_xsync_reg_udp = nullptr;
  177. shared_ptr<I_XSUDP> m_xsync_timecode_udp_listener = nullptr;
  178. shared_ptr<I_XSUDP> m_xsync_camera_sync_udp_listener = nullptr;
  179. string m_xsync_ip;
  180. bool m_is_connected = false;
  181. xsync_on_camera_sync_msg_t m_on_camera_sync_msg_cb = nullptr;
  182. xsync_on_timecode_msg_t m_on_timecode_msg_cb = nullptr;
  183. xsync_on_workstate_change_msg_t m_on_workstate_change_msg_cb = nullptr;
  184. int txpacket_index = 0;
  185. uint8_t m_xync_cmd_rxdata_cache[2560];
  186. xsync_net_state_t m_net_state = kxsync_net_state_disconnect;
  187. std::recursive_mutex lock_;
  188. Xsync(/* args */);
  189. public:
  190. static Xsync &Ins();
  191. void initialize(I_XSUDPFactory *xsync_udp_factory);
  192. /*******************************************************************************
  193. * *
  194. *******************************************************************************/
  195. bool ping(string xsync_ip);
  196. xs_error_code_t connect(string xsync_ip);
  197. xs_error_code_t disConnect();
  198. xsync_net_state_t getNetState();
  199. /*******************************************************************************
  200. * *
  201. *******************************************************************************/
  202. void Basic_registerOnTimecodeMsgCallback(xsync_on_timecode_msg_t on_timecode_msg_cb);
  203. void Basic_registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t on_camera_sync_msg_cb);
  204. void Basic_registerOnWorkstateChangeMsgCallback(xsync_on_workstate_change_msg_t on_workstate_change_msg_cb);
  205. /*******************************************************************************
  206. * *
  207. *******************************************************************************/
  208. xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &regbackvalue, int32_t overtime_ms = 100);
  209. xs_error_code_t reg_read(uint32_t regadd, uint32_t &regvalue, int32_t overtime_ms = 100);
  210. xs_error_code_t reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues, int32_t overtime_ms = 100);
  211. /*******************************************************************************
  212. * *
  213. *******************************************************************************/
  214. xs_error_code_t Basic_generatorNewMac();
  215. xs_error_code_t Basic_factoryReset();
  216. xs_error_code_t Basic_reboot();
  217. xs_error_code_t Basic_changeNetworkConfig(string ip, string mask, string gateway);
  218. xs_error_code_t Basic_clearXsyncCameraSyncIndexCount();
  219. xs_error_code_t Basic_setGenlockFormat(GenlockFormat_t format);
  220. xs_error_code_t Basic_getGenlockFormat(GenlockFormat_t &format);
  221. xs_error_code_t Basic_setTimecodeFormat(TimecodeFormat_t format);
  222. // xs_error_code_t Basic_getTimecodeFormat(TimecodeFormat_t &format);
  223. /*******************************************************************************
  224. * *
  225. *******************************************************************************/
  226. xs_error_code_t TimecodeInputModule_setSrcSelect(timecode_input_module::TriggerSigType_t timecode_select);
  227. xs_error_code_t TimecodeInputModule_getSrcSelect(timecode_input_module::TriggerSigType_t &timecode_select);
  228. xs_error_code_t TimecodeInputModule_setTimecodeFormat(TimecodeFormat_t format);
  229. xs_error_code_t TimecodeInputModule_getTimecodeFormat(TimecodeFormat_t &format);
  230. xs_error_code_t TimecodeInputModule_getTimecode(XsyncTimecode_t &timecode);
  231. /*******************************************************************************
  232. * TTL输入模块 *
  233. *******************************************************************************/
  234. xs_error_code_t TTLInputModule_setEn(int32_t index, bool en);
  235. xs_error_code_t TTLInputModule_getEn(int32_t index, bool &en);
  236. xs_error_code_t TTLInputModule_setFilterFactor(int32_t index, uint32_t factor);
  237. xs_error_code_t TTLInputModule_getFilterFactor(int32_t index, uint32_t &factor);
  238. xs_error_code_t TTLInputModule_setDivideFactor(int32_t index, uint32_t factor);
  239. xs_error_code_t TTLInputModule_getDivideFactor(int32_t index, uint32_t &factor);
  240. /*******************************************************************************
  241. * *
  242. *******************************************************************************/
  243. /**
  244. * :
  245. * 0.
  246. * 1. TIMECODE触发模式下
  247. */
  248. /**
  249. * @brief
  250. *
  251. * @param mode
  252. * @return xs_error_code_t
  253. */
  254. xs_error_code_t SigGenerator_setControlMode(sig_generator_module::ControlMode_t mode);
  255. /**
  256. * @brief
  257. *
  258. * @param mode
  259. * @return xs_error_code_t
  260. */
  261. xs_error_code_t SigGenerator_getControlMode(sig_generator_module::ControlMode_t &mode);
  262. /**
  263. * @brief
  264. *
  265. * @return xs_error_code_t
  266. */
  267. xs_error_code_t SigGenerator_manualStart();
  268. /**
  269. * @brief
  270. *
  271. * @return xs_error_code_t
  272. */
  273. xs_error_code_t SigGenerator_manualStop();
  274. /**
  275. * @brief
  276. *
  277. * @param timecode
  278. * @return xs_error_code_t
  279. */
  280. xs_error_code_t SigGenerator_setTimecode(XsyncTimecode_t timecode);
  281. /**
  282. * @brief
  283. *
  284. * @param timecode
  285. * @return xs_error_code_t
  286. */
  287. xs_error_code_t SigGenerator_getTimecode(XsyncTimecode_t &timecode);
  288. /**
  289. * @brief
  290. *
  291. * @param timecode
  292. * @return xs_error_code_t
  293. */
  294. xs_error_code_t SigGenerator_setAutoStartTimecode(XsyncTimecode_t timecode);
  295. /**
  296. * @brief
  297. *
  298. * @param timecode
  299. * @return xs_error_code_t
  300. */
  301. xs_error_code_t SigGenerator_getAutoStartTimecode(XsyncTimecode_t &timecode);
  302. /**
  303. * @brief
  304. *
  305. * @param work_state
  306. * @return xs_error_code_t
  307. */
  308. xs_error_code_t SigGenerator_getWorkState(uint32_t &work_state);
  309. /**
  310. * @brief genlock格式
  311. *
  312. * @param format
  313. * @return xs_error_code_t
  314. */
  315. xs_error_code_t SigGenerator_setGenlockFormat(GenlockFormat_t format); // 不建议使用这个方法,用Basic_setGenlockFormat
  316. xs_error_code_t SigGenerator_getGenlockFormat(GenlockFormat_t &format); // 不建议使用这个方法,用Basic_getGenlockFormat
  317. /**
  318. * @brief timecode格式
  319. *
  320. * @param format
  321. * @return xs_error_code_t
  322. */
  323. xs_error_code_t SigGenerator_setTimecodeFormat(TimecodeFormat_t format); // 不建议使用这个方法,用Basic_setTimecodeFormat
  324. xs_error_code_t SigGenerator_getTimecodeFormat(TimecodeFormat_t &format); // 不建议使用这个方法,用Basic_getTimecodeFormat
  325. public:
  326. /*******************************************************************************
  327. * *
  328. *******************************************************************************/
  329. /**
  330. * @brief
  331. *
  332. * @param timecode_select
  333. * @return xs_error_code_t
  334. */
  335. xs_error_code_t TimecodeOutputModule_setTimecodeSrcSelect(timecode_output_module::TriggerSigType_t timecode_select);
  336. xs_error_code_t TimecodeOutputModule_getTimecodeSrcSelect(timecode_output_module::TriggerSigType_t &timecode_select);
  337. /**
  338. * @brief
  339. *
  340. * @param timecode_now
  341. * @return xs_error_code_t
  342. */
  343. xs_error_code_t TimecodeOutputModule_getTimecodeNow(XsyncTimecode_t &timecode_now);
  344. /**
  345. * @brief
  346. *
  347. * @param timecode_format
  348. * @return xs_error_code_t
  349. */
  350. xs_error_code_t TimecodeOutputModule_getTimecodeFormat(TimecodeFormat_t &timecode_format);
  351. /**
  352. * @brief BNC输出电平
  353. *
  354. * @param level
  355. * @return xs_error_code_t
  356. */
  357. xs_error_code_t TimecodeOutputModule_setBncOutputLevel(timecode_output_module::OutputSigLevelType_t &level);
  358. xs_error_code_t TimecodeOutputModule_getBncOutputLevel(timecode_output_module::OutputSigLevelType_t &level);
  359. /**
  360. * @brief
  361. *
  362. * @param level
  363. * @return xs_error_code_t
  364. */
  365. xs_error_code_t TimecodeOutputModule_setHeadphoneOutputLevel(timecode_output_module::OutputSigLevelType_t &level);
  366. xs_error_code_t TimecodeOutputModule_getHeadphoneOutputLevel(timecode_output_module::OutputSigLevelType_t &level);
  367. public:
  368. /*******************************************************************************
  369. * *
  370. *******************************************************************************/
  371. /**
  372. * @brief
  373. *
  374. * @param sig
  375. * @return xs_error_code_t
  376. */
  377. xs_error_code_t CameraSyncPacketGeneratorModule_setTriggerSig(camera_sync_packet_generator_module::TriggerSigType_t sig);
  378. xs_error_code_t CameraSyncPacketGeneratorModule_getTriggerSig(camera_sync_packet_generator_module::TriggerSigType_t &sig);
  379. /**
  380. * @brief
  381. *
  382. * @param packetNum :30 30
  383. * @return xs_error_code_t
  384. */
  385. xs_error_code_t CameraSyncPacketGeneratorModule_setReportPeriod(uint32_t packetNum);
  386. xs_error_code_t CameraSyncPacketGeneratorModule_getReportPeriod(uint32_t &packetNum);
  387. /**
  388. * @brief
  389. *
  390. * @param index
  391. * @return xs_error_code_t
  392. */
  393. xs_error_code_t CameraSyncPacketGeneratorModule_getPacketIndex(uint32_t &index);
  394. /**
  395. * @brief
  396. *
  397. * @return xs_error_code_t
  398. */
  399. xs_error_code_t CameraSyncPacketGeneratorModule_clearPacketIndex();
  400. public:
  401. /*******************************************************************************
  402. * TTL输出模块控制 *
  403. *******************************************************************************/
  404. /**
  405. * :
  406. * 1.
  407. * 2.
  408. * 3.
  409. * 4.
  410. */
  411. /**
  412. * @brief TTL输出模块的输入信号
  413. *
  414. * @param index
  415. * @param source
  416. * @return xs_error_code_t
  417. */
  418. xs_error_code_t TTLOutputModule_setInputSigType(int32_t index, ttlout_module::TriggerSigType_t source);
  419. xs_error_code_t TTLOutputModule_getInputSigType(int32_t index, ttlout_module::TriggerSigType_t &source);
  420. /**
  421. * @brief TTL输出模块的输出信号
  422. *
  423. * @param index
  424. * @param output_type
  425. * @return xs_error_code_t
  426. */
  427. xs_error_code_t TTLOutputModule_setOutputSigType(int32_t index, ttlout_module::OutputSigType_t output_type);
  428. xs_error_code_t TTLOutputModule_getOutputSigType(int32_t index, ttlout_module::OutputSigType_t &output_type);
  429. /**
  430. * @brief
  431. *
  432. * @param index
  433. * @param pulse_width_ms
  434. * @return xs_error_code_t
  435. */
  436. xs_error_code_t TTLOutputModule_setTriggerModePulseWidth(int32_t index, uint32_t pulse_width_ms);
  437. xs_error_code_t TTLOutputModule_getTriggerModePulseWidth(int32_t index, uint32_t &pulse_width_ms);
  438. /**
  439. * @brief
  440. *
  441. * @param index
  442. * @param pulse_delay_ms
  443. * @return xs_error_code_t
  444. */
  445. xs_error_code_t TTLOutputModule_setTriggerModePulseDelay(int32_t index, uint32_t pulse_delay_ms);
  446. xs_error_code_t TTLOutputModule_getTriggerModePulseDelay(int32_t index, uint32_t &pulse_delay_ms);
  447. private:
  448. xs_error_code_t doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms = 100);
  449. xs_error_code_t storageConfig();
  450. xs_error_code_t xsync_send_cmd_block(iflytop_xsync_packet_header_t *cmd, iflytop_xsync_packet_header_t *rx_data, int32_t buffersize, int32_t overtime_ms);
  451. void parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
  452. void parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length);
  453. };
  454. } // namespace xsync