Recorder.cpp 4.06 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5 6 7 8 9
 *
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
 */
xiongziliang committed
10 11 12

#include "Recorder.h"
#include "Common/config.h"
13
#include "Common/MediaSource.h"
xiongziliang committed
14 15 16 17 18 19 20
#include "MP4Recorder.h"
#include "HlsRecorder.h"

using namespace toolkit;

namespace mediakit {

21
string Recorder::getRecordPath(Recorder::type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path) {
xiongziliang committed
22
    GET_CONFIG(bool, enableVhost, General::kEnableVhost);
23 24 25 26 27 28 29 30 31 32 33
    switch (type) {
        case Recorder::type_hls: {
            GET_CONFIG(string, hlsPath, Hls::kFilePath);
            string m3u8FilePath;
            if (enableVhost) {
                m3u8FilePath = vhost + "/" + app + "/" + stream_id + "/hls.m3u8";
            } else {
                m3u8FilePath = app + "/" + stream_id + "/hls.m3u8";
            }
            //Here we use the customized file path.
            if (!customized_path.empty()) {
34
                return File::absolutePath(m3u8FilePath, customized_path);
35 36 37 38 39 40 41 42 43 44 45 46 47 48
            }
            return File::absolutePath(m3u8FilePath, hlsPath);
        }
        case Recorder::type_mp4: {
            GET_CONFIG(string, recordPath, Record::kFilePath);
            GET_CONFIG(string, recordAppName, Record::kAppName);
            string mp4FilePath;
            if (enableVhost) {
                mp4FilePath = vhost + "/" + recordAppName + "/" + app + "/" + stream_id + "/";
            } else {
                mp4FilePath = recordAppName + "/" + app + "/" + stream_id + "/";
            }
            //Here we use the customized file path.
            if (!customized_path.empty()) {
49
                return File::absolutePath(mp4FilePath, customized_path);
50 51 52 53 54
            }
            return File::absolutePath(mp4FilePath, recordPath);
        }
        default:
            return "";
55
    }
xiongziliang committed
56
}
57

58
std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path){
59
    auto path = Recorder::getRecordPath(type, vhost, app, stream_id, customized_path);
60 61 62
    switch (type) {
        case Recorder::type_hls: {
#if defined(ENABLE_HLS)
63 64
            GET_CONFIG(bool, enable_vhost, General::kEnableVhost);
            auto ret = std::make_shared<HlsRecorder>(path, enable_vhost ? string(VHOST_KEY) + "=" + vhost : "");
65 66
            ret->setMediaSource(vhost, app, stream_id);
            return ret;
67 68
#else
            throw std::invalid_argument("hls相关功能未打开,请开启ENABLE_HLS宏后编译再测试");
69
#endif
70

71 72 73
        }

        case Recorder::type_mp4: {
xiongziliang committed
74
#if defined(ENABLE_MP4)
75
            return std::make_shared<MP4Recorder>(path, vhost, app, stream_id);
76 77
#else
            throw std::invalid_argument("mp4相关功能未打开,请开启ENABLE_MP4宏后编译再测试");
78 79 80
#endif
        }

81
        default: throw std::invalid_argument("未知的录制类型");
82 83
    }
}
84

85
bool Recorder::isRecording(type type, const string &vhost, const string &app, const string &stream_id){
86
    auto src = MediaSource::find(vhost, app, stream_id);
87 88 89 90 91 92 93
    if(!src){
        return false;
    }
    return src->isRecording(type);
}

bool Recorder::startRecord(type type, const string &vhost, const string &app, const string &stream_id,const string &customized_path){
94
    auto src = MediaSource::find(vhost, app, stream_id);
95 96
    if (!src) {
        WarnL << "未找到相关的MediaSource,startRecord失败:" << vhost << "/" << app << "/" << stream_id;
97 98
        return false;
    }
99
    return src->setupRecord(type, true, customized_path);
100 101 102
}

bool Recorder::stopRecord(type type, const string &vhost, const string &app, const string &stream_id){
103
    auto src = MediaSource::find(vhost, app, stream_id);
104 105 106 107 108 109
    if(!src){
        return false;
    }
    return src->setupRecord(type, false, "");
}

xiongziliang committed
110
} /* namespace mediakit */