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.

378 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, 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/peerconnectionfactory.h"
  28. #include "talk/app/webrtc/audiotrack.h"
  29. #include "talk/app/webrtc/localaudiosource.h"
  30. #include "talk/app/webrtc/localvideosource.h"
  31. #include "talk/app/webrtc/mediastreamproxy.h"
  32. #include "talk/app/webrtc/mediastreamtrackproxy.h"
  33. #include "talk/app/webrtc/peerconnection.h"
  34. #include "talk/app/webrtc/peerconnectionproxy.h"
  35. #include "talk/app/webrtc/portallocatorfactory.h"
  36. #include "talk/app/webrtc/videosourceproxy.h"
  37. #include "talk/app/webrtc/videotrack.h"
  38. #include "talk/media/devices/dummydevicemanager.h"
  39. #include "talk/media/webrtc/webrtcmediaengine.h"
  40. using talk_base::scoped_refptr;
  41. namespace {
  42. typedef talk_base::TypedMessageData<bool> InitMessageData;
  43. struct CreatePeerConnectionParams : public talk_base::MessageData {
  44. CreatePeerConnectionParams(
  45. const webrtc::PeerConnectionInterface::IceServers& configuration,
  46. const webrtc::MediaConstraintsInterface* constraints,
  47. webrtc::PortAllocatorFactoryInterface* allocator_factory,
  48. webrtc::PeerConnectionObserver* observer)
  49. : configuration(configuration),
  50. constraints(constraints),
  51. allocator_factory(allocator_factory),
  52. observer(observer) {
  53. }
  54. scoped_refptr<webrtc::PeerConnectionInterface> peerconnection;
  55. const webrtc::PeerConnectionInterface::IceServers& configuration;
  56. const webrtc::MediaConstraintsInterface* constraints;
  57. scoped_refptr<webrtc::PortAllocatorFactoryInterface> allocator_factory;
  58. webrtc::PeerConnectionObserver* observer;
  59. };
  60. struct CreatePeerConnectionParamsDeprecated : public talk_base::MessageData {
  61. CreatePeerConnectionParamsDeprecated(
  62. const std::string& configuration,
  63. webrtc::PortAllocatorFactoryInterface* allocator_factory,
  64. webrtc::PeerConnectionObserver* observer)
  65. : configuration(configuration),
  66. allocator_factory(allocator_factory),
  67. observer(observer) {
  68. }
  69. scoped_refptr<webrtc::PeerConnectionInterface> peerconnection;
  70. const std::string& configuration;
  71. scoped_refptr<webrtc::PortAllocatorFactoryInterface> allocator_factory;
  72. webrtc::PeerConnectionObserver* observer;
  73. };
  74. struct CreateAudioSourceParams : public talk_base::MessageData {
  75. explicit CreateAudioSourceParams(
  76. const webrtc::MediaConstraintsInterface* constraints)
  77. : constraints(constraints) {
  78. }
  79. const webrtc::MediaConstraintsInterface* constraints;
  80. scoped_refptr<webrtc::AudioSourceInterface> source;
  81. };
  82. struct CreateVideoSourceParams : public talk_base::MessageData {
  83. CreateVideoSourceParams(cricket::VideoCapturer* capturer,
  84. const webrtc::MediaConstraintsInterface* constraints)
  85. : capturer(capturer),
  86. constraints(constraints) {
  87. }
  88. cricket::VideoCapturer* capturer;
  89. const webrtc::MediaConstraintsInterface* constraints;
  90. scoped_refptr<webrtc::VideoSourceInterface> source;
  91. };
  92. enum {
  93. MSG_INIT_FACTORY = 1,
  94. MSG_TERMINATE_FACTORY,
  95. MSG_CREATE_PEERCONNECTION,
  96. MSG_CREATE_AUDIOSOURCE,
  97. MSG_CREATE_VIDEOSOURCE,
  98. };
  99. } // namespace
  100. namespace webrtc {
  101. scoped_refptr<PeerConnectionFactoryInterface>
  102. CreatePeerConnectionFactory() {
  103. scoped_refptr<PeerConnectionFactory> pc_factory(
  104. new talk_base::RefCountedObject<PeerConnectionFactory>());
  105. if (!pc_factory->Initialize()) {
  106. return NULL;
  107. }
  108. return pc_factory;
  109. }
  110. scoped_refptr<PeerConnectionFactoryInterface>
  111. CreatePeerConnectionFactory(talk_base::Thread* worker_thread,
  112. talk_base::Thread* signaling_thread,
  113. AudioDeviceModule* default_adm) {
  114. scoped_refptr<PeerConnectionFactory> pc_factory(
  115. new talk_base::RefCountedObject<PeerConnectionFactory>(
  116. worker_thread, signaling_thread, default_adm));
  117. if (!pc_factory->Initialize()) {
  118. return NULL;
  119. }
  120. return pc_factory;
  121. }
  122. PeerConnectionFactory::PeerConnectionFactory()
  123. : owns_ptrs_(true),
  124. signaling_thread_(new talk_base::Thread),
  125. worker_thread_(new talk_base::Thread) {
  126. bool result = signaling_thread_->Start();
  127. ASSERT(result);
  128. result = worker_thread_->Start();
  129. ASSERT(result);
  130. }
  131. PeerConnectionFactory::PeerConnectionFactory(
  132. talk_base::Thread* worker_thread,
  133. talk_base::Thread* signaling_thread,
  134. AudioDeviceModule* default_adm)
  135. : owns_ptrs_(false),
  136. signaling_thread_(signaling_thread),
  137. worker_thread_(worker_thread),
  138. default_adm_(default_adm) {
  139. ASSERT(worker_thread != NULL);
  140. ASSERT(signaling_thread != NULL);
  141. // TODO: Currently there is no way creating an external adm in
  142. // libjingle source tree. So we can 't currently assert if this is NULL.
  143. // ASSERT(default_adm != NULL);
  144. }
  145. PeerConnectionFactory::~PeerConnectionFactory() {
  146. signaling_thread_->Clear(this);
  147. signaling_thread_->Send(this, MSG_TERMINATE_FACTORY);
  148. if (owns_ptrs_) {
  149. delete signaling_thread_;
  150. delete worker_thread_;
  151. }
  152. }
  153. bool PeerConnectionFactory::Initialize() {
  154. InitMessageData result(false);
  155. signaling_thread_->Send(this, MSG_INIT_FACTORY, &result);
  156. return result.data();
  157. }
  158. void PeerConnectionFactory::OnMessage(talk_base::Message* msg) {
  159. switch (msg->message_id) {
  160. case MSG_INIT_FACTORY: {
  161. InitMessageData* pdata = static_cast<InitMessageData*> (msg->pdata);
  162. pdata->data() = Initialize_s();
  163. break;
  164. }
  165. case MSG_TERMINATE_FACTORY: {
  166. Terminate_s();
  167. break;
  168. }
  169. case MSG_CREATE_PEERCONNECTION: {
  170. CreatePeerConnectionParams* pdata =
  171. static_cast<CreatePeerConnectionParams*> (msg->pdata);
  172. pdata->peerconnection = CreatePeerConnection_s(pdata->configuration,
  173. pdata->constraints,
  174. pdata->allocator_factory,
  175. pdata->observer);
  176. break;
  177. }
  178. case MSG_CREATE_AUDIOSOURCE: {
  179. CreateAudioSourceParams* pdata =
  180. static_cast<CreateAudioSourceParams*>(msg->pdata);
  181. pdata->source = CreateAudioSource_s(pdata->constraints);
  182. break;
  183. }
  184. case MSG_CREATE_VIDEOSOURCE: {
  185. CreateVideoSourceParams* pdata =
  186. static_cast<CreateVideoSourceParams*> (msg->pdata);
  187. pdata->source = CreateVideoSource_s(pdata->capturer, pdata->constraints);
  188. break;
  189. }
  190. }
  191. }
  192. bool PeerConnectionFactory::Initialize_s() {
  193. talk_base::InitRandom(talk_base::Time());
  194. if (owns_ptrs_) {
  195. allocator_factory_ = PortAllocatorFactory::Create(worker_thread_);
  196. if (!allocator_factory_)
  197. return false;
  198. }
  199. cricket::DummyDeviceManager* device_manager(
  200. new cricket::DummyDeviceManager());
  201. // TODO: Need to make sure only one VoE is created inside
  202. // WebRtcMediaEngine.
  203. cricket::WebRtcMediaEngine* webrtc_media_engine(
  204. new cricket::WebRtcMediaEngine(default_adm_.get(),
  205. NULL)); // No secondary adm.
  206. channel_manager_.reset(new cricket::ChannelManager(
  207. webrtc_media_engine, device_manager, worker_thread_));
  208. if (!channel_manager_->Init()) {
  209. return false;
  210. }
  211. return true;
  212. }
  213. // Terminate what we created on the signaling thread.
  214. void PeerConnectionFactory::Terminate_s() {
  215. channel_manager_.reset(NULL);
  216. if (owns_ptrs_) {
  217. allocator_factory_ = NULL;
  218. }
  219. }
  220. talk_base::scoped_refptr<AudioSourceInterface>
  221. PeerConnectionFactory::CreateAudioSource_s(
  222. const MediaConstraintsInterface* constraints) {
  223. talk_base::scoped_refptr<LocalAudioSource> source(
  224. LocalAudioSource::Create(constraints));
  225. return source;
  226. }
  227. talk_base::scoped_refptr<VideoSourceInterface>
  228. PeerConnectionFactory::CreateVideoSource_s(
  229. cricket::VideoCapturer* capturer,
  230. const MediaConstraintsInterface* constraints) {
  231. talk_base::scoped_refptr<LocalVideoSource> source(
  232. LocalVideoSource::Create(channel_manager_.get(), capturer,
  233. constraints));
  234. return VideoSourceProxy::Create(signaling_thread_, source);
  235. }
  236. scoped_refptr<PeerConnectionInterface>
  237. PeerConnectionFactory::CreatePeerConnection(
  238. const PeerConnectionInterface::IceServers& configuration,
  239. const MediaConstraintsInterface* constraints,
  240. PortAllocatorFactoryInterface* allocator_factory,
  241. PeerConnectionObserver* observer) {
  242. CreatePeerConnectionParams params(configuration, constraints,
  243. allocator_factory, observer);
  244. signaling_thread_->Send(this, MSG_CREATE_PEERCONNECTION, &params);
  245. return params.peerconnection;
  246. }
  247. scoped_refptr<PeerConnectionInterface>
  248. PeerConnectionFactory::CreatePeerConnection(
  249. const PeerConnectionInterface::IceServers& configuration,
  250. const MediaConstraintsInterface* constraints,
  251. PeerConnectionObserver* observer) {
  252. return CreatePeerConnection(configuration, constraints, NULL, observer);
  253. }
  254. talk_base::scoped_refptr<PeerConnectionInterface>
  255. PeerConnectionFactory::CreatePeerConnection_s(
  256. const PeerConnectionInterface::IceServers& configuration,
  257. const MediaConstraintsInterface* constraints,
  258. PortAllocatorFactoryInterface* allocator_factory,
  259. PeerConnectionObserver* observer) {
  260. ASSERT(allocator_factory || allocator_factory_);
  261. talk_base::scoped_refptr<PeerConnection> pc(
  262. new talk_base::RefCountedObject<PeerConnection>(this));
  263. if (!pc->Initialize(
  264. configuration,
  265. constraints,
  266. allocator_factory ? allocator_factory : allocator_factory_.get(),
  267. observer)) {
  268. return NULL;
  269. }
  270. return PeerConnectionProxy::Create(signaling_thread(), pc);
  271. }
  272. scoped_refptr<LocalMediaStreamInterface>
  273. PeerConnectionFactory::CreateLocalMediaStream(
  274. const std::string& label) {
  275. return MediaStreamProxy::Create(label, signaling_thread_);
  276. }
  277. talk_base::scoped_refptr<AudioSourceInterface>
  278. PeerConnectionFactory::CreateAudioSource(
  279. const MediaConstraintsInterface* constraints) {
  280. CreateAudioSourceParams params(constraints);
  281. signaling_thread_->Send(this, MSG_CREATE_AUDIOSOURCE, &params);
  282. return params.source;
  283. }
  284. talk_base::scoped_refptr<VideoSourceInterface>
  285. PeerConnectionFactory::CreateVideoSource(
  286. cricket::VideoCapturer* capturer,
  287. const MediaConstraintsInterface* constraints) {
  288. CreateVideoSourceParams params(capturer,
  289. constraints);
  290. signaling_thread_->Send(this, MSG_CREATE_VIDEOSOURCE, &params);
  291. return params.source;
  292. }
  293. talk_base::scoped_refptr<VideoTrackInterface>
  294. PeerConnectionFactory::CreateVideoTrack(
  295. const std::string& id,
  296. VideoSourceInterface* source) {
  297. talk_base::scoped_refptr<VideoTrackInterface> track(
  298. VideoTrack::Create(id, source));
  299. return VideoTrackProxy::Create(track, signaling_thread_);
  300. }
  301. // Deprecated: Please use the version that take a source as input.
  302. // TODO(perkj) Remove when Chrome does not make use of this.
  303. scoped_refptr<LocalVideoTrackInterface>
  304. PeerConnectionFactory::CreateLocalVideoTrack(
  305. const std::string& id,
  306. cricket::VideoCapturer* video_device) {
  307. talk_base::scoped_refptr<VideoSourceInterface> source(
  308. CreateVideoSource(video_device, NULL));
  309. return CreateVideoTrack(id, source);
  310. }
  311. scoped_refptr<AudioTrackInterface> PeerConnectionFactory::CreateAudioTrack(
  312. const std::string& id,
  313. AudioSourceInterface* source) {
  314. talk_base::scoped_refptr<AudioTrackInterface> track(
  315. AudioTrack::Create(id, source));
  316. return AudioTrackProxy::Create(track, signaling_thread_);
  317. }
  318. // Deprecated: Please use the version that take a source as input.
  319. // TODO(perkj): Remove when Chrome does not make use of this.
  320. scoped_refptr<LocalAudioTrackInterface>
  321. PeerConnectionFactory::CreateLocalAudioTrack(
  322. const std::string& id,
  323. AudioDeviceModule* audio_device) {
  324. // |audio_device| have never been used in an audio track.
  325. return CreateAudioTrack(id, NULL);
  326. }
  327. cricket::ChannelManager* PeerConnectionFactory::channel_manager() {
  328. return channel_manager_.get();
  329. }
  330. talk_base::Thread* PeerConnectionFactory::signaling_thread() {
  331. return signaling_thread_;
  332. }
  333. talk_base::Thread* PeerConnectionFactory::worker_thread() {
  334. return worker_thread_;
  335. }
  336. } // namespace webrtc