Class: Pili::Stream

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hub, key, client) ⇒ Stream

Returns a new instance of Stream.



34
35
36
37
38
39
40
41
42
# File 'lib/pili/stream.rb', line 34

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

Instance Attribute Details

#hubObject (readonly)

Returns the value of attribute hub.



32
33
34
# File 'lib/pili/stream.rb', line 32

def hub
  @hub
end

#keyObject (readonly)

Returns the value of attribute key.



32
33
34
# File 'lib/pili/stream.rb', line 32

def key
  @key
end

Instance Method Details

#disableObject

禁用一个流.



51
52
53
# File 'lib/pili/stream.rb', line 51

def disable
  @client.rpc.call_with_json("POST", "#{@base_url}/disabled", {:disabledTill=>-1})
end

#enableObject

启用一个流.



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

def enable
  @client.rpc.call_with_json("POST", "#{@base_url}/disabled", {:disabledTill=>0})
end

#history_activity(opt = {}) ⇒ Object

查询直播历史.

参数: start, end 是 Unix 时间戳, 限定了查询的时间范围, 0 值表示不限定, 系统会返回所有时间的直播历史.

返回:

{
  "start" => <Integer>,
  "end" => <Integer>
}


98
99
100
101
102
103
104
105
# File 'lib/pili/stream.rb', line 98

def history_activity(opt = {})
  url = "#{@base_url}/historyactivity"
  if !opt.empty?
    url += "?#{URI.encode_www_form opt}"
  end
  ret = @client.rpc.call_with_json("GET", url, nil)
  ret["items"]
end

#infoObject

Info 获得流信息.



45
46
47
48
# File 'lib/pili/stream.rb', line 45

def info
  ret = @client.rpc.call_with_json("GET", @base_url, nil)
  StreamInfo.new @hub, @key, ret["disabledTill"]
end

#live_statusObject

查询直播状态.

返回

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


74
75
76
# File 'lib/pili/stream.rb', line 74

def live_status
  @client.rpc.call_with_json("GET", "#{@base_url}/live", nil)
end

#save(opt = {}) ⇒ Object

保存直播回放.

参数: start, end 是 Unix 时间戳, 限定了保存的直播的时间范围, 0 值表示不限定, 系统会默认保存最近一次直播的内容.

返回保存的文件名, 由系统生成.



83
84
85
86
# File 'lib/pili/stream.rb', line 83

def save(opt = {})
  ret = @client.rpc.call_with_json("POST", "#{@base_url}/saveas", opt)
  ret["fname"]
end

#to_jsonObject



111
112
113
# File 'lib/pili/stream.rb', line 111

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

#to_sObject



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

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