summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/session/phone/call.cpp
blob: 31b12e92bef157ae4f555fa95f9356010494cd4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * libjingle
 * Copyright 2004--2005, Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products 
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "talk/base/thread.h"
#include "talk/p2p/base/helpers.h"
#include "talk/session/phone/call.h"

namespace cricket {

const uint32 MSG_CHECKAUTODESTROY = 1;

Call::Call(PhoneSessionClient *session_client) : muted_(false) {
  session_client_ = session_client;
  id_ = CreateRandomId();
}

Call::~Call() {
  while (sessions_.begin() != sessions_.end()) {
    Session *session = sessions_[0];
    RemoveSession(session);
    session_client_->session_manager()->DestroySession(session);
  }
  Thread::Current()->Clear(this);
}

Session *Call::InitiateSession(const buzz::Jid &jid) {
  Session *session = session_client_->CreateSession(this);
  AddSession(session);
  session->Initiate(jid.Str(), session_client_->CreateOfferSessionDescription());
  return session;
}

void Call::AcceptSession(Session *session) {
  std::vector<Session *>::iterator it;
  it = std::find(sessions_.begin(), sessions_.end(), session);
  assert(it != sessions_.end());
  if (it != sessions_.end())
    session->Accept(session_client_->CreateAcceptSessionDescription(session->remote_description()));
}

void Call::RedirectSession(Session *session, const buzz::Jid &to) {
  std::vector<Session *>::iterator it;
  it = std::find(sessions_.begin(), sessions_.end(), session);
  assert(it != sessions_.end());
  if (it != sessions_.end())
    session->Redirect(to.Str());
}

void Call::RejectSession(Session *session) {
  std::vector<Session *>::iterator it;
  it = std::find(sessions_.begin(), sessions_.end(), session);
  assert(it != sessions_.end());
  if (it != sessions_.end())
    session->Reject();
}

void Call::TerminateSession(Session *session) {
  assert(std::find(sessions_.begin(), sessions_.end(), session) != sessions_.end());
  std::vector<Session *>::iterator it;
  it = std::find(sessions_.begin(), sessions_.end(), session);
  if (it != sessions_.end())
    (*it)->Terminate();
}

void Call::Terminate() {
  // There may be more than one session to terminate
  std::vector<Session *>::iterator it = sessions_.begin();
  for (it = sessions_.begin(); it != sessions_.end(); it++)
    TerminateSession(*it);
}

void Call::OnMessage(Message *message) {
  switch (message->message_id) {
  case MSG_CHECKAUTODESTROY:
    // If no more sessions for this call, delete it
    if (sessions_.size() == 0)
      session_client_->DestroyCall(this);
    break;
  }
}

const std::vector<Session *> &Call::sessions() {
  return sessions_;
}

void Call::AddSession(Session *session) {
  // Add session to list, create voice channel for this session
  sessions_.push_back(session);
  session->SignalState.connect(this, &Call::OnSessionState);
  session->SignalError.connect(this, &Call::OnSessionError);

  VoiceChannel *channel = session_client_->channel_manager()->CreateVoiceChannel(session);
  channel_map_[session->id()] = channel;

  // If this call has the focus, enable this channel
  if (session_client_->GetFocus() == this)
    channel->Enable(true);

  // Signal client
  SignalAddSession(this, session);
}

void Call::RemoveSession(Session *session) {
  // Remove session from list
  std::vector<Session *>::iterator it_session;
  it_session = std::find(sessions_.begin(), sessions_.end(), session);
  if (it_session == sessions_.end())
    return;
  sessions_.erase(it_session);

  // Destroy session channel
  std::map<SessionID, VoiceChannel *>::iterator it_channel;
  it_channel = channel_map_.find(session->id());
  if (it_channel != channel_map_.end()) {
    VoiceChannel *channel = it_channel->second;
    channel_map_.erase(it_channel);
    session_client_->channel_manager()->DestroyVoiceChannel(channel);
  }

  // Signal client
  SignalRemoveSession(this, session);

  // The call auto destroys when the lass session is removed
  Thread::Current()->Post(this, MSG_CHECKAUTODESTROY);
}

VoiceChannel* Call::GetChannel(Session* session) {
  std::map<SessionID, VoiceChannel *>::iterator it = channel_map_.find(session->id());
  assert(it != channel_map_.end());
  return it->second;
}

void Call::EnableChannels(bool enable) {
  std::vector<Session *>::iterator it;
  for (it = sessions_.begin(); it != sessions_.end(); it++) {
    VoiceChannel *channel = channel_map_[(*it)->id()];
    if (channel != NULL)
      channel->Enable(enable);
  }
}

void Call::Mute(bool mute) {
  muted_ = mute;
  std::vector<Session *>::iterator it;
  for (it = sessions_.begin(); it != sessions_.end(); it++) {
    VoiceChannel *channel = channel_map_[(*it)->id()];
    if (channel != NULL)
      channel->Mute(mute);
  }
}

void Call::Join(Call *call, bool enable) {
  while (call->sessions_.size() != 0) {
    // Move session
    Session *session = call->sessions_[0];
    call->sessions_.erase(call->sessions_.begin());
    sessions_.push_back(session);
    session->SignalState.connect(this, &Call::OnSessionState);
    session->SignalError.connect(this, &Call::OnSessionError);

    // Move channel
    std::map<SessionID, VoiceChannel *>::iterator it_channel;
    it_channel = call->channel_map_.find(session->id());
    if (it_channel != call->channel_map_.end()) {
      VoiceChannel *channel = (*it_channel).second;
      call->channel_map_.erase(it_channel);
      channel_map_[session->id()] = channel;
      channel->Enable(enable);
    }
  }
}

void Call::StartConnectionMonitor(Session *session, int cms) {
  std::map<SessionID, VoiceChannel *>::iterator it_channel;
  it_channel = channel_map_.find(session->id());
  if (it_channel != channel_map_.end()) {
    VoiceChannel *channel = (*it_channel).second;
    channel->SignalConnectionMonitor.connect(this, &Call::OnConnectionMonitor);
    channel->StartConnectionMonitor(cms);
  }
}

void Call::StopConnectionMonitor(Session *session) {
  std::map<SessionID, VoiceChannel *>::iterator it_channel;
  it_channel = channel_map_.find(session->id());
  if (it_channel != channel_map_.end()) {
    VoiceChannel *channel = (*it_channel).second;
    channel->StopConnectionMonitor();
    channel->SignalConnectionMonitor.disconnect(this);
  }
}

void Call::StartAudioMonitor(Session *session, int cms) {
  std::map<SessionID, VoiceChannel *>::iterator it_channel;
  it_channel = channel_map_.find(session->id());
  if (it_channel != channel_map_.end()) {
    VoiceChannel *channel = (*it_channel).second;
    channel->SignalAudioMonitor.connect(this, &Call::OnAudioMonitor);
    channel->StartAudioMonitor(cms);
  }
}

void Call::StopAudioMonitor(Session *session) {
  std::map<SessionID, VoiceChannel *>::iterator it_channel;
  it_channel = channel_map_.find(session->id());
  if (it_channel != channel_map_.end()) {
    VoiceChannel *channel = (*it_channel).second;
    channel->StopAudioMonitor();
    channel->SignalAudioMonitor.disconnect(this);
  }
}


void Call::OnConnectionMonitor(VoiceChannel *channel, const std::vector<ConnectionInfo> &infos) {
  SignalConnectionMonitor(this, channel->session(), infos);
}

void Call::OnAudioMonitor(VoiceChannel *channel, const AudioInfo& info) {
  SignalAudioMonitor(this, channel->session(), info);
}

uint32 Call::id() {
  return id_;
}

void Call::OnSessionState(Session *session, Session::State state) {
  SignalSessionState(this, session, state);
}

void Call::OnSessionError(Session *session, Session::Error error) {
  SignalSessionError(this, session, error);
}

}