transmuxer.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  3. *
  4. * @author zheng qian <xqq@xqq.im>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. import EventEmitter from 'events';
  19. import work from 'webworkify-webpack';
  20. import Log from '../utils/logger.js';
  21. import LoggingControl from '../utils/logging-control.js';
  22. import TransmuxingController from './transmuxing-controller.js';
  23. import TransmuxingEvents from './transmuxing-events.js';
  24. import TransmuxingWorker from './transmuxing-worker.js';
  25. import MediaInfo from './media-info.js';
  26. class Transmuxer {
  27. constructor(mediaDataSource, config) {
  28. this.TAG = 'Transmuxer';
  29. this._emitter = new EventEmitter();
  30. if (config.enableWorker && typeof (Worker) !== 'undefined') {
  31. try {
  32. this._worker = work(require.resolve('./transmuxing-worker'));
  33. this._workerDestroying = false;
  34. this._worker.addEventListener('message', this._onWorkerMessage.bind(this));
  35. this._worker.postMessage({cmd: 'init', param: [mediaDataSource, config]});
  36. this.e = {
  37. onLoggingConfigChanged: this._onLoggingConfigChanged.bind(this)
  38. };
  39. LoggingControl.registerListener(this.e.onLoggingConfigChanged);
  40. this._worker.postMessage({cmd: 'logging_config', param: LoggingControl.getConfig()});
  41. } catch (error) {
  42. Log.e(this.TAG, 'Error while initialize transmuxing worker, fallback to inline transmuxing');
  43. this._worker = null;
  44. this._controller = new TransmuxingController(mediaDataSource, config);
  45. }
  46. } else {
  47. this._controller = new TransmuxingController(mediaDataSource, config);
  48. }
  49. if (this._controller) {
  50. let ctl = this._controller;
  51. ctl.on(TransmuxingEvents.IO_ERROR, this._onIOError.bind(this));
  52. ctl.on(TransmuxingEvents.DEMUX_ERROR, this._onDemuxError.bind(this));
  53. ctl.on(TransmuxingEvents.INIT_SEGMENT, this._onInitSegment.bind(this));
  54. ctl.on(TransmuxingEvents.MEDIA_SEGMENT, this._onMediaSegment.bind(this));
  55. ctl.on(TransmuxingEvents.LOADING_COMPLETE, this._onLoadingComplete.bind(this));
  56. ctl.on(TransmuxingEvents.RECOVERED_EARLY_EOF, this._onRecoveredEarlyEof.bind(this));
  57. ctl.on(TransmuxingEvents.MEDIA_INFO, this._onMediaInfo.bind(this));
  58. ctl.on(TransmuxingEvents.METADATA_ARRIVED, this._onMetaDataArrived.bind(this));
  59. ctl.on(TransmuxingEvents.SCRIPTDATA_ARRIVED, this._onScriptDataArrived.bind(this));
  60. ctl.on(TransmuxingEvents.STATISTICS_INFO, this._onStatisticsInfo.bind(this));
  61. ctl.on(TransmuxingEvents.RECOMMEND_SEEKPOINT, this._onRecommendSeekpoint.bind(this));
  62. }
  63. }
  64. destroy() {
  65. if (this._worker) {
  66. if (!this._workerDestroying) {
  67. this._workerDestroying = true;
  68. this._worker.postMessage({cmd: 'destroy'});
  69. LoggingControl.removeListener(this.e.onLoggingConfigChanged);
  70. this.e = null;
  71. }
  72. } else {
  73. this._controller.destroy();
  74. this._controller = null;
  75. }
  76. this._emitter.removeAllListeners();
  77. this._emitter = null;
  78. }
  79. on(event, listener) {
  80. this._emitter.addListener(event, listener);
  81. }
  82. off(event, listener) {
  83. this._emitter.removeListener(event, listener);
  84. }
  85. hasWorker() {
  86. return this._worker != null;
  87. }
  88. open() {
  89. if (this._worker) {
  90. this._worker.postMessage({cmd: 'start'});
  91. } else {
  92. this._controller.start();
  93. }
  94. }
  95. close() {
  96. if (this._worker) {
  97. this._worker.postMessage({cmd: 'stop'});
  98. } else {
  99. this._controller.stop();
  100. }
  101. }
  102. seek(milliseconds) {
  103. if (this._worker) {
  104. this._worker.postMessage({cmd: 'seek', param: milliseconds});
  105. } else {
  106. this._controller.seek(milliseconds);
  107. }
  108. }
  109. pause() {
  110. if (this._worker) {
  111. this._worker.postMessage({cmd: 'pause'});
  112. } else {
  113. this._controller.pause();
  114. }
  115. }
  116. resume() {
  117. if (this._worker) {
  118. this._worker.postMessage({cmd: 'resume'});
  119. } else {
  120. this._controller.resume();
  121. }
  122. }
  123. _onInitSegment(type, initSegment) {
  124. // do async invoke
  125. Promise.resolve().then(() => {
  126. this._emitter.emit(TransmuxingEvents.INIT_SEGMENT, type, initSegment);
  127. });
  128. }
  129. _onMediaSegment(type, mediaSegment) {
  130. Promise.resolve().then(() => {
  131. this._emitter.emit(TransmuxingEvents.MEDIA_SEGMENT, type, mediaSegment);
  132. });
  133. }
  134. _onLoadingComplete() {
  135. Promise.resolve().then(() => {
  136. this._emitter.emit(TransmuxingEvents.LOADING_COMPLETE);
  137. });
  138. }
  139. _onRecoveredEarlyEof() {
  140. Promise.resolve().then(() => {
  141. this._emitter.emit(TransmuxingEvents.RECOVERED_EARLY_EOF);
  142. });
  143. }
  144. _onMediaInfo(mediaInfo) {
  145. Promise.resolve().then(() => {
  146. this._emitter.emit(TransmuxingEvents.MEDIA_INFO, mediaInfo);
  147. });
  148. }
  149. _onMetaDataArrived(metadata) {
  150. Promise.resolve().then(() => {
  151. this._emitter.emit(TransmuxingEvents.METADATA_ARRIVED, metadata);
  152. });
  153. }
  154. _onScriptDataArrived(data) {
  155. Promise.resolve().then(() => {
  156. this._emitter.emit(TransmuxingEvents.SCRIPTDATA_ARRIVED, data);
  157. });
  158. }
  159. _onStatisticsInfo(statisticsInfo) {
  160. Promise.resolve().then(() => {
  161. this._emitter.emit(TransmuxingEvents.STATISTICS_INFO, statisticsInfo);
  162. });
  163. }
  164. _onIOError(type, info) {
  165. Promise.resolve().then(() => {
  166. this._emitter.emit(TransmuxingEvents.IO_ERROR, type, info);
  167. });
  168. }
  169. _onDemuxError(type, info) {
  170. Promise.resolve().then(() => {
  171. this._emitter.emit(TransmuxingEvents.DEMUX_ERROR, type, info);
  172. });
  173. }
  174. _onRecommendSeekpoint(milliseconds) {
  175. Promise.resolve().then(() => {
  176. this._emitter.emit(TransmuxingEvents.RECOMMEND_SEEKPOINT, milliseconds);
  177. });
  178. }
  179. _onLoggingConfigChanged(config) {
  180. if (this._worker) {
  181. this._worker.postMessage({cmd: 'logging_config', param: config});
  182. }
  183. }
  184. _onWorkerMessage(e) {
  185. let message = e.data;
  186. let data = message.data;
  187. if (message.msg === 'destroyed' || this._workerDestroying) {
  188. this._workerDestroying = false;
  189. this._worker.terminate();
  190. this._worker = null;
  191. return;
  192. }
  193. switch (message.msg) {
  194. case TransmuxingEvents.INIT_SEGMENT:
  195. case TransmuxingEvents.MEDIA_SEGMENT:
  196. this._emitter.emit(message.msg, data.type, data.data);
  197. break;
  198. case TransmuxingEvents.LOADING_COMPLETE:
  199. case TransmuxingEvents.RECOVERED_EARLY_EOF:
  200. this._emitter.emit(message.msg);
  201. break;
  202. case TransmuxingEvents.MEDIA_INFO:
  203. Object.setPrototypeOf(data, MediaInfo.prototype);
  204. this._emitter.emit(message.msg, data);
  205. break;
  206. case TransmuxingEvents.METADATA_ARRIVED:
  207. case TransmuxingEvents.SCRIPTDATA_ARRIVED:
  208. case TransmuxingEvents.STATISTICS_INFO:
  209. this._emitter.emit(message.msg, data);
  210. break;
  211. case TransmuxingEvents.IO_ERROR:
  212. case TransmuxingEvents.DEMUX_ERROR:
  213. this._emitter.emit(message.msg, data.type, data.info);
  214. break;
  215. case TransmuxingEvents.RECOMMEND_SEEKPOINT:
  216. this._emitter.emit(message.msg, data);
  217. break;
  218. case 'logcat_callback':
  219. Log.emitter.emit('log', data.type, data.logcat);
  220. break;
  221. default:
  222. break;
  223. }
  224. }
  225. }
  226. export default Transmuxer;