~ubuntu-branches/ubuntu/trusty/kopete/trusty

« back to all changes in this revision

Viewing changes to .pc/new_linphone.diff/protocols/jabber/googletalk/libjingle/talk/session/phone/linphonemediaengine.cc

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2013-11-23 17:46:40 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20131123174640-gz1zjv1xqh81bi2h
Tags: 4:4.11.80-0ubuntu1
* New upstream beta release
* Bump build dependency to libotr5-dev
* Update symbols
* Update install files
* Drop new_linphone.diff, not required anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * libjingle
3
 
 * Copyright 2010, 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
 
 
28
 
#ifndef MSILBC_LIBRARY
29
 
#define MSILBC_LIBRARY "/usr/lib/mediastreamer/plugins/libmsilbc.so"
30
 
#endif
31
 
 
32
 
#define PORT_UNUSED -1
33
 
 
34
 
#ifdef HAVE_LINPHONE
35
 
 
36
 
// LinphoneMediaEngine is a Linphone implementation of MediaEngine
37
 
extern "C" {
38
 
#include <mediastreamer2/mediastream.h>
39
 
#include <mediastreamer2/mssndcard.h>
40
 
#include <mediastreamer2/msfilter.h>
41
 
}
42
 
 
43
 
#include "talk/session/phone/linphonemediaengine.h"
44
 
 
45
 
#include "talk/base/buffer.h"
46
 
#include "talk/base/event.h"
47
 
#include "talk/base/logging.h"
48
 
#include "talk/base/pathutils.h"
49
 
#include "talk/base/stream.h"
50
 
#include "talk/session/phone/rtpdump.h"
51
 
 
52
 
#ifndef WIN32
53
 
#include <libgen.h>
54
 
#endif
55
 
 
56
 
namespace cricket {
57
 
 
58
 
///////////////////////////////////////////////////////////////////////////
59
 
// Implementation of LinphoneMediaEngine.
60
 
///////////////////////////////////////////////////////////////////////////
61
 
LinphoneMediaEngine::LinphoneMediaEngine(const std::string& ringWav,  const std::string& callWav) : ring_wav_(ringWav), call_wav_(callWav) {
62
 
  ortp_init();
63
 
  ms_init();
64
 
 
65
 
#ifndef WIN32
66
 
  char * path = strdup(MSILBC_LIBRARY);
67
 
  char * dirc = dirname(path);
68
 
  ms_load_plugins(dirc);
69
 
  free(path);
70
 
#endif
71
 
 
72
 
  if (ms_filter_codec_supported("iLBC"))
73
 
    have_ilbc = true;
74
 
  else
75
 
    have_ilbc = false;
76
 
 
77
 
  if (ms_filter_codec_supported("speex"))
78
 
    have_speex = true;
79
 
  else
80
 
    have_speex = false;
81
 
 
82
 
  if (ms_filter_codec_supported("gsm"))
83
 
    have_gsm = true;
84
 
  else
85
 
    have_gsm = false;
86
 
 
87
 
  if (have_speex) {
88
 
    voice_codecs_.push_back(AudioCodec(110, payload_type_speex_wb.mime_type, payload_type_speex_wb.clock_rate, 0, 1, 8));
89
 
    voice_codecs_.push_back(AudioCodec(111, payload_type_speex_nb.mime_type, payload_type_speex_nb.clock_rate, 0, 1, 7));
90
 
  }
91
 
 
92
 
  if (have_ilbc)
93
 
    voice_codecs_.push_back(AudioCodec(102, payload_type_ilbc.mime_type, payload_type_ilbc.clock_rate, 0, 1, 4));
94
 
 
95
 
  if (have_gsm)
96
 
    voice_codecs_.push_back(AudioCodec(3, payload_type_gsm.mime_type, payload_type_gsm.clock_rate, 0, 1, 3));
97
 
 
98
 
  voice_codecs_.push_back(AudioCodec(0, payload_type_pcmu8000.mime_type, payload_type_pcmu8000.clock_rate, 0, 1, 2));
99
 
  voice_codecs_.push_back(AudioCodec(101, payload_type_telephone_event.mime_type, payload_type_telephone_event.clock_rate, 0, 1, 1));
100
 
}
101
 
 
102
 
void LinphoneMediaEngine::Terminate() {
103
 
  fflush(stdout);
104
 
}
105
 
 
106
 
 
107
 
int LinphoneMediaEngine::GetCapabilities() {
108
 
  int capabilities = 0;
109
 
  capabilities |= AUDIO_SEND;
110
 
  capabilities |= AUDIO_RECV;
111
 
  return capabilities;
112
 
}
113
 
 
114
 
VoiceMediaChannel* LinphoneMediaEngine::CreateChannel() {
115
 
  return new LinphoneVoiceChannel(this);
116
 
}
117
 
 
118
 
VideoMediaChannel* LinphoneMediaEngine::CreateVideoChannel(VoiceMediaChannel* voice_ch) {
119
 
  return NULL;
120
 
}
121
 
 
122
 
bool LinphoneMediaEngine::FindAudioCodec(const AudioCodec &c) {
123
 
  if (c.id == 0)
124
 
    return true;
125
 
  if (c.name == payload_type_telephone_event.mime_type)
126
 
    return true;
127
 
  if (have_speex && c.name == payload_type_speex_wb.mime_type && c.clockrate == payload_type_speex_wb.clock_rate)
128
 
    return true;
129
 
  if (have_speex && c.name == payload_type_speex_nb.mime_type && c.clockrate == payload_type_speex_nb.clock_rate)
130
 
    return true;
131
 
  if (have_ilbc && c.name == payload_type_ilbc.mime_type)
132
 
    return true;
133
 
  if (have_gsm && c.name == payload_type_gsm.mime_type)
134
 
    return true;
135
 
  return false;
136
 
}
137
 
 
138
 
 
139
 
///////////////////////////////////////////////////////////////////////////
140
 
// Implementation of LinphoneVoiceChannel.
141
 
///////////////////////////////////////////////////////////////////////////
142
 
LinphoneVoiceChannel::LinphoneVoiceChannel(LinphoneMediaEngine*eng)
143
 
    : pt_(-1),
144
 
      audio_stream_(0),
145
 
      engine_(eng),
146
 
      ring_stream_(0)
147
 
{
148
 
 
149
 
  talk_base::Thread *thread = talk_base::ThreadManager::Instance()->CurrentThread();
150
 
  talk_base::SocketServer *ss = thread->socketserver();
151
 
  socket_.reset(ss->CreateAsyncSocket(SOCK_DGRAM));
152
 
 
153
 
  socket_->Bind(talk_base::SocketAddress("localhost", 0)); /* 0 means that OS will choose some free port */
154
 
  port1 = socket_->GetLocalAddress().port(); /* and here we get port choosed by OS */
155
 
  port2 = PORT_UNUSED;
156
 
  socket_->SignalReadEvent.connect(this, &LinphoneVoiceChannel::OnIncomingData);
157
 
 
158
 
}
159
 
 
160
 
LinphoneVoiceChannel::~LinphoneVoiceChannel()
161
 
{
162
 
  fflush(stdout);
163
 
  StopRing();
164
 
 
165
 
  if (audio_stream_)
166
 
    audio_stream_stop(audio_stream_);
167
 
}
168
 
 
169
 
bool LinphoneVoiceChannel::SetPlayout(bool playout) {
170
 
  play_ = playout;
171
 
  return true;
172
 
}
173
 
 
174
 
bool LinphoneVoiceChannel::SetSendCodecs(const std::vector<AudioCodec>& codecs) {
175
 
 
176
 
  bool first = true;
177
 
  std::vector<AudioCodec>::const_iterator i;
178
 
 
179
 
  ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
180
 
 
181
 
  for (i = codecs.begin(); i < codecs.end(); i++) {
182
 
 
183
 
    if (!engine_->FindAudioCodec(*i))
184
 
      continue;
185
 
    if (engine_->have_ilbc && i->name == payload_type_ilbc.mime_type) {
186
 
      rtp_profile_set_payload(&av_profile, i->id, &payload_type_ilbc);
187
 
    } else if (engine_->have_speex && i->name == payload_type_speex_wb.mime_type && i->clockrate == payload_type_speex_wb.clock_rate) {
188
 
      rtp_profile_set_payload(&av_profile, i->id, &payload_type_speex_wb);
189
 
    } else if (engine_->have_speex && i->name == payload_type_speex_nb.mime_type && i->clockrate == payload_type_speex_nb.clock_rate) {
190
 
      rtp_profile_set_payload(&av_profile, i->id, &payload_type_speex_nb);
191
 
    } else if (engine_->have_gsm && i->name == payload_type_gsm.mime_type) {
192
 
      rtp_profile_set_payload(&av_profile, i->id, &payload_type_gsm);
193
 
    } else if (i->name == payload_type_telephone_event.mime_type) {
194
 
      rtp_profile_set_payload(&av_profile, i->id, &payload_type_telephone_event);
195
 
    } else if (i->id == 0)
196
 
      rtp_profile_set_payload(&av_profile, 0, &payload_type_pcmu8000);
197
 
 
198
 
    if (first) {
199
 
      StopRing();
200
 
      LOG(LS_INFO) << "Using " << i->name << "/" << i->clockrate;
201
 
      pt_ = i->id;
202
 
      audio_stream_ = audio_stream_start(&av_profile, -1, "localhost", port1, i->id, 250, 0); /* -1 means that function will choose some free port */
203
 
      port2 = rtp_session_get_local_port(audio_stream_->session);
204
 
      first = false;
205
 
    }
206
 
  }
207
 
 
208
 
  if (first) {
209
 
    StopRing();
210
 
    // We're being asked to set an empty list of codecs. This will only happen when
211
 
    // working with a buggy client; let's try PCMU.
212
 
    LOG(LS_WARNING) << "Received empty list of codces; using PCMU/8000";
213
 
    audio_stream_ = audio_stream_start(&av_profile, -1, "localhost", port1, 0, 250, 0); /* -1 means that function will choose some free port */
214
 
    port2 = rtp_session_get_local_port(audio_stream_->session);
215
 
  }
216
 
 
217
 
  return true;
218
 
}
219
 
 
220
 
bool LinphoneVoiceChannel::SetSend(SendFlags flag) {
221
 
  mute_ = !flag;
222
 
  return true;
223
 
}
224
 
 
225
 
void LinphoneVoiceChannel::OnPacketReceived(talk_base::Buffer* packet) {
226
 
  const void* data = packet->data();
227
 
  int len = packet->length();
228
 
  uint8 buf[2048];
229
 
  memcpy(buf, data, len);
230
 
 
231
 
  if (port2 == PORT_UNUSED)
232
 
    return;
233
 
 
234
 
  /* We may receive packets with payload type 13: comfort noise. Linphone can't
235
 
   * handle them, so let's ignore those packets.
236
 
   */
237
 
  int payloadtype = buf[1] & 0x7f;
238
 
  if (play_ && payloadtype != 13)
239
 
    socket_->SendTo(buf, len, talk_base::SocketAddress("localhost",port2));
240
 
}
241
 
 
242
 
void LinphoneVoiceChannel::StartRing(bool bIncomingCall)
243
 
{
244
 
  MSSndCard *sndcard = NULL;
245
 
  sndcard=ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());
246
 
  if (sndcard)
247
 
  {
248
 
    if (bIncomingCall)
249
 
    {
250
 
      if (engine_->GetRingWav().size() > 0)
251
 
      {
252
 
        LOG(LS_VERBOSE) << "incoming ring. sound file: " << engine_->GetRingWav().c_str() << "\n";
253
 
        ring_stream_ = ring_start (engine_->GetRingWav().c_str(), 1, sndcard);
254
 
      }
255
 
    }
256
 
    else
257
 
    {
258
 
      if (engine_->GetCallWav().size() > 0)
259
 
      {
260
 
        LOG(LS_VERBOSE) << "outgoing ring. sound file: " << engine_->GetCallWav().c_str() << "\n";
261
 
        ring_stream_ = ring_start (engine_->GetCallWav().c_str(), 1, sndcard);
262
 
      }
263
 
    }
264
 
  }
265
 
}
266
 
 
267
 
void LinphoneVoiceChannel::StopRing()
268
 
{
269
 
  if (ring_stream_) {
270
 
    ring_stop(ring_stream_);
271
 
    ring_stream_ = 0;
272
 
  }
273
 
}
274
 
 
275
 
void LinphoneVoiceChannel::OnIncomingData(talk_base::AsyncSocket *s)
276
 
{
277
 
  char *buf[2048];
278
 
  int len;
279
 
  len = s->Recv(buf, sizeof(buf));
280
 
  talk_base::Buffer packet(buf, len, sizeof(buf));
281
 
  if (network_interface_ && !mute_)
282
 
    network_interface_->SendPacket(&packet);
283
 
}
284
 
 
285
 
}
286
 
 
287
 
#endif // HAVE_LINPHONE