Skip to content

Commit bcf4d79

Browse files
committed
STTプラグインにJSONRPC通信処理とプロセス終了処理を追加
1 parent b34a9f9 commit bcf4d79

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/plugin.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22
use std::fs::File;
3-
use std::io::BufWriter;
3+
use std::io::{BufWriter, Write};
44
use std::path::PathBuf;
55
use std::process::{Command, Stdio};
66
use std::time::Duration;
@@ -98,11 +98,29 @@ impl SpeechToTextPlugin {
9898
})
9999
}
100100

101+
// TODO: ブロックする可能性があるので専用のスレッドで実行する
101102
fn execute_stt(
102103
&mut self,
103104
input_stream_id: MediaStreamId,
104105
chunk: Vec<i16>,
105106
) -> orfail::Result<()> {
107+
let request = nojson::object(|f| {
108+
f.member("jsonrpc", "2.0")?;
109+
f.member("method", "stt")?;
110+
f.member("id", 0)?;
111+
f.member(
112+
"params",
113+
nojson::object(|f| {
114+
f.member("input_stream_id", input_stream_id.get())?;
115+
f.member("audio_data", &chunk)?;
116+
Ok(())
117+
}),
118+
)?;
119+
Ok(())
120+
});
121+
writeln!(self.stdin, "{request}").or_fail()?;
122+
self.stdin.flush().or_fail()?;
123+
106124
todo!()
107125
}
108126
}
@@ -118,7 +136,7 @@ impl MediaProcessor for SpeechToTextPlugin {
118136

119137
fn process_input(&mut self, input: MediaProcessorInput) -> orfail::Result<()> {
120138
if let Some(sample) = input.sample {
121-
let mut input_stream = self.input_streams.get_mut(&input.stream_id).or_fail()?;
139+
let input_stream = self.input_streams.get_mut(&input.stream_id).or_fail()?;
122140
let data = sample.expect_audio_data().or_fail()?;
123141
input_stream
124142
.chunk
@@ -144,4 +162,9 @@ impl MediaProcessor for SpeechToTextPlugin {
144162
}
145163
}
146164

147-
// TODO: kill when drop
165+
impl Drop for SpeechToTextPlugin {
166+
fn drop(&mut self) {
167+
let _ = self.process.kill();
168+
let _ = self.process.wait();
169+
}
170+
}

0 commit comments

Comments
 (0)