video_stream_player.cpp 16 KB

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