video_stream_player.cpp 15 KB

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