* avoid launching one thread for each rescan request.

* erase the defaults settings file when writing it, previous content still existed at the end of the file.
* don't save an empty defaults settings file when node information is not available, this should mean the add on server is already gone.
* added some debug output.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36452 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval
2010-04-24 16:56:22 +00:00
parent 1f4cc99a2a
commit bbfd31096c
2 changed files with 100 additions and 61 deletions
+98 -61
View File
@@ -54,7 +54,9 @@ DefaultManager::DefaultManager()
fSystemTimeSource(-1), fSystemTimeSource(-1),
fTimeSource(-1), fTimeSource(-1),
fAudioMixer(-1), fAudioMixer(-1),
fPhysicalAudioOutInputID(0) fPhysicalAudioOutInputID(0),
fThreadId(-1),
fRescanRequested(0)
{ {
strcpy(fPhysicalAudioOutInputName, "default"); strcpy(fPhysicalAudioOutInputName, "default");
fBeginHeader[0] = 0xab00150b; fBeginHeader[0] = 0xab00150b;
@@ -92,23 +94,39 @@ DefaultManager::LoadState()
TRACE("0x%08lx %ld\n", fBeginHeader[0], fBeginHeader[0]); TRACE("0x%08lx %ld\n", fBeginHeader[0], fBeginHeader[0]);
TRACE("0x%08lx %ld\n", fBeginHeader[1], fBeginHeader[1]); TRACE("0x%08lx %ld\n", fBeginHeader[1], fBeginHeader[1]);
TRACE("0x%08lx %ld\n", fBeginHeader[2], fBeginHeader[2]); TRACE("0x%08lx %ld\n", fBeginHeader[2], fBeginHeader[2]);
if (file.Read(&category_count, sizeof(uint32)) < (int32)sizeof(uint32)) if (file.Read(&category_count, sizeof(uint32)) < (int32)sizeof(uint32)) {
fprintf(stderr,
"DefaultManager::LoadState() failed to read category_count\n");
return B_ERROR; return B_ERROR;
}
TRACE("DefaultManager::LoadState() category_count %ld\n", category_count);
while (category_count--) { while (category_count--) {
BMessage settings; BMessage settings;
uint32 msg_header; uint32 msg_header;
uint32 default_type; uint32 default_type;
if (file.Read(&msg_header, sizeof(uint32)) < (int32)sizeof(uint32)) if (file.Read(&msg_header, sizeof(uint32)) < (int32)sizeof(uint32)) {
fprintf(stderr,
"DefaultManager::LoadState() failed to read msg_header\n");
return B_ERROR; return B_ERROR;
if (file.Read(&default_type, sizeof(uint32)) < (int32)sizeof(uint32)) }
if (file.Read(&default_type, sizeof(uint32)) < (int32)sizeof(uint32)) {
fprintf(stderr,
"DefaultManager::LoadState() failed to read default_type\n");
return B_ERROR; return B_ERROR;
}
if (settings.Unflatten(&file) == B_OK) { if (settings.Unflatten(&file) == B_OK) {
settings.PrintToStream(); settings.PrintToStream();
fMsgList.AddItem(new BMessage(settings)); fMsgList.AddItem(new BMessage(settings));
} } else
fprintf(stderr, "DefaultManager::LoadState() failed to unflatten\n");
} }
if (file.Read(fEndHeader, sizeof(uint32)*3) < (int32)sizeof(uint32)*3) if (file.Read(fEndHeader, sizeof(uint32)*3) < (int32)sizeof(uint32)*3) {
fprintf(stderr,
"DefaultManager::LoadState() failed to read fEndHeader\n");
return B_ERROR; return B_ERROR;
}
TRACE("LoadState returns B_OK\n");
return B_OK; return B_OK;
} }
@@ -126,17 +144,14 @@ DefaultManager::SaveState(NodeManager *node_manager)
if ((err = create_directory(path.Path(), 0755)) != B_OK) if ((err = create_directory(path.Path(), 0755)) != B_OK)
return err; return err;
path.Append(kDefaultManagerSettingsFile); path.Append(kDefaultManagerSettingsFile);
BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE);
uint32 default_types[] = {kMsgTypeVideoIn, kMsgTypeVideoOut, uint32 default_types[] = {kMsgTypeVideoIn, kMsgTypeVideoOut,
kMsgTypeAudioIn, kMsgTypeAudioOut}; kMsgTypeAudioIn, kMsgTypeAudioOut};
uint32 media_node_ids[] = {fPhysicalVideoIn, fPhysicalVideoOut, media_node_id media_node_ids[] = {fPhysicalVideoIn, fPhysicalVideoOut,
fPhysicalAudioIn, fPhysicalAudioOut}; fPhysicalAudioIn, fPhysicalAudioOut};
for (uint32 i=0; i<sizeof(default_types)/sizeof(default_types[0]); i++) { for (uint32 i = 0; i < sizeof(default_types) / sizeof(default_types[0]);
BMessage *settings = new BMessage(); i++) {
settings->AddInt32(kDefaultManagerType, default_types[i]);
// we call the node manager to have more infos about nodes // we call the node manager to have more infos about nodes
dormant_node_info info; dormant_node_info info;
media_node node; media_node node;
@@ -146,9 +161,16 @@ DefaultManager::SaveState(NodeManager *node_manager)
|| node_manager->GetDormantNodeInfo(node, &info) != B_OK || node_manager->GetDormantNodeInfo(node, &info) != B_OK
|| node_manager->ReleaseNodeReference(media_node_ids[i], || node_manager->ReleaseNodeReference(media_node_ids[i],
be_app->Team()) != B_OK be_app->Team()) != B_OK
|| node_manager->GetAddOnRef(info.addon, &ref) != B_OK) || node_manager->GetAddOnRef(info.addon, &ref) != B_OK) {
if (media_node_ids[i] != -1) {
// failed to get node info thus just return
return B_ERROR;
}
continue; continue;
}
BMessage *settings = new BMessage();
settings->AddInt32(kDefaultManagerType, default_types[i]);
BPath path(&ref); BPath path(&ref);
settings->AddInt32(kDefaultManagerAddon, info.addon); settings->AddInt32(kDefaultManagerAddon, info.addon);
settings->AddInt32(kDefaultManagerFlavorId, info.flavor_id); settings->AddInt32(kDefaultManagerFlavorId, info.flavor_id);
@@ -161,6 +183,8 @@ DefaultManager::SaveState(NodeManager *node_manager)
TRACE("message %s added\n", info.name); TRACE("message %s added\n", info.name);
} }
BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if (file.Write(fBeginHeader, sizeof(uint32)*3) < (int32)sizeof(uint32)*3) if (file.Write(fBeginHeader, sizeof(uint32)*3) < (int32)sizeof(uint32)*3)
return B_ERROR; return B_ERROR;
int32 category_count = list.CountItems(); int32 category_count = list.CountItems();
@@ -304,9 +328,13 @@ DefaultManager::Get(media_node_id *nodeid, char *input_name, int32 *inputid,
status_t status_t
DefaultManager::Rescan() DefaultManager::Rescan()
{ {
thread_id fThreadId = spawn_thread(rescan_thread, "rescan defaults", atomic_add(&fRescanRequested, 1);
B_NORMAL_PRIORITY - 2, this); if (fThreadId < 0) {
resume_thread(fThreadId); fThreadId = spawn_thread(rescan_thread, "rescan defaults",
B_NORMAL_PRIORITY - 2, this);
resume_thread(fThreadId);
}
return B_OK; return B_OK;
} }
@@ -322,51 +350,60 @@ DefaultManager::rescan_thread(void *arg)
void void
DefaultManager::RescanThread() DefaultManager::RescanThread()
{ {
printf("DefaultManager::RescanThread() enter\n"); TRACE("DefaultManager::RescanThread() enter\n");
// We do not search for the system time source, while (atomic_and(&fRescanRequested, 0) != 0) {
// it should already exist // We do not search for the system time source,
ASSERT(fSystemTimeSource != -1); // it should already exist
ASSERT(fSystemTimeSource != -1);
if (fPhysicalVideoOut == -1) {
FindPhysical(&fPhysicalVideoOut, kMsgTypeVideoOut, false, if (fPhysicalVideoOut == -1) {
B_MEDIA_RAW_VIDEO); FindPhysical(&fPhysicalVideoOut, kMsgTypeVideoOut, false,
FindPhysical(&fPhysicalVideoOut, kMsgTypeVideoOut, false, B_MEDIA_RAW_VIDEO);
B_MEDIA_ENCODED_VIDEO); FindPhysical(&fPhysicalVideoOut, kMsgTypeVideoOut, false,
B_MEDIA_ENCODED_VIDEO);
}
if (fPhysicalVideoIn == -1) {
FindPhysical(&fPhysicalVideoIn, kMsgTypeVideoIn, true,
B_MEDIA_RAW_VIDEO);
FindPhysical(&fPhysicalVideoIn, kMsgTypeVideoIn, true,
B_MEDIA_ENCODED_VIDEO);
}
if (fPhysicalAudioOut == -1)
FindPhysical(&fPhysicalAudioOut, kMsgTypeAudioOut, false,
B_MEDIA_RAW_AUDIO);
if (fPhysicalAudioIn == -1)
FindPhysical(&fPhysicalAudioIn, kMsgTypeAudioIn, true,
B_MEDIA_RAW_AUDIO);
if (fAudioMixer == -1)
FindAudioMixer();
// The normal time source is searched for after the
// Physical Audio Out has been created.
if (fTimeSource == -1)
FindTimeSource();
// Connect the mixer and physical audio out (soundcard)
if (!fMixerConnected && fAudioMixer != -1 && fPhysicalAudioOut != -1) {
fMixerConnected = B_OK == ConnectMixerToOutput();
if (!fMixerConnected)
ERROR("DefaultManager: failed to connect mixer and"
"soundcard\n");
} else {
ERROR("DefaultManager: Did not try to connect mixer and"
"soundcard\n");
}
if (fMixerConnected) {
add_on_server_rescan_finished_notify_command cmd;
SendToAddOnServer(ADD_ON_SERVER_RESCAN_FINISHED_NOTIFY, &cmd,
sizeof(cmd));
}
} }
if (fPhysicalVideoIn == -1) {
FindPhysical(&fPhysicalVideoIn, kMsgTypeVideoIn, true, fThreadId = -1;
B_MEDIA_RAW_VIDEO);
FindPhysical(&fPhysicalVideoIn, kMsgTypeVideoIn, true,
B_MEDIA_ENCODED_VIDEO);
}
if (fPhysicalAudioOut == -1)
FindPhysical(&fPhysicalAudioOut, kMsgTypeAudioOut, false,
B_MEDIA_RAW_AUDIO);
if (fPhysicalAudioIn == -1)
FindPhysical(&fPhysicalAudioIn, kMsgTypeAudioIn, true,
B_MEDIA_RAW_AUDIO);
if (fAudioMixer == -1)
FindAudioMixer();
// The normal time source is searched for after the TRACE("DefaultManager::RescanThread() leave\n");
// Physical Audio Out has been created.
if (fTimeSource == -1)
FindTimeSource();
// Connect the mixer and physical audio out (soundcard)
if (!fMixerConnected && fAudioMixer != -1 && fPhysicalAudioOut != -1) {
fMixerConnected = B_OK == ConnectMixerToOutput();
if (!fMixerConnected)
ERROR("DefaultManager: failed to connect mixer and soundcard\n");
} else {
ERROR("DefaultManager: Did not try to connect mixer and soundcard\n");
}
add_on_server_rescan_finished_notify_command cmd;
SendToAddOnServer(ADD_ON_SERVER_RESCAN_FINISHED_NOTIFY, &cmd, sizeof(cmd));
printf("DefaultManager::RescanThread() leave\n");
} }
+2
View File
@@ -51,4 +51,6 @@ private:
uint32 fBeginHeader[3]; uint32 fBeginHeader[3];
uint32 fEndHeader[3]; uint32 fEndHeader[3];
thread_id fThreadId;
vint32 fRescanRequested;
}; };