kurento_rails 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8dcfeecf35003641102a7b7fad6946972d90f73
4
- data.tar.gz: 6cdf0bd799043fb6e029afca34b2be0e5af2c834
3
+ metadata.gz: 28a49fc77329e48dadd00ed81834c289ffd7e7b7
4
+ data.tar.gz: c818b3bc5ada7420c3e6bfc2624f9df42a14ad9b
5
5
  SHA512:
6
- metadata.gz: 343ad1e03d3ababe00a7273d956c66a2a4955d48c7b37b59a131c5ef29159c1a7716ba42ce33fefb4f5cf97c1ae9e21723877f0867709729206d9d742f530116
7
- data.tar.gz: cc61f729198cf610c3caa763ecda7ffd7884ec202fa9ebd961991f67ecbc79291f20c77fdc3a6171334a1d010891f7ab70a33ed9afae3100901016bb9ff9b3bf
6
+ metadata.gz: 468cef5a7b00449b9d6d20b613563403764b7614f30bcfbd697a62871704c45104a8cabe4ab2c01d37ad9b85458597a0cf820e9260a3acb91c27d59e3e5fe372
7
+ data.tar.gz: de11558b3b879b1319d5b5746b275609bc5ed37907050469a1b37aa546116de0a62331f67f99ed3cd3f03431eff2f6e6f234ae14613563352a0ec9e0b6fbf899
@@ -17,6 +17,8 @@ WebsocketRails::EventMap.describe do
17
17
  subscribe :broadcast, to: KurentoWebsocketsController, with_method: :broadcast
18
18
  subscribe :stop_broadcasting, to: KurentoWebsocketsController, with_method: :stop_broadcasting
19
19
  subscribe :view, to: KurentoWebsocketsController, with_method: :view
20
+ subscribe :stop_viewing, to: KurentoWebsocketsController, with_method: :stop_viewing
21
+ subscribe :is_live?, to: KurentoWebsocketsController, with_method: :is_live?
20
22
  end
21
23
 
22
24
  subscribe :client_disconnected, to: KurentoWebsocketsController, with_method: :user_disconnect
@@ -1,62 +1,74 @@
1
1
  class KurentoWebsocketsController < WebsocketRails::BaseController
2
+ before_action :set_stream, only: [:stop_broadcasting, :view, :start_viewing, :stop_viewing]
2
3
 
3
4
  public
4
- # just to see if this can make things work
5
- # def process_action(method, event)
6
- # puts "Calling process action with method #{method} and event #{event}"
7
- # super(method, event)
8
- # end
9
-
10
5
  def active_streams
11
- search_params = message.merge streaming: true
12
- streams = KurentoRailsVideoStream.where(search_params)
6
+ puts "Started a request for get active streams"
7
+ puts message
8
+ streams = KurentoRailsVideoStream.live.where(stream_params(message))
9
+ puts streams
13
10
  trigger_success streams
14
11
  end
15
12
 
16
13
  def recorded_streams
17
- search_params = message.merge streaming: false
18
- streams = KurentoRailsVideoStream.where(search_params)
14
+ streams = KurentoRailsVideoStream.recorded.where(stream_params(message))
19
15
  trigger_success streams
20
16
  end
21
17
 
22
18
  # tell all users that a new video is available
23
19
  def broadcast
24
- stream = KurentoRailsVideoStream.new(stream_params(message))
25
- if stream.save
20
+ puts "Starting broadcast"
21
+ puts message
22
+ @stream = KurentoRailsVideoStream.new(stream_params(message))
23
+ if @stream && @stream.save
26
24
  # notify any people watching active streams that this stream is available
27
- WebsocketRails[:video_streams].trigger 'new', stream
25
+ WebsocketRails[:video_streams].trigger 'new', @stream
28
26
  # store the fact that this user was broadcasting
29
- connection_store[:streaming] = stream.id
27
+ connection_store[:streaming] = @stream.id
30
28
  # and now trigger the success callback
31
- trigger_success stream
29
+ trigger_success @stream
32
30
  else
33
- trigger_failure stream
31
+ trigger_failure @stream
34
32
  end
35
33
  end
36
34
 
37
35
  # tell all users that this broadcast is no longer streaming
38
36
  def stop_broadcasting
39
- stream = KurentoRailsVideoStream.find(connection_store[:streaming])
40
- stream.streaming = false
41
- if stream.save
37
+ @stream.streaming = false
38
+ if @stream.save
42
39
  # perform the relevant stop streaming actions
