~ehmry/nim_tox

7e7a2bc0f94a465733023fb9473597fcbcb98506 — Emery Hemingway 5 months ago c4e39e7
WiP! av groupchat
1 files changed, 40 insertions(+), 9 deletions(-)

M src/toxcore/av.nim
M src/toxcore/av.nim => src/toxcore/av.nim +40 -9
@@ 139,14 139,8 @@ callbackSetter video_receive_frame:
      ystride, ustride, vstride: int32; userData: pointer) {.cdecl.} =
    callThru(friend, width, height, y, u, v, ystride, ustride, vstride)

callbackSetter group_audio:
  type cbType = proc (av: AvCore; group: Conference; peer: Peer; pcm: ptr int16;
      samples: uint32; channels: uint8; sampleRate: uint32;
      userData: pointer) {.cdecl.}
  proc wrapper(av: AvCore; group: Conference; peer: Peer; pcm: ptr int16;
      samples: uint32; channels: uint8; sampleRate: uint32;
      userData: pointer) {.cdecl.} =
    callThru(group, peer, cast[Samples](pcm), (int)samples, channels, sampleRate)
proc onGroupAudio*(av: ToxAv; callback: CbGroupAudio) =
  av.groupAudio = callback

type Err_New* {.ctoxError.} = enum
  TOXAV_ERR_NEW_OK,


@@ 420,6 414,42 @@ proc videoSetBitRate*(av: ToxAv; friend: Friend; bitRate: uint32): bool =
  ctoxCheck(TOXAV_ERR_BIT_RATE_SET_OK) do:
    result = video_set_bit_rate(av.core, friend, bitRate, err)

type toxav_group_audio_cb = proc (tox: Core; group: Conference; peer: Peer;
    pcm: ptr int16; sampleCount: cuint; channels: uint8; sampleRate: uint32;
    userData: pointer) {.cdecl.}
  ## C callback type

proc addAvGroupchat*(av: var ToxAv): Conference =
  ## Create a new toxav group.
  proc add_av_groupchat(tox: Core; callback: toxav_group_audio_cb;
      userData: pointer): cint {.ctoxProc.}
  proc wrapper(tox: Core; group: Conference; peer: Peer; pcm: ptr int16;
      count: cuint; channels: uint8; rate: uint32; userData: pointer) {.cdecl.} =
    let av = cast[ToxAv](userData)
    if not av.groupAudio.isNil:
      av.groupAudio(group, peer, cast[Samples](pcm), (int)count, channels, rate)
  let res = add_av_groupchat(getTox(av.core), wrapper, addr av)
  if res == -1:
    raise newException(ToxError, "failed to join conference")
  (Conference)res

proc joinAvGroupchat*(av: var ToxAv; friend: Friend;
    cookie: string): Conference =
  ## Join a AV group (you need to have been invited first.)
  proc join_av_groupchat(tox: Core; friend: Friend; cookie: cstring;
      length: uint16; callback: toxav_group_audio_cb;
      userData: pointer): cint {.ctoxProc.}
  proc wrapper(tox: Core; group: Conference; peer: Peer; pcm: ptr int16;
      count: cuint; channels: uint8; rate: uint32; userData: pointer) {.cdecl.} =
    let av = cast[ToxAv](userData)
    if not av.groupAudio.isNil:
      av.groupAudio(group, peer, cast[Samples](pcm), (int)count, channels, rate)
  let res = join_av_groupchat(
      getTox(av.core), friend, cookie, (uint16)cookie.len, wrapper, addr av)
  if res == -1:
    raise newException(ToxError, "failed to join conference")
  (Conference)res

proc groupSendAudio*(
    av: ToxAv; group: Conference; pcm: ptr int16;
    samples: int; channels: uint8; sampleRate: uint32): bool =


@@ 437,7 467,8 @@ proc groupSendAudio*(
  proc group_send_audio(
      tox: AvCore; group: Conference; pcm: ptr int16;
      samples: cuint; channels: uint8; sample_rate: uint32): cint {.ctoxProc.}
  let res = group_send_audio(av.core, group, pcm, (cuint)samples, channels, sampleRate)
  let res = group_send_audio(
      av.core, group, pcm, (cuint)samples, channels, sampleRate)
  res == 0