BitmapToolkit Scol plugin
MediaPlayerToolkit.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2016 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25#include <scolPlugin.h>
26
27#include <limits>
28#include "MediaPlayer.h"
29#include "ScolConvert.h"
30
31/*
32 Scol toolkit for the MediaPlayer object
33 First version : jan 2016
34 Author : Hector Labanca
35 */
36
49
50// Helper function to retrieve a media player parameter from the machine.
51MediaPlayer* getPlayerFromStack(mmachine m, int pos)
52{
53 int playerPtr = MMget(m, pos);
54 if (playerPtr == NIL)
55 return NULL;
56
57 SCOL_PTR_TYPE player = *(SCOL_PTR_TYPE*)MMstart(m, MTOP(playerPtr));
58 return (MediaPlayer*)player;
59}
60
61int destroyMediaPlayerObj(mmachine m, SCOL_PTR_TYPE handsys, int objm)
62{
63 MediaPlayer* player = (MediaPlayer*) handsys;
64 SAFE_DELETE(player);
65
66 //reset buffer value
67 *(SCOL_PTR_TYPE*)MMstart(m, MTOP(objm)) = 0;
68
69 return 0;
70}
71
80int _CRmediaPlayer(mmachine m)
81{
82#ifdef _SCOL_DEBUG_
83 MMechostr(MSKDEBUG, "_CRMediaPlayer\n");
84#endif
85 // Make sure the channel is there
86 if (MMget(m, 0)==NIL)
87 return SCERRSET(m, EARGNIL, MSKDEBUG);
88
89 MediaPlayer* player = 0;
90 try
91 {
92 player = new MediaPlayer();
93 }
94 catch (std::exception &)
95 {
96 return SCERRSET(m, EMEM, MSKDEBUG);
97 }
98
99 if ((MMpushPointer(m, player) != 0))
100 {
101 SAFE_DELETE(player);
102 return SCERRVM(m, MERRMEM);
103 }
104
105 return OBJcreate(m, OBJMEDIAPLAYERSCOL, SCOL_PTR player, NIL, 0);
106}
107
116int _DSmediaPlayer(mmachine m)
117{
118#ifdef _SCOL_DEBUG_
119 MMechostr(MSKDEBUG, "_DSMediaPlayer\n");
120#endif
121 int obj = MMget(m, 0);
122 if (obj == NIL)
123 {
124 MMset(m, 0, NIL);
125 return 0;
126 }
127
128 OBJdelTM(m, OBJMEDIAPLAYERSCOL, obj);
129
130 MMset(m, 0, ITOM(0));
131 return 0;
132}
133
143int _OPENmediaPlayerUrl(mmachine m)
144{
145#ifdef _SCOL_DEBUG_
146 MMechostr(MSKDEBUG, "_OPENmediaPlayerUrl\n");
147#endif
148 int ipath = MMpull(m);
149 MediaPlayer* player = getPlayerFromStack(m, 0);
150
151 if ((player == NULL) || (ipath == NIL))
152 {
153 MMset(m, 0, NIL);
154 return 0;
155 }
156
157 std::string path = MMstartstr(m, MTOP(ipath));
158 if (path.empty())
159 {
160 MMset(m, 0, NIL);
161 return 0;
162 }
163
164 try
165 {
166 player->OpenUrl(path);
167 }
168 catch(std::exception &e)
169 {
170 std::string errs = e.what();
171 MMechostr(MSKRUNTIME, "_OPENmediaPlayerUrl : %s", e.what());
172 MMset(m, 0, NIL);
173 return 0;
174 }
175
176 return 0;
177}
178
188int _OPENmediaPlayerFile(mmachine m)
189{
190#ifdef _SCOL_DEBUG_
191 MMechostr(MSKDEBUG, "_OPENmediaPlayerFile\n");
192#endif
193 int ipath = MMpull(m);
194 MediaPlayer* player = getPlayerFromStack(m, 0);
195
196 if ((player == NULL) || (ipath == NIL))
197 {
198 MMset(m, 0, NIL);
199 return 0;
200 }
201
202 std::string path = MMstartstr(m, MTOP(ipath));
203 if (path.empty())
204 {
205 MMset(m, 0, NIL);
206 return 0;
207 }
208
209 try
210 {
211 player->Open(path);
212 }
213 catch(std::exception &e)
214 {
215 MMechostr(MSKRUNTIME, "_OPENmediaPlayerFile : %s", e.what());
216 MMset(m, 0, NIL);
217 return 0;
218 }
219
220 return 0;
221}
222
231int _BLTmediaPlayer(mmachine m)
232{
233#ifdef _SCOL_DEBUG_
234 MMechostr(MSKDEBUG, "_BLTmediaPlayer\n");
235#endif
236 int bitmap = MMpull(m);
237 MediaPlayer* player = getPlayerFromStack(m, 0);
238
239 if ((player == NULL) || (bitmap == NIL))
240 {
241 MMset(m, 0, NIL);
242 return 0;
243 }
244
245 PtrObjVoid OB = (PtrObjVoid) MMstart(m, MTOP(bitmap));
246 PtrObjBitmap B = (PtrObjBitmap) MMstart(m, MTOP(OB->Buffer));
247
248 if (player->GetFrame(B))
249 MMset(m, 0, bitmap);
250 else
251 MMset(m, 0, NIL);
252
253 return 0;
254}
255
268{
269#ifdef _SCOL_DEBUG_
270 MMechostr(MSKDEBUG, "_BLTmediaPlayerBuffer\n");
271#endif
272 int ibytesperpixel = MMpull(m);
273 int iheight = MMpull(m);
274 int iwidth = MMpull(m);
275 int iBuff = MMpull(m);
276
277 MediaPlayer* player = getPlayerFromStack(m, 0);
278
279 if ((player == NULL) || (iBuff == NIL) || (iwidth == NIL) || (iheight == NIL) || (ibytesperpixel == NIL))
280 {
281 MMset(m, 0, NIL);
282 return 0;
283 }
284
285 unsigned char* buffer = MMgetPointer<unsigned char*>(m, MTOP(iBuff));
286 if (buffer == NULL)
287 {
288 MMset(m, 0, NIL);
289 return 0;
290 }
291
292 ibytesperpixel = MTOI(ibytesperpixel);
293 iheight = MTOI(iheight);
294 iwidth = MTOI(iwidth);
295
296 cv::Mat imagesrc = ConversionTools::ScolBitmapBufferToMat((void*)buffer, iwidth, iheight, ibytesperpixel);
297 if (imagesrc.empty())
298 {
299 MMset(m, 0, NIL);
300 return 0;
301 }
302
303 if (player->GetFrame(imagesrc))
304 MMset(m, 0, iBuff);
305 else
306 MMset(m, 0, NIL);
307
308 return 0;
309}
310
322{
323#ifdef _SCOL_DEBUG_
324 MMechostr(MSKDEBUG, "_GETmediaPlayerBuffer\n");
325#endif
326
327 MediaPlayer* player = getPlayerFromStack(m, 0);
328
329 if (player == NULL)
330 {
331 MMset(m, 0, NIL);
332 return 0;
333 }
334
335 cv::Mat frame = player->GetFrame();
336 if (frame.empty())
337 {
338 MMset(m, 0, NIL);
339 return 0;
340 }
341
342 MMpull(m);
343 if ((MMpushPointer(m, frame.data) != 0))
344 {
345 MMpush(m, NIL);
346 return 0;
347 }
348
349 MMpush(m, ITOM(frame.cols));
350 MMpush(m, ITOM(frame.rows));
351 MMpush(m, ITOM(frame.channels()));
352 MMpush(m, ITOM(4));
353 MBdeftab(m);
354
355 return 0;
356}
357
365int _PLAYmediaPlayer(mmachine m)
366{
367#ifdef _SCOL_DEBUG_
368 MMechostr(MSKDEBUG, "_PLAYmediaPlayer\n");
369#endif
370
371 MediaPlayer* player = getPlayerFromStack(m, 0);
372
373 if (player == NULL)
374 {
375 MMset(m, 0, NIL);
376 return 0;
377 }
378
379 try
380 {
381 player->Play(-1);
382 }
383 catch (std::exception &e)
384 {
385 MMechostr(MSKRUNTIME, "_PLAYmediaPlayer : %s\n", e.what());
386 MMset(m, 0, NIL);
387 }
388
389 return 0;
390}
391
399int _PAUSEmediaPlayer(mmachine m)
400{
401#ifdef _SCOL_DEBUG_
402 MMechostr(MSKDEBUG, "_PAUSEmediaPlayer\n");
403#endif
404
405 MediaPlayer* player = getPlayerFromStack(m, 0);
406
407 if (player == NULL)
408 {
409 MMset(m, 0, NIL);
410 return 0;
411 }
412
413 try
414 {
415 player->Pause();
416 }
417 catch (std::exception &e)
418 {
419 MMechostr(MSKRUNTIME, "_PAUSEmediaPlayer : %s\n", e.what());
420 MMset(m, 0, NIL);
421 }
422
423 return 0;
424}
425
433int _STOPmediaPlayer(mmachine m)
434{
435#ifdef _SCOL_DEBUG_
436 MMechostr(MSKDEBUG, "_STOPmediaPlayer\n");
437#endif
438
439 MediaPlayer* player = getPlayerFromStack(m, 0);
440
441 if (player == NULL)
442 {
443 MMset(m, 0, NIL);
444 return 0;
445 }
446
447 player->Stop();
448 return 0;
449}
450
460int _SETmediaPlayerSize(mmachine m)
461{
462#ifdef _SCOL_DEBUG_
463 MMechostr(MSKDEBUG, "_SETmediaPlayerSize\n");
464#endif
465 int iheight = MMpull(m);
466 int iwidth = MMpull(m);
467 MediaPlayer* player = getPlayerFromStack(m, 0);
468
469 if (player == NULL)
470 {
471 MMset(m, 0, NIL);
472 return 0;
473 }
474
475 int width = 0;
476 if (iwidth != NIL)
477 width = abs(MTOI(iwidth));
478
479 int height = 0;
480 if (iheight != NIL)
481 height = abs(MTOI(iheight));
482
483 player->SetSize(width, height);
484 return 0;
485}
486
494int _GETmediaPlayerSize(mmachine m)
495{
496#ifdef _SCOL_DEBUG_
497 MMechostr(MSKDEBUG, "_GETmediaPlayerSize\n");
498#endif
499 MediaPlayer* player = getPlayerFromStack(m, 0);
500
501 if (player == NULL)
502 {
503 MMset(m, 0, NIL);
504 return 0;
505 }
506
507 int width = 0;
508 int height = 0;
509
510 try
511 {
512 player->GetSize(width, height);
513 }
514 catch (std::exception& e)
515 {
516 MMechostr(MSKRUNTIME, "_GETmediaPlayerSize : %s\n", e.what());
517 MMset(m, 0, NIL);
518 return 0;
519 }
520
521 int result = MMmalloc(m, 2, TYPETAB);
522 if(result==NIL)
523 {
524 MMset(m, 0, NIL);
525 return MERRMEM;
526 }
527
528 MMstore(m, result, 0, ITOM(width));
529 MMstore(m, result, 1, ITOM(height));
530 MMset(m, 0, PTOM(result));
531
532 return 0;
533}
534
543{
544#ifdef _SCOL_DEBUG_
545 MMechostr(MSKDEBUG, "_GETmediaPlayerSourceSize\n");
546#endif
547 MediaPlayer* player = getPlayerFromStack(m, 0);
548
549 if (player == NULL)
550 {
551 MMset(m, 0, NIL);
552 return 0;
553 }
554
555 int width = 0;
556 int height = 0;
557
558 try
559 {
560 player->GetSourceSize(width, height);
561 }
562 catch (std::exception& e)
563 {
564 MMechostr(MSKRUNTIME, "_GETmediaPlayerSourceSize : %s\n", e.what());
565 MMset(m, 0, NIL);
566 return 0;
567 }
568
569 int result = MMmalloc(m, 2, TYPETAB);
570 if(result==NIL)
571 {
572 MMset(m, 0, NIL);
573 return MERRMEM;
574 }
575
576 MMstore(m, result, 0, ITOM(width));
577 MMstore(m, result, 1, ITOM(height));
578 MMset(m, 0, PTOM(result));
579
580 return 0;
581}
582
590int _GETmediaPlayerTime(mmachine m)
591{
592#ifdef _SCOL_DEBUG_
593 MMechostr(MSKDEBUG, "_GETmediaPlayerTime\n");
594#endif
595
596 MediaPlayer* player = getPlayerFromStack(m, 0);
597
598 if (player == NULL)
599 {
600 MMset(m, 0, NIL);
601 return 0;
602 }
603
604 long time = 0;
605 try
606 {
607 time = player->GetCurrentTime();
608 }
609 catch (std::exception& e)
610 {
611 MMechostr(MSKRUNTIME, "_GETmediaPlayerTime : %s\n", e.what());
612 MMset(m, 0, NIL);
613 return 0;
614 }
615
616 if (time > std::numeric_limits<int>::max())
617 {
618 MMechostr(MSKRUNTIME, "_GETmediaPlayerTime : warning : timestamp is too large for int type : %u\n", time);
619 time = std::numeric_limits<int>::max();
620 }
621
622 MMset(m, 0, ITOM((int)time));
623 return 0;
624}
625
626
635{
636#ifdef _SCOL_DEBUG_
637 MMechostr(MSKDEBUG, "_GETmediaPlayerTime\n");
638#endif
639
640 MediaPlayer* player = getPlayerFromStack(m, 0);
641
642 if (player == NULL)
643 {
644 MMset(m, 0, NIL);
645 return 0;
646 }
647
648 long length = 0;
649 try
650 {
651 length = player->GetLength();
652 }
653 catch (std::exception& e)
654 {
655 MMechostr(MSKRUNTIME, "_GETmediaPlayerLength : %s\n", e.what());
656 MMset(m, 0, NIL);
657 return 0;
658 }
659
660 MMset(m, 0, ITOM((int)length));
661 return 0;
662}
663
672int _SEEKmediaPlayer(mmachine m)
673{
674#ifdef _SCOL_DEBUG_
675 MMechostr(MSKDEBUG, "_SEEKmediaPlayer\n");
676#endif
677 int vtime = MMpull(m);
678 if (vtime == NIL)
679 {
680 MMechostr(MSKRUNTIME, "_SEEKmediaPlayer : video time is nil.\n");
681 MMset(m, 0, NIL);
682 return 0;
683 }
684
685 vtime = MTOI(vtime);
686 MediaPlayer* player = getPlayerFromStack(m, 0);
687
688 if (player == NULL)
689 {
690 MMset(m, 0, NIL);
691 return 0;
692 }
693
694 try
695 {
696 player->SetCurrentTime(vtime);
697 }
698 catch (std::exception& e)
699 {
700 MMechostr(MSKRUNTIME, "_SEEKmediaPlayer : %s\n", e.what());
701 MMset(m, 0, NIL);
702 return 0;
703 }
704
705 return 0;
706}
707
716int _SETmediaPlayerLoop(mmachine m)
717{
718#ifdef _SCOL_DEBUG_
719 MMechostr(MSKDEBUG, "_SETmediaPlayerLoop\n");
720#endif
721 int iloop = MMpull(m);
722 MediaPlayer* player = getPlayerFromStack(m, 0);
723
724 if (iloop == NIL)
725 {
726 MMechostr(MSKRUNTIME, "_SETmediaPlayerLoop : loop value is nil.\n");
727 MMset(m, 0, NIL);
728 return 0;
729 }
730
731 if (player == NULL)
732 {
733 MMset(m, 0, NIL);
734 return 0;
735 }
736
737 player->SetLoop(MTOI(iloop) == 0 ? false : true);
738
739 return 0;
740}
741
751{
752#ifdef _SCOL_DEBUG_
753 MMechostr(MSKDEBUG, "_SETmediaPlayerStream\n");
754#endif
755 int istream = MMpull(m);
756 MediaPlayer* player = getPlayerFromStack(m, 0);
757
758 if (istream == NIL)
759 {
760 MMechostr(MSKRUNTIME, "_SETmediaPlayerStream : stream index is nil.\n");
761 MMset(m, 0, NIL);
762 return 0;
763 }
764
765 if (player == NULL)
766 {
767 MMset(m, 0, NIL);
768 return 0;
769 }
770
771 try
772 {
773 player->SetVideoStream(MTOI(istream));
774 }
775 catch (std::exception& e)
776 {
777 MMechostr(MSKRUNTIME, "_SETmediaPlayerStream : couldn't open stream %d : %s\n", MTOI(istream), e.what());
778 MMset(m, 0, NIL);
779 }
780
781 return 0;
782}
783
793{
794#ifdef _SCOL_DEBUG_
795 MMechostr(MSKDEBUG, "_SETmediaPlayerAudioStream\n");
796#endif
797 int istream = MMpull(m);
798 MediaPlayer* player = getPlayerFromStack(m, 0);
799
800 if (istream == NIL)
801 {
802 MMechostr(MSKRUNTIME, "_SETmediaPlayerAudioStream : stream index is nil.\n");
803 MMset(m, 0, NIL);
804 return 0;
805 }
806
807 if (player == NULL)
808 {
809 MMset(m, 0, NIL);
810 return 0;
811 }
812
813 try
814 {
815 player->SetAudioStream(MTOI(istream));
816 }
817 catch (std::exception& e)
818 {
819 MMechostr(MSKRUNTIME, "_SETmediaPlayerAudioStream : couldn't open stream %d : %s\n", MTOI(istream), e.what());
820 MMset(m, 0, NIL);
821 }
822
823 return 0;
824}
825
833int _GETmediaPlayerAudio(mmachine m)
834{
835#ifdef _SCOL_DEBUG_
836 MMechostr(MSKDEBUG, "_GETmediaPlayerAudio\n");
837#endif
838 MediaPlayer* player = getPlayerFromStack(m, 0);
839
840 if (player == NULL || !player->HasAudio())
841 {
842 MMset(m, 0, NIL);
843 return 0;
844 }
845
846 char tmpBuffer[MAX_OUT_BUFFER] = {0};
847 int bytes = 0;
848 try
849 {
850 bytes = player->GetAudioThreaded(tmpBuffer, MAX_OUT_BUFFER);
851 }
852 catch (std::exception& e)
853 {
854 MMechostr(MSKRUNTIME, "_GETmediaPlayerAudio : %s\n", e.what());
855 MMset(m, 0, NIL);
856 return 0;
857 }
858
859 if (bytes == 0)
860 {
861 MMset(m, 0, NIL);
862 return 0;
863 }
864
865 int res = MMmalloc(m, STR_SIZE(bytes), TYPEBUF);
866 if (res == NIL)
867 {
868 MMset(m, 0, NIL);
869 return MERRMEM;
870 }
871
872 MMstore(m, res, 0, bytes);
873 char* buffer = MMstartstr(m, res);
874 memcpy(buffer, tmpBuffer, bytes);
875 buffer[bytes] = 0;
876 MMset(m, 0, PTOM(res));
877 return 0;
878}
879
890{
891#ifdef _SCOL_DEBUG_
892 MMechostr(MSKDEBUG, "_SETmediaPlayerAudioFormat\n");
893#endif
894 int irate = MMpull(m);
895 int iformat = MMpull(m);
896 MediaPlayer* player = getPlayerFromStack(m, 0);
897
898 if ((irate == NIL) || (iformat == NIL) || (player == NULL))
899 {
900 MMechostr(MSKRUNTIME, "_SETmediaPlayerAudioFormat : one or more invalid parameter\n");
901 MMset(m, 0, NIL);
902 return 0;
903 }
904
905 try
906 {
907 player->SetAudioFormat(MediaPlayer::AudioFormats(MTOI(iformat)), MTOI(irate));
908 }
909 catch (const std::exception& e)
910 {
911 MMechostr(MSKRUNTIME, "_SETmediaPlayerAudioFormat error: %s\n", e.what());
912 MMset(m, 0, NIL);
913 return 0;
914 }
915
916 return 0;
917}
918
927{
928#ifdef _SCOL_DEBUG_
929 MMechostr(MSKDEBUG, "_GETmediaPlayerHasAudio\n");
930#endif
931 MediaPlayer* player = getPlayerFromStack(m, 0);
932
933 if (player == NULL)
934 {
935 MMechostr(MSKRUNTIME, "_GETmediaPlayerHasAudio : invalid media player object\n");
936 MMset(m, 0, NIL);
937 return 0;
938 }
939
940 int hasAudio = player->HasAudio() ? 1 : 0;
941
942 MMset(m, 0, ITOM(hasAudio));
943
944 return 0;
945}
946
955{
956#ifdef _SCOL_DEBUG_
957 MMechostr(MSKDEBUG, "_GETmediaPlayerHasVideo\n");
958#endif
959 MediaPlayer* player = getPlayerFromStack(m, 0);
960
961 if (player == NULL)
962 {
963 MMechostr(MSKRUNTIME, "_GETmediaPlayerHasVideo : invalid media player object\n");
964 MMset(m, 0, NIL);
965 return 0;
966 }
967
968 int hasVideo = player->HasVideo() ? 1 : 0;
969
970 MMset(m, 0, ITOM(hasVideo));
971
972 return 0;
973}
974
982int _GETmediaPlayerState(mmachine m)
983{
984#ifdef _SCOL_DEBUG_
985 MMechostr(MSKDEBUG, "_GETmediaPlayerState\n");
986#endif
987 MediaPlayer* player = getPlayerFromStack(m, 0);
988
989 if (player == NULL)
990 {
991 MMechostr(MSKRUNTIME, "_GETmediaPlayerState : invalid media player object\n");
992 MMset(m, 0, NIL);
993 return 0;
994 }
995
996 int state = (int)player->GetState();
997
998 MMset(m, 0, ITOM(state));
999
1000 return 0;
1001}
1002
1011{
1012#ifdef _SCOL_DEBUG_
1013 MMechostr(MSKDEBUG, "_GETmediaPlayerIsLiveStream\n");
1014#endif
1015 MediaPlayer* player = getPlayerFromStack(m, 0);
1016
1017 if (player == NULL)
1018 {
1019 MMechostr(MSKRUNTIME, "_GETmediaPlayerIsLiveStream : invalid media player object\n");
1020 MMset(m, 0, NIL);
1021 return 0;
1022 }
1023
1024 int isLive = player->IsLiveStream() ? 1 : 0;
1025
1026 MMset(m, 0, ITOM(isLive));
1027
1028 return 0;
1029}
1030
1039{
1040#ifdef _SCOL_DEBUG_
1041 MMechostr(MSKDEBUG, "_GETmediaPlayerIsSeekable\n");
1042#endif
1043 MediaPlayer* player = getPlayerFromStack(m, 0);
1044
1045 if (player == NULL)
1046 {
1047 MMechostr(MSKRUNTIME, "_GETmediaPlayerIsSeekable : invalid media player object\n");
1048 MMset(m, 0, NIL);
1049 return 0;
1050 }
1051
1052 int isSeekable = player->IsSeekable() ? 1 : 0;
1053
1054 MMset(m, 0, ITOM(isSeekable));
1055
1056 return 0;
1057}
1058
1068{
1069#ifdef _SCOL_DEBUG_
1070 MMechostr(MSKDEBUG, "_GETmediaPlayerAudioStreams\n");
1071#endif
1072 MediaPlayer* player = getPlayerFromStack(m, 0);
1073
1074 if (player == NULL)
1075 {
1076 MMechostr(MSKRUNTIME, "_GETmediaPlayerAudioStreams : invalid media player object\n");
1077 MMset(m, 0, NIL);
1078 return 0;
1079 }
1080
1081 std::vector<std::string> streams = player->ListStreams(AVMEDIA_TYPE_AUDIO);
1082
1083 if (streams.empty())
1084 {
1085 MMechostr(MSKRUNTIME, "_GETmediaPlayerAudioStreams : no audio stream\n");
1086 MMset(m, 0, NIL);
1087 return 0;
1088 }
1089
1090 // The initial argument is still there, remove it now
1091 MMpull(m);
1092
1093 for (unsigned int i = 0; i < streams.size(); i++)
1094 {
1095 std::string data = streams.at(i);
1096 Mpushstrbloc(m, (char*)data.c_str());
1097 }
1098
1099 if(MMpush(m, NIL))
1100 return MERRMEM;
1101
1102 for (unsigned int i = 0; i < streams.size(); i++)
1103 {
1104 if (MMpush(m, 2*2))
1105 return MERRMEM;
1106
1107 if (int k=MBdeftab(m))
1108 return k;
1109 }
1110
1111 return 0;
1112}
1113
1123{
1124#ifdef _SCOL_DEBUG_
1125 MMechostr(MSKDEBUG, "_GETmediaPlayerVideoStreams\n");
1126#endif
1127 MediaPlayer* player = getPlayerFromStack(m, 0);
1128
1129 if (player == NULL)
1130 {
1131 MMechostr(MSKRUNTIME, "_GETmediaPlayerVideoStreams : invalid media player object\n");
1132 MMset(m, 0, NIL);
1133 return 0;
1134 }
1135
1136 std::vector<std::string> streams = player->ListStreams(AVMEDIA_TYPE_VIDEO);
1137
1138 if (streams.empty())
1139 {
1140 MMechostr(MSKRUNTIME, "_GETmediaPlayerAudioStreams : no video stream\n");
1141 MMset(m, 0, NIL);
1142 return 0;
1143 }
1144
1145 MMpull(m);
1146
1147 for (unsigned int i = 0; i < streams.size(); i++)
1148 {
1149 std::string data = streams.at(i);
1150 Mpushstrbloc(m, (char*)data.c_str());
1151 }
1152
1153 if(MMpush(m, NIL))
1154 return MERRMEM;
1155
1156 for (unsigned int i = 0; i < streams.size(); i++)
1157 {
1158 if (MMpush(m, 2*2))
1159 return MERRMEM;
1160
1161 if (int k=MBdeftab(m))
1162 return k;
1163 }
1164
1165 return 0;
1166}
1167
1168// Callbacks
1169
1177int _CBmediaPlayerEnd(mmachine m)
1178{
1179 return OBJaddreflex(m, OBJMEDIAPLAYERSCOL, SCOL_MEDIAPLAYER_END_CB);
1180}
1181
1182int getMediaPlayerEndCb(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE ret)
1183{
1184 MediaPlayer* player = (MediaPlayer *) id;
1185
1186 if (!MediaPlayer::IsValidPlayer(player))
1187 return 0;
1188
1189 // OBJbeginreflex(mmachine, objecttype, objectptr, cbtype)
1190 if (OBJbeginreflex(m, OBJMEDIAPLAYERSCOL, SCOL_PTR player, SCOL_MEDIAPLAYER_END_CB))
1191 return 0;
1192
1193 return OBJcallreflex(m, 0);
1194}
1195
1204{
1205 return OBJaddreflex(m, OBJMEDIAPLAYERSCOL, SCOL_MEDIAPLAYER_LOADED_CB);
1206}
1207
1208int getMediaPlayerLoadedCb(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE ret)
1209{
1210 MediaPlayer* player = (MediaPlayer *)id;
1211
1212 if (!MediaPlayer::IsValidPlayer(player))
1213 return 0;
1214
1215 // OBJbeginreflex(mmachine, objecttype, objectptr, cbtype)
1216 if (OBJbeginreflex(m, OBJMEDIAPLAYERSCOL, SCOL_PTR player, SCOL_MEDIAPLAYER_LOADED_CB))
1217 return 0;
1218
1219 MMpush(m, ITOM((int)ret));
1220
1221 return OBJcallreflex(m, 1);
1222}
1223
1227// Everything inside _cond and _endcond is ignored by doxygen
1229
1230NativeDefinition videoToolkitEngine[] =
1231{
1232 { "ObjMediaPlayer", TYPTYPE, NULL, NULL },
1233 { "MP_AUDIO_8BIT_MONO", TYPVAR, "I", SCOL_TYPTYPE(MediaPlayer::AUDIO_8BIT_MONO) },
1234 { "MP_AUDIO_8BIT_STEREO", TYPVAR, "I", SCOL_TYPTYPE(MediaPlayer::AUDIO_8BIT_STEREO) },
1235 { "MP_AUDIO_16BIT_MONO", TYPVAR, "I", SCOL_TYPTYPE(MediaPlayer::AUDIO_16BIT_MONO) },
1236 { "MP_AUDIO_16BIT_STEREO", TYPVAR, "I", SCOL_TYPTYPE(MediaPlayer::AUDIO_16BIT_STEREO) },
1237 { "MP_STATE_STOPPED", TYPVAR, "I", SCOL_TYPTYPE(MediaPlayer::STOPPED) },
1238 { "MP_STATE_PLAYING", TYPVAR, "I", SCOL_TYPTYPE(MediaPlayer::PLAYING) },
1239 { "MP_STATE_PAUSED", TYPVAR, "I", SCOL_TYPTYPE(MediaPlayer::PAUSED) },
1240 { "_CRmediaPlayer", 1, "fun [Chn] ObjMediaPlayer", _CRmediaPlayer },
1241 { "_DSmediaPlayer", 1, "fun [ObjMediaPlayer] I", _DSmediaPlayer },
1242 { "_OPENmediaPlayerUrl", 2, "fun [ObjMediaPlayer S] ObjMediaPlayer", _OPENmediaPlayerUrl },
1243 { "_OPENmediaPlayerFile", 2, "fun [ObjMediaPlayer P] ObjMediaPlayer", _OPENmediaPlayerFile },
1244 { "_BLTmediaPlayer", 2, "fun [ObjMediaPlayer ObjBitmap] ObjBitmap", _BLTmediaPlayer },
1245 { "_BLTmediaPlayerBuffer", 5, "fun [ObjMediaPlayer ObjBuff I I I] ObjBuff", _BLTmediaPlayerBuffer },
1246 { "_GETmediaPlayerBuffer", 1, "fun [ObjMediaPlayer] [ObjBuff I I I]", _GETmediaPlayerBuffer },
1247 { "_PLAYmediaPlayer", 1, "fun [ObjMediaPlayer] ObjMediaPlayer", _PLAYmediaPlayer },
1248 { "_PAUSEmediaPlayer", 1, "fun [ObjMediaPlayer] ObjMediaPlayer", _PAUSEmediaPlayer },
1249 { "_STOPmediaPlayer", 1, "fun [ObjMediaPlayer] ObjMediaPlayer", _STOPmediaPlayer },
1250 { "_SETmediaPlayerSize", 3, "fun [ObjMediaPlayer I I] ObjMediaPlayer", _SETmediaPlayerSize },
1251 { "_GETmediaPlayerSize", 1, "fun [ObjMediaPlayer] [I I]", _GETmediaPlayerSize },
1252 { "_GETmediaPlayerSourceSize", 1, "fun [ObjMediaPlayer] [I I]", _GETmediaPlayerSourceSize },
1253 { "_GETmediaPlayerTime", 1, "fun [ObjMediaPlayer] I", _GETmediaPlayerTime },
1254 { "_GETmediaPlayerLength", 1, "fun [ObjMediaPlayer] I", _GETmediaPlayerLength },
1255 { "_SEEKmediaPlayer", 2, "fun [ObjMediaPlayer I] ObjMediaPlayer", _SEEKmediaPlayer },
1256 { "_SETmediaPlayerLoop", 2, "fun [ObjMediaPlayer I] ObjMediaPlayer", _SETmediaPlayerLoop },
1257 { "_SETmediaPlayerStream", 2, "fun [ObjMediaPlayer I] ObjMediaPlayer", _SETmediaPlayerStream },
1258 { "_SETmediaPlayerAudioStream", 2, "fun [ObjMediaPlayer I] ObjMediaPlayer", _SETmediaPlayerAudioStream },
1259 { "_GETmediaPlayerAudio", 1, "fun [ObjMediaPlayer] S", _GETmediaPlayerAudio },
1260 { "_SETmediaPlayerAudioFormat", 3, "fun [ObjMediaPlayer I I] ObjMediaPlayer", _SETmediaPlayerAudioFormat },
1261 { "_GETmediaPlayerHasAudio", 1, "fun [ObjMediaPlayer] I", _GETmediaPlayerHasAudio },
1262 { "_GETmediaPlayerHasVideo", 1, "fun [ObjMediaPlayer] I", _GETmediaPlayerHasVideo },
1263 { "_GETmediaPlayerState", 1, "fun [ObjMediaPlayer] I", _GETmediaPlayerState },
1264 { "_GETmediaPlayerIsLiveStream", 1, "fun [ObjMediaPlayer] I", _GETmediaPlayerIsLiveStream },
1265 { "_GETmediaPlayerIsSeekable", 1, "fun [ObjMediaPlayer] I", _GETmediaPlayerIsSeekable },
1266 { "_GETmediaPlayerAudioStreams", 1, "fun [ObjMediaPlayer] [S r1]", _GETmediaPlayerAudioStreams },
1267 { "_GETmediaPlayerVideoStreams", 1, "fun [ObjMediaPlayer] [S r1]", _GETmediaPlayerVideoStreams },
1268 { "_CBmediaPlayerEnd", 3, "fun [ObjMediaPlayer fun [ObjMediaPlayer u0] u1 u0] ObjMediaPlayer", _CBmediaPlayerEnd},
1269 { "_CBmediaPlayerLoaded", 3, "fun [ObjMediaPlayer fun [ObjMediaPlayer u0 I] u1 u0] ObjMediaPlayer", _CBmediaPlayerLoaded}
1270};
1271
1276int LoadMediaPlayerToolkit(mmachine m)
1277{
1278 int k;
1279 MMechostr(MSKDEBUG, " > Loading MediaPlayerToolkit\n");
1281
1282 MEDIAPLAYER_END_CB = OBJgetUserEvent();
1284
1285 MEDIAPLAYER_LOADED_CB = OBJgetUserEvent();
1287
1288 OBJMEDIAPLAYERSCOL = OBJregister(NBMEDIAPLAYER_CALLBACK, 0, destroyMediaPlayerObj, "OBJMEDIAPLAYERSCOL");
1289
1290 k = PKhardpak2(m, "MediaPlayerToolkitEngine", sizeof(videoToolkitEngine) / sizeof(videoToolkitEngine[0]), videoToolkitEngine);
1291 MMechostr(MSKDEBUG, " > Successfully Loaded\n\n");
1292 return k;
1293}
1294
1296{
1297 MMechostr(MSKDEBUG,"\n");
1298 MMechostr(MSKDEBUG," > Unloading MediaPlayerToolkit\n");
1299
1301 MMechostr(MSKDEBUG," > Successfully Unloaded\n\n");
1302 return 0;
1303}
1304
#define MAX_OUT_BUFFER
Definition MediaPlayer.h:46
int UnloadMediaPlayerToolkit()
int LoadMediaPlayerToolkit(mmachine m)
static cv::Mat ScolBitmapBufferToMat(void *buffer, int width, int height, int nbcomponents, bool align=true)
This class provides media playback functionality.
Definition MediaPlayer.h:53
int GetAudioThreaded(char *destBuffer, size_t length)
Get decoded audio data from the media (using the dedicated audio thread).
void OpenUrl(std::string url)
Open the media at the given URL.
int SetVideoStream(int streamIndex=-1)
Select the video stream to play from the current file.
bool HasAudio()
Check whether the player has an audio track selected and ready to play.
State GetState()
Get the current state of this player.
void Play(long startPosition=0)
Play/resume the currently loaded media.
void SetAudioFormat(AudioFormats format, int sampleRate)
Set the output format for audio.
bool IsLiveStream()
Check whether the current media is a live stream.
bool GetFrame(ObjBitmap *scolBitmap)
Get the current decoded video frame.
std::vector< std::string > ListStreams(AVMediaType type)
void Stop()
Stop media playback.
bool HasVideo()
Check whether the player has a video track selected and ready to play.
void GetSize(int &width, int &height)
Get the current size of the video (after resize)
void GetSourceSize(int &width, int &height)
Get the original size of the video (before resize)
long GetCurrentTime()
Get the current playback time of the media.
int SetAudioStream(int streamIndex=-1)
Select the audio stream to play from the current file.
void SetLoop(bool loop)
Set whether media playback should loop or not.
bool IsSeekable()
Check whether the file or stream is seekable.
void SetCurrentTime(long time)
Set the current playback time of the media.
long GetLength()
Get the length of the current media stream.
static void DeInitFFmpeg()
Free resources allocated by init. Call this when you're done with MediaPlayer.
void Pause()
Pause the current media playback.
static void InitFFmpeg()
Init FFmpeg functionalities. Call this before any other MediaPlayer functions.
void Open(std::string path)
Open the media file at the given path. If a media was already loaded, it will be replaced by the new ...
void SetSize(int width, int height)
Set the target size for the video.
static bool IsValidPlayer(MediaPlayer *player)
Check whether a media player is still valid.
int OBJMEDIAPLAYERSCOL
int _GETmediaPlayerSize(mmachine m)
_GETmediaPlayerSize : Get the current video size. Prototype: fun [ObjMediaPlayer] [I I]
int _BLTmediaPlayerBuffer(mmachine m)
_BLTmediaPlayerBuffer : This function blits the current video frame into a buffer Prototype: fun [Obj...
int _OPENmediaPlayerUrl(mmachine m)
_OPENmediaPlayerUrl : open a media player url.
int destroyMediaPlayerObj(mmachine m, SCOL_PTR_TYPE handsys, int objm)
int _SETmediaPlayerLoop(mmachine m)
_SETmediaPlayerLoop : Toggle loop playback mode. Prototype: fun [ObjMediaPlayer I] ObjMediaPlayer
int _STOPmediaPlayer(mmachine m)
_STOPmediaPlayer : Stop the video. Prototype: fun [ObjMediaPlayer] ObjMediaPlayer
int SCOL_MEDIAPLAYER_END_CB
int _GETmediaPlayerHasAudio(mmachine m)
_GETmediaPlayerHasAudio : Check whether an audio stream is selected and ready to play....
int MEDIAPLAYER_END_CB
int _OPENmediaPlayerFile(mmachine m)
_OPENmediaPlayerFile : open a media player file.
int _PAUSEmediaPlayer(mmachine m)
_PAUSEmediaPlayer : Pause the video. Prototype: fun [ObjMediaPlayer] ObjMediaPlayer
int _GETmediaPlayerSourceSize(mmachine m)
_GETmediaPlayerSourceSize : Get the size of the source video, before resize. Prototype: fun [ObjMedia...
int _CBmediaPlayerLoaded(mmachine m)
_CBmediaPlayerLoaded : This function set the Callback for loaded event of a media player Prototype: f...
int getMediaPlayerEndCb(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE ret)
int _PLAYmediaPlayer(mmachine m)
_PLAYmediaPlayer : Play / resume (if paused) the video. Prototype: fun [ObjMediaPlayer] ObjMediaPlaye...
int getMediaPlayerLoadedCb(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE ret)
int _GETmediaPlayerHasVideo(mmachine m)
_GETmediaPlayerHasVideo : Check whether a video stream is selected and ready to play....
int _SETmediaPlayerAudioStream(mmachine m)
_SETmediaPlayerAudioStream : Select an audio stream to play in the current file. Prototype: fun [ObjM...
int _GETmediaPlayerIsSeekable(mmachine m)
_GETmediaPlayerIsSeekable : Check whether the current media source supports seeking....
int _SETmediaPlayerSize(mmachine m)
_SETmediaPlayerSize : Change the video size. Prototype: fun [ObjMediaPlayer I I] ObjMediaPlayer
int _GETmediaPlayerIsLiveStream(mmachine m)
_GETmediaPlayerIsLiveStream : Check whether the current media source is a live stream or not....
MediaPlayer * getPlayerFromStack(mmachine m, int pos)
int _SETmediaPlayerStream(mmachine m)
_SETmediaPlayerStream : Select a video stream to play in the current file. Prototype: fun [ObjMediaPl...
int MEDIAPLAYER_LOADED_CB
int _GETmediaPlayerLength(mmachine m)
_GETmediaPlayerLength : Get the media player length. Prototype: fun [ObjMediaPlayer] I
int _GETmediaPlayerState(mmachine m)
_GETmediaPlayerState : Get the current playback state of a MediaPlayer. Prototype: fun [ObjMediaPlaye...
int _DSmediaPlayer(mmachine m)
_DSMediaPlayer : Delete a media player object.
int _GETmediaPlayerVideoStreams(mmachine m)
_GETmediaPlayerVideoStreams : Get a list of the current file's video streams. Prototype: fun [ObjMedi...
int SCOL_MEDIAPLAYER_LOADED_CB
int _GETmediaPlayerAudioStreams(mmachine m)
_GETmediaPlayerAudioStreams : Get a list of the current file's audio streams. Prototype: fun [ObjMedi...
int _SEEKmediaPlayer(mmachine m)
_SEEKmediaPlayer : Seek to the given position in milliseconds. Prototype: fun [ObjMediaPlayer I] ObjM...
int _CRmediaPlayer(mmachine m)
_CRMediaPlayer : Create an empty media player (no video loaded).
int _BLTmediaPlayer(mmachine m)
_BLTmediaPlayer : This function blits the current video frame into a bitmap Prototype: fun [ObjMediaP...
int NBMEDIAPLAYER_CALLBACK
int _SETmediaPlayerAudioFormat(mmachine m)
_SETmediaPlayerAudioFormat : Set the output format for audio. Prototype: fun [ObjMediaPlayer I I] Obj...
int _GETmediaPlayerAudio(mmachine m)
_GETmediaPlayerAudio : Get raw audio data from the currently playing video. Prototype: fun [ObjMediaP...
int _GETmediaPlayerTime(mmachine m)
_GETmediaPlayerTime : Get the playback position in the current video. Prototype: fun [ObjMediaPlayer]...
int _CBmediaPlayerEnd(mmachine m)
_CBmediaPlayerEnd : This function set the Callback for end event of a media player content Prototype:...
int _GETmediaPlayerBuffer(mmachine m)
_GETmediaPlayerBuffer : This function blits the current video frame into a buffer Prototype: fun [Obj...