MediaRecorder.cpp 3.19 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * 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"
xiongziliang committed
33
using namespace toolkit;
xzl committed
34

xiongziliang committed
35
namespace mediakit {
xzl committed
36

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

xiongziliang committed
43 44 45 46
    GET_CONFIG_AND_REGISTER(string,hlsPath,Hls::kFilePath);
    GET_CONFIG_AND_REGISTER(uint32_t,hlsBufSize,Hls::kFileBufSize);
    GET_CONFIG_AND_REGISTER(uint32_t,hlsDuration,Hls::kSegmentDuration);
    GET_CONFIG_AND_REGISTER(uint32_t,hlsNum,Hls::kSegmentNum);
xzl committed
47

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

    if(enableHls) {
55
        auto m3u8FilePath = hlsPath + "/" + strVhost + "/" + strApp + "/" + strId + "/hls.m3u8";
56
        _hlsMaker.reset(new HLSMaker(m3u8FilePath,hlsBufSize, hlsDuration, hlsNum));
57
    }
58

59
#ifdef ENABLE_MP4V2
xiongziliang committed
60 61
    GET_CONFIG_AND_REGISTER(string,recordPath,Record::kFilePath);
    GET_CONFIG_AND_REGISTER(string,recordAppName,Record::kAppName);
62

63
    if(enableMp4){
64
        auto mp4FilePath = recordPath + "/" + strVhost + "/" + recordAppName + "/" + strApp + "/"  + strId + "/";
65
        _mp4Maker.reset(new Mp4Maker(mp4FilePath,strVhost,strApp,strId));
66
    }
67
#endif //ENABLE_MP4V2
xzl committed
68 69 70 71 72
}

MediaRecorder::~MediaRecorder() {
}

xiongziliang committed
73 74 75
void MediaRecorder::inputFrame(const Frame::Ptr &frame) {
    if (_hlsMaker) {
        _hlsMaker->inputFrame(frame);
76
    }
77
#ifdef ENABLE_MP4V2
xiongziliang committed
78 79
    if (_mp4Maker) {
        _mp4Maker->inputFrame(frame);
80
    }
81
#endif //ENABLE_MP4V2
xzl committed
82 83
}

xiongziliang committed
84 85 86
void MediaRecorder::addTrack(const Track::Ptr &track) {
    if (_hlsMaker) {
        _hlsMaker->addTrack(track);
87
    }
88
#ifdef ENABLE_MP4V2
xiongziliang committed
89 90
    if (_mp4Maker) {
        _mp4Maker->addTrack(track);
91
    }
92
#endif //ENABLE_MP4V2
xzl committed
93 94
}

xiongziliang committed
95
} /* namespace mediakit */