5050#include " xiaoai_sdk/aivs/ClientLoggerHooker.h"
5151#include " xiaoai_sdk/audio_config/config.h"
5252#include " audio_assistant/audio_assitant.hpp"
53+ #include " audio_base/audio_queue.hpp"
5354#include " audio_assistant/mp3decoder.hpp"
5455
5556#include " xiaoai_sdk/aivs/RobotController.h"
@@ -96,6 +97,13 @@ typedef struct tts_info
9697 tts_type_t type;
9798 unsigned char * address;
9899 unsigned int length;
100+ void free_mem ()
101+ {
102+ if (address != nullptr ) {
103+ fprintf (stdout, " vc: free ttsinfo memory\n " );
104+ free (address);
105+ }
106+ }
99107} tts_info_t ;
100108
101109typedef struct tts_queue
@@ -105,6 +113,8 @@ typedef struct tts_queue
105113} tts_queue_t ;
106114static tts_queue_t mMsgQueueTts ;
107115
116+ static athena_audio::MsgQueue<tts_info_t > TTSMsgQueue;
117+
108118bool getAivsTtsRunStatus (void )
109119{
110120 return mAivsTtsRunStatus ;
@@ -139,12 +149,7 @@ int tts_player_decode_paly(void);
139149
140150int tts_stream_flush (void )
141151{
142- // will check here
143- std::cout << " vc: befor clear, queue.size " << mMsgQueueTts .queue .size () << std::endl;
144- mMsgQueueTts .mutex .lock (); // queue lock
145- mMsgQueueTts .queue .clear ();
146- mMsgQueueTts .mutex .unlock (); // queue unlock
147- std::cout << " vc: befor clear, queue.size " << mMsgQueueTts .queue .size () << std::endl;
152+ TTSMsgQueue.Clear ();
148153 return 0 ;
149154}
150155
@@ -159,47 +164,38 @@ int aivs_set_interrupt(void)
159164 return 0 ;
160165}
161166
162- int ai_push_msg_playback (tts_info_t ttsInfo)
167+ int ai_push_msg_playback (const tts_info_t & ttsInfo)
163168{
164- std::cout << " vc: enter ai_push_msg_playback()" << std::endl;
165- tts_info_t ttsInfoPlayback = {TTS_TYPE_INVALID, NULL , 0 };
166- tts_info_t ttsInfoPlayStart = {TTS_TYPE_INVALID, NULL , 0 };
167- tts_info_t ttsInfoPlayStop = {TTS_TYPE_INVALID, NULL , 0 };
169+ std::cout << " vc: enter ai_push_msg_playback(), type: " << ttsInfo.type << std::endl;
170+ tts_info_t ttsInfoPlay = {ttsInfo.type , NULL , ttsInfo.length };
168171
169172 switch (ttsInfo.type ) {
170173 case TTS_TYPE_STREAM:
171174 std::cout << " vc: TTS_TYPE_STREAM Case !!" << std::endl;
172175
173176 if (ttsInfo.address == NULL || ttsInfo.length == 0 ) {
174- std::cout << " vc: ai_push_msg_playback invalid paramter !!!" << std::endl;
177+ std::cout << " vc: ai_push_msg_playback invalid paramter, stream addr is null !!!" <<
178+ std::endl;
175179 return -1 ;
176180 } else {
177- mMsgQueueTts .mutex .lock (); // queue lock
178- ttsInfoPlayback.type = ttsInfo.type ;
179- ttsInfoPlayback.length = ttsInfo.length ;
180- ttsInfoPlayback.address = (unsigned char *)malloc (ttsInfo.length );
181- if (ttsInfoPlayback.address != NULL ) {
182- memcpy (ttsInfoPlayback.address , ttsInfo.address , ttsInfoPlayback.length );
183- mMsgQueueTts .queue .push_front (ttsInfoPlayback);
181+ ttsInfoPlay.address = (unsigned char *)malloc (ttsInfo.length );
182+ if (ttsInfoPlay.address != NULL ) {
183+ memcpy (ttsInfoPlay.address , ttsInfo.address , ttsInfoPlay.length );
184+ TTSMsgQueue.EnQueue (ttsInfoPlay);
185+ } else {
186+ std::cout << " vc: ai_push_msg_playback failed, cannot allocate memory!" << std::endl;
184187 }
185- mMsgQueueTts .mutex .unlock (); // queue unlock
186188 }
187189 break ;
188190
189191 case TTS_TYPE_SYNC_START:
190192 std::cout << " vc: TTS_TYPE_SYNC_START Case !!" << std::endl;
191- mMsgQueueTts .mutex .lock (); // queue lock
192- ttsInfoPlayStart.type = ttsInfo.type ;
193- mMsgQueueTts .queue .push_front (ttsInfoPlayStart);
194- mMsgQueueTts .mutex .unlock (); // queue unlock
193+ TTSMsgQueue.EnQueue (ttsInfoPlay);
195194 break ;
196195
197196 case TTS_TYPE_SYNC_STOP:
198197 std::cout << " vc: TTS_TYPE_SYNC_STOP Case !!" << std::endl;
199- mMsgQueueTts .mutex .lock (); // queue lock
200- ttsInfoPlayStop.type = ttsInfo.type ;
201- mMsgQueueTts .queue .push_front (ttsInfoPlayStop);
202- mMsgQueueTts .mutex .unlock (); // queue unlock
198+ TTSMsgQueue.EnQueue (ttsInfoPlay);
203199 break ;
204200
205201 default :
@@ -222,51 +218,46 @@ int aivsTtsHandler(void)
222218 continue ;
223219 }
224220
225- mMsgQueueTts .mutex .lock (); // queue lock
226- if (!mMsgQueueTts .queue .empty ()) {
227- ttsInfoPlayback = mMsgQueueTts .queue .back ();
228- mMsgQueueTts .queue .pop_back ();
229- mMsgQueueTts .mutex .unlock (); // queue unlock
230- switch (ttsInfoPlayback.type ) {
231- case TTS_TYPE_STREAM:
232- if (ttsInfoPlayback.address == NULL || ttsInfoPlayback.length == 0 ) {
233- usleep (1000 * 100 );
234- continue ;
235- } else {
236- std::cout << " vc: Process TTS_TYPE_STREAM " << std::endl;
237- cyberdog_audio::mp3decoder::GetInstance ()->tts_player_accumulate (
238- ttsInfoPlayback.address ,
239- ttsInfoPlayback.length );
240- free (ttsInfoPlayback.address );
241- }
242- break ;
221+ if (!TTSMsgQueue.DeQueue (ttsInfoPlayback)) {
222+ std::cout << " vc: aivsTtsHandler get msg with empty, skip once!" << std::endl;
223+ continue ;
224+ }
243225
244- case TTS_TYPE_SYNC_START:
245- std::cout << " vc: Process TTS_TYPE_SYNC_START " << std::endl;
246- cyberdog_audio::mp3decoder::GetInstance ()->tts_player_init ();
247- ai_push_msg (101 ); /* led control for start-dialog status*/
248- break ;
226+ switch (ttsInfoPlayback.type ) {
227+ case TTS_TYPE_STREAM:
228+ if (ttsInfoPlayback.address == NULL || ttsInfoPlayback.length == 0 ) {
229+ usleep (1000 * 100 );
230+ continue ;
231+ } else {
232+ std::cout << " vc: Process TTS_TYPE_STREAM " << std::endl;
233+ cyberdog_audio::mp3decoder::GetInstance ()->tts_player_accumulate (
234+ ttsInfoPlayback.address ,
235+ ttsInfoPlayback.length );
236+ free (ttsInfoPlayback.address );
237+ }
238+ break ;
249239
250- case TTS_TYPE_SYNC_STOP:
251- std::cout << " vc: Process TTS_TYPE_SYNC_STOP " << std::endl;
240+ case TTS_TYPE_SYNC_START:
241+ std::cout << " vc: Process TTS_TYPE_SYNC_START " << std::endl;
242+ cyberdog_audio::mp3decoder::GetInstance ()->tts_player_init ();
243+ ai_push_msg (101 ); /* led control for start-dialog status*/
244+ break ;
252245
253- if (cyberdog_audio::mp3decoder::GetInstance () == nullptr ) {
254- fprintf (stderr, " Process TTS_TYPE_SYNC_STOP Error with invalid pointer!\n " );
255- break ;
256- }
246+ case TTS_TYPE_SYNC_STOP:
247+ std::cout << " vc: Process TTS_TYPE_SYNC_STOP " << std::endl;
257248
258- cyberdog_audio::mp3decoder::GetInstance ()-> tts_player_decode_paly ();
259- // ai_push_msg(104);/*led control for end -dialog status*/
249+ if ( cyberdog_audio::mp3decoder::GetInstance () == nullptr ) {
250+ fprintf (stderr, " Process TTS_TYPE_SYNC_STOP Error with invalid pointer! \n " );
260251 break ;
252+ }
261253
262- default :
263- std::cout << " vc: Process default case !!" << std::endl;
264- break ;
265- }
266- } else {
267- mMsgQueueTts .mutex .unlock (); // queue unlock
268- usleep (1000 * 100 );
269- continue ;
254+ cyberdog_audio::mp3decoder::GetInstance ()->tts_player_decode_paly ();
255+ // ai_push_msg(104);/*led control for end -dialog status*/
256+ break ;
257+
258+ default :
259+ std::cout << " vc: Process default case !!" << std::endl;
260+ break ;
270261 }
271262 }
272263}
@@ -778,6 +769,8 @@ void initAivsDeviceOAuth()
778769 clientInfo->setDeviceId (DEVICE_OAUTH_DEVICE_ID);
779770
780771 auto config = getAudioConfig ();
772+ config->putBoolean (aivs::AivsConfig::Auth::REQ_TOKEN_HYBRID, true );
773+ config->putBoolean (aivs::AivsConfig::Auth::REQ_TOKEN_BY_SDK, true );
781774 gEngine = aivs::Engine::create (config, clientInfo, aivs::Engine::ENGINE_AUTH_DEVICE_OAUTH);
782775}
783776
0 commit comments