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.

302 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/media/base/capturemanager.h"
  28. #include <algorithm>
  29. #include "talk/base/logging.h"
  30. #include "talk/media/base/videoprocessor.h"
  31. #include "talk/media/base/videorenderer.h"
  32. namespace cricket {
  33. const VideoFormatPod VideoCapturerState::kDefaultCaptureFormat = {
  34. 640, 360, FPS_TO_INTERVAL(30), FOURCC_ANY};
  35. VideoCapturerState::VideoCapturerState(CaptureRenderAdapter* adapter)
  36. : adapter_(adapter),
  37. start_count_(1) {
  38. }
  39. VideoCapturerState* VideoCapturerState::Create(VideoCapturer* video_capturer) {
  40. CaptureRenderAdapter* adapter = CaptureRenderAdapter::Create(video_capturer);
  41. if (!adapter) {
  42. return NULL;
  43. }
  44. return new VideoCapturerState(adapter);
  45. }
  46. void VideoCapturerState::AddCaptureResolution(
  47. const VideoFormat& desired_format) {
  48. for (CaptureFormats::iterator iter = capture_formats_.begin();
  49. iter != capture_formats_.end(); ++iter) {
  50. if (desired_format == iter->video_format) {
  51. ++(iter->format_ref_count);
  52. return;
  53. }
  54. }
  55. CaptureResolutionInfo capture_resolution = {desired_format, 1};
  56. capture_formats_.push_back(capture_resolution);
  57. }
  58. bool VideoCapturerState::RemoveCaptureResolution(
  59. const VideoFormat& format) {
  60. for (CaptureFormats::iterator iter = capture_formats_.begin();
  61. iter != capture_formats_.end(); ++iter) {
  62. if (format == iter->video_format) {
  63. --(iter->format_ref_count);
  64. if (iter->format_ref_count == 0) {
  65. capture_formats_.erase(iter);
  66. }
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. VideoFormat VideoCapturerState::GetHighestFormat(
  73. VideoCapturer* video_capturer) const {
  74. VideoFormat highest_format(0, 0, VideoFormat::FpsToInterval(1), FOURCC_ANY);
  75. if (capture_formats_.empty()) {
  76. VideoFormat default_format(kDefaultCaptureFormat);
  77. return default_format;
  78. }
  79. for (CaptureFormats::const_iterator iter = capture_formats_.begin();
  80. iter != capture_formats_.end(); ++iter) {
  81. if (iter->video_format.width > highest_format.width) {
  82. highest_format.width = iter->video_format.width;
  83. }
  84. if (iter->video_format.height > highest_format.height) {
  85. highest_format.height = iter->video_format.height;
  86. }
  87. if (iter->video_format.interval < highest_format.interval) {
  88. highest_format.interval = iter->video_format.interval;
  89. }
  90. }
  91. return highest_format;
  92. }
  93. int VideoCapturerState::IncCaptureStartRef() {
  94. return ++start_count_;
  95. }
  96. int VideoCapturerState::DecCaptureStartRef() {
  97. if (start_count_ > 0) {
  98. // Start count may be 0 if a capturer was added but never started.
  99. --start_count_;
  100. }
  101. return start_count_;
  102. }
  103. CaptureManager::~CaptureManager() {
  104. while (!capture_states_.empty()) {
  105. // There may have been multiple calls to StartVideoCapture which means that
  106. // an equal number of calls to StopVideoCapture must be made. Note that
  107. // StopVideoCapture will remove the element from |capture_states_| when a
  108. // successfull stop has been made.
  109. UnregisterVideoCapturer(capture_states_.begin()->second);
  110. }
  111. }
  112. bool CaptureManager::StartVideoCapture(VideoCapturer* video_capturer,
  113. const VideoFormat& desired_format) {
  114. if (desired_format.width == 0 || desired_format.height == 0) {
  115. return false;
  116. }
  117. if (!video_capturer) {
  118. return false;
  119. }
  120. VideoCapturerState* capture_state = GetCaptureState(video_capturer);
  121. if (capture_state) {
  122. const int ref_count = capture_state->IncCaptureStartRef();
  123. if (ref_count < 1) {
  124. ASSERT(false);
  125. }
  126. // VideoCapturer has already been started. Don't start listening to
  127. // callbacks since that has already been done.
  128. capture_state->AddCaptureResolution(desired_format);
  129. return true;
  130. }
  131. if (!RegisterVideoCapturer(video_capturer)) {
  132. return false;
  133. }
  134. capture_state = GetCaptureState(video_capturer);
  135. ASSERT(capture_state != NULL);
  136. capture_state->AddCaptureResolution(desired_format);
  137. if (!StartWithBestCaptureFormat(capture_state, video_capturer)) {
  138. UnregisterVideoCapturer(capture_state);
  139. return false;
  140. }
  141. return true;
  142. }
  143. bool CaptureManager::StopVideoCapture(VideoCapturer* video_capturer,
  144. const VideoFormat& format) {
  145. VideoCapturerState* capture_state = GetCaptureState(video_capturer);
  146. if (!capture_state) {
  147. return false;
  148. }
  149. if (!capture_state->RemoveCaptureResolution(format)) {
  150. return false;
  151. }
  152. if (capture_state->DecCaptureStartRef() == 0) {
  153. // Unregistering cannot fail as capture_state is not NULL.
  154. UnregisterVideoCapturer(capture_state);
  155. }
  156. return true;
  157. }
  158. bool CaptureManager::AddVideoRenderer(VideoCapturer* video_capturer,
  159. VideoRenderer* video_renderer) {
  160. if (!video_capturer || !video_renderer) {
  161. return false;
  162. }
  163. CaptureRenderAdapter* adapter = GetAdapter(video_capturer);
  164. if (!adapter) {
  165. return false;
  166. }
  167. return adapter->AddRenderer(video_renderer);
  168. }
  169. bool CaptureManager::RemoveVideoRenderer(VideoCapturer* video_capturer,
  170. VideoRenderer* video_renderer) {
  171. if (!video_capturer || !video_renderer) {
  172. return false;
  173. }
  174. CaptureRenderAdapter* adapter = GetAdapter(video_capturer);
  175. if (!adapter) {
  176. return false;
  177. }
  178. return adapter->RemoveRenderer(video_renderer);
  179. }
  180. bool CaptureManager::AddVideoProcessor(VideoCapturer* video_capturer,
  181. VideoProcessor* video_processor) {
  182. if (!video_capturer || !video_processor) {
  183. return false;
  184. }
  185. if (!IsCapturerRegistered(video_capturer)) {
  186. return false;
  187. }
  188. video_capturer->AddVideoProcessor(video_processor);
  189. return true;
  190. }
  191. bool CaptureManager::RemoveVideoProcessor(VideoCapturer* video_capturer,
  192. VideoProcessor* video_processor) {
  193. if (!video_capturer || !video_processor) {
  194. return false;
  195. }
  196. if (!IsCapturerRegistered(video_capturer)) {
  197. return false;
  198. }
  199. return video_capturer->RemoveVideoProcessor(video_processor);
  200. }
  201. bool CaptureManager::IsCapturerRegistered(VideoCapturer* video_capturer) const {
  202. return GetCaptureState(video_capturer) != NULL;
  203. }
  204. bool CaptureManager::RegisterVideoCapturer(VideoCapturer* video_capturer) {
  205. VideoCapturerState* capture_state = VideoCapturerState::Create(
  206. video_capturer);
  207. if (!capture_state) {
  208. return false;
  209. }
  210. capture_states_[video_capturer] = capture_state;
  211. SignalCapturerStateChange.repeat(video_capturer->SignalStateChange);
  212. return true;
  213. }
  214. void CaptureManager::UnregisterVideoCapturer(
  215. VideoCapturerState* capture_state) {
  216. VideoCapturer* video_capturer = capture_state->GetVideoCapturer();
  217. capture_states_.erase(video_capturer);
  218. delete capture_state;
  219. // When unregistering a VideoCapturer, the CaptureManager needs to unregister
  220. // from all state change callbacks from the VideoCapturer. E.g. to avoid
  221. // problems with multiple callbacks if registering the same VideoCapturer
  222. // multiple times. The VideoCapturer will update the capturer state. However,
  223. // this is done through Post-calls which means it may happen at any time. If
  224. // the CaptureManager no longer is listening to the VideoCapturer it will not
  225. // receive those callbacks. Here it is made sure that the the callback is
  226. // indeed sent by letting the ChannelManager do the signaling. The downside is
  227. // that the callback may happen before the VideoCapturer is stopped. However,
  228. // for the CaptureManager it doesn't matter as it will no longer receive any
  229. // frames from the VideoCapturer.
  230. SignalCapturerStateChange.stop(video_capturer->SignalStateChange);
  231. video_capturer->Stop();
  232. SignalCapturerStateChange(video_capturer, CS_STOPPED);
  233. }
  234. bool CaptureManager::StartWithBestCaptureFormat(
  235. VideoCapturerState* capture_state,
  236. VideoCapturer* video_capturer) {
  237. VideoFormat highest_asked_format =
  238. capture_state->GetHighestFormat(video_capturer);
  239. VideoFormat capture_format;
  240. if (!video_capturer->GetBestCaptureFormat(highest_asked_format,
  241. &capture_format)) {
  242. LOG(LS_WARNING) << "Unsupported format:"
  243. << " width=" << highest_asked_format.width
  244. << " height=" << highest_asked_format.height
  245. << ". Supported formats are:";
  246. const std::vector<VideoFormat>* formats =
  247. video_capturer->GetSupportedFormats();
  248. for (std::vector<VideoFormat>::const_iterator i = formats->begin();
  249. i != formats->end(); ++i) {
  250. const VideoFormat& format = *i;
  251. LOG(LS_WARNING) << " " << GetFourccName(format.fourcc) << ":"
  252. << format.width << "x" << format.height << "x"
  253. << format.framerate();
  254. }
  255. }
  256. return video_capturer->StartCapturing(capture_format);
  257. }
  258. VideoCapturerState* CaptureManager::GetCaptureState(
  259. VideoCapturer* video_capturer) const {
  260. CaptureStates::const_iterator iter = capture_states_.find(video_capturer);
  261. if (iter == capture_states_.end()) {
  262. return NULL;
  263. }
  264. return iter->second;
  265. }
  266. CaptureRenderAdapter* CaptureManager::GetAdapter(
  267. VideoCapturer* video_capturer) const {
  268. VideoCapturerState* capture_state = GetCaptureState(video_capturer);
  269. if (!capture_state) {
  270. return NULL;
  271. }
  272. return capture_state->adapter();
  273. }
  274. } // namespace cricket