diff --git a/src/kits/media/MediaRoster.cpp b/src/kits/media/MediaRoster.cpp index 3b95ddc388..8083018898 100644 --- a/src/kits/media/MediaRoster.cpp +++ b/src/kits/media/MediaRoster.cpp @@ -495,19 +495,17 @@ BMediaRosterEx::PublishInputs(const media_node& node, List* list) BTimeSource* -BMediaRosterEx::MakeTimeSourceObject(media_node_id timesourceID) +BMediaRosterEx::MakeTimeSourceObject(media_node_id timeSourceID) { - BTimeSource* source; media_node clone; - status_t rv; - - rv = GetNodeFor(timesourceID, &clone); - if (rv != B_OK) { - ERROR("BMediaRosterEx::MakeTimeSourceObject: GetNodeFor failed\n"); + status_t status = GetNodeFor(timeSourceID, &clone); + if (status != B_OK) { + ERROR("BMediaRosterEx::MakeTimeSourceObject: GetNodeFor failed: %s\n", + strerror(status)); return NULL; } - source = _TimeSourceObjectManager->GetTimeSource(clone); + BTimeSource* source = gTimeSourceObjectManager->GetTimeSource(clone); if (source == NULL) { ERROR("BMediaRosterEx::MakeTimeSourceObject: GetTimeSource failed\n"); return NULL; diff --git a/src/kits/media/TimeSourceObject.cpp b/src/kits/media/TimeSourceObject.cpp index 698e160e36..3401d80904 100644 --- a/src/kits/media/TimeSourceObject.cpp +++ b/src/kits/media/TimeSourceObject.cpp @@ -1,32 +1,42 @@ -/*********************************************************************** - * Copyright (c) 2002 Marcus Overhagen. All Rights Reserved. - * This file may be used under the terms of the OpenBeOS License. - * - * The object returned by BMediaRoster's - * BTimeSource * MakeTimeSourceFor(const media_node & for_node); - * - ***********************************************************************/ +/* + * Copyright 2002 Marcus Overhagen. All Rights Reserved. + * This file may be used under the terms of the MIT License. + */ + + +/*! The object returned by BMediaRoster's + MakeTimeSourceFor(const media_node& forNode); +*/ + + +#include "TimeSourceObject.h" -#include #include #include + #include -#include "MediaMisc.h" -#include "TimeSourceObject.h" +#include + +#include +#include + #include "TimeSourceObjectManager.h" -TimeSourceObject::TimeSourceObject(const media_node &node) - : BMediaNode("some timesource object", node.node, node.kind), + +TimeSourceObject::TimeSourceObject(const media_node& node) + : + BMediaNode("some timesource object", node.node, node.kind), BTimeSource(node.node) { TRACE("TimeSourceObject::TimeSourceObject enter, id = %ld\n", node.node); if (fControlPort > 0) delete_port(fControlPort); - - // we use the control port of the real time source object. + + // We use the control port of the real time source object. // this way, all messages are send to the real time source, // and this shadow object won't receive any. fControlPort = node.port; + ASSERT(fNodeID == node.node); ASSERT(fKinds == node.kind); @@ -34,49 +44,50 @@ TimeSourceObject::TimeSourceObject(const media_node &node) strcpy(fName, "System Clock"); fIsRealtime = true; } else { - live_node_info lni; - if (B_OK == BMediaRoster::Roster()->GetLiveNodeInfo(node, &lni)) { - strcpy(fName, lni.name); - } else { - sprintf(fName, "timesource %ld", node.node); - } + live_node_info liveNodeInfo; + if (BMediaRoster::Roster()->GetLiveNodeInfo(node, &liveNodeInfo) + == B_OK) + strlcpy(fName, liveNodeInfo.name, B_MEDIA_NAME_LENGTH); + else + snprintf(fName, B_MEDIA_NAME_LENGTH, "timesource %ld", node.node); } AddNodeKind(NODE_KIND_SHADOW_TIMESOURCE); AddNodeKind(NODE_KIND_NO_REFCOUNTING); - + TRACE("TimeSourceObject::TimeSourceObject leave, node id %ld\n", fNodeID); } -/* virtual */ status_t -TimeSourceObject::TimeSourceOp( - const time_source_op_info & op, - void * _reserved) + +status_t +TimeSourceObject::TimeSourceOp(const time_source_op_info& op, void* _reserved) { // we don't get anything here return B_OK; } -/* virtual */ BMediaAddOn* -TimeSourceObject::AddOn(int32 *internal_id) const + +BMediaAddOn* +TimeSourceObject::AddOn(int32* _id) const { - if (internal_id) - *internal_id = 0; + if (_id != NULL) + *_id = 0; + return NULL; } -/* virtual */ status_t -TimeSourceObject::DeleteHook(BMediaNode *node) + +status_t +TimeSourceObject::DeleteHook(BMediaNode* node) { - status_t status; // if (fIsRealtime) { // ERROR("TimeSourceObject::DeleteHook: system time source clone delete hook called\n"); // return B_ERROR; // } - printf("TimeSourceObject::DeleteHook enter\n"); - _TimeSourceObjectManager->ObjectDeleted(this); - status = BTimeSource::DeleteHook(node); - printf("TimeSourceObject::DeleteHook leave\n"); + PRINT("TimeSourceObject::DeleteHook enter\n"); + gTimeSourceObjectManager->ObjectDeleted(this); + status_t status = BTimeSource::DeleteHook(node); + PRINT("TimeSourceObject::DeleteHook leave\n"); return status; } diff --git a/src/kits/media/TimeSourceObject.h b/src/kits/media/TimeSourceObject.h index 2ad21f41fd..5cf3915387 100644 --- a/src/kits/media/TimeSourceObject.h +++ b/src/kits/media/TimeSourceObject.h @@ -1,36 +1,40 @@ -/*********************************************************************** - * Copyright (c) 2002 Marcus Overhagen. All Rights Reserved. - * This file may be used under the terms of the OpenBeOS License. - * - * The object returned by BMediaRoster's - * BTimeSource * MakeTimeSourceFor(const media_node & for_node); - * - ***********************************************************************/ -#ifndef _TIME_SOURCE_OBJECT_H_ -#define _TIME_SOURCE_OBJECT_H_ +/* + * Copyright 2002 Marcus Overhagen. All Rights Reserved. + * This file may be used under the terms of the MIT License. + */ +#ifndef TIME_SOURCE_OBJECT_H +#define TIME_SOURCE_OBJECT_H + #include -#include "MediaMisc.h" -namespace BPrivate { namespace media { +#include -class TimeSourceObject : public BTimeSource -{ + +namespace BPrivate { +namespace media { + + +class TimeSourceObject : public BTimeSource { public: - TimeSourceObject(const media_node &node); - + TimeSourceObject(const media_node& node); + protected: - virtual status_t TimeSourceOp( - const time_source_op_info & op, - void * _reserved); + virtual status_t TimeSourceOp(const time_source_op_info& op, + void* _reserved); - virtual BMediaAddOn* AddOn( - int32 * internal_id) const; + virtual BMediaAddOn* AddOn(int32* _id) const; - // override from BMediaNode - virtual status_t DeleteHook(BMediaNode * node); + // override from BMediaNode + virtual status_t DeleteHook(BMediaNode* node); }; -} } using namespace BPrivate::media; -#endif +} // namespace media +} // namespace BPrivate + + +using namespace BPrivate::media; + + +#endif // TIME_SOURCE_OBJECT_H diff --git a/src/kits/media/TimeSourceObjectManager.cpp b/src/kits/media/TimeSourceObjectManager.cpp index 1582172f6f..fa1333422e 100644 --- a/src/kits/media/TimeSourceObjectManager.cpp +++ b/src/kits/media/TimeSourceObjectManager.cpp @@ -1,102 +1,107 @@ -/*********************************************************************** - * Copyright (c) 2002 Marcus Overhagen. All Rights Reserved. +/* + * Copyright 2002 Marcus Overhagen. All Rights Reserved. * This file may be used under the terms of the MIT License. - * - * This works like a cache for time source objects, to make sure - * each team only has one object representation for each time source. - * - ***********************************************************************/ + */ + + +/*! This works like a cache for time source objects, to make sure + each team only has one object representation for each time source. +*/ + -#include -#include -#include -#include #include "TimeSourceObjectManager.h" + +#include + +#include +#include + +#include +#include + #include "TimeSourceObject.h" -#include "MediaMisc.h" -#include "debug.h" -static BPrivate::media::TimeSourceObjectManager manager; -BPrivate::media::TimeSourceObjectManager *_TimeSourceObjectManager = &manager; +static BPrivate::media::TimeSourceObjectManager sManager; +BPrivate::media::TimeSourceObjectManager* gTimeSourceObjectManager = &sManager; + namespace BPrivate { namespace media { + TimeSourceObjectManager::TimeSourceObjectManager() -// : fSystemTimeSource(0) + : + BLocker("time source object manager") { - CALLED(); - fLock = new BLocker("timesource object manager locker"); - fMap = new Map; } TimeSourceObjectManager::~TimeSourceObjectManager() { CALLED(); - delete fLock; - // force unloading all currently loaded - BTimeSource **pts; - for (fMap->Rewind(); fMap->GetNext(&pts); ) { - PRINT(1, "Forcing release of TimeSource id %ld...\n", (*pts)->ID()); - int debugcnt = 0; - while ((*pts)->Release() != NULL) - debugcnt++; - PRINT(1, "Forcing release of TimeSource done, released %d times\n", debugcnt); + // force unloading all currently loaded time sources + NodeMap::iterator iterator = fMap.begin(); + for (; iterator != fMap.end(); iterator++) { + BTimeSource* timeSource = iterator->second; + + PRINT(1, "Forcing release of TimeSource id %ld...\n", timeSource->ID()); + int32 debugCount = 0; + while (timeSource->Release() != NULL) + debugCount++; + + PRINT(1, "Forcing release of TimeSource done, released %d times\n", + debugCount); } - - delete fMap; } -/* BMediaRoster::MakeTimeSourceFor does use this function to request - * a time source object. If it is already in memory, it will be - * Acquired(), if not, a new TimeSourceObject will be created. - */ -BTimeSource * -TimeSourceObjectManager::GetTimeSource(const media_node &node) + +/*! BMediaRoster::MakeTimeSourceFor does use this function to request + a time source object. If it is already in memory, it will be + Acquired(), if not, a new TimeSourceObject will be created. +*/ +BTimeSource* +TimeSourceObjectManager::GetTimeSource(const media_node& node) { CALLED(); - BAutolock lock(fLock); + BAutolock _(this); -// printf("TimeSourceObjectManager::GetTimeSource, node id %ld\n", node.node); + PRINT("TimeSourceObjectManager::GetTimeSource, node id %ld\n", node.node); - BTimeSource **pts; - if (fMap->Get(node.node, &pts)) - return dynamic_cast((*pts)->Acquire()); + NodeMap::iterator found = fMap.find(node.node); + if (found != fMap.end()) + return dynamic_cast(found->second->Acquire()); // time sources are not accounted in node reference counting - BTimeSource *ts; - ts = new TimeSourceObject(node); - fMap->Insert(node.node, ts); - return ts; + BTimeSource* timeSource = new(std::nothrow) TimeSourceObject(node); + if (timeSource == NULL) + return NULL; + + fMap.insert(std::make_pair(node.node, timeSource)); + return timeSource; } -/* This function is called during deletion of the time source object. - * - * I'm not sure if there is a race condition with the function above. - */ + +/*! This function is called during deletion of the time source object. +*/ void -TimeSourceObjectManager::ObjectDeleted(BTimeSource *timesource) +TimeSourceObjectManager::ObjectDeleted(BTimeSource* timeSource) { CALLED(); - BAutolock lock(fLock); + BAutolock _(this); -// printf("TimeSourceObjectManager::ObjectDeleted, node id %ld\n", timesource->ID()); - - bool b; - b = fMap->Remove(timesource->ID()); - if (!b) { - ERROR("TimeSourceObjectManager::ObjectDeleted, Remove failed\n"); - } - - status_t rv; - rv = BMediaRoster::Roster()->ReleaseNode(timesource->Node()); - if (rv != B_OK) { + PRINT("TimeSourceObjectManager::ObjectDeleted, node id %ld\n", + timeSource->ID()); + + fMap.erase(timeSource->ID()); + + status_t status = BMediaRoster::Roster()->ReleaseNode(timeSource->Node()); + if (status != B_OK) { ERROR("TimeSourceObjectManager::ObjectDeleted, ReleaseNode failed\n"); } } -}; // namespace media -}; // namespace BPrivate + +} // namespace media +} // namespace BPrivate diff --git a/src/kits/media/TimeSourceObjectManager.h b/src/kits/media/TimeSourceObjectManager.h index 9528f271de..5c056d1bd7 100644 --- a/src/kits/media/TimeSourceObjectManager.h +++ b/src/kits/media/TimeSourceObjectManager.h @@ -1,35 +1,47 @@ /* - * Copyright (c) 2002 Marcus Overhagen. All Rights Reserved. + * Copyright 2002 Marcus Overhagen. All Rights Reserved. * This file may be used under the terms of the MIT License. */ -#ifndef _TIME_SOURCE_OBJECT_MANAGER_H_ -#define _TIME_SOURCE_OBJECT_MANAGER_H_ +#ifndef TIME_SOURCE_OBJECT_MANAGER_H +#define TIME_SOURCE_OBJECT_MANAGER_H -#include "TMap.h" +#include -class BLocker; +#include +#include + + +class BTimeSource; namespace BPrivate { namespace media { -class TimeSourceObjectManager { -public: - TimeSourceObjectManager(); - ~TimeSourceObjectManager(); - BTimeSource *GetTimeSource(const media_node &node); - void ObjectDeleted(BTimeSource *timesource); +class TimeSourceObjectManager : BLocker { +public: + TimeSourceObjectManager(); + ~TimeSourceObjectManager(); + + BTimeSource* GetTimeSource(const media_node& node); + void ObjectDeleted(BTimeSource* timeSource); private: - Map *fMap; - BLocker *fLock; + typedef std::map NodeMap; + + NodeMap fMap; }; -} // namespace media -} // namespace BPrivate -extern BPrivate::media::TimeSourceObjectManager *_TimeSourceObjectManager; +extern TimeSourceObjectManager* gTimeSourceObjectManager; + + +} // namespace media +} // namespace BPrivate + + +using BPrivate::media::gTimeSourceObjectManager; + #endif // _TIME_SOURCE_OBJECT_MANAGER_H_