localrecorder.cpp 3.09 KB
Newer Older
1 2 3 4
/*
 *  Copyright (C) 2018 Savoir-faire Linux Inc.
 *
 *  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 31 32

namespace ring {

33 34
LocalRecorder::LocalRecorder(const bool& audioOnly)
{
35 36
    isAudioOnly_ = audioOnly;
    recorder_->audioOnly(audioOnly);
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
}

void
LocalRecorder::setPath(const std::string& path)
{
    if (isRecording()) {
        RING_ERR("can't set path while recording");
        return;
    }

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

bool
LocalRecorder::startRecording()
{
    if (isRecording()) {
        RING_ERR("recording already started!");
        return false;
    }

    if (path_.empty()) {
        RING_ERR("could not start recording (path not set)");
        return false;
    }

    if (!recorder_) {
        RING_ERR("could not start recording (no recorder)");
        return false;
    }

    // audio recording
70 71 72
    // create read offset in RingBuffer
    Manager::instance().getRingBufferPool().bindHalfDuplexOut(path_, RingBufferPool::DEFAULT_ID);
    Manager::instance().startAudioDriverStream();
73

74
    audioInput_ = ring::getAudioInput(path_);
Philippe Gorley's avatar
Philippe Gorley committed
75
    audioInput_->setFormat(AudioFormat::STEREO());
76
    audioInput_->attach(recorder_->addStream(audioInput_->getInfo()));
77 78 79 80

#ifdef RING_VIDEO
    // video recording
    if (!isAudioOnly_) {
81 82
        videoInput_ = std::static_pointer_cast<video::VideoInput>(ring::getVideoCamera());
        if (videoInput_) {
83
            videoInput_->attach(recorder_->addStream(videoInput_->getInfo()));
84
        } else {
85
            RING_ERR() << "Unable to record video (no video input)";
86 87 88 89 90 91 92 93 94 95 96
            return false;
        }
    }
#endif

    return Recordable::startRecording(path_);
}

void
LocalRecorder::stopRecording()
{
97
    Recordable::stopRecording();
98
    Manager::instance().getRingBufferPool().unBindHalfDuplexOut(path_, RingBufferPool::DEFAULT_ID);
99 100
    if (auto ob = recorder_->getStream(audioInput_->getInfo().name))
        audioInput_->detach(ob);
101
    if (videoInput_)
102 103
        if (auto ob = recorder_->getStream(videoInput_->getInfo().name))
            videoInput_->detach(ob);
104 105
    audioInput_.reset();
    videoInput_.reset();
106 107 108
}

} // namespace ring