1
1
use std:: collections:: HashMap ;
2
2
use std:: fs:: File ;
3
- use std:: io:: BufWriter ;
3
+ use std:: io:: { BufWriter , Write } ;
4
4
use std:: path:: PathBuf ;
5
5
use std:: process:: { Command , Stdio } ;
6
6
use std:: time:: Duration ;
@@ -98,11 +98,29 @@ impl SpeechToTextPlugin {
98
98
} )
99
99
}
100
100
101
+ // TODO: ブロックする可能性があるので専用のスレッドで実行する
101
102
fn execute_stt (
102
103
& mut self ,
103
104
input_stream_id : MediaStreamId ,
104
105
chunk : Vec < i16 > ,
105
106
) -> 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
+
106
124
todo ! ( )
107
125
}
108
126
}
@@ -118,7 +136,7 @@ impl MediaProcessor for SpeechToTextPlugin {
118
136
119
137
fn process_input ( & mut self , input : MediaProcessorInput ) -> orfail:: Result < ( ) > {
120
138
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 ( ) ?;
122
140
let data = sample. expect_audio_data ( ) . or_fail ( ) ?;
123
141
input_stream
124
142
. chunk
@@ -144,4 +162,9 @@ impl MediaProcessor for SpeechToTextPlugin {
144
162
}
145
163
}
146
164
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