43
- stop_streaming stream
40
+ stop_streaming @stream
44
41
  # and finally, trigger the success callback
45
- trigger_success stream
42
+ trigger_success @stream
46
43
  else
47
- trigger_failure stream
44
+ trigger_failure @stream
48
45
  end
49
46
  end
50
47
 
51
48
  # let a user know if a video is available for streaming or not
52
- def view
53
- stream = KurentoRailsVideoStream.find(stream_params(message)[:id])
49
+ def is_live?
54
50
  # if this stream is active, great
55
- if stream.streaming
56
- trigger_success stream
51
+ if @stream.streaming
52
+ trigger_success @stream
57
53
  else
58
- trigger_failure stream
54
+ trigger_failure @stream
55
+ end
56
+ end
57
+
58
+ # increment the count
59
+ def view
60
+ if @stream.live?
61
+ @stream.current_viewers = (@stream.current_viewers || 0) + 1
62
+ WebsocketRails["stream-#{@stream.id}"].trigger 'viewer-connected'
59
63
  end
64
+ @stream.total_viewers = (@stream.total_viewers || 0) + 1
65
+ if @stream.save
66
+ connection_store[:viewing] = @stream.id
67
+ end
68
+ end
69
+
70
+ def stop_viewing
71
+ on_viewer_stop @stream
60
72
  end
61
73
 
62
74
  # handle a user disconnecting from rails.
@@ -64,32 +76,28 @@ class KurentoWebsocketsController < WebsocketRails::BaseController
64
76
  def user_disconnect
65
77
  # check to see if the user was broadcasting video. If they were, then we need
66
78
  # to notify all users viewing their channel that they're no longer broadcasting
79
+ # If they were viewing video, then we need to decrement our number of active viewers
67
80
  if connection_store[:streaming]
68
- stream = KurentoRailsVideoStream.find(connection_store[:streaming])
69
- stop_streaming stream
81
+ stop_streaming @stream
82
+ elsif connection_store[:viewing]
83
+ on_viewer_stop @stream
70
84
  end
71
85
  end
72
86
 
73
87
  private
74
88
  def stream_params(params)
75
- new_params = {}
76
- new_params[:id] = params[:id]
77
- new_params[:pipeline] = params[:pipeline]
78
- new_params[:file_url] = params[:file_url]
79
- new_params[:sender_rtc] = params
80
-
81
- permit params, :id, :pipeline, :file_url, :sender_rtc, :streaming, :name
89
+ permit params, :id, :pipeline, :file_url, :sender_rtc, :streaming, :name, :meeting_id
82
90
  end
83
91
 
84
92
  def permit(original_hash, *allowed_fields)
85
93
  new_hash = {}
86
- if allowed_fields.nil?
94
+ if allowed_fields.nil? || !original_hash.is_a?(Hash)
87
95
  return {}
88
96
  end
89
97
  allowed_fields.each do |field|
90
- new_hash[field] = original_hash[field]
98
+ new_hash[field] = original_hash[field] if original_hash[field]
91
99
  end
92
- new_hash
100
+ return new_hash
93
101
  end
94
102
 
95
103
  def stop_streaming(stream)
@@ -100,4 +108,25 @@ class KurentoWebsocketsController < WebsocketRails::BaseController
100
108
  # remove the stream from the connection store
101
109
  connection_store[:streaming] = nil
102
110
  end
111
+
112
+ def on_viewer_stop(stream)
113
+ if stream && stream.live?
114
+ stream.current_viewers -= 1
115
+ stream.save
116
+ WebsocketRails["stream-#{stream.id}"].trigger 'viewer-disconnected'
117
+ end
118
+ connection_store[:viewing] = nil
119
+ end
120
+
121
+ def set_stream
122
+ if message && stream_params(message).has_key?(:id)
123
+ @stream = KurentoRailsVideoStream.find(stream_params(message)[:id])
124
+ elsif connection_store
125
+ if connection_store.has_key?(:streaming)
126
+ @stream = KurentoRailsVideoStream.find(connection_store[:streaming])
127
+ elsif connection_store.has_key?(:viewing)
128
+ @stream = KurentoRailsVideoStream.find(connection_store[:viewing])
129
+ end
130
+ end
131
+ end
103
132
  end
@@ -320,7 +320,10 @@
320
320
  return onLiveViewError(error);
321
321
  }
