srs.publisher.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * the SrsPublisher object.
  3. * @param container the html container id.
  4. * @param width a float value specifies the width of publisher.
  5. * @param height a float value specifies the height of publisher.
  6. * @param private_object [optional] an object that used as private object,
  7. * for example, the logic chat object which owner this publisher.
  8. */
  9. function SrsPublisher(container, width, height, private_object) {
  10. if (!SrsPublisher.__id) {
  11. SrsPublisher.__id = 100;
  12. }
  13. if (!SrsPublisher.__publishers) {
  14. SrsPublisher.__publishers = [];
  15. }
  16. SrsPublisher.__publishers.push(this);
  17. this.private_object = private_object;
  18. this.container = container;
  19. this.width = width;
  20. this.height = height;
  21. this.id = SrsPublisher.__id++;
  22. this.callbackObj = null;
  23. // set the values when publish.
  24. this.url = null;
  25. this.vcodec = {};
  26. this.acodec = {};
  27. // callback set the following values.
  28. this.cameras = [];
  29. this.microphones = [];
  30. this.code = 0;
  31. // error code defines.
  32. this.errors = {
  33. "100": "无法获取指定的摄像头。", //error_camera_get
  34. "101": "无法获取指定的麦克风。", //error_microphone_get
  35. "102": "摄像头为禁用状态,推流时请允许flash访问摄像头。", //error_camera_muted
  36. "103": "服务器关闭了连接。", //error_connection_closed
  37. "104": "服务器连接失败。", //error_connection_failed
  38. "199": "未知错误。"
  39. };
  40. }
  41. /**
  42. * user can set some callback, then start the publisher.
  43. * callbacks:
  44. * on_publisher_ready(cameras, microphones):int, when srs publisher ready, user can publish.
  45. * on_publisher_error(code, desc):int, when srs publisher error, callback this method.
  46. * on_publisher_warn(code, desc):int, when srs publisher warn, callback this method.
  47. */
  48. SrsPublisher.prototype.start = function() {
  49. // embed the flash.
  50. var flashvars = {};
  51. flashvars.id = this.id;
  52. flashvars.width = this.width;
  53. flashvars.height = this.height;
  54. flashvars.on_publisher_ready = "__srs_on_publisher_ready";
  55. flashvars.on_publisher_error = "__srs_on_publisher_error";
  56. flashvars.on_publisher_warn = "__srs_on_publisher_warn";
  57. var params = {};
  58. params.wmode = "opaque";
  59. params.allowFullScreen = "true";
  60. params.allowScriptAccess = "always";
  61. var attributes = {};
  62. var self = this;
  63. swfobject.embedSWF(
  64. "srs_publisher/release/srs_publisher.swf?_version="+srs_get_version_code(),
  65. this.container,
  66. this.width, this.height,
  67. "11.1.0", "js/AdobeFlashPlayerInstall.swf",
  68. flashvars, params, attributes,
  69. function(callbackObj){
  70. self.callbackObj = callbackObj;
  71. }
  72. );
  73. return this;
  74. }
  75. /**
  76. * publish stream to server.
  77. * @param url a string indicates the rtmp url to publish.
  78. * @param vcodec an object contains the video codec info.
  79. * @param acodec an object contains the audio codec info.
  80. */
  81. SrsPublisher.prototype.publish = function(url, vcodec, acodec) {
  82. this.stop();
  83. SrsPublisher.__publishers.push(this);
  84. if (url) {
  85. this.url = url;
  86. }
  87. if (vcodec) {
  88. this.vcodec = vcodec;
  89. }
  90. if (acodec) {
  91. this.acodec = acodec;
  92. }
  93. this.callbackObj.ref.__publish(this.url, this.width, this.height, this.vcodec, this.acodec);
  94. }
  95. SrsPublisher.prototype.stop = function() {
  96. for (var i = 0; i < SrsPublisher.__publishers.length; i++) {
  97. var player = SrsPublisher.__publishers[i];
  98. if (player.id != this.id) {
  99. continue;
  100. }
  101. SrsPublisher.__publishers.splice(i, 1);
  102. break;
  103. }
  104. this.callbackObj.ref.__stop();
  105. }
  106. /**
  107. * when publisher ready.
  108. * @param cameras a string array contains the names of cameras.
  109. * @param microphones a string array contains the names of microphones.
  110. */
  111. SrsPublisher.prototype.on_publisher_ready = function(cameras, microphones) {
  112. }
  113. /**
  114. * when publisher error.
  115. * @code the error code.
  116. * @desc the error desc message.
  117. */
  118. SrsPublisher.prototype.on_publisher_error = function(code, desc) {
  119. throw new Error("publisher error. code=" + code + ", desc=" + desc);
  120. }
  121. SrsPublisher.prototype.on_publisher_warn = function(code, desc) {
  122. throw new Error("publisher warn. code=" + code + ", desc=" + desc);
  123. }
  124. function __srs_find_publisher(id) {
  125. for (var i = 0; i < SrsPublisher.__publishers.length; i++) {
  126. var publisher = SrsPublisher.__publishers[i];
  127. if (publisher.id != id) {
  128. continue;
  129. }
  130. return publisher;
  131. }
  132. throw new Error("publisher not found. id=" + id);
  133. }
  134. function __srs_on_publisher_ready(id, cameras, microphones) {
  135. var publisher = __srs_find_publisher(id);
  136. publisher.cameras = cameras;
  137. publisher.microphones = microphones;
  138. publisher.on_publisher_ready(cameras, microphones);
  139. }
  140. function __srs_on_publisher_error(id, code) {
  141. var publisher = __srs_find_publisher(id);
  142. publisher.code = code;
  143. publisher.on_publisher_error(code, publisher.errors[""+code]);
  144. }
  145. function __srs_on_publisher_warn(id, code) {
  146. var publisher = __srs_find_publisher(id);
  147. publisher.code = code;
  148. publisher.on_publisher_warn(code, publisher.errors[""+code]);
  149. }