live.service.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. import { Injectable } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { AlertController } from '@ionic/angular';
  4. import * as Parse from 'parse';
  5. import { AiChatService } from './aichart.service';
  6. import { HttpService } from './http.service';
  7. import { MessageService } from './message.service';
  8. declare const AgoraRTC: any;
  9. import BeautyExtension from 'agora-extension-beauty-effect'; // 引入美颜扩展
  10. @Injectable({
  11. providedIn: 'root',
  12. })
  13. export class LiveService {
  14. isAnchor: boolean = false; //是否是主播
  15. options: {
  16. appid: string;
  17. channel: string;
  18. token: string;
  19. } = {
  20. appid: '',
  21. channel: '',
  22. token: '',
  23. };
  24. localTracks: any = {
  25. audioTrack: null,
  26. videoTrack: null,
  27. };
  28. rid?: string; //房间id(channel)
  29. profile?: any = localStorage.getItem('profile');
  30. client: any; //客户端
  31. company: string = '';
  32. UID: any;
  33. tools: any = {
  34. audio: false, //是否关闭音频
  35. camera: false, //是否切换摄像头
  36. glorify: false, //是否开启美颜
  37. mute: false, //是否静音
  38. };
  39. user_published_list: Array<any> = []; //已订阅频道用户列表
  40. timer: any; //轮询获取频道token的定时
  41. alert: any; //提示框
  42. media_devices: {
  43. //音视频设备列表
  44. audioDevices: Array<any>;
  45. videoDevices: Array<any>;
  46. } = {
  47. audioDevices: [],
  48. videoDevices: [],
  49. };
  50. currentUsedDevice: { audioDevice: string; videoDevice: string } = {
  51. //当前使用的设备
  52. audioDevice: '',
  53. videoDevice: '',
  54. };
  55. room?: Parse.Object; //直播间
  56. connection_state?: string; //连接状态
  57. surplusNumber: number = 0; //剩余可通话时长(单位:秒s)
  58. countdown: number = 0; // 新增倒计时变量
  59. timer_countdown: any;
  60. liveLog?: Parse.Object; //直播记录
  61. isOpenEvaluate: boolean = false; //是否开启评价
  62. isBeautyExtensionRegistered: boolean = false; //是否注册美颜扩展
  63. processor: any; //音视频处理
  64. constructor(
  65. private http: HttpService,
  66. private router: Router,
  67. private aiServ: AiChatService,
  68. private alertController: AlertController,
  69. private msgSer: MessageService
  70. ) {
  71. this.client?.leave();
  72. this.company = this.aiServ.company;
  73. this.getProfile();
  74. }
  75. async getProfile() {
  76. if (this.profile) return;
  77. let queryProfile = new Parse.Query('Profile');
  78. queryProfile.equalTo('user', Parse.User.current()?.id);
  79. queryProfile.notEqualTo('isDeleted', true);
  80. queryProfile.equalTo('isCross', true);
  81. this.profile = await queryProfile.first();
  82. this.profile?.id &&
  83. localStorage.setItem('profile', JSON.stringify(this.profile.toJSON()));
  84. }
  85. /* 初始化Agora */
  86. initAgora() {
  87. this.tools = {
  88. audio: false, //是否关闭音频
  89. camera: false, //是否切换摄像头
  90. glorify: false, //是否开启美颜
  91. mute: false, //是否静音
  92. };
  93. this.timer && clearTimeout(this.timer);
  94. this.timer_countdown && clearInterval(this.timer_countdown);
  95. this.timer_countdown = null;
  96. this.options['token'] = '';
  97. this.options['channel'] = '';
  98. this.user_published_list = [];
  99. this.client?.leave();
  100. this.client = AgoraRTC.createClient({ mode: 'rtc', codec: 'h264' });
  101. AgoraRTC.enableLogUpload();
  102. if (!this.isBeautyExtensionRegistered) {
  103. // 创建 BeautyExtension 实例
  104. const extension: any = new BeautyExtension();
  105. // 注册插件
  106. AgoraRTC.registerExtensions([extension]);
  107. // 创建 BeautyProcessor 实例
  108. this.processor = extension.createProcessor();
  109. }
  110. this.setBeautyOptions();
  111. }
  112. // 获取所有音视频设备
  113. getDevices() {
  114. AgoraRTC.getDevices()
  115. .then((devices: any) => {
  116. this.media_devices.audioDevices = devices.filter(function (
  117. device: any
  118. ) {
  119. return device.kind === 'audioinput';
  120. });
  121. this.media_devices.videoDevices = devices.filter(function (
  122. device: any
  123. ) {
  124. return device.kind === 'videoinput';
  125. });
  126. console.log(this.media_devices);
  127. this.currentUsedDevice = {
  128. audioDevice: this.media_devices.audioDevices[0].deviceId,
  129. videoDevice: this.media_devices.videoDevices[0].deviceId,
  130. };
  131. })
  132. .then((tracks: any) => {
  133. console.log(tracks);
  134. })
  135. .catch((err: any) => {
  136. console.warn('获取媒体权限失败', err);
  137. this.alertTips('获取媒体权限失败');
  138. });
  139. }
  140. /* 获取token */
  141. async getToken(room: Parse.Object) {
  142. this.room = room;
  143. this.isAnchor = room?.get('user')?.id === Parse.User.current()?.id;
  144. this.timer && clearTimeout(this.timer);
  145. let remoteEle = document.getElementById('vice-video');
  146. (remoteEle as any).style.display = 'none';
  147. //获取频道token记录
  148. let baseurl = 'https://server.fmode.cn/api/ailiao/token';
  149. if (this.isAnchor) {
  150. this.UID = 111111;
  151. baseurl = 'https://server.fmode.cn/api/webrtc/build_token';
  152. }
  153. let reqBody = {
  154. company: this.company, // this.aiSer.company,
  155. profile: room?.get('profile').id,
  156. channelName: room?.get('profile').id,
  157. };
  158. let data: any = await this.http.httpRequst(baseurl, reqBody, 'POST');
  159. console.log(data);
  160. if (data.code == 200) {
  161. this.options.token = data.data.token;
  162. this.options.appid = data.data.appid;
  163. } else {
  164. this.timer = setTimeout(() => {
  165. this.getToken(room);
  166. }, 2000);
  167. return;
  168. }
  169. this.options.channel = room?.get('profile').id;
  170. this.join();
  171. }
  172. async updateToken(pid: string) {
  173. let sql = `select "rtc"."objectId" as "rid","rtc"."channel", "rtc"."token", "rtc"."expiraTime" from "RtcToken" as "rtc" where "rtc"."profile" = '${pid}' and "rtc"."expiraTime" >= now() order by "createdAt" desc limit 1`;
  174. let tokenData: any = await this.http.customSQL(sql);
  175. if (
  176. tokenData &&
  177. tokenData.code == 200 &&
  178. tokenData.data &&
  179. tokenData.data.length > 0
  180. ) {
  181. return tokenData.data[0];
  182. } else {
  183. return null;
  184. }
  185. }
  186. /* 进入频道 */
  187. async join() {
  188. let path = location.pathname;
  189. if (path.indexOf('/live/link-room/') == -1) return;
  190. console.log('频道id:', this.options.channel);
  191. this.client
  192. .join(
  193. this.options.appid,
  194. this.options.channel,
  195. this.options.token,
  196. this.UID
  197. )
  198. .then(async (uid: any) => {
  199. // await this.client.setClientRole('host');
  200. this.connection_state = this.client.connectionState;
  201. console.log('进入频道当前uid:', uid, '状态:', this.connection_state);
  202. let user = Parse.User.current();
  203. this.msgSer?.publishMessage(
  204. `${user?.get('name') || user?.get('nickname')}加入频道直播`,
  205. this.room?.get('user')?.id
  206. );
  207. if (!this.isAnchor) {
  208. // 观众进入直播间,创建直播记录
  209. await this.createLiveLog(uid);
  210. } else {
  211. //主播进入直播间直接发送自己推流
  212. await this.publishSelf();
  213. }
  214. await this.joinReady();
  215. this.afterJoin();
  216. })
  217. .catch((err: any) => {
  218. console.error('进入频道失败:', err);
  219. });
  220. }
  221. /* 发布本地视频 */
  222. async publishSelf() {
  223. try {
  224. let data = await Promise.all([
  225. /* 创建音频和视频轨道 */
  226. AgoraRTC.createMicrophoneAudioTrack(this.currentUsedDevice.audioDevice),
  227. AgoraRTC.createCameraVideoTrack(this.currentUsedDevice.videoDevice),
  228. ]);
  229. this.localTracks.audioTrack = data[0];
  230. this.localTracks.videoTrack = data[1];
  231. // console.log(this.localTracks);
  232. let remoteEle = document.getElementById('vice-video');
  233. if (remoteEle) {
  234. remoteEle.textContent = '';
  235. remoteEle.style.display = 'block';
  236. }
  237. this.localTracks.videoTrack.play('vice-video'); //播放自己视频渲染
  238. // if (this.tools['audio']) {
  239. // this.localTracks.audioTrack.setEnabled(false);
  240. // } else {
  241. // this.localTracks.audioTrack.setEnabled(true);
  242. // }
  243. if (this.processor && this.localTracks.videoTrack) {
  244. // 将插件注入 SDK 内的视频处理管道
  245. this.localTracks.videoTrack
  246. .pipe(this.processor)
  247. .pipe(this.localTracks.videoTrack.processorDestination);
  248. // 开启美颜
  249. await this.processor.enable();
  250. }
  251. await this.client.publish(Object.values(this.localTracks));
  252. } catch (err) {
  253. console.log('发布本地视频失败:', err);
  254. // history.back()
  255. this.alertTips('发布本地视频失败,请检查摄像头是否授权或正常', '提示');
  256. this.client.leave();
  257. }
  258. }
  259. /* 设置各项美颜参数 */
  260. async setBeautyOptions() {
  261. this.processor.setOptions({
  262. lighteningContrastLevel: 2, // 对比度
  263. lighteningLevel: 0.8, // 亮度
  264. smoothnessLevel: 0.8, // 平滑度
  265. sharpnessLevel: 0.5, // 锐化程度
  266. rednessLevel: 0.5, // 红润度
  267. });
  268. this.tools['glorify'] = true;
  269. }
  270. /* 订阅远程视频 */
  271. async joinReady() {
  272. this.client.remoteUsers.forEach((user: any) => {
  273. // console.log('remoteUsers', user.uid);
  274. this.client.subscribe(user, 'audio').then((audioTrack: any) => {
  275. if (!this.user_published_list.find((item) => item.uid === user.uid)) {
  276. this.user_published_list.push(user);
  277. }
  278. audioTrack.setVolume(100);
  279. audioTrack.play();
  280. });
  281. this.client.subscribe(user, 'video').then((videoTrack: any) => {
  282. let remoteEle = document.getElementById('video');
  283. if (remoteEle) {
  284. remoteEle.textContent = '';
  285. }
  286. videoTrack.play('video');
  287. });
  288. });
  289. this.client.on('user-joined', (user: any) => {
  290. console.log(user, `${user.uid} 加入频道`);
  291. });
  292. this.client.on('user-published', async (user: any, mediaType: string) => {
  293. console.log('用户推流成功', user);
  294. await this.client.subscribe(user, mediaType);
  295. let remoteEle = document.getElementById('video');
  296. if (remoteEle) {
  297. remoteEle.textContent = '';
  298. }
  299. if (mediaType === 'video') {
  300. user.videoTrack.play('video');
  301. this.alert?.dismiss();
  302. }
  303. if (mediaType === 'audio') {
  304. user.audioTrack.play();
  305. if (!this.user_published_list.find((item) => item.uid === user.uid)) {
  306. this.user_published_list.push(user);
  307. }
  308. }
  309. });
  310. this.client.on('user-unpublished', async (user: any, mediaType: string) => {
  311. if (mediaType === 'audio') {
  312. console.log('对方已静音');
  313. let idx = this.user_published_list.findIndex(
  314. (item) => item.uid === user.uid
  315. );
  316. if (idx >= 0) {
  317. this.user_published_list.splice(idx, 1);
  318. }
  319. }
  320. if (mediaType === 'video') {
  321. console.log('用户取消推流');
  322. // let remoteEle = document.getElementById('video');
  323. // if (remoteEle) {
  324. // remoteEle.textContent = '对方已离开直播间';
  325. // }
  326. // // this.client.leave();
  327. // this.alertTips('对方已离开直播间');
  328. }
  329. });
  330. // 注册用户离开事件处理函数
  331. this.client.on('user-left', (user: any) => {
  332. console.log(`用户 ${user.uid} 离开了频道`);
  333. //主播离开,停止计时计费
  334. if (user.uid !== 100001) {
  335. this.client.leave();
  336. this.alertTips('对方已离开直播间');
  337. }
  338. });
  339. this.client.on(
  340. 'connection-state-change',
  341. (curState: any, prevState: any) => {
  342. this.connection_state = curState;
  343. if (
  344. curState == 'RECONNECTING' ||
  345. curState == 'DISCONNECTED' ||
  346. curState == 'DISCONNECTING'
  347. ) {
  348. this.timer && clearTimeout(this.timer);
  349. this.timer_countdown && clearInterval(this.timer_countdown);
  350. if (!this.isAnchor) {
  351. //用户离开直播间,断开与主播状态连接频道
  352. this.msgSer.unsubscribeMessage(this.room?.get('user')?.id);
  353. this.isOpenEvaluate = true;
  354. }
  355. console.log(this.router.url);
  356. if (this.router.url.indexOf('/live/link-room/') == 0 && curState == 'DISCONNECTED'){
  357. localStorage.setItem('isLive', null);
  358. history.back();
  359. }
  360. }
  361. console.log('live状态变更:', this.connection_state);
  362. }
  363. );
  364. this.monitorDevices();
  365. }
  366. async createLiveLog(uid: string) {
  367. let profile = JSON.parse(localStorage.getItem('profile') || '{}');
  368. let obj = Parse.Object.extend('LiveLog');
  369. let liveLog = new obj();
  370. liveLog.set('title', `与${profile?.name || profile?.mobile}进行直播`);
  371. liveLog.set('uid', String(uid));
  372. liveLog.set('company', {
  373. __type: 'Pointer',
  374. className: 'Company',
  375. objectId: this.company,
  376. });
  377. liveLog.set('room', {
  378. __type: 'Pointer',
  379. className: 'Room',
  380. objectId: this.room?.id,
  381. });
  382. liveLog.set('user', {
  383. __type: 'Pointer',
  384. className: '_User',
  385. objectId: Parse.User.current()?.id,
  386. });
  387. liveLog.set('profile', {
  388. __type: 'Pointer',
  389. className: 'Profile',
  390. objectId: this.room?.get('profile')?.id,
  391. });
  392. this.liveLog = await liveLog.save();
  393. }
  394. /* 连线成功立即创建直播记录,同步获取剩余可通话时长,并且开始计时 */
  395. async afterJoin() {
  396. this.timer && clearTimeout(this.timer);
  397. this.timer_countdown && clearInterval(this.timer_countdown);
  398. const targetUser = this.client.remoteUsers?.find(
  399. (user: any) => user.uid !== 100001
  400. ); //排出超管
  401. console.log(targetUser);
  402. if (this.client.remoteUsers.length > 0 && targetUser) {
  403. if (this.isAnchor) {
  404. //如果是主播,进入获取remoteUsers.user获取livelog再获取对方剩余通话时长
  405. this.getLiveLog(targetUser.uid);
  406. } else {
  407. let query = new Parse.Query('LiveLog');
  408. query.equalTo('objectId', this.liveLog?.id);
  409. query.equalTo('isLive', true);
  410. query.select('objectId');
  411. const resultData = await query.first();
  412. if (resultData?.id) {
  413. //首次进入至少计时2分钟,不足2分钟按2分钟计算扣时
  414. await this.publishSelf();
  415. await this.get_duration();
  416. this.computeDuration(60000 * 2);
  417. } else {
  418. this.timer = setTimeout(() => {
  419. this.afterJoin();
  420. }, 1000);
  421. }
  422. }
  423. } else {
  424. this.timer = setTimeout(() => {
  425. this.afterJoin();
  426. }, 1000);
  427. }
  428. }
  429. async getLiveLog(tarUid: string) {
  430. // let uid = this.client.remoteUsers[0].uid;
  431. this.timer && clearTimeout(this.timer);
  432. let query = new Parse.Query('LiveLog');
  433. query.equalTo('uid', String(tarUid));
  434. query.equalTo('room', this.room?.id);
  435. query.notEqualTo('isDeleted', true);
  436. query.notEqualTo('isLive', true);
  437. query.descending('createdAt');
  438. this.liveLog = await query.first();
  439. if (this.liveLog?.id) {
  440. this.liveLog?.set('isLive', true);
  441. await this.liveLog?.save();
  442. await this.get_duration();
  443. this.getCallDuration();
  444. return;
  445. }
  446. this.timer = setTimeout(() => {
  447. this.getLiveLog(tarUid);
  448. }, 1000);
  449. }
  450. async get_duration() {
  451. // this.timer_countdown && clearInterval(this.timer_countdown);
  452. let url = 'https://server.fmode.cn/api/ailiao/remain_second';
  453. let params = {
  454. rid: this.room?.id,
  455. uid: this.liveLog?.get('user')?.id || this.liveLog?.get('user')?.objectId,
  456. };
  457. let data = await this.http.httpRequst(url, params, 'POST');
  458. console.log(data);
  459. this.surplusNumber = data.data ?? 0;
  460. this.countdown = this.surplusNumber; // 初始化倒计时
  461. let arr = ['CONNECTING', 'CONNECTED'];
  462. if (arr.includes(this.connection_state as any)) {
  463. if (this.countdown <= 120) {
  464. this.alertTips('剩余通话时间不足2分钟,请及时充值');
  465. }
  466. !this.timer_countdown && this.startCountdown();
  467. }
  468. }
  469. startCountdown() {
  470. this.timer_countdown = setInterval(() => {
  471. if (this.countdown > 0) {
  472. this.countdown--;
  473. console.log(this.countdown);
  474. } else {
  475. clearInterval(this.timer_countdown);
  476. // history.back()
  477. this.alertTips('通话时间结束');
  478. this.client.leave(); // 结束通话
  479. }
  480. }, 1000);
  481. }
  482. /* 开始计时 */
  483. async computeDuration(num?: number) {
  484. //每10秒保存一次数据
  485. this.timer && clearTimeout(this.timer);
  486. let p = JSON.parse(this.profile);
  487. // console.log(p);
  488. let url = 'https://server.fmode.cn/api/ailiao/count/duration';
  489. let params = {
  490. company: this.company,
  491. profile: p?.objectId,
  492. rid: this.room?.id,
  493. uid: Parse.User.current()?.id,
  494. duration: num ? num / 1000 : 10,
  495. logid: this.liveLog?.id,
  496. };
  497. let data = await this.http.httpRequst(url, params, 'POST');
  498. // console.log(data);
  499. this.timer = setTimeout(() => {
  500. this.computeDuration();
  501. }, num ?? 10000);
  502. }
  503. /* 主播每10s获取一次对方可通话时长 */
  504. getCallDuration() {
  505. this.timer && clearTimeout(this.timer);
  506. this.timer = setTimeout(async () => {
  507. await this.get_duration();
  508. let arr = ['CONNECTING', 'CONNECTED'];
  509. if (arr.includes(this.connection_state as any)) {
  510. this.getCallDuration();
  511. }
  512. }, 10000);
  513. }
  514. /* 监听音视频设备插拔 */
  515. monitorDevices() {
  516. AgoraRTC.onMicrophoneChanged = async (changedDevice: any) => {
  517. // 插入麦克风设备时,切换到新插入的设备
  518. if (changedDevice.state === 'ACTIVE') {
  519. this.localTracks.audioTrack.setDevice(changedDevice.device.deviceId);
  520. this.currentUsedDevice.audioDevice = changedDevice.device.deviceId;
  521. // 拔出设备为当前设备时,切换到一个已有的设备
  522. } else if (
  523. changedDevice.device.label ===
  524. this.localTracks.audioTrack.getTrackLabel()
  525. ) {
  526. const oldMicrophones = await AgoraRTC.getMicrophones();
  527. oldMicrophones[0] &&
  528. this.localTracks.audioTrack.setDevice(oldMicrophones[0].deviceId);
  529. this.currentUsedDevice.audioDevice = oldMicrophones[0].deviceId;
  530. }
  531. };
  532. AgoraRTC.onCameraChanged = async (changedDevice: any) => {
  533. // 插入相机设备时,切换到新插入的设备
  534. if (changedDevice.state === 'ACTIVE') {
  535. this.localTracks.videoTrack.setDevice(changedDevice.device.deviceId);
  536. this.currentUsedDevice.videoDevice = changedDevice.device.deviceId;
  537. // 拔出设备为当前设备时,切换到一个已有的设备
  538. } else if (
  539. changedDevice.device.label ===
  540. this.localTracks.videoTrack.getTrackLabel()
  541. ) {
  542. const oldCameras = await AgoraRTC.getCameras();
  543. oldCameras[0] &&
  544. this.localTracks.videoTrack.setDevice(oldCameras[0].deviceId);
  545. this.currentUsedDevice.videoDevice = oldCameras[0].deviceId;
  546. }
  547. };
  548. }
  549. /* 关开麦 */
  550. async updatePublishedAudioTrack() {
  551. this.tools['audio'] = !this.tools['audio'];
  552. if (this.tools['audio'] && this.localTracks.audioTrack) {
  553. // await this.localTracks.audioTrack.setEnabled(false);
  554. this.localTracks.audioTrack.setVolume(0);
  555. console.log('停止推送音频');
  556. } else {
  557. // await this.localTracks.audioTrack.setEnabled(true);
  558. this.localTracks.audioTrack.setVolume(100);
  559. console.log('恢复推送音频');
  560. }
  561. // await this.client.unpublish(this.localTracks.audioTrack);
  562. return true;
  563. }
  564. /* 静音 */
  565. async muteAudio() {
  566. this.tools['mute'] = !this.tools['mute'];
  567. let list = Array.from(this.user_published_list);
  568. for (let index = 0; index < list.length; index++) {
  569. const user = list[index];
  570. if (this.tools['mute']) {
  571. console.log('静音');
  572. await this.client.unsubscribe(user, 'audio');
  573. } else {
  574. await this.client.subscribe(user, 'audio').then((audioTrack: any) => {
  575. audioTrack.setVolume(100);
  576. audioTrack.play();
  577. });
  578. console.log('恢复声音');
  579. }
  580. }
  581. }
  582. /* 切换摄像头 */
  583. async changeCamera() {
  584. const oldCameras = await AgoraRTC.getCameras(true);
  585. // console.log('oldCameras:', oldCameras);
  586. let newCamers = oldCameras.find(
  587. (item: any) => item.deviceId !== this.currentUsedDevice.videoDevice
  588. );
  589. console.log(newCamers);
  590. newCamers &&
  591. this.localTracks.videoTrack
  592. .setDevice(newCamers.deviceId)
  593. .then(() => {
  594. this.tools['camera'] = !this.tools['camera'];
  595. this.currentUsedDevice.videoDevice = newCamers.deviceId;
  596. })
  597. .catch((err: any) => {
  598. console.log('set device error', err);
  599. this.tools['camera'] = !this.tools['camera'];
  600. this.currentUsedDevice.videoDevice = newCamers.deviceId;
  601. // this.alertTips('切换摄像头失败');
  602. });
  603. // await this.localTracks.videoTrack.setDevice({facingMode: this.tools['camera'] ? 'user' : 'environment'})
  604. // this.tools['camera'] = !this.tools['camera'];
  605. // this.currentUsedDevice.videoDevice = newCamers.deviceId;
  606. return true;
  607. }
  608. /* 切换美颜 */
  609. changeBeauty() {
  610. if (!this.tools['glorify']) {
  611. this.setBeautyOptions();
  612. } else {
  613. // 关闭美颜
  614. this.processor.setOptions({
  615. lighteningContrastLevel: 0,
  616. lighteningLevel: 0,
  617. smoothnessLevel: 0,
  618. sharpnessLevel: 0,
  619. rednessLevel: 0,
  620. });
  621. this.tools['glorify'] = false;
  622. }
  623. }
  624. /* 提示 */
  625. async alertTips(message: string, title?: string, callBack?: Function) {
  626. this.alert = await this.alertController.create({
  627. header: title || '提示',
  628. message: message,
  629. backdropDismiss: false,
  630. buttons: [
  631. {
  632. text: '确认',
  633. handler: () => {
  634. callBack && callBack();
  635. },
  636. },
  637. ],
  638. });
  639. await this.alert.present();
  640. }
  641. }