1
1
use std:: collections:: HashMap ;
2
2
use std:: fs:: File ;
3
- use std:: io:: { BufWriter , Write } ;
3
+ use std:: io:: { BufRead , BufReader , BufWriter , Write } ;
4
4
use std:: path:: PathBuf ;
5
5
use std:: process:: { Command , Stdio } ;
6
6
use std:: time:: Duration ;
@@ -63,6 +63,7 @@ impl InputStream {
63
63
pub struct SpeechToTextPlugin {
64
64
process : std:: process:: Child ,
65
65
stdin : std:: process:: ChildStdin ,
66
+ stdout : BufReader < std:: process:: ChildStdout > ,
66
67
chunk_duration : Duration ,
67
68
input_streams : HashMap < MediaStreamId , InputStream > ,
68
69
output_file : BufWriter < File > ,
@@ -91,6 +92,7 @@ impl SpeechToTextPlugin {
91
92
. collect ( ) ;
92
93
Ok ( Self {
93
94
stdin : process. stdin . take ( ) . or_fail ( ) ?,
95
+ stdout : BufReader :: new ( process. stdout . take ( ) . or_fail ( ) ?) ,
94
96
process,
95
97
chunk_duration : spec. chunk_duration ,
96
98
input_streams,
@@ -104,10 +106,11 @@ impl SpeechToTextPlugin {
104
106
input_stream_id : MediaStreamId ,
105
107
chunk : Vec < i16 > ,
106
108
) -> orfail:: Result < ( ) > {
109
+ let request_id: usize = 0 ;
107
110
let request = nojson:: object ( |f| {
108
111
f. member ( "jsonrpc" , "2.0" ) ?;
109
112
f. member ( "method" , "stt" ) ?;
110
- f. member ( "id" , 0 ) ?;
113
+ f. member ( "id" , request_id ) ?;
111
114
f. member (
112
115
"params" ,
113
116
nojson:: object ( |f| {
@@ -121,6 +124,23 @@ impl SpeechToTextPlugin {
121
124
writeln ! ( self . stdin, "{request}" ) . or_fail ( ) ?;
122
125
self . stdin . flush ( ) . or_fail ( ) ?;
123
126
127
+ let mut response_line = String :: new ( ) ;
128
+ self . stdout . read_line ( & mut response_line) . or_fail ( ) ?;
129
+
130
+ let json = nojson:: RawJson :: parse ( & response_line) . or_fail ( ) ?;
131
+ let obj = JsonObject :: new ( json. value ( ) ) . or_fail ( ) ?;
132
+ ( obj. get_required :: < String > ( "jsonrpc" ) . or_fail ( ) ? == "2.0" ) . or_fail ( ) ?;
133
+ ( obj. get_required :: < usize > ( "id" ) . or_fail ( ) ? == request_id) . or_fail ( ) ?;
134
+
135
+ // TODO: handle error
136
+ let result = json
137
+ . value ( )
138
+ . to_member ( "result" )
139
+ . or_fail ( ) ?
140
+ . required ( )
141
+ . or_fail ( ) ?;
142
+ let obj = JsonObject :: new ( result) . or_fail ( ) ?;
143
+ let text: String = obj. get_required ( "text" ) . or_fail ( ) ?;
124
144
todo ! ( )
125
145
}
126
146
}
0 commit comments