CarlifeLink.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #include "CarlifeLink.h"
  2. #include "carlifeplayer.h"
  3. #include "util.h"
  4. #include "AudioRecord.h"
  5. #include "CCarLifeLibWrapper.h"
  6. #include "UsbHostService.h"
  7. using namespace CCarLifeLibH;
  8. CarlifeLink::CarlifeLink()
  9. {
  10. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  11. #ifdef USE_CARLIFE
  12. m_pCarlifePlayer = new CarlifePlayer();
  13. mBRecorder = true;
  14. m_blongpress = false;
  15. #endif
  16. }
  17. CarlifeLink::~CarlifeLink(){
  18. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  19. #ifdef USE_CARLIFE
  20. if(m_pCarlifePlayer){
  21. delete m_pCarlifePlayer;
  22. m_pCarlifePlayer = nullptr;
  23. }
  24. #endif
  25. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  26. }
  27. #ifdef USE_CARLIFE
  28. extern int gvid;
  29. bool CarlifeLink::init(LinkMode linkMode)
  30. {
  31. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  32. if(m_pCarlifePlayer){
  33. m_pCarlifePlayer->SetEapLink(true);
  34. m_pCarlifePlayer->SetAoaLink(true);
  35. m_pCarlifePlayer->SetidVendor(gvid);
  36. m_pCarlifePlayer->Initialize();
  37. //m_pCarlifePlayer->GetCarScreenSize(mLinkConfig.screen_width,mLinkConfig.screen_height);
  38. //printf("mLinkConfig.screen_width:%d,mLinkConfig.screen_height:%d\r\n",mLinkConfig.screen_width, mLinkConfig.screen_height);
  39. m_pCarlifePlayer->SetLinkstatusCallback(std::bind(&CarlifeLink::status_callback_func,this, std::placeholders::_1,std::placeholders::_2),this);
  40. m_pCarlifePlayer->SetVideoStartCallback(std::bind(&CarlifeLink::video_start_callback_func,this, std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4),this);
  41. m_pCarlifePlayer->SetVideoInfoCallback(true, std::bind(&CarlifeLink::video_callback_func,this, std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5),this);
  42. m_pCarlifePlayer->SetAudioStartCallback(std::bind(&CarlifeLink::audio_start_callback_func,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5,std::placeholders::_6),this);
  43. m_pCarlifePlayer->SetAudioInfoCallback(std::bind(&CarlifeLink::audio_callback_func,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4),this);
  44. m_pCarlifePlayer->SetCmdPhoneNumberCallback(std::bind(&CarlifeLink::phone_number_callback_func,this,std::placeholders::_1,std::placeholders::_2),this);
  45. }
  46. return true;
  47. }
  48. void CarlifeLink::set_inserted(bool inserted, PhoneType phoneType)
  49. {
  50. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  51. if(m_pCarlifePlayer){
  52. if(phoneType == Phone_IOS)
  53. m_pCarlifePlayer->SetLinkType(IOS_CARLIFE);
  54. else if(phoneType == Phone_Android)
  55. m_pCarlifePlayer->SetLinkType(ANDROID_CARLIFE);
  56. m_pCarlifePlayer->SetInserted(inserted);
  57. mPhoneType = phoneType;
  58. }
  59. }
  60. bool CarlifeLink::release()
  61. {
  62. if(m_pCarlifePlayer){
  63. m_pCarlifePlayer->UnInitialize();
  64. delete m_pCarlifePlayer;
  65. m_pCarlifePlayer = nullptr;
  66. }
  67. }
  68. bool CarlifeLink::start()
  69. {
  70. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  71. if(m_pCarlifePlayer){
  72. onSdkConnectStatus(CONNECT_STATUS_CONNECTING, mPhoneType);
  73. m_pCarlifePlayer->StartLink(NULL);
  74. }
  75. return true;
  76. }
  77. bool CarlifeLink::stop()
  78. {
  79. if(m_pCarlifePlayer){
  80. m_pCarlifePlayer->ExitProcess();
  81. }
  82. return true;
  83. }
  84. bool CarlifeLink::start_mirror()
  85. {
  86. if(m_pCarlifePlayer)
  87. m_pCarlifePlayer->VideoStart();
  88. return true;
  89. }
  90. bool CarlifeLink::stop_mirror()
  91. {
  92. if(m_pCarlifePlayer)
  93. m_pCarlifePlayer->VideoPause();
  94. return true;
  95. }
  96. void CarlifeLink::set_mac(string mac)
  97. {
  98. if(m_pCarlifePlayer)
  99. m_pCarlifePlayer->SetIPAddress((char*)mac.c_str());
  100. }
  101. bool CarlifeLink::send_key(KeyCode keyCode)
  102. {
  103. m_pCarlifePlayer->Key(keyCode);
  104. return true;
  105. }
  106. void CarlifeLink::send_touch(int x, int y, TouchCode touchCode)
  107. {
  108. if(touchCode == Touch_Press)
  109. {
  110. m_src_x[0] = x;
  111. m_src_y[0] = y;
  112. m_old_src_x[0] = x;
  113. m_old_src_y[0] = y;
  114. m_blongpress = false;
  115. if(m_pCarlifePlayer->GetPhoneOS() == IOS){
  116. m_pCarlifePlayer->Touch(m_src_x[0], m_src_y[0], mVideoFrame.dst_width, mVideoFrame.dst_height, ScreenPress);
  117. return ;
  118. }
  119. else
  120. {
  121. m_pCarlifePlayer->Touch(m_src_x[0], m_src_y[0], mVideoFrame.dst_width, mVideoFrame.dst_height, ScreenPress);
  122. }
  123. }
  124. else if(touchCode == Touch_Up)
  125. {
  126. if(m_pCarlifePlayer->GetPhoneOS() != IOS){
  127. m_pCarlifePlayer->Touch(x, y, mVideoFrame.dst_width, mVideoFrame.dst_height, ScreenRelease);
  128. }
  129. else if(m_pCarlifePlayer->GetPhoneOS() == IOS /*&& m_blongpress == false*/)
  130. {
  131. if(abs(m_old_src_x[0] - x) < 10 && abs(m_old_src_y[0] - y) < 10)
  132. {
  133. m_pCarlifePlayer->Touch(m_src_x[0], m_src_y[0], mVideoFrame.dst_width, mVideoFrame.dst_height,ScreenSingleClick);
  134. }
  135. }
  136. m_blongpress = false;
  137. }
  138. else if(touchCode == Touch_Move)
  139. {
  140. if(m_pCarlifePlayer->GetPhoneOS() == IOS && m_blongpress == false)
  141. {
  142. m_blongpress = true;
  143. m_pCarlifePlayer->Touch(x, y, mVideoFrame.dst_width, mVideoFrame.dst_height,ScreenLongPress);
  144. m_src_x[0] = x;
  145. m_src_y[0] = y;
  146. return;
  147. }
  148. m_pCarlifePlayer->Touch(x, y, mVideoFrame.dst_width, mVideoFrame.dst_height,ScreenMove);
  149. m_src_x[0] = x;
  150. m_src_y[0] = y;
  151. }
  152. }
  153. void CarlifeLink::request_status(RequestAppStatus requestAppStatus, void *reserved)
  154. {
  155. app_status(APP_RESERVED, nullptr);
  156. }
  157. bool CarlifeLink::send_wheel(WheelCode wheel, bool foucs)//foucs配置的时候生效
  158. {
  159. if(wheel == Wheel_Next){
  160. m_pCarlifePlayer->Key(KEYCODE_Selector_Next);
  161. }
  162. else if(wheel == Wheel_Previous){
  163. m_pCarlifePlayer->Key(KEYCODE_Selector_Previous);
  164. }
  165. return true;
  166. }
  167. bool CarlifeLink::send_night_mode(bool night)
  168. {
  169. }
  170. bool CarlifeLink::set_background()
  171. {
  172. m_pCarlifePlayer->SetPlayForeground(true);
  173. //VideoDecoder::instance()->Show(false);
  174. return true;
  175. }
  176. bool CarlifeLink::set_foreground()
  177. {
  178. m_pCarlifePlayer->SetPlayForeground(false);
  179. return true;
  180. }
  181. bool CarlifeLink::get_audio_focus()
  182. {
  183. return true;
  184. }
  185. bool CarlifeLink::release_audio_focus()
  186. {
  187. return true;
  188. }
  189. void CarlifeLink::status_callback_func(int status, void* parameter)
  190. {
  191. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  192. CarlifeLink *pthis = (CarlifeLink*)parameter;
  193. if(CMD_FAILED == status){
  194. onSdkConnectStatus(CONNECT_STATUS_CONNECT_FAILED, UnKnown);
  195. }
  196. else if(CMD_SUCCESS == status){
  197. m_pCarlifePlayer->GetCarScreenSize(mVideoFrame.dst_width, mVideoFrame.dst_height);
  198. onSdkConnectStatus(CONNECT_STATUS_CONNECT_SUCCEED,mPhoneType);
  199. }
  200. else if(CMD_REMOVED == status){
  201. onSdkConnectStatus(CONNECT_STATUS_DISCONNECTED, UnKnown);
  202. }
  203. else if(CMD_RECORDER_INIT == status){
  204. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  205. AudioInfo info = {16000, 1, 16};
  206. record_start(info);
  207. }
  208. else if(CMD_RECORDER_UNINIT == status){
  209. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  210. record_stop();
  211. }
  212. else if(CMD_RECORD_START == status){
  213. record_pause(false);
  214. }
  215. else if(CMD_RECORD_END == status)
  216. {
  217. record_pause(true);
  218. }
  219. else if(CMD_OPEN_CARLIFE == status){
  220. app_status(APP_NOT_RUNNING, nullptr);
  221. }
  222. else if(CMD_BACKGROUND == status){
  223. app_status(APP_BACKGROUND, nullptr);
  224. }
  225. else if(CMD_FOREGROUND == status){
  226. app_status(APP_FOREGROUND, nullptr);
  227. }
  228. else if(status >= CMD_BT_CALL_INCOMMING){
  229. bt_call_status(status);
  230. }
  231. else if(status == CMD_CALL_PHONE){
  232. bt_call_action(CALL_TYPE_DAIL, nullptr, pthis->mPhoneNumber.c_str());
  233. }
  234. else if(status == CMD_CALL_PHONE_EXIT){
  235. bt_call_action(CALL_TYPE_HANG_UP, nullptr, pthis->mPhoneNumber.c_str());
  236. }
  237. }
  238. void CarlifeLink::bt_call_status(int status)
  239. {
  240. // call_action(status, nullptr, mPhoneNumber);
  241. }
  242. #define DUMP_REC_FILE 0
  243. #if DUMP_REC_FILE
  244. static FILE *pRecfile = NULL;
  245. #endif
  246. void CarlifeLink::record_audio_callback(unsigned char *data, int len)
  247. {
  248. #if DUMP_REC_FILE
  249. if (NULL == pRecfile) {
  250. pRecfile = fopen("/tmp/rec_in.pcm", "w");
  251. }
  252. if (pRecfile) {
  253. fwrite(data, 1, len, pRecfile);
  254. printf("rec len:%d\r\n ", len);
  255. }
  256. #endif
  257. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  258. CCarLifeLib::getInstance()->sendVRRecordData(data, len, 0);
  259. usleep(10000);
  260. //printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  261. }
  262. void CarlifeLink::send_screen_size(int width, int height)
  263. {
  264. if(m_pCarlifePlayer){
  265. printf("%s:%s:%d\r\n",__FILE__,__func__,__LINE__);
  266. m_pCarlifePlayer->GetCarScreenSize(width, height);
  267. }
  268. }
  269. void CarlifeLink::send_car_bluetooth(const string& name, const string& address, const string& pin)
  270. {
  271. m_pCarlifePlayer->SendCarBluetooth(name, address,pin);
  272. }
  273. bool CarlifeLink::open_page(AppPage appPage)
  274. {
  275. switch (appPage) {
  276. case APP_PAGE_NAVIGATION:
  277. m_pCarlifePlayer->Key(KEYCODE_Navi);
  278. break;
  279. case APP_PAGE_MAIN:
  280. m_pCarlifePlayer->Key(KEYCODE_Home);
  281. break;
  282. case APP_PAGE_MUSIC:
  283. m_pCarlifePlayer->Key(KEYCODE_Media);
  284. break;
  285. case APP_PAGE_VR:
  286. m_pCarlifePlayer->Key(KEYCODE_VR_Start);
  287. break;
  288. case APP_PAGE_NAVI_HOME:
  289. case APP_PAGE_4S_SHOP:
  290. case APP_PAGE_CAR_PARK:
  291. case APP_PAGE_NAVI_WORK:
  292. case APP_PAGE_NAVI_GAS_STATION:
  293. break;
  294. }
  295. return true;
  296. }
  297. void CarlifeLink::video_start_callback_func(bool start, int width, int height, void *parameter)
  298. {
  299. if(start)
  300. video_start(0, 0, width, height);
  301. else
  302. video_stop();
  303. }
  304. void CarlifeLink::video_callback_func(int width, int height, unsigned char* data, int length, void* parameter)
  305. {
  306. video_play(data, length);
  307. }
  308. void CarlifeLink::audio_start_callback_func(bool start, int type, int rate, int bit, int channel,void* parameter)
  309. {
  310. if(start)
  311. audio_start((AudioType)type, rate, bit, channel);
  312. else
  313. audio_stop((AudioType)type);
  314. }
  315. void CarlifeLink::audio_callback_func(int type, unsigned char* data, int length, void* parameter)
  316. {
  317. audio_play((AudioType)type, data, length);
  318. }
  319. void CarlifeLink::phone_number_callback_func(string number, void* parameter)
  320. {
  321. mPhoneNumber = number;
  322. }
  323. #endif