Skip to main content

Session

Struct Session 

Source
pub struct Session { /* private fields */ }
Expand description

任务会话。

Implementations§

Source§

impl Session

Source

pub fn family(&self) -> String

模型族名(借用的 C 字符串,即刻拷贝为 Rust 字符串)。

Source

pub fn task_kind(&self) -> String

任务种类(如 "vad")。

Source

pub fn run_mode(&self) -> String

运行模式(如 "offline" / "streaming")。

Source

pub fn backend(&self) -> Backend

创建会话时请求的计算后端。

这是传给 crate::Model::create_task_session 的 backend 参数原样 记录,不经过上游再查询——C ABI 与上游引擎目前均无“实际落定后端” 查询接口。显式传 Backend::Cpu/Vulkan/... 时返回值即实际后端; 传 Backend::Best 时引擎内部按可用设备解析,返回值只能说明 “请求的是自动选择”,不要据此显示具体设备名。

Source

pub fn streaming_policy(&self) -> Result<StreamingPolicy, Error>

流式策略描述(输入/输出类型、推荐分块大小)。

§Errors

C ABI 调用失败或返回的 JSON 无法解析时返回对应 Error 变体。

Source

pub fn prepare<R: IntoRequest>(&self, request: R) -> Result<(), Error>

为一次请求做准备(离线与流式共用)。

request 为 crate::request::IntoRequest,可用类型化的 crate::request::Request 枚举(推荐,每个任务一种变体),或任意 JSON 字符串。携带 audio / text / voice 等输入,或用 audio_path 指向本地 WAV 文件。空请求可传 ()。

§Errors

请求序列化失败、路径含 NUL,或 C ABI 调用失败时返回对应 Error 变体。

Source

pub fn set_event_callback<F>(&self, cb: Option<F>)
where F: FnMut(StreamEvent) + Send + 'static,

注册流式事件回调;调用 None 清除。

回调会在每次流事件产生时被调用(可能来自 C++ 侧线程),其内容是 解析为 StreamEvent 的对象。回调内不得再调用本会话的方法 (含 set_event_callback/reset,否则可能死锁);回调内的 panic 会被捕获忽略,不会传播。

以 &self 提供(内部有 Mutex 保护),因此可在持有 &Session 的任意线程上注册 / 更换回调。同一个会话只保留一份回调。

Source

pub fn start<R: IntoRequest>(&self, request: R) -> Result<(), Error>

开始一次流式任务(先 prepare 再进入流式读取循环)。

request 同 Session::prepare,类型化枚举或 JSON 字符串均可; 空请求传 ()。

§Errors

请求序列化失败、路径含 NUL,或 C ABI 调用失败时返回对应 Error 变体。

Source

pub fn process_audio( &self, samples: &[f32], sample_rate: i32, channels: i32, start_sample: i64, ) -> Result<Option<StreamEvent>, Error>

将一段音频送入流式会话,返回该块触发的第一个事件(若有)。

samples 为 float 采样(-1..1),start_sample 为该块在输入流中的 起始采样点。若同时注册了事件回调,事件会经回调到达;返回值是 shim 同步返回的事件 JSON。

§Errors

C ABI 调用失败或返回的 JSON 无法解析时返回对应 Error 变体。

Source

pub fn finish(&self) -> Result<TaskResult, Error>

结束流式会话,返回最终 TaskResult。

§Errors

C ABI 调用失败或返回的 JSON 无法解析时返回对应 Error 变体。 check_rc 通过却无 payload 属于 shim 违约,同样报错而非伪造空结果。

Source

pub fn run_offline<R: IntoRequest>( &self, request: R, ) -> Result<TaskResult, Error>

离线执行一次请求,返回 TaskResult。

相当于 prepare + 一次完整执行(shim 内部已做 prepare)。request 同 Session::prepare,类型化枚举或 JSON 字符串均可。

§Errors

请求序列化失败、路径含 NUL,或 C ABI 调用失败时返回对应 Error 变体。

Source

pub fn reset(&self) -> Result<(), Error>

重置流式会话内部状态(可复用会话对象重新开始)。

§Errors

C ABI 调用失败时返回对应 Error 变体。

Trait Implementations§

Source§

impl Drop for Session

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for Session

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.