call.cpp 6.55 KB
Newer Older
yanmorin's avatar
 
yanmorin committed
1
/*
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
jpbl's avatar
jpbl committed
3
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
yanmorin's avatar
 
yanmorin committed
4 5
 *  Author : Laurielle Lea <laurielle.lea@savoirfairelinux.com>
 *
jpbl's avatar
jpbl committed
6 7
 *  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
8
 *  the Free Software Foundation; either version 3 of the License, or
jpbl's avatar
jpbl committed
9
 *  (at your option) any later version.
yanmorin's avatar
 
yanmorin committed
10
 *
jpbl's avatar
jpbl committed
11 12 13 14
 *  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.
yanmorin's avatar
 
yanmorin committed
15
 *
jpbl's avatar
jpbl committed
16 17
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
19 20 21 22 23 24 25 26 27 28 29
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
jpbl's avatar
jpbl committed
30 31
 */
#include "call.h"
32
#include "manager.h"
33
#include "audio/mainbuffer.h"
34
#include "history/historyitem.h"
jpbl's avatar
jpbl committed
35

36
Call::Call(const std::string& id, Call::CallType type, const std::string &accountID)
37 38 39
    : callMutex_()
    , localIPAddress_("")
    , localAudioPort_(0)
40
    , localVideoPort_(0)
41
    , id_(id)
42
    , confID_()
43
    , type_(type)
44
    , accountID_(accountID)
45 46 47
    , connectionState_(Call::DISCONNECTED)
    , callState_(Call::INACTIVE)
    , isIPToIP_(false)
48
    , peerNumber_()
49
    , displayName_()
50 51 52 53 54
    , timestamp_start_(0)
    , timestamp_stop_(0)
{
    time(&timestamp_start_);
}
jpbl's avatar
jpbl committed
55

Julien Bonjean's avatar
Julien Bonjean committed
56
Call::~Call()
57
{}
jpbl's avatar
jpbl committed
58

59
void
60
Call::setConnectionState(ConnectionState state)
Julien Bonjean's avatar
Julien Bonjean committed
61
{
62
    std::lock_guard<std::mutex> lock(callMutex_);
63
    connectionState_ = state;
jpbl's avatar
jpbl committed
64 65
}

yanmorin's avatar
 
yanmorin committed
66
Call::ConnectionState
Julien Bonjean's avatar
Julien Bonjean committed
67 68
Call::getConnectionState()
{
69
    std::lock_guard<std::mutex> lock(callMutex_);
70
    return connectionState_;
jpbl's avatar
jpbl committed
71 72
}

73
void
74
Call::setState(CallState state)
Julien Bonjean's avatar
Julien Bonjean committed
75
{
76
    std::lock_guard<std::mutex> lock(callMutex_);
77
    callState_ = state;
jpbl's avatar
jpbl committed
78 79 80
}

Call::CallState
Julien Bonjean's avatar
Julien Bonjean committed
81 82
Call::getState()
{
83
    std::lock_guard<std::mutex> lock(callMutex_);
84
    return callState_;
jpbl's avatar
jpbl committed
85 86
}

87
std::string
88
Call::getStateStr()
Julien Bonjean's avatar
Julien Bonjean committed
89
{
Rafaël Carré's avatar
Rafaël Carré committed
90
    switch (getState()) {
91
        case ACTIVE:
92
            switch (getConnectionState()) {
93
                case RINGING:
94
                    return isIncoming() ? "INCOMING" : "RINGING";
95
                case CONNECTED:
96
                default:
97
                    return "CURRENT";
98 99
            }

100
        case HOLD:
101
            return "HOLD";
102
        case BUSY:
103
            return "BUSY";
104
        case INACTIVE:
105 106

            switch (getConnectionState()) {
107
                case RINGING:
108
                    return isIncoming() ? "INCOMING" : "RINGING";
109
                case CONNECTED:
110 111 112 113 114
                    return "CURRENT";
                default:
                    return "INACTIVE";
            }

115
        case CONFERENCING:
116
            return "CONFERENCING";
117 118
        case REFUSED:
        case ERROR:
119 120
        default:
            return "FAILURE";
121 122 123 124
    }
}


125
std::string
Julien Bonjean's avatar
Julien Bonjean committed
126 127
Call::getLocalIp()
{
128
    std::lock_guard<std::mutex> lock(callMutex_);
129
    return localIPAddress_;
130 131
}

132
unsigned int
Julien Bonjean's avatar
Julien Bonjean committed
133 134
Call::getLocalAudioPort()
{
135
    std::lock_guard<std::mutex> lock(callMutex_);
136
    return localAudioPort_;
137 138
}

139 140 141
unsigned int
Call::getLocalVideoPort()
{
142
    std::lock_guard<std::mutex> lock(callMutex_);
143
    return localVideoPort_;
144 145
}

146
bool
147
Call::toggleRecording()
Julien Bonjean's avatar
Julien Bonjean committed
148
{
149
    const bool startRecording = Recordable::recAudio_.toggleRecording();
150
    MainBuffer &mbuffer = Manager::instance().getMainBuffer();
151
    std::string process_id = Recordable::recorder_.getRecorderID();
152

153
    if (startRecording) {
154 155
        mbuffer.bindHalfDuplexOut(process_id, id_);
        mbuffer.bindHalfDuplexOut(process_id, MainBuffer::DEFAULT_ID);
156

157
        Recordable::recorder_.start();
Rafaël Carré's avatar
Rafaël Carré committed
158
    } else {
159 160
        mbuffer.unBindHalfDuplexOut(process_id, id_);
        mbuffer.unBindHalfDuplexOut(process_id, MainBuffer::DEFAULT_ID);
161 162
    }

163
    Manager::instance().getMainBuffer().dumpInfo();
164

165
    return startRecording;
166
}
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

void Call::time_stop()
{
    time(&timestamp_stop_);
}

std::string Call::getTypeStr() const
{
    switch (type_) {
        case INCOMING:
            return "incoming";
        case OUTGOING:
            return "outgoing";
        case MISSED:
            return "missed";
        default:
            return "";
    }
}

187 188 189 190 191 192 193 194 195
namespace {
    std::string timestamp_to_string(const time_t &timestamp)
    {
        std::stringstream time_str;
        time_str << timestamp;
        return time_str.str();
    }
}

196 197
std::map<std::string, std::string> Call::createHistoryEntry() const
{
198
    using sfl::HistoryItem;
199
    std::map<std::string, std::string> result;
200

201
    result[HistoryItem::ACCOUNT_ID_KEY] = accountID_;
202 203 204 205 206
    result[HistoryItem::CONFID_KEY] = confID_;
    result[HistoryItem::CALLID_KEY] = id_;
    result[HistoryItem::DISPLAY_NAME_KEY] = displayName_;
    result[HistoryItem::PEER_NUMBER_KEY] = peerNumber_;
    result[HistoryItem::RECORDING_PATH_KEY] = recAudio_.fileExists() ? getFilename() : "";
207 208
    result[HistoryItem::TIMESTAMP_START_KEY] = timestamp_to_string(timestamp_start_);
    result[HistoryItem::TIMESTAMP_STOP_KEY] = timestamp_to_string(timestamp_stop_);
209 210 211 212 213 214
    if (connectionState_ == RINGING)
        result[HistoryItem::STATE_KEY] = HistoryItem::MISSED_STRING;
    else
        result[HistoryItem::STATE_KEY] = getTypeStr();
    return result;
}
215 216 217 218 219 220 221 222 223 224 225 226 227

std::map<std::string, std::string>
Call::getDetails()
{
    std::map<std::string, std::string> details;
    std::ostringstream type;
    type << type_;
    details["CALL_TYPE"] = type.str();
    details["PEER_NUMBER"] = peerNumber_;
    details["DISPLAY_NAME"] = displayName_;
    details["CALL_STATE"] = getStateStr();
    details["CONF_ID"] = confID_;
    details["TIMESTAMP_START"] = timestamp_to_string(timestamp_start_);
228
    details["ACCOUNTID"] = accountID_;
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    return details;
}

std::map<std::string, std::string>
Call::getNullDetails()
{
    std::map<std::string, std::string> details;
    details["ACCOUNTID"] = "";
    details["PEER_NUMBER"] = "Unknown";
    details["PEER_NAME"] = "Unknown";
    details["DISPLAY_NAME"] = "Unknown";
    details["CALL_STATE"] = "UNKNOWN";
    details["CALL_TYPE"] = "0";
    return details;
}