MediaRecorder.cpp 3.46 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) {

xiongziliang committed
44 45 46 47
    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
48

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

55
#if defined(ENABLE_HLS)
56
    if(enableHls) {
57
        auto m3u8FilePath = hlsPath + "/" + strVhost + "/" + strApp + "/" + strId + "/hls.m3u8";
58
        _hlsMaker.reset(new HlsRecorder(m3u8FilePath,string(VHOST_KEY) + "=" + strVhost ,hlsBufSize, hlsDuration, hlsNum));
59
    }
60
#endif //defined(ENABLE_HLS)
61

62
#if defined(ENABLE_MP4V2)
xiongziliang committed
63 64
    GET_CONFIG_AND_REGISTER(string,recordPath,Record::kFilePath);
    GET_CONFIG_AND_REGISTER(string,recordAppName,Record::kAppName);
65

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

MediaRecorder::~MediaRecorder() {
}

xiongziliang committed
76
void MediaRecorder::inputFrame(const Frame::Ptr &frame) {
77
#if defined(ENABLE_HLS)
xiongziliang committed
78 79
    if (_hlsMaker) {
        _hlsMaker->inputFrame(frame);
80
    }
81 82 83
#endif //defined(ENABLE_HLS)

#if defined(ENABLE_MP4V2)
xiongziliang committed
84 85
    if (_mp4Maker) {
        _mp4Maker->inputFrame(frame);
86
    }
87
#endif //defined(ENABLE_MP4V2)
xzl committed
88 89
}

xiongziliang committed
90
void MediaRecorder::addTrack(const Track::Ptr &track) {
91
#if defined(ENABLE_HLS)
xiongziliang committed
92 93
    if (_hlsMaker) {
        _hlsMaker->addTrack(track);
94
    }
95 96 97
#endif //defined(ENABLE_HLS)

#if defined(ENABLE_MP4V2)
xiongziliang committed
98 99
    if (_mp4Maker) {
        _mp4Maker->addTrack(track);
100
    }
101
#endif //defined(ENABLE_MP4V2)
xzl committed
102 103
}

xiongziliang committed
104
} /* namespace mediakit */