MediaRecorder.cpp 3.91 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26 27
 */

#include "MediaRecorder.h"
xiongziliang committed
28
#include "Common/config.h"
xzl committed
29
#include "Http/HttpSession.h"
xiongzilaing committed
30 31 32
#include "Util/util.h"
#include "Util/mini.h"
#include "Network/sockutil.h"
33
#include "HlsMakerImp.h"
xiongziliang committed
34
using namespace toolkit;
xzl committed
35

xiongziliang committed
36
namespace mediakit {
xzl committed
37

38 39 40 41 42 43
MediaRecorder::MediaRecorder(const string &strVhost_tmp,
                             const string &strApp,
                             const string &strId,
                             bool enableHls,
                             bool enableMp4) {

44 45 46 47 48
    GET_CONFIG(string,hlsPath,Hls::kFilePath);
    GET_CONFIG(uint32_t,hlsBufSize,Hls::kFileBufSize);
    GET_CONFIG(uint32_t,hlsDuration,Hls::kSegmentDuration);
    GET_CONFIG(uint32_t,hlsNum,Hls::kSegmentNum);
    GET_CONFIG(bool,enableVhost,General::kEnableVhost);
xzl committed
49

50 51 52 53 54 55
    string strVhost = strVhost_tmp;
    if(trim(strVhost).empty()){
        //如果strVhost为空,则强制为默认虚拟主机
        strVhost = DEFAULT_VHOST;
    }

56
#if defined(ENABLE_HLS)
57
    if(enableHls) {
58 59 60
        string m3u8FilePath;
        if(enableVhost){
            m3u8FilePath = hlsPath + "/" + strVhost + "/" + strApp + "/" + strId + "/hls.m3u8";
xiongziliang committed
61
            _hlsRecorder.reset(new HlsRecorder(m3u8FilePath,string(VHOST_KEY) + "=" + strVhost ,hlsBufSize, hlsDuration, hlsNum));
62 63
        }else{
            m3u8FilePath = hlsPath + "/" + strApp + "/" + strId + "/hls.m3u8";
xiongziliang committed
64
            _hlsRecorder.reset(new HlsRecorder(m3u8FilePath,"",hlsBufSize, hlsDuration, hlsNum));
65
        }
66
    }
67
#endif //defined(ENABLE_HLS)
68

69
#if defined(ENABLE_MP4RECORD)
70 71
    GET_CONFIG(string,recordPath,Record::kFilePath);
    GET_CONFIG(string,recordAppName,Record::kAppName);
72

73
    if(enableMp4){
74 75 76 77 78 79
        string mp4FilePath;
        if(enableVhost){
            mp4FilePath = recordPath + "/" + strVhost + "/" + recordAppName + "/" + strApp + "/"  + strId + "/";
        } else {
            mp4FilePath = recordPath + "/" + recordAppName + "/" + strApp + "/"  + strId + "/";
        }
xiongziliang committed
80
        _mp4Recorder.reset(new MP4Recorder(mp4FilePath,strVhost,strApp,strId));
81
    }
82
#endif //defined(ENABLE_MP4RECORD)
xzl committed
83 84 85 86 87
}

MediaRecorder::~MediaRecorder() {
}

xiongziliang committed
88
void MediaRecorder::inputFrame(const Frame::Ptr &frame) {
89
#if defined(ENABLE_HLS)
xiongziliang committed
90 91
    if (_hlsRecorder) {
        _hlsRecorder->inputFrame(frame);
92
    }
93 94
#endif //defined(ENABLE_HLS)

95
#if defined(ENABLE_MP4RECORD)
xiongziliang committed
96 97
    if (_mp4Recorder) {
        _mp4Recorder->inputFrame(frame);
98
    }
99
#endif //defined(ENABLE_MP4RECORD)
xzl committed
100 101
}

xiongziliang committed
102
void MediaRecorder::addTrack(const Track::Ptr &track) {
103
#if defined(ENABLE_HLS)
xiongziliang committed
104 105
    if (_hlsRecorder) {
        _hlsRecorder->addTrack(track);
106
    }
107 108
#endif //defined(ENABLE_HLS)

xiongziliang committed
109
#if defined(ENABLE_MP4RECORD)
xiongziliang committed
110 111
    if (_mp4Recorder) {
        _mp4Recorder->addTrack(track);
112
    }
xiongziliang committed
113
#endif //defined(ENABLE_MP4RECORD)
xzl committed
114 115
}

xiongziliang committed
116
} /* namespace mediakit */