IUserLinkPlayer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. #include "IUserLinkPlayer.h"
  2. #include "CarlifeLink.h"
  3. #include "CarplayLink.h"
  4. #include "EasyConnectLink.h"
  5. #include "MirrorLink.h"
  6. #include "HiCarLink.h"
  7. #include "UsbHostService.h"
  8. #include "VideoDecoder.h"
  9. #include "AudioDecoder.h"
  10. #include "AudioRecord.h"
  11. #include "ConfigParser.h"
  12. #include "util.h"
  13. #include "LinkAssist.h"
  14. #include <stdio.h>
  15. #include <cstdlib>
  16. #include <iostream>
  17. #include <sstream>
  18. #include <sys/time.h>
  19. using namespace XEngine;
  20. uint64_t getTickCount()
  21. {
  22. timeval tv;
  23. gettimeofday(&tv, NULL);
  24. return ((uint64_t)tv.tv_sec) * 1000 + tv.tv_usec / 1000;
  25. }
  26. IUserLinkPlayer::IUserLinkPlayer():mpIULPlayer(this),mpMusicDecoder(new AudioDecoder("plug:softvol2")),
  27. mpTTSDecoder(new AudioDecoder("plug:softvol1")),mpVRDecoder(new AudioDecoder("plug:softvol4")),mpCallDecoder(new AudioDecoder("plug:softvol3")),mFuncAppStatusCallback(nullptr),mFuncConnectCallback(nullptr),mConnected(false),mVideoStart(false)
  28. ,mFuncBlueToothPhoneCallback(nullptr)
  29. {
  30. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  31. }
  32. IUserLinkPlayer::~IUserLinkPlayer()
  33. {
  34. printf("%d:%s\r\n",__LINE__, __func__);
  35. delete mpMusicDecoder;
  36. delete mpTTSDecoder;
  37. delete mpVRDecoder;
  38. delete mpCallDecoder;
  39. }
  40. void IUserLinkPlayer::RegisterConnectCallback(FUNCCONNECTCALLBACK funcConnectCallback)
  41. {
  42. mFuncConnectCallback = funcConnectCallback;
  43. }
  44. void IUserLinkPlayer::RegisterAppStatusCallback(FUNCAPPSTATUSCALLBACK funcAppStatusCallback)
  45. {
  46. mFuncAppStatusCallback = funcAppStatusCallback;
  47. }
  48. void IUserLinkPlayer::RegisterBlueToothPhoneCallback(FUNCBLUETOOTHPHONECALLBACK funcBlueToothPhoneCallback)
  49. {
  50. mFuncBlueToothPhoneCallback = funcBlueToothPhoneCallback;
  51. }
  52. void IUserLinkPlayer::GetIniConfig(LinkAssist *pLinkAssist)
  53. {
  54. mLinkConfig = pLinkAssist->GetConfigInfo();
  55. mCarplayConfig = pLinkAssist->GetCarplayInfo();
  56. mCarlifeConfig = pLinkAssist->GetCarlifeInfo();
  57. }
  58. bool IUserLinkPlayer::Initialize(LinkMode linkMode, PhoneType phoneType)
  59. {
  60. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  61. printf("mpIULPlayer = %x, linkMode = %d, phoneType = %d\n",mpIULPlayer, linkMode, phoneType);
  62. mpIULPlayer->set_inserted(true, phoneType);
  63. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  64. mpIULPlayer->init(linkMode);
  65. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  66. return true;
  67. }
  68. bool IUserLinkPlayer::Start()
  69. {
  70. if(mpIULPlayer)
  71. mpIULPlayer->start();
  72. return true;
  73. }
  74. bool IUserLinkPlayer::Stop()
  75. {
  76. if(mpIULPlayer)
  77. mpIULPlayer->stop();
  78. return true;
  79. }
  80. void IUserLinkPlayer::Release()
  81. {
  82. printf("%d:%s\r\n",__LINE__, __func__);
  83. if(mpIULPlayer){
  84. mpIULPlayer->release();
  85. }
  86. }
  87. bool IUserLinkPlayer::StartMirror()
  88. {
  89. if(mpIULPlayer)
  90. mpIULPlayer->start_mirror();
  91. return true;
  92. }
  93. bool IUserLinkPlayer::StopMirror()
  94. {
  95. if(mpIULPlayer)
  96. mpIULPlayer->stop_mirror();
  97. return true;
  98. }
  99. bool IUserLinkPlayer::SetBackground()
  100. {
  101. //VideoDecoder::instance()->Show(false);
  102. if(mpIULPlayer)
  103. mpIULPlayer->set_background();
  104. return true;
  105. }
  106. bool IUserLinkPlayer::SetForeground()
  107. {
  108. //VideoDecoder::instance()->Show(true);
  109. if(mpIULPlayer)
  110. mpIULPlayer->set_foreground();
  111. return true;
  112. }
  113. void IUserLinkPlayer::SetInserted(bool inserted, PhoneType phoneType)
  114. {
  115. if(mpIULPlayer)
  116. mpIULPlayer->set_inserted(inserted, phoneType);
  117. }
  118. void IUserLinkPlayer::SendScreenSize(int width, int height)
  119. {
  120. if(mpIULPlayer)
  121. mpIULPlayer->send_screen_size(width, height);
  122. }
  123. void IUserLinkPlayer::SendTouch(int x, int y, TouchCode touchCode)
  124. {
  125. if(mpIULPlayer){
  126. mpIULPlayer->send_touch(x, y, touchCode);
  127. }
  128. }
  129. void IUserLinkPlayer::SendMultiTouch(int x1, int y1, TouchCode touchCode1,int x2, int y2, TouchCode touchCode2)
  130. {
  131. if(mpIULPlayer){
  132. mpIULPlayer->send_multi_touch(x1, y1, touchCode1, x2, y2, touchCode2);
  133. }
  134. }
  135. void IUserLinkPlayer::SendKey(KeyCode keyCode)
  136. {
  137. if(mpIULPlayer){
  138. mpIULPlayer->send_key(keyCode);
  139. }
  140. }
  141. void IUserLinkPlayer::SendWheel(WheelCode wheelCode, bool bFoucs)
  142. {
  143. if(mpIULPlayer){
  144. mpIULPlayer->send_wheel(wheelCode, bFoucs);
  145. }
  146. }
  147. bool IUserLinkPlayer::RequestStatus(RequestAppStatus requestAppStatus, void *reserved)
  148. {
  149. if(mpIULPlayer){
  150. mpIULPlayer->request_status(requestAppStatus,reserved);
  151. }
  152. }
  153. void IUserLinkPlayer::SendCarBluetooth(const string& name, const string& address, const string& pin)
  154. {
  155. if(mpIULPlayer){
  156. mpIULPlayer->send_car_bluetooth(name, address, pin);
  157. }
  158. }
  159. void IUserLinkPlayer::SendPhoneBluetooth(const string& address)
  160. {
  161. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  162. printf("mpIULPlayer = %x\r\n", mpIULPlayer);
  163. if(mpIULPlayer){
  164. mpIULPlayer->send_phone_bluetooth(address);
  165. }
  166. }
  167. void IUserLinkPlayer::SendCarWifi(WifiInfo& info){
  168. if(mpIULPlayer){
  169. mpIULPlayer->send_car_wifi(info);
  170. }
  171. }
  172. void IUserLinkPlayer::SendBlueToothCmd(const string& cmd)
  173. {
  174. if(mpIULPlayer){
  175. mpIULPlayer->send_bluetooth_cmd(cmd);
  176. }
  177. }
  178. void IUserLinkPlayer::SendBroadcast(bool enable)
  179. {
  180. if(mpIULPlayer){
  181. mpIULPlayer->send_broadcast(enable);
  182. }
  183. }
  184. void IUserLinkPlayer::SendDelayRecord(int millisecond)
  185. {
  186. if(mpIULPlayer){
  187. mpIULPlayer->send_delay_record(millisecond);
  188. }
  189. }
  190. void IUserLinkPlayer::SendWifiStateChanged(WifiStateAction action, WifiState state, const string& phoneIp, const string& carIp)
  191. {
  192. if(mpIULPlayer){
  193. mpIULPlayer->send_wifi_state_changed(action, state, phoneIp, carIp);
  194. }
  195. }
  196. void IUserLinkPlayer::onSdkConnectStatus(ConnectedStatus status, PhoneType type)
  197. {
  198. // todo 获取连接状态
  199. // 根据不同的连接状态,车机界面给予用户不同的提示或者界面跳转
  200. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  201. //第一次插入已经知晓UsbHostService notify手机类型
  202. switch (status)
  203. {
  204. case CONNECT_STATUS_CONNECTING:
  205. {
  206. // todo 正在连接
  207. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  208. break;
  209. }
  210. case CONNECT_STATUS_CONNECT_FAILED:
  211. {
  212. // todo 连接失败
  213. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  214. break;
  215. }
  216. case CONNECT_STATUS_CONNECT_SUCCEED:
  217. {
  218. // todo 连接成功
  219. mConnected = true;
  220. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  221. break;
  222. }
  223. case CONNECT_STATUS_DISCONNECTED:
  224. {
  225. // todo 连接断开
  226. mConnected = false;
  227. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  228. break;
  229. }
  230. case CONNECT_STATUS_APP_EXIT:
  231. {
  232. // todo 手机app退出
  233. mConnected = false;
  234. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  235. break;
  236. }
  237. case CONNECT_STATUS_INTERRUPTED_BY_APP:
  238. {
  239. // todo 手机app中断
  240. mConnected = false;
  241. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  242. break;
  243. }
  244. default:
  245. break;
  246. }
  247. if(mFuncConnectCallback){
  248. mFuncConnectCallback(status, type);
  249. }
  250. }
  251. bool IUserLinkPlayer::OpenPage(AppPage appPage)
  252. {
  253. if(mpIULPlayer)
  254. mpIULPlayer->open_page(appPage);
  255. return true;
  256. }
  257. bool IUserLinkPlayer::SendNightMode(bool night){
  258. if(mpIULPlayer)
  259. mpIULPlayer->send_night_mode(night);
  260. return true;
  261. }
  262. bool IUserLinkPlayer::SendRightHandDriver(bool right){
  263. if(mpIULPlayer)
  264. mpIULPlayer->send_right_hand_driver(right);
  265. return true;
  266. }
  267. bool IUserLinkPlayer::SendIphoneMacAddress(string address)
  268. {
  269. if(mpIULPlayer){
  270. mpIULPlayer->set_mac(address);
  271. }
  272. return true;
  273. }
  274. void IUserLinkPlayer::SendLisenceCode(const string& license)
  275. {
  276. if(mpIULPlayer){
  277. mpIULPlayer->send_license(license);
  278. }
  279. }
  280. void IUserLinkPlayer::SendInputText(const string &text)
  281. {
  282. if(mpIULPlayer){
  283. mpIULPlayer->send_input_text(text);
  284. }
  285. }
  286. void IUserLinkPlayer::SendInputSelection(int start, int stop){
  287. if(mpIULPlayer){
  288. mpIULPlayer->send_input_selection(start,stop);
  289. }
  290. }
  291. void IUserLinkPlayer::SendInputAction(int action, int keyCode)
  292. {
  293. if(mpIULPlayer){
  294. mpIULPlayer->send_input_action(action, keyCode);
  295. }
  296. }
  297. void IUserLinkPlayer::video_start(int offset_x, int offset_y, int width, int height)
  298. {
  299. printf("video_start width = %d, height = %d\r\n", width, height);
  300. printf("video_start diy_screen = %d, screen_width = %d, screen_height = %d\r\n",
  301. mLinkConfig.diy_screen, mLinkConfig.screen_width, mLinkConfig.screen_height);
  302. if(mLinkConfig.diy_screen){
  303. mVideoFrame = {offset_x,offset_y,width, height,
  304. mLinkConfig.offset_x, mLinkConfig.offset_y ,
  305. mLinkConfig.screen_width, mLinkConfig.screen_height,VERTICAL};
  306. }
  307. else
  308. {
  309. int screen_width = 800;
  310. int screen_height = 480;
  311. // Util::getFrameBufferFixedSize(screen_width, screen_height, "/dev/fb0");
  312. mVideoFrame = {offset_x, offset_y ,width, height, 0, 0, screen_width, screen_height, VERTICAL};
  313. }
  314. mVideoStart = true;
  315. VideoDecoder::instance()->Init(&mVideoFrame);
  316. }
  317. void IUserLinkPlayer::video_stop()
  318. {
  319. printf("++ video_stop ++\r\n");
  320. Autolock l(&mMutexVideo);
  321. mVideoStart = false;
  322. VideoDecoder::instance()->Uninit();
  323. printf("-- video_stop --\r\n");
  324. }
  325. void IUserLinkPlayer::video_play(const void *data, int32_t len)
  326. {
  327. //printf("video len = %d\r\n", len);
  328. static int frameCounter = 0;
  329. static int64_t beginTime = 0;
  330. int64_t currentTime;
  331. currentTime = getTickCount();
  332. if (!beginTime)
  333. {
  334. beginTime = currentTime;
  335. }
  336. frameCounter++;
  337. if (currentTime - beginTime >= 5000)
  338. {
  339. printf("5 seconds average fps: %d\n", (int)(frameCounter * 1000 / (currentTime - beginTime)));
  340. beginTime = 0;
  341. frameCounter = 0;
  342. }
  343. // printf("++ video len = %d ++\n", len);
  344. Autolock l(&mMutexVideo);
  345. if(mVideoStart)
  346. VideoDecoder::instance()->InputDecoder(data, len);
  347. // printf("-- video len = %d --\n", len);
  348. }
  349. void IUserLinkPlayer::record_start(AudioInfo &audioInfo)
  350. {
  351. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  352. AudioRecord::instance()->registerRecordAudio(std::bind(&IUserLinkPlayer::record_audio_callback,this, std::placeholders::_1,std::placeholders::_2));
  353. AudioRecord::instance()->setMediaParam(audioInfo.sampleRate, audioInfo.format, audioInfo.channel);
  354. AudioRecord::instance()->start();
  355. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  356. }
  357. void IUserLinkPlayer::record_stop()
  358. {
  359. printf("%d:%s\r\n",__LINE__, __func__);
  360. AudioRecord::instance()->Release();
  361. AudioRecord::instance()->join();
  362. printf("%d:%s\r\n",__LINE__, __func__);
  363. }
  364. void IUserLinkPlayer::record_pause(bool pause)
  365. {
  366. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  367. if(pause)
  368. AudioRecord::instance()->stopRecordSound();
  369. else
  370. AudioRecord::instance()->resumeRecordSound();
  371. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  372. }
  373. bool IUserLinkPlayer::app_status(AppStatusMessage appStatusMessage, void *reserved)
  374. {
  375. printf("appStatusMessage = %d\r\n", appStatusMessage);
  376. if(mConnected){
  377. if(appStatusMessage == APP_FOREGROUND){
  378. VideoDecoder::instance()->Show(true);
  379. }
  380. else if(appStatusMessage == APP_BACKGROUND){
  381. printf("back :%d\r\n", reserved);
  382. if((bool)reserved){
  383. Autolock l(&mMutexVideo);
  384. mVideoStart = false;
  385. }
  386. VideoDecoder::instance()->Show(false);
  387. }
  388. }
  389. if(mFuncAppStatusCallback)
  390. {
  391. mFuncAppStatusCallback(appStatusMessage, reserved);
  392. }
  393. }
  394. bool IUserLinkPlayer::bt_call_action(CallType callType, const char *name, const char* number)
  395. {
  396. printf("calltype = %d, name = %s, number = %s\r\n",callType, name, number);
  397. if(mFuncBlueToothPhoneCallback)
  398. {
  399. mFuncBlueToothPhoneCallback(callType, (char*)name, (char*)number);
  400. }
  401. return true;
  402. }
  403. bool IUserLinkPlayer::audio_start(AudioType audioType, int rate, int bit, int channel)
  404. {
  405. printf("start audio type = %d, rate = %d, bit = %d, channel = %d\r\n", audioType, rate, bit, channel);
  406. if(audioType == AUDIO_TYPE_MUSIC){
  407. mpMusicDecoder->setMediaParam(rate,bit, channel);
  408. app_status(APP_MUSIC_STARTED, nullptr);
  409. }
  410. else if(audioType == AUDIO_TYPE_TTS){
  411. mpTTSDecoder->setMediaParam(rate,bit, channel);
  412. app_status(APP_NAVI_SOUND_STARTED, nullptr);
  413. }
  414. else if(audioType == AUDIO_TYPE_VR){
  415. mpVRDecoder->setMediaParam(rate,bit, channel);
  416. app_status(APP_VR_STARTED, nullptr);
  417. }
  418. else if(audioType == AUDIO_TYPE_CALL){
  419. mpCallDecoder->setMediaParam(rate,bit, channel);
  420. //app_status(APP_PHONE_STARTED, nullptr);
  421. }
  422. return true;
  423. }
  424. bool IUserLinkPlayer::audio_stop(AudioType audioType)
  425. {
  426. printf("stop audio type = %d\r\n", audioType);
  427. if(audioType == AUDIO_TYPE_MUSIC){
  428. mpMusicDecoder->release();
  429. app_status(APP_MUSIC_STOPPED, nullptr);
  430. }
  431. else if(audioType == AUDIO_TYPE_TTS){
  432. mpTTSDecoder->release();
  433. app_status(APP_NAVI_SOUND_STOPPED, nullptr);
  434. }
  435. else if(audioType == AUDIO_TYPE_VR){
  436. mpVRDecoder->release();
  437. app_status(APP_VR_STOPPED, nullptr);
  438. }
  439. else if(audioType == AUDIO_TYPE_CALL){
  440. mpCallDecoder->release();
  441. app_status(APP_PHONE_STOPPED, nullptr);
  442. }
  443. return true;
  444. }
  445. static int PrintCurrentSpeaker(int length)
  446. {
  447. static long length_1s = 0;
  448. static time_t old_time = 0;
  449. time_t now_time = time(NULL);
  450. if(old_time == 0) old_time = now_time;
  451. length_1s += length;
  452. if((now_time - old_time) >= 1){
  453. printf("consume speaker out:%ld byte/s\n ", length_1s/(now_time - old_time));
  454. old_time = now_time;
  455. length_1s = 0;
  456. }
  457. return 0;
  458. }
  459. void IUserLinkPlayer::audio_play(AudioType audioType, const void* data, uint32_t len)
  460. {
  461. // printf("audiotype = %d , len = %d\r\n", audioType, len);
  462. // printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  463. if(audioType == AUDIO_TYPE_MUSIC){
  464. // PrintCurrentSpeaker(len);
  465. mpMusicDecoder->playSound((unsigned char*)data, len);
  466. }
  467. else if(audioType == AUDIO_TYPE_TTS){
  468. mpTTSDecoder->playSound((unsigned char*)data, len);
  469. }
  470. else if(audioType == AUDIO_TYPE_VR){
  471. mpVRDecoder->playSound((unsigned char*)data, len);
  472. }
  473. else if(audioType == AUDIO_TYPE_CALL){
  474. mpCallDecoder->playSound((unsigned char*)data, len);
  475. }
  476. // printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  477. }