OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/media_stream_dependency_factory.h" | 5 #include "content/renderer/media/media_stream_dependency_factory.h" |
6 | 6 |
| 7 #include <vector> |
| 8 |
7 #include "content/renderer/media/video_capture_module_impl.h" | 9 #include "content/renderer/media/video_capture_module_impl.h" |
8 #include "content/renderer/media/webrtc_audio_device_impl.h" | 10 #include "content/renderer/media/webrtc_audio_device_impl.h" |
9 #include "content/renderer/p2p/ipc_network_manager.h" | 11 #include "content/renderer/p2p/ipc_network_manager.h" |
10 #include "content/renderer/p2p/ipc_socket_factory.h" | 12 #include "content/renderer/p2p/ipc_socket_factory.h" |
11 #include "content/renderer/p2p/port_allocator.h" | 13 #include "content/renderer/p2p/port_allocator.h" |
12 #include "jingle/glue/thread_wrapper.h" | 14 #include "jingle/glue/thread_wrapper.h" |
13 #include "third_party/libjingle/source/talk/app/webrtcv1/peerconnection.h" | 15 #include "third_party/libjingle/source/talk/app/webrtc/peerconnection.h" |
14 #include "third_party/libjingle/source/talk/session/phone/dummydevicemanager.h" | |
15 #include "third_party/libjingle/source/talk/session/phone/webrtcmediaengine.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
17 | 17 |
| 18 static std::string ParseHostName(const std::string& hostname) { |
| 19 std::string address; |
| 20 size_t pos = hostname.find(':'); |
| 21 if (pos == std::string::npos) { |
| 22 DVLOG(1) << "No port specified."; |
| 23 } else { |
| 24 address = hostname.substr(0, pos); |
| 25 } |
| 26 return address; |
| 27 } |
| 28 |
| 29 class P2PPortAllocatorFactory : public webrtc::PortAllocatorFactoryInterface { |
| 30 public: |
| 31 P2PPortAllocatorFactory( |
| 32 content::P2PSocketDispatcher* socket_dispatcher, |
| 33 talk_base::NetworkManager* network_manager, |
| 34 talk_base::PacketSocketFactory* socket_factory) |
| 35 : socket_dispatcher_(socket_dispatcher), |
| 36 network_manager_(network_manager), |
| 37 socket_factory_(socket_factory) { |
| 38 } |
| 39 |
| 40 virtual cricket::PortAllocator* CreatePortAllocator( |
| 41 const std::vector<StunConfiguration>& stun_servers, |
| 42 const std::vector<TurnConfiguration>& turn_configurations) OVERRIDE { |
| 43 WebKit::WebFrame* web_frame = WebKit::WebFrame::frameForCurrentContext(); |
| 44 if (!web_frame) { |
| 45 LOG(ERROR) << "WebFrame is NULL."; |
| 46 return NULL; |
| 47 } |
| 48 webkit_glue::P2PTransport::Config config; |
| 49 if (stun_servers.size() > 0) { |
| 50 config.stun_server = ParseHostName(stun_servers[0].server.hostname()); |
| 51 config.stun_server_port = stun_servers[0].server.port(); |
| 52 } |
| 53 if (turn_configurations.size() > 0) { |
| 54 config.relay_server = ParseHostName( |
| 55 turn_configurations[0].server.hostname()); |
| 56 config.relay_server_port = turn_configurations[0].server.port(); |
| 57 config.relay_username = turn_configurations[0].username; |
| 58 config.relay_password = turn_configurations[0].password; |
| 59 } |
| 60 |
| 61 return new content::P2PPortAllocator(web_frame, |
| 62 socket_dispatcher_, |
| 63 network_manager_, |
| 64 socket_factory_, |
| 65 config); |
| 66 } |
| 67 |
| 68 protected: |
| 69 virtual ~P2PPortAllocatorFactory() {} |
| 70 |
| 71 private: |
| 72 // socket_dispatcher_ is a weak reference, owned by RenderView. It's valid |
| 73 // for the lifetime of RenderView. |
| 74 content::P2PSocketDispatcher* socket_dispatcher_; |
| 75 // network_manager_ and socket_factory_ are a weak references, owned by |
| 76 // MediaStreamImpl. |
| 77 talk_base::NetworkManager* network_manager_; |
| 78 talk_base::PacketSocketFactory* socket_factory_; |
| 79 }; |
| 80 |
| 81 |
18 MediaStreamDependencyFactory::MediaStreamDependencyFactory() {} | 82 MediaStreamDependencyFactory::MediaStreamDependencyFactory() {} |
19 | 83 |
20 MediaStreamDependencyFactory::~MediaStreamDependencyFactory() {} | 84 MediaStreamDependencyFactory::~MediaStreamDependencyFactory() {} |
21 | 85 |
22 cricket::WebRtcMediaEngine* | 86 bool MediaStreamDependencyFactory::CreatePeerConnectionFactory( |
23 MediaStreamDependencyFactory::CreateWebRtcMediaEngine() { | 87 talk_base::Thread* worker_thread, |
24 webrtc::AudioDeviceModule* adm = new WebRtcAudioDeviceImpl(); | 88 talk_base::Thread* signaling_thread, |
25 webrtc::AudioDeviceModule* adm_sc = new WebRtcAudioDeviceImpl(); | 89 content::P2PSocketDispatcher* socket_dispatcher, |
26 return new cricket::WebRtcMediaEngine(adm, adm_sc, NULL); | 90 talk_base::NetworkManager* network_manager, |
27 } | 91 talk_base::PacketSocketFactory* socket_factory) { |
| 92 if (!pc_factory_.get()) { |
| 93 talk_base::scoped_refptr<P2PPortAllocatorFactory> pa_factory = |
| 94 new talk_base::RefCountedObject<P2PPortAllocatorFactory>( |
| 95 socket_dispatcher, |
| 96 network_manager, |
| 97 socket_factory); |
28 | 98 |
29 bool MediaStreamDependencyFactory::CreatePeerConnectionFactory( | 99 talk_base::scoped_refptr<webrtc::PeerConnectionFactoryInterface> factory( |
30 cricket::MediaEngineInterface* media_engine, | 100 webrtc::CreatePeerConnectionFactory(worker_thread, |
31 talk_base::Thread* worker_thread) { | 101 signaling_thread, |
32 if (!pc_factory_.get()) { | 102 pa_factory.release(), |
33 scoped_ptr<webrtc::PeerConnectionFactory> factory( | 103 new WebRtcAudioDeviceImpl())); |
34 new webrtc::PeerConnectionFactory(media_engine, | 104 if (factory.get()) |
35 new cricket::DummyDeviceManager(), | 105 pc_factory_ = factory.release(); |
36 worker_thread)); | |
37 if (factory->Initialize()) | |
38 pc_factory_.reset(factory.release()); | |
39 } | 106 } |
40 return pc_factory_.get() != NULL; | 107 return pc_factory_.get() != NULL; |
41 } | 108 } |
42 | 109 |
43 void MediaStreamDependencyFactory::DeletePeerConnectionFactory() { | 110 void MediaStreamDependencyFactory::ReleasePeerConnectionFactory() { |
44 pc_factory_.reset(); | 111 if (pc_factory_.get()) |
| 112 pc_factory_ = NULL; |
45 } | 113 } |
46 | 114 |
47 bool MediaStreamDependencyFactory::PeerConnectionFactoryCreated() { | 115 bool MediaStreamDependencyFactory::PeerConnectionFactoryCreated() { |
48 return pc_factory_.get() != NULL; | 116 return pc_factory_.get() != NULL; |
49 } | 117 } |
50 | 118 |
51 cricket::PortAllocator* MediaStreamDependencyFactory::CreatePortAllocator( | 119 talk_base::scoped_refptr<webrtc::PeerConnectionInterface> |
52 content::P2PSocketDispatcher* socket_dispatcher, | 120 MediaStreamDependencyFactory::CreatePeerConnection( |
53 talk_base::NetworkManager* network_manager, | 121 const std::string& config, |
54 talk_base::PacketSocketFactory* socket_factory, | 122 webrtc::PeerConnectionObserver* observer) { |
55 const webkit_glue::P2PTransport::Config& config) { | 123 return pc_factory_->CreatePeerConnection(config, observer); |
56 WebKit::WebFrame* web_frame = WebKit::WebFrame::frameForCurrentContext(); | |
57 if (!web_frame) { | |
58 DVLOG(1) << "WebFrame is NULL."; | |
59 return NULL; | |
60 } | |
61 return new content::P2PPortAllocator(web_frame, | |
62 socket_dispatcher, | |
63 network_manager, | |
64 socket_factory, | |
65 config); | |
66 } | 124 } |
67 | 125 |
68 webrtc::PeerConnection* MediaStreamDependencyFactory::CreatePeerConnection( | 126 talk_base::scoped_refptr<webrtc::LocalMediaStreamInterface> |
69 cricket::PortAllocator* port_allocator, | 127 MediaStreamDependencyFactory::CreateLocalMediaStream( |
70 talk_base::Thread* signaling_thread) { | 128 const std::string& label) { |
71 return pc_factory_->CreatePeerConnection(port_allocator, signaling_thread); | 129 return pc_factory_->CreateLocalMediaStream(label); |
72 } | 130 } |
| 131 |
| 132 talk_base::scoped_refptr<webrtc::LocalVideoTrackInterface> |
| 133 MediaStreamDependencyFactory::CreateLocalVideoTrack( |
| 134 const std::string& label, |
| 135 cricket::VideoCapturer* video_device) { |
| 136 return pc_factory_->CreateLocalVideoTrack(label, video_device); |
| 137 } |
| 138 |
| 139 talk_base::scoped_refptr<webrtc::LocalAudioTrackInterface> |
| 140 MediaStreamDependencyFactory::CreateLocalAudioTrack( |
| 141 const std::string& label, |
| 142 webrtc::AudioDeviceModule* audio_device) { |
| 143 return pc_factory_->CreateLocalAudioTrack(label, audio_device); |
| 144 } |
OLD | NEW |