video_stream_player.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /**************************************************************************/
  2. /* video_stream_player.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_stream_player.h"
  31. #include "core/os/os.h"
  32. #include "servers/audio_server.h"
  33. int VideoStreamPlayer::sp_get_channel_count() const {
  34. if (playback.is_null()) {
  35. return 0;
  36. }
  37. return playback->get_channels();
  38. }
  39. bool VideoStreamPlayer::mix(AudioFrame *p_buffer, int p_frames) {
  40. // Check the amount resampler can really handle.
  41. // If it cannot, wait "wait_resampler_phase_limit" times.
  42. // This mechanism contributes to smoother pause/unpause operation.
  43. if (p_frames <= resampler.get_num_of_ready_frames() ||
  44. wait_resampler_limit <= wait_resampler) {
  45. wait_resampler = 0;
  46. return resampler.mix(p_buffer, p_frames);
  47. }
  48. wait_resampler++;
  49. return false;
  50. }
  51. // Called from main thread (e.g. VideoStreamPlaybackTheora::update).
  52. int VideoStreamPlayer::_audio_mix_callback(void *p_udata, const float *p_data, int p_frames) {
  53. ERR_FAIL_NULL_V(p_udata, 0);
  54. ERR_FAIL_NULL_V(p_data, 0);
  55. VideoStreamPlayer *vp = static_cast<VideoStreamPlayer *>(p_udata);
  56. int todo = MIN(vp->resampler.get_writer_space(), p_frames);
  57. float *wb = vp->resampler.get_write_buffer();
  58. int c = vp->resampler.get_channel_count();
  59. for (int i = 0; i < todo * c; i++) {
  60. wb[i] = p_data[i];
  61. }
  62. vp->resampler.write(todo);
  63. return todo;
  64. }
  65. void VideoStreamPlayer::_mix_audios(void *p_self) {
  66. ERR_FAIL_NULL(p_self);
  67. static_cast<VideoStreamPlayer *>(p_self)->_mix_audio();
  68. }
  69. // Called from audio thread
  70. void VideoStreamPlayer::_mix_audio() {
  71. if (stream.is_null()) {
  72. return;
  73. }
  74. if (playback.is_null() || !playback->is_playing() || playback->is_paused()) {
  75. return;
  76. }
  77. AudioFrame *buffer = mix_buffer.ptrw();
  78. int buffer_size = mix_buffer.size();
  79. // Resample
  80. if (!mix(buffer, buffer_size)) {
  81. return;
  82. }
  83. AudioFrame vol = AudioFrame(volume, volume);
  84. int cc = AudioServer::get_singleton()->get_channel_count();
  85. if (cc == 1) {
  86. AudioFrame *target = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus_index, 0);
  87. ERR_FAIL_NULL(target);
  88. for (int j = 0; j < buffer_size; j++) {
  89. target[j] += buffer[j] * vol;
  90. }
  91. } else {
  92. AudioFrame *targets[4];
  93. for (int k = 0; k < cc; k++) {
  94. targets[k] = AudioServer::get_singleton()->thread_get_channel_mix_buffer(bus_index, k);
  95. ERR_FAIL_NULL(targets[k]);
  96. }
  97. for (int j = 0; j < buffer_size; j++) {
  98. AudioFrame frame = buffer[j] * vol;
  99. for (int k = 0; k < cc; k++) {
  100. targets[k][j] += frame;
  101. }
  102. }
  103. }
  104. }
  105. void VideoStreamPlayer::_notification(int p_notification) {
  106. switch (p_notification) {
  107. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  108. RID ae = get_accessibility_element();
  109. ERR_FAIL_COND(ae.is_null());
  110. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_VIDEO);
  111. } break;
  112. case NOTIFICATION_ENTER_TREE: {
  113. AudioServer::get_singleton()->add_mix_callback(_mix_audios, this);
  114. if (stream.is_valid() && autoplay && !Engine::get_singleton()->is_editor_hint()) {
  115. play();
  116. }
  117. } break;
  118. case NOTIFICATION_EXIT_TREE: {
  119. stop();
  120. AudioServer::get_singleton()->remove_mix_callback(_mix_audios, this);
  121. } break;
  122. case NOTIFICATION_INTERNAL_PROCESS: {
  123. bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus);
  124. if (stream.is_null() || paused || playback.is_null() || !playback->is_playing()) {
  125. return;
  126. }
  127. double delta = first_frame ? 0 : get_process_delta_time();
  128. first_frame = false;
  129. resampler.set_playback_speed(Engine::get_singleton()->get_time_scale() * speed_scale);
  130. playback->update(delta * speed_scale); // playback->is_playing() returns false in the last video frame
  131. if (!playback->is_playing()) {
  132. resampler.flush();
  133. if (loop) {
  134. play();
  135. return;
  136. }
  137. emit_signal(SceneStringName(finished));
  138. }
  139. } break;
  140. case NOTIFICATION_DRAW: {
  141. if (texture.is_null()) {
  142. return;
  143. }
  144. if (texture->get_width() == 0) {
  145. return;
  146. }
  147. Size2 s = expand ? get_size() : texture_size;
  148. draw_texture_rect(texture, Rect2(Point2(), s), false);
  149. } break;
  150. case NOTIFICATION_SUSPENDED:
  151. case NOTIFICATION_PAUSED: {
  152. if (is_playing() && !is_paused()) {
  153. paused_from_tree = true;
  154. if (playback.is_valid()) {
  155. playback->set_paused(true);
  156. set_process_internal(false);
  157. }
  158. }
  159. } break;
  160. case NOTIFICATION_UNSUSPENDED: {
  161. if (get_tree()->is_paused()) {
  162. break;
  163. }
  164. [[fallthrough]];
  165. }
  166. case NOTIFICATION_UNPAUSED: {
  167. if (paused_from_tree) {
  168. paused_from_tree = false;
  169. if (playback.is_valid()) {
  170. playback->set_paused(false);
  171. set_process_internal(true);
  172. }
  173. }
  174. } break;
  175. }
  176. }
  177. void VideoStreamPlayer::texture_changed(const Ref<Texture2D> &p_texture) {
  178. const Size2 new_texture_size = p_texture.is_valid() ? p_texture->get_size() : Size2();
  179. if (new_texture_size == texture_size) {
  180. return;
  181. }
  182. texture_size = new_texture_size;
  183. queue_redraw();
  184. if (!expand) {
  185. update_minimum_size();
  186. }
  187. }
  188. Size2 VideoStreamPlayer::get_minimum_size() const {
  189. if (!expand && texture.is_valid()) {
  190. return texture_size;
  191. } else {
  192. return Size2();
  193. }
  194. }
  195. void VideoStreamPlayer::set_expand(bool p_expand) {
  196. if (expand == p_expand) {
  197. return;
  198. }
  199. expand = p_expand;
  200. queue_redraw();
  201. update_minimum_size();
  202. }
  203. bool VideoStreamPlayer::has_expand() const {
  204. return expand;
  205. }
  206. void VideoStreamPlayer::set_loop(bool p_loop) {
  207. loop = p_loop;
  208. }
  209. bool VideoStreamPlayer::has_loop() const {
  210. return loop;
  211. }
  212. void VideoStreamPlayer::set_stream(const Ref<VideoStream> &p_stream) {
  213. stop();
  214. // Make sure to handle stream changes seamlessly, e.g. when done via
  215. // translation remapping.
  216. if (stream.is_valid()) {
  217. stream->disconnect_changed(callable_mp(this, &VideoStreamPlayer::set_stream));
  218. }
  219. AudioServer::get_singleton()->lock();
  220. mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size());
  221. stream = p_stream;
  222. if (stream.is_valid()) {
  223. stream->set_audio_track(audio_track);
  224. playback = stream->instantiate_playback();
  225. } else {
  226. playback = Ref<VideoStreamPlayback>();
  227. }
  228. AudioServer::get_singleton()->unlock();
  229. if (stream.is_valid()) {
  230. stream->connect_changed(callable_mp(this, &VideoStreamPlayer::set_stream).bind(stream));
  231. }
  232. if (texture.is_valid()) {
  233. texture->disconnect_changed(callable_mp(this, &VideoStreamPlayer::texture_changed));
  234. }
  235. if (playback.is_valid()) {
  236. playback->set_paused(paused);
  237. texture = playback->get_texture();
  238. if (texture.is_valid()) {
  239. texture_size = texture->get_size();
  240. texture->connect_changed(callable_mp(this, &VideoStreamPlayer::texture_changed).bind(texture));
  241. }
  242. const int channels = playback->get_channels();
  243. AudioServer::get_singleton()->lock();
  244. if (channels > 0) {
  245. resampler.setup(channels, playback->get_mix_rate(), AudioServer::get_singleton()->get_mix_rate(), buffering_ms, 0);
  246. } else {
  247. resampler.clear();
  248. }
  249. AudioServer::get_singleton()->unlock();
  250. if (channels > 0) {
  251. playback->set_mix_callback(_audio_mix_callback, this);
  252. }
  253. } else {
  254. texture.unref();
  255. AudioServer::get_singleton()->lock();
  256. resampler.clear();
  257. AudioServer::get_singleton()->unlock();
  258. }
  259. queue_redraw();
  260. if (!expand) {
  261. update_minimum_size();
  262. }
  263. }
  264. Ref<VideoStream> VideoStreamPlayer::get_stream() const {
  265. return stream;
  266. }
  267. void VideoStreamPlayer::play() {
  268. ERR_FAIL_COND(!is_inside_tree());
  269. if (playback.is_null()) {
  270. return;
  271. }
  272. playback->play();
  273. set_process_internal(true);
  274. first_frame = true;
  275. if (!can_process()) {
  276. _notification(NOTIFICATION_PAUSED);
  277. }
  278. }
  279. void VideoStreamPlayer::stop() {
  280. if (!is_inside_tree()) {
  281. return;
  282. }
  283. if (playback.is_null()) {
  284. return;
  285. }
  286. playback->stop();
  287. resampler.flush();
  288. set_process_internal(false);
  289. }
  290. bool VideoStreamPlayer::is_playing() const {
  291. if (playback.is_null()) {
  292. return false;
  293. }
  294. return playback->is_playing();
  295. }
  296. void VideoStreamPlayer::set_paused(bool p_paused) {
  297. if (paused == p_paused) {
  298. return;
  299. }
  300. paused = p_paused;
  301. if (!p_paused && !can_process()) {
  302. paused_from_tree = true;
  303. return;
  304. } else if (p_paused && paused_from_tree) {
  305. paused_from_tree = false;
  306. return;
  307. }
  308. if (playback.is_valid()) {
  309. playback->set_paused(p_paused);
  310. set_process_internal(!p_paused);
  311. }
  312. }
  313. bool VideoStreamPlayer::is_paused() const {
  314. return paused;
  315. }
  316. void VideoStreamPlayer::set_buffering_msec(int p_msec) {
  317. buffering_ms = p_msec;
  318. }
  319. int VideoStreamPlayer::get_buffering_msec() const {
  320. return buffering_ms;
  321. }
  322. void VideoStreamPlayer::set_audio_track(int p_track) {
  323. audio_track = p_track;
  324. if (stream.is_valid()) {
  325. stream->set_audio_track(audio_track);
  326. }
  327. if (playback.is_valid()) {
  328. playback->set_audio_track(audio_track);
  329. }
  330. }
  331. int VideoStreamPlayer::get_audio_track() const {
  332. return audio_track;
  333. }
  334. void VideoStreamPlayer::set_volume(float p_vol) {
  335. volume = p_vol;
  336. }
  337. float VideoStreamPlayer::get_volume() const {
  338. return volume;
  339. }
  340. void VideoStreamPlayer::set_volume_db(float p_db) {
  341. if (p_db < -79) {
  342. set_volume(0);
  343. } else {
  344. set_volume(Math::db_to_linear(p_db));
  345. }
  346. }
  347. float VideoStreamPlayer::get_volume_db() const {
  348. if (volume == 0) {
  349. return -80;
  350. } else {
  351. return Math::linear_to_db(volume);
  352. }
  353. }
  354. void VideoStreamPlayer::set_speed_scale(float p_speed_scale) {
  355. ERR_FAIL_COND(p_speed_scale < 0.0);
  356. speed_scale = p_speed_scale;
  357. }
  358. float VideoStreamPlayer::get_speed_scale() const {
  359. return speed_scale;
  360. }
  361. String VideoStreamPlayer::get_stream_name() const {
  362. if (stream.is_null()) {
  363. return "<No Stream>";
  364. }
  365. return stream->get_name();
  366. }
  367. double VideoStreamPlayer::get_stream_length() const {
  368. if (playback.is_null()) {
  369. return 0;
  370. }
  371. return playback->get_length();
  372. }
  373. double VideoStreamPlayer::get_stream_position() const {
  374. if (playback.is_null()) {
  375. return 0;
  376. }
  377. return playback->get_playback_position();
  378. }
  379. void VideoStreamPlayer::set_stream_position(double p_position) {
  380. if (playback.is_valid()) {
  381. resampler.flush();
  382. playback->seek(p_position);
  383. first_frame = true;
  384. }
  385. }
  386. Ref<Texture2D> VideoStreamPlayer::get_video_texture() const {
  387. if (playback.is_valid()) {
  388. return playback->get_texture();
  389. }
  390. return Ref<Texture2D>();
  391. }
  392. void VideoStreamPlayer::set_autoplay(bool p_enable) {
  393. autoplay = p_enable;
  394. }
  395. bool VideoStreamPlayer::has_autoplay() const {
  396. return autoplay;
  397. }
  398. void VideoStreamPlayer::set_bus(const StringName &p_bus) {
  399. // If audio is active, must lock this.
  400. AudioServer::get_singleton()->lock();
  401. bus = p_bus;
  402. AudioServer::get_singleton()->unlock();
  403. }
  404. StringName VideoStreamPlayer::get_bus() const {
  405. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  406. if (AudioServer::get_singleton()->get_bus_name(i) == bus) {
  407. return bus;
  408. }
  409. }
  410. return SceneStringName(Master);
  411. }
  412. void VideoStreamPlayer::_validate_property(PropertyInfo &p_property) const {
  413. if (!Engine::get_singleton()->is_editor_hint()) {
  414. return;
  415. }
  416. if (p_property.name == "bus") {
  417. String options;
  418. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  419. if (i > 0) {
  420. options += ",";
  421. }
  422. String name = AudioServer::get_singleton()->get_bus_name(i);
  423. options += name;
  424. }
  425. p_property.hint_string = options;
  426. }
  427. }
  428. void VideoStreamPlayer::_bind_methods() {
  429. ClassDB::bind_method(D_METHOD("set_stream", "stream"), &VideoStreamPlayer::set_stream);
  430. ClassDB::bind_method(D_METHOD("get_stream"), &VideoStreamPlayer::get_stream);
  431. ClassDB::bind_method(D_METHOD("play"), &VideoStreamPlayer::play);
  432. ClassDB::bind_method(D_METHOD("stop"), &VideoStreamPlayer::stop);
  433. ClassDB::bind_method(D_METHOD("is_playing"), &VideoStreamPlayer::is_playing);
  434. ClassDB::bind_method(D_METHOD("set_paused", "paused"), &VideoStreamPlayer::set_paused);
  435. ClassDB::bind_method(D_METHOD("is_paused"), &VideoStreamPlayer::is_paused);
  436. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &VideoStreamPlayer::set_loop);
  437. ClassDB::bind_method(D_METHOD("has_loop"), &VideoStreamPlayer::has_loop);
  438. ClassDB::bind_method(D_METHOD("set_volume", "volume"), &VideoStreamPlayer::set_volume);
  439. ClassDB::bind_method(D_METHOD("get_volume"), &VideoStreamPlayer::get_volume);
  440. ClassDB::bind_method(D_METHOD("set_volume_db", "db"), &VideoStreamPlayer::set_volume_db);
  441. ClassDB::bind_method(D_METHOD("get_volume_db"), &VideoStreamPlayer::get_volume_db);
  442. ClassDB::bind_method(D_METHOD("set_speed_scale", "speed_scale"), &VideoStreamPlayer::set_speed_scale);
  443. ClassDB::bind_method(D_METHOD("get_speed_scale"), &VideoStreamPlayer::get_speed_scale);
  444. ClassDB::bind_method(D_METHOD("set_audio_track", "track"), &VideoStreamPlayer::set_audio_track);
  445. ClassDB::bind_method(D_METHOD("get_audio_track"), &VideoStreamPlayer::get_audio_track);
  446. ClassDB::bind_method(D_METHOD("get_stream_name"), &VideoStreamPlayer::get_stream_name);
  447. ClassDB::bind_method(D_METHOD("get_stream_length"), &VideoStreamPlayer::get_stream_length);
  448. ClassDB::bind_method(D_METHOD("set_stream_position", "position"), &VideoStreamPlayer::set_stream_position);
  449. ClassDB::bind_method(D_METHOD("get_stream_position"), &VideoStreamPlayer::get_stream_position);
  450. ClassDB::bind_method(D_METHOD("set_autoplay", "enabled"), &VideoStreamPlayer::set_autoplay);
  451. ClassDB::bind_method(D_METHOD("has_autoplay"), &VideoStreamPlayer::has_autoplay);
  452. ClassDB::bind_method(D_METHOD("set_expand", "enable"), &VideoStreamPlayer::set_expand);
  453. ClassDB::bind_method(D_METHOD("has_expand"), &VideoStreamPlayer::has_expand);
  454. ClassDB::bind_method(D_METHOD("set_buffering_msec", "msec"), &VideoStreamPlayer::set_buffering_msec);
  455. ClassDB::bind_method(D_METHOD("get_buffering_msec"), &VideoStreamPlayer::get_buffering_msec);
  456. ClassDB::bind_method(D_METHOD("set_bus", "bus"), &VideoStreamPlayer::set_bus);
  457. ClassDB::bind_method(D_METHOD("get_bus"), &VideoStreamPlayer::get_bus);
  458. ClassDB::bind_method(D_METHOD("get_video_texture"), &VideoStreamPlayer::get_video_texture);
  459. ADD_SIGNAL(MethodInfo("finished"));
  460. ADD_PROPERTY(PropertyInfo(Variant::INT, "audio_track", PROPERTY_HINT_RANGE, "0,128,1"), "set_audio_track", "get_audio_track");
  461. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "VideoStream"), "set_stream", "get_stream");
  462. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01,suffix:dB"), "set_volume_db", "get_volume_db");
  463. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume", PROPERTY_HINT_RANGE, "0,15,0.01,exp", PROPERTY_USAGE_NONE), "set_volume", "get_volume");
  464. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,4,0.001,or_greater"), "set_speed_scale", "get_speed_scale");
  465. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "has_autoplay");
  466. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_paused", "is_paused");
  467. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand");
  468. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  469. ADD_PROPERTY(PropertyInfo(Variant::INT, "buffering_msec", PROPERTY_HINT_RANGE, "10,1000,suffix:ms"), "set_buffering_msec", "get_buffering_msec");
  470. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stream_position", PROPERTY_HINT_RANGE, "0,1280000,0.1", PROPERTY_USAGE_NONE), "set_stream_position", "get_stream_position");
  471. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
  472. }
  473. VideoStreamPlayer::~VideoStreamPlayer() {
  474. resampler.clear(); // Not necessary here, but make in consistent with other "stream_player" classes.
  475. }