ShellCMD.h 3.9 KB
Newer Older
xiongziliang committed
1 2 3
/*
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5 6 7 8 9
 *
 * 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.
 */
10

xiongziliang committed
11 12 13
#ifndef SRC_SHELL_SHELLCMD_H_
#define SRC_SHELL_SHELLCMD_H_

14
#include "Util/CMD.h"
15
#include "Common/MediaSource.h"
xiongziliang committed
16
using namespace toolkit;
17

xiongziliang committed
18
namespace mediakit {
19

20 21

class CMD_media: public CMD {
22
public:
23 24
    CMD_media(){
        _parser.reset(new OptionParser([](const std::shared_ptr<ostream> &stream,mINI &ini){
xiongziliang committed
25 26
            MediaSource::for_each_media([&](const MediaSource::Ptr &media){
                if(!ini["schema"].empty() && ini["schema"] != media->getSchema()){
27 28 29
                    //筛选协议不匹配
                    return;
                }
xiongziliang committed
30
                if(!ini["vhost"].empty() && ini["vhost"] != media->getVhost()){
31 32 33
                    //筛选虚拟主机不匹配
                    return;
                }
xiongziliang committed
34
                if(!ini["app"].empty() && ini["app"] != media->getApp()){
35 36 37
                    //筛选应用名不匹配
                    return;
                }
xiongziliang committed
38
                if(!ini["stream"].empty() && ini["stream"] != media->getId()){
39 40 41 42 43 44
                    //流id不匹配
                    return;
                }
                if(ini.find("list") != ini.end()){
                    //列出源
                    (*stream) << "\t"
xiongziliang committed
45 46 47 48
                              << media->getSchema() << "/"
                              << media->getVhost() << "/"
                              << media->getApp() << "/"
                              << media->getId()
49 50 51 52
                              << "\r\n";
                    return;
                }

xiongziliang committed
53
                EventPollerPool::Instance().getPoller()->async([ini,media,stream](){
54 55 56 57 58 59 60 61 62 63
                    if(ini.find("kick") != ini.end()){
                        //踢出源
                        do{
                            if(!media) {
                                break;
                            }
                            if(!media->close(true)) {
                                break;
                            }
                            (*stream) << "\t踢出成功:"
xiongziliang committed
64 65 66 67
                                      << media->getSchema() << "/"
                                      << media->getVhost() << "/"
                                      << media->getApp() << "/"
                                      << media->getId()
68 69 70 71
                                      << "\r\n";
                            return;
                        }while(0);
                        (*stream) << "\t踢出失败:"
xiongziliang committed
72 73 74 75
                                  << media->getSchema() << "/"
                                  << media->getVhost() << "/"
                                  << media->getApp() << "/"
                                  << media->getId()
76
                                  << "\r\n";
77 78 79
                    }
                },false);

80 81 82 83 84 85 86 87 88

            });
        }));
        (*_parser) << Option('k', "kick", Option::ArgNone,nullptr,false, "踢出媒体源", nullptr);
        (*_parser) << Option('l', "list", Option::ArgNone,nullptr,false, "列出媒体源", nullptr);
        (*_parser) << Option('S', "schema", Option::ArgRequired,nullptr,false, "协议筛选", nullptr);
        (*_parser) << Option('v', "vhost", Option::ArgRequired,nullptr,false, "虚拟主机筛选", nullptr);
        (*_parser) << Option('a', "app", Option::ArgRequired,nullptr,false, "应用名筛选", nullptr);
        (*_parser) << Option('s', "stream", Option::ArgRequired,nullptr,false, "流id筛选", nullptr);
89
    }
90
    virtual ~CMD_media() {}
91
    const char *description() const override {
92
        return "媒体源相关操作.";
93 94 95
    }
};

xiongziliang committed
96
} /* namespace mediakit */
97

xiongziliang committed
98
#endif //SRC_SHELL_SHELLCMD_H_