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.

202 lines
6.6 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/app/webrtc/videosourceproxy.h"
  28. #include "talk/media/base/videocapturer.h"
  29. namespace {
  30. enum {
  31. MSG_REGISTER_OBSERVER = 1,
  32. MSG_UNREGISTER_OBSERVER,
  33. MSG_STATE,
  34. MSG_ADD_SINK,
  35. MSG_REMOVE_SINK,
  36. MSG_GET_VIDEO_CAPTURER,
  37. MSG_OPTIONS,
  38. MSG_TERMINATE
  39. };
  40. typedef talk_base::TypedMessageData<webrtc::ObserverInterface*>
  41. ObserverMessageData;
  42. typedef talk_base::TypedMessageData<cricket::VideoRenderer*> SinkMessageData;
  43. typedef talk_base::TypedMessageData<cricket::VideoCapturer*>
  44. CapturerMessageData;
  45. typedef talk_base::TypedMessageData<const cricket::VideoOptions*>
  46. OptionsMessageData;
  47. typedef talk_base::TypedMessageData
  48. <webrtc::MediaSourceInterface::SourceState> StateMessageData;
  49. } // namespace
  50. namespace webrtc {
  51. VideoSourceInterface* VideoSourceProxy::Create(
  52. talk_base::Thread* signaling_thread,
  53. VideoSourceInterface* source) {
  54. ASSERT(signaling_thread != NULL);
  55. ASSERT(source != NULL);
  56. return new talk_base::RefCountedObject<VideoSourceProxy>(signaling_thread,
  57. source);
  58. }
  59. VideoSourceProxy::VideoSourceProxy(talk_base::Thread* signaling_thread,
  60. VideoSourceInterface* source)
  61. : signaling_thread_(signaling_thread),
  62. source_(source) {
  63. }
  64. VideoSourceProxy::~VideoSourceProxy() {
  65. // Since VideoSourceInterface is reference counted we don't know which
  66. // application thread that holds on to the last reference.
  67. // Here we ensure the real implementation is always released on the
  68. // signaling thread.
  69. signaling_thread_->Send(this, MSG_TERMINATE);
  70. }
  71. cricket::VideoCapturer* VideoSourceProxy::GetVideoCapturer() {
  72. if (!signaling_thread_->IsCurrent()) {
  73. CapturerMessageData msg(NULL);
  74. signaling_thread_->Send(const_cast<VideoSourceProxy*>(this),
  75. MSG_GET_VIDEO_CAPTURER, &msg);
  76. return msg.data();
  77. }
  78. return source_->GetVideoCapturer();
  79. }
  80. void VideoSourceProxy::AddSink(cricket::VideoRenderer* output) {
  81. if (!signaling_thread_->IsCurrent()) {
  82. SinkMessageData msg(output);
  83. signaling_thread_->Send(const_cast<VideoSourceProxy*>(this),
  84. MSG_ADD_SINK, &msg);
  85. return;
  86. }
  87. return source_->AddSink(output);
  88. }
  89. void VideoSourceProxy::RemoveSink(cricket::VideoRenderer* output) {
  90. if (!signaling_thread_->IsCurrent()) {
  91. SinkMessageData msg(output);
  92. signaling_thread_->Send(const_cast<VideoSourceProxy*>(this),
  93. MSG_REMOVE_SINK, &msg);
  94. return;
  95. }
  96. return source_->RemoveSink(output);
  97. }
  98. const cricket::VideoOptions* VideoSourceProxy::options() const {
  99. if (!signaling_thread_->IsCurrent()) {
  100. OptionsMessageData msg(NULL);
  101. signaling_thread_->Send(const_cast<VideoSourceProxy*>(this),
  102. MSG_OPTIONS, &msg);
  103. return msg.data();
  104. }
  105. return source_->options();
  106. }
  107. MediaSourceInterface::SourceState VideoSourceProxy::state() const {
  108. if (!signaling_thread_->IsCurrent()) {
  109. StateMessageData msg(MediaSourceInterface::kEnded);
  110. signaling_thread_->Send(const_cast<VideoSourceProxy*>(this),
  111. MSG_STATE, &msg);
  112. return msg.data();
  113. }
  114. return source_->state();
  115. }
  116. void VideoSourceProxy::RegisterObserver(ObserverInterface* observer) {
  117. if (!signaling_thread_->IsCurrent()) {
  118. ObserverMessageData msg(observer);
  119. signaling_thread_->Send(this, MSG_REGISTER_OBSERVER, &msg);
  120. return;
  121. }
  122. return source_->RegisterObserver(observer);
  123. }
  124. void VideoSourceProxy::UnregisterObserver(ObserverInterface* observer) {
  125. if (!signaling_thread_->IsCurrent()) {
  126. ObserverMessageData msg(observer);
  127. signaling_thread_->Send(this, MSG_UNREGISTER_OBSERVER, &msg);
  128. return;
  129. }
  130. return source_->UnregisterObserver(observer);
  131. }
  132. void VideoSourceProxy::OnMessage(talk_base::Message* msg) {
  133. switch (msg->message_id) {
  134. case MSG_STATE: {
  135. StateMessageData* ready_state =
  136. static_cast<StateMessageData*>(msg->pdata);
  137. ready_state->data() = source_->state();
  138. break;
  139. }
  140. case MSG_REGISTER_OBSERVER: {
  141. ObserverMessageData* observer =
  142. static_cast<ObserverMessageData*>(msg->pdata);
  143. source_->RegisterObserver(observer->data());
  144. break;
  145. }
  146. case MSG_UNREGISTER_OBSERVER: {
  147. ObserverMessageData* observer = static_cast<ObserverMessageData*>(
  148. msg->pdata);
  149. source_->UnregisterObserver(observer->data());
  150. break;
  151. }
  152. case MSG_ADD_SINK: {
  153. SinkMessageData* sink = static_cast<SinkMessageData*>(
  154. msg->pdata);
  155. source_->AddSink(sink->data());
  156. break;
  157. }
  158. case MSG_REMOVE_SINK: {
  159. SinkMessageData* sink = static_cast<SinkMessageData*>(
  160. msg->pdata);
  161. source_->RemoveSink(sink->data());
  162. break;
  163. }
  164. case MSG_GET_VIDEO_CAPTURER: {
  165. CapturerMessageData* capturer = static_cast<CapturerMessageData*>(
  166. msg->pdata);
  167. capturer->data() = source_->GetVideoCapturer();
  168. break;
  169. }
  170. case MSG_OPTIONS: {
  171. OptionsMessageData* options = static_cast<OptionsMessageData*>(
  172. msg->pdata);
  173. options->data() = source_->options();
  174. break;
  175. }
  176. case MSG_TERMINATE: {
  177. source_ = NULL;
  178. break;
  179. }
  180. }
  181. }
  182. } // namespace webrtc