322
322
  if (_this.view.live.kurentoObjects.callback) {
323
- return _this.view.live.kurentoObjects.callback(null, "Successfully started stream!");
323
+ _this.view.live.kurentoObjects.callback(null, "Successfully started stream!");
324
+ }
325
+ if (_this.view.live.kurentoObjects.stream) {
326
+ return _this.rails_websocket_dispatcher.trigger('streams.view', _this.view.live.kurentoObjects.stream);
324
327
  }
325
328
  });
326
329
  });
@@ -335,6 +338,7 @@
335
338
  }
336
339
  _this.view.live.kurentoObjects.callback = callback;
337
340
  _this.view.live.kurentoObjects.videoElement = videoElement;
341
+ _this.view.live.kurentoObjects.stream = stream;
338
342
  if (videoElement === null || $(videoElement).prop("tagName").toLowerCase().trim() === !"video") {
339
343
  return onLiveViewError("You must pass in a valid video tag");
340
344
  }
@@ -383,6 +387,10 @@
383
387
  _this.view.live.kurentoObjects.videoElement.src = '';
384
388
  _this.view.live.kurentoObjects.videoElement = null;
385
389
  }
390
+ if (_this.view.live.kurentoObjects.stream) {
391
+ _this.rails_websocket_dispatcher.trigger('streams.stop_viewing', _this.view.live.kurentoObjects.stream);
392
+ _this.view.live.kurentoObjects.stream = null;
393
+ }
386
394
  if (_this.view.live.kurentoObjects.pipeline) {
387
395
  _this.view.live.kurentoObjects.pipeline = null;
388
396
  }
@@ -565,7 +573,7 @@
565
573
  recorded = function() {
566
574
  return _this.view.recorded.startViewing(stream, videoElement, callback);
567
575
  };
568
- return _this.rails_websocket_dispatcher.trigger('streams.view', '', live, recorded);
576
+ return _this.rails_websocket_dispatcher.trigger('streams.is_live?', '', live, recorded);
569
577
  };
570
578
  })(this);
571
579
  this.view.stopViewing = (function(_this) {
@@ -586,7 +594,7 @@
586
594
  recorded = function() {
587
595
  return _this.view.recorded.onEndOfStream(stream, endOfStreamHandler);
588
596
  };
589
- return _this.rails_websocket_dispatcher.trigger('streams.view', '', live, recorded);
597
+ return _this.rails_websocket_dispatcher.trigger('streams.is_live?', '', live, recorded);
590
598
  };
591
599
  })(this);
