UVCManager.cs 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  1. #define ENABLE_LOG
  2. #define DEBUG
  3. /*
  4. * Copyright (c) 2014 - 2022 t_saki@serenegiant.com
  5. */
  6. using AOT;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Runtime.CompilerServices;
  11. using System.Runtime.InteropServices;
  12. using System.Threading;
  13. using UnityEngine;
  14. #if UNITY_ANDROID && UNITY_2018_3_OR_NEWER
  15. using UnityEngine.Android;
  16. #endif
  17. namespace Serenegiant.UVC
  18. {
  19. [RequireComponent(typeof(AndroidUtils))]
  20. public class UVCManager : MonoBehaviour
  21. {
  22. private const string TAG = "UVCManager#";
  23. private const string FQCN_DETECTOR = "com.serenegiant.usb.DeviceDetectorFragment";
  24. //--------------------------------------------------------------------------------
  25. private const int FRAME_TYPE_MJPEG = 0x000007;
  26. private const int FRAME_TYPE_H264 = 0x000014;
  27. private const int FRAME_TYPE_H264_FRAME = 0x030011;
  28. //--------------------------------------------------------------------------------
  29. // Camera Terminal DescriptorのbmControlsフィールドのビットマスク
  30. private const UInt64 CTRL_SCANNING = 0x00000001; // D0: Scanning Mode
  31. private const UInt64 CTRL_AE = 0x00000002; // D1: Auto-Exposure Mode
  32. private const UInt64 CTRL_AE_PRIORITY = 0x00000004; // D2: Auto-Exposure Priority
  33. private const UInt64 CTRL_AE_ABS = 0x00000008; // D3: Exposure Time (Absolute)
  34. private const UInt64 CTRL_AE_REL = 0x00000010; // D4: Exposure Time (Relative)
  35. private const UInt64 CTRL_FOCUS_ABS = 0x00000020; // D5: Focus (Absolute)
  36. private const UInt64 CTRL_FOCUS_REL = 0x00000040; // D6: Focus (Relative)
  37. private const UInt64 CTRL_IRIS_ABS = 0x00000080; // D7: Iris (Absolute)
  38. private const UInt64 CTRL_IRIS_REL = 0x00000100; // D8: Iris (Relative)
  39. private const UInt64 CTRL_ZOOM_ABS = 0x00000200; // D9: Zoom (Absolute)
  40. private const UInt64 CTRL_ZOOM_REL = 0x00000400; // D10: Zoom (Relative)
  41. private const UInt64 CTRL_PANTILT_ABS = 0x00000800; // D11: PanTilt (Absolute)
  42. private const UInt64 CTRL_PAN_ABS = 0x01000800; // D11: PanTilt (Absolute)
  43. private const UInt64 CTRL_TILT_ABS = 0x02000800; // D11: PanTilt (Absolute)
  44. private const UInt64 CTRL_PANTILT_REL = 0x00001000; // D12: PanTilt (Relative)
  45. private const UInt64 CTRL_PAN_REL = 0x01001000; // D12: PanTilt (Relative)
  46. private const UInt64 CTRL_TILT_REL = 0x02001000; // D12: PanTilt (Relative)
  47. private const UInt64 CTRL_ROLL_ABS = 0x00002000; // D13: Roll (Absolute)
  48. private const UInt64 CTRL_ROLL_REL = 0x00004000; // D14: Roll (Relative)
  49. private const UInt64 CTRL_D15 = 0x00008000; // D15: Reserved
  50. private const UInt64 CTRL_D16 = 0x00010000; // D16: Reserved
  51. private const UInt64 CTRL_FOCUS_AUTO = 0x00020000; // D17: Focus, Auto
  52. private const UInt64 CTRL_PRIVACY = 0x00040000; // D18: Privacy
  53. private const UInt64 CTRL_FOCUS_SIMPLE = 0x00080000; // D19: Focus, Simple
  54. private const UInt64 CTRL_WINDOW = 0x00100000; // D20: Window
  55. private const UInt64 CTRL_ROI = 0x00200000; // D21: ROI
  56. private const UInt64 CTRL_D22 = 0x00400000; // D22: Reserved
  57. private const UInt64 CTRL_D23 = 0x00800000; // D23: Reserved
  58. // Processing Unit DescriptorのbmControlsフィールドのビットマスク
  59. private const UInt64 PU_BRIGHTNESS = 0x00000001; // D0: Brightness
  60. private const UInt64 PU_CONTRAST = 0x00000002; // D1: Contrast
  61. private const UInt64 PU_HUE = 0x00000004; // D2: Hue
  62. private const UInt64 PU_SATURATION = 0x00000008; // D3: Saturation
  63. private const UInt64 PU_SHARPNESS = 0x00000010; // D4: Sharpness
  64. private const UInt64 PU_GAMMA = 0x00000020; // D5: Gamma
  65. private const UInt64 PU_WB_TEMP = 0x00000040; // D6: White Balance Temperature
  66. private const UInt64 PU_WB_COMPO = 0x00000080; // D7: White Balance Component
  67. private const UInt64 PU_BACKLIGHT = 0x00000100; // D8: Backlight Compensation
  68. private const UInt64 PU_GAIN = 0x00000200; // D9: Gain
  69. private const UInt64 PU_POWER_LF = 0x00000400; // D10: Power Line Frequency
  70. private const UInt64 PU_HUE_AUTO = 0x00000800; // D11: Hue, Auto
  71. private const UInt64 PU_WB_TEMP_AUTO = 0x00001000; // D12: White Balance Temperature, Auto
  72. private const UInt64 PU_WB_COMPO_AUTO = 0x00002000; // D13: White Balance Component, Auto
  73. private const UInt64 PU_DIGITAL_MULT = 0x00004000; // D14: Digital Multiplier
  74. private const UInt64 PU_DIGITAL_LIMIT = 0x00008000; // D15: Digital Multiplier Limit
  75. private const UInt64 PU_AVIDEO_STD = 0x00010000; // D16: Analog Video Standard
  76. private const UInt64 PU_AVIDEO_LOCK = 0x00020000; // D17: Analog Video Lock Status
  77. private const UInt64 PU_CONTRAST_AUTO = 0x00040000; // D18: Contrast, Auto
  78. private const UInt64 PU_D19 = 0x00080000; // D19: Reserved
  79. private const UInt64 PU_D20 = 0x00100000; // D20: Reserved
  80. private const UInt64 PU_D21 = 0x00200000; // D21: Reserved
  81. private const UInt64 PU_D22 = 0x00400000; // D22: Reserved
  82. private const UInt64 PU_D23 = 0x00800000; // D23: Reserved
  83. // プロセッシングユニットのコントロールタイプを識別するために最上位ビットを立てる
  84. private const UInt64 PU_MASK = 0x80000000;
  85. //--------------------------------------------------------------------------------
  86. private static readonly UInt64[] SUPPORTED_CTRLS = {
  87. CTRL_SCANNING,
  88. CTRL_AE,
  89. CTRL_AE_PRIORITY,
  90. CTRL_AE_ABS,
  91. //CTRL_AE_REL,
  92. CTRL_FOCUS_ABS,
  93. //CTRL_FOCUS_REL,
  94. CTRL_IRIS_ABS,
  95. //CTRL_IRIS_REL,
  96. CTRL_ZOOM_ABS,
  97. //CTRL_ZOOM_REL,
  98. //CTRL_PANTILT_ABS,
  99. CTRL_PAN_ABS,
  100. CTRL_TILT_ABS,
  101. //CTRL_PANTILT_REL,
  102. //CTRL_PAN_REL,
  103. //CTRL_TILT_REL,
  104. CTRL_ROLL_ABS,
  105. //CTRL_ROLL_REL,
  106. CTRL_FOCUS_AUTO,
  107. //CTRL_PRIVACY,
  108. //CTRL_FOCUS_SIMPLE,
  109. //CTRL_WINDOW,
  110. //CTRL_ROI,
  111. };
  112. private static readonly UInt64[] SUPPORTED_PROCS =
  113. {
  114. PU_BRIGHTNESS,
  115. PU_CONTRAST,
  116. PU_HUE,
  117. PU_SATURATION,
  118. PU_SHARPNESS,
  119. PU_GAMMA,
  120. PU_WB_TEMP,
  121. PU_WB_COMPO,
  122. PU_BACKLIGHT,
  123. PU_GAIN,
  124. PU_POWER_LF,
  125. PU_HUE_AUTO,
  126. PU_WB_TEMP_AUTO,
  127. PU_WB_COMPO_AUTO,
  128. PU_DIGITAL_MULT,
  129. PU_DIGITAL_LIMIT,
  130. PU_AVIDEO_STD,
  131. PU_AVIDEO_LOCK,
  132. PU_CONTRAST_AUTO,
  133. };
  134. //测试重置接口
  135. internal static int onResize(int id, int frameType, int width, int height)
  136. {
  137. return Resize(id, frameType, width, height);
  138. }
  139. //--------------------------------------------------------------------------------
  140. /**
  141. * IUVCSelectorがセットされていないとき
  142. * またはIUVCSelectorが解像度選択時にnullを
  143. * 返したときのデフォルトの解像度(幅)
  144. */
  145. public Int32 DefaultWidth = 1280;
  146. /**
  147. * IUVCSelectorがセットされていないとき
  148. * またはIUVCSelectorが解像度選択時にnullを
  149. * 返したときのデフォルトの解像度(高さ)
  150. */
  151. public Int32 DefaultHeight = 720;
  152. /**
  153. * UVC機器とのネゴシエーション時に
  154. * H.264を優先してネゴシエーションするかどうか
  155. * Android実機のみ有効
  156. * true: H.264 > MJPEG > YUV
  157. * false: MJPEG > H.264 > YUV
  158. */
  159. public bool PreferH264 = false;
  160. /**
  161. * 可能な場合にUACから音声取得を行うかどうか
  162. */
  163. public bool UACEnabled = false;
  164. /**
  165. * シーンレンダリングの前にUVC機器映像のテクスチャへのレンダリング要求を行うかどうか
  166. */
  167. public bool RenderBeforeSceneRendering = false;
  168. /**
  169. * UVC関係のイベンドハンドラー
  170. */
  171. [SerializeField, ComponentRestriction(typeof(IUVCDrawer))]
  172. public Component[] UVCDrawers;
  173. /**
  174. * 使用中のカメラ情報を保持するホルダークラス
  175. */
  176. public class CameraInfo
  177. {
  178. internal readonly UVCDevice device;
  179. internal Texture previewTexture;
  180. internal int frameType;
  181. internal volatile Int32 activeId;
  182. private Int32 currentWidth;
  183. private Int32 currentHeight;
  184. private bool isRenderBeforeSceneRendering;
  185. private bool isRendering;
  186. private Dictionary<UInt64, UVCCtrlInfo> ctrlInfos = new Dictionary<UInt64, UVCCtrlInfo>();
  187. //PC测试用
  188. internal CameraInfo(Texture texture)
  189. {
  190. this.device = null;
  191. activeId = 1;
  192. previewTexture = texture;
  193. SetSize(texture.width, texture.height);
  194. }
  195. internal CameraInfo(UVCDevice device)
  196. {
  197. this.device = device;
  198. }
  199. /**
  200. * 機器idを取得
  201. */
  202. public Int32 Id
  203. {
  204. get { return device.id; }
  205. }
  206. /**
  207. * 機器名を取得
  208. */
  209. public string DeviceName
  210. {
  211. get { return device.name; }
  212. }
  213. /**
  214. * ベンダーIDを取得
  215. */
  216. public int Vid
  217. {
  218. get { return device.vid; }
  219. }
  220. /**
  221. * プロダクトIDを取得
  222. */
  223. public int Pid
  224. {
  225. get { return device.pid; }
  226. }
  227. /**
  228. * 映像取得中かどうか
  229. */
  230. public bool IsPreviewing
  231. {
  232. get { return (activeId != 0) && (previewTexture != null); }
  233. }
  234. /**
  235. * 現在の解像度(幅)
  236. * プレビュー中でなければ0
  237. */
  238. public Int32 CurrentWidth
  239. {
  240. get { return currentWidth; }
  241. }
  242. /**
  243. * 現在の解像度(高さ)
  244. * プレビュー中でなければ0
  245. */
  246. public Int32 CurrentHeight
  247. {
  248. get { return currentHeight; }
  249. }
  250. /**
  251. * 2024/01/19 返回一个size
  252. */
  253. public Vector2 Size => new Vector2(currentWidth, currentHeight);
  254. public Vector2Int IndexToCoord(int i)
  255. {
  256. var y = i / currentWidth;
  257. var x = i % currentWidth;
  258. return new Vector2Int(x, y);
  259. }
  260. public int CoordToIndex(int x, int y)
  261. {
  262. return y * currentWidth + x;
  263. }
  264. /**
  265. * 現在の解像度を変更
  266. * @param width
  267. * @param height
  268. */
  269. internal void SetSize(Int32 width, Int32 height)
  270. {
  271. currentWidth = width;
  272. currentHeight = height;
  273. Debug.Log("设置 SetSize:[" + width + "," + height + "]");
  274. }
  275. /**
  276. * サポートしているUVCコントロール/プロセッシング機能の情報を更新する
  277. */
  278. public void UpdateCtrls()
  279. {
  280. ctrlInfos.Clear();
  281. var ctrls = GetCtrlSupports(Id);
  282. foreach (UInt64 ctrl in SUPPORTED_CTRLS)
  283. {
  284. if ((ctrls & ctrl) == ctrl)
  285. {
  286. UVCCtrlInfo info = new UVCCtrlInfo();
  287. info.type = ctrl;
  288. if (GetCtrlInfo(Id, ref info) == 0)
  289. {
  290. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  291. Console.WriteLine($"{TAG}ctrl({ctrl:X}):={info}");
  292. #endif
  293. ctrlInfos.Add(info.type, info);
  294. }
  295. }
  296. }
  297. ctrls = GetProcSupports(Id);
  298. foreach (UInt64 ctrl in SUPPORTED_PROCS)
  299. {
  300. if ((ctrls & ctrl) == ctrl)
  301. {
  302. UVCCtrlInfo info = new UVCCtrlInfo();
  303. info.type = ctrl | PU_MASK;
  304. if (GetCtrlInfo(Id, ref info) == 0)
  305. {
  306. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  307. Console.WriteLine($"{TAG}proc({ctrl:X}):={info}");
  308. #endif
  309. ctrlInfos.Add(info.type, info);
  310. }
  311. }
  312. }
  313. }
  314. /**
  315. * 対応しているUVCコントロール/プロセッシング機能のtype一覧を取得
  316. */
  317. public List<UInt64> GetCtrls()
  318. {
  319. return new List<UInt64>(ctrlInfos.Keys);
  320. }
  321. /**
  322. * 指定したUVCコントロール/プロセッシング機能の情報を取得
  323. * @param type
  324. * @return UVCCtrlInfo
  325. * @throws ArgumentOutOfRangeException
  326. */
  327. public UVCCtrlInfo GetInfo(UInt64 type)
  328. {
  329. if (ctrlInfos.ContainsKey(type))
  330. {
  331. return ctrlInfos.GetValueOrDefault(type, new UVCCtrlInfo());
  332. }
  333. else
  334. {
  335. throw new ArgumentOutOfRangeException($"Not supported control type{type:X}");
  336. }
  337. }
  338. public UInt64 GetTypeByName(string typeName)
  339. {
  340. UInt64 type = 0;
  341. switch (typeName)
  342. {
  343. // Control Unit Cases
  344. case "CTRL_SCANNING":
  345. type = CTRL_SCANNING;
  346. break;
  347. case "CTRL_AE":
  348. type = CTRL_AE;
  349. break;
  350. case "CTRL_AE_PRIORITY":
  351. type = CTRL_AE_PRIORITY;
  352. break;
  353. case "CTRL_AE_ABS":
  354. type = CTRL_AE_ABS;
  355. break;
  356. case "CTRL_FOCUS_ABS":
  357. type = CTRL_FOCUS_ABS;
  358. break;
  359. case "CTRL_IRIS_ABS":
  360. type = CTRL_IRIS_ABS;
  361. break;
  362. case "CTRL_ZOOM_ABS":
  363. type = CTRL_ZOOM_ABS;
  364. break;
  365. case "CTRL_PAN_ABS":
  366. type = CTRL_PAN_ABS;
  367. break;
  368. case "CTRL_TILT_ABS":
  369. type = CTRL_TILT_ABS;
  370. break;
  371. case "CTRL_ROLL_ABS":
  372. type = CTRL_ROLL_ABS;
  373. break;
  374. case "CTRL_FOCUS_AUTO":
  375. type = CTRL_FOCUS_AUTO;
  376. break;
  377. // Processing Unit Cases
  378. case "PU_BRIGHTNESS":
  379. type = PU_BRIGHTNESS | PU_MASK;
  380. break;
  381. case "PU_CONTRAST":
  382. type = PU_CONTRAST | PU_MASK;
  383. break;
  384. case "PU_HUE":
  385. type = PU_HUE | PU_MASK;
  386. break;
  387. case "PU_SATURATION":
  388. type = PU_SATURATION | PU_MASK;
  389. break;
  390. case "PU_SHARPNESS":
  391. type = PU_SHARPNESS | PU_MASK;
  392. break;
  393. case "PU_GAMMA":
  394. type = PU_GAMMA | PU_MASK;
  395. break;
  396. case "PU_WB_TEMP":
  397. type = PU_WB_TEMP | PU_MASK;
  398. break;
  399. case "PU_WB_COMPO":
  400. type = PU_WB_COMPO | PU_MASK;
  401. break;
  402. case "PU_BACKLIGHT":
  403. type = PU_BACKLIGHT | PU_MASK;
  404. break;
  405. case "PU_GAIN":
  406. type = PU_GAIN | PU_MASK;
  407. break;
  408. case "PU_POWER_LF":
  409. type = PU_POWER_LF | PU_MASK;
  410. break;
  411. case "PU_HUE_AUTO":
  412. type = PU_HUE_AUTO | PU_MASK;
  413. break;
  414. case "PU_WB_TEMP_AUTO":
  415. type = PU_WB_TEMP_AUTO | PU_MASK;
  416. break;
  417. case "PU_WB_COMPO_AUTO":
  418. type = PU_WB_COMPO_AUTO | PU_MASK;
  419. break;
  420. case "PU_DIGITAL_MULT":
  421. type = PU_DIGITAL_MULT | PU_MASK;
  422. break;
  423. case "PU_DIGITAL_LIMIT":
  424. type = PU_DIGITAL_LIMIT | PU_MASK;
  425. break;
  426. case "PU_AVIDEO_STD":
  427. type = PU_AVIDEO_STD | PU_MASK;
  428. break;
  429. case "PU_AVIDEO_LOCK":
  430. type = PU_AVIDEO_LOCK | PU_MASK;
  431. break;
  432. case "PU_CONTRAST_AUTO":
  433. type = PU_CONTRAST_AUTO | PU_MASK;
  434. break;
  435. default:
  436. Console.WriteLine($"Unknown processing unit: {type}");
  437. break;
  438. }
  439. return type;
  440. }
  441. public bool ContainsKey(UInt64 type)
  442. {
  443. return ctrlInfos.ContainsKey(type);
  444. }
  445. /**
  446. * UVCコントロール/プロセッシング機能の設定値を取得
  447. * @param type
  448. * @return 変更後の値
  449. * @throws ArgumentOutOfRangeException
  450. * @throws Exception
  451. */
  452. public Int32 GetValue(UInt64 type)
  453. {
  454. if (ctrlInfos.ContainsKey(type))
  455. {
  456. Int32 value = 0;
  457. var r = GetCtrlValue(Id, type, ref value);
  458. if (r == 0)
  459. {
  460. return value;
  461. }
  462. else
  463. {
  464. throw new Exception($"Failed to get control value,type={type},err={r}");
  465. }
  466. }
  467. else
  468. {
  469. throw new ArgumentOutOfRangeException($"Not supported control type{type:X}");
  470. }
  471. }
  472. /**
  473. * UVCコントロール/プロセッシング機能の設定変更
  474. * @param type
  475. * @param value
  476. * @return 変更後の値
  477. * @throws ArgumentOutOfRangeException
  478. * @throws Exception
  479. */
  480. public Int32 SetValue(UInt64 type, Int32 value)
  481. {
  482. if (ctrlInfos.ContainsKey(type))
  483. {
  484. var r = SetCtrlValue(Id, type, value);
  485. if (r == 0)
  486. {
  487. r = GetCtrlValue(Id, type, ref value);
  488. if (r == 0)
  489. {
  490. var info = ctrlInfos.GetValueOrDefault(type, new UVCCtrlInfo());
  491. info.current = value;
  492. ctrlInfos[type] = info;
  493. return value;
  494. }
  495. else
  496. {
  497. Debug.LogError($"Failed to get control value,type={type},err={r}");
  498. }
  499. }
  500. else
  501. {
  502. Debug.LogError($"Failed to set control value,type={type},err={r}");
  503. }
  504. }
  505. else
  506. {
  507. Debug.LogError($"Not supported control type{type:X}");
  508. }
  509. return 0;
  510. }
  511. public override string ToString()
  512. {
  513. return $"{base.ToString()}({currentWidth}x{currentHeight},id={Id},activeId={activeId},IsPreviewing={IsPreviewing})";
  514. }
  515. /**
  516. * UVC機器からの映像のレンダリングを開始
  517. * @param manager
  518. */
  519. internal Coroutine StartRender(UVCManager manager, bool renderBeforeSceneRendering)
  520. {
  521. StopRender(manager);
  522. isRenderBeforeSceneRendering = renderBeforeSceneRendering;
  523. isRendering = true;
  524. if (renderBeforeSceneRendering)
  525. {
  526. return manager.StartCoroutine(OnRenderBeforeSceneRendering());
  527. }
  528. else
  529. {
  530. return manager.StartCoroutine(OnRender());
  531. }
  532. }
  533. /**
  534. * UVC機器からの映像のレンダリングを終了
  535. * @param manager
  536. */
  537. internal void StopRender(UVCManager manager)
  538. {
  539. if (isRendering)
  540. {
  541. isRendering = false;
  542. if (isRenderBeforeSceneRendering)
  543. {
  544. manager.StopCoroutine(OnRenderBeforeSceneRendering());
  545. }
  546. else
  547. {
  548. manager.StopCoroutine(OnRender());
  549. }
  550. }
  551. }
  552. /**
  553. * レンダーイベント処理用
  554. * コールーチンとして実行される
  555. * シーンレンダリングの前にUVC機器からの映像をテクスチャへレンダリング要求する
  556. */
  557. private IEnumerator OnRenderBeforeSceneRendering()
  558. {
  559. var renderEventFunc = GetRenderEventFunc();
  560. for (; activeId != 0;)
  561. {
  562. yield return null;
  563. GL.IssuePluginEvent(renderEventFunc, activeId);
  564. }
  565. yield break;
  566. }
  567. /**
  568. * レンダーイベント処理用
  569. * コールーチンとして実行される
  570. * レンダリング後にUVC機器からの映像をテクスチャへレンダリング要求する
  571. */
  572. private IEnumerator OnRender()
  573. {
  574. var renderEventFunc = GetRenderEventFunc();
  575. for (; activeId != 0;)
  576. {
  577. yield return new WaitForEndOfFrame();
  578. GL.IssuePluginEvent(renderEventFunc, activeId);
  579. }
  580. yield break;
  581. }
  582. } // CameraInfo
  583. /**
  584. * UAC機器からの音声取得に関するオブジェクトを保持するためのホルダークラス
  585. */
  586. public class AudioInfo
  587. {
  588. internal readonly UVCDevice device;
  589. private UACInfo info = new UACInfo();
  590. private int samplesPerFrame = 0;
  591. private Int16[] buffer; // 今のところPCM16にしか対応しない, OnPCM16Readないではなくここで保持するともしかするとメモリーブロックが不連続になったり移動したりするかもしれないけど
  592. private volatile AudioClip audioClip;
  593. private volatile Int32 activeId;
  594. internal AudioInfo(UVCDevice device)
  595. {
  596. this.device = device;
  597. }
  598. /**
  599. * 音声取得中かどうか
  600. */
  601. public bool IsStreaming
  602. {
  603. get { return (activeId != 0) && (audioClip != null); }
  604. }
  605. /**
  606. * 音声取得開始する
  607. * すでに音声取得中なら何もしない
  608. * @param manager
  609. */
  610. internal AudioClip Start(UVCManager manager)
  611. {
  612. var result = StartUAC(device.id);
  613. if (result == 0)
  614. {
  615. if (GetUACInfo(device.id, ref info) == 0)
  616. {
  617. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  618. Console.WriteLine($"{TAG}Start:info={info}");
  619. #endif
  620. try
  621. {
  622. // PCM16のはず
  623. activeId = device.id;
  624. samplesPerFrame = info.packetBytes / (info.resolution / 8);
  625. buffer = new Int16[samplesPerFrame];
  626. audioClip = AudioClip.Create(device.name, Int32.MaxValue, info.channels, info.samplingFreq, true, OnPCM16Read);
  627. return audioClip;
  628. }
  629. catch (Exception e)
  630. {
  631. activeId = 0;
  632. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  633. Console.WriteLine($"Failed to create audio clip,err={e}");
  634. #endif
  635. throw e;
  636. }
  637. }
  638. else
  639. {
  640. manager.StopAudio(device);
  641. throw new Exception($"Failed to get streaming info,err={result}");
  642. }
  643. }
  644. else
  645. {
  646. throw new Exception($"Failed to start uac streaming,err={result}");
  647. }
  648. }
  649. /**
  650. * 音声取得停止する
  651. * @param manager
  652. */
  653. internal void Stop(UVCManager manager)
  654. {
  655. activeId = 0;
  656. audioClip = null;
  657. manager.StopAudio(device);
  658. }
  659. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  660. private int readCnt = 0;
  661. #endif
  662. /**
  663. * AudioClipからのコールバック
  664. * @param data
  665. */
  666. private void OnPCM16Read(float[] data)
  667. {
  668. if (!IsStreaming) return;
  669. var numSamples = data.Length; // 今回読み取る最大サンプル数
  670. var maxReadCnt = numSamples / samplesPerFrame; // 最大読み込み回数
  671. if (maxReadCnt == 0)
  672. {
  673. maxReadCnt = 1;
  674. }
  675. var result = -1;
  676. Int64 ptsUs = 0;
  677. //var buffer = new Int16[samplesPerFrame]; // 高速化のためにStartでアロケーションするように変更した
  678. Int32 dataBytes = 0;
  679. Int32 totalSamples = 0;
  680. for (int i = 0; (i < maxReadCnt) && (totalSamples < numSamples); i++)
  681. {
  682. result = GetUACFrame(device.id, buffer, ref dataBytes, ref ptsUs);
  683. if ((result == 0) && (dataBytes >= 2))
  684. {
  685. var samples = Math.Min(dataBytes / 2, samplesPerFrame);
  686. for (int j = 0; j < samples; j++)
  687. {
  688. data[j + totalSamples] = buffer[j] / (float)short.MaxValue;
  689. }
  690. totalSamples += samples;
  691. }
  692. else
  693. {
  694. break;
  695. }
  696. }
  697. if (totalSamples < numSamples)
  698. {
  699. Array.Resize<float>(ref data, totalSamples);
  700. }
  701. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  702. if ((readCnt++ % 100) == 0)
  703. {
  704. Console.WriteLine($"{TAG}OnPCM16Read:len={numSamples},total={totalSamples},r={result},bytes={dataBytes},pts={ptsUs}");
  705. }
  706. #endif
  707. }
  708. } // AudioInfo
  709. /**
  710. * メインスレッド上で実行するためのSynchronizationContextインスタンス
  711. */
  712. private SynchronizationContext mainContext;
  713. /**
  714. * 端末に接続されたUVC機器の状態が変化した時のイベントコールバックを受け取るデリゲーター
  715. */
  716. private PluginCallbackManager.OnDeviceChangedFunc callback;
  717. /**
  718. * 端末に接続されたUVC機器リスト
  719. */
  720. private List<UVCDevice> attachedDevices = new List<UVCDevice>();
  721. /**
  722. * 映像取得中のUVC機器のマップ
  723. * 機器識別用のid - CameraInfoペアを保持する
  724. */
  725. private Dictionary<Int32, CameraInfo> cameraInfos = new Dictionary<int, CameraInfo>();
  726. /**
  727. * 音声取得中のUVC機器のマップ
  728. * 機器識別用のid - AudioInfoペアを保持する
  729. */
  730. private Dictionary<Int32, AudioInfo> audioInFos = new Dictionary<int, AudioInfo>();
  731. #region 额外添加的操作
  732. public bool InitUVCEnabled = true;
  733. #endregion
  734. //--------------------------------------------------------------------------------
  735. // UnityEngineからの呼び出し
  736. //--------------------------------------------------------------------------------
  737. // Start is called before the first frame update
  738. IEnumerator Start()
  739. {
  740. if (!InitUVCEnabled) yield break;
  741. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  742. Console.WriteLine($"{TAG}Start:");
  743. #endif
  744. mainContext = SynchronizationContext.Current;
  745. callback = PluginCallbackManager.Add(this);
  746. Debug.Log($"{TAG}Start:");
  747. yield return Initialize();
  748. }
  749. /// <summary>
  750. /// 调用来初始化
  751. /// </summary>
  752. /// <returns></returns>
  753. public IEnumerator startUVCManager()
  754. {
  755. yield return new WaitForSeconds(1.0f);
  756. Debug.Log("startUVCManager Start:");
  757. Debug.Log($"{TAG} startUVCManager Start:");
  758. mainContext = SynchronizationContext.Current;
  759. callback = PluginCallbackManager.Add(this);
  760. yield return Initialize();
  761. }
  762. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  763. void OnApplicationFocus()
  764. {
  765. Console.WriteLine($"{TAG}OnApplicationFocus:");
  766. }
  767. #endif
  768. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  769. void OnApplicationPause(bool pauseStatus)
  770. {
  771. Console.WriteLine($"{TAG}OnApplicationPause:{pauseStatus}");
  772. }
  773. #endif
  774. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  775. void OnApplicationQuits()
  776. {
  777. Console.WriteLine($"{TAG}OnApplicationQuits:");
  778. }
  779. #endif
  780. void OnDestroy()
  781. {
  782. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  783. Console.WriteLine($"{TAG}OnDestroy:");
  784. #endif
  785. StopAll();
  786. PluginCallbackManager.Remove(this);
  787. }
  788. //--------------------------------------------------------------------------------
  789. // UVC機器接続状態が変化したときのプラグインからのコールバック関数
  790. //--------------------------------------------------------------------------------
  791. public void OnDeviceChanged(IntPtr devicePtr, bool attached)
  792. {
  793. var id = UVCDevice.GetId(devicePtr);
  794. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  795. Console.WriteLine($"{TAG}OnDeviceChangedInternal:id={id},attached={attached}");
  796. #endif
  797. if (attached)
  798. {
  799. UVCDevice device = new UVCDevice(devicePtr);
  800. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  801. Console.WriteLine($"{TAG}OnDeviceChangedInternal:device={device.ToString()}");
  802. #endif
  803. if (HandleOnAttachEvent(device))
  804. {
  805. attachedDevices.Add(device);
  806. StartPreview(device);
  807. if (UACEnabled)
  808. { // UVCManagerのUAC機能が有効な場合
  809. StartAudio(device);
  810. }
  811. }
  812. }
  813. else
  814. {
  815. var found = attachedDevices.Find(item =>
  816. {
  817. return item != null && item.id == id;
  818. });
  819. if (found != null)
  820. {
  821. HandleOnDetachEvent(found);
  822. StopPreview(found);
  823. StopAudio(found);
  824. RemoveCamera(found);
  825. RemoveAudio(found);
  826. attachedDevices.Remove(found);
  827. }
  828. }
  829. }
  830. //================================================================================
  831. /**
  832. * 接続中のUVC機器一覧を取得
  833. * @return 接続中のUVC機器一覧List
  834. */
  835. public List<CameraInfo> GetAttachedDevices()
  836. {
  837. return new List<CameraInfo>(cameraInfos.Values);
  838. }
  839. // /**
  840. // * 対応解像度を取得
  841. // * @param camera 対応解像度を取得するUVC機器を指定
  842. // * @return 対応解像度 既にカメラが取り外されている/closeしているのであればnull
  843. // */
  844. // public SupportedFormats GetSupportedVideoSize(CameraInfo camera)
  845. // {
  846. // var info = (camera != null) ? GetCamera(camera.device) : null;
  847. // if ((info != null) && info.IsOpen)
  848. // {
  849. // return GetSupportedVideoSize(info.DeviceName);
  850. // }
  851. // else
  852. // {
  853. // return null;
  854. // }
  855. // }
  856. // /**
  857. // * 解像度を変更
  858. // * @param 解像度を変更するUVC機器を指定
  859. // * @param 変更する解像度を指定, nullならデフォルトに戻す
  860. // * @param 解像度が変更されたかどうか
  861. // */
  862. // public bool SetVideoSize(CameraInfo camera, SupportedFormats.Size size)
  863. // {
  864. // var info = (camera != null) ? GetCamera(camera.device) : null;
  865. // var width = size != null ? size.Width : DefaultWidth;
  866. // var height = size != null ? size.Height : DefaultHeight;
  867. // if ((info != null) && info.IsPreviewing)
  868. // {
  869. // if ((width != info.CurrentWidth) || (height != info.CurrentHeight))
  870. // { // 解像度が変更になるとき
  871. // StopPreview(info.DeviceName);
  872. // StartPreview(info.DeviceName, width, height);
  873. // return true;
  874. // }
  875. // }
  876. // return false;
  877. // }
  878. public void onStartUVCManager()
  879. {
  880. StartCoroutine(startUVCManager());
  881. }
  882. public void onStartPreview(UVCDevice device)
  883. {
  884. // StartPreview(device);
  885. if (HandleOnAttachEvent(device))
  886. {
  887. attachedDevices.Add(device);
  888. StartPreview(device);
  889. if (UACEnabled)
  890. { // UVCManagerのUAC機能が有効な場合
  891. StartAudio(device);
  892. }
  893. }
  894. }
  895. public void onStopPreview(UVCDevice device)
  896. {
  897. var found = attachedDevices.Find(item =>
  898. {
  899. return item != null && item.id == device.id;
  900. });
  901. if (found != null)
  902. {
  903. HandleOnDetachEvent(found);
  904. StopPreview(found);
  905. StopAudio(found);
  906. RemoveCamera(found);
  907. RemoveAudio(found);
  908. attachedDevices.Remove(found);
  909. }
  910. }
  911. private void StartPreview(UVCDevice device)
  912. {
  913. var info = CreateCameraIfNotExist(device);
  914. if ((info != null) && !info.IsPreviewing)
  915. {
  916. Debug.Log("开始渲染StartPreview DefaultWidth:" + DefaultWidth + " , DefaultHeight:" + DefaultHeight);
  917. int width = DefaultWidth;
  918. int height = DefaultHeight;
  919. // var supportedVideoSize = GetSupportedVideoSize(deviceName);
  920. // if (supportedVideoSize == null)
  921. // {
  922. // throw new ArgumentException("fauled to get supported video size");
  923. // }
  924. // // 解像度の選択処理
  925. // if ((UVCDrawers != null) && (UVCDrawers.Length > 0))
  926. // {
  927. // foreach (var drawer in UVCDrawers)
  928. // {
  929. // if ((drawer is IUVCDrawer) && ((drawer as IUVCDrawer).IsUVCEnabled(this, info.device)))
  930. // {
  931. // var size = (drawer as IUVCDrawer).OnUVCSelectSize(this, info.device, supportedVideoSize);
  932. //#if (!NDEBUG && DEBUG && ENABLE_LOG)
  933. // Console.WriteLine($"{TAG}StartPreview:selected={size}");
  934. //#endif
  935. // if (size != null)
  936. // { // 一番最初に見つかった描画可能なIUVCDrawersがnull以外を返せばそれを使う
  937. // width = size.Width;
  938. // height = size.Height;
  939. // break;
  940. // }
  941. // }
  942. // }
  943. // }
  944. // FIXME 対応解像度の確認処理
  945. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  946. Console.WriteLine($"{TAG}StartPreview:({width}x{height}),id={device.id}");
  947. #endif
  948. int[] frameTypes = {
  949. PreferH264 ? FRAME_TYPE_H264 : FRAME_TYPE_MJPEG,
  950. PreferH264 ? FRAME_TYPE_MJPEG : FRAME_TYPE_H264,
  951. };
  952. foreach (var frameType in frameTypes)
  953. {
  954. if (Resize(device.id, frameType, width, height) == 0)
  955. {
  956. info.frameType = frameType;
  957. break;
  958. }
  959. }
  960. info.SetSize(width, height);
  961. info.activeId = device.id;
  962. info.UpdateCtrls();
  963. mainContext.Post(__ =>
  964. { // テクスチャの生成はメインスレッドで行わないといけない
  965. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  966. Console.WriteLine($"{TAG}映像受け取り用テクスチャ生成:({width}x{height})");
  967. #endif
  968. Texture2D tex = new Texture2D(
  969. width, height,
  970. TextureFormat.ARGB32,
  971. false, /* mipmap */
  972. true /* linear */);
  973. tex.filterMode = FilterMode.Point;
  974. tex.Apply();
  975. info.previewTexture = tex;
  976. var nativeTexPtr = info.previewTexture.GetNativeTexturePtr();
  977. Debug.Log(TAG + "nativeTexPtr:"+ nativeTexPtr.ToInt32() + "[" + width + "," + height + "]");
  978. Start(device.id, nativeTexPtr.ToInt32());
  979. HandleOnStartPreviewEvent(info);
  980. //测试
  981. //InfraredDemo._ins.MyCameraRender2.texture = info.previewTexture;
  982. info.StartRender(this, RenderBeforeSceneRendering);
  983. }, null);
  984. }
  985. }
  986. private void StopPreview(UVCDevice device)
  987. {
  988. var info = GetCamera(device);
  989. if ((info != null) && info.IsPreviewing)
  990. {
  991. mainContext.Post(__ =>
  992. {
  993. HandleOnStopPreviewEvent(info);
  994. Stop(device.id);
  995. info.StopRender(this);
  996. // Release the old texture if it exists
  997. if (info.previewTexture != null)
  998. {
  999. Destroy(info.previewTexture);
  1000. info.previewTexture = null;
  1001. }
  1002. info.SetSize(0, 0);
  1003. info.activeId = 0;
  1004. }, null);
  1005. }
  1006. }
  1007. private void StartAudio(UVCDevice device)
  1008. {
  1009. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1010. Console.WriteLine($"{TAG}StartAudio:");
  1011. #endif
  1012. if (device.isUAC)
  1013. {
  1014. var audio = CreateAudioIfNotExist(device);
  1015. if ((audio != null) && !audio.IsStreaming)
  1016. {
  1017. mainContext.Post(__ =>
  1018. {
  1019. var audioClip = audio.Start(this);
  1020. HandleOnStartAudioEvent(audio, audioClip);
  1021. }, null);
  1022. }
  1023. }
  1024. else
  1025. {
  1026. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1027. Console.WriteLine($"{TAG}StartAudio:Not a UAC device");
  1028. #endif
  1029. }
  1030. }
  1031. private void StopAudio(UVCDevice device)
  1032. {
  1033. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1034. Console.WriteLine($"{TAG}StopAudio:");
  1035. #endif
  1036. var audio = GetAudio(device);
  1037. if (audio != null && audio.IsStreaming)
  1038. {
  1039. mainContext.Post(__ =>
  1040. {
  1041. HandleOnStopAudioEvent(audio);
  1042. audio.Stop(this);
  1043. }, null);
  1044. }
  1045. }
  1046. private void StopAll()
  1047. {
  1048. List<CameraInfo> values = new List<CameraInfo>(cameraInfos.Values);
  1049. foreach (var info in values)
  1050. {
  1051. StopAudio(info.device);
  1052. StopPreview(info.device);
  1053. Debug.Log("******StopAll:" + info.device.ToString());
  1054. }
  1055. }
  1056. //--------------------------------------------------------------------------------
  1057. /**
  1058. * UVC機器が接続されたときの処理の実体
  1059. * @param info
  1060. * @return true: 接続されたUVC機器を使用する, false: 接続されたUVC機器を使用しない
  1061. */
  1062. private bool HandleOnAttachEvent(UVCDevice device/*NonNull*/)
  1063. {
  1064. if ((UVCDrawers == null) || (UVCDrawers.Length == 0))
  1065. { // IUVCDrawerが割り当てられていないときはtrue(接続されたUVC機器を使用する)を返す
  1066. return true;
  1067. }
  1068. else
  1069. {
  1070. bool hasDrawer = false;
  1071. foreach (var drawer in UVCDrawers)
  1072. {
  1073. if (drawer is IUVCDrawer)
  1074. {
  1075. hasDrawer = true;
  1076. if ((drawer as IUVCDrawer).OnUVCAttachEvent(this, device))
  1077. { // どれか1つのIUVCDrawerがtrueを返せばtrue(接続されたUVC機器を使用する)を返す
  1078. return true;
  1079. }
  1080. }
  1081. }
  1082. // IUVCDrawerが割り当てられていないときはtrue(接続されたUVC機器を使用する)を返す
  1083. return !hasDrawer;
  1084. }
  1085. }
  1086. /**
  1087. * UVC機器が取り外されたときの処理の実体
  1088. * @param info
  1089. */
  1090. private void HandleOnDetachEvent(UVCDevice device/*NonNull*/)
  1091. {
  1092. if ((UVCDrawers != null) && (UVCDrawers.Length > 0))
  1093. {
  1094. foreach (var drawer in UVCDrawers)
  1095. {
  1096. if (drawer is IUVCDrawer)
  1097. {
  1098. (drawer as IUVCDrawer).OnUVCDetachEvent(this, device);
  1099. }
  1100. }
  1101. }
  1102. }
  1103. /**
  1104. * UVC機器からの映像取得を開始した
  1105. * @param args UVC機器の識別文字列
  1106. */
  1107. void HandleOnStartPreviewEvent(CameraInfo camera)
  1108. {
  1109. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1110. Console.WriteLine($"{TAG}HandleOnStartPreviewEvent:({camera})");
  1111. #endif
  1112. if ((camera != null) && camera.IsPreviewing && (UVCDrawers != null))
  1113. {
  1114. foreach (var drawer in UVCDrawers)
  1115. {
  1116. if ((drawer is IUVCDrawer) && (drawer as IUVCDrawer).IsUVCEnabled(this, camera.device))
  1117. {
  1118. (drawer as IUVCDrawer).OnUVCStartEvent(this, camera.device, camera.previewTexture);
  1119. }
  1120. }
  1121. }
  1122. else
  1123. {
  1124. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1125. Console.WriteLine($"{TAG}HandleOnStartPreviewEvent:No UVCDrawers");
  1126. #endif
  1127. }
  1128. }
  1129. /**
  1130. * UVC機器からの映像取得を終了した
  1131. * @param args UVC機器の識別文字列
  1132. */
  1133. void HandleOnStopPreviewEvent(CameraInfo camera)
  1134. {
  1135. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1136. Console.WriteLine($"{TAG}HandleOnStopPreviewEvent:({camera})");
  1137. #endif
  1138. if (UVCDrawers != null)
  1139. {
  1140. foreach (var drawer in UVCDrawers)
  1141. {
  1142. if ((drawer is IUVCDrawer) && (drawer as IUVCDrawer).IsUVCEnabled(this, camera.device))
  1143. {
  1144. (drawer as IUVCDrawer).OnUVCStopEvent(this, camera.device);
  1145. }
  1146. }
  1147. }
  1148. }
  1149. void HandleOnStartAudioEvent(AudioInfo audio, AudioClip audioClip)
  1150. {
  1151. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1152. Console.WriteLine($"{TAG}HandleOnStartAudioEvent:({audio})");
  1153. #endif
  1154. if ((audio != null) && audio.IsStreaming && (UVCDrawers != null))
  1155. {
  1156. foreach (var drawer in UVCDrawers)
  1157. {
  1158. if ((drawer is IUVCDrawer) && (drawer as IUVCDrawer).IsUACEnabled(this, audio.device))
  1159. { // IsUACEnabledがtrueを返したIUVCDrawerだけOnUACStartEventを呼び出す
  1160. (drawer as IUVCDrawer).OnUACStartEvent(this, audio.device, audioClip);
  1161. }
  1162. }
  1163. }
  1164. }
  1165. void HandleOnStopAudioEvent(AudioInfo audio)
  1166. {
  1167. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1168. Console.WriteLine($"{TAG}HandleOnStopAudioEvent:({audio})");
  1169. #endif
  1170. if (UVCDrawers != null)
  1171. {
  1172. foreach (var drawer in UVCDrawers)
  1173. {
  1174. if ((drawer is IUVCDrawer) && (drawer as IUVCDrawer).IsUACEnabled(this, audio.device))
  1175. { // IsUACEnabledがtrueを返したIUVCDrawerだけOnUACStopEventを呼び出す
  1176. (drawer as IUVCDrawer).OnUACStopEvent(this, audio.device);
  1177. }
  1178. }
  1179. }
  1180. }
  1181. //--------------------------------------------------------------------------------
  1182. /**
  1183. * 指定したUVC識別文字列に対応するCameraInfoを取得する
  1184. * まだ登録させていなければ新規作成する
  1185. * @param deviceName UVC機器識別文字列
  1186. * @param CameraInfoを返す
  1187. */
  1188. /*NonNull*/
  1189. private CameraInfo CreateCameraIfNotExist(UVCDevice device)
  1190. {
  1191. if (!cameraInfos.ContainsKey(device.id))
  1192. {
  1193. cameraInfos[device.id] = new CameraInfo(device);
  1194. }
  1195. return cameraInfos[device.id];
  1196. }
  1197. /**
  1198. * 指定したUVC識別文字列に対応するCameraInfoを取得する
  1199. * @param device
  1200. * @param 登録してあればCameraInfoを返す、登録されていなければnull
  1201. */
  1202. /*Nullable*/
  1203. private CameraInfo GetCamera(UVCDevice device)
  1204. {
  1205. return cameraInfos.ContainsKey(device.id) ? cameraInfos[device.id] : null;
  1206. }
  1207. /**
  1208. * 指定したUVCDeviceに対応するCameraInfoを削除する
  1209. * @param device
  1210. * @param 対応するAudioInfoまたはnull
  1211. */
  1212. private CameraInfo RemoveCamera(UVCDevice device)
  1213. {
  1214. var result = GetCamera(device);
  1215. cameraInfos.Remove(device.id);
  1216. return result;
  1217. }
  1218. /**
  1219. * 指定したUVC識別文字列に対応するCameraInfoを取得する
  1220. * まだ登録させていなければ新規作成する
  1221. * @param deviceName UVC機器識別文字列
  1222. * @param CameraInfoを返す
  1223. */
  1224. /*NonNull*/
  1225. private AudioInfo CreateAudioIfNotExist(UVCDevice device)
  1226. {
  1227. if (!audioInFos.ContainsKey(device.id))
  1228. {
  1229. audioInFos[device.id] = new AudioInfo(device);
  1230. }
  1231. return audioInFos[device.id];
  1232. }
  1233. /**
  1234. * 指定したUVCDeviceに対応するAudioInfoを取得する
  1235. * @param device
  1236. * @param 登録してあればAudioInfoを返す、登録されていなければnull
  1237. */
  1238. /*Nullable*/
  1239. private AudioInfo GetAudio(UVCDevice device)
  1240. {
  1241. return audioInFos.ContainsKey(device.id) ? audioInFos[device.id] : null;
  1242. }
  1243. /**
  1244. * 指定したUVCDeviceに対応するAudioInfoを削除する
  1245. * @param device
  1246. * @param 対応するAudioInfoまたはnull
  1247. */
  1248. private AudioInfo RemoveAudio(UVCDevice device)
  1249. {
  1250. var result = GetAudio(device);
  1251. audioInFos.Remove(device.id);
  1252. return result;
  1253. }
  1254. //--------------------------------------------------------------------------------
  1255. /**
  1256. * プラグインを初期化
  1257. * パーミッションの確認を行って取得できれば実際のプラグイン初期化処理#InitPluginを呼び出す
  1258. */
  1259. private IEnumerator Initialize()
  1260. {
  1261. #if UNITY_ANDROID
  1262. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1263. Console.WriteLine($"{TAG}Initialize:");
  1264. #endif
  1265. if (AndroidUtils.CheckAndroidVersion(28))
  1266. {
  1267. yield return AndroidUtils.GrantCameraPermission((string permission, AndroidUtils.PermissionGrantResult result) =>
  1268. {
  1269. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1270. Console.WriteLine($"{TAG}OnPermission:{permission}={result}");
  1271. #endif
  1272. switch (result)
  1273. {
  1274. case AndroidUtils.PermissionGrantResult.PERMISSION_GRANT:
  1275. InitPlugin();
  1276. break;
  1277. case AndroidUtils.PermissionGrantResult.PERMISSION_DENY:
  1278. if (AndroidUtils.ShouldShowRequestPermissionRationale(AndroidUtils.PERMISSION_CAMERA))
  1279. {
  1280. // パーミッションを取得できなかった
  1281. // FIXME 説明用のダイアログ等を表示しないといけない
  1282. Debug.LogWarning($"{TAG}OnPermission:PERMISSION_DENY,没有办法获取权限");
  1283. }
  1284. break;
  1285. case AndroidUtils.PermissionGrantResult.PERMISSION_DENY_AND_NEVER_ASK_AGAIN:
  1286. Debug.Log($"{TAG} 2222222222222222222222222222");
  1287. break;
  1288. }
  1289. });
  1290. }
  1291. else
  1292. {
  1293. Debug.Log($"{TAG}OnPermission:CheckAndroidVersion(28) = false");
  1294. InitPlugin();
  1295. }
  1296. yield break;
  1297. #endif
  1298. #if UNITY_STANDALONE_WIN
  1299. yield break;
  1300. #endif
  1301. }
  1302. /**
  1303. * プラグインを初期化
  1304. * uvc-plugin-unityへの処理要求
  1305. */
  1306. private void InitPlugin()
  1307. {
  1308. #if UNITY_ANDROID
  1309. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1310. Console.WriteLine($"{TAG}InitPlugin:");
  1311. #endif
  1312. // IUVCDrawersが割り当てられているかどうかをチェック
  1313. var hasDrawer = false;
  1314. if ((UVCDrawers != null) && (UVCDrawers.Length > 0))
  1315. {
  1316. foreach (var drawer in UVCDrawers)
  1317. {
  1318. if (drawer is IUVCDrawer)
  1319. {
  1320. hasDrawer = true;
  1321. break;
  1322. }
  1323. }
  1324. }
  1325. if (!hasDrawer)
  1326. { // インスペクタでIUVCDrawerが設定されていないときは
  1327. // このスクリプトがaddされているゲームオブジェクトからの取得を試みる
  1328. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1329. Console.WriteLine($"{TAG}InitPlugin:has no IUVCDrawer, try to get from gameObject");
  1330. #endif
  1331. var drawers = GetComponents(typeof(IUVCDrawer));
  1332. if ((drawers != null) && (drawers.Length > 0))
  1333. {
  1334. UVCDrawers = new Component[drawers.Length];
  1335. int i = 0;
  1336. foreach (var drawer in drawers)
  1337. {
  1338. UVCDrawers[i++] = drawer;
  1339. }
  1340. }
  1341. }
  1342. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  1343. Console.WriteLine($"{TAG}InitPlugin:num drawers={UVCDrawers.Length}");
  1344. #endif
  1345. // aandusbのDeviceDetectorを読み込み要求
  1346. using (AndroidJavaClass clazz = new AndroidJavaClass(FQCN_DETECTOR))
  1347. {
  1348. clazz.CallStatic("initUVCDeviceDetector",
  1349. AndroidUtils.GetCurrentActivity());
  1350. }
  1351. #endif
  1352. }
  1353. //--------------------------------------------------------------------------------
  1354. // ネイティブプラグイン関係の定義・宣言
  1355. //--------------------------------------------------------------------------------
  1356. /**
  1357. * プラグインでのレンダーイベント取得用native(c/c++)関数
  1358. */
  1359. [DllImport("unityuvcplugin")]
  1360. private static extern IntPtr GetRenderEventFunc();
  1361. /**
  1362. * 初期設定
  1363. */
  1364. [DllImport("unityuvcplugin", EntryPoint = "Config")]
  1365. private static extern Int32 Config(Int32 deviceId, Int32 enabled, Int32 useFirstConfig);
  1366. /**
  1367. * 映像取得開始
  1368. */
  1369. [DllImport("unityuvcplugin", EntryPoint = "Start")]
  1370. private static extern Int32 Start(Int32 deviceId, Int32 tex);
  1371. /**
  1372. * 映像取得終了
  1373. */
  1374. [DllImport("unityuvcplugin", EntryPoint = "Stop")]
  1375. private static extern Int32 Stop(Int32 deviceId);
  1376. /**
  1377. * 映像サイズ設定
  1378. */
  1379. [DllImport("unityuvcplugin")]
  1380. private static extern Int32 Resize(Int32 deviceId, Int32 frameType, Int32 width, Int32 height);
  1381. /**
  1382. * 対応するUVCコントロール機能マスクを取得
  1383. */
  1384. [DllImport("unityuvcplugin")]
  1385. private static extern UInt64 GetCtrlSupports(Int32 deviceId);
  1386. /**
  1387. * 対応するUVCプロセッシング機能マスクを取得
  1388. */
  1389. [DllImport("unityuvcplugin")]
  1390. private static extern UInt64 GetProcSupports(Int32 deviceId);
  1391. /**
  1392. * 対応するUVCコントロール/プロセッシング機能情報を取得
  1393. */
  1394. [DllImport("unityuvcplugin", CallingConvention = CallingConvention.StdCall)]
  1395. private static extern Int32 GetCtrlInfo(Int32 deviceId, ref UVCCtrlInfo info);
  1396. /**
  1397. * 対応するUVCコントロール/プロセッシング機能設定値を取得
  1398. */
  1399. [DllImport("unityuvcplugin")]
  1400. private static extern Int32 GetCtrlValue(Int32 deviceId, UInt64 ctrl, ref Int32 value);
  1401. /**
  1402. * 対応するUVCコントロール/プロセッシング機能設定値を設定
  1403. */
  1404. [DllImport("unityuvcplugin")]
  1405. private static extern Int32 SetCtrlValue(Int32 deviceId, UInt64 ctrl, Int32 value);
  1406. /**
  1407. * UACからの音声取得開始
  1408. */
  1409. [DllImport("unityuvcplugin")]
  1410. private static extern Int32 StartUAC(Int32 deviceId);
  1411. /**
  1412. * UACからの音声取得終了
  1413. */
  1414. [DllImport("unityuvcplugin")]
  1415. private static extern Int32 StopUAC(Int32 deviceId);
  1416. /**
  1417. * 対応するUVCコントロール/プロセッシング機能情報を取得
  1418. */
  1419. [DllImport("unityuvcplugin", CallingConvention = CallingConvention.StdCall)]
  1420. private static extern Int32 GetUACInfo(Int32 deviceId, ref UACInfo info);
  1421. /**
  1422. * UACからの音声データを取得(呼び出し元スレッドを最大で500ミリ秒ブロックする)
  1423. */
  1424. [DllImport("unityuvcplugin", CallingConvention = CallingConvention.StdCall)]
  1425. private static extern Int32 GetUACFrame(Int32 deviceId, short[] data, ref Int32 dataLen, ref Int64 ptsUs);
  1426. } // UVCManager
  1427. /**
  1428. * IL2Cppだとc/c++からのコールバックにつかうデリゲーターをマーシャリングできないので
  1429. * staticなクラス・関数で処理をしないといけない。
  1430. * だだしそれだと呼び出し元のオブジェクトの関数を呼び出せないのでマネージャークラスを作成
  1431. * とりあえずはUVCManagerだけを受け付けるのでインターフェースにはしていない
  1432. */
  1433. public static class PluginCallbackManager
  1434. {
  1435. //コールバック関数の型を宣言
  1436. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  1437. public delegate void OnDeviceChangedFunc(Int32 id, IntPtr devicePtr, bool attached);
  1438. /**
  1439. * プラグインのnative側登録関数
  1440. */
  1441. [DllImport("unityuvcplugin")]
  1442. private static extern IntPtr Register(Int32 id, OnDeviceChangedFunc deviceChanged);
  1443. /**
  1444. * プラグインのnative側登録解除関数
  1445. */
  1446. [DllImport("unityuvcplugin")]
  1447. private static extern IntPtr Unregister(Int32 id);
  1448. private static Dictionary<Int32, UVCManager> sManagers = new Dictionary<Int32, UVCManager>();
  1449. /**
  1450. * 指定したUVCManagerを接続機器変化コールバックに追加
  1451. */
  1452. public static OnDeviceChangedFunc Add(UVCManager manager)
  1453. {
  1454. Int32 id = manager.GetHashCode();
  1455. OnDeviceChangedFunc onDeviceChanged = new OnDeviceChangedFunc(OnDeviceChanged);
  1456. sManagers.Add(id, manager);
  1457. Register(id, onDeviceChanged);
  1458. return onDeviceChanged;
  1459. }
  1460. /**
  1461. * 指定したUVCManagerを接続機器変化コールバックから削除
  1462. */
  1463. public static void Remove(UVCManager manager)
  1464. {
  1465. Int32 id = manager.GetHashCode();
  1466. Unregister(id);
  1467. sManagers.Remove(id);
  1468. }
  1469. [MonoPInvokeCallback(typeof(OnDeviceChangedFunc))]
  1470. public static void OnDeviceChanged(Int32 id, IntPtr devicePtr, bool attached)
  1471. {
  1472. var manager = sManagers.ContainsKey(id) ? sManagers[id] : null;
  1473. if (manager != null)
  1474. {
  1475. manager.OnDeviceChanged(devicePtr, attached);
  1476. }
  1477. }
  1478. } // PluginCallbackManager
  1479. } // namespace Serenegiant.UVC