Skip to content

Commit

Permalink
Merge pull request #233 from opentok/devx-5446-force-mute-feature
Browse files Browse the repository at this point in the history
DEVX-5446 Implement Force Mute feature
  • Loading branch information
superchilled authored Dec 7, 2021
2 parents e89d18f + 441dd9c commit da77421
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 5 deletions.
39 changes: 39 additions & 0 deletions lib/opentok/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,45 @@ def forceDisconnect(session_id, connection_id)
raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}"
end

def force_mute_stream(session_id, stream_id)
response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/stream/#{stream_id}/mute", {
:headers => generate_headers("Content-Type" => "application/json")
})
case response.code
when 200
response
when 400
raise ArgumentError, "Force mute failed. Stream ID #{stream_id} or Session ID #{session_id} is invalid"
when 403
raise OpenTokAuthenticationError, "Authentication failed. API Key: #{@api_key}"
when 404
raise OpenTokConnectionError, "Either Stream ID #{stream_id} or Session ID #{session_id} is invalid"
end
rescue StandardError => e
raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}"
end

def force_mute_session(session_id, opts)
opts.extend(HashExtensions)
body = opts.camelize_keys!
response = self.class.post("/v2/project/#{@api_key}/session/#{session_id}/mute", {
:body => body.to_json,
:headers => generate_headers("Content-Type" => "application/json")
})
case response.code
when 200
response
when 400
raise ArgumentError, "Force mute failed. The request could not be processed due to a bad request"
when 403
raise OpenTokAuthenticationError, "Authentication failed. API Key: #{@api_key}"
when 404
raise OpenTokConnectionError, "Session ID #{session_id} is invalid"
end
rescue StandardError => e
raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}"
end

def signal(session_id, connection_id, opts)
opts.extend(HashExtensions)
connectionPath = connection_id.to_s.empty? ? "" : "/connection/#{connection_id}"
Expand Down
2 changes: 1 addition & 1 deletion lib/opentok/connections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def forceDisconnect(session_id, connection_id )
end

end
end
end
2 changes: 1 addition & 1 deletion lib/opentok/opentok.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def signals
@signals ||= Signals.new client
end

# A Connections object, which lets disconnect clients from an OpenTok session.
# A Connections object, which lets you disconnect clients from an OpenTok session.
def connections
@connections ||= Connections.new client
end
Expand Down
32 changes: 31 additions & 1 deletion lib/opentok/streams.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,35 @@ def layout(session_id, opts)
(200..300).include? response.code
end

# Force a specific stream connected to an OpenTok session to mute itself.
#
# @param [String] session_id The session ID of the OpenTok session.
# @param [String] stream_id The stream ID of the stream in the session.
#
def force_mute(session_id, stream_id)
response = @client.force_mute_stream(session_id, stream_id)
end

# Force all streams connected to an OpenTok session to mute themselves.
#
# @param [String] session_id The session ID of the OpenTok session.
# @param [Hash] opts An optional hash defining options for muting action. For example:
# @option opts [true, false] :active Whether streams published after this call, in
# addition to the current streams in the session, should be muted (true) or not (false).
# @option opts [Array] :excluded_streams The stream IDs for streams that should not be muted.
# This is an optional property. If you omit this property, all streams in the session will be muted.
# @example
# {
# "active": true,
# "excluded_streams": [
# "excludedStreamId1",
# "excludedStreamId2"
# ]
# }
#
def force_mute_all(session_id, opts = {})
response = @client.force_mute_session(session_id, opts)
end

end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/opentok/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
response = connection.forceDisconnect(session_id, connection_id)
expect(response).not_to be_nil
end
end
end
22 changes: 21 additions & 1 deletion spec/opentok/streams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,24 @@
response = streams.layout(session_id, streams_list)
expect(response).not_to be_nil
end
end

it "forces the specified stream to be muted", :vcr => { :erb => { :version => OpenTok::VERSION + "-Ruby-Version-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"} } do
response = streams.force_mute(session_id, stream_id)
expect(response.code).to eq(200)
end

it "forces all streams in a session to be muted", :vcr => { :erb => { :version => OpenTok::VERSION + "-Ruby-Version-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"} } do
response = streams.force_mute_all(session_id)
expect(response.code).to eq(200)
end

it "forces all current streams in a session and future streams joining the session to be muted", :vcr => { :erb => { :version => OpenTok::VERSION + "-Ruby-Version-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"} } do
response = streams.force_mute_all(session_id, { "active" => "true" })
expect(response.code).to eq(200)
end

it "forces all current streams in a session to be muted except for the specified excluded streams", :vcr => { :erb => { :version => OpenTok::VERSION + "-Ruby-Version-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"} } do
response = streams.force_mute_all(session_id, { "excludedStreams" => ["b1963d15-537f-459a-be89-e00fc310b82b"] })
expect(response.code).to eq(200)
end
end

0 comments on commit da77421

Please sign in to comment.