Class: Pili::Hub

Inherits:
Object
  • Object
show all
Defined in:
lib/pili/hub.rb

Overview

Hub 表示一个 Hub, 一个 Hub 包含多个 Stream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hub, client) ⇒ Hub

Returns a new instance of Hub.



8
9
10
11
12
# File 'lib/pili/hub.rb', line 8

def initialize(hub, client)
  @hub = hub
  @client = client
  @base_url = "#{Config.api_base_url}/hubs/#{hub}"
end

Instance Attribute Details

#hubObject (readonly)

Returns the value of attribute hub.



6
7
8
# File 'lib/pili/hub.rb', line 6

def hub
  @hub
end

Instance Method Details

#batch_query_live_status(stream_titles) ⇒ Object

批量查询直播实时信息

参数:

stream_titles 字符串数组,其中元素是想要查询的流的标题。

返回:

{
  "key" => <String>, # 流标题
  "startAt" => <Integer>, # 直播开始的 Unix 时间戳, 0 表示当前没在直播.
  "clientIp" => <String>, # 直播的客户端 IP.
  "bps" => <Integer>, # 直播的码率、帧率信息.
  "fps" => {
    "audio" => <Integer>,
    "video" => <Integer>,
    "data" => <Integer>
  }
}

注意:

查询的流不存在或不在直播不会出现在返回结果里。



107
108
109
110
# File 'lib/pili/hub.rb', line 107

def batch_query_live_status(stream_titles)
  ret = @client.rpc.call_with_json("POST", "#{@base_url}/livestreams", {:items=>stream_titles})
  ret["items"]
end

#create(stream_title) ⇒ Object

创建一个流对象. 使用一个合法 rtmp_publish_url 发起推流就会自动创建流对象. 一般情况下不需要调用这个 API, 除非是想提前对这一个流做一些特殊配置.



17
18
19
20
# File 'lib/pili/hub.rb', line 17

def create(stream_title)
  @client.rpc.call_with_json("POST", "#{@base_url}/streams", {:key => stream_title})
  Stream.new(@hub, stream_title, @client)
end

#list(opt = {}) ⇒ Object

list 根据 prefix 遍历 Hub 的流列表.

参数:

limit 限定了一次最多可以返回的流个数, 实际返回的流个数可能小于这个 limit 值. marker 是上一次遍历得到的流标. omarker 记录了此次遍历到的游标, 在下次请求时应该带上, 如果 omarker 为 “” 表示已经遍历完所有流.

For example:

marker = ""
while true
  titles, marker = hub.list(:marker=> marker)
  # do something with titles.
  if marker == ""
    break
  end
end


56
57
58
59
# File 'lib/pili/hub.rb', line 56

def list(opt = {})
  opt[:liveonly] = false
  plist(opt)
end

#list_live(opt = {}) ⇒ Object

list 根据 prefix 遍历 Hub 正在播放的流列表.

参数:

limit 限定了一次最多可以返回的流个数, 实际返回的流个数可能小于这个 limit 值. marker 是上一次遍历得到的流标. omarker 记录了此次遍历到的游标, 在下次请求时应该带上, 如果 omarker 为 “” 表示已经遍历完所有流.

For example:

marker = ""
while true
  titles, marker = hub.list_live(:marker=> marker)
  # do something with titles.
  if marker == ""
    break
  end
end


79
80
81
82
# File 'lib/pili/hub.rb', line 79

def list_live(opt = {})
  opt[:liveonly] = true
  plist(opt)
end

#stream(stream_title) ⇒ Object

初始化一个流对象.



23
24
25
# File 'lib/pili/hub.rb', line 23

def stream(stream_title)
  Stream.new(@hub, stream_title, @client)
end

#to_jsonObject



116
117
118
# File 'lib/pili/hub.rb', line 116

def to_json
  {:hub=>@hub}.to_json
end

#to_sObject



112
113
114
# File 'lib/pili/hub.rb', line 112

def to_s
  "#<#{self.class} #{@hub}>"
end