Skip to content
This repository was archived by the owner on Oct 19, 2023. It is now read-only.

Commit 2dfc868

Browse files
committed
google-assistant-sdk/pushtotalk: add explicit locking
- remove end_of_utterance() - stop recording from main thread with explicit locking Change-Id: I07b24b8193560783945acde9f2b55e5533578ebd
1 parent 187b485 commit 2dfc868

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

google-assistant-sdk/googlesamples/assistant/grpc/audio_helpers.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,19 @@ def __init__(self, source, sink, iter_size, sample_width):
267267
self._iter_size = iter_size
268268
self._sample_width = sample_width
269269
self._volume_percentage = 50
270-
self._end_of_utterance = threading.Event()
270+
self._stop_recording = threading.Event()
271+
self._source_lock = threading.RLock()
271272

272273
def start_recording(self):
273274
"""Start recording from the audio source."""
274-
self._end_of_utterance.clear()
275+
self._stop_recording.clear()
275276
self._source.start()
276277

277278
def stop_recording(self):
278279
"""Stop recording from the audio source."""
279-
self._source.stop()
280+
self._stop_recording.set()
281+
with self._source_lock:
282+
self._source.stop()
280283

281284
def start_playback(self):
282285
"""Start playback to the audio sink."""
@@ -287,9 +290,6 @@ def stop_playback(self):
287290
self._sink.flush()
288291
self._sink.stop()
289292

290-
def end_of_utterance(self):
291-
self._end_of_utterance.set()
292-
293293
@property
294294
def volume_percentage(self):
295295
"""The current volume setting as an integer percentage (1-100)."""
@@ -302,7 +302,8 @@ def volume_percentage(self, new_volume_percentage):
302302
def read(self, size):
303303
"""Read bytes from the source (if currently recording).
304304
"""
305-
return self._source.read(size)
305+
with self._source_lock:
306+
return self._source.read(size)
306307

307308
def write(self, buf):
308309
"""Write bytes to the sink (if currently playing).
@@ -319,7 +320,7 @@ def close(self):
319320
def __iter__(self):
320321
"""Returns a generator reading data from the stream."""
321322
while True:
322-
if self._end_of_utterance.is_set():
323+
if self._stop_recording.is_set():
323324
raise StopIteration
324325
yield self.read(self._iter_size)
325326

google-assistant-sdk/googlesamples/assistant/grpc/pushtotalk.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import os.path
2222
import pathlib2 as pathlib
2323
import sys
24-
import threading
2524
import uuid
2625

2726
import click
@@ -121,20 +120,21 @@ def assist(self):
121120
self.conversation_stream.start_recording()
122121
logging.info('Recording audio request.')
123122

124-
def iter_assist_requests():
123+
def iter_log_assist_requests():
125124
for c in self.gen_assist_requests():
126125
assistant_helpers.log_assist_request_without_audio(c)
127126
yield c
128-
self.conversation_stream.stop_recording()
127+
logging.debug('Reached end of AssistRequest iteration.')
129128

130129
# This generator yields AssistResponse proto messages
131130
# received from the gRPC Google Assistant API.
132-
for resp in self.assistant.Assist(iter_assist_requests(),
131+
for resp in self.assistant.Assist(iter_log_assist_requests(),
133132
self.deadline):
134133
assistant_helpers.log_assist_response_without_audio(resp)
135134
if resp.event_type == END_OF_UTTERANCE:
136-
logging.info('End of audio request detected')
137-
self.conversation_stream.end_of_utterance()
135+
logging.info('End of audio request detected.')
136+
logging.info('Stopping recording.')
137+
self.conversation_stream.stop_recording()
138138
if resp.speech_results:
139139
logging.info('Transcript of user request: "%s".',
140140
' '.join(r.transcript

0 commit comments

Comments
 (0)