call.cpp 6.37 KB
Newer Older
yanmorin's avatar
yanmorin committed
1
/*
2
 *  Copyright (C) 2004-2012 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)
37 38 39
    : callMutex_()
    , localIPAddress_("")
    , localAudioPort_(0)
40
    , localVideoPort_(0)
41
    , id_(id)
42
    , confID_()
43
    , type_(type)
44 45 46
    , connectionState_(Call::DISCONNECTED)
    , callState_(Call::INACTIVE)
    , isIPToIP_(false)
47
    , peerNumber_()
48
    , displayName_()
49 50 51 52 53
    , timestamp_start_(0)
    , timestamp_stop_(0)
{
    time(&timestamp_start_);
}
jpbl's avatar
jpbl committed
54

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

58
void
59
Call::setConnectionState(ConnectionState state)
Julien Bonjean's avatar
Julien Bonjean committed
60
{
61 62
    ost::MutexLock m(callMutex_);
    connectionState_ = state;
jpbl's avatar
jpbl committed
63 64
}

yanmorin's avatar
yanmorin committed
65
Call::ConnectionState
Julien Bonjean's avatar
Julien Bonjean committed
66 67
Call::getConnectionState()
{
68 69
    ost::MutexLock m(callMutex_);
    return connectionState_;
jpbl's avatar
jpbl committed
70 71
}

72
void
73
Call::setState(CallState state)
Julien Bonjean's avatar
Julien Bonjean committed
74
{
75 76
    ost::MutexLock m(callMutex_);
    callState_ = state;
jpbl's avatar
jpbl committed
77 78 79
}

Call::CallState
Julien Bonjean's avatar
Julien Bonjean committed
80 81
Call::getState()
{
82 83
    ost::MutexLock m(callMutex_);
    return callState_;
jpbl's avatar
jpbl committed
84 85
}

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

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

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

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


124
std::string
Julien Bonjean's avatar
Julien Bonjean committed
125 126
Call::getLocalIp()
{
127 128
    ost::MutexLock m(callMutex_);
    return localIPAddress_;
129 130
}

131
unsigned int
Julien Bonjean's avatar
Julien Bonjean committed
132 133
Call::getLocalAudioPort()
{
134 135
    ost::MutexLock m(callMutex_);
    return localAudioPort_;
136 137
}

138 139 140
unsigned int
Call::getLocalVideoPort()
{
141 142
    ost::MutexLock m (callMutex_);
    return localVideoPort_;
143 144
}

145
bool
Julien Bonjean's avatar
Julien Bonjean committed
146 147
Call::setRecording()
{
148
    bool recordStatus = Recordable::recAudio_.isRecording();
149

150
    Recordable::recAudio_.setRecording();
151
    MainBuffer &mbuffer = Manager::instance().getMainBuffer();
152
    std::string process_id = Recordable::recorder_.getRecorderID();
153

Emmanuel Milou's avatar
Emmanuel Milou committed
154
    if (!recordStatus) {
155 156
        mbuffer.bindHalfDuplexOut(process_id, id_);
        mbuffer.bindHalfDuplexOut(process_id, MainBuffer::DEFAULT_ID);
157

158
        Recordable::recorder_.start();
159
    } else {
160 161
        mbuffer.unBindHalfDuplexOut(process_id, id_);
        mbuffer.unBindHalfDuplexOut(process_id, MainBuffer::DEFAULT_ID);
162 163
    }

164
    Manager::instance().getMainBuffer().dumpInfo();
165 166 167

    return recordStatus;
}
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

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 "";
    }
}

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

197 198 199 200 201 202 203 204 205
std::map<std::string, std::string> Call::createHistoryEntry() const
{
    std::map<std::string, std::string> result;
    result[HistoryItem::ACCOUNT_ID_KEY] = Manager::instance().getAccountFromCall(id_);
    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() : "";
206 207
    result[HistoryItem::TIMESTAMP_START_KEY] = timestamp_to_string(timestamp_start_);
    result[HistoryItem::TIMESTAMP_STOP_KEY] = timestamp_to_string(timestamp_stop_);
208 209 210 211 212 213
    if (connectionState_ == RINGING)
        result[HistoryItem::STATE_KEY] = HistoryItem::MISSED_STRING;
    else
        result[HistoryItem::STATE_KEY] = getTypeStr();
    return result;
}
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

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_);
    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;
}