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(credentials, options = {}) ⇒ Stream

Returns a new instance of Stream.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pili/stream.rb', line 9

def initialize(credentials, options = {})
  @credentials      = credentials
  @id               = options["id"]
  @title            = options["title"]
  @hub              = options["hub"]
  @profiles         = options["profiles"] || []
  @publish_key      = options["publishKey"]
  @publish_security = options["publishSecurity"]
  @disabled         = options["disabled"]
  @hosts            = options["hosts"]

  @created_at       = options["createdAt"]
  @updated_at       = options["updatedAt"]
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



5
6
7
# File 'lib/pili/stream.rb', line 5

def created_at
  @created_at
end

#credentialsObject (readonly)

Returns the value of attribute credentials.



5
6
7
# File 'lib/pili/stream.rb', line 5

def credentials
  @credentials
end

#disabledObject

Returns the value of attribute disabled.



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

def disabled
  @disabled
end

#hostsObject (readonly)

Returns the value of attribute hosts.



5
6
7
# File 'lib/pili/stream.rb', line 5

def hosts
  @hosts
end

#hubObject (readonly)

Returns the value of attribute hub.



5
6
7
# File 'lib/pili/stream.rb', line 5

def hub
  @hub
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/pili/stream.rb', line 5

def id
  @id
end

#profilesObject (readonly)

Returns the value of attribute profiles.



5
6
7
# File 'lib/pili/stream.rb', line 5

def profiles
  @profiles
end

#publish_keyObject

Returns the value of attribute publish_key.



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

def publish_key
  @publish_key
end

#publish_securityObject

Returns the value of attribute publish_security.



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

def publish_security
  @publish_security
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/pili/stream.rb', line 5

def title
  @title
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



5
6
7
# File 'lib/pili/stream.rb', line 5

def updated_at
  @updated_at
end

Instance Method Details

#deleteObject



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

def delete
  API.delete_stream(@credentials, @id)
end

#disableObject



40
41
42
# File 'lib/pili/stream.rb', line 40

def disable
  API.update_stream(@credentials, @id, disabled: true)
end

#enableObject



35
36
37
# File 'lib/pili/stream.rb', line 35

def enable
  API.update_stream(@credentials, @id, disabled: false)
end

#hls_live_urlsObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pili/stream.rb', line 94

def hls_live_urls
  live_hls_host = @hosts["live"]["hls"]

  urls = { Config.origin => "http://#{live_hls_host}/#{@hub}/#{@title}.m3u8" }

  @profiles.each do |profile|
    urls[profile] = "http://#{live_hls_host}/#{@hub}/#{@title}@#{profile}.m3u8"
  end

  urls
end

#hls_playback_urls(start_second = -1,, end_second = -1)) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/pili/stream.rb', line 107

def hls_playback_urls(start_second = -1, end_second = -1)
  name = Time.now.to_i.to_s
  resp = API.save_stream_as(@credentials, @id, name, nil, start_second, end_second)
  urls = { Config.origin => resp["url"] }

  urls
end

#http_flv_live_urlsObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pili/stream.rb', line 81

def http_flv_live_urls
  live_hdl_host = @hosts["live"]["hdl"]

  urls = { Config.origin => "http://#{live_hdl_host}/#{@hub}/#{@title}.flv" }

  @profiles.each do |profile|
    urls[profile] = "http://#{live_hdl_host}/#{@hub}/#{@title}@#{profile}.flv"
  end

  urls
end

#rtmp_live_urlsObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pili/stream.rb', line 68

def rtmp_live_urls
  live_rtmp_host = @hosts["live"]["rtmp"]

  urls = { Config.origin => "rtmp://#{live_rtmp_host}/#{@hub}/#{@title}" }

  @profiles.each do |profile|
    urls[profile] = "rtmp://#{live_rtmp_host}/#{@hub}/#{@title}@#{profile}"
  end

  urls
end

#rtmp_publish_urlObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pili/stream.rb', line 55

def rtmp_publish_url
  rtmp_publish_host = @hosts["publish"]["rtmp"]

  if @publish_security == "static"
    return "rtmp://#{rtmp_publish_host}/#{@hub}/#{@title}?key=#{@publish_key}"
  else
    nonce = Time.now.to_i
    token = Credentials.sign(publish_key, "/#{@hub}/#{@title}?nonce=#{nonce}")
    return "rtmp://#{rtmp_publish_host}/#{@hub}/#{@title}?nonce=#{nonce}&token=#{token}"
  end
end

#save_as(name, format, start_time, end_time, notify_url = nil) ⇒ Object



137
138
139
# File 'lib/pili/stream.rb', line 137

def save_as(name, format, start_time, end_time, notify_url = nil)
  API.save_stream_as(@credentials, @id, name, format, start_time, end_time, notify_url)
end

#segments(options = {}) ⇒ Object



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

def segments(options = {})
  API.get_stream_segments(@credentials, @id, options)
end

#snapshot(name, format, options = {}) ⇒ Object



132
133
134
# File 'lib/pili/stream.rb', line 132

def snapshot(name, format, options = {})
  API.snapshot(@credentials, @id, name, format, options)
end

#statusObject



25
26
27
# File 'lib/pili/stream.rb', line 25

def status
  API.get_stream_status(@credentials, @id)
end

#to_hObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/pili/stream.rb', line 142

def to_h
  {
    credentials:      @credentials,
    id:               @id,
    title:            @title,
    hub:              @hub,
    profiles:         @profiles,
    publish_key:      @publish_key,
    publish_security: @publish_security,
    disabled:         @disabled,
    hosts:            @hosts,
    created_at:       @created_at,
    updated_at:       @updated_at
  }
end

#to_jsonObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pili/stream.rb', line 116

def to_json
  {
    id: @id,
    title: @title,
    hub: @hub,
    profiles: @profiles,
    publish_key: @publish_key,
    publish_security: @publish_security,
    disabled: @disabled,
    hosts: @hosts,
    created_at: @created_at,
    updated_at: @updated_at
  }.to_json
end

#update(options = {}) ⇒ Object



30
31
32
# File 'lib/pili/stream.rb', line 30

def update(options = {})
  API.update_stream(@credentials, @id, self.to_h.merge!(options))
end