MediaSource.cpp 6.25 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * MIT License
 *
 * 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.
 */


#include "MediaSource.h"
#include "MediaFile/MediaReader.h"
#include "Util/util.h"
#include "Rtsp/Rtsp.h"
32
#include "Network/sockutil.h"
33

xiongziliang committed
34
using namespace toolkit;
35

xiongziliang committed
36
namespace mediakit {
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

recursive_mutex MediaSource::g_mtxMediaSrc;
MediaSource::SchemaVhostAppStreamMap MediaSource::g_mapMediaSrc;

MediaSource::Ptr MediaSource::find(
        const string &schema,
        const string &vhost_tmp,
        const string &app,
        const string &id,
        bool bMake) {
    string vhost = vhost_tmp;
    if(vhost.empty()){
        vhost = DEFAULT_VHOST;
    }
    lock_guard<recursive_mutex> lock(g_mtxMediaSrc);
    MediaSource::Ptr ret;
    searchMedia(schema, vhost, app, id,
                [&](SchemaVhostAppStreamMap::iterator &it0 ,
                    VhostAppStreamMap::iterator &it1,
                    AppStreamMap::iterator &it2,
                    StreamMap::iterator &it3){
                    ret = it3->second.lock();
                    if(!ret){
                        //该对象已经销毁
                        it2->second.erase(it3);
                        eraseIfEmpty(it0,it1,it2);
                        return false;
                    }
                    return true;
                });
    if(!ret && bMake){
        //查找某一媒体源,找到后返回
        ret = MediaReader::onMakeMediaSource(schema, vhost,app,id);
    }
    return ret;

}
bool MediaSource::regist() {
    //注册该源,注册后服务器才能找到该源
76 77 78 79 80 81
    bool success;
    {
        lock_guard<recursive_mutex> lock(g_mtxMediaSrc);
        auto pr = g_mapMediaSrc[_strSchema][_strVhost][_strApp].emplace(_strId, shared_from_this());
        success = pr.second;
    }
82
    if(success){
83
        InfoL << _strSchema << " " << _strVhost << " " << _strApp << " " << _strId;
84 85
        NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged,
                                           true,
86 87 88 89
                                           _strSchema,
                                           _strVhost,
                                           _strApp,
                                           _strId,
90
                                           *this);
91 92 93 94 95 96
    }
    return success;
}
bool MediaSource::unregist() {
    //反注册该源
    lock_guard<recursive_mutex> lock(g_mtxMediaSrc);
97
    return searchMedia(_strSchema, _strVhost, _strApp, _strId, [&](SchemaVhostAppStreamMap::iterator &it0 ,
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                                                                       VhostAppStreamMap::iterator &it1,
                                                                       AppStreamMap::iterator &it2,
                                                                       StreamMap::iterator &it3){
        auto strongMedia = it3->second.lock();
        if(strongMedia && this != strongMedia.get()){
            //不是自己,不允许反注册
            return false;
        }
        it2->second.erase(it3);
        eraseIfEmpty(it0,it1,it2);
        unregisted();
        return true;
    });
}
void MediaSource::unregisted(){
113
    InfoL <<  "" <<  _strSchema << " " << _strVhost << " " << _strApp << " " << _strId;
114 115
    NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged,
                                       false,
116 117 118 119
                                       _strSchema,
                                       _strVhost,
                                       _strApp,
                                       _strId,
120
                                       *this);
121 122 123 124 125 126
}

void MediaInfo::parse(const string &url){
    //string url = "rtsp://127.0.0.1:8554/live/id?key=val&a=1&&b=2&vhost=vhost.com";
    auto schema_pos = url.find("://");
    if(schema_pos != string::npos){
127
        _schema = url.substr(0,schema_pos);
128 129 130 131 132 133 134 135
    }else{
        schema_pos = -3;
    }
    auto split_vec = split(url.substr(schema_pos + 3),"/");
    if(split_vec.size() > 0){
        auto vhost = split_vec[0];
        auto pos = vhost.find(":");
        if(pos != string::npos){
136 137
            _host = _vhost = vhost.substr(0,pos);
            _port = vhost.substr(pos + 1);
138
        } else{
139
            _host = _vhost = vhost;
140 141 142
        }
    }
    if(split_vec.size() > 1){
143
        _app = split_vec[1];
144 145 146 147 148 149 150 151 152 153 154
    }
    if(split_vec.size() > 2){
        string steamid;
        for(int i = 2 ; i < split_vec.size() ; ++i){
            steamid.append(split_vec[i] + "/");
        }
        if(steamid.back() == '/'){
            steamid.pop_back();
        }
        auto pos = steamid.find("?");
        if(pos != string::npos){
155
            _streamid = steamid.substr(0,pos);
156 157
            _param_strs = steamid.substr(pos + 1);
            _params = Parser::parseArgs(_param_strs);
158 159
            if(_params.find(VHOST_KEY) != _params.end()){
                _vhost = _params[VHOST_KEY];
160 161
            }
        } else{
162
            _streamid = steamid;
163 164
        }
    }
165
    if(_vhost.empty()){
166
        //无效vhost
167
        _vhost = DEFAULT_VHOST;
168
    }else{
169
        if(INADDR_NONE != inet_addr(_vhost.data())){
170
            //这是ip,未指定vhost;使用默认vhost
171
            _vhost = DEFAULT_VHOST;
172 173 174 175 176
        }
    }
}


xiongziliang committed
177
} /* namespace mediakit */