video_player.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*************************************************************************/
  2. /* video_player.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "video_player.h"
  31. #include "scene/scene_string_names.h"
  32. #include "core/os/os.h"
  33. #include "servers/audio_server.h"
  34. int VideoPlayer::sp_get_channel_count() const {
  35. return playback->get_channels();
  36. }
  37. bool VideoPlayer::mix(AudioFrame *p_buffer, int p_frames) {
  38. // Check the amount resampler can really handle.
  39. // If it cannot, wait "wait_resampler_phase_limit" times.
  40. // This mechanism contributes to smoother pause/unpause operation.
  41. if (p_frames <= resampler.get_num_of_ready_frames() ||
  42. wait_resampler_limit <= wait_resampler) {
  43. wait_resampler = 0;
  44. return resampler.mix(p_buffer, p_frames);
  45. }
  46. wait_resampler++;
  47. return false;
  48. }
  49. // Called from main thread (eg VideoStreamPlaybackWebm::update)
  50. int VideoPlayer::_audio_mix_callback(void *p_udata, const float *p_data, int p_frames) {
  51. VideoPlayer *vp = (VideoPlayer *)p_udata;
  52. int todo = MIN(vp->resampler.get_writer_space(), p_frames);
  53. float *wb = vp->resampler.get_write_buffer();
  54. int c = vp->resampler.get_channel_count();
  55. for (int i = 0; i < todo * c; i++) {
  56. wb[i] = p_data[i];
  57. }
  58. vp->resampler.write(todo);
  59. return todo;
  60. }
  61. // Called from audio thread
  62. void VideoPlayer::_mix_audio() {
  63. if (!stream.is_valid()) {
  64. return;
  65. }
  66. if (!playback.is_valid() || !playback->is_playing() || playback->is_paused()) {
  67. return;
  68. }
  69. AudioFrame *buffer = mix_buffer.ptrw();
  70. int buffer_size = mix_buffer.size();
  71. // Resample
  72. if (!mix(buffer, buffer_size))
  73. return;
  74. AudioFrame vol = AudioFrame(volume, volume);
  75. int cc = AudioServer::get_singleton()->get_channel_count();
  76. if (cc == 1) {
  77. AudioFrame *target = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus_index, 0);
  78. ERR_FAIL_COND(!target);
  79. for (int j = 0; j < buffer_size; j++) {
  80. target[j] += buffer[j] * vol;
  81. }
  82. } else {
  83. AudioFrame *targets[4];
  84. for (int k = 0; k < cc; k++) {
  85. targets[k] = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus_index, k);
  86. ERR_FAIL_COND(!targets[k]);
  87. }
  88. for (int j = 0; j < buffer_size; j++) {
  89. AudioFrame frame = buffer[j] * vol;
  90. for (int k = 0; k < cc; k++) {
  91. targets[k][j] += frame;
  92. }
  93. }
  94. }
  95. }
  96. void VideoPlayer::_notification(int p_notification) {
  97. switch (p_notification) {
  98. case NOTIFICATION_ENTER_TREE: {
  99. AudioServer::get_singleton()->add_callback(_mix_audios, this);
  100. if (stream.is_valid() && autoplay && !Engine::get_singleton()->is_editor_hint()) {
  101. play();
  102. }
  103. } break;
  104. case NOTIFICATION_EXIT_TREE: {
  105. AudioServer::get_singleton()->remove_callback(_mix_audios, this);
  106. } break;
  107. case NOTIFICATION_INTERNAL_PROCESS: {
  108. bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus);
  109. if (stream.is_null() || paused || !playback->is_playing())
  110. return;
  111. double audio_time = USEC_TO_SEC(OS::get_singleton()->get_ticks_usec());
  112. double delta = last_audio_time == 0 ? 0 : audio_time - last_audio_time;
  113. last_audio_time = audio_time;
  114. if (delta == 0)
  115. return;
  116. playback->update(delta); // playback->is_playing() returns false in the last video frame
  117. if (!playback->is_playing()) {
  118. emit_signal(SceneStringNames::get_singleton()->finished);
  119. }
  120. } break;
  121. case NOTIFICATION_DRAW: {
  122. if (texture.is_null())
  123. return;
  124. if (texture->get_width() == 0)
  125. return;
  126. Size2 s = expand ? get_size() : texture->get_size();
  127. draw_texture_rect(texture, Rect2(Point2(), s), false);
  128. } break;
  129. };
  130. };
  131. Size2 VideoPlayer::get_minimum_size() const {
  132. if (!expand && !texture.is_null())
  133. return texture->get_size();
  134. else
  135. return Size2();
  136. }
  137. void VideoPlayer::set_expand(bool p_expand) {
  138. expand = p_expand;
  139. update();
  140. minimum_size_changed();
  141. }
  142. bool VideoPlayer::has_expand() const {
  143. return expand;
  144. }
  145. void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) {
  146. stop();
  147. AudioServer::get_singleton()->lock();
  148. mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size());
  149. AudioServer::get_singleton()->unlock();
  150. stream = p_stream;
  151. if (stream.is_valid()) {
  152. stream->set_audio_track(audio_track);
  153. playback = stream->instance_playback();
  154. } else {
  155. playback = Ref<VideoStreamPlayback>();
  156. }
  157. if (!playback.is_null()) {
  158. playback->set_loop(loops);
  159. playback->set_paused(paused);
  160. texture = playback->get_texture();
  161. const int channels = playback->get_channels();
  162. AudioServer::get_singleton()->lock();
  163. if (channels > 0)
  164. resampler.setup(channels, playback->get_mix_rate(), AudioServer::get_singleton()->get_mix_rate(), buffering_ms, 0);
  165. else
  166. resampler.clear();
  167. AudioServer::get_singleton()->unlock();
  168. if (channels > 0)
  169. playback->set_mix_callback(_audio_mix_callback, this);
  170. } else {
  171. texture.unref();
  172. AudioServer::get_singleton()->lock();
  173. resampler.clear();
  174. AudioServer::get_singleton()->unlock();
  175. }
  176. update();
  177. };
  178. Ref<VideoStream> VideoPlayer::get_stream() const {
  179. return stream;
  180. };
  181. void VideoPlayer::play() {
  182. ERR_FAIL_COND(!is_inside_tree());
  183. if (playback.is_null())
  184. return;
  185. playback->stop();
  186. playback->play();
  187. set_process_internal(true);
  188. // AudioServer::get_singleton()->stream_set_active(stream_rid,true);
  189. // AudioServer::get_singleton()->stream_set_volume_scale(stream_rid,volume);
  190. last_audio_time = 0;
  191. };
  192. void VideoPlayer::stop() {
  193. if (!is_inside_tree())
  194. return;
  195. if (playback.is_null())
  196. return;
  197. playback->stop();
  198. // AudioServer::get_singleton()->stream_set_active(stream_rid,false);
  199. resampler.flush();
  200. set_process_internal(false);
  201. last_audio_time = 0;
  202. };
  203. bool VideoPlayer::is_playing() const {
  204. if (playback.is_null())
  205. return false;
  206. return playback->is_playing();
  207. };
  208. void VideoPlayer::set_paused(bool p_paused) {
  209. paused = p_paused;
  210. if (playback.is_valid()) {
  211. playback->set_paused(p_paused);
  212. set_process_internal(!p_paused);
  213. };
  214. last_audio_time = 0;
  215. };
  216. bool VideoPlayer::is_paused() const {
  217. return paused;
  218. }
  219. void VideoPlayer::set_buffering_msec(int p_msec) {
  220. buffering_ms = p_msec;
  221. }
  222. int VideoPlayer::get_buffering_msec() const {
  223. return buffering_ms;
  224. }
  225. void VideoPlayer::set_audio_track(int p_track) {
  226. audio_track = p_track;
  227. }
  228. int VideoPlayer::get_audio_track() const {
  229. return audio_track;
  230. }
  231. void VideoPlayer::set_volume(float p_vol) {
  232. volume = p_vol;
  233. };
  234. float VideoPlayer::get_volume() const {
  235. return volume;
  236. };
  237. void VideoPlayer::set_volume_db(float p_db) {
  238. if (p_db < -79)
  239. set_volume(0);
  240. else
  241. set_volume(Math::db2linear(p_db));
  242. };
  243. float VideoPlayer::get_volume_db() const {
  244. if (volume == 0)
  245. return -80;
  246. else
  247. return Math::linear2db(volume);
  248. };
  249. String VideoPlayer::get_stream_name() const {
  250. if (stream.is_null())
  251. return "<No Stream>";
  252. return stream->get_name();
  253. };
  254. float VideoPlayer::get_stream_position() const {
  255. if (playback.is_null())
  256. return 0;
  257. return playback->get_playback_position();
  258. };
  259. void VideoPlayer::set_stream_position(float p_position) {
  260. if (playback.is_valid())
  261. playback->seek(p_position);
  262. }
  263. Ref<Texture> VideoPlayer::get_video_texture() {
  264. if (playback.is_valid())
  265. return playback->get_texture();
  266. return Ref<Texture>();
  267. }
  268. void VideoPlayer::set_autoplay(bool p_enable) {
  269. autoplay = p_enable;
  270. };
  271. bool VideoPlayer::has_autoplay() const {
  272. return autoplay;
  273. };
  274. void VideoPlayer::set_bus(const StringName &p_bus) {
  275. //if audio is active, must lock this
  276. AudioServer::get_singleton()->lock();
  277. bus = p_bus;
  278. AudioServer::get_singleton()->unlock();
  279. }
  280. StringName VideoPlayer::get_bus() const {
  281. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  282. if (AudioServer::get_singleton()->get_bus_name(i) == bus) {
  283. return bus;
  284. }
  285. }
  286. return "Master";
  287. }
  288. void VideoPlayer::_validate_property(PropertyInfo &property) const {
  289. if (property.name == "bus") {
  290. String options;
  291. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  292. if (i > 0)
  293. options += ",";
  294. String name = AudioServer::get_singleton()->get_bus_name(i);
  295. options += name;
  296. }
  297. property.hint_string = options;
  298. }
  299. }
  300. void VideoPlayer::_bind_methods() {
  301. ClassDB::bind_method(D_METHOD("set_stream", "stream"), &VideoPlayer::set_stream);
  302. ClassDB::bind_method(D_METHOD("get_stream"), &VideoPlayer::get_stream);
  303. ClassDB::bind_method(D_METHOD("play"), &VideoPlayer::play);
  304. ClassDB::bind_method(D_METHOD("stop"), &VideoPlayer::stop);
  305. ClassDB::bind_method(D_METHOD("is_playing"), &VideoPlayer::is_playing);
  306. ClassDB::bind_method(D_METHOD("set_paused", "paused"), &VideoPlayer::set_paused);
  307. ClassDB::bind_method(D_METHOD("is_paused"), &VideoPlayer::is_paused);
  308. ClassDB::bind_method(D_METHOD("set_volume", "volume"), &VideoPlayer::set_volume);
  309. ClassDB::bind_method(D_METHOD("get_volume"), &VideoPlayer::get_volume);
  310. ClassDB::bind_method(D_METHOD("set_volume_db", "db"), &VideoPlayer::set_volume_db);
  311. ClassDB::bind_method(D_METHOD("get_volume_db"), &VideoPlayer::get_volume_db);
  312. ClassDB::bind_method(D_METHOD("set_audio_track", "track"), &VideoPlayer::set_audio_track);
  313. ClassDB::bind_method(D_METHOD("get_audio_track"), &VideoPlayer::get_audio_track);
  314. ClassDB::bind_method(D_METHOD("get_stream_name"), &VideoPlayer::get_stream_name);
  315. ClassDB::bind_method(D_METHOD("set_stream_position", "position"), &VideoPlayer::set_stream_position);
  316. ClassDB::bind_method(D_METHOD("get_stream_position"), &VideoPlayer::get_stream_position);
  317. ClassDB::bind_method(D_METHOD("set_autoplay", "enabled"), &VideoPlayer::set_autoplay);
  318. ClassDB::bind_method(D_METHOD("has_autoplay"), &VideoPlayer::has_autoplay);
  319. ClassDB::bind_method(D_METHOD("set_expand", "enable"), &VideoPlayer::set_expand);
  320. ClassDB::bind_method(D_METHOD("has_expand"), &VideoPlayer::has_expand);
  321. ClassDB::bind_method(D_METHOD("set_buffering_msec", "msec"), &VideoPlayer::set_buffering_msec);
  322. ClassDB::bind_method(D_METHOD("get_buffering_msec"), &VideoPlayer::get_buffering_msec);
  323. ClassDB::bind_method(D_METHOD("set_bus", "bus"), &VideoPlayer::set_bus);
  324. ClassDB::bind_method(D_METHOD("get_bus"), &VideoPlayer::get_bus);
  325. ClassDB::bind_method(D_METHOD("get_video_texture"), &VideoPlayer::get_video_texture);
  326. ADD_SIGNAL(MethodInfo("finished"));
  327. ADD_PROPERTY(PropertyInfo(Variant::INT, "audio_track", PROPERTY_HINT_RANGE, "0,128,1"), "set_audio_track", "get_audio_track");
  328. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "VideoStream"), "set_stream", "get_stream");
  329. //ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/loop"), "set_loop", "has_loop") ;
  330. ADD_PROPERTY(PropertyInfo(Variant::REAL, "volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01"), "set_volume_db", "get_volume_db");
  331. ADD_PROPERTY(PropertyInfo(Variant::REAL, "volume", PROPERTY_HINT_EXP_RANGE, "0,15,0.01", 0), "set_volume", "get_volume");
  332. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "has_autoplay");
  333. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_paused", "is_paused");
  334. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand");
  335. ADD_PROPERTY(PropertyInfo(Variant::INT, "buffering_msec", PROPERTY_HINT_RANGE, "10,1000"), "set_buffering_msec", "get_buffering_msec");
  336. ADD_PROPERTY(PropertyInfo(Variant::REAL, "stream_position", PROPERTY_HINT_RANGE, "0,1280000,0.1", 0), "set_stream_position", "get_stream_position");
  337. ADD_PROPERTY(PropertyInfo(Variant::STRING, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
  338. }
  339. VideoPlayer::VideoPlayer() {
  340. volume = 1;
  341. loops = false;
  342. paused = false;
  343. autoplay = false;
  344. expand = true;
  345. audio_track = 0;
  346. bus_index = 0;
  347. buffering_ms = 500;
  348. // internal_stream.player=this;
  349. // stream_rid=AudioServer::get_singleton()->audio_stream_create(&internal_stream);
  350. last_audio_time = 0;
  351. wait_resampler = 0;
  352. wait_resampler_limit = 2;
  353. };
  354. VideoPlayer::~VideoPlayer() {
  355. // if (stream_rid.is_valid())
  356. // AudioServer::get_singleton()->free(stream_rid);
  357. resampler.clear(); //Not necessary here, but make in consistent with other "stream_player" classes
  358. };