IUserLinkPlayer.cpp 12 KB

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