video_player.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*************************************************************************/
  2. /* video_player.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. if (playback.is_null()) {
  36. return 0;
  37. }
  38. return playback->get_channels();
  39. }
  40. bool VideoPlayer::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 (eg VideoStreamPlaybackWebm::update)
  53. int VideoPlayer::_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. VideoPlayer *vp = (VideoPlayer *)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 VideoPlayer::_mix_audios(void *p_self) {
  67. ERR_FAIL_NULL(p_self);
  68. reinterpret_cast<VideoPlayer *>(p_self)->_mix_audio();
  69. }
  70. // Called from audio thread
  71. void VideoPlayer::_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. 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_COND(!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_COND(!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 VideoPlayer::_notification(int p_notification) {
  106. switch (p_notification) {
  107. case NOTIFICATION_ENTER_TREE: {
  108. AudioServer::get_singleton()->add_callback(_mix_audios, this);
  109. if (stream.is_valid() && autoplay && !Engine::get_singleton()->is_editor_hint()) {
  110. play();
  111. }
  112. } break;
  113. case NOTIFICATION_EXIT_TREE: {
  114. AudioServer::get_singleton()->remove_callback(_mix_audios, this);
  115. } break;
  116. case NOTIFICATION_INTERNAL_PROCESS: {
  117. bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus);
  118. if (stream.is_null() || paused || playback.is_null() || !playback->is_playing())
  119. return;
  120. double audio_time = USEC_TO_SEC(OS::get_singleton()->get_ticks_usec());
  121. double delta = last_audio_time == 0 ? 0 : audio_time - last_audio_time;
  122. last_audio_time = audio_time;
  123. if (delta == 0)
  124. return;
  125. playback->update(delta); // playback->is_playing() returns false in the last video frame
  126. if (!playback->is_playing()) {
  127. emit_signal(SceneStringNames::get_singleton()->finished);
  128. }
  129. } break;
  130. case NOTIFICATION_DRAW: {
  131. if (texture.is_null())
  132. return;
  133. if (texture->get_width() == 0)
  134. return;
  135. Size2 s = expand ? get_size() : texture->get_size();
  136. draw_texture_rect(texture, Rect2(Point2(), s), false);
  137. } break;
  138. };
  139. };
  140. Size2 VideoPlayer::get_minimum_size() const {
  141. if (!expand && !texture.is_null())
  142. return texture->get_size();
  143. else
  144. return Size2();
  145. }
  146. void VideoPlayer::set_expand(bool p_expand) {
  147. expand = p_expand;
  148. update();
  149. minimum_size_changed();
  150. }
  151. bool VideoPlayer::has_expand() const {
  152. return expand;
  153. }
  154. void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) {
  155. stop();
  156. AudioServer::get_singleton()->lock();
  157. mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size());
  158. stream = p_stream;
  159. if (stream.is_valid()) {
  160. stream->set_audio_track(audio_track);
  161. playback = stream->instance_playback();
  162. } else {
  163. playback = Ref<VideoStreamPlayback>();
  164. }
  165. AudioServer::get_singleton()->unlock();
  166. if (!playback.is_null()) {
  167. playback->set_loop(loops);
  168. playback->set_paused(paused);
  169. texture = playback->get_texture();
  170. const int channels = playback->get_channels();
  171. AudioServer::get_singleton()->lock();
  172. if (channels > 0)
  173. resampler.setup(channels, playback->get_mix_rate(), AudioServer::get_singleton()->get_mix_rate(), buffering_ms, 0);
  174. else
  175. resampler.clear();
  176. AudioServer::get_singleton()->unlock();
  177. if (channels > 0)
  178. playback->set_mix_callback(_audio_mix_callback, this);
  179. } else {
  180. texture.unref();
  181. AudioServer::get_singleton()->lock();
  182. resampler.clear();
  183. AudioServer::get_singleton()->unlock();
  184. }
  185. update();
  186. if (!expand) {
  187. minimum_size_changed();
  188. }
  189. };
  190. Ref<VideoStream> VideoPlayer::get_stream() const {
  191. return stream;
  192. };
  193. void VideoPlayer::play() {
  194. ERR_FAIL_COND(!is_inside_tree());
  195. if (playback.is_null())
  196. return;
  197. playback->stop();
  198. playback->play();
  199. set_process_internal(true);
  200. // AudioServer::get_singleton()->stream_set_active(stream_rid,true);
  201. // AudioServer::get_singleton()->stream_set_volume_scale(stream_rid,volume);
  202. last_audio_time = 0;
  203. };
  204. void VideoPlayer::stop() {
  205. if (!is_inside_tree())
  206. return;
  207. if (playback.is_null())
  208. return;
  209. playback->stop();
  210. // AudioServer::get_singleton()->stream_set_active(stream_rid,false);
  211. resampler.flush();
  212. set_process_internal(false);
  213. last_audio_time = 0;
  214. };
  215. bool VideoPlayer::is_playing() const {
  216. if (playback.is_null())
  217. return false;
  218. return playback->is_playing();
  219. };
  220. void VideoPlayer::set_paused(bool p_paused) {
  221. paused = p_paused;
  222. if (playback.is_valid()) {
  223. playback->set_paused(p_paused);
  224. set_process_internal(!p_paused);
  225. };
  226. last_audio_time = 0;
  227. };
  228. bool VideoPlayer::is_paused() const {
  229. return paused;
  230. }
  231. void VideoPlayer::set_buffering_msec(int p_msec) {
  232. buffering_ms = p_msec;
  233. }
  234. int VideoPlayer::get_buffering_msec() const {
  235. return buffering_ms;
  236. }
  237. void VideoPlayer::set_audio_track(int p_track) {
  238. audio_track = p_track;
  239. }
  240. int VideoPlayer::get_audio_track() const {
  241. return audio_track;
  242. }
  243. void VideoPlayer::set_volume(float p_vol) {
  244. volume = p_vol;
  245. };
  246. float VideoPlayer::get_volume() const {
  247. return volume;
  248. };
  249. void VideoPlayer::set_volume_db(float p_db) {
  250. if (p_db < -79)
  251. set_volume(0);
  252. else
  253. set_volume(Math::db2linear(p_db));
  254. };
  255. float VideoPlayer::get_volume_db() const {
  256. if (volume == 0)
  257. return -80;
  258. else
  259. return Math::linear2db(volume);
  260. };
  261. String VideoPlayer::get_stream_name() const {
  262. if (stream.is_null())
  263. return "<No Stream>";
  264. return stream->get_name();
  265. };
  266. float VideoPlayer::get_stream_position() const {
  267. if (playback.is_null())
  268. return 0;
  269. return playback->get_playback_position();
  270. };
  271. void VideoPlayer::set_stream_position(float p_position) {
  272. if (playback.is_valid())
  273. playback->seek(p_position);
  274. }
  275. Ref<Texture> VideoPlayer::get_video_texture() const {
  276. if (playback.is_valid())
  277. return playback->get_texture();
  278. return Ref<Texture>();
  279. }
  280. void VideoPlayer::set_autoplay(bool p_enable) {
  281. autoplay = p_enable;
  282. };
  283. bool VideoPlayer::has_autoplay() const {
  284. return autoplay;
  285. };
  286. void VideoPlayer::set_bus(const StringName &p_bus) {
  287. //if audio is active, must lock this
  288. AudioServer::get_singleton()->lock();
  289. bus = p_bus;
  290. AudioServer::get_singleton()->unlock();
  291. }
  292. StringName VideoPlayer::get_bus() const {
  293. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  294. if (AudioServer::get_singleton()->get_bus_name(i) == bus) {
  295. return bus;
  296. }
  297. }
  298. return "Master";
  299. }
  300. void VideoPlayer::_validate_property(PropertyInfo &p_property) const {
  301. if (p_property.name == "bus") {
  302. String options;
  303. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  304. if (i > 0)
  305. options += ",";
  306. String name = AudioServer::get_singleton()->get_bus_name(i);
  307. options += name;
  308. }
  309. p_property.hint_string = options;
  310. }
  311. }
  312. void VideoPlayer::_bind_methods() {
  313. ClassDB::bind_method(D_METHOD("set_stream", "stream"), &VideoPlayer::set_stream);
  314. ClassDB::bind_method(D_METHOD("get_stream"), &VideoPlayer::get_stream);
  315. ClassDB::bind_method(D_METHOD("play"), &VideoPlayer::play);
  316. ClassDB::bind_method(D_METHOD("stop"), &VideoPlayer::stop);
  317. ClassDB::bind_method(D_METHOD("is_playing"), &VideoPlayer::is_playing);
  318. ClassDB::bind_method(D_METHOD("set_paused", "paused"), &VideoPlayer::set_paused);
  319. ClassDB::bind_method(D_METHOD("is_paused"), &VideoPlayer::is_paused);
  320. ClassDB::bind_method(D_METHOD("set_volume", "volume"), &VideoPlayer::set_volume);
  321. ClassDB::bind_method(D_METHOD("get_volume"), &VideoPlayer::get_volume);
  322. ClassDB::bind_method(D_METHOD("set_volume_db", "db"), &VideoPlayer::set_volume_db);
  323. ClassDB::bind_method(D_METHOD("get_volume_db"), &VideoPlayer::get_volume_db);
  324. ClassDB::bind_method(D_METHOD("set_audio_track", "track"), &VideoPlayer::set_audio_track);
  325. ClassDB::bind_method(D_METHOD("get_audio_track"), &VideoPlayer::get_audio_track);
  326. ClassDB::bind_method(D_METHOD("get_stream_name"), &VideoPlayer::get_stream_name);
  327. ClassDB::bind_method(D_METHOD("set_stream_position", "position"), &VideoPlayer::set_stream_position);
  328. ClassDB::bind_method(D_METHOD("get_stream_position"), &VideoPlayer::get_stream_position);
  329. ClassDB::bind_method(D_METHOD("set_autoplay", "enabled"), &VideoPlayer::set_autoplay);
  330. ClassDB::bind_method(D_METHOD("has_autoplay"), &VideoPlayer::has_autoplay);
  331. ClassDB::bind_method(D_METHOD("set_expand", "enable"), &VideoPlayer::set_expand);
  332. ClassDB::bind_method(D_METHOD("has_expand"), &VideoPlayer::has_expand);
  333. ClassDB::bind_method(D_METHOD("set_buffering_msec", "msec"), &VideoPlayer::set_buffering_msec);
  334. ClassDB::bind_method(D_METHOD("get_buffering_msec"), &VideoPlayer::get_buffering_msec);
  335. ClassDB::bind_method(D_METHOD("set_bus", "bus"), &VideoPlayer::set_bus);
  336. ClassDB::bind_method(D_METHOD("get_bus"), &VideoPlayer::get_bus);
  337. ClassDB::bind_method(D_METHOD("get_video_texture"), &VideoPlayer::get_video_texture);
  338. ADD_SIGNAL(MethodInfo("finished"));
  339. ADD_PROPERTY(PropertyInfo(Variant::INT, "audio_track", PROPERTY_HINT_RANGE, "0,128,1"), "set_audio_track", "get_audio_track");
  340. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "VideoStream"), "set_stream", "get_stream");
  341. //ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/loop"), "set_loop", "has_loop") ;
  342. ADD_PROPERTY(PropertyInfo(Variant::REAL, "volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01"), "set_volume_db", "get_volume_db");
  343. ADD_PROPERTY(PropertyInfo(Variant::REAL, "volume", PROPERTY_HINT_EXP_RANGE, "0,15,0.01", 0), "set_volume", "get_volume");
  344. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "has_autoplay");
  345. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_paused", "is_paused");
  346. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand");
  347. ADD_PROPERTY(PropertyInfo(Variant::INT, "buffering_msec", PROPERTY_HINT_RANGE, "10,1000"), "set_buffering_msec", "get_buffering_msec");
  348. ADD_PROPERTY(PropertyInfo(Variant::REAL, "stream_position", PROPERTY_HINT_RANGE, "0,1280000,0.1", 0), "set_stream_position", "get_stream_position");
  349. ADD_PROPERTY(PropertyInfo(Variant::STRING, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
  350. }
  351. VideoPlayer::VideoPlayer() {
  352. volume = 1;
  353. loops = false;
  354. paused = false;
  355. autoplay = false;
  356. expand = true;
  357. audio_track = 0;
  358. bus_index = 0;
  359. buffering_ms = 500;
  360. // internal_stream.player=this;
  361. // stream_rid=AudioServer::get_singleton()->audio_stream_create(&internal_stream);
  362. last_audio_time = 0;
  363. wait_resampler = 0;
  364. wait_resampler_limit = 2;
  365. };
  366. VideoPlayer::~VideoPlayer() {
  367. // if (stream_rid.is_valid())
  368. // AudioServer::get_singleton()->free(stream_rid);
  369. resampler.clear(); //Not necessary here, but make in consistent with other "stream_player" classes
  370. };