room.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. async stoptranscode() {
  78. // 调用录制接口
  79. // 取得公共参数头
  80. const SecretId = 'AKIDCAtWu3GhY9VbQzJgfOq4QVJq8AxWi3Sa';
  81. const SecretKey = '3sRknnwoOhlY4JH2Wjf6VeYCdnhKr88z';
  82. // 导入对应产品模块的client models。
  83. const TrtcClient = tencentcloud.trtc.v20190722.Client;
  84. const models = tencentcloud.trtc.v20190722.Models;
  85. const Credential = tencentcloud.common.Credential;
  86. const ClientProfile = tencentcloud.common.ClientProfile;
  87. const HttpProfile = tencentcloud.common.HttpProfile;
  88. const cred = new Credential(SecretId, SecretKey);
  89. const httpProfile = new HttpProfile();
  90. httpProfile.endpoint = 'trtc.tencentcloudapi.com';
  91. const clientProfile = new ClientProfile();
  92. clientProfile.httpProfile = httpProfile;
  93. const client = new TrtcClient(cred, 'ap-shanghai', clientProfile);
  94. const req = new models.StopMCUMixTranscodeRequest();
  95. const params = {};
  96. params.SdkAppId = this.app.config.sdkappid;
  97. params.RoomId = this.ctx.query.roomname;
  98. req.from_json_string(JSON.stringify(params));
  99. client.StartMCUMixTranscode(req, function(errMsg, response) {
  100. if (errMsg) {
  101. console.log(errMsg);
  102. return;
  103. }
  104. console.log(response.to_json_string());
  105. });
  106. this.ctx.ok({ });
  107. }
  108. async deletefile() {
  109. // 调用录制接口
  110. // 取得公共参数头
  111. const SecretId = 'AKIDCAtWu3GhY9VbQzJgfOq4QVJq8AxWi3Sa';
  112. const SecretKey = '3sRknnwoOhlY4JH2Wjf6VeYCdnhKr88z';
  113. // 导入对应产品模块的client models。
  114. const VodClient = tencentcloud.vod.v20180717.Client;
  115. const models = tencentcloud.vod.v20180717.Models;
  116. const Credential = tencentcloud.common.Credential;
  117. const ClientProfile = tencentcloud.common.ClientProfile;
  118. const HttpProfile = tencentcloud.common.HttpProfile;
  119. const cred = new Credential(SecretId, SecretKey);
  120. const httpProfile = new HttpProfile();
  121. httpProfile.endpoint = 'vod.tencentcloudapi.com';
  122. const clientProfile = new ClientProfile();
  123. clientProfile.httpProfile = httpProfile;
  124. const client = new VodClient(cred, 'ap-shanghai', clientProfile);
  125. const req = new models.DeleteMediaRequest();
  126. const params = { FileId: this.ctx.query.fileid };
  127. req.from_json_string(JSON.stringify(params));
  128. client.StartMCUMixTranscode(req, function(errMsg, response) {
  129. if (errMsg) {
  130. console.log(errMsg);
  131. return;
  132. }
  133. console.log(response.to_json_string());
  134. });
  135. this.ctx.ok({ });
  136. }
  137. }
  138. module.exports = CrudController(RoomController, meta);