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.

1010 lines
34 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004 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 <string>
  28. #include "talk/base/helpers.h"
  29. #include "talk/base/logging.h"
  30. #include "talk/base/thread.h"
  31. #include "talk/base/window.h"
  32. #include "talk/media/base/screencastid.h"
  33. #include "talk/p2p/base/parsing.h"
  34. #include "talk/session/media/call.h"
  35. #include "talk/session/media/mediasessionclient.h"
  36. namespace cricket {
  37. const uint32 MSG_CHECKAUTODESTROY = 1;
  38. const uint32 MSG_TERMINATECALL = 2;
  39. const uint32 MSG_PLAYDTMF = 3;
  40. namespace {
  41. const int kDTMFDelay = 300; // msec
  42. const size_t kMaxDTMFDigits = 30;
  43. const int kSendToVoicemailTimeout = 1000*20;
  44. const int kNoVoicemailTimeout = 1000*180;
  45. const int kMediaMonitorInterval = 1000*15;
  46. // In order to be the same as the server-side switching, this must be 100.
  47. const int kAudioMonitorPollPeriodMillis = 100;
  48. // V is a pointer type.
  49. template<class K, class V>
  50. V FindOrNull(const std::map<K, V>& map,
  51. const K& key) {
  52. typename std::map<K, V>::const_iterator it = map.find(key);
  53. return (it != map.end()) ? it->second : NULL;
  54. }
  55. }
  56. Call::Call(MediaSessionClient* session_client)
  57. : id_(talk_base::CreateRandomId()),
  58. session_client_(session_client),
  59. local_renderer_(NULL),
  60. has_video_(false),
  61. has_data_(false),
  62. muted_(false),
  63. video_muted_(false),
  64. send_to_voicemail_(true),
  65. playing_dtmf_(false) {
  66. }
  67. Call::~Call() {
  68. while (media_session_map_.begin() != media_session_map_.end()) {
  69. Session* session = media_session_map_.begin()->second.session;
  70. RemoveSession(session);
  71. session_client_->session_manager()->DestroySession(session);
  72. }
  73. talk_base::Thread::Current()->Clear(this);
  74. }
  75. Session* Call::InitiateSession(const buzz::Jid& to,
  76. const buzz::Jid& initiator,
  77. const CallOptions& options) {
  78. const SessionDescription* offer = session_client_->CreateOffer(options);
  79. Session* session = session_client_->CreateSession(this);
  80. session->set_initiator_name(initiator.Str());
  81. AddSession(session, offer);
  82. session->Initiate(to.Str(), offer);
  83. // After this timeout, terminate the call because the callee isn't
  84. // answering
  85. session_client_->session_manager()->signaling_thread()->Clear(this,
  86. MSG_TERMINATECALL);
  87. session_client_->session_manager()->signaling_thread()->PostDelayed(
  88. send_to_voicemail_ ? kSendToVoicemailTimeout : kNoVoicemailTimeout,
  89. this, MSG_TERMINATECALL);
  90. return session;
  91. }
  92. void Call::IncomingSession(Session* session, const SessionDescription* offer) {
  93. AddSession(session, offer);
  94. // Make sure the session knows about the incoming ssrcs. This needs to be done
  95. // prior to the SignalSessionState call, because that may trigger handling of
  96. // these new SSRCs, so they need to be registered before then.
  97. UpdateRemoteMediaStreams(session, offer->contents(), false);
  98. // Missed the first state, the initiate, which is needed by
  99. // call_client.
  100. SignalSessionState(this, session, Session::STATE_RECEIVEDINITIATE);
  101. }
  102. void Call::AcceptSession(Session* session,
  103. const cricket::CallOptions& options) {
  104. MediaSessionMap::iterator it = media_session_map_.find(session->id());
  105. if (it != media_session_map_.end()) {
  106. it->second.session->Accept(
  107. session_client_->CreateAnswer(session->remote_description(), options));
  108. }
  109. }
  110. void Call::RejectSession(Session* session) {
  111. // Assume polite decline.
  112. MediaSessionMap::iterator it = media_session_map_.find(session->id());
  113. if (it != media_session_map_.end())
  114. it->second.session->Reject(STR_TERMINATE_DECLINE);
  115. }
  116. void Call::TerminateSession(Session* session) {
  117. MediaSessionMap::iterator it = media_session_map_.find(session->id());
  118. if (it != media_session_map_.end()) {
  119. // Assume polite terminations.
  120. it->second.session->Terminate();
  121. }
  122. }
  123. void Call::Terminate() {
  124. // Copy the list so that we can iterate over it in a stable way
  125. std::vector<Session*> sessions = this->sessions();
  126. // There may be more than one session to terminate
  127. std::vector<Session*>::iterator it;
  128. for (it = sessions.begin(); it != sessions.end(); ++it) {
  129. TerminateSession(*it);
  130. }
  131. }
  132. bool Call::SendViewRequest(Session* session,
  133. const ViewRequest& view_request) {
  134. StaticVideoViews::const_iterator it;
  135. for (it = view_request.static_video_views.begin();
  136. it != view_request.static_video_views.end(); ++it) {
  137. StreamParams found_stream;
  138. bool found = false;
  139. MediaStreams* recv_streams = GetMediaStreams(session);
  140. if (recv_streams)
  141. found = recv_streams->GetVideoStreamBySsrc(it->ssrc, &found_stream);
  142. if (!found) {
  143. LOG(LS_WARNING) <<
  144. "Tried sending view request for bad ssrc: " << it->ssrc;
  145. return false;
  146. }
  147. }
  148. XmlElements elems;
  149. WriteError error;
  150. if (!WriteJingleViewRequest(CN_VIDEO, view_request, &elems, &error)) {
  151. LOG(LS_ERROR) << "Couldn't write out view request: " << error.text;
  152. return false;
  153. }
  154. return session->SendInfoMessage(elems);
  155. }
  156. void Call::SetLocalRenderer(VideoRenderer* renderer) {
  157. local_renderer_ = renderer;
  158. if (session_client_->GetFocus() == this) {
  159. session_client_->channel_manager()->SetLocalRenderer(renderer);
  160. }
  161. }
  162. void Call::SetVideoRenderer(Session* session, uint32 ssrc,
  163. VideoRenderer* renderer) {
  164. VideoChannel* video_channel = GetVideoChannel(session);
  165. if (video_channel) {
  166. video_channel->SetRenderer(ssrc, renderer);
  167. LOG(LS_INFO) << "Set renderer of ssrc " << ssrc
  168. << " to " << renderer << ".";
  169. } else {
  170. LOG(LS_INFO) << "Failed to set renderer of ssrc " << ssrc << ".";
  171. }
  172. }
  173. void Call::OnMessage(talk_base::Message* message) {
  174. switch (message->message_id) {
  175. case MSG_CHECKAUTODESTROY:
  176. // If no more sessions for this call, delete it
  177. if (media_session_map_.empty())
  178. session_client_->DestroyCall(this);
  179. break;
  180. case MSG_TERMINATECALL:
  181. // Signal to the user that a timeout has happened and the call should
  182. // be sent to voicemail.
  183. if (send_to_voicemail_) {
  184. SignalSetupToCallVoicemail();
  185. }
  186. // Callee didn't answer - terminate call
  187. Terminate();
  188. break;
  189. case MSG_PLAYDTMF:
  190. ContinuePlayDTMF();
  191. }
  192. }
  193. std::vector<Session*> Call::sessions() {
  194. std::vector<Session*> sessions;
  195. MediaSessionMap::iterator it;
  196. for (it = media_session_map_.begin(); it != media_session_map_.end(); ++it)
  197. sessions.push_back(it->second.session);
  198. return sessions;
  199. }
  200. bool Call::AddSession(Session* session, const SessionDescription* offer) {
  201. bool succeeded = true;
  202. MediaSession media_session;
  203. media_session.session = session;
  204. media_session.voice_channel = NULL;
  205. media_session.video_channel = NULL;
  206. media_session.data_channel = NULL;
  207. media_session.recv_streams = NULL;
  208. const ContentInfo* audio_offer = GetFirstAudioContent(offer);
  209. const ContentInfo* video_offer = GetFirstVideoContent(offer);
  210. const ContentInfo* data_offer = GetFirstDataContent(offer);
  211. has_video_ = (video_offer != NULL);
  212. has_data_ = (data_offer != NULL);
  213. ASSERT(audio_offer != NULL);
  214. // Create voice channel and start a media monitor.
  215. media_session.voice_channel =
  216. session_client_->channel_manager()->CreateVoiceChannel(
  217. session, audio_offer->name, has_video_);
  218. // voice_channel can be NULL in case of NullVoiceEngine.
  219. if (media_session.voice_channel) {
  220. media_session.voice_channel->SignalMediaMonitor.connect(
  221. this, &Call::OnMediaMonitor);
  222. media_session.voice_channel->StartMediaMonitor(kMediaMonitorInterval);
  223. } else {
  224. succeeded = false;
  225. }
  226. // If desired, create video channel and start a media monitor.
  227. if (has_video_ && succeeded) {
  228. media_session.video_channel =
  229. session_client_->channel_manager()->CreateVideoChannel(
  230. session, video_offer->name, true, media_session.voice_channel);
  231. // video_channel can be NULL in case of NullVideoEngine.
  232. if (media_session.video_channel) {
  233. media_session.video_channel->SignalMediaMonitor.connect(
  234. this, &Call::OnMediaMonitor);
  235. media_session.video_channel->StartMediaMonitor(kMediaMonitorInterval);
  236. } else {
  237. succeeded = false;
  238. }
  239. }
  240. // If desired, create data channel
  241. if (has_data_ && succeeded) {
  242. bool rtcp = false;
  243. media_session.data_channel =
  244. session_client_->channel_manager()->CreateDataChannel(
  245. session, data_offer->name, rtcp);
  246. if (media_session.data_channel) {
  247. media_session.data_channel->SignalDataReceived.connect(
  248. this, &Call::OnDataReceived);
  249. } else {
  250. succeeded = false;
  251. }
  252. }
  253. if (succeeded) {
  254. // Add session to list, create channels for this session.
  255. media_session.recv_streams = new MediaStreams;
  256. media_session_map_[session->id()] = media_session;
  257. session->SignalState.connect(this, &Call::OnSessionState);
  258. session->SignalError.connect(this, &Call::OnSessionError);
  259. session->SignalInfoMessage.connect(
  260. this, &Call::OnSessionInfoMessage);
  261. session->SignalRemoteDescriptionUpdate.connect(
  262. this, &Call::OnRemoteDescriptionUpdate);
  263. session->SignalReceivedTerminateReason
  264. .connect(this, &Call::OnReceivedTerminateReason);
  265. // If this call has the focus, enable this session's channels.
  266. if (session_client_->GetFocus() == this) {
  267. EnableSessionChannels(session, true);
  268. }
  269. // Signal client.
  270. SignalAddSession(this, session);
  271. }
  272. return succeeded;
  273. }
  274. void Call::RemoveSession(Session* session) {
  275. MediaSessionMap::iterator it = media_session_map_.find(session->id());
  276. if (it == media_session_map_.end())
  277. return;
  278. // Remove all the screencasts, if they haven't been already.
  279. while (!it->second.started_screencasts.empty()) {
  280. uint32 ssrc = it->second.started_screencasts.begin()->first;
  281. if (!StopScreencastWithoutSendingUpdate(it->second.session, ssrc)) {
  282. LOG(LS_ERROR) << "Unable to stop screencast with ssrc " << ssrc;
  283. ASSERT(false);
  284. }
  285. }
  286. // Destroy video channel
  287. VideoChannel* video_channel = it->second.video_channel;
  288. if (video_channel != NULL)
  289. session_client_->channel_manager()->DestroyVideoChannel(video_channel);
  290. // Destroy voice channel
  291. VoiceChannel* voice_channel = it->second.voice_channel;
  292. if (voice_channel != NULL)
  293. session_client_->channel_manager()->DestroyVoiceChannel(voice_channel);
  294. // Destroy data channel
  295. DataChannel* data_channel = it->second.data_channel;
  296. if (data_channel != NULL)
  297. session_client_->channel_manager()->DestroyDataChannel(data_channel);
  298. delete it->second.recv_streams;
  299. media_session_map_.erase(it);
  300. // Destroy speaker monitor
  301. StopSpeakerMonitor(session);
  302. // Signal client
  303. SignalRemoveSession(this, session);
  304. // The call auto destroys when the last session is removed
  305. talk_base::Thread::Current()->Post(this, MSG_CHECKAUTODESTROY);
  306. }
  307. VoiceChannel* Call::GetVoiceChannel(Session* session) const {
  308. MediaSessionMap::const_iterator it = media_session_map_.find(session->id());
  309. return (it != media_session_map_.end()) ? it->second.voice_channel : NULL;
  310. }
  311. VideoChannel* Call::GetVideoChannel(Session* session) const {
  312. MediaSessionMap::const_iterator it = media_session_map_.find(session->id());
  313. return (it != media_session_map_.end()) ? it->second.video_channel : NULL;
  314. }
  315. DataChannel* Call::GetDataChannel(Session* session) const {
  316. MediaSessionMap::const_iterator it = media_session_map_.find(session->id());
  317. return (it != media_session_map_.end()) ? it->second.data_channel : NULL;
  318. }
  319. MediaStreams* Call::GetMediaStreams(Session* session) const {
  320. MediaSessionMap::const_iterator it = media_session_map_.find(session->id());
  321. return (it != media_session_map_.end()) ? it->second.recv_streams : NULL;
  322. }
  323. void Call::EnableChannels(bool enable) {
  324. MediaSessionMap::iterator it;
  325. for (it = media_session_map_.begin(); it != media_session_map_.end(); ++it) {
  326. EnableSessionChannels(it->second.session, enable);
  327. }
  328. session_client_->channel_manager()->SetLocalRenderer(
  329. (enable) ? local_renderer_ : NULL);
  330. }
  331. void Call::EnableSessionChannels(Session* session, bool enable) {
  332. MediaSessionMap::iterator it = media_session_map_.find(session->id());
  333. if (it == media_session_map_.end())
  334. return;
  335. VoiceChannel* voice_channel = it->second.voice_channel;
  336. VideoChannel* video_channel = it->second.video_channel;
  337. DataChannel* data_channel = it->second.data_channel;
  338. if (voice_channel != NULL)
  339. voice_channel->Enable(enable);
  340. if (video_channel != NULL)
  341. video_channel->Enable(enable);
  342. if (data_channel != NULL)
  343. data_channel->Enable(enable);
  344. }
  345. void Call::Mute(bool mute) {
  346. muted_ = mute;
  347. MediaSessionMap::iterator it;
  348. for (it = media_session_map_.begin(); it != media_session_map_.end(); ++it) {
  349. if (it->second.voice_channel != NULL)
  350. it->second.voice_channel->MuteStream(0, mute);
  351. }
  352. }
  353. void Call::MuteVideo(bool mute) {
  354. video_muted_ = mute;
  355. MediaSessionMap::iterator it;
  356. for (it = media_session_map_.begin(); it != media_session_map_.end(); ++it) {
  357. if (it->second.video_channel != NULL)
  358. it->second.video_channel->MuteStream(0, mute);
  359. }
  360. }
  361. void Call::SendData(Session* session,
  362. const SendDataParams& params,
  363. const std::string& data) {
  364. DataChannel* data_channel = GetDataChannel(session);
  365. if (!data_channel) {
  366. LOG(LS_WARNING) << "Could not send data: no data channel.";
  367. return;
  368. }
  369. data_channel->SendData(params, data);
  370. }
  371. void Call::PressDTMF(int event) {
  372. // Queue up this digit
  373. if (queued_dtmf_.size() < kMaxDTMFDigits) {
  374. LOG(LS_INFO) << "Call::PressDTMF(" << event << ")";
  375. queued_dtmf_.push_back(event);
  376. if (!playing_dtmf_) {
  377. ContinuePlayDTMF();
  378. }
  379. }
  380. }
  381. cricket::VideoFormat ScreencastFormatFromFps(int fps) {
  382. // The capturer pretty much ignore this, but just in case we give it
  383. // a resolution big enough to cover any expected desktop. In any
  384. // case, it can't be 0x0, or the CaptureManager will fail to use it.
  385. return cricket::VideoFormat(
  386. 1, 1,
  387. cricket::VideoFormat::FpsToInterval(fps), cricket::FOURCC_ANY);
  388. }
  389. bool Call::StartScreencast(Session* session,
  390. const std::string& stream_name, uint32 ssrc,
  391. const ScreencastId& screencastid, int fps) {
  392. MediaSessionMap::iterator it = media_session_map_.find(session->id());
  393. if (it == media_session_map_.end()) {
  394. return false;
  395. }
  396. VideoChannel *video_channel = GetVideoChannel(session);
  397. if (!video_channel) {
  398. LOG(LS_WARNING) << "Cannot add screencast"
  399. << " because there is no video channel.";
  400. return false;
  401. }
  402. VideoCapturer *capturer = video_channel->AddScreencast(ssrc, screencastid);
  403. if (capturer == NULL) {
  404. LOG(LS_WARNING) << "Could not create screencast capturer.";
  405. return false;
  406. }
  407. VideoFormat format = ScreencastFormatFromFps(fps);
  408. if (!session_client_->channel_manager()->StartVideoCapture(
  409. capturer, format)) {
  410. LOG(LS_WARNING) << "Could not start video capture.";
  411. video_channel->RemoveScreencast(ssrc);
  412. return false;
  413. }
  414. if (!video_channel->SetCapturer(ssrc, capturer)) {
  415. LOG(LS_WARNING) << "Could not start sending screencast.";
  416. session_client_->channel_manager()->StopVideoCapture(
  417. capturer, ScreencastFormatFromFps(fps));
  418. video_channel->RemoveScreencast(ssrc);
  419. }
  420. // TODO(pthatcher): Once the CaptureManager has a nicer interface
  421. // for removing captures (such as having StartCapture return a
  422. // handle), remove this StartedCapture stuff.
  423. it->second.started_screencasts.insert(
  424. std::make_pair(ssrc, StartedCapture(capturer, format)));
  425. // TODO(pthatcher): Verify we aren't re-using an existing name or
  426. // ssrc.
  427. StreamParams stream;
  428. stream.name = stream_name;
  429. stream.ssrcs.push_back(ssrc);
  430. VideoContentDescription* video = CreateVideoStreamUpdate(stream);
  431. // TODO(pthatcher): Wait until view request before sending video.
  432. video_channel->SetLocalContent(video, CA_UPDATE);
  433. SendVideoStreamUpdate(session, video);
  434. return true;
  435. }
  436. bool Call::StopScreencast(Session* session,
  437. const std::string& stream_name, uint32 ssrc) {
  438. if (!StopScreencastWithoutSendingUpdate(session, ssrc)) {
  439. return false;
  440. }
  441. VideoChannel *video_channel = GetVideoChannel(session);
  442. if (!video_channel) {
  443. LOG(LS_WARNING) << "Cannot add screencast"
  444. << " because there is no video channel.";
  445. return false;
  446. }
  447. StreamParams stream;
  448. stream.name = stream_name;
  449. // No ssrcs
  450. VideoContentDescription* video = CreateVideoStreamUpdate(stream);
  451. video_channel->SetLocalContent(video, CA_UPDATE);
  452. SendVideoStreamUpdate(session, video);
  453. return true;
  454. }
  455. bool Call::StopScreencastWithoutSendingUpdate(
  456. Session* session, uint32 ssrc) {
  457. MediaSessionMap::iterator it = media_session_map_.find(session->id());
  458. if (it == media_session_map_.end()) {
  459. return false;
  460. }
  461. VideoChannel *video_channel = GetVideoChannel(session);
  462. if (!video_channel) {
  463. LOG(LS_WARNING) << "Cannot remove screencast"
  464. << " because there is no video channel.";
  465. return false;
  466. }
  467. StartedScreencastMap::const_iterator screencast_iter =
  468. it->second.started_screencasts.find(ssrc);
  469. if (screencast_iter == it->second.started_screencasts.end()) {
  470. LOG(LS_WARNING) << "Could not stop screencast " << ssrc
  471. << " because there is no capturer.";
  472. return false;
  473. }
  474. VideoCapturer* capturer = screencast_iter->second.capturer;
  475. VideoFormat format = screencast_iter->second.format;
  476. video_channel->SetCapturer(ssrc, NULL);
  477. if (!session_client_->channel_manager()->StopVideoCapture(
  478. capturer, format)) {
  479. LOG(LS_WARNING) << "Could not stop screencast " << ssrc
  480. << " because could not stop capture.";
  481. return false;
  482. }
  483. video_channel->RemoveScreencast(ssrc);
  484. it->second.started_screencasts.erase(ssrc);
  485. return true;
  486. }
  487. VideoContentDescription* Call::CreateVideoStreamUpdate(
  488. const StreamParams& stream) {
  489. VideoContentDescription* video = new VideoContentDescription();
  490. video->set_multistream(true);
  491. video->set_partial(true);
  492. video->AddStream(stream);
  493. return video;
  494. }
  495. void Call::SendVideoStreamUpdate(
  496. Session* session, VideoContentDescription* video) {
  497. const ContentInfo* video_info =
  498. GetFirstVideoContent(session->local_description());
  499. if (video_info == NULL) {
  500. LOG(LS_WARNING) << "Cannot send stream update for video.";
  501. delete video;
  502. return;
  503. }
  504. std::vector<ContentInfo> contents;
  505. contents.push_back(
  506. ContentInfo(video_info->name, video_info->type, video));
  507. session->SendDescriptionInfoMessage(contents);
  508. }
  509. void Call::ContinuePlayDTMF() {
  510. playing_dtmf_ = false;
  511. // Check to see if we have a queued tone
  512. if (queued_dtmf_.size() > 0) {
  513. playing_dtmf_ = true;
  514. int tone = queued_dtmf_.front();
  515. queued_dtmf_.pop_front();
  516. LOG(LS_INFO) << "Call::ContinuePlayDTMF(" << tone << ")";
  517. for (MediaSessionMap::iterator it = media_session_map_.begin();
  518. it != media_session_map_.end(); ++it) {
  519. if (it->second.voice_channel != NULL) {
  520. it->second.voice_channel->PressDTMF(tone, true);
  521. }
  522. }
  523. // Post a message to play the next tone or at least clear the playing_dtmf_
  524. // bit.
  525. talk_base::Thread::Current()->PostDelayed(kDTMFDelay, this, MSG_PLAYDTMF);
  526. }
  527. }
  528. void Call::Join(Call* call, bool enable) {
  529. for (MediaSessionMap::iterator it = call->media_session_map_.begin();
  530. it != call->media_session_map_.end(); ++it) {
  531. // Shouldn't already exist.
  532. ASSERT(media_session_map_.find(it->first) == media_session_map_.end());
  533. media_session_map_[it->first] = it->second;
  534. it->second.session->SignalState.connect(this, &Call::OnSessionState);
  535. it->second.session->SignalError.connect(this, &Call::OnSessionError);
  536. it->second.session->SignalReceivedTerminateReason
  537. .connect(this, &Call::OnReceivedTerminateReason);
  538. EnableSessionChannels(it->second.session, enable);
  539. }
  540. // Moved all the sessions over, so the other call should no longer have any.
  541. call->media_session_map_.clear();
  542. }
  543. void Call::StartConnectionMonitor(Session* session, int cms) {
  544. VoiceChannel* voice_channel = GetVoiceChannel(session);
  545. if (voice_channel) {
  546. voice_channel->SignalConnectionMonitor.connect(this,
  547. &Call::OnConnectionMonitor);
  548. voice_channel->StartConnectionMonitor(cms);
  549. }
  550. VideoChannel* video_channel = GetVideoChannel(session);
  551. if (video_channel) {
  552. video_channel->SignalConnectionMonitor.connect(this,
  553. &Call::OnConnectionMonitor);
  554. video_channel->StartConnectionMonitor(cms);
  555. }
  556. }
  557. void Call::StopConnectionMonitor(Session* session) {
  558. VoiceChannel* voice_channel = GetVoiceChannel(session);
  559. if (voice_channel) {
  560. voice_channel->StopConnectionMonitor();
  561. voice_channel->SignalConnectionMonitor.disconnect(this);
  562. }
  563. VideoChannel* video_channel = GetVideoChannel(session);
  564. if (video_channel) {
  565. video_channel->StopConnectionMonitor();
  566. video_channel->SignalConnectionMonitor.disconnect(this);
  567. }
  568. }
  569. void Call::StartAudioMonitor(Session* session, int cms) {
  570. VoiceChannel* voice_channel = GetVoiceChannel(session);
  571. if (voice_channel) {
  572. voice_channel->SignalAudioMonitor.connect(this, &Call::OnAudioMonitor);
  573. voice_channel->StartAudioMonitor(cms);
  574. }
  575. }
  576. void Call::StopAudioMonitor(Session* session) {
  577. VoiceChannel* voice_channel = GetVoiceChannel(session);
  578. if (voice_channel) {
  579. voice_channel->StopAudioMonitor();
  580. voice_channel->SignalAudioMonitor.disconnect(this);
  581. }
  582. }
  583. bool Call::IsAudioMonitorRunning(Session* session) {
  584. VoiceChannel* voice_channel = GetVoiceChannel(session);
  585. if (voice_channel) {
  586. return voice_channel->IsAudioMonitorRunning();
  587. } else {
  588. return false;
  589. }
  590. }
  591. void Call::StartSpeakerMonitor(Session* session) {
  592. if (speaker_monitor_map_.find(session->id()) == speaker_monitor_map_.end()) {
  593. if (!IsAudioMonitorRunning(session)) {
  594. StartAudioMonitor(session, kAudioMonitorPollPeriodMillis);
  595. }
  596. CurrentSpeakerMonitor* speaker_monitor =
  597. new cricket::CurrentSpeakerMonitor(this, session);
  598. speaker_monitor->SignalUpdate.connect(this, &Call::OnSpeakerMonitor);
  599. speaker_monitor->Start();
  600. speaker_monitor_map_[session->id()] = speaker_monitor;
  601. } else {
  602. LOG(LS_WARNING) << "Already started speaker monitor for session "
  603. << session->id() << ".";
  604. }
  605. }
  606. void Call::StopSpeakerMonitor(Session* session) {
  607. if (speaker_monitor_map_.find(session->id()) == speaker_monitor_map_.end()) {
  608. LOG(LS_WARNING) << "Speaker monitor for session "
  609. << session->id() << " already stopped.";
  610. } else {
  611. CurrentSpeakerMonitor* monitor = speaker_monitor_map_[session->id()];
  612. monitor->Stop();
  613. speaker_monitor_map_.erase(session->id());
  614. delete monitor;
  615. }
  616. }
  617. void Call::OnConnectionMonitor(VoiceChannel* channel,
  618. const std::vector<ConnectionInfo> &infos) {
  619. SignalConnectionMonitor(this, infos);
  620. }
  621. void Call::OnMediaMonitor(VoiceChannel* channel, const VoiceMediaInfo& info) {
  622. last_voice_media_info_ = info;
  623. SignalMediaMonitor(this, info);
  624. }
  625. void Call::OnAudioMonitor(VoiceChannel* channel, const AudioInfo& info) {
  626. SignalAudioMonitor(this, info);
  627. }
  628. void Call::OnSpeakerMonitor(CurrentSpeakerMonitor* monitor, uint32 ssrc) {
  629. Session* session = static_cast<Session*>(monitor->session());
  630. MediaStreams* recv_streams = GetMediaStreams(session);
  631. if (recv_streams) {
  632. StreamParams stream;
  633. recv_streams->GetAudioStreamBySsrc(ssrc, &stream);
  634. SignalSpeakerMonitor(this, session, stream);
  635. }
  636. }
  637. void Call::OnConnectionMonitor(VideoChannel* channel,
  638. const std::vector<ConnectionInfo> &infos) {
  639. SignalVideoConnectionMonitor(this, infos);
  640. }
  641. void Call::OnMediaMonitor(VideoChannel* channel, const VideoMediaInfo& info) {
  642. SignalVideoMediaMonitor(this, info);
  643. }
  644. void Call::OnDataReceived(DataChannel* channel,
  645. const ReceiveDataParams& params,
  646. const std::string& data) {
  647. SignalDataReceived(this, params, data);
  648. }
  649. uint32 Call::id() {
  650. return id_;
  651. }
  652. void Call::OnSessionState(BaseSession* base_session, BaseSession::State state) {
  653. Session* session = static_cast<Session*>(base_session);
  654. switch (state) {
  655. case Session::STATE_RECEIVEDACCEPT:
  656. UpdateRemoteMediaStreams(session,
  657. session->remote_description()->contents(), false);
  658. session_client_->session_manager()->signaling_thread()->Clear(this,
  659. MSG_TERMINATECALL);
  660. break;
  661. case Session::STATE_RECEIVEDREJECT:
  662. case Session::STATE_RECEIVEDTERMINATE:
  663. session_client_->session_manager()->signaling_thread()->Clear(this,
  664. MSG_TERMINATECALL);
  665. break;
  666. default:
  667. break;
  668. }
  669. SignalSessionState(this, session, state);
  670. }
  671. void Call::OnSessionError(BaseSession* base_session, Session::Error error) {
  672. session_client_->session_manager()->signaling_thread()->Clear(this,
  673. MSG_TERMINATECALL);
  674. SignalSessionError(this, static_cast<Session*>(base_session), error);
  675. }
  676. void Call::OnSessionInfoMessage(Session* session,
  677. const buzz::XmlElement* action_elem) {
  678. if (!IsJingleViewRequest(action_elem)) {
  679. return;
  680. }
  681. ViewRequest view_request;
  682. ParseError error;
  683. if (!ParseJingleViewRequest(action_elem, &view_request, &error)) {
  684. LOG(LS_WARNING) << "Failed to parse view request: " << error.text;
  685. return;
  686. }
  687. VideoChannel* video_channel = GetVideoChannel(session);
  688. if (video_channel == NULL) {
  689. LOG(LS_WARNING) << "Ignore view request since we have no video channel.";
  690. return;
  691. }
  692. if (!video_channel->ApplyViewRequest(view_request)) {
  693. LOG(LS_WARNING) << "Failed to ApplyViewRequest.";
  694. }
  695. }
  696. void Call::OnRemoteDescriptionUpdate(BaseSession* base_session,
  697. const ContentInfos& updated_contents) {
  698. Session* session = static_cast<Session*>(base_session);
  699. const ContentInfo* audio_content = GetFirstAudioContent(updated_contents);
  700. if (audio_content) {
  701. const AudioContentDescription* audio_update =
  702. static_cast<const AudioContentDescription*>(audio_content->description);
  703. if (!audio_update->codecs().empty()) {
  704. UpdateVoiceChannelRemoteContent(session, audio_update);
  705. }
  706. }
  707. const ContentInfo* video_content = GetFirstVideoContent(updated_contents);
  708. if (video_content) {
  709. const VideoContentDescription* video_update =
  710. static_cast<const VideoContentDescription*>(video_content->description);
  711. if (!video_update->codecs().empty()) {
  712. UpdateVideoChannelRemoteContent(session, video_update);
  713. }
  714. }
  715. const ContentInfo* data_content = GetFirstDataContent(updated_contents);
  716. if (data_content) {
  717. const DataContentDescription* data_update =
  718. static_cast<const DataContentDescription*>(data_content->description);
  719. if (!data_update->codecs().empty()) {
  720. UpdateDataChannelRemoteContent(session, data_update);
  721. }
  722. }
  723. UpdateRemoteMediaStreams(session, updated_contents, true);
  724. }
  725. bool Call::UpdateVoiceChannelRemoteContent(
  726. Session* session, const AudioContentDescription* audio) {
  727. VoiceChannel* voice_channel = GetVoiceChannel(session);
  728. if (!voice_channel->SetRemoteContent(audio, CA_UPDATE)) {
  729. LOG(LS_ERROR) << "Failure in audio SetRemoteContent with CA_UPDATE";
  730. session->SetError(BaseSession::ERROR_CONTENT);
  731. return false;
  732. }
  733. return true;
  734. }
  735. bool Call::UpdateVideoChannelRemoteContent(
  736. Session* session, const VideoContentDescription* video) {
  737. VideoChannel* video_channel = GetVideoChannel(session);
  738. if (!video_channel->SetRemoteContent(video, CA_UPDATE)) {
  739. LOG(LS_ERROR) << "Failure in video SetRemoteContent with CA_UPDATE";
  740. session->SetError(BaseSession::ERROR_CONTENT);
  741. return false;
  742. }
  743. return true;
  744. }
  745. bool Call::UpdateDataChannelRemoteContent(
  746. Session* session, const DataContentDescription* data) {
  747. DataChannel* data_channel = GetDataChannel(session);
  748. if (!data_channel->SetRemoteContent(data, CA_UPDATE)) {
  749. LOG(LS_ERROR) << "Failure in data SetRemoteContent with CA_UPDATE";
  750. session->SetError(BaseSession::ERROR_CONTENT);
  751. return false;
  752. }
  753. return true;
  754. }
  755. void Call::UpdateRemoteMediaStreams(Session* session,
  756. const ContentInfos& updated_contents,
  757. bool update_channels) {
  758. MediaStreams* recv_streams = GetMediaStreams(session);
  759. if (!recv_streams)
  760. return;
  761. cricket::MediaStreams added_streams;
  762. cricket::MediaStreams removed_streams;
  763. const ContentInfo* audio_content = GetFirstAudioContent(updated_contents);
  764. if (audio_content) {
  765. const AudioContentDescription* audio_update =
  766. static_cast<const AudioContentDescription*>(audio_content->description);
  767. UpdateRecvStreams(audio_update->streams(),
  768. update_channels ? GetVoiceChannel(session) : NULL,
  769. recv_streams->mutable_audio(),
  770. added_streams.mutable_audio(),
  771. removed_streams.mutable_audio());
  772. }
  773. const ContentInfo* video_content = GetFirstVideoContent(updated_contents);
  774. if (video_content) {
  775. const VideoContentDescription* video_update =
  776. static_cast<const VideoContentDescription*>(video_content->description);
  777. UpdateRecvStreams(video_update->streams(),
  778. update_channels ? GetVideoChannel(session) : NULL,
  779. recv_streams->mutable_video(),
  780. added_streams.mutable_video(),
  781. removed_streams.mutable_video());
  782. }
  783. const ContentInfo* data_content = GetFirstDataContent(updated_contents);
  784. if (data_content) {
  785. const DataContentDescription* data_update =
  786. static_cast<const DataContentDescription*>(data_content->description);
  787. UpdateRecvStreams(data_update->streams(),
  788. update_channels ? GetDataChannel(session) : NULL,
  789. recv_streams->mutable_data(),
  790. added_streams.mutable_data(),
  791. removed_streams.mutable_data());
  792. }
  793. if (!added_streams.empty() || !removed_streams.empty()) {
  794. SignalMediaStreamsUpdate(this, session, added_streams, removed_streams);
  795. }
  796. }
  797. void FindStreamChanges(const std::vector<StreamParams>& streams,
  798. const std::vector<StreamParams>& updates,
  799. std::vector<StreamParams>* added_streams,
  800. std::vector<StreamParams>* removed_streams) {
  801. for (std::vector<StreamParams>::const_iterator update = updates.begin();
  802. update != updates.end(); ++update) {
  803. StreamParams stream;
  804. if (GetStreamByNickAndName(streams, update->nick, update->name, &stream)) {
  805. if (!update->has_ssrcs()) {
  806. removed_streams->push_back(stream);
  807. }
  808. } else {
  809. // There's a bug on reflector that will send <stream>s even
  810. // though there is not ssrc (which means there isn't really a
  811. // stream). To work around it, we simply ignore new <stream>s
  812. // that don't have any ssrcs.
  813. if (update->has_ssrcs()) {
  814. added_streams->push_back(*update);
  815. }
  816. }
  817. }
  818. }
  819. void Call::UpdateRecvStreams(const std::vector<StreamParams>& update_streams,
  820. BaseChannel* channel,
  821. std::vector<StreamParams>* recv_streams,
  822. std::vector<StreamParams>* added_streams,
  823. std::vector<StreamParams>* removed_streams) {
  824. FindStreamChanges(*recv_streams,
  825. update_streams, added_streams, removed_streams);
  826. AddRecvStreams(*added_streams,
  827. channel, recv_streams);
  828. RemoveRecvStreams(*removed_streams,
  829. channel, recv_streams);
  830. }
  831. void Call::AddRecvStreams(const std::vector<StreamParams>& added_streams,
  832. BaseChannel* channel,
  833. std::vector<StreamParams>* recv_streams) {
  834. std::vector<StreamParams>::const_iterator stream;
  835. for (stream = added_streams.begin();
  836. stream != added_streams.end();
  837. ++stream) {
  838. AddRecvStream(*stream, channel, recv_streams);
  839. }
  840. }
  841. void Call::AddRecvStream(const StreamParams& stream,
  842. BaseChannel* channel,
  843. std::vector<StreamParams>* recv_streams) {
  844. if (channel && stream.has_ssrcs()) {
  845. channel->AddRecvStream(stream);
  846. }
  847. recv_streams->push_back(stream);
  848. }
  849. void Call::RemoveRecvStreams(const std::vector<StreamParams>& removed_streams,
  850. BaseChannel* channel,
  851. std::vector<StreamParams>* recv_streams) {
  852. std::vector<StreamParams>::const_iterator stream;
  853. for (stream = removed_streams.begin();
  854. stream != removed_streams.end();
  855. ++stream) {
  856. RemoveRecvStream(*stream, channel, recv_streams);
  857. }
  858. }
  859. void Call::RemoveRecvStream(const StreamParams& stream,
  860. BaseChannel* channel,
  861. std::vector<StreamParams>* recv_streams) {
  862. if (channel && stream.has_ssrcs()) {
  863. // TODO(pthatcher): Change RemoveRecvStream to take a stream argument.
  864. channel->RemoveRecvStream(stream.first_ssrc());
  865. }
  866. RemoveStreamByNickAndName(recv_streams, stream.nick, stream.name);
  867. }
  868. void Call::OnReceivedTerminateReason(Session* session,
  869. const std::string& reason) {
  870. session_client_->session_manager()->signaling_thread()->Clear(this,
  871. MSG_TERMINATECALL);
  872. SignalReceivedTerminateReason(this, session, reason);
  873. }
  874. } // namespace cricket