qiang.li

get stream info use http api #227

@@ -188,6 +188,10 @@ public: @@ -188,6 +188,10 @@ public:
188 * when ingester start to play stream. 188 * when ingester start to play stream.
189 */ 189 */
190 virtual int on_ingest_play(); 190 virtual int on_ingest_play();
  191 + /**
  192 + * get state info.
  193 + */
  194 + virtual int get_state() { return state; }
191 }; 195 };
192 196
193 /** 197 /**
@@ -219,6 +223,10 @@ public: @@ -219,6 +223,10 @@ public:
219 * proxy unpublish stream to edge. 223 * proxy unpublish stream to edge.
220 */ 224 */
221 virtual void on_proxy_unpublish(); 225 virtual void on_proxy_unpublish();
  226 + /**
  227 + * get state info.
  228 + */
  229 + virtual int get_state() { return state; }
222 }; 230 };
223 231
224 #endif 232 #endif
@@ -35,6 +35,8 @@ using namespace std; @@ -35,6 +35,8 @@ using namespace std;
35 #include <srs_app_json.hpp> 35 #include <srs_app_json.hpp>
36 #include <srs_kernel_utility.hpp> 36 #include <srs_kernel_utility.hpp>
37 #include <srs_app_utility.hpp> 37 #include <srs_app_utility.hpp>
  38 +#include <srs_app_source.hpp>
  39 +#include <srs_protocol_rtmp.hpp>
38 40
39 SrsApiRoot::SrsApiRoot() 41 SrsApiRoot::SrsApiRoot()
40 { 42 {
@@ -122,6 +124,8 @@ SrsApiV1::SrsApiV1() @@ -122,6 +124,8 @@ SrsApiV1::SrsApiV1()
122 handlers.push_back(new SrsApiMemInfos()); 124 handlers.push_back(new SrsApiMemInfos());
123 handlers.push_back(new SrsApiAuthors()); 125 handlers.push_back(new SrsApiAuthors());
124 handlers.push_back(new SrsApiRequests()); 126 handlers.push_back(new SrsApiRequests());
  127 + handlers.push_back(new SrsApiVhosts());
  128 + handlers.push_back(new SrsApiStreams());
125 } 129 }
126 130
127 SrsApiV1::~SrsApiV1() 131 SrsApiV1::~SrsApiV1()
@@ -147,7 +151,9 @@ int SrsApiV1::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) @@ -147,7 +151,9 @@ int SrsApiV1::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
147 << __SRS_JFIELD_STR("system_proc_stats", "the system process stats") << __SRS_JFIELD_CONT 151 << __SRS_JFIELD_STR("system_proc_stats", "the system process stats") << __SRS_JFIELD_CONT
148 << __SRS_JFIELD_STR("meminfos", "the meminfo of system") << __SRS_JFIELD_CONT 152 << __SRS_JFIELD_STR("meminfos", "the meminfo of system") << __SRS_JFIELD_CONT
149 << __SRS_JFIELD_STR("authors", "the primary authors and contributors") << __SRS_JFIELD_CONT 153 << __SRS_JFIELD_STR("authors", "the primary authors and contributors") << __SRS_JFIELD_CONT
150 - << __SRS_JFIELD_STR("requests", "the request itself, for http debug") 154 + << __SRS_JFIELD_STR("requests", "the request itself, for http debug") << __SRS_JFIELD_CONT
  155 + << __SRS_JFIELD_STR("vhosts", "list all vhosts") << __SRS_JFIELD_CONT
  156 + << __SRS_JFIELD_STR("streams?(name/vhost)=xxx", "list streams that match the name or vhost")