592
600
  this.view.currentlyViewing = (function(_this) {
@@ -1,3 +1,3 @@
1
1
  module KurentoRails
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -17,6 +17,8 @@ WebsocketRails::EventMap.describe do
17
17
  subscribe :broadcast, to: KurentoWebsocketsController, with_method: :broadcast
18
18
  subscribe :stop_broadcasting, to: KurentoWebsocketsController, with_method: :stop_broadcasting
19
19
  subscribe :view, to: KurentoWebsocketsController, with_method: :view
20
+ subscribe :stop_viewing, to: KurentoWebsocketsController, with_method: :stop_viewing
21
+ subscribe :is_live?, to: KurentoWebsocketsController, with_method: :is_live?
20
22
  end
21
23
 
22
24
  subscribe :client_disconnected, to: KurentoWebsocketsController, with_method: :user_disconnect
@@ -208,6 +208,7 @@ class StreamingHelper
208
208
  @view.live.kurentoObjects.presenterWebRtcEndpoint.connect endpoint, (error) =>
209
209
  return onLiveViewError(error) if error or not @view.live.kurentoObjects.pipeline
210
210
  @view.live.kurentoObjects.callback(null, "Successfully started stream!") if @view.live.kurentoObjects.callback
211
+ @rails_websocket_dispatcher.trigger 'streams.view', @view.live.kurentoObjects.stream if @view.live.kurentoObjects.stream
211
212
 
212
213
  #### Start Viewing (view.live.startViewing)
213
214
  # This function accesses a live stream and starts it viewing. There is no error checking for if the stream is actually live, so make sure you know that it is before you start it viewing.
@@ -219,6 +220,7 @@ class StreamingHelper
219
220
  @view.live.startViewing = (stream, videoElement, callback = null) =>
220
221
  @view.live.kurentoObjects.callback = callback
221
222
  @view.live.kurentoObjects.videoElement = videoElement
223
+ @view.live.kurentoObjects.stream = stream
222
224
  return onLiveViewError("You must pass in a valid video tag") if videoElement is null or $(videoElement).prop("tagName").toLowerCase().trim() is not "video"
223
225
  videoElement = $(videoElement)[0]
224
226
  @kurento.getMediaobjectById stream.sender_rtc, (error, webRtcEndpoint) =>
@@ -247,6 +249,9 @@ class StreamingHelper
247
249
  if @view.live.kurentoObjects.videoElement
248
250
  @view.live.kurentoObjects.videoElement.src = ''
249
251
  @view.live.kurentoObjects.videoElement = null
252
+ if @view.live.kurentoObjects.stream
253
+ @rails_websocket_dispatcher.trigger 'streams.stop_viewing', @view.live.kurentoObjects.stream
254
+ @view.live.kurentoObjects.stream = null
250
255
  @view.live.kurentoObjects.pipeline = null if @view.live.kurentoObjects.pipeline
251
256
  @view.live.kurentoObjects.presenterWebRtcEndpoint = null if @view.live.kurentoObjects.presenterWebRtcEndpoint
252
257
  callback() if callback
@@ -346,7 +351,7 @@ class StreamingHelper
346
351
  @view.startViewing = (stream, videoElement, callback = null) =>
347
352
  live = => @view.live.startViewing(stream, videoElement, callback)
348
353
  recorded = => @view.recorded.startViewing(stream, videoElement, callback)
349
- @rails_websocket_dispatcher.trigger 'streams.view', '', live, recorded
354
+ @rails_websocket_dispatcher.trigger 'streams.is_live?', '', live, recorded
350
355
 
351
356
  #### Stop Viewing (view.stopViewing)
352
357
  # This is a helper function that makes no distinction about whether the currently playing stream is live or recorded (or even if you have one of each playing). It will stop the streams either way.
@@ -359,7 +364,7 @@ class StreamingHelper
359
364
  @view.onEndOfStream = (stream, endOfStreamHandler) =>
360
365
  live = => @view.live.onEndOfStream(stream, endOfStreamHandler)
361
366
  recorded = => @view.recorded.onEndOfStream(stream, endOfStreamHandler)
362
- @rails_websocket_dispatcher.trigger 'streams.view', '', live, recorded
367
+ @rails_websocket_dispatcher.trigger 'streams.is_live?', '', live, recorded
363
368
 
364
369
  #### Currently Viewing
365
370
  # This is just a simple boolean function to determine if something is currently playing
@@ -1,62 +1,74 @@
1
1
  class KurentoWebsocketsController < WebsocketRails::BaseController
2
+ before_action :set_stream, only: [:stop_broadcasting, :view, :start_viewing, :stop_viewing]
2
3
 
3
4
  public
4
- # just to see if this can make things work
5
- # def process_action(method, event)
6
- # puts "Calling process action with method #{method} and event #{event}"
7
- # super(method, event)
8
- # end
9
-
10
5
  def active_streams
11
- search_params = message.merge streaming: true
12
- streams = KurentoRailsVideoStream.where(search_params)
6
+ puts "Started a request for get active streams"
7
+ puts message
8
+ streams = KurentoRailsVideoStream.live.where(stream_params(message))
9
+ puts streams
13
10
  trigger_success streams
14
11
  end
15
12
 
16
13
  def recorded_streams
17
- search_params = message.merge streaming: false
18
- streams = KurentoRailsVideoStream.where(search_params)
14
+ streams = KurentoRailsVideoStream.recorded.where(stream_params(message))
19
15
  trigger_success streams
20
16
  end
21
17
 
22
18
  # tell all users that a new video is available
23
19
  def broadcast
24
- stream = KurentoRailsVideoStream.new(stream_params(message))
25
- if stream.save
20
+ puts "Starting broadcast"
21
+ puts message
22
+ @stream = KurentoRailsVideoStream.new(stream_params(message))
23
+ if @stream && @stream.save
26
24
  # notify any people watching active streams that this stream is available
27
- WebsocketRails[:video_streams].trigger 'new', stream
25
+ WebsocketRails[:video_streams].trigger 'new', @stream
28
26
  # store the fact that this user was broadcasting
29
- connection_store[:streaming] = stream.id
27
+ connection_store[:streaming] = @stream.id
30
28
  # and now trigger the success callback
31
- trigger_success stream
29
+ trigger_success @stream
32
30
  else
33
- trigger_failure stream
31
+ trigger_failure @stream
34
32
  end
35
33
  end
36
34
 
37
35
  # tell all users that this broadcast is no longer streaming
38
36
  def stop_broadcasting
39
- stream = KurentoRailsVideoStream.find(connection_store[:streaming])
40
- stream.streaming = false
41
- if stream.save
37
+ @stream.streaming = false
38
+ if @stream.save
42
39
  # perform the relevant stop streaming actions
43
- stop_streaming stream
40
+ stop_streaming @stream
44
41
  # and finally, trigger the success callback
45
- trigger_success stream
42
+ trigger_success @stream
46
43
  else
47
- trigger_failure stream
44
+ trigger_failure @stream
48
45
  end
49
46
  end
50
47
 
51
48
  # let a user know if a video is available for streaming or not
52
- def view
53
- stream = KurentoRailsVideoStream.find(stream_params(message)[:id])
49
+ def is_live?
54
50
  # if this stream is active, great
55
- if stream.streaming
56
- trigger_success stream
51
+ if @stream.streaming
52
+ trigger_success @stream
57
53
  else
58
- trigger_failure stream
54
+ trigger_failure @stream
55
+ end
56
+ end
57
+
58
+ # increment the count
59
+ def view
60
+ if @stream.live?
61
+ @stream.current_viewers = (@stream.current_viewers || 0) + 1
62
+ WebsocketRails["stream-#{@stream.id}"].trigger 'viewer-connected'
59
63
  end
64
+ @stream.total_viewers = (@stream.total_viewers || 0) + 1
65
+ if @stream.save
66
+ connection_store[:viewing] = @stream.id
67
+ end
68
+ end
69
+
70
+ def stop_viewing
71
+ on_viewer_stop @stream
60
72
  end
61
73
 
62
74
  # handle a user disconnecting from rails.
@@ -64,32 +76,28 @@ class KurentoWebsocketsController < WebsocketRails::BaseController
64
76
  def user_disconnect
65
77
  # check to see if the user was broadcasting video. If they were, then we need
66
78
  # to notify all users viewing their channel that they're no longer broadcasting
79
+ # If they were viewing video, then we need to decrement our number of active viewers
67
80
  if connection_store[:streaming]
68
- stream = KurentoRailsVideoStream.find(connection_store[:streaming])
69
- stop_streaming stream
81
+ stop_streaming @stream
82
+ elsif connection_store[:viewing]
83
+ on_viewer_stop @stream
70
84
  end
71
85
  end
72
86
 
73
87
  private
74
88
  def stream_params(params)
75
- new_params = {}
76
- new_params[:id] = params[:id]
77
- new_params[:pipeline] = params[:pipeline]
78
- new_params[:file_url] = params[:file_url]
79
- new_params[:sender_rtc] = params
80
-
81
- permit params, :id, :pipeline, :file_url, :sender_rtc, :streaming, :name
89
+ permit params, :id, :pipeline, :file_url, :sender_rtc, :streaming, :name, :meeting_id
82
90
  end
83
91
 
84
92
  def permit(original_hash, *allowed_fields)
85
93
  new_hash = {}
86
- if allowed_fields.nil?
94
+ if allowed_fields.nil? || !original_hash.is_a?(Hash)
87
95
  return {}
88
96
  end
89
97
  allowed_fields.each do |field|
90
- new_hash[field] = original_hash[field]
98
+ new_hash[field] = original_hash[field] if original_hash[field]
91
99
  end
92
- new_hash
100
+ return new_hash
93
101
  end
94
102
 
95
103
  def stop_streaming(stream)
@@ -100,4 +108,25 @@ class KurentoWebsocketsController < WebsocketRails::BaseController
100
108
  # remove the stream from the connection store
101
109
  connection_store[:streaming] = nil
102
110
  end
111
+
112
+ def on_viewer_stop(stream)
113
+ if stream && stream.live?
114
+ stream.current_viewers -= 1
115
+ stream.save
116
+ WebsocketRails["stream-#{stream.id}"].trigger 'viewer-disconnected'
117
+ end
118
+ connection_store[:viewing] = nil
119
+ end
120
+
121
+ def set_stream
122
+ if message && stream_params(message).has_key?(:id)
123
+ @stream = KurentoRailsVideoStream.find(stream_params(message)[:id])
124
+ elsif connection_store
125
+ if connection_store.has_key?(:streaming)
126
+ @stream = KurentoRailsVideoStream.find(connection_store[:streaming])
127
+ elsif connection_store.has_key?(:viewing)
128
+ @stream = KurentoRailsVideoStream.find(connection_store[:viewing])
129
+ end
130
+ end
131
+ end
103
132
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kurento_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Stephenson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-12 00:00:00.000000000 Z
11
+ date: 2016-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler