diff --git a/lib/opentok/client.rb b/lib/opentok/client.rb index 2808520f..b5ee3158 100644 --- a/lib/opentok/client.rb +++ b/lib/opentok/client.rb @@ -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}" diff --git a/lib/opentok/connections.rb b/lib/opentok/connections.rb index 5a5fa7c0..86919a2e 100644 --- a/lib/opentok/connections.rb +++ b/lib/opentok/connections.rb @@ -25,4 +25,4 @@ def forceDisconnect(session_id, connection_id ) end end -end \ No newline at end of file +end diff --git a/lib/opentok/opentok.rb b/lib/opentok/opentok.rb index 6d0933ce..9062a7d7 100644 --- a/lib/opentok/opentok.rb +++ b/lib/opentok/opentok.rb @@ -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 diff --git a/lib/opentok/streams.rb b/lib/opentok/streams.rb index 59c91fde..fd302b91 100644 --- a/lib/opentok/streams.rb +++ b/lib/opentok/streams.rb @@ -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 \ No newline at end of file +end diff --git a/spec/cassettes/OpenTok_Streams/forces_all_current_streams_in_a_session_and_future_streams_joining_the_session_to_be_muted.yml b/spec/cassettes/OpenTok_Streams/forces_all_current_streams_in_a_session_and_future_streams_joining_the_session_to_be_muted.yml new file mode 100644 index 00000000..0070751b --- /dev/null +++ b/spec/cassettes/OpenTok_Streams/forces_all_current_streams_in_a_session_and_future_streams_joining_the_session_to_be_muted.yml @@ -0,0 +1,39 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.opentok.com/v2/project/123456/session/SESSIONID/mute + body: + encoding: UTF-8 + string: '{"active":"true"}' + headers: + User-Agent: + - OpenTok-Ruby-SDK/<%= version %> + X-Opentok-Auth: + - eyJpc3QiOiJwcm9qZWN0IiwiYWxnIjoiSFMyNTYifQ.eyJpc3MiOiIxMjM0NTYiLCJpYXQiOjE0OTI1MTA2NjAsImV4cCI6MTQ5MjUxMDk2MH0.BplMVhJWx4ld7KLKXqEmow6MjNPPFw9W8IHCMfeb120 + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Fri, 29 Oct 2021 11:57:18 GMT + Content-Type: + - application/json + Content-Length: + - '0' + Connection: + - keep-alive + body: + encoding: UTF-8 + string: '' + recorded_at: Tue, 18 Apr 2017 10:17:40 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/cassettes/OpenTok_Streams/forces_all_current_streams_in_a_session_to_be_muted_except_for_the_specified_excluded_streams.yml b/spec/cassettes/OpenTok_Streams/forces_all_current_streams_in_a_session_to_be_muted_except_for_the_specified_excluded_streams.yml new file mode 100644 index 00000000..8743afcc --- /dev/null +++ b/spec/cassettes/OpenTok_Streams/forces_all_current_streams_in_a_session_to_be_muted_except_for_the_specified_excluded_streams.yml @@ -0,0 +1,39 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.opentok.com/v2/project/123456/session/SESSIONID/mute + body: + encoding: UTF-8 + string: '{"excludedStreams":["b1963d15-537f-459a-be89-e00fc310b82b"]}' + headers: + User-Agent: + - OpenTok-Ruby-SDK/<%= version %> + X-Opentok-Auth: + - eyJpc3QiOiJwcm9qZWN0IiwiYWxnIjoiSFMyNTYifQ.eyJpc3MiOiIxMjM0NTYiLCJpYXQiOjE0OTI1MTA2NjAsImV4cCI6MTQ5MjUxMDk2MH0.BplMVhJWx4ld7KLKXqEmow6MjNPPFw9W8IHCMfeb120 + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Fri, 29 Oct 2021 12:01:42 GMT + Content-Type: + - application/json + Content-Length: + - '0' + Connection: + - keep-alive + body: + encoding: UTF-8 + string: '' + recorded_at: Tue, 18 Apr 2017 10:17:40 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/cassettes/OpenTok_Streams/forces_all_streams_in_a_session_to_be_muted.yml b/spec/cassettes/OpenTok_Streams/forces_all_streams_in_a_session_to_be_muted.yml new file mode 100644 index 00000000..49087043 --- /dev/null +++ b/spec/cassettes/OpenTok_Streams/forces_all_streams_in_a_session_to_be_muted.yml @@ -0,0 +1,39 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.opentok.com/v2/project/123456/session/SESSIONID/mute + body: + encoding: UTF-8 + string: "{}" + headers: + User-Agent: + - OpenTok-Ruby-SDK/<%= version %> + X-Opentok-Auth: + - eyJpc3QiOiJwcm9qZWN0IiwiYWxnIjoiSFMyNTYifQ.eyJpc3MiOiIxMjM0NTYiLCJpYXQiOjE0OTI1MTA2NjAsImV4cCI6MTQ5MjUxMDk2MH0.BplMVhJWx4ld7KLKXqEmow6MjNPPFw9W8IHCMfeb120 + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Fri, 29 Oct 2021 11:48:19 GMT + Content-Type: + - application/json + Content-Length: + - '0' + Connection: + - close + body: + encoding: UTF-8 + string: '' + recorded_at: Tue, 18 Apr 2017 10:17:40 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/cassettes/OpenTok_Streams/forces_the_specified_stream_to_be_muted.yml b/spec/cassettes/OpenTok_Streams/forces_the_specified_stream_to_be_muted.yml new file mode 100644 index 00000000..4460155e --- /dev/null +++ b/spec/cassettes/OpenTok_Streams/forces_the_specified_stream_to_be_muted.yml @@ -0,0 +1,39 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.opentok.com/v2/project/123456/session/SESSIONID/stream/STREAMID/mute + body: + encoding: UTF-8 + string: '' + headers: + User-Agent: + - OpenTok-Ruby-SDK/<%= version %> + X-Opentok-Auth: + - eyJpc3QiOiJwcm9qZWN0IiwiYWxnIjoiSFMyNTYifQ.eyJpc3MiOiIxMjM0NTYiLCJpYXQiOjE0OTI1MTA2NjAsImV4cCI6MTQ5MjUxMDk2MH0.BplMVhJWx4ld7KLKXqEmow6MjNPPFw9W8IHCMfeb120 + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Fri, 29 Oct 2021 11:01:54 GMT + Content-Type: + - application/json + Content-Length: + - '0' + Connection: + - keep-alive + body: + encoding: UTF-8 + string: '' + recorded_at: Tue, 18 Apr 2017 10:17:40 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/opentok/connection_spec.rb b/spec/opentok/connection_spec.rb index 3d682436..662714a4 100644 --- a/spec/opentok/connection_spec.rb +++ b/spec/opentok/connection_spec.rb @@ -35,4 +35,4 @@ response = connection.forceDisconnect(session_id, connection_id) expect(response).not_to be_nil end -end \ No newline at end of file +end diff --git a/spec/opentok/streams_spec.rb b/spec/opentok/streams_spec.rb index 3fc45451..211be08c 100644 --- a/spec/opentok/streams_spec.rb +++ b/spec/opentok/streams_spec.rb @@ -72,4 +72,24 @@ response = streams.layout(session_id, streams_list) expect(response).not_to be_nil end -end \ No newline at end of file + + 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