summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/session/phone/voicechannel.cc
blob: 58e1db608e77d2e5def156396e17f7c825581319 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * 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/session/phone/voicechannel.h"
#include "talk/session/phone/channelmanager.h"
#include "talk/session/phone/phonesessionclient.h"
#include "talk/base/logging.h"
#include <cassert>
#undef SetPort

namespace {

// Delay before quality estimate is meaningful.
uint32 kQualityDelay = 5000; // in ms

}

namespace cricket {

VoiceChannel::VoiceChannel(ChannelManager *manager, Session *session, MediaChannel *channel) {
  channel_manager_ = manager;
  assert(channel_manager_->worker_thread() == Thread::Current());
  channel_ = channel;
  session_ = session;
  socket_monitor_ = NULL;
  audio_monitor_ = NULL;
  socket_ = session_->CreateSocket("rtp");
  socket_->SignalState.connect(this, &VoiceChannel::OnSocketState);
  socket_->SignalReadPacket.connect(this, &VoiceChannel::OnSocketRead);
  channel->SetInterface(this);
  enabled_ = false;
  paused_ = false;
  socket_writable_ = false;
  muted_ = false;
  LOG(INFO) << "Created voice channel";
  start_time_ = 0xFFFFFFFF - kQualityDelay;

  session->SignalState.connect(this, &VoiceChannel::OnSessionState);
  OnSessionState(session, session->state());
}

VoiceChannel::~VoiceChannel() {
  assert(channel_manager_->worker_thread() == Thread::Current());
  enabled_ = false;
  ChangeState();
  delete socket_monitor_;
  delete audio_monitor_;
  Thread::Current()->Clear(this);
  if (socket_ != NULL)
    session_->DestroySocket(socket_);
  LOG(INFO) << "Destroyed voice channel";
}

void VoiceChannel::OnMessage(Message *pmsg) {
  switch (pmsg->message_id) {
  case MSG_ENABLE:
    EnableMedia_w();
    break;

  case MSG_DISABLE:
    DisableMedia_w();
    break;

  case MSG_MUTE:
    MuteMedia_w();
    break;

  case MSG_UNMUTE:
    UnmuteMedia_w();
    break;

  case MSG_SETSENDCODEC:
    SetSendCodec_w();
    break;
  }
}

void VoiceChannel::Enable(bool enable) {
  // Can be called from thread other than worker thread
  channel_manager_->worker_thread()->Post(this, enable ? MSG_ENABLE : MSG_DISABLE);
}

void VoiceChannel::Mute(bool mute) {
  // Can be called from thread other than worker thread
  channel_manager_->worker_thread()->Post(this, mute ? MSG_MUTE : MSG_UNMUTE);
}

MediaChannel * VoiceChannel::channel() {
  return channel_;
}

void VoiceChannel::OnSessionState(Session* session, Session::State state) {
  if ((state == Session::STATE_RECEIVEDACCEPT) ||
      (state == Session::STATE_RECEIVEDINITIATE)) {
    channel_manager_->worker_thread()->Post(this, MSG_SETSENDCODEC);
  }
}

void VoiceChannel::SetSendCodec_w() {
  assert(channel_manager_->worker_thread() == Thread::Current());

  const PhoneSessionDescription* desc =
      static_cast<const PhoneSessionDescription*>(session()->remote_description());

  const char *codec = NULL;

  if (desc->codecs().size() > 0)
   PhoneSessionClient::FindMediaCodec(channel_manager_->media_engine(), desc, &codec);

  // The other client should have returned one of the codecs that we offered.
  // If they could not, they should have rejected the session.  So, if we get
  // into this state, we're dealing with a bad client, so we may as well just
  // pick the mostt common format there is: payload type zero.
  if (codec == NULL)
    codec = "PCMU";

  channel_->SetCodec(codec);
}

void VoiceChannel::OnSocketState(P2PSocket *socket, P2PSocket::State state) {
  switch (state) {
  case P2PSocket::STATE_WRITABLE:
    SocketWritable_w();
    break;

  default:
    SocketNotWritable_w();
    break;
  }
}

void VoiceChannel::OnSocketRead(P2PSocket *socket, const char *data, size_t len) {
  assert(channel_manager_->worker_thread() == Thread::Current());
  // OnSocketRead gets called from P2PSocket; now pass data to MediaEngine
  channel_->OnPacketReceived(data, (int)len);
}

void VoiceChannel::SendPacket(const void *data, size_t len) {
  // SendPacket gets called from MediaEngine; send to socket
  // MediaEngine will call us on a random thread.  The Send operation on the socket is
  // special in that it can handle this.
  socket_->Send(static_cast<const char *>(data), len);
}

void VoiceChannel::ChangeState() {
  if (paused_ || !enabled_ || !socket_writable_) {
    channel_->SetPlayout(false);
    channel_->SetSend(false);
  } else {
    if (muted_) {
      channel_->SetSend(false);
      channel_->SetPlayout(true);
    } else {
      channel_->SetSend(true);
      channel_->SetPlayout(true);
    }
  }
}

void VoiceChannel::PauseMedia_w() {
  assert(channel_manager_->worker_thread() == Thread::Current());
  assert(!paused_);

  LOG(INFO) << "Voice channel paused";
  paused_ = true;
  ChangeState();
}

void VoiceChannel::UnpauseMedia_w() {
  assert(channel_manager_->worker_thread() == Thread::Current());
  assert(paused_);

  LOG(INFO) << "Voice channel unpaused";
  paused_ = false;
  ChangeState();
}

void VoiceChannel::EnableMedia_w() {
  assert(channel_manager_->worker_thread() == Thread::Current());
  if (enabled_)
    return;

  LOG(INFO) << "Voice channel enabled";
  enabled_ = true;
  start_time_ = Time();
  ChangeState();
}

void VoiceChannel::DisableMedia_w() {
  assert(channel_manager_->worker_thread() == Thread::Current());
  if (!enabled_)
    return;

  LOG(INFO) << "Voice channel disabled";
  enabled_ = false;
  ChangeState();
}

void VoiceChannel::MuteMedia_w() {
  assert(channel_manager_->worker_thread() == Thread::Current());
  if (muted_)
    return;

  LOG(INFO) << "Voice channel muted";
  muted_ = true;
  ChangeState();
}

void VoiceChannel::UnmuteMedia_w() {
  assert(channel_manager_->worker_thread() == Thread::Current());
  if (!muted_)
    return;

  LOG(INFO) << "Voice channel unmuted";
  muted_ = false;
  ChangeState();
}

void VoiceChannel::SocketWritable_w() {
  assert(channel_manager_->worker_thread() == Thread::Current());
  if (socket_writable_)
    return;

  LOG(INFO) << "Voice channel socket writable";
  socket_writable_ = true;
  ChangeState();
}

void VoiceChannel::SocketNotWritable_w() {
  assert(channel_manager_->worker_thread() == Thread::Current());
  if (!socket_writable_)
    return;

  LOG(INFO) << "Voice channel socket not writable";
  socket_writable_ = false;
  ChangeState();
}

void VoiceChannel::StartConnectionMonitor(int cms) {
  delete socket_monitor_;
  socket_monitor_ = new SocketMonitor(socket_, Thread::Current());
  socket_monitor_
    ->SignalUpdate.connect(this, &VoiceChannel::OnConnectionMonitorUpdate);
  socket_monitor_->Start(cms);
}

void VoiceChannel::StopConnectionMonitor() {
  if (socket_monitor_ != NULL) {
    socket_monitor_->Stop();
    socket_monitor_->SignalUpdate.disconnect(this);
    delete socket_monitor_;
    socket_monitor_ = NULL;
  }
}

void VoiceChannel::OnConnectionMonitorUpdate(SocketMonitor *monitor,
                      const std::vector<ConnectionInfo> &infos) {
  SignalConnectionMonitor(this, infos);
}

void VoiceChannel::StartAudioMonitor(int cms) {
  delete audio_monitor_;
  audio_monitor_ = new AudioMonitor(this, Thread::Current());
  audio_monitor_
    ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
  audio_monitor_->Start(cms);
}

void VoiceChannel::StopAudioMonitor() {
  if (audio_monitor_ != NULL) {
    audio_monitor_ ->Stop();
    audio_monitor_ ->SignalUpdate.disconnect(this);
    delete audio_monitor_ ;
    audio_monitor_  = NULL;
  }
}

void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor *monitor,
                                        const AudioInfo& info) {
  SignalAudioMonitor(this, info);
}

Session *VoiceChannel::session() {
  return session_;
}

bool VoiceChannel::HasQuality() {
  return Time() >= start_time_ + kQualityDelay;
}

float VoiceChannel::GetCurrentQuality() {
  return channel_->GetCurrentQuality();
}

int VoiceChannel::GetInputLevel_w() {
  return channel_manager_->media_engine()->GetInputLevel();
}

int VoiceChannel::GetOutputLevel_w() {
  return channel_->GetOutputLevel();
}

Thread* VoiceChannel::worker_thread() {
  return channel_manager_->worker_thread();
}

}