ShellCMD.h 3.56 KB
Newer Older
1
//
2 3 4
// Created by xzl on 2017/12/1.
//

xiongziliang committed
5 6 7
#ifndef SRC_SHELL_SHELLCMD_H_
#define SRC_SHELL_SHELLCMD_H_

8
#include "Util/CMD.h"
9
#include "Common/MediaSource.h"
xiongziliang committed
10
using namespace toolkit;
11

xiongziliang committed
12
namespace mediakit {
13

14 15

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

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

74 75 76 77 78 79 80 81 82

            });
        }));
        (*_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);
83
    }
84
    virtual ~CMD_media() {}
85
    const char *description() const override {
86
        return "媒体源相关操作.";
87 88 89
    }
};

xiongziliang committed
90
} /* namespace mediakit */
91

xiongziliang committed
92
#endif //SRC_SHELL_SHELLCMD_H_