Skip to content

Commit

Permalink
Fixes an issue that memory keeps growing when there is no recognition…
Browse files Browse the repository at this point in the history
… request. (#23)
  • Loading branch information
BillWSY authored Sep 17, 2018
1 parent 928b63c commit d536a3d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions gcloud_speech/src/speech_to_text_action_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ SpeechToTextActionHandler::SpeechToTextActionHandler(
SpeechToTextSimpleActionServer* simple_action_server) {
recognizer_ = recognizer;
simple_action_server_ = simple_action_server;
is_active_.store(false);
}


Expand All @@ -30,16 +31,19 @@ void SpeechToTextActionHandler::AudioMsgCallback(
DCHECK(false);
return;
}
std::unique_ptr<speech::AudioSample> audio_sample(
new speech::AudioSample(msg->data));
audio_queue_.push(std::move(audio_sample));
if (is_active_.load()) {
std::unique_ptr<speech::AudioSample> audio_sample(
new speech::AudioSample(msg->data));
audio_queue_.push(std::move(audio_sample));
}
}

void SpeechToTextActionHandler::ExecuteSpeechToTextAction(
const gcloud_speech_msgs::SpeechToTextGoalConstPtr& goal) {
audio_queue_.clear();
result_queue_.clear();
recognizer_->Stop();
is_active_.store(true);

int max_audio_seconds = goal->listen_duration_sec;
if (max_audio_seconds == 0) {
Expand Down Expand Up @@ -118,6 +122,7 @@ void SpeechToTextActionHandler::ExecuteSpeechToTextAction(
} else {
simple_action_server_->setSucceeded(result_msg);
}
is_active_.store(false);
}

} // namespace gcloud_speech
6 changes: 6 additions & 0 deletions gcloud_speech/src/speech_to_text_action_handler.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef SPEECH_TO_TEXT_ACTION_HANDLER_H_
#define SPEECH_TO_TEXT_ACTION_HANDLER_H_

#include <atomic>

#include "actionlib/server/simple_action_server.h"
#include "gcloud_speech_msgs/SpeechToTextAction.h"
#include "gcloud_speech_msgs/RecognitionHypothesis.h"
Expand Down Expand Up @@ -36,6 +38,10 @@ class SpeechToTextActionHandler {
// AudioMsgCallback function.
cogrob::cloud::speech::AudioQueue audio_queue_;

// is_active_ indicates whether the action server is active. This handler will
// only record audio if the action server is active.
std::atomic_bool is_active_;

// result_queue_ contains results from recognizer_.
util::SimpleThreadSafeQueue<cogrob::cloud::speech::RecognitionResult>
result_queue_;
Expand Down

0 comments on commit d536a3d

Please sign in to comment.