From 787af5109e404509a9860b1d1671c4cd7fce29ac Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 23 Apr 2020 23:45:20 +0800 Subject: [PATCH] =?UTF-8?q?api=E8=BF=94=E5=9B=9E=E9=9F=B3=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E7=9B=B8=E5=85=B3=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 64 +++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 775c80f2..3200ca93 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -372,6 +372,43 @@ void installWebApi() { #endif//#if !defined(_WIN32) + static auto makeMediaSourceJson = [](const MediaSource::Ptr &media){ + Value item; + item["schema"] = media->getSchema(); + item["vhost"] = media->getVhost(); + item["app"] = media->getApp(); + item["stream"] = media->getId(); + item["readerCount"] = media->readerCount(); + item["totalReaderCount"] = media->totalReaderCount(); + for(auto &track : media->getTracks()){ + Value obj; + auto codec_type = track->getTrackType(); + obj["codec_id"] = track->getCodecId(); + obj["ready"] = track->ready(); + obj["codec_type"] = codec_type; + switch(codec_type){ + case TrackAudio : { + auto audio_track = dynamic_pointer_cast(track); + obj["sample_rate"] = audio_track->getAudioSampleRate(); + obj["channels"] = audio_track->getAudioChannel(); + obj["sample_bit"] = audio_track->getAudioSampleBit(); + break; + } + case TrackVideo : { + auto video_track = dynamic_pointer_cast(track); + obj["width"] = video_track->getVideoWidth(); + obj["height"] = video_track->getVideoHeight(); + obj["fps"] = (int)video_track->getVideoFps(); + break; + } + default: + break; + } + item["tracks"].append(obj); + } + return item; + }; + //获取流列表,可选筛选参数 //测试url0(获取所有流) http://127.0.0.1/index/api/getMediaList //测试url1(获取虚拟主机为"__defaultVost__"的流) http://127.0.0.1/index/api/getMediaList?vhost=__defaultVost__ @@ -389,21 +426,7 @@ void installWebApi() { if(!allArgs["app"].empty() && allArgs["app"] != media->getApp()){ return; } - Value item; - item["schema"] = media->getSchema(); - item["vhost"] = media->getVhost(); - item["app"] = media->getApp(); - item["stream"] = media->getId(); - item["readerCount"] = media->readerCount(); - item["totalReaderCount"] = media->totalReaderCount(); - for(auto &track : media->getTracks()){ - Value obj; - obj["codec_id"] = track->getCodecId(); - obj["codec_type"] = track->getTrackType(); - obj["ready"] = track->ready(); - item["tracks"].append(obj); - } - val["data"].append(item); + val["data"].append(makeMediaSourceJson(media)); }); }); @@ -423,16 +446,9 @@ void installWebApi() { val["online"] = false; return; } + val = makeMediaSourceJson(src); val["online"] = true; - val["readerCount"] = src->readerCount(); - val["totalReaderCount"] = src->totalReaderCount(); - for(auto &track : src->getTracks()){ - Value obj; - obj["codec_id"] = track->getCodecId(); - obj["codec_type"] = track->getTrackType(); - obj["ready"] = track->ready(); - val["tracks"].append(obj); - } + val["code"] = API::Success; }); //主动关断流,包括关断拉流、推流