151 << __SRS_JOBJECT_END 157 << __SRS_JOBJECT_END
152 << __SRS_JOBJECT_END; 158 << __SRS_JOBJECT_END;
153 159
@@ -500,6 +506,88 @@ int SrsApiAuthors::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) @@ -500,6 +506,88 @@ int SrsApiAuthors::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
500 return res_json(skt, req, ss.str()); 506 return res_json(skt, req, ss.str());
501 } 507 }
502 508
  509 +SrsApiVhosts::SrsApiVhosts()
  510 +{
  511 +}
  512 +
  513 +SrsApiVhosts::~SrsApiVhosts()
  514 +{
  515 +}
  516 +
  517 +bool SrsApiVhosts::can_handle(const char* path, int length, const char** /*pchild*/)
  518 +{
  519 + return srs_path_equals("/vhosts", path, length);
  520 +}
  521 +
  522 +int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
  523 +{
  524 + std::stringstream ss;
  525 +
  526 + ss << __SRS_JARRAY_START;
  527 + bool first = true;
  528 + std::map<std::string, SrsSource*> *source_pool = SrsSource::get_source_pool();
  529 + std::map<std::string, SrsSource*>::iterator it;
  530 + for (it=source_pool->begin(); it!=source_pool->end(); it++) {
  531 + SrsRequest* source_req = it->second->get_reqinfo();
  532 + if (first) first = false;
  533 + else ss << __SRS_JFIELD_CONT;
  534 +
  535 + ss << "\"" << source_req->vhost << "\"";
  536 + }
  537 + ss << __SRS_JARRAY_END;
  538 +
  539 + return res_json(skt, req, ss.str());
  540 +}
  541 +
  542 +SrsApiStreams::SrsApiStreams()
  543 +{
  544 +}
  545 +
  546 +SrsApiStreams::~SrsApiStreams()
  547 +{
  548 +}
  549 +
  550 +bool SrsApiStreams::can_handle(const char* path, int length, const char** /*pchild*/)
  551 +{
  552 + return srs_path_equals("/streams", path, length);
  553 +}
  554 +
  555 +int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
  556 +{
  557 + std::stringstream ss;
  558 +
  559 + std::string query_name = req->query_get("name");
  560 + std::string query_vhost = req->query_get("vhost");
  561 + if (query_name.size()>0 || query_vhost.size()>0) {
  562 + ss << __SRS_JARRAY_START;
  563 + bool first = true;
  564 + std::map<std::string, SrsSource*> *source_pool = SrsSource::get_source_pool();
  565 + std::map<std::string, SrsSource*>::iterator it;
  566 + for (it=source_pool->begin(); it!=source_pool->end(); it++) {
  567 + SrsSource* source = it->second;
  568 + SrsRequest* source_req = source->get_reqinfo();
  569 + if (source_req->stream==query_name || source_req->vhost==query_vhost) {
  570 + if (first) first = false;
  571 + else ss << __SRS_JFIELD_CONT;
  572 +
  573 + ss << __SRS_JOBJECT_START
  574 + << __SRS_JFIELD_STR("name", source_req->stream) << __SRS_JFIELD_CONT
  575 + << __SRS_JFIELD_STR("url", source_req->tcUrl) << __SRS_JFIELD_CONT
  576 + << __SRS_JFIELD_ORG("clients", source->get_consumers_size()) << __SRS_JFIELD_CONT
  577 + << __SRS_JFIELD_STR("status", (source->can_publish()?"idle":"streaming")) << __SRS_JFIELD_CONT
  578 + << __SRS_JFIELD_STR("type", source->get_source_type()) << __SRS_JFIELD_CONT
  579 + << __SRS_JFIELD_STR("codec", "")
  580 + << __SRS_JOBJECT_END;
  581 + }
  582 + }
  583 + ss << __SRS_JARRAY_END;
  584 + } else {
  585 + return res_error(skt, req, 400, "Bad Request", "unknown query");
  586 + }
  587 +
  588 + return res_json(skt, req, ss.str());
  589 +}
  590 +
503 SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler) 591 SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler)
504 : SrsConnection(srs_server, client_stfd) 592 : SrsConnection(srs_server, client_stfd)
505 { 593 {
@@ -164,6 +164,28 @@ protected: @@ -164,6 +164,28 @@ protected:
164 virtual int do_process_request(SrsStSocket* skt, SrsHttpMessage* req); 164 virtual int do_process_request(SrsStSocket* skt, SrsHttpMessage* req);
165 }; 165 };
166 166
  167 +class SrsApiVhosts : public SrsHttpHandler
  168 +{
  169 +public:
  170 + SrsApiVhosts();
  171 + virtual ~SrsApiVhosts();
  172 +public:
  173 + virtual bool can_handle(const char* path, int length, const char** pchild);
  174 +protected:
  175 + virtual int do_process_request(SrsStSocket* skt, SrsHttpMessage* req);
  176 +};
  177 +
  178 +class SrsApiStreams : public SrsHttpHandler
  179 +{
  180 +public:
  181 + SrsApiStreams();
  182 + virtual ~SrsApiStreams();
  183 +public:
  184 + virtual bool can_handle(const char* path, int length, const char** pchild);
  185 +protected:
  186 + virtual int do_process_request(SrsStSocket* skt, SrsHttpMessage* req);
  187 +};
  188 +
167 class SrsHttpApi : public SrsConnection 189 class SrsHttpApi : public SrsConnection
168 { 190 {
169 private: 191 private:
@@ -1807,4 +1807,13 @@ void SrsSource::destroy_forwarders() @@ -1807,4 +1807,13 @@ void SrsSource::destroy_forwarders()
1807 forwarders.clear(); 1807 forwarders.clear();
1808 } 1808 }
1809 1809
1810 - 1810 +std::string SrsSource::get_source_type()
  1811 +{
  1812 + if (play_edge->get_state() == SrsEdgeStateIngestConnected) {
  1813 + return "origin pull";
  1814 + } else if (publish_edge->get_state() == SrsEdgeStatePublish) {
  1815 + return "edge publish";
  1816 + } else {
  1817 + return "normal publish";
  1818 + }
  1819 +}
@@ -486,6 +486,12 @@ public: @@ -486,6 +486,12 @@ public:
486 private: 486 private:
487 virtual int create_forwarders(); 487 virtual int create_forwarders();
488 virtual void destroy_forwarders(); 488 virtual void destroy_forwarders();
  489 +//get information
  490 +public:
  491 + static std::map<std::string, SrsSource*>* get_source_pool() { return &pool; }
  492 + virtual SrsRequest* get_reqinfo() { return _req; }
  493 + virtual std::size_t get_consumers_size() { return consumers.size(); }
  494 + virtual std::string get_source_type();
489 }; 495 };
490 496
491 #endif 497 #endif