Skip to content

Commit 59f37df

Browse files
committed
Native audio buffer
1 parent d1d84e9 commit 59f37df

File tree

4 files changed

+167
-15
lines changed

4 files changed

+167
-15
lines changed

livekit-uniffi/generated/kotlin/uniffi/livekit_uniffi/livekit_uniffi.kt

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ private fun uniffiCheckApiChecksums(lib: IntegrityCheckingUniffiLib) {
832832
if (lib.uniffi_livekit_uniffi_checksum_method_resampler_flush() != 32136.toShort()) {
833833
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
834834
}
835-
if (lib.uniffi_livekit_uniffi_checksum_method_resampler_push() != 59438.toShort()) {
835+
if (lib.uniffi_livekit_uniffi_checksum_method_resampler_push() != 58546.toShort()) {
836836
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
837837
}
838838
if (lib.uniffi_livekit_uniffi_checksum_constructor_resampler_new() != 65134.toShort()) {
@@ -1363,7 +1363,7 @@ public interface ResamplerInterface {
13631363
* and output rates, and returns any resampled data that is available after processing the input.
13641364
13651365
*/
1366-
fun `push`(`input`: List<kotlin.Short>): List<kotlin.Short>
1366+
fun `push`(`input`: NativeAudioBuffer): List<kotlin.Short>
13671367

13681368
companion object
13691369
}
@@ -1504,13 +1504,13 @@ open class Resampler: Disposable, AutoCloseable, ResamplerInterface
15041504
* and output rates, and returns any resampled data that is available after processing the input.
15051505
15061506
*/
1507-
@Throws(ResamplerException::class)override fun `push`(`input`: List<kotlin.Short>): List<kotlin.Short> {
1507+
@Throws(ResamplerException::class)override fun `push`(`input`: NativeAudioBuffer): List<kotlin.Short> {
15081508
return FfiConverterSequenceShort.lift(
15091509
callWithHandle {
15101510
uniffiRustCallWithError(ResamplerException) { _status ->
15111511
UniffiLib.uniffi_livekit_uniffi_fn_method_resampler_push(
15121512
it,
1513-
FfiConverterSequenceShort.lower(`input`),_status)
1513+
FfiConverterTypeNativeAudioBuffer.lower(`input`),_status)
15141514
}
15151515
}
15161516
)
@@ -1731,6 +1731,45 @@ public object FfiConverterTypeLogForwardEntry: FfiConverterRustBuffer<LogForward
17311731

17321732

17331733

1734+
/**
1735+
* A buffer owned by and whose lifetime is managed by the foreign language.
1736+
*/
1737+
data class NativeAudioBuffer (
1738+
var `ptr`: kotlin.ULong
1739+
,
1740+
var `len`: kotlin.ULong
1741+
1742+
){
1743+
1744+
1745+
1746+
companion object
1747+
}
1748+
1749+
/**
1750+
* @suppress
1751+
*/
1752+
public object FfiConverterTypeNativeAudioBuffer: FfiConverterRustBuffer<NativeAudioBuffer> {
1753+
override fun read(buf: ByteBuffer): NativeAudioBuffer {
1754+
return NativeAudioBuffer(
1755+
FfiConverterULong.read(buf),
1756+
FfiConverterULong.read(buf),
1757+
)
1758+
}
1759+
1760+
override fun allocationSize(value: NativeAudioBuffer) = (
1761+
FfiConverterULong.allocationSize(value.`ptr`) +
1762+
FfiConverterULong.allocationSize(value.`len`)
1763+
)
1764+
1765+
override fun write(value: NativeAudioBuffer, buf: ByteBuffer) {
1766+
FfiConverterULong.write(value.`ptr`, buf)
1767+
FfiConverterULong.write(value.`len`, buf)
1768+
}
1769+
}
1770+
1771+
1772+
17341773
data class ResamplerSettings (
17351774
var `inputRate`: kotlin.Double
17361775
,

livekit-uniffi/generated/python/livekit_uniffi.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ def _uniffi_check_api_checksums(lib):
493493
raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
494494
if lib.uniffi_livekit_uniffi_checksum_method_resampler_flush() != 32136:
495495
raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
496-
if lib.uniffi_livekit_uniffi_checksum_method_resampler_push() != 59438:
496+
if lib.uniffi_livekit_uniffi_checksum_method_resampler_push() != 58546:
497497
raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
498498

499499
# A ctypes library to expose the extern-C FFI definitions.
@@ -1500,6 +1500,45 @@ def write(value, buf):
15001500
_UniffiFfiConverterOptionalUInt32.write(value.line, buf)
15011501
_UniffiFfiConverterString.write(value.message, buf)
15021502

1503+
@dataclass
1504+
class NativeAudioBuffer:
1505+
"""
1506+
A buffer owned by and whose lifetime is managed by the foreign language.
1507+
"""
1508+
def __init__(self, *, ptr:int, len:int):
1509+
self.ptr = ptr
1510+
self.len = len
1511+
1512+
1513+
1514+
1515+
def __str__(self):
1516+
return "NativeAudioBuffer(ptr={}, len={})".format(self.ptr, self.len)
1517+
def __eq__(self, other):
1518+
if self.ptr != other.ptr:
1519+
return False
1520+
if self.len != other.len:
1521+
return False
1522+
return True
1523+
1524+
class _UniffiFfiConverterTypeNativeAudioBuffer(_UniffiConverterRustBuffer):
1525+
@staticmethod
1526+
def read(buf):
1527+
return NativeAudioBuffer(
1528+
ptr=_UniffiFfiConverterUInt64.read(buf),
1529+
len=_UniffiFfiConverterUInt64.read(buf),
1530+
)
1531+
1532+
@staticmethod
1533+
def check_lower(value):
1534+
_UniffiFfiConverterUInt64.check_lower(value.ptr)
1535+
_UniffiFfiConverterUInt64.check_lower(value.len)
1536+
1537+
@staticmethod
1538+
def write(value, buf):
1539+
_UniffiFfiConverterUInt64.write(value.ptr, buf)
1540+
_UniffiFfiConverterUInt64.write(value.len, buf)
1541+
15031542
class _UniffiFfiConverterFloat64(_UniffiConverterPrimitiveFloat):
15041543
@staticmethod
15051544
def read(buf):
@@ -2121,7 +2160,7 @@ def flush(self, ) -> typing.List[int]:
21212160
21222161
"""
21232162
raise NotImplementedError
2124-
def push(self, input: typing.List[int]) -> typing.List[int]:
2163+
def push(self, input: NativeAudioBuffer) -> typing.List[int]:
21252164
"""
21262165
Push audio data into the resampler and retrieve any available resampled data.
21272166
@@ -2188,7 +2227,7 @@ def flush(self, ) -> typing.List[int]:
21882227
*_uniffi_lowered_args,
21892228
)
21902229
return _uniffi_lift_return(_uniffi_ffi_result)
2191-
def push(self, input: typing.List[int]) -> typing.List[int]:
2230+
def push(self, input: NativeAudioBuffer) -> typing.List[int]:
21922231
"""
21932232
Push audio data into the resampler and retrieve any available resampled data.
21942233
@@ -2197,10 +2236,10 @@ def push(self, input: typing.List[int]) -> typing.List[int]:
21972236
21982237
"""
21992238

2200-
_UniffiFfiConverterSequenceInt16.check_lower(input)
2239+
_UniffiFfiConverterTypeNativeAudioBuffer.check_lower(input)
22012240
_uniffi_lowered_args = (
22022241
self._uniffi_clone_handle(),
2203-
_UniffiFfiConverterSequenceInt16.lower(input),
2242+
_UniffiFfiConverterTypeNativeAudioBuffer.lower(input),
22042243
)
22052244
_uniffi_lift_return = _UniffiFfiConverterSequenceInt16.lift
22062245
_uniffi_error_converter = _UniffiFfiConverterTypeResamplerError
@@ -2419,6 +2458,7 @@ def verify_token(token: str,credentials: typing.Optional[ApiCredentials]) -> Cla
24192458
"SipGrants",
24202459
"Claims",
24212460
"LogForwardEntry",
2461+
"NativeAudioBuffer",
24222462
"ResamplerSettings",
24232463
"TokenOptions",
24242464
"build_version",

livekit-uniffi/generated/swift/livekit_uniffi.swift

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ public protocol ResamplerProtocol: AnyObject, Sendable {
594594
* and output rates, and returns any resampled data that is available after processing the input.
595595

596596
*/
597-
func push(input: [Int16]) throws -> [Int16]
597+
func push(input: NativeAudioBuffer) throws -> [Int16]
598598

599599
}
600600
open class Resampler: ResamplerProtocol, @unchecked Sendable {
@@ -678,11 +678,11 @@ open func flush()throws -> [Int16] {
678678
* and output rates, and returns any resampled data that is available after processing the input.
679679

680680
*/
681-
open func push(input: [Int16])throws -> [Int16] {
681+
open func push(input: NativeAudioBuffer)throws -> [Int16] {
682682
return try FfiConverterSequenceInt16.lift(try rustCallWithError(FfiConverterTypeResamplerError_lift) {
683683
uniffi_livekit_uniffi_fn_method_resampler_push(
684684
self.uniffiCloneHandle(),
685-
FfiConverterSequenceInt16.lower(input),$0
685+
FfiConverterTypeNativeAudioBuffer_lower(input),$0
686686
)
687687
})
688688
}
@@ -945,6 +945,61 @@ public func FfiConverterTypeLogForwardEntry_lower(_ value: LogForwardEntry) -> R
945945
}
946946

947947

948+
/**
949+
* A buffer owned by and whose lifetime is managed by the foreign language.
950+
*/
951+
public struct NativeAudioBuffer: Equatable, Hashable {
952+
public var ptr: UInt64
953+
public var len: UInt64
954+
955+
// Default memberwise initializers are never public by default, so we
956+
// declare one manually.
957+
public init(ptr: UInt64, len: UInt64) {
958+
self.ptr = ptr
959+
self.len = len
960+
}
961+
962+
963+
}
964+
965+
#if compiler(>=6)
966+
extension NativeAudioBuffer: Sendable {}
967+
#endif
968+
969+
#if swift(>=5.8)
970+
@_documentation(visibility: private)
971+
#endif
972+
public struct FfiConverterTypeNativeAudioBuffer: FfiConverterRustBuffer {
973+
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> NativeAudioBuffer {
974+
return
975+
try NativeAudioBuffer(
976+
ptr: FfiConverterUInt64.read(from: &buf),
977+
len: FfiConverterUInt64.read(from: &buf)
978+
)
979+
}
980+
981+
public static func write(_ value: NativeAudioBuffer, into buf: inout [UInt8]) {
982+
FfiConverterUInt64.write(value.ptr, into: &buf)
983+
FfiConverterUInt64.write(value.len, into: &buf)
984+
}
985+
}
986+
987+
988+
#if swift(>=5.8)
989+
@_documentation(visibility: private)
990+
#endif
991+
public func FfiConverterTypeNativeAudioBuffer_lift(_ buf: RustBuffer) throws -> NativeAudioBuffer {
992+
return try FfiConverterTypeNativeAudioBuffer.lift(buf)
993+
}
994+
995+
#if swift(>=5.8)
996+
@_documentation(visibility: private)
997+
#endif
998+
public func FfiConverterTypeNativeAudioBuffer_lower(_ value: NativeAudioBuffer) -> RustBuffer {
999+
return FfiConverterTypeNativeAudioBuffer.lower(value)
1000+
}
1001+
1002+
9481003
public struct ResamplerSettings: Equatable, Hashable {
9491004
public var inputRate: Double
9501005
public var outputRate: Double
@@ -2127,7 +2182,7 @@ private let initializationResult: InitializationResult = {
21272182
if (uniffi_livekit_uniffi_checksum_method_resampler_flush() != 32136) {
21282183
return InitializationResult.apiChecksumMismatch
21292184
}
2130-
if (uniffi_livekit_uniffi_checksum_method_resampler_push() != 59438) {
2185+
if (uniffi_livekit_uniffi_checksum_method_resampler_push() != 58546) {
21312186
return InitializationResult.apiChecksumMismatch
21322187
}
21332188
if (uniffi_livekit_uniffi_checksum_constructor_resampler_new() != 65134) {

livekit-uniffi/src/audio.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ use livekit_audio::resampler::{
1717
};
1818
use std::sync::Mutex;
1919

20+
/// A buffer owned by and whose lifetime is managed by the foreign language.
21+
#[derive(uniffi::Record)]
22+
pub struct NativeAudioBuffer {
23+
ptr: u64,
24+
len: u64,
25+
}
26+
27+
impl NativeAudioBuffer {
28+
/// Forms a read-only slice to the underlying memory owned by the foreign language.
29+
unsafe fn as_slice(&self) -> &[i16] {
30+
if self.ptr == 0 {
31+
panic!("Foreign language passed a null pointer")
32+
}
33+
std::slice::from_raw_parts(self.ptr as *const i16, self.len as usize)
34+
}
35+
}
36+
2037
#[uniffi::remote(Error)]
2138
pub enum ResamplerError {
2239
Initialization(String),
@@ -58,9 +75,10 @@ impl Resampler {
5875
/// This method accepts audio data, resamples it according to the configured input
5976
/// and output rates, and returns any resampled data that is available after processing the input.
6077
///
61-
pub fn push(&self, input: &[i16]) -> Result<Vec<i16>, ResamplerError> {
78+
pub fn push(&self, input: NativeAudioBuffer) -> Result<Vec<i16>, ResamplerError> {
6279
let mut inner = self.inner.lock().unwrap();
63-
Ok(inner.push(input)?.to_vec())
80+
let slice = unsafe { input.as_slice() };
81+
Ok(inner.push(slice)?.to_vec())
6482
}
6583

6684
/// Flush any remaining audio data through the resampler and retrieve the resampled data.

0 commit comments

Comments
 (0)