Class: GrubY::NTgCalls::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gruubY/ntgcalls.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
20

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lib_path: nil) ⇒ Client

Returns a new instance of Client.

Raises:



32
33
34
35
36
# File 'lib/gruubY/ntgcalls.rb', line 32

def initialize(lib_path: nil)
  Native.load!(lib_path)
  @handle = Native.ntg_init
  raise Error, "ntgcalls init failed" if @handle.nil? || @handle.zero?
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



30
31
32
# File 'lib/gruubY/ntgcalls.rb', line 30

def handle
  @handle
end

Class Method Details

.read_c_string_ptr(pointer_to_pointer) ⇒ Object



176
177
178
179
180
181
# File 'lib/gruubY/ntgcalls.rb', line 176

def read_c_string_ptr(pointer_to_pointer)
  ptr = pointer_to_pointer.read_pointer
  return "" if ptr.null?

  ptr.read_string
end

.version(lib_path: nil) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
# File 'lib/gruubY/ntgcalls.rb', line 38

def self.version(lib_path: nil)
  Native.load!(lib_path)
  version_ptr = FFI::MemoryPointer.new(:pointer)
  code = Native.ntg_get_version(version_ptr)
  raise Error, "ntg_get_version failed with code=#{code}" if code.negative?

  read_c_string_ptr(version_ptr)
end

Instance Method Details

#closeObject Also known as: stop

Raises:



47
48
49
50
51
52
53
54
# File 'lib/gruubY/ntgcalls.rb', line 47

def close
  return if @handle.nil? || @handle.zero?

  code = Native.ntg_destroy(@handle)
  raise Error, "ntg_destroy failed with code=#{code}" if code.negative?

  @handle = nil
end

#connect(chat_id:, params:, is_presentation: false, timeout: DEFAULT_TIMEOUT) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/gruubY/ntgcalls.rb', line 62

def connect(chat_id:, params:, is_presentation: false, timeout: DEFAULT_TIMEOUT)
  call_async(
    :ntg_connect,
    [handle!, chat_id.to_i, params.to_s, !!is_presentation],
    timeout: timeout
  )
end

#cpu_usage(timeout: DEFAULT_TIMEOUT) ⇒ Object



111
112
113
114
115
# File 'lib/gruubY/ntgcalls.rb', line 111

def cpu_usage(timeout: DEFAULT_TIMEOUT)
  out = FFI::MemoryPointer.new(:double)
  call_async(:ntg_cpu_usage, [handle!, out], timeout: timeout)
  out.read_double
end

#create(chat_id:, timeout: DEFAULT_TIMEOUT) ⇒ Object



58
59
60
# File 'lib/gruubY/ntgcalls.rb', line 58

def create(chat_id:, timeout: DEFAULT_TIMEOUT)
  call_async_string(:ntg_create, [handle!, chat_id.to_i], timeout: timeout)
end

#mute(chat_id:, timeout: DEFAULT_TIMEOUT) ⇒ Object



99
100
101
# File 'lib/gruubY/ntgcalls.rb', line 99

def mute(chat_id:, timeout: DEFAULT_TIMEOUT)
  call_async(:ntg_mute, [handle!, chat_id.to_i], timeout: timeout)
end

#pause(chat_id:, timeout: DEFAULT_TIMEOUT) ⇒ Object



91
92
93
# File 'lib/gruubY/ntgcalls.rb', line 91

def pause(chat_id:, timeout: DEFAULT_TIMEOUT)
  call_async(:ntg_pause, [handle!, chat_id.to_i], timeout: timeout)
end

#play(chat_id:, stream:, sample_rate: 48_000, channels: 2, keep_open: false, timeout: DEFAULT_TIMEOUT) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gruubY/ntgcalls.rb', line 70

def play(chat_id:, stream:, sample_rate: 48_000, channels: 2, keep_open: false, timeout: DEFAULT_TIMEOUT)
  audio = Native::NtgAudioDescriptionStruct.new
  audio[:mediaSource] = MediaSource::FILE
  audio[:input] = stream.to_s
  audio[:sampleRate] = sample_rate.to_i
  audio[:channelCount] = channels.to_i
  audio[:keepOpen] = !!keep_open

  media = Native::NtgMediaDescriptionStruct.new
  media[:microphone] = audio.to_ptr
  media[:speaker] = FFI::Pointer::NULL
  media[:camera] = FFI::Pointer::NULL
  media[:screen] = FFI::Pointer::NULL

  call_async(
    :ntg_set_stream_sources,
    [handle!, chat_id.to_i, StreamMode::CAPTURE, media],
    timeout: timeout
  )
end

#resume(chat_id:, timeout: DEFAULT_TIMEOUT) ⇒ Object



95
96
97
# File 'lib/gruubY/ntgcalls.rb', line 95

def resume(chat_id:, timeout: DEFAULT_TIMEOUT)
  call_async(:ntg_resume, [handle!, chat_id.to_i], timeout: timeout)
end

#stop_call(chat_id:, timeout: DEFAULT_TIMEOUT) ⇒ Object



107
108
109
# File 'lib/gruubY/ntgcalls.rb', line 107

def stop_call(chat_id:, timeout: DEFAULT_TIMEOUT)
  call_async(:ntg_stop, [handle!, chat_id.to_i], timeout: timeout)
end

#time(chat_id:, mode: StreamMode::CAPTURE, timeout: DEFAULT_TIMEOUT) ⇒ Object



117
118
119
120
121
# File 'lib/gruubY/ntgcalls.rb', line 117

def time(chat_id:, mode: StreamMode::CAPTURE, timeout: DEFAULT_TIMEOUT)
  out = FFI::MemoryPointer.new(:int64)
  call_async(:ntg_time, [handle!, chat_id.to_i, mode.to_i, out], timeout: timeout)
  out.read_int64
end

#unmute(chat_id:, timeout: DEFAULT_TIMEOUT) ⇒ Object



103
104
105
# File 'lib/gruubY/ntgcalls.rb', line 103

def unmute(chat_id:, timeout: DEFAULT_TIMEOUT)
  call_async(:ntg_unmute, [handle!, chat_id.to_i], timeout: timeout)
end