video_rtp_session.cpp 7.46 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3
 *  Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
4
 *  Author: Guillaume Roguez <Guillaume.Roguez@savoirfairelinux.com>
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 *  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 3 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
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 *  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.
 */

32
#include "client/video_controls.h"
33
#include "video_rtp_session.h"
34 35
#include "video_send_thread.h"
#include "video_receive_thread.h"
36
#include "video_mixer.h"
37
#include "socket_pair.h"
38
#include "sip/sdp.h"
39
#include "sip/sipvoiplink.h"
40
#include "manager.h"
Tristan Matthews's avatar
Tristan Matthews committed
41
#include "logger.h"
42

43 44 45 46 47 48
#include <sstream>
#include <map>
#include <string>
#include <thread>
#include <chrono>

49
namespace sfl_video {
50

51 52 53
using std::map;
using std::string;

54 55
VideoRtpSession::VideoRtpSession(const string &callID,
								 const map<string, string> &txArgs) :
56
    socketPair_(), sendThread_(), receiveThread_(), txArgs_(txArgs),
57
    rxArgs_(), sending_(false), receiving_(false), callID_(callID),
58
    videoMixer_(), videoLocal_()
59
{}
60

61
VideoRtpSession::~VideoRtpSession()
62
{ stop(); }
63

64
void VideoRtpSession::updateSDP(const Sdp &sdp)
65
{
66
    string desc(sdp.getIncomingVideoDescription());
67
    // if port has changed
68
    if (not desc.empty() and desc != rxArgs_["receiving_sdp"]) {
69
        rxArgs_["receiving_sdp"] = desc;
70
        DEBUG("Updated incoming SDP to:\n%s",
71
              rxArgs_["receiving_sdp"].c_str());
72
    }
73

74 75 76 77 78
    if (desc.empty()) {
        DEBUG("Video is inactive");
        receiving_ = false;
        sending_ = false;
    } else if (desc.find("sendrecv") != string::npos) {
79
        DEBUG("Sending and receiving video");
80 81
        receiving_ = true;
        sending_ = true;
82
    } else if (desc.find("inactive") != string::npos) {
83
        DEBUG("Video is inactive");
84
        receiving_ = false;
85
        sending_ = false;
86
    } else if (desc.find("sendonly") != string::npos) {
87
        DEBUG("Receiving video disabled, video set to sendonly");
88 89
        receiving_ = false;
        sending_ = true;
90
    } else if (desc.find("recvonly") != string::npos) {
91
        DEBUG("Sending video disabled, video set to recvonly");
92 93 94
        sending_ = false;
        receiving_ = true;
    }
95 96
    // even if it says sendrecv or recvonly, our peer may disable video by
    // setting the port to 0
97
    if (desc.find("m=video 0") != string::npos) {
98
        DEBUG("Receiving video disabled, port was set to 0");
99 100
        receiving_ = false;
    }
101

102 103
    if (sending_)
        sending_ = sdp.getOutgoingVideoSettings(txArgs_);
104 105
}

106 107
void VideoRtpSession::updateDestination(const string &destination,
                                        unsigned int port)
108
{
109 110 111 112
    if (destination.empty()) {
        ERROR("Destination is empty, ignoring");
        return;
    }
113 114

    std::stringstream tmp;
115
    tmp << "rtp://" << destination << ":" << port;
116
    // if destination has changed
117
    if (tmp.str() != txArgs_["destination"]) {
118 119 120 121
        if (sendThread_.get() != 0) {
            ERROR("Video is already being sent");
            return;
        }
122
        txArgs_["destination"] = tmp.str();
123
        DEBUG("updated dest to %s",  txArgs_["destination"].c_str());
124
    }
125

126
    if (port == 0) {
127
        DEBUG("Sending video disabled, port was set to 0");
128 129
        sending_ = false;
    }
130 131
}

132
void VideoRtpSession::start(int localPort)
133
{
134 135 136
    if (not sending_ and not receiving_)
        return;

137 138 139
    try {
        socketPair_.reset(new SocketPair(txArgs_["destination"].c_str(), localPort));
    } catch (const std::runtime_error &e) {
140
        ERROR("Socket creation failed on port %d: %s", localPort, e.what());
141 142
        return;
    }
143

144 145 146 147 148 149 150 151 152
	if (sending_) {
        // Local video startup if needed
        auto videoCtrl = Manager::instance().getVideoControls();
        if (!videoCtrl->hasPreviewStarted()) {
            videoCtrl->startPreview();
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }

        videoLocal_ = videoCtrl->getVideoPreview();
153 154 155 156
        if (sendThread_.get())
            WARN("Restarting video sender");

        sendThread_.reset(new VideoSendThread(callID_, txArgs_, *socketPair_));
157 158 159 160 161
    } else {
        DEBUG("Video sending disabled");
        sendThread_.reset();
    }

162
    if (receiving_) {
163 164
        if (receiveThread_.get())
            WARN("restarting video receiver");
165
        receiveThread_.reset(new VideoReceiveThread(callID_, rxArgs_));
166
        receiveThread_->setRequestKeyFrameCallback(&SIPVoIPLink::enqueueKeyframeRequest);
167
        receiveThread_->addIOContext(*socketPair_);
168 169
        receiveThread_->start();
    } else {
170
        DEBUG("Video receiving disabled");
171
        receiveThread_.reset();
172
    }
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

    // Setup pipeline
    if (videoMixer_) {
        // We are in conference mode

        if (sendThread_) {
            videoLocal_->detach(sendThread_.get());
            videoMixer_->attach(sendThread_.get());
        }

        if (receiveThread_) {
            receiveThread_->enterConference();
            receiveThread_->attach(videoMixer_);
        }
    } else {
        if (sendThread_)
            videoLocal_->attach(sendThread_.get());
    }
191 192
}

193
void VideoRtpSession::stop()
194
{
195 196 197 198 199 200 201
    if (videoLocal_)
        videoLocal_->detach(sendThread_.get());

    if (videoMixer_) {
        videoMixer_->detach(sendThread_.get());
        receiveThread_->detach(videoMixer_);
    }
202

203 204
    if (socketPair_.get())
        socketPair_->interrupt();
205

206 207
    receiveThread_.reset();
    sendThread_.reset();
208
    socketPair_.reset();
209
}
210

211 212
void VideoRtpSession::forceKeyFrame()
{
213 214
    if (sendThread_.get())
        sendThread_->forceKeyFrame();
215 216
}

217
void VideoRtpSession::addReceivingDetails(std::map<std::string, std::string> &details)
218
{
219 220 221 222 223 224 225 226 227 228
    if (receiveThread_)
        receiveThread_->addReceivingDetails(details);
}

void VideoRtpSession::enterConference(Conference *conf)
{
    auto mixer = conf->getVideoMixer();
    if (!mixer) {
        ERROR("No conference mixer!");
        return;
229
    }
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

    /* Detach from a possible previous conference */
    exitConference();

    videoMixer_ = mixer; // catched during start()
}

void VideoRtpSession::exitConference()
{
    if (videoMixer_) {
        if (sendThread_)
            videoMixer_->detach(sendThread_.get());

        if (receiveThread_) {
            receiveThread_->detach(videoMixer_);
            receiveThread_->exitConference();
        }

        videoMixer_ = nullptr;
    }

    if (videoLocal_)
        videoLocal_->attach(sendThread_.get());
253 254
}

255
} // end namespace sfl_video