-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathopenai_live_audio_session.cpp
More file actions
276 lines (231 loc) · 9.85 KB
/
openai_live_audio_session.cpp
File metadata and controls
276 lines (231 loc) · 9.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <chrono>
#include <cstdint>
#include <nlohmann/json.hpp>
#include "openai/openai_live_audio_session.h"
#include "openai/openai_live_audio_types.h"
#include "foundry_local_internal_core.h"
#include "foundry_local_exception.h"
#include "core_interop_request.h"
#include "thread_safe_queue.h"
#include "logger.h"
namespace foundry_local {
LiveAudioTranscriptionSession::LiveAudioTranscriptionSession(
gsl::not_null<Internal::IFoundryLocalCore*> core,
std::string modelId,
gsl::not_null<ILogger*> logger)
: core_(core), modelId_(std::move(modelId)), logger_(logger) {}
LiveAudioTranscriptionSession::~LiveAudioTranscriptionSession() noexcept {
try {
std::unique_lock<std::mutex> lock(mutex_);
if (state_ == SessionState::Started) {
StopInternal(lock);
}
}
catch (...) {
// Suppress exceptions in destructor
}
}
void LiveAudioTranscriptionSession::Start() {
std::unique_lock<std::mutex> lock(mutex_);
if (state_ != SessionState::Created) {
throw Exception("Session has already been started.", *logger_);
}
// Transition to Starting state before releasing lock for FFI call
state_ = SessionState::Starting;
activeSettings_ = settings_;
// Validate queue capacity early
if (activeSettings_.push_queue_capacity <= 0) {
state_ = SessionState::Created;
throw Exception("push_queue_capacity must be greater than 0.", *logger_);
}
// Build the start command
CoreInteropRequest req("audio_stream_start");
req.AddParam("Model", modelId_);
req.AddParam("SampleRate", std::to_string(activeSettings_.sample_rate));
req.AddParam("Channels", std::to_string(activeSettings_.channels));
req.AddParam("BitsPerSample", std::to_string(activeSettings_.bits_per_sample));
if (activeSettings_.language.has_value()) {
req.AddParam("Language", activeSettings_.language.value());
}
std::string json = req.ToJson();
// Release lock during FFI call to avoid holding mutex across boundary
lock.unlock();
auto response = core_->call(req.Command(), *logger_, &json);
lock.lock();
if (response.HasError()) {
state_ = SessionState::Created;
throw Exception("Failed to start audio stream: " + response.error, *logger_);
}
sessionHandle_ = std::move(response.data);
if (sessionHandle_.empty()) {
state_ = SessionState::Created;
throw Exception("audio_stream_start returned an empty session handle.", *logger_);
}
// Validate queue capacity
const size_t queueCapacity = static_cast<size_t>(activeSettings_.push_queue_capacity);
// Create the queues
pushQueue_ = std::make_unique<Internal::ThreadSafeQueue<AudioChunk>>(queueCapacity);
resultQueue_ = std::make_unique<Internal::ThreadSafeQueue<LiveAudioTranscriptionResponse>>(queueCapacity);
state_ = SessionState::Started;
// Start the push worker thread
pushThread_ = std::thread([this] { PushWorkerLoop(); });
}
void LiveAudioTranscriptionSession::Append(const uint8_t* pcmData, size_t length) {
{
std::lock_guard<std::mutex> lock(mutex_);
if (state_ != SessionState::Started) {
throw Exception(
state_ == SessionState::Stopped
? "Session has already been stopped."
: "Session is not started. Call Start() first.",
*logger_);
}
}
AudioChunk chunk(pcmData, pcmData + length);
if (!pushQueue_->Push(std::move(chunk))) {
throw Exception("Failed to enqueue audio data: session is closed.", *logger_);
}
}
TranscriptionStatus LiveAudioTranscriptionSession::TryGetNext(LiveAudioTranscriptionResponse& result,
std::chrono::milliseconds timeout) {
{
std::lock_guard<std::mutex> lock(mutex_);
if (state_ != SessionState::Started && state_ != SessionState::Stopped) {
throw Exception("Session is not started. Call Start() first.", *logger_);
}
}
auto status = resultQueue_->TryPop(result, timeout);
switch (status) {
case Internal::DequeueStatus::Item:
return TranscriptionStatus::Result;
case Internal::DequeueStatus::Timeout:
return TranscriptionStatus::Timeout;
case Internal::DequeueStatus::Closed: {
// Return the final result from Stop() if available
std::lock_guard<std::mutex> lock(mutex_);
if (hasFinalResult_) {
result = std::move(finalResult_);
hasFinalResult_ = false;
return TranscriptionStatus::Result;
}
return TranscriptionStatus::Closed;
}
case Internal::DequeueStatus::Error:
return TranscriptionStatus::Error;
default:
return TranscriptionStatus::Error;
}
}
void LiveAudioTranscriptionSession::Stop() {
std::unique_lock<std::mutex> lock(mutex_);
if (state_ != SessionState::Started) {
return;
}
StopInternal(lock);
}
void LiveAudioTranscriptionSession::StopInternal(std::unique_lock<std::mutex>& lock) {
state_ = SessionState::Stopped;
std::string handle = sessionHandle_;
// Close the push queue to signal the worker thread to finish
if (pushQueue_) {
pushQueue_->Close();
}
// Close the result queue to unblock any blocked Push() in the worker thread,
// preventing a deadlock when joining below.
if (resultQueue_) {
resultQueue_->Close();
}
lock.unlock();
// Wait for the push thread to finish (safe now — worker is unblocked)
if (pushThread_.joinable()) {
pushThread_.join();
}
// Send stop command to core
CoreInteropRequest req("audio_stream_stop");
req.AddParam("SessionHandle", handle);
std::string json = req.ToJson();
auto response = core_->call(req.Command(), *logger_, &json);
// Store the final result or error for retrieval via TryGetNext
if (response.HasError()) {
if (resultQueue_) {
resultQueue_->CloseWithError("audio_stream_stop failed: " + response.error);
}
}
else if (!response.data.empty()) {
try {
finalResult_ = LiveAudioTranscriptionResponse::FromJson(response.data);
hasFinalResult_ = true;
}
catch (const std::exception& e) {
logger_->Log(LogLevel::Warning,
std::string("Failed to parse final transcription response: ") + e.what());
}
}
lock.lock();
}
void LiveAudioTranscriptionSession::PushWorkerLoop() {
AudioChunk chunk;
while (true) {
auto status = pushQueue_->Pop(chunk);
if (status != Internal::DequeueStatus::Item) {
break;
}
std::string handle;
{
std::lock_guard<std::mutex> lock(mutex_);
handle = sessionHandle_;
}
CoreInteropRequest req("audio_stream_push");
req.AddParam("SessionHandle", handle);
std::string json = req.ToJson();
auto response = core_->callWithBinary(req.Command(), *logger_, &json,
chunk.data(), chunk.size());
if (response.HasError()) {
auto coreError = CoreErrorResponse::TryParse(response.error);
std::string msg =
(coreError.has_value() && !coreError->message.empty())
? coreError->message
: response.error;
logger_->Log(LogLevel::Error, "audio_stream_push failed: " + msg);
pushQueue_->Close();
resultQueue_->CloseWithError(msg);
std::lock_guard<std::mutex> lock(mutex_);
errorMessage_ = std::move(msg);
return;
}
// Parse the response as a transcription result if there is data
if (!response.data.empty()) {
try {
auto result = LiveAudioTranscriptionResponse::FromJson(response.data);
if (!resultQueue_->TryPush(std::move(result))) {
logger_->Log(
LogLevel::Warning,
"Dropping transcription result because the result queue is full.");
}
}
catch (const std::exception& e) {
logger_->Log(LogLevel::Warning,
std::string("Failed to parse transcription response: ") + e.what());
}
}
}
}
std::string LiveAudioTranscriptionSession::GetErrorMessage() const {
std::lock_guard<std::mutex> lock(mutex_);
return errorMessage_;
}
bool LiveAudioTranscriptionSession::IsStarted() const {
std::lock_guard<std::mutex> lock(mutex_);
return state_ == SessionState::Started;
}
bool LiveAudioTranscriptionSession::IsStopped() const {
std::lock_guard<std::mutex> lock(mutex_);
return state_ == SessionState::Stopped;
}
} // namespace foundry_local