1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
|
/*
Copyright (C) 2000-2002 Stefan Westerfeld
stefan@space.twc.de
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "virtualports.h"
#include "startupmanager.h"
#include "gslschedule.h"
#include "debug.h"
#include "asyncschedule.h"
#include "audiosubsys.h"
#include <gsl/gslcommon.h>
#include <gsl/gslengine.h>
#include <algorithm>
#include <stdio.h>
#include <iostream>
#include <stack>
/* HACK */
class GslMainLoop {
protected:
std::list<GslClass *> freeClassList;
public:
GslEngineLoop loop;
static bool waitOnTransNeedData;
static bool gslDataCalculated;
/* static check function */
static gboolean gslCheck(gpointer /* data */, guint /* n_values */,
glong* /* timeout_p */,
guint /* n_fds */, const GPollFD* /* fds */,
gboolean /* revents_filled */)
{
return waitOnTransNeedData;
}
/* mainloop integration: initialize (called to get initial loop setup) */
void initialize()
{
gsl_transact(gsl_job_add_poll (gslCheck, 0, 0, 0, 0), 0);
gsl_engine_prepare(&loop);
for(unsigned int i = 0; i != loop.n_fds; i++)
{
printf("TODO: engine fd %d\n",i);
}
}
/* mainloop integration: process (TODO - should be called by IOManager) */
void process()
{
printf("TODO: mainloop wrapper for fd watches\n");
if(gsl_engine_check(&loop))
gsl_engine_dispatch();
}
/* wait for a transaction */
void waitOnTrans()
{
arts_return_if_fail(waitOnTransNeedData == false);
gsl_engine_wait_on_trans();
}
/* make the engine calculate something */
void run()
{
waitOnTransNeedData = true;
gslDataCalculated = false;
while(!gslDataCalculated && gsl_engine_check(&loop))
gsl_engine_dispatch();
gslDataCalculated = false;
waitOnTransNeedData = false;
if(!freeClassList.empty())
{
/*
* make sure that all transactions that are still pending
* get finished (especially important in threaded case,
* since an entry in the free list doesn't necessarily
* mean that the module has entierly been freed)
*/
waitOnTrans();
std::list<GslClass *>::iterator fi;
for(fi = freeClassList.begin(); fi != freeClassList.end(); fi++)
free(*fi);
freeClassList.clear();
}
}
void freeGslClass(GslClass *klass)
{
freeClassList.push_back(klass);
}
} gslMainLoop;
bool GslMainLoop::waitOnTransNeedData = false;
bool GslMainLoop::gslDataCalculated = false;
namespace Arts { extern void *gslGlobalMutexTable; }
using namespace std;
using namespace Arts;
// ----------- Port -----------
Port::Port(const string& name, void *ptr, long flags, StdScheduleNode* parent)
: _name(name), _ptr(ptr), _flags((AttributeType)flags),
parent(parent), _dynamicPort(false)
{
_vport = new VPort(this);
}
Port::~Port()
{
if(_vport)
delete _vport;
}
AttributeType Port::flags()
{
return _flags;
}
string Port::name()
{
return _name;
}
ASyncPort *Port::asyncPort()
{
return 0;
}
AudioPort *Port::audioPort()
{
return 0;
}
void Port::addAutoDisconnect(Port *source)
{
autoDisconnect.push_back(source);
source->autoDisconnect.push_back(this);
}
void Port::removeAutoDisconnect(Port *source)
{
std::list<Port *>::iterator adi;
// remove our autodisconnection entry for source port
adi = find(autoDisconnect.begin(),autoDisconnect.end(),source);
assert(adi != autoDisconnect.end());
autoDisconnect.erase(adi);
// remove the source port autodisconnection entry to us
adi=find(source->autoDisconnect.begin(),source->autoDisconnect.end(),this);
assert(adi != source->autoDisconnect.end());
source->autoDisconnect.erase(adi);
}
void Port::disconnectAll()
{
if(_vport)
delete _vport;
_vport = 0;
assert(autoDisconnect.empty());
while(!autoDisconnect.empty())
{
Port *other = *autoDisconnect.begin();
// syntax is disconnect(source)
if(_flags & streamIn)
// if we're incoming, other port is source
vport()->disconnect(other->vport());
else
// if we're outgoing, we're the source
other->vport()->disconnect(this->vport());
}
}
void Port::setPtr(void *ptr)
{
_ptr = ptr;
}
// ------- AudioPort ---------
AudioPort::AudioPort(const string& name,
void *ptr, long flags,StdScheduleNode *parent)
: Port(name,ptr,flags,parent)
{
destcount = 0;
sourcemodule = 0;
source = 0;
gslIsConstant = false;
}
AudioPort::~AudioPort()
{
//
}
AudioPort *AudioPort::audioPort()
{
return this;
}
void AudioPort::setFloatValue(float f)
{
gslIsConstant = true;
gslConstantValue = f;
parent->_connectionCountChanged = true;
}
void AudioPort::connect(Port *psource)
{
if (source) return; // Error, should not happen (See BR70028)
source = psource->audioPort();
assert(source);
addAutoDisconnect(psource);
source->parent->_connectionCountChanged = parent->_connectionCountChanged = true;
source->destcount++;
sourcemodule = source->parent;
// GSL connect
GslTrans *trans = gsl_trans_open();
gsl_trans_add(trans, gsl_job_connect(source->parent->gslModule,
source->gslEngineChannel,
parent->gslModule,
gslEngineChannel));
gsl_trans_commit(trans);
}
void AudioPort::disconnect(Port *psource)
{
if (!source || source != psource->audioPort()) return; // Error, should not happen (See BR70028)
assert(source);
assert(source == psource->audioPort());
removeAutoDisconnect(psource);
assert(sourcemodule == source->parent);
sourcemodule = 0;
source->parent->_connectionCountChanged = parent->_connectionCountChanged = true;
source->destcount--;
source = 0;
// GSL disconnect
GslTrans *trans = gsl_trans_open();
gsl_trans_add(trans, gsl_job_disconnect(parent->gslModule,
gslEngineChannel));
gsl_trans_commit(trans);
}
// --------- MultiPort ----------
MultiPort::MultiPort(const string& name,
void *ptr, long flags,StdScheduleNode *parent)
: Port(name,ptr,flags,parent)
{
conns = 0;
nextID = 0;
initConns();
}
MultiPort::~MultiPort()
{
if(conns)
{
delete[] conns;
conns = 0;
}
}
void MultiPort::initConns()
{
if(conns != 0) delete[] conns;
conns = new float_ptr[parts.size() + 1];
conns[parts.size()] = (float *)0;
*(float ***)_ptr = conns;
long n = 0;
std::list<Part>::iterator i;
for(i = parts.begin();i != parts.end(); i++)
{
AudioPort *p = i->dest;
p->setPtr((void *)&conns[n++]);
}
}
void MultiPort::connect(Port *port)
{
AudioPort *dport;
char sid[20];
sprintf(sid,"%ld",nextID++);
addAutoDisconnect(port);
dport = new AudioPort("_"+_name+string(sid),0,streamIn,parent);
Part part;
part.src = (AudioPort *)port;
part.dest = dport;
parts.push_back(part);
initConns();
parent->addDynamicPort(dport);
dport->vport()->connect(port->vport());
}
void MultiPort::disconnect(Port *sport)
{
AudioPort *port = (AudioPort *)sport;
removeAutoDisconnect(sport);
std::list<Part>::iterator i;
for(i = parts.begin(); i != parts.end(); i++)
{
if(i->src == port)
{
AudioPort *dport = i->dest;
parts.erase(i);
initConns();
dport->vport()->disconnect(port->vport());
parent->removeDynamicPort(dport);
delete dport;
return;
}
}
}
// -------- StdScheduleNode ---------
void StdScheduleNode::freeConn()
{
if(inConn)
{
delete[] inConn;
inConn = 0;
}
if(outConn)
{
delete[] outConn;
outConn = 0;
}
inConnCount = outConnCount = 0;
if(gslModule)
{
gsl_transact(gsl_job_discard(gslModule),0);
gslModule = 0;
gslRunning = false;
}
}
void StdScheduleNode::gslProcess(GslModule *module, guint n_values)
{
StdScheduleNode *node = (StdScheduleNode *)module->user_data;
if(!node->running) /* FIXME: need reasonable suspend in the engine */
return;
arts_return_if_fail(node->module != 0);
GslMainLoop::gslDataCalculated = true;
unsigned long j;
for(j=0;j<node->inConnCount;j++)
{
if(node->inConn[j]->gslIsConstant)
*((float **)node->inConn[j]->_ptr) =
gsl_engine_const_values(node->inConn[j]->gslConstantValue);
else
*((float **)node->inConn[j]->_ptr) = const_cast<float *>(module->istreams[j].values);
}
for(j=0;j<node->outConnCount;j++)
*((float **)node->outConn[j]->_ptr) = module->ostreams[j].values;
node->module->calculateBlock(n_values);
}
static void gslModuleFree(gpointer /* data */, const GslClass *klass)
{
gslMainLoop.freeGslClass(const_cast<GslClass *>(klass));
}
void StdScheduleNode::rebuildConn()
{
std::list<Port *>::iterator i;
freeConn();
inConnCount = outConnCount = 0;
inConn = new AudioPort_ptr[ports.size()];
outConn = new AudioPort_ptr[ports.size()];
for(i=ports.begin();i != ports.end();i++)
{
AudioPort *p = (*i)->audioPort();
if(p)
{
if(p->flags() & streamIn)
{
p->gslEngineChannel = inConnCount;
inConn[inConnCount++] = p;
}
if(p->flags() & streamOut)
{
p->gslEngineChannel = outConnCount;
outConn[outConnCount++] = p;
}
}
}
/* create GSL node */
GslClass *gslClass = (GslClass *)calloc(sizeof(GslClass),1);
gslClass->n_istreams = inConnCount;
gslClass->n_ostreams = outConnCount;
gslClass->process = gslProcess;
gslClass->free = gslModuleFree;
gslModule = gsl_module_new (gslClass, (StdScheduleNode *)this);
GslTrans *trans = gsl_trans_open();
gsl_trans_add(trans,gsl_job_integrate(gslModule));
gsl_trans_add(trans,gsl_job_set_consumer(gslModule, running));
gslRunning = running;
/* since destroying the old module and creating a new one will destroy
* all the connections, we need to restore them here
*/
unsigned int c;
for(c = 0; c < inConnCount; c++)
{
if(inConn[c]->source)
{
gsl_trans_add(trans,
gsl_job_connect(inConn[c]->source->parent->gslModule,
inConn[c]->source->gslEngineChannel,
inConn[c]->parent->gslModule,
inConn[c]->gslEngineChannel));
}
}
for(c = 0; c < outConnCount; c++)
{
std::list<Port *>::iterator ci;
for(ci = outConn[c]->autoDisconnect.begin();
ci != outConn[c]->autoDisconnect.end(); ci++)
{
AudioPort *dest = (*ci)->audioPort();
if( dest )
{
gsl_trans_add(trans,
gsl_job_connect(outConn[c]->parent->gslModule,
outConn[c]->gslEngineChannel,
dest->parent->gslModule,
dest->gslEngineChannel));
}
else
{
arts_debug( "no audio port: %s for %s", ( *ci )->name().c_str(), _object->_interfaceName().c_str() );
}
}
}
gsl_trans_commit(trans);
}
Object_skel *StdScheduleNode::object()
{
return _object;
}
void *StdScheduleNode::cast(const string &target)
{
if(target == "StdScheduleNode") return (StdScheduleNode *)this;
return 0;
}
void StdScheduleNode::accessModule()
{
if(module) return;
module = (SynthModule_base *)_object->_cast(Arts::SynthModule_base::_IID);
if(!module)
arts_warning("Error using interface %s in the flowsystem: only objects"
" implementing Arts::SynthModule should carry streams.",
_object->_interfaceName().c_str());
}
StdScheduleNode::StdScheduleNode(Object_skel *object, StdFlowSystem *flowSystem) : ScheduleNode(object)
{
_object = object;
this->flowSystem = flowSystem;
running = false;
suspended = false;
module = 0;
gslModule = 0;
gslRunning = false;
queryInitStreamFunc = 0;
inConn = outConn = 0;
inConnCount = outConnCount = 0;
}
StdScheduleNode::~StdScheduleNode()
{
/* stop module if still running */
if(running) stop();
/* disconnect all ports */
stack<Port *> disconnect_stack;
/*
* we must be a bit careful here, as dynamic ports (which are created
* for connections by MultiPorts) will suddenly start disappearing, so
* we better make a copy of those ports that will stay, and disconnect
* them then
*/
std::list<Port *>::iterator i;
for(i=ports.begin();i != ports.end();i++)
{
if(!(*i)->dynamicPort()) disconnect_stack.push(*i);
}
while(!disconnect_stack.empty())
{
disconnect_stack.top()->disconnectAll();
disconnect_stack.pop();
}
/* free them */
for(i=ports.begin();i != ports.end();i++)
delete (*i);
ports.clear();
freeConn();
}
void StdScheduleNode::initStream(const string& name, void *ptr, long flags)
{
if(flags == -1)
{
queryInitStreamFunc = (QueryInitStreamFunc)ptr;
}
else if(flags & streamAsync)
{
ports.push_back(new ASyncPort(name,ptr,flags,this));
}
else if(flags & streamMulti)
{
ports.push_back(new MultiPort(name,ptr,flags,this));
}
else
{
ports.push_back(new AudioPort(name,ptr,flags,this));
}
// TODO: maybe initialize a bit later
rebuildConn();
}
void StdScheduleNode::addDynamicPort(Port *port)
{
port->setDynamicPort();
ports.push_back(port);
rebuildConn();
}
void StdScheduleNode::removeDynamicPort(Port *port)
{
std::list<Port *>::iterator i;
for(i=ports.begin();i!=ports.end();i++)
{
Port *p = *i;
if(p->name() == port->name())
{
ports.erase(i);
rebuildConn();
return;
}
}
}
void StdScheduleNode::start()
{
assert(!running);
running = true;
//cout << "start" << endl;
accessModule();
module->streamInit();
module->streamStart();
flowSystem->startedChanged();
}
void StdScheduleNode::stop()
{
assert(running);
running = false;
accessModule();
module->streamEnd();
flowSystem->startedChanged();
}
void StdScheduleNode::requireFlow()
{
// cout << "rf" << module->_interfaceName() << endl;
flowSystem->updateStarted();
gslMainLoop.run();
}
AutoSuspendState StdScheduleNode::suspendable()
{
if(running) {
accessModule();
return module->autoSuspend();
}
// if its not running, who cares?
return asSuspend;
}
void StdScheduleNode::suspend()
{
if(running) {
accessModule();
suspended = true;
if((module->autoSuspend() & asSuspendMask) == asSuspendStop) stop();
}
}
void StdScheduleNode::restart()
{
if(suspended) {
accessModule();
suspended = false;
if(!running && (module->autoSuspend() & asSuspendMask) == asSuspendStop) start();
}
}
Port *StdScheduleNode::findPort(const string& name)
{
std::list<Port *>::iterator i;
for(i=ports.begin();i!=ports.end();i++)
{
Port *p = *i;
if(p->name() == name) return p;
}
if(queryInitStreamFunc)
{
if(queryInitStreamFunc(_object,name))
{
for(i=ports.begin();i!=ports.end();i++)
{
Port *p = *i;
if(p->name() == name) return p;
}
}
}
return 0;
}
void StdScheduleNode::virtualize(const std::string& port,
ScheduleNode *implNode,
const std::string& implPort)
{
StdScheduleNode *impl=(StdScheduleNode *)implNode->cast("StdScheduleNode");
if(impl)
{
Port *p1 = findPort(port);
Port *p2 = impl->findPort(implPort);
assert(p1);
assert(p2);
p1->vport()->virtualize(p2->vport());
}
}
void StdScheduleNode::devirtualize(const std::string& port,
ScheduleNode *implNode,
const std::string& implPort)
{
StdScheduleNode *impl=(StdScheduleNode *)implNode->cast("StdScheduleNode");
if(impl)
{
Port *p1 = findPort(port);
Port *p2 = impl->findPort(implPort);
p1->vport()->devirtualize(p2->vport());
}
}
void StdScheduleNode::connect(const string& port, ScheduleNode *dest,
const string& destport)
{
RemoteScheduleNode *rsn = dest->remoteScheduleNode();
if(rsn)
{
// RemoteScheduleNodes know better how to connect remotely
rsn->connect(destport,this,port);
return;
}
flowSystem->restart();
Port *p1 = findPort(port);
Port *p2 = ((StdScheduleNode *)dest)->findPort(destport);
if(p1 && p2)
{
if((p1->flags() & streamIn) && (p2->flags() & streamOut))
{
p1->vport()->connect(p2->vport());
}
else if((p2->flags() & streamIn) && (p1->flags() & streamOut))
{
p2->vport()->connect(p1->vport());
}
}
}
void StdScheduleNode::disconnect(const string& port, ScheduleNode *dest,
const string& destport)
{
RemoteScheduleNode *rsn = dest->remoteScheduleNode();
if(rsn)
{
// RemoteScheduleNodes know better how to disconnect remotely
rsn->disconnect(destport,this,port);
return;
}
flowSystem->restart();
Port *p1 = findPort(port);
Port *p2 = ((StdScheduleNode *)dest)->findPort(destport);
if(p1 && p2)
{
if((p1->flags() & streamIn) && (p2->flags() & streamOut))
{
p1->vport()->disconnect(p2->vport());
}
else if((p2->flags() & streamIn) && (p1->flags() & streamOut))
{
p2->vport()->disconnect(p1->vport());
}
}
}
AttributeType StdScheduleNode::queryFlags(const std::string& port)
{
arts_debug("findPort(%s)", port.c_str());
arts_debug("have %ld ports", ports.size());
Port *p1 = findPort(port);
arts_debug("done");
if(p1)
{
arts_debug("result %d",(long)p1->flags());
return p1->flags();
}
arts_debug("failed");
return (AttributeType)0;
}
void StdScheduleNode::setFloatValue(const string& port, float value)
{
AudioPort *p = findPort(port)->audioPort();
if(p) {
p->vport()->setFloatValue(value);
} else {
assert(false);
}
}
unsigned long StdScheduleNode::inputConnectionCount(const string& port)
{
unsigned long result = 0;
unsigned int c;
for(c = 0; c < inConnCount; c++)
{
if(inConn[c]->name() == port)
{
if(inConn[c]->source || inConn[c]->gslIsConstant)
result++;
}
}
return result;
}
unsigned long StdScheduleNode::outputConnectionCount(const string& port)
{
unsigned long result = 0;
unsigned int c;
for(c = 0; c < outConnCount; c++)
{
if(outConn[c]->name() == port)
result += outConn[c]->destcount;
}
return result;
}
StdFlowSystem::StdFlowSystem()
{
_suspended = false;
needUpdateStarted = false;
/* TODO: correct parameters */
static bool gsl_is_initialized = false;
if(!gsl_is_initialized)
{
GslConfigValue values[3] = {
{ "wave_chunk_padding", 8 },
{ "dcache_block_size", 4000, },
{ 0, 0 }
};
gsl_is_initialized = true;
if (!g_thread_supported ())
g_thread_init(0);
gsl_init(values, (GslMutexTable *)gslGlobalMutexTable);
/*
* FIXME: both of these are really supposed to be tunable
* - the 512 because of low-latency apps, where calculating smaller
* block sizes might be necessary
* - the 44100 because of the obvious reason, that not every artsd
* is running at that rate
*/
gsl_engine_init(false, 512, 44100, /* subsamplemask */ 63);
if(gslGlobalMutexTable)
arts_debug("gsl: using Unix98 pthreads directly for mutexes and conditions");
/*gsl_engine_debug_enable(GslEngineDebugLevel(GSL_ENGINE_DEBUG_JOBS | GSL_ENGINE_DEBUG_SCHED));*/
}
gslMainLoop.initialize();
}
ScheduleNode *StdFlowSystem::addObject(Object_skel *object)
{
// do not add new modules when being in suspended state
restart();
StdScheduleNode *node = new StdScheduleNode(object,this);
nodes.push_back(node);
return node;
}
void StdFlowSystem::removeObject(ScheduleNode *node)
{
StdScheduleNode *xnode = (StdScheduleNode *)node->cast("StdScheduleNode");
assert(xnode);
nodes.remove(xnode);
delete xnode;
}
bool StdFlowSystem::suspended()
{
return _suspended;
}
bool StdFlowSystem::suspendable()
{
/*
* What it does:
* -------------
*
* The suspension algorithm will first divide the graph of modules into
* subgraphs of interconnected modules. A subgraph is suspendable if
* all of its modules are suspendable and the subgraph does not contain
* producer(s) and consumer(s) at the same time.
*
* Finally, our module graph is suspendable if all its subgraphs are.
*
* How it is implemented:
* ----------------------
*
* For efficiency reasons, both steps are merged together. First all
* modules will be marked as unseen. Then a module is picked and
* all modules that it connects to are recursively added to the
* subgraph.
*/
/*
* initialization: no nodes are seen
*/
std::list<StdScheduleNode *>::iterator i;
for (i = nodes.begin(); i != nodes.end(); i++)
{
StdScheduleNode *node = *i;
node->suspendTag = false;
}
stack<StdScheduleNode*> todo;
for(i = nodes.begin(); i != nodes.end(); i++)
{
bool haveConsumer = false;
bool haveProducer = false;
/*
* examine the subgraph consisting of all nodes connected to (*i)
* (only will do anything if suspendTag is not already set)
*/
todo.push(*i);
do
{
StdScheduleNode *node = todo.top();
todo.pop();
if(!node->suspendTag)
{
node->suspendTag = true; // never examine this node again
switch (node->suspendable())
{
case asNoSuspend|asProducer:
case asNoSuspend|asConsumer:
case asNoSuspend:
return false;
break;
case asSuspend:
case asSuspendStop:
/* nothing */
break;
case asSuspend|asProducer:
case asSuspendStop|asProducer:
if(haveConsumer)
return false;
else
haveProducer = true;
break;
case asSuspend|asConsumer:
case asSuspendStop|asConsumer:
if(haveProducer)
return false;
else
haveConsumer = true;
break;
default:
arts_fatal("bad suspend value %d", node->suspendable());
}
unsigned int c;
for(c = 0; c < node->inConnCount; c++)
{
if(node->inConn[c]->source)
todo.push(node->inConn[c]->source->parent);
}
for(c = 0; c < node->outConnCount; c++)
{
std::list<Port *>::iterator ci;
for(ci = node->outConn[c]->autoDisconnect.begin();
ci != node->outConn[c]->autoDisconnect.end(); ci++)
{
AudioPort *dest = (*ci)->audioPort();
if(dest)
todo.push(dest->parent);
}
}
}
} while(!todo.empty());
}
return true;
}
void StdFlowSystem::suspend()
{
if(!_suspended)
{
std::list<StdScheduleNode *>::iterator i;
for(i = nodes.begin();i != nodes.end();i++)
{
StdScheduleNode *node = *i;
node->suspend();
}
_suspended = true;
}
}
void StdFlowSystem::restart()
{
if(_suspended)
{
std::list<StdScheduleNode *>::iterator i;
for(i = nodes.begin();i != nodes.end();i++)
{
StdScheduleNode *node = *i;
node->restart();
}
_suspended = false;
}
}
/* remote accessibility */
void StdFlowSystem::startObject(Object node)
{
StdScheduleNode *sn =
(StdScheduleNode *)node._node()->cast("StdScheduleNode");
sn->start();
}
void StdFlowSystem::stopObject(Object node)
{
StdScheduleNode *sn =
(StdScheduleNode *)node._node()->cast("StdScheduleNode");
sn->stop();
}
void StdFlowSystem::connectObject(Object sourceObject,const string& sourcePort,
Object destObject, const std::string& destPort)
{
arts_debug("connect port %s to %s", sourcePort.c_str(), destPort.c_str());
StdScheduleNode *sn =
(StdScheduleNode *)sourceObject._node()->cast("StdScheduleNode");
assert(sn);
Port *port = sn->findPort(sourcePort);
assert(port);
StdScheduleNode *destsn =
(StdScheduleNode *)destObject._node()->cast("StdScheduleNode");
if(destsn)
{
sn->connect(sourcePort,destsn,destPort);
return;
}
ASyncPort *ap = port->asyncPort();
if(ap)
{
FlowSystemSender sender;
FlowSystemReceiver receiver;
FlowSystem remoteFs;
string dest = destObject.toString() + ":" + destPort;
ASyncNetSend *netsend = new ASyncNetSend(ap, dest);
sender = FlowSystemSender::_from_base(netsend); // don't release netsend
remoteFs = destObject._flowSystem();
receiver = remoteFs.createReceiver(destObject, destPort, sender);
netsend->setReceiver(receiver);
arts_debug("connected an asyncnetsend");
}
}
void StdFlowSystem::disconnectObject(Object sourceObject,
const string& sourcePort, Object destObject, const std::string& destPort)
{
arts_debug("disconnect port %s and %s",sourcePort.c_str(),destPort.c_str());
StdScheduleNode *sn =
(StdScheduleNode *)sourceObject._node()->cast("StdScheduleNode");
assert(sn);
Port *port = sn->findPort(sourcePort);
assert(port);
StdScheduleNode *destsn =
(StdScheduleNode *)destObject._node()->cast("StdScheduleNode");
if(destsn)
{
sn->disconnect(sourcePort,destsn,destPort);
return;
}
ASyncPort *ap = port->asyncPort();
if(ap)
{
string dest = destObject.toString() + ":" + destPort;
ap->disconnectRemote(dest);
arts_debug("disconnected an asyncnetsend");
}
}
AttributeType StdFlowSystem::queryFlags(Object node, const std::string& port)
{
StdScheduleNode *sn =
(StdScheduleNode *)node._node()->cast("StdScheduleNode");
assert(sn);
return sn->queryFlags(port);
}
void StdFlowSystem::setFloatValue(Object node, const std::string& port,
float value)
{
StdScheduleNode *sn =
(StdScheduleNode *)node._node()->cast("StdScheduleNode");
assert(sn);
sn->setFloatValue(port,value);
}
FlowSystemReceiver StdFlowSystem::createReceiver(Object object,
const string &port, FlowSystemSender sender)
{
StdScheduleNode *sn =
(StdScheduleNode *)object._node()->cast("StdScheduleNode");
Port *p = sn->findPort(port);
assert(p);
ASyncPort *ap = p->asyncPort();
if(ap)
{
arts_debug("creating packet receiver");
return FlowSystemReceiver::_from_base(new ASyncNetReceive(ap, sender));
}
return FlowSystemReceiver::null();
}
void StdFlowSystem::updateStarted()
{
if(!needUpdateStarted)
return;
needUpdateStarted = false;
std::list<StdScheduleNode*>::iterator ni;
GslTrans *trans = 0;
for(ni = nodes.begin(); ni != nodes.end(); ni++)
{
StdScheduleNode *node = *ni;
if(node->running != node->gslRunning)
{
if(!trans)
trans = gsl_trans_open();
gsl_trans_add(trans, gsl_job_set_consumer(node->gslModule, node->running));
node->gslRunning = node->running;
}
}
if(trans)
gsl_trans_commit(trans);
}
void StdFlowSystem::startedChanged()
{
needUpdateStarted = true;
}
// hacked initialization of Dispatcher::the()->flowSystem ;)
namespace Arts {
static class SetFlowSystem : public StartupClass {
FlowSystem_impl *fs;
public:
void startup()
{
fs = new StdFlowSystem;
Dispatcher::the()->setFlowSystem(fs);
}
void shutdown()
{
fs->_release();
}
} sfs;
}
|