video_player.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*************************************************************************/
  2. /* video_player.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "video_player.h"
  30. void VideoPlayer::_notification(int p_notification) {
  31. switch (p_notification) {
  32. case NOTIFICATION_ENTER_SCENE: {
  33. //set_idle_process(false); //don't annoy
  34. if (stream.is_valid() && autoplay && !get_scene()->is_editor_hint())
  35. play();
  36. } break;
  37. case NOTIFICATION_PROCESS: {
  38. if (stream.is_null())
  39. return;
  40. if (paused)
  41. return;
  42. while (stream->get_pending_frame_count()) {
  43. Image img = stream->pop_frame();
  44. if (texture->get_width() == 0) {
  45. texture->create(img.get_width(),img.get_height(),img.get_format(),Texture::FLAG_VIDEO_SURFACE|Texture::FLAG_FILTER);
  46. update();
  47. minimum_size_changed();
  48. } else {
  49. if (stream->get_pending_frame_count() == 0)
  50. texture->set_data(img);
  51. };
  52. };
  53. } break;
  54. case NOTIFICATION_DRAW: {
  55. if (texture.is_null())
  56. return;
  57. if (texture->get_width() == 0)
  58. return;
  59. Size2 s=expand?get_size():texture->get_size();
  60. RID ci = get_canvas_item();
  61. printf("drawing with size %f, %f\n", s.x, s.y);
  62. draw_texture_rect(texture,Rect2(Point2(),s),false);
  63. } break;
  64. };
  65. };
  66. Size2 VideoPlayer::get_minimum_size() const {
  67. if (!expand && !texture.is_null())
  68. return texture->get_size();
  69. else
  70. return Size2();
  71. }
  72. void VideoPlayer::set_expand(bool p_expand) {
  73. expand=p_expand;
  74. update();
  75. minimum_size_changed();
  76. }
  77. bool VideoPlayer::has_expand() const {
  78. return expand;
  79. }
  80. void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) {
  81. stop();
  82. if (stream_rid.is_valid())
  83. AudioServer::get_singleton()->free(stream_rid);
  84. stream_rid=RID();
  85. texture = Ref<ImageTexture>(memnew(ImageTexture));
  86. stream=p_stream;
  87. if (!stream.is_null()) {
  88. stream->set_loop(loops);
  89. stream->set_paused(paused);
  90. stream_rid=AudioServer::get_singleton()->audio_stream_create(stream->get_audio_stream());
  91. }
  92. };
  93. Ref<VideoStream> VideoPlayer::get_stream() const {
  94. return stream;
  95. };
  96. void VideoPlayer::play() {
  97. ERR_FAIL_COND(!is_inside_scene());
  98. if (stream.is_null())
  99. return;
  100. stream->play();
  101. AudioServer::get_singleton()->stream_set_active(stream_rid,true);
  102. AudioServer::get_singleton()->stream_set_volume_scale(stream_rid,volume);
  103. set_process(true);
  104. };
  105. void VideoPlayer::stop() {
  106. if (!is_inside_scene())
  107. return;
  108. if (stream.is_null())
  109. return;
  110. AudioServer::get_singleton()->stream_set_active(stream_rid,false);
  111. stream->stop();
  112. set_process(false);
  113. };
  114. bool VideoPlayer::is_playing() const {
  115. if (stream.is_null())
  116. return false;
  117. return stream->is_playing();
  118. };
  119. void VideoPlayer::set_paused(bool p_paused) {
  120. paused=p_paused;
  121. if (stream.is_valid()) {
  122. stream->set_paused(p_paused);
  123. set_process(!p_paused);
  124. };
  125. };
  126. bool VideoPlayer::is_paused() const {
  127. return paused;
  128. };
  129. void VideoPlayer::set_volume(float p_vol) {
  130. volume=p_vol;
  131. if (stream_rid.is_valid())
  132. AudioServer::get_singleton()->stream_set_volume_scale(stream_rid,volume);
  133. };
  134. float VideoPlayer::get_volume() const {
  135. return volume;
  136. };
  137. void VideoPlayer::set_volume_db(float p_db) {
  138. if (p_db<-79)
  139. set_volume(0);
  140. else
  141. set_volume(Math::db2linear(p_db));
  142. };
  143. float VideoPlayer::get_volume_db() const {
  144. if (volume==0)
  145. return -80;
  146. else
  147. return Math::linear2db(volume);
  148. };
  149. String VideoPlayer::get_stream_name() const {
  150. if (stream.is_null())
  151. return "<No Stream>";
  152. return stream->get_name();
  153. };
  154. float VideoPlayer::get_pos() const {
  155. if (stream.is_null())
  156. return 0;
  157. return stream->get_pos();
  158. };
  159. void VideoPlayer::set_autoplay(bool p_enable) {
  160. autoplay=p_enable;
  161. };
  162. bool VideoPlayer::has_autoplay() const {
  163. return autoplay;
  164. };
  165. void VideoPlayer::_bind_methods() {
  166. ObjectTypeDB::bind_method(_MD("set_stream","stream:Stream"),&VideoPlayer::set_stream);
  167. ObjectTypeDB::bind_method(_MD("get_stream:Stream"),&VideoPlayer::get_stream);
  168. ObjectTypeDB::bind_method(_MD("play"),&VideoPlayer::play);
  169. ObjectTypeDB::bind_method(_MD("stop"),&VideoPlayer::stop);
  170. ObjectTypeDB::bind_method(_MD("is_playing"),&VideoPlayer::is_playing);
  171. ObjectTypeDB::bind_method(_MD("set_paused","paused"),&VideoPlayer::set_paused);
  172. ObjectTypeDB::bind_method(_MD("is_paused"),&VideoPlayer::is_paused);
  173. ObjectTypeDB::bind_method(_MD("set_volume","volume"),&VideoPlayer::set_volume);
  174. ObjectTypeDB::bind_method(_MD("get_volume"),&VideoPlayer::get_volume);
  175. ObjectTypeDB::bind_method(_MD("set_volume_db","db"),&VideoPlayer::set_volume_db);
  176. ObjectTypeDB::bind_method(_MD("get_volume_db"),&VideoPlayer::get_volume_db);
  177. ObjectTypeDB::bind_method(_MD("get_stream_name"),&VideoPlayer::get_stream_name);
  178. ObjectTypeDB::bind_method(_MD("get_pos"),&VideoPlayer::get_pos);
  179. ObjectTypeDB::bind_method(_MD("set_autoplay","enabled"),&VideoPlayer::set_autoplay);
  180. ObjectTypeDB::bind_method(_MD("has_autoplay"),&VideoPlayer::has_autoplay);
  181. ObjectTypeDB::bind_method(_MD("set_expand","enable"), &VideoPlayer::set_expand );
  182. ObjectTypeDB::bind_method(_MD("has_expand"), &VideoPlayer::has_expand );
  183. ADD_PROPERTY( PropertyInfo(Variant::OBJECT, "stream/stream", PROPERTY_HINT_RESOURCE_TYPE,"AudioStream"), _SCS("set_stream"), _SCS("get_stream") );
  184. // ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/loop"), _SCS("set_loop"), _SCS("has_loop") );
  185. ADD_PROPERTY( PropertyInfo(Variant::REAL, "stream/volume_db", PROPERTY_HINT_RANGE,"-80,24,0.01"), _SCS("set_volume_db"), _SCS("get_volume_db") );
  186. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/autoplay"), _SCS("set_autoplay"), _SCS("has_autoplay") );
  187. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/paused"), _SCS("set_paused"), _SCS("is_paused") );
  188. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "expand" ), _SCS("set_expand"),_SCS("has_expand") );
  189. }
  190. VideoPlayer::VideoPlayer() {
  191. volume=1;
  192. loops=false;
  193. paused=false;
  194. autoplay=false;
  195. expand = true;
  196. loops = false;
  197. };
  198. VideoPlayer::~VideoPlayer() {
  199. if (stream_rid.is_valid())
  200. AudioServer::get_singleton()->free(stream_rid);
  201. };