Browse Source

腾讯云TTS调通了

张洋 1 month ago
parent
commit
778dd1f8c0

+ 1 - 1
zhipuzi_pos_windows/tool/tts/CryptoUtil.cpp

@@ -1,4 +1,4 @@
-#include "pch.h"
+#include "pch.h"
 // 签名/编解码工具实现(OpenSSL)
 // 签名/编解码工具实现(OpenSSL)
 #include "CryptoUtil.h"
 #include "CryptoUtil.h"
 
 

+ 1 - 1
zhipuzi_pos_windows/tool/tts/PcmPlayer.cpp

@@ -1,4 +1,4 @@
-#include "pch.h"
+#include "pch.h"
 // 内存 PCM 播放器实现(Windows waveOut)
 // 内存 PCM 播放器实现(Windows waveOut)
 #ifdef _WIN32
 #ifdef _WIN32
 
 

+ 1 - 1
zhipuzi_pos_windows/tool/tts/StreamTtsPlayer.cpp

@@ -1,4 +1,4 @@
-// 腾讯云实时语音合成流式客户端实现(OpenSSL WSS + HMAC-SHA1 签名)
+// 腾讯云实时语音合成流式客户端实现(OpenSSL WSS + HMAC-SHA1 签名)
 //
 //
 // 签名规则以官方 Python SDK 为准(tencentcloud-speech-sdk-python/tts/speech_synthesizer_ws.py):
 // 签名规则以官方 Python SDK 为准(tencentcloud-speech-sdk-python/tts/speech_synthesizer_ws.py):
 //   签名原文 = "GET" + "tts.cloud.tencent.com" + "/stream_ws?" + 字典序 k=v&k=v(值不编码)
 //   签名原文 = "GET" + "tts.cloud.tencent.com" + "/stream_ws?" + 字典序 k=v&k=v(值不编码)

+ 4 - 1
zhipuzi_pos_windows/tool/tts/StreamTtsPlayer.h

@@ -19,7 +19,10 @@
 #endif
 #endif
 
 
 struct StreamTtsOptions {
 struct StreamTtsOptions {
-    // 账号 AppId(控制台 https://console.cloud.tencent.com/developer 查看)
+    // 账号 AppId,纯数字(如 1300123456)。获取路径:官网右上角头像 → 账号信息 → APPID。
+    // 直达链接 https://console.cloud.tencent.com/developer
+    // 注意:不是同页的「账号 ID(UIN)」,也不是 SecretId(AKID 开头的字符串),
+    //      填错的现象是 WebSocket 握手返回 403。
     long long appid = 0;
     long long appid = 0;
     std::string secret_id;
     std::string secret_id;
     std::string secret_key;
     std::string secret_key;

+ 29 - 3
zhipuzi_pos_windows/tool/tts/TtsClient.cpp

@@ -1,4 +1,4 @@
-// 腾讯云语音合成 (TTS) - TextToVoice 轻量 C++ 客户端 实现
+// 腾讯云语音合成 (TTS) - TextToVoice 轻量 C++ 客户端 实现
 //
 //
 // 签名算法:TC3-HMAC-SHA256
 // 签名算法:TC3-HMAC-SHA256
 // 参考:https://cloud.tencent.com/document/api/213/30654
 // 参考:https://cloud.tencent.com/document/api/213/30654
@@ -328,7 +328,18 @@ TtsResult TtsClient::Synthesize(const std::string& text_utf8, const TtsOptions&
     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
     if (verify_peer_) {
     if (verify_peer_) {
         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
-        if (!ca_file_.empty()) curl_easy_setopt(curl, CURLOPT_CAINFO, ca_file_.c_str());
+        if (!ca_file_.empty()) {
+            curl_easy_setopt(curl, CURLOPT_CAINFO, ca_file_.c_str());
+        } else {
+            // Windows + curl >= 7.71:直接用操作系统自带的证书库。
+            // 这是最省事也最安全的做法 —— OpenSSL 后端在 Windows 上默认找不到
+            // OpenSSL 自己目录下的根证书,不设这个就必定报
+            // "SSL peer certificate or SSH remote key was not OK"。
+            // 有了它就不用自己下载维护 cacert.pem,安全性也不打折。
+#if defined(_WIN32) && LIBCURL_VERSION_NUM >= 0x074700
+            curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, (long)CURLSSLOPT_NATIVE_CA);
+#endif
+        }
     } else {
     } else {
         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
     }
     }
@@ -343,7 +354,22 @@ TtsResult TtsClient::Synthesize(const std::string& text_utf8, const TtsOptions&
     last_response_ = response;
     last_response_ = response;
 
 
     if (rc != CURLE_OK) {
     if (rc != CURLE_OK) {
-        result.error_message = std::string("network error: ") + curl_easy_strerror(rc);
+        std::string msg = std::string("network error: ") + curl_easy_strerror(rc);
+        // 证书校验失败是最常见的配置问题,给出明确的下一步而不是干巴巴的英文
+        if (rc == CURLE_PEER_FAILED_VERIFICATION) {
+            msg += "(提示:libcurl 找不到/校验不通过服务端证书。";
+            if (verify_peer_) {
+                if (ca_file_.empty()) {
+                    msg += "可下载 https://curl.se/ca/cacert.pem,设环境变量 TTS_CA_FILE;"
+                           "或调 client.SetCaFile();或临时 SetVerifyPeer(false) 跳过校验。)";
+                } else {
+                    msg += "已设置 CA 文件但仍失败,请确认路径正确、是 PEM 格式且未被防火墙篡改。)";
+                }
+            } else {
+                msg += "已禁用证书校验仍失败,问题不在证书,可能是 TLS 1.2 不支持:见 README 9.2。)";
+            }
+        }
+        result.error_message = msg;
         return result;
         return result;
     }
     }
 
 

+ 1 - 1
zhipuzi_pos_windows/tool/tts/WssClient.cpp

@@ -1,4 +1,4 @@
-// 极简 WSS 客户端实现:OpenSSL + 原生 socket(Win7 兼容)
+// 极简 WSS 客户端实现:OpenSSL + 原生 socket(Win7 兼容)
 #include "pch.h"
 #include "pch.h"
 #include "WssClient.h"
 #include "WssClient.h"