HlsMakerImp.cpp 3.29 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * 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.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 */

#include "HlsMakerImp.h"
#include "Util/util.h"
#include "Util/uv_errno.h"
using namespace toolkit;

namespace mediakit {

HlsMakerImp::HlsMakerImp(const string &m3u8_file,
                         const string &params,
                         uint32_t bufSize,
                         float seg_duration,
                         uint32_t seg_number) : HlsMaker(seg_duration, seg_number) {
    _path_prefix = m3u8_file.substr(0, m3u8_file.rfind('/'));
    _path_hls = m3u8_file;
    _params = params;
    _buf_size = bufSize;
    _file_buf.reset(new char[bufSize],[](char *ptr){
        delete[] ptr;
    });
}

HlsMakerImp::~HlsMakerImp() {
xiongziliang committed
33
    //录制完了
xiongziliang committed
34
    flushLastSegment(true);
xiongziliang committed
35
    if(isLive()){
xiongziliang committed
36 37 38
        //hls直播才删除文件
        File::delete_file(_path_prefix.data());
    }
39 40
}

xiongziliang committed
41
string HlsMakerImp::onOpenSegment(int index) {
42 43 44
    string segment_name , segment_path;
    {
        auto strDate = getTimeStr("%Y-%m-%d");
45 46 47
        auto strHour = getTimeStr("%H");
        auto strTime = getTimeStr("%M-%S");
        segment_name = StrPrinter << strDate + "/" + strHour + "/" + strTime << "_" << index << ".ts";
48
        segment_path = _path_prefix + "/" +  segment_name;
xiongziliang committed
49 50 51
        if(isLive()){
            _segment_file_paths.emplace(index,segment_path);
        }
52 53
    }
    _file = makeFile(segment_path, true);
54
    if(!_file){
55
        WarnL << "create file falied," << segment_path << " " <<  get_uv_errmsg();
56 57
    }
    if(_params.empty()){
58
        return std::move(segment_name);
59
    }
60
    return std::move(segment_name + "?" + _params);
61 62
}

xiongziliang committed
63
void HlsMakerImp::onDelSegment(int index) {
64 65 66 67 68 69
    auto it = _segment_file_paths.find(index);
    if(it == _segment_file_paths.end()){
        return;
    }
    File::delete_file(it->second.data());
    _segment_file_paths.erase(it);
70 71
}

xiongziliang committed
72
void HlsMakerImp::onWriteSegment(const char *data, int len) {
73 74 75 76 77 78 79 80 81 82
    if (_file) {
        fwrite(data, len, 1, _file.get());
    }
}

void HlsMakerImp::onWriteHls(const char *data, int len) {
    auto hls = makeFile(_path_hls);
    if(hls){
        fwrite(data,len,1,hls.get());
        hls.reset();
83 84 85
        if(_media_src){
            _media_src->registHls();
        }
86 87 88 89 90 91 92 93
    } else{
        WarnL << "create hls file falied," << _path_hls << " " <<  get_uv_errmsg();
    }
    //DebugL << "\r\n"  << string(data,len);
}


std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file,bool setbuf) {
94
    auto file_buf = _file_buf;
xiongziliang committed
95
    auto ret= shared_ptr<FILE>(File::create_file(file.data(), "wb"), [file_buf](FILE *fp) {
96 97 98 99 100 101 102 103 104 105
        if (fp) {
            fclose(fp);
        }
    });
    if(ret && setbuf){
        setvbuf(ret.get(), _file_buf.get(), _IOFBF, _buf_size);
    }
    return ret;
}

106
void HlsMakerImp::setMediaSource(const string &vhost, const string &app, const string &stream_id) {
107 108 109
    _media_src = std::make_shared<HlsMediaSource>(vhost, app, stream_id);
}

110 111 112 113
MediaSource::Ptr HlsMakerImp::getMediaSource() const{
    return _media_src;
}

114
}//namespace mediakit