localrecorder.cpp 3.2 KB
Newer Older
1
/*
2
 *  Copyright (C) 2018-2019 Savoir-faire Linux Inc.
3 4
 *
 *  Author: Hugo Lefeuvre <hugo.lefeuvre@savoirfairelinux.com>
5
 *  Author: Philippe Gorley <philippe.gorley@savoirfairelinux.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  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
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#include "localrecorder.h"
#include "audio/ringbufferpool.h"
#include "audio/ringbuffer.h"
25
#include "client/videomanager.h"
26 27 28
#include "media_stream.h"
#include "manager.h"
#include "logger.h"
29
#include "client/videomanager.h"
30

Adrien Béraud's avatar
Adrien Béraud committed
31
namespace jami {
32

33 34
LocalRecorder::LocalRecorder(const bool& audioOnly)
{
35 36
    isAudioOnly_ = audioOnly;
    recorder_->audioOnly(audioOnly);
37 38
}

39 40 41 42 43 44
LocalRecorder::~LocalRecorder()
{
    if (isRecording())
        stopRecording();
}

45 46 47 48
void
LocalRecorder::setPath(const std::string& path)
{
    if (isRecording()) {
Adrien Béraud's avatar
Adrien Béraud committed
49
        JAMI_ERR("can't set path while recording");
50 51 52 53 54 55 56 57 58 59 60
        return;
    }

    recorder_->setPath(path);
    path_ = path;
}

bool
LocalRecorder::startRecording()
{
    if (isRecording()) {
Adrien Béraud's avatar
Adrien Béraud committed
61
        JAMI_ERR("recording already started!");
62 63 64 65
        return false;
    }

    if (path_.empty()) {
Adrien Béraud's avatar
Adrien Béraud committed
66
        JAMI_ERR("could not start recording (path not set)");
67 68 69 70
        return false;
    }

    if (!recorder_) {
Adrien Béraud's avatar
Adrien Béraud committed
71
        JAMI_ERR("could not start recording (no recorder)");
72 73 74 75
        return false;
    }

    // audio recording
76 77 78
    // create read offset in RingBuffer
    Manager::instance().getRingBufferPool().bindHalfDuplexOut(path_, RingBufferPool::DEFAULT_ID);
    Manager::instance().startAudioDriverStream();
79

Adrien Béraud's avatar
Adrien Béraud committed
80
    audioInput_ = jami::getAudioInput(path_);
81
    audioInput_->setFormat(AudioFormat::STEREO());
82
    audioInput_->attach(recorder_->addStream(audioInput_->getInfo()));
83

Adrien Béraud's avatar
Adrien Béraud committed
84
#ifdef ENABLE_VIDEO
85 86
    // video recording
    if (!isAudioOnly_) {
Adrien Béraud's avatar
Adrien Béraud committed
87
        videoInput_ = std::static_pointer_cast<video::VideoInput>(jami::getVideoCamera());
88
        if (videoInput_) {
89
            videoInput_->attach(recorder_->addStream(videoInput_->getInfo()));
90
        } else {
Adrien Béraud's avatar
Adrien Béraud committed
91
            JAMI_ERR() << "Unable to record video (no video input)";
92 93 94 95 96 97 98 99 100 101 102
            return false;
        }
    }
#endif

    return Recordable::startRecording(path_);
}

void
LocalRecorder::stopRecording()
{
103 104
    if (auto ob = recorder_->getStream(audioInput_->getInfo().name))
        audioInput_->detach(ob);
105
    if (videoInput_)
106 107
        if (auto ob = recorder_->getStream(videoInput_->getInfo().name))
            videoInput_->detach(ob);
108 109 110
    Manager::instance().getRingBufferPool().unBindHalfDuplexOut(path_, RingBufferPool::DEFAULT_ID);
    // NOTE stopRecording should be last call to avoid data races
    Recordable::stopRecording();
111 112
}

Adrien Béraud's avatar
Adrien Béraud committed
113
} // namespace jami