WebcamCaptureDemo.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using UnityEngine;
  2. using System.Collections;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProMovieCapture.Demos
  7. {
  8. /// <summary>
  9. /// Allows the user to select from a list of webcams and creates a capture instance for the webcam recording.
  10. /// Currently only a single webcam can be captured at once.
  11. /// </summary>
  12. public class WebcamCaptureDemo : MonoBehaviour
  13. {
  14. #pragma warning disable 0414 // x is assigned but its value is never used
  15. [SerializeField] GUISkin _skin = null;
  16. [SerializeField] GameObject _prefab = null;
  17. [SerializeField] int _webcamResolutionWidth = 640;
  18. [SerializeField] int _webcamResolutionHeight = 480;
  19. [SerializeField] int _webcamFrameRate = 30;
  20. #pragma warning restore 0414
  21. #if AVPRO_MOVIECAPTURE_WEBCAMTEXTURE_SUPPORT
  22. private class Instance
  23. {
  24. public string name;
  25. public WebCamTexture texture;
  26. public CaptureFromWebCamTexture capture;
  27. public CaptureGUI gui;
  28. }
  29. // State
  30. private Instance[] _instances;
  31. private int _selectedWebcamIndex = -1;
  32. private IEnumerator Start()
  33. {
  34. // Make sure we're authorised for using the camera. On iOS the OS will forcibly
  35. // close the application if authorisation has not been granted. Make sure the
  36. // "Camera Usage Description" field has been filled in the player settings.
  37. // This needs to be done first otherwise no cameras will be reported.
  38. if (!Application.HasUserAuthorization(UserAuthorization.WebCam))
  39. {
  40. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  41. }
  42. // Make sure we're authorised for using the microphone. On iOS the OS will forcibly
  43. // close the application if authorisation has not been granted. Make sure the
  44. // "Microphone Usage Description" field has been filled in the player settings.
  45. // if (_capture.AudioCaptureSource == AudioCaptureSource.Microphone)
  46. {
  47. if (!Application.HasUserAuthorization(UserAuthorization.Microphone))
  48. {
  49. yield return Application.RequestUserAuthorization(UserAuthorization.Microphone);
  50. }
  51. if (Application.HasUserAuthorization(UserAuthorization.Microphone))
  52. {
  53. // On iOS modified the audio session to allow recording from the microphone.
  54. NativePlugin.SetMicrophoneRecordingHint(true);
  55. }
  56. }
  57. // Create instance data per webcam
  58. int numCams = WebCamTexture.devices.Length;
  59. _instances = new Instance[numCams];
  60. GameObject go = this.gameObject;
  61. Instance instance = new Instance();
  62. instance.name = WebCamTexture.devices[0].name;
  63. instance.capture = go.GetComponent<CaptureFromWebCamTexture>();
  64. instance.capture.FilenamePrefix = "Demo4Webcam-" + 0;
  65. //instance.gui = go.GetComponent<CaptureGUI>();
  66. //instance.gui.ShowUI = false;
  67. _instances[0] = instance;
  68. if (numCams > 0)
  69. {
  70. SelectWebcam(0);
  71. }
  72. }
  73. private void StartWebcam(Instance instance)
  74. {
  75. // NOTE: WebcamTexture can be slow for high resolutions, this can cause issues with audio-video sync.
  76. // Our plugins AVPro Live Camera or AVPro DeckLink can be used to capture high resolution devices
  77. #if UNITY_2018_4_OR_NEWER
  78. Debug.Log($"_webcamResolutionWidth: {_webcamResolutionWidth}, _webcamResolutionHeight: {_webcamResolutionHeight}, _webcamFrameRate: {_webcamFrameRate}");
  79. #endif
  80. instance.texture = new WebCamTexture(instance.name, _webcamResolutionWidth, _webcamResolutionHeight, _webcamFrameRate);
  81. instance.texture.Play();
  82. if (instance.texture.isPlaying)
  83. {
  84. instance.capture.WebCamTexture = instance.texture;
  85. Debug.Log(string.Format("Running webcam in mode {0}x{1}", instance.texture.width, instance.texture.height));
  86. }
  87. else
  88. {
  89. Debug.Log(string.Format("Unable to start webcam in mode {0}x{1}@{2}", _webcamResolutionWidth, _webcamResolutionHeight, _webcamFrameRate));
  90. StopWebcam(instance);
  91. }
  92. }
  93. private void StopWebcam(Instance instance)
  94. {
  95. if (instance.texture != null)
  96. {
  97. if (instance.capture != null && instance.capture.IsCapturing())
  98. {
  99. instance.capture.WebCamTexture = null;
  100. instance.capture.StopCapture();
  101. }
  102. instance.texture.Stop();
  103. Destroy(instance.texture);
  104. instance.texture = null;
  105. }
  106. _selectedWebcamIndex = -1;
  107. }
  108. private void OnDestroy()
  109. {
  110. for (int i = 0; i < _instances.Length; i++)
  111. {
  112. StopWebcam(_instances[i]);
  113. }
  114. }
  115. private void SelectWebcam(int index)
  116. {
  117. // Stop any currently
  118. if (_selectedWebcamIndex >= 0)
  119. {
  120. StopWebcam(_instances[_selectedWebcamIndex]);
  121. _selectedWebcamIndex = -1;
  122. }
  123. if (index >= 0)
  124. {
  125. _selectedWebcamIndex = index;
  126. for (int j = 0; j < _instances.Length; j++)
  127. {
  128. // _instances[j].gui.ShowUI = (j == _selectedWebcamIndex);
  129. }
  130. StartWebcam(_instances[_selectedWebcamIndex]);
  131. }
  132. }
  133. private void OnGUI()
  134. {
  135. GUI.skin = _skin;
  136. GUILayout.BeginArea(new Rect(Screen.width - 512, 0, 512, Screen.height));
  137. GUILayout.BeginVertical();
  138. GUILayout.Label("Select webcam:");
  139. for (int i = 0; i < _instances.Length; i++)
  140. {
  141. Instance webcam = _instances[i];
  142. GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
  143. if (webcam.capture.IsCapturing())
  144. {
  145. float t = Mathf.PingPong(Time.timeSinceLevelLoad, 0.25f) * 4f;
  146. GUI.backgroundColor = Color.Lerp(GUI.backgroundColor, Color.white, t);
  147. GUI.color = Color.Lerp(Color.red, Color.white, t);
  148. }
  149. if (_selectedWebcamIndex == i)
  150. {
  151. GUI.backgroundColor = Color.green;
  152. }
  153. if (GUILayout.Button(webcam.name, GUILayout.Width(200), GUILayout.ExpandWidth(true)))
  154. {
  155. if (_selectedWebcamIndex != i)
  156. {
  157. SelectWebcam(i);
  158. }
  159. else
  160. {
  161. StopWebcam(webcam);
  162. }
  163. }
  164. GUI.backgroundColor = Color.white;
  165. GUI.color = Color.white;
  166. if (webcam.texture != null)
  167. {
  168. Rect camRect = GUILayoutUtility.GetRect(256, 256.0f / (webcam.texture.width / (float)webcam.texture.height));
  169. GUI.DrawTexture(camRect, webcam.texture);
  170. }
  171. else
  172. {
  173. GUILayout.Label(string.Empty, GUILayout.MinWidth(256.0f), GUILayout.MaxWidth(256.0f), GUILayout.ExpandWidth(false));
  174. }
  175. GUILayout.EndHorizontal();
  176. }
  177. GUILayout.EndVertical();
  178. GUILayout.EndArea();
  179. }
  180. #else
  181. void Start()
  182. {
  183. Debug.LogError("[AVProMovieCapture] To use WebCamTexture capture component/demo you must add the string AVPRO_MOVIECAPTURE_WEBCAMTEXTURE_SUPPORT must be added to `Scriping Define Symbols` in `Player Settings > Other Settings > Script Compilation`");
  184. }
  185. #endif // AVPRO_MOVIECAPTURE_WEBCAMTEXTURE_SUPPORT
  186. }
  187. }