CarplayLink.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. #include "CarplayLink.h"
  2. #include "CarplayLinkCbsImpl.h"
  3. #include "CarplayAudioCtx.h"
  4. #include "WebrtcWrapper.h"
  5. #include "carplayVideoWrapper.h"
  6. #include "carplayAudioWrapper.h"
  7. #include "carplayWrapper.h"
  8. #include "BufferQueue.h"
  9. #include <string.h>
  10. #ifdef USE_CARPLAY
  11. typedef enum
  12. {
  13. MEDIA_NONE = 0,
  14. MEDIA_PLAY,
  15. MEDIA_PAUSE,
  16. MEDIA_PLAY_PAUSE,
  17. MEDIA_NEXT,
  18. MEDIA_PREVIOUS
  19. }MEDIA;
  20. int ICarplayVideoCallbacksImpl::carplayVideoStartCB()
  21. {
  22. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  23. if(mHandle){
  24. mHandle->video_start(0, 0, mHandle->getLinkConfig().screen_width, mHandle->getLinkConfig().screen_height);
  25. mHandle->onSdkConnectStatus(CONNECT_STATUS_CONNECT_SUCCEED, mHandle->getPhoneType());
  26. }
  27. return 0;
  28. }
  29. void ICarplayVideoCallbacksImpl::carplayVideoStopCB()
  30. {
  31. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  32. if(mHandle)
  33. mHandle->video_stop();
  34. }
  35. int ICarplayVideoCallbacksImpl::carplayVideoDataProcCB(const char *buf, int len)
  36. {
  37. //printf("%s:%s:%d len:%d\r\n",__FILE__,__func__,__LINE__, len);
  38. if(mHandle)
  39. mHandle->video_play(buf, len);
  40. return 0;
  41. }
  42. void ICarplayAudioCallbacksImpl::carplayAudioStartCB(int handle, AudioStreamType type, int rate, int bits, int channels)
  43. {
  44. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  45. if (type == AudioStreamRec)
  46. return;
  47. printf("type:%d, rate:%d, bits:%d, channels:%d\n",type,rate, bits, channels);
  48. if (type == AudioStreamRECOGNITION || type == AudioStreamCall) {
  49. mHandle->mAudioStreamType = type;
  50. if(mHandle){
  51. AudioInfo info = {rate, channels,bits };
  52. mHandle->record_start(info);
  53. if(type == AudioStreamRECOGNITION){
  54. mHandle->app_status(APP_RECOGNITION_STARTED);
  55. }
  56. else if(type == AudioStreamCall){
  57. #ifdef AEC_DELAY
  58. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  59. mHandle->mAecHandle = WebRtcAecInit();
  60. SetWebRtcAecParam(mHandle->mAecHandle ,rate, rate, NULL);
  61. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  62. #endif
  63. //mHandle->app_status(APP_PHONE_STARTED);
  64. }
  65. }
  66. //sendAudioMsg(mHandle, true, ChannelAudioIn, type, rate, bits, channels);
  67. }
  68. mHandle->audio_start((AudioType)type,rate, bits, channels);
  69. CarplayAudioCtx* audioHandle = new CarplayAudioCtx(mHandle, handle, type, rate, bits, channels);
  70. //sendAudioMsg(mHandle, true, ChannelAudioOut, type, rate, bits, channels);
  71. Autolock l(&mLock);
  72. mAudioHandlList.push_back(audioHandle);
  73. }
  74. void ICarplayAudioCallbacksImpl::carplayAudioStopCB(int handle, AudioStreamType type)
  75. {
  76. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  77. if (type == AudioStreamRec)
  78. return;
  79. if (type == AudioStreamRECOGNITION || type == AudioStreamCall) {
  80. if(mHandle){
  81. mHandle->record_stop();
  82. }
  83. if(type == AudioStreamRECOGNITION){
  84. mHandle->app_status(APP_RECOGNITION_STOPPED);
  85. }
  86. else if(type == AudioStreamCall){
  87. #ifdef AEC_DELAY
  88. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  89. WebRtcAecRelease(mHandle->mAecHandle);
  90. mHandle->mAecHandle = NULL;
  91. mHandle->mAecQueue->ClearBufferQueue();
  92. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  93. #endif
  94. mHandle->app_status(APP_PHONE_STOPPED);
  95. }
  96. }
  97. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  98. std::list<CarplayAudioCtx *>::iterator itor;
  99. CarplayAudioCtx *tmpAudioHandle = NULL;
  100. bool isFound = false;
  101. Autolock l(&mLock);
  102. for (itor = mAudioHandlList.begin(); itor != mAudioHandlList.end();) {
  103. tmpAudioHandle = *itor;
  104. if (tmpAudioHandle->getStreamHandle() == handle) {
  105. isFound = true;
  106. mAudioHandlList.erase(itor);
  107. break;
  108. } else {
  109. itor++;
  110. }
  111. }
  112. if (isFound && tmpAudioHandle)
  113. delete tmpAudioHandle;
  114. mHandle->audio_stop((AudioType)type);
  115. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  116. }
  117. #endif
  118. CarplayLink::CarplayLink()
  119. {
  120. #ifdef USE_CARPLAY
  121. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  122. mHandle = new CarplayWrapper();
  123. mVideoCbs = new ICarplayVideoCallbacksImpl(this);
  124. mAudioCbs = new ICarplayAudioCallbacksImpl(this);
  125. mCbs = new CarplayLinkCbsImpl(this);
  126. mDefaultWifi = true;
  127. mDdefaultPhonebtMac = true;
  128. mAecHandle = NULL;
  129. mAecQueue = new BufferQueue();
  130. mAudioStreamType = AudioStreamCall;
  131. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  132. #endif
  133. }
  134. CarplayLink::~CarplayLink()
  135. {
  136. #ifdef USE_CARPLAY
  137. delete mHandle;
  138. delete mVideoCbs;
  139. delete mAudioCbs;
  140. delete mCbs;
  141. delete mAecQueue;
  142. #endif
  143. }
  144. #ifdef USE_CARPLAY
  145. bool CarplayLink::init(LinkMode linkMode)
  146. {
  147. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  148. printf("phone_bt_mac = %s\r\n",mLinkConfig.phone_bt_mac.c_str());
  149. if(!mLinkConfig.phone_bt_mac.empty()){
  150. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  151. if(mDdefaultPhonebtMac){
  152. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  153. mDdefaultPhonebtMac = true;
  154. mHandle->CarplaySetStringParameter("REMOTE_BTMAC", mLinkConfig.phone_bt_mac);
  155. }
  156. }
  157. printf("car_wifi_ssid = %s\r\n",mLinkConfig.car_wifi_ssid.c_str());
  158. if(!(mLinkConfig.car_wifi_ssid.empty()| mLinkConfig.car_wifi_passphrase.empty() | mLinkConfig.car_wifi_channel.empty())){
  159. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  160. if(mDefaultWifi){
  161. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  162. mDefaultWifi = true;
  163. mHandle->CarplaySetStringParameter("WIFI_SSID", mLinkConfig.car_wifi_ssid);
  164. mHandle->CarplaySetStringParameter("WIFI_PASSWD",mLinkConfig.car_wifi_passphrase);
  165. mHandle->CarplaySetIntParameter("WIFI_CHANNEL",stoi(mLinkConfig.car_wifi_channel));
  166. }
  167. }
  168. mLinkMode = linkMode;
  169. mHandle->CarplaySetIntParameter("NEED_AUTH", 1);
  170. mHandle->CarplaySetStringParameter("NAME", "CarPlay System");
  171. mHandle->CarplaySetStringParameter("MODEID", "Linux");
  172. mHandle->CarplaySetStringParameter("MANFACTURER", "ark");
  173. mHandle->CarplaySetStringParameter("SERIALNUMBER", "223000ac3987cf");
  174. mHandle->CarplaySetStringParameter("SW_VER", "sw1.1.0");
  175. mHandle->CarplaySetStringParameter("HW_VER", "hw1.1.0");
  176. mHandle->CarplaySetStringParameter("VEHICLE_NAME", "Ark");
  177. mHandle->CarplaySetIntParameter("OEM_ICON_VISIBLE", 1);
  178. mHandle->CarplaySetStringParameter("OEM_ICON_LABEL", "Home");
  179. mHandle->CarplaySetStringParameter("OEM_ICON_PATH", "/etc/icon_120x120.png");
  180. mHandle->CarplaySetStringParameter("OSINFO", "linux4.14");
  181. mHandle->CarplaySetStringParameter("IOSVER_MIN", "11D257");
  182. mHandle->CarplaySetStringParameter("GUUID", "a527cd2d-7a0f-4165-674b-579f625a160d");
  183. mHandle->CarplaySetStringParameter("DEVID", "00:11:22:33:44:55");
  184. mHandle->CarplaySetStringParameter("BTMAC", "00:11:22:33:44:55");
  185. // mHandle->CarplaySetIntParameter("RIGHTHAND_DRIVER", 0);
  186. mHandle->CarplaySetIntParameter("NIGHTMODE", 0);
  187. mHandle->CarplaySetIntParameter("HAVE_TELBUTTON", 1);
  188. mHandle->CarplaySetIntParameter("HAVE_MEDIABUTTON", 1);
  189. mHandle->CarplaySetIntParameter("HAVE_KNOB", 1);
  190. mHandle->CarplaySetIntParameter("HAVE_PROXSENSOR", 0);
  191. mHandle->CarplaySetIntParameter("HAVE_ENHANCED_REQCARUI", 0);
  192. mHandle->CarplaySetIntParameter("HAVE_ETCSUPPORTED", 0);
  193. mHandle->CarplaySetIntParameter("HIFI_TOUCH", 1);
  194. mHandle->CarplaySetIntParameter("LOFI_TOUCH", 0);
  195. mHandle->CarplaySetIntParameter("USB_COUNRY_CODE", 33);
  196. mHandle->CarplaySetIntParameter("PRODUCT_CODE", 0xa4a1);
  197. mHandle->CarplaySetIntParameter("VERNDOR_CODE", 0x0525);
  198. mHandle->CarplaySetIntParameter("I2C_NUM", 0);
  199. mHandle->CarplaySetIntParameter("I2C_ADDR", 0x22);
  200. mHandle->CarplaySetIntParameter("VIDEO_WIDTH", mLinkConfig.screen_width);
  201. mHandle->CarplaySetIntParameter("VIDEO_HEIGHT", mLinkConfig.screen_height);
  202. mHandle->CarplaySetIntParameter("SCREEN_WIDTH_MM", mCarplayConfig.screen_physical_width);
  203. mHandle->CarplaySetIntParameter("SCREEN_HEIGHT_MM", mCarplayConfig.screen_physical_height);
  204. mHandle->CarplaySetIntParameter("FPS", 30);
  205. mHandle->CarplaySetIntParameter("SCREEN_WIDTH", mLinkConfig.screen_width);
  206. mHandle->CarplaySetIntParameter("SCREEN_HEIGHT", mLinkConfig.screen_height);
  207. mHandle->CarplaySetIntParameter("USB_INDEX",mLinkConfig.usb_index);
  208. mHandle->CarplaySetIntParameter("AUDIO_HANDLE_BY_PLUGIN",1);
  209. /*
  210. mHandle->CarplaySetStringParameter("WIFI_SSID", "CAR-WiFi_49ac");
  211. mHandle->CarplaySetStringParameter("WIFI_PASSWD","49accb62");
  212. mHandle->CarplaySetIntParameter("WIFI_CHANNEL",11);
  213. */
  214. //mHandle->CarplaySetStringParameter("IAP_PATH", "/dev/iap");
  215. static bool flags = false;
  216. if(!flags){
  217. mHandle->init();
  218. mHandle->registerCallbacks(mCbs);
  219. mHandle->registerVideoCallbacks(mVideoCbs);
  220. mHandle->registerAudioCallbacks(mAudioCbs);
  221. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  222. flags = true;
  223. }
  224. return true;
  225. }
  226. bool CarplayLink::release()
  227. {
  228. if(mHandle){
  229. mHandle->CarplayStop();
  230. }
  231. return true;
  232. }
  233. bool CarplayLink::start()
  234. {
  235. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  236. if(mHandle){
  237. if(mLinkMode == Wired)
  238. mHandle->CarplaySetIntParameter("LINK_TYPE", 0);//设置成无线carplay模式 CARPLAY_WL CARPLAY
  239. else if(mLinkMode == Wireless){
  240. mHandle->CarplaySetIntParameter("LINK_TYPE", 7);
  241. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  242. }
  243. mHandle->CarplayStart();
  244. onSdkConnectStatus(CONNECT_STATUS_CONNECTING, mPhoneType);
  245. }
  246. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  247. return true;
  248. }
  249. bool CarplayLink::stop()
  250. {
  251. if(mHandle)
  252. {
  253. mHandle->CarplayChangeModes(1, 500, 500, 100, 0, 0, 0, 0, 0, 0, 0);
  254. mHandle->CarplayChangeModes(0, 0, 0, 0, 1, 500, 500, 100, 0, 0, 0);
  255. }
  256. return true;
  257. }
  258. bool CarplayLink::start_mirror()
  259. {
  260. return true;
  261. }
  262. bool CarplayLink::stop_mirror()
  263. {
  264. return true;
  265. }
  266. bool CarplayLink::set_background()
  267. {
  268. if(mHandle){
  269. mHandle->CarplayChangeModes(1, 500, 500, 100, 0, 0, 0, 0, 0, 0, 0);
  270. }
  271. }
  272. bool CarplayLink::set_foreground()
  273. {
  274. if(mHandle){
  275. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  276. mHandle->CarplayRequestUI("");
  277. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  278. }
  279. }
  280. bool CarplayLink::get_audio_focus()
  281. {
  282. if(mHandle){
  283. //mHandle->CarplayChangeModes();
  284. }
  285. }
  286. bool CarplayLink::release_audio_focus()
  287. {
  288. }
  289. void CarplayLink::set_inserted(bool inserted, PhoneType phoneType)
  290. {
  291. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  292. mPhoneType = phoneType;
  293. }
  294. void CarplayLink::send_screen_size(int width, int height)
  295. {
  296. }
  297. #define DUMP_PLAY_FILE 0
  298. #if DUMP_PLAY_FILE
  299. static FILE *pfile = NULL;
  300. #endif
  301. #define DUMP_REC_FILE 0
  302. #if DUMP_REC_FILE
  303. static FILE *pRecfile = NULL;
  304. #endif
  305. #include <sys/time.h>
  306. #include <unistd.h>
  307. void CarplayLink::record_audio_callback(unsigned char *data, int len)
  308. {
  309. std::lock_guard<std::mutex> lock(mMutex);
  310. //mRecData += string((char*)data, len);
  311. #if 1 //Only for test.
  312. struct timeval test_tv;
  313. gettimeofday(&test_tv,NULL);
  314. long test = test_tv.tv_sec * 1000 + test_tv.tv_usec / 1000;
  315. #endif
  316. #if DUMP_REC_FILE
  317. if (NULL == pRecfile) {
  318. pRecfile = fopen("/tmp/rec_in.pcm", "w");
  319. }
  320. if (pRecfile) {
  321. fwrite(data, 1, len, pRecfile);
  322. //printf("rec len:%d\r\n ", len);
  323. }
  324. #endif
  325. #ifdef AEC_DELAY
  326. //printf("mAudioStreamType = %d\n", mAudioStreamType);
  327. if(mAudioStreamType == AudioStreamCall){
  328. int AecLen = 0;
  329. unsigned char * pAecBuf = NULL;
  330. int type = 0 ;
  331. if(mAecQueue->IsExitedThread(pthread_self()))
  332. return ;
  333. if(mAecQueue->ReadQueue(&type, &pAecBuf, &AecLen) > 0){
  334. // printf("Aec len = %d\r\n", AecLen);
  335. short *farFrame = (short *)pAecBuf;
  336. short *nearFrame = (short *)data;
  337. short *outFrame = (short *)data;
  338. int frameCount = (len/2)/160;
  339. for(int i=0; i<frameCount; i++) {
  340. if(mAecHandle)
  341. WebRtcAecFrameProcess(mAecHandle, (short*)farFrame, (short*)nearFrame, (short*)outFrame, mCarplayConfig.aec_delay);
  342. farFrame += 160;
  343. nearFrame += 160;
  344. outFrame += 160;
  345. }
  346. }
  347. free(pAecBuf);
  348. pAecBuf = NULL;
  349. }
  350. #endif
  351. if(mHandle)
  352. mHandle->AudioRecordStream(mHandle1, data, len, len / 2, 0);
  353. //mRecData.clear();
  354. //printf("%s:%s:%d record len = %d\r\n",__FILE__,__func__,__LINE__,len);
  355. #if 1 //Only for test.
  356. struct timeval test_tv1;
  357. gettimeofday(&test_tv1,NULL);
  358. long test1 = test_tv1.tv_sec * 1000 + test_tv1.tv_usec / 1000;
  359. printf("echo cancel time:%ldms\n", test1-test);
  360. #endif
  361. }
  362. void CarplayLink::send_car_bluetooth(const string& name, const string& address, const string& pin)
  363. {
  364. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  365. }
  366. void CarplayLink::send_phone_bluetooth(const string& address)
  367. {
  368. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  369. printf("address = %s\r\n",address.c_str());
  370. if(mHandle){
  371. mDdefaultPhonebtMac = false;
  372. mHandle->CarplaySetStringParameter("REMOTE_BTMAC", address);
  373. }
  374. }
  375. void CarplayLink::send_touch(int x, int y, TouchCode touchCode)
  376. {
  377. printf("carplay x:%d, y:%d, press:%d\r\n",x, y, touchCode);
  378. if(mHandle){
  379. mHandle->CarplaySendSingleTouchPoint(x, y, touchCode);
  380. }
  381. }
  382. void CarplayLink::send_car_wifi(WifiInfo& info){
  383. if(mHandle){
  384. mDefaultWifi = false;
  385. mHandle->CarplaySetStringParameter("WIFI_SSID", info.ssid);
  386. mHandle->CarplaySetStringParameter("WIFI_PASSWD",info.passphrase);
  387. mHandle->CarplaySetIntParameter("WIFI_CHANNEL",stoi(info.channel_id));
  388. }
  389. }
  390. bool CarplayLink::send_key(KeyCode keyCode)
  391. {
  392. printf("%s:%s:%d key = %d\r\n",__FILE__,__func__,__LINE__, keyCode);
  393. if(mHandle){
  394. if(keyCode == KEY_MUSIC_PLAY){
  395. mHandle->CarplaySendMediaKey(MEDIA_PLAY, true);
  396. mHandle->CarplaySendMediaKey(MEDIA_PLAY, false);
  397. }
  398. else if(keyCode == KEY_MUSIC_PAUSE){
  399. mHandle->CarplaySendMediaKey(MEDIA_PAUSE, true);
  400. mHandle->CarplaySendMediaKey(MEDIA_PLAY, false);
  401. }
  402. else if(keyCode == KEY_MUSIC_PLAY_PAUSE){
  403. mHandle->CarplaySendMediaKey(MEDIA_PLAY_PAUSE, true);
  404. mHandle->CarplaySendMediaKey(MEDIA_PLAY_PAUSE, false);
  405. }
  406. else if(keyCode == KEY_MUSIC_NEXT){
  407. mHandle->CarplaySendMediaKey(MEDIA_NEXT, true);
  408. mHandle->CarplaySendMediaKey(MEDIA_NEXT, true);
  409. mHandle->CarplaySendMediaKey(MEDIA_NEXT, false);
  410. }
  411. else if(keyCode == KEY_MUSIC_PREVIOUS){
  412. mHandle->CarplaySendMediaKey(MEDIA_PREVIOUS, true);
  413. mHandle->CarplaySendMediaKey(MEDIA_PREVIOUS, true);
  414. mHandle->CarplaySendMediaKey(MEDIA_PREVIOUS, false);
  415. }
  416. else if(keyCode == KEY_PICKUP_PHONE){
  417. mHandle->CarplaySendTelephoneKey(1, true);
  418. mHandle->CarplaySendTelephoneKey(0, false);
  419. }
  420. else if(keyCode == KEY_HANGUP_PHONE){
  421. mHandle->CarplaySendTelephoneKey(3, true);
  422. mHandle->CarplaySendTelephoneKey(0, false);
  423. }
  424. else if(keyCode == KYE_HOME){
  425. mHandle->CarplayRequestUI("");
  426. }
  427. else if(keyCode == KEY_PHONE){
  428. mHandle->CarplayRequestUI("mobilephone:");
  429. }
  430. else if(keyCode == KEY_VOICE_ASSISTANT){
  431. mHandle->CarplaysendSiriButton(true);
  432. sleep(1);
  433. mHandle->CarplaysendSiriButton(false);
  434. }
  435. else if(keyCode == KEY_LIGHTMODE){
  436. //mHandle->CarplaySetIntParameter("NIGHTMODE", 0);
  437. mHandle->CarplaySendNightMode(0);
  438. }
  439. else if(keyCode == KEY_NIGHTMODE){
  440. //mHandle->CarplaySetIntParameter("NIGHTMODE", 1);
  441. mHandle->CarplaySendNightMode(1);
  442. }
  443. else if(keyCode == KEY_CAR_FRONT){
  444. set_foreground();
  445. }
  446. else if(keyCode == KEY_CAR_BACK){
  447. set_background();
  448. }
  449. }
  450. return true;
  451. }
  452. bool CarplayLink::send_wheel(WheelCode wheel, bool foucs)
  453. {
  454. if(mHandle){
  455. mHandle->CarplaySendKnob(foucs, 0, 0, 0, 0, wheel);
  456. }
  457. return true;
  458. }
  459. bool CarplayLink::send_night_mode(bool night)
  460. {
  461. if(mHandle){
  462. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  463. mHandle->CarplaySetIntParameter("NIGHTMODE", night);
  464. }
  465. return true;
  466. }
  467. bool CarplayLink::send_right_hand_driver(bool right){
  468. if(mHandle){
  469. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  470. mHandle->CarplaySetIntParameter("RIGHTHAND_DRIVER", right);
  471. }
  472. }
  473. bool CarplayLink::open_page(AppPage appPage)
  474. {
  475. if(mHandle == NULL)
  476. return false;
  477. if(appPage == APP_PAGE_NAVIGATION){
  478. mHandle->CarplayRequestUI("maps:");
  479. }
  480. else if(appPage == APP_PAGE_MAIN){
  481. mHandle->CarplayRequestUI("");
  482. }
  483. return true;
  484. }
  485. void CarplayLink::request_status(RequestAppStatus requestAppStatus, void *reserved)
  486. {
  487. if(requestAppStatus == QUERYTIME){
  488. static long long time = mLocalTime;
  489. app_status(APP_RESERVED, (void*)time);
  490. printf("static local time = %lld\r\n",time);
  491. }
  492. }
  493. bool CarplayLink::sendPlayData(int handle, char type, const char* buf, int len, int frames, long long time_stamp)
  494. {
  495. printf("play hanle :%x type = %d, len = %d, time_stamp = %d\r\n",handle, type, len, time_stamp);
  496. #if 0 //Only for test.
  497. struct timeval test_tv;
  498. gettimeofday(&test_tv,NULL);
  499. long test = test_tv.tv_sec * 1000 + test_tv.tv_usec / 1000;
  500. #endif
  501. if(mHandle){
  502. mHandle->AudioPlayStream(handle, (void*)buf, len, frames, time_stamp);
  503. audio_play((AudioType)type, buf, len);
  504. #ifdef AEC_DELAY
  505. if(mAecQueue && mAecHandle && type == AudioStreamCall){
  506. uint8_t *p = (uint8_t*)malloc(len);
  507. memcpy(p, buf, len);
  508. mAecQueue->WriteQueue(0, p, len);
  509. // printf("insert p = %d\r\n", len);
  510. }
  511. #endif
  512. usleep(10);
  513. #if DUMP_PLAY_FILE
  514. if (NULL == pfile) {
  515. pfile = fopen("/tmp/play_out.pcm", "w");
  516. }
  517. if (pfile) {
  518. fwrite(buf, 1, len, pfile);
  519. printf("play len:%d\r\n ", len);
  520. }
  521. #endif
  522. }
  523. #if 0 //Only for test.
  524. struct timeval test_tv1;
  525. gettimeofday(&test_tv1,NULL);
  526. long test1 = test_tv1.tv_sec * 1000 + test_tv1.tv_usec / 1000;
  527. printf("play time:%ldms\n", test1-test);
  528. #endif
  529. return true;
  530. }
  531. bool CarplayLink::receiveRecordData(int handle, int frames)
  532. {
  533. mHandle1 = handle;
  534. // std::lock_guard<std::mutex> lock(mMutex);
  535. /*
  536. void *tmp = NULL;
  537. int len = mRecData.size();
  538. tmp = (void*)mRecData.c_str();
  539. printf("rec len = %d\r\n",len);
  540. if(mHandle)
  541. mHandle->AudioRecordStream(handle, tmp, len, len / frames, 0);
  542. //printf("rec len = %d\r\n",len);
  543. mRecData.clear();
  544. */
  545. sleep(1);
  546. return true;
  547. }
  548. void CarplayLink::setLocalTime(long long local)
  549. {
  550. mLocalTime = local;
  551. printf("mLocalTime time = %lld\r\n",mLocalTime);
  552. }
  553. #endif