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.

131 lines
5.3 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. // The CaptureManager class manages VideoCapturers to make it possible to share
  28. // the same VideoCapturers across multiple instances. E.g. if two instances of
  29. // some class want to listen to same VideoCapturer they can't individually stop
  30. // and start capturing as doing so will affect the other instance.
  31. // The class employs reference counting on starting and stopping of capturing of
  32. // frames such that if anyone is still listening it will not be stopped. The
  33. // class also provides APIs for attaching VideoRenderers to a specific capturer
  34. // such that the VideoRenderers are fed frames directly from the capturer. In
  35. // addition, these frames can be altered before being sent to the capturers by
  36. // way of VideoProcessors.
  37. // CaptureManager is Thread-unsafe. This means that none of its APIs may be
  38. // called concurrently. Note that callbacks are called by the VideoCapturer's
  39. // thread which is normally a separate unmarshalled thread and thus normally
  40. // require lock protection.
  41. #ifndef TALK_MEDIA_BASE_CAPTUREMANAGER_H_
  42. #define TALK_MEDIA_BASE_CAPTUREMANAGER_H_
  43. #include <map>
  44. #include <vector>
  45. #include "talk/base/sigslotrepeater.h"
  46. #include "talk/media/base/capturerenderadapter.h"
  47. #include "talk/media/base/videocapturer.h"
  48. #include "talk/media/base/videocommon.h"
  49. namespace cricket {
  50. class VideoProcessor;
  51. class VideoRenderer;
  52. // CaptureManager helper class.
  53. class VideoCapturerState {
  54. public:
  55. static const VideoFormatPod kDefaultCaptureFormat;
  56. static VideoCapturerState* Create(VideoCapturer* video_capturer);
  57. ~VideoCapturerState() {}
  58. void AddCaptureResolution(const VideoFormat& desired_format);
  59. bool RemoveCaptureResolution(const VideoFormat& format);
  60. VideoFormat GetHighestFormat(VideoCapturer* video_capturer) const;
  61. int IncCaptureStartRef();
  62. int DecCaptureStartRef();
  63. CaptureRenderAdapter* adapter() { return adapter_.get(); }
  64. VideoCapturer* GetVideoCapturer() { return adapter()->video_capturer(); }
  65. private:
  66. struct CaptureResolutionInfo {
  67. VideoFormat video_format;
  68. int format_ref_count;
  69. };
  70. typedef std::vector<CaptureResolutionInfo> CaptureFormats;
  71. explicit VideoCapturerState(CaptureRenderAdapter* adapter);
  72. talk_base::scoped_ptr<CaptureRenderAdapter> adapter_;
  73. int start_count_;
  74. CaptureFormats capture_formats_;
  75. };
  76. class CaptureManager : public sigslot::has_slots<> {
  77. public:
  78. CaptureManager() {}
  79. virtual ~CaptureManager();
  80. virtual bool StartVideoCapture(VideoCapturer* video_capturer,
  81. const VideoFormat& desired_format);
  82. virtual bool StopVideoCapture(VideoCapturer* video_capturer,
  83. const VideoFormat& format);
  84. virtual bool AddVideoRenderer(VideoCapturer* video_capturer,
  85. VideoRenderer* video_renderer);
  86. virtual bool RemoveVideoRenderer(VideoCapturer* video_capturer,
  87. VideoRenderer* video_renderer);
  88. virtual bool AddVideoProcessor(VideoCapturer* video_capturer,
  89. VideoProcessor* video_processor);
  90. virtual bool RemoveVideoProcessor(VideoCapturer* video_capturer,
  91. VideoProcessor* video_processor);
  92. sigslot::repeater2<VideoCapturer*, CaptureState> SignalCapturerStateChange;
  93. private:
  94. typedef std::map<VideoCapturer*, VideoCapturerState*> CaptureStates;
  95. bool IsCapturerRegistered(VideoCapturer* video_capturer) const;
  96. bool RegisterVideoCapturer(VideoCapturer* video_capturer);
  97. void UnregisterVideoCapturer(VideoCapturerState* capture_state);
  98. bool StartWithBestCaptureFormat(VideoCapturerState* capture_info,
  99. VideoCapturer* video_capturer);
  100. VideoCapturerState* GetCaptureState(VideoCapturer* video_capturer) const;
  101. CaptureRenderAdapter* GetAdapter(VideoCapturer* video_capturer) const;
  102. CaptureStates capture_states_;
  103. };
  104. } // namespace cricket
  105. #endif // TALK_MEDIA_BASE_CAPTUREMANAGER_H_