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
|
/*
* Jingle call example
* Copyright 2004--2005, Google Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// LinphoneMediaEngine is a Linphone implementation of MediaEngine
#include <mediastreamer2/mediastream.h>
//#ifdef HAVE_ILBC
//#include <mediastreamer2/msilbcdec.h>
//#endif
//#ifdef HAVE_SPEEX
//#include <mediastreamer2/msspeexdec.h>
//#endif
#include <ortp/ortp.h>
#include <ortp/telephonyevents.h>
//#include <netdb.h>
//#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include "talk/base/logging.h"
#include "talk/base/thread.h"
#include "talk/base/helpers.h"
#include "talk/session/phone/codec.h"
#include "talk/session/phone/linphonemediaengine.h"
using namespace cricket;
void LinphoneMediaChannel::OnIncomingData(talk_base::AsyncSocket *s)
{
char *buf[2048];
int len;
len = s->Recv(buf, sizeof(buf));
if (network_interface_ && !mute_)
network_interface_->SendPacket(buf, len);
}
LinphoneMediaChannel::LinphoneMediaChannel(LinphoneMediaEngine*eng) :
pt_(-1),
audio_stream_(0),
engine_(eng) {
talk_base::Thread *thread = talk_base::ThreadManager::CurrentThread();
talk_base::SocketServer *ss = thread->socketserver();
socket_.reset(ss->CreateAsyncSocket(SOCK_DGRAM));
port_in_ = 2000 + CreateRandomId() % 1000;
port_out_ = 3000 + CreateRandomId() % 1000;
socket_->Bind(talk_base::SocketAddress("localhost", port_out_));
socket_->SignalReadEvent.connect(this, &LinphoneMediaChannel::OnIncomingData);
}
LinphoneMediaChannel::~LinphoneMediaChannel() {
if (audio_stream_ != NULL)
audio_stream_stop(audio_stream_);
}
void LinphoneMediaChannel::SetCodecs(const std::vector<Codec> &codecs) {
bool first = true;
std::vector<Codec>::const_iterator i;
for (i = codecs.begin(); i < codecs.end(); i++) {
if (!engine_->FindCodec(*i))
continue;
#ifdef HAVE_ILBC
if (i->name == payload_type_ilbc.mime_type) {
rtp_profile_set_payload(&av_profile, i->id, &payload_type_ilbc);
}
#endif
#ifdef HAVE_SPEEX
if (i->name == payload_type_speex_wb.mime_type && i->clockrate == payload_type_speex_wb.clock_rate) {
rtp_profile_set_payload(&av_profile, i->id, &payload_type_speex_wb);
} else if (i->name == payload_type_speex_nb.mime_type && i->clockrate == payload_type_speex_nb.clock_rate) {
rtp_profile_set_payload(&av_profile, i->id, &payload_type_speex_nb);
}
#endif
if (i->id == 0)
rtp_profile_set_payload(&av_profile, 0, &payload_type_pcmu8000);
if (i->name == payload_type_telephone_event.mime_type) {
rtp_profile_set_payload(&av_profile, i->id, &payload_type_telephone_event);
}
if (first) {
LOG(LS_INFO) << "Using " << i->name << "/" << i->clockrate;
pt_ = i->id;
audio_stream_ = audio_stream_start(&av_profile, port_in_, "127.0.0.1", port_out_, i->id, 250, false);
first = false;
}
}
if (first) {
// We're being asked to set an empty list of codecs. This will only happen when
// working with a buggy client; let's try PCMU.
LOG(LS_WARNING) << "Received empty list of codces; using PCMU/8000";
audio_stream_ = audio_stream_start(&av_profile, port_in_, "127.0.0.1", port_out_, 0, 250, false);
}
}
bool LinphoneMediaEngine::FindCodec(const Codec &c) {
if (c.id == 0)
return true;
if (c.name == payload_type_telephone_event.mime_type)
return true;
#ifdef HAVE_SPEEX
if (c.name == payload_type_speex_wb.mime_type && c.clockrate == payload_type_speex_wb.clock_rate)
return true;
if (c.name == payload_type_speex_nb.mime_type && c.clockrate == payload_type_speex_nb.clock_rate)
return true;
#endif
#ifdef HAVE_ILBC
if (c.name == payload_type_ilbc.mime_type)
return true;
#endif
return false;
}
void LinphoneMediaChannel::OnPacketReceived(const void *data, int len) {
uint8 buf[2048];
memcpy(buf, data, len);
/* We may receive packets with payload type 13: comfort noise. Linphone can't
* handle them, so let's ignore those packets.
*/
int payloadtype = buf[1] & 0x7f;
if (play_ && payloadtype != 13)
socket_->SendTo(buf, len, talk_base::SocketAddress("localhost", port_in_));
}
void LinphoneMediaChannel::SetPlayout(bool playout) {
play_ = playout;
}
void LinphoneMediaChannel::SetSend(bool send) {
mute_ = !send;
}
int LinphoneMediaChannel::GetOutputLevel() { return 0; }
LinphoneMediaEngine::LinphoneMediaEngine() {}
LinphoneMediaEngine::~LinphoneMediaEngine() {}
/*
static void null_log_handler(const char *log_domain,
GLogLevelFlags log_level,
const char *message,
gpointer user_data) {
LOG(LS_INFO) << log_domain << " " << message;
}
*/
bool LinphoneMediaEngine::Init() {
// g_log_set_handler("MediaStreamer", G_LOG_LEVEL_MASK, null_log_handler, this);
// g_log_set_handler("oRTP", G_LOG_LEVEL_MASK, null_log_handler, this);
// g_log_set_handler("oRTP-stats", G_LOG_LEVEL_MASK, null_log_handler, this);
ortp_init();
ms_init();
#ifdef HAVE_SPEEX
//ms_speex_codec_init();
codecs_.push_back(Codec(110, payload_type_speex_wb.mime_type, payload_type_speex_wb.clock_rate, 0, 1, 8));
codecs_.push_back(Codec(111, payload_type_speex_nb.mime_type, payload_type_speex_nb.clock_rate, 0, 1, 7));
#endif
#ifdef HAVE_ILBC
//ms_ilbc_codec_init();
codecs_.push_back(Codec(102, payload_type_ilbc.mime_type, payload_type_ilbc.clock_rate, 0, 1, 4));
#endif
codecs_.push_back(Codec(0, payload_type_pcmu8000.mime_type, payload_type_pcmu8000.clock_rate, 0, 1, 2));
codecs_.push_back(Codec(101, payload_type_telephone_event.mime_type, payload_type_telephone_event.clock_rate, 0, 1, 1));
return true;
}
void LinphoneMediaEngine::Terminate() {
}
MediaChannel *LinphoneMediaEngine::CreateChannel() {
return new LinphoneMediaChannel(this);
}
int LinphoneMediaEngine::SetAudioOptions(int options) { return 0; }
int LinphoneMediaEngine::SetSoundDevices(int wave_in_device, int wave_out_device) { return 0; }
float LinphoneMediaEngine::GetCurrentQuality() { return 0; }
int LinphoneMediaEngine::GetInputLevel() { return 0; }
|