room.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. const _ = require('lodash');
  3. const meta = require('./.room.js');
  4. const Controller = require('egg').Controller;
  5. const { CrudController } = require('naf-framework-mongoose/lib/controller');
  6. const tencentcloud = require('tencentcloud-sdk-nodejs');
  7. class RoomController extends Controller {
  8. constructor(ctx) {
  9. super(ctx);
  10. this.service = this.ctx.service.room;
  11. }
  12. async findroomname() {
  13. const res = await this.service.findroomname(this.ctx.query);
  14. this.ctx.ok({ data: res });
  15. }
  16. async index() {
  17. const data = await this.service.query(this.ctx.query);
  18. this.ctx.ok({ ...data });
  19. }
  20. async startrecord() {
  21. const data = await this.service.startrecord(this.ctx.request.body);
  22. this.ctx.ok({ ...data });
  23. }
  24. async starttranscode() {
  25. // 调用录制接口
  26. // 取得公共参数头
  27. const SecretId = 'AKIDCAtWu3GhY9VbQzJgfOq4QVJq8AxWi3Sa';
  28. const SecretKey = '3sRknnwoOhlY4JH2Wjf6VeYCdnhKr88z';
  29. // 导入对应产品模块的client models。
  30. const TrtcClient = tencentcloud.trtc.v20190722.Client;
  31. const models = tencentcloud.trtc.v20190722.Models;
  32. const Credential = tencentcloud.common.Credential;
  33. const ClientProfile = tencentcloud.common.ClientProfile;
  34. const HttpProfile = tencentcloud.common.HttpProfile;
  35. const cred = new Credential(SecretId, SecretKey);
  36. const httpProfile = new HttpProfile();
  37. httpProfile.endpoint = 'trtc.tencentcloudapi.com';
  38. const clientProfile = new ClientProfile();
  39. clientProfile.httpProfile = httpProfile;
  40. const client = new TrtcClient(cred, 'ap-shanghai', clientProfile);
  41. const req = new models.StartMCUMixTranscodeRequest();
  42. const params = {};
  43. params.SdkAppId = this.app.config.sdkappid;
  44. params.RoomId = this.ctx.query.roomname;
  45. const OutputParams_ = {};
  46. OutputParams_.StreamId = this.app.config.sdkappid + '_' + this.ctx.query.roomid + '_mix';
  47. OutputParams_.PureAudioStream = 0;
  48. OutputParams_.RecordId = this.app.config.sdkappid + '_' + this.ctx.query.roomid;
  49. OutputParams_.RecordAudioOnly = 0;
  50. params.OutputParams = OutputParams_;
  51. const EncodeParams_ = {};
  52. EncodeParams_.VideoWidth = 1280;
  53. EncodeParams_.VideoHeight = 720;
  54. EncodeParams_.VideoBitrate = 1560;
  55. EncodeParams_.VideoFramerate = 15;
  56. EncodeParams_.VideoGop = 2;
  57. EncodeParams_.BackgroundColor = 0;
  58. EncodeParams_.AudioSampleRate = 48000;
  59. EncodeParams_.AudioBitrate = 64;
  60. EncodeParams_.AudioChannels = 2;
  61. params.EncodeParams = EncodeParams_;
  62. const LayoutParams_ = {};
  63. LayoutParams_.Template = 2;
  64. LayoutParams_.MainVideoUserId = 'main_pc';
  65. LayoutParams_.MainVideoStreamType = 1;
  66. params.LayoutParams = LayoutParams_;
  67. req.from_json_string(JSON.stringify(params));
  68. client.StartMCUMixTranscode(req, function(errMsg, response) {
  69. if (errMsg) {
  70. console.log(errMsg);
  71. return;
  72. }
  73. console.log(response.to_json_string());
  74. });
  75. this.ctx.ok({ });
  76. }
  77. }
  78. module.exports = CrudController(RoomController, meta);