video_send_thread.cpp 3.94 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 "video_send_thread.h"
33
#include "video_mixer.h"
34
#include "socket_pair.h"
35
#include "client/video_controls.h"
36
#include "check.h"
37
#include "manager.h"
38

39
#include <map>
40
#include <unistd.h>
41

42

43
namespace sfl_video {
44

45 46
using std::string;

47 48
VideoSendThread::VideoSendThread(const std::string &id,
                                 const std::map<string, string> &args,
49
                                 SocketPair& socketPair) :
50
    args_(args)
51
    , id_(id)
52
	, videoEncoder_()
53 54
    , forceKeyFrame_(0)
	, frameNumber_(0)
55
    , muxContext_(socketPair.getIOContext())
56
    , sdp_()
57
{
58
    setup();
Guillaume Roguez's avatar
Guillaume Roguez committed
59 60
}

61
bool VideoSendThread::setup()
62
{
63
    const char *enc_name = args_["codec"].c_str();
64

65
    videoEncoder_ = new VideoEncoder();
66 67 68

	/* Encoder setup */
	if (!args_["width"].empty()) {
69 70
		const char *s = args_["width"].c_str();
		videoEncoder_->setOption("width", s);
71 72 73 74
	} else {
        ERROR("width option not set");
        return false;
    }
75 76

	if (!args_["height"].empty()) {
77 78
		const char *s = args_["height"].c_str();
		videoEncoder_->setOption("height", s);
79 80 81 82
	} else {
        ERROR("height option not set");
        return false;
    }
83

84 85
	videoEncoder_->setOption("bitrate", args_["bitrate"].c_str());

86 87 88 89 90 91 92 93 94 95
	if (!args_["framerate"].empty())
		videoEncoder_->setOption("framerate", args_["framerate"].c_str());

	if (!args_["parameters"].empty())
		videoEncoder_->setOption("parameters", args_["parameters"].c_str());

    if (!args_["payload_type"].empty()) {
        DEBUG("Writing stream header for payload type %s",
			  args_["payload_type"].c_str());
        videoEncoder_->setOption("payload_type", args_["payload_type"].c_str());
96
    }
97

98 99 100 101
	if (videoEncoder_->openOutput(enc_name, "rtp", args_["destination"].c_str(),
                                  NULL)) {
        ERROR("encoder openOutput() failed");
        return false;
102
    }
103

104 105 106 107
	videoEncoder_->setIOContext(muxContext_);
	if (videoEncoder_->startIO()) {
        ERROR("encoder start failed");
        return false;
108
    }
109

110 111
	videoEncoder_->print_sdp(sdp_);
    return true;
112 113
}

114
void VideoSendThread::encodeAndSendVideo(VideoFrame& input_frame)
115
{
116 117 118 119 120
	bool is_keyframe = forceKeyFrame_ > 0;

	if (is_keyframe)
		atomic_decrement(&forceKeyFrame_);

121 122
    if (videoEncoder_->encode(input_frame, is_keyframe, frameNumber_++) < 0)
        ERROR("encoding failed");
123 124
}

125
void VideoSendThread::update(Observable<VideoFrameSP>* obs, VideoFrameSP& frame_p)
126 127 128
{
    encodeAndSendVideo(*frame_p);
}
129

130
void VideoSendThread::forceKeyFrame()
131
{ atomic_increment(&forceKeyFrame_); }
132

133
} // end namespace sfl_video