SimpleMediaClient: Use virtual inheritance to reduce duplication

This commit is contained in:
Dario Casalinuovo 2016-12-11 19:14:26 +01:00
parent d1742cc336
commit 83cd87d033
2 changed files with 82 additions and 106 deletions

View File

@ -79,54 +79,18 @@ private:
}; };
class BSimpleMediaInput : public BMediaInput { class BSimpleMediaConnection : public virtual BMediaConnection {
public: public:
enum notification { enum notification {
B_CONNECTED = 1, // Inputs
B_DISCONNECTED, B_INPUT_CONNECTED = 1,
B_INPUT_DISCONNECTED,
B_FORMAT_CHANGED B_FORMAT_CHANGED,
};
// This function is called when it is the moment to handle a buffer. // Outputs
typedef void (*process_hook)( B_OUTPUT_CONNECTED,
BMediaConnection* connection, B_OUTPUT_DISCONNECTED,
BBuffer* buffer);
// Used to notify or inquire the client about what to do when certain
// events happen.
typedef status_t (*notify_hook)(
notification what,
BMediaConnection* connection,
...);
BSimpleMediaInput();
// Use this to set your callbacks.
void SetHooks(process_hook processHook = NULL,
notify_hook notifyHook = NULL,
void* cookie = NULL);
void* Cookie() const;
protected:
virtual void Connected(const media_format& format);
virtual void Disconnected();
void BufferReceived(BBuffer* buffer);
private:
process_hook fProcessHook;
notify_hook fNotifyHook;
void* fBufferCookie;
};
class BSimpleMediaOutput : public BMediaOutput {
public:
enum notification {
B_CONNECTED = 1,
B_DISCONNECTED,
B_PREPARE_TO_CONNECT, // media_format* format, media_source* source, B_PREPARE_TO_CONNECT, // media_format* format, media_source* source,
// char* name // char* name
@ -147,8 +111,6 @@ public:
BMediaConnection* connection, BMediaConnection* connection,
...); ...);
BSimpleMediaOutput();
// Use this to set your callbacks. // Use this to set your callbacks.
void SetHooks(process_hook processHook = NULL, void SetHooks(process_hook processHook = NULL,
notify_hook notifyHook = NULL, notify_hook notifyHook = NULL,
@ -156,16 +118,37 @@ public:
void* Cookie() const; void* Cookie() const;
protected:
BSimpleMediaConnection(
media_connection_kinds kinds);
process_hook fProcessHook;
notify_hook fNotifyHook;
void* fBufferCookie;
};
class BSimpleMediaInput : public BSimpleMediaConnection, public BMediaInput {
public:
BSimpleMediaInput();
protected:
virtual void Connected(const media_format& format);
virtual void Disconnected();
virtual void BufferReceived(BBuffer* buffer);
};
class BSimpleMediaOutput : public BSimpleMediaConnection, public BMediaOutput {
public:
BSimpleMediaOutput();
protected: protected:
virtual void Connected(const media_format& format); virtual void Connected(const media_format& format);
virtual void Disconnected(); virtual void Disconnected();
virtual status_t FormatProposal(media_format* format); virtual status_t FormatProposal(media_format* format);
private:
process_hook fProcessHook;
notify_hook fNotifyHook;
void* fBufferCookie;
}; };

View File

@ -61,7 +61,7 @@ void
BSimpleMediaClient::HandleStart(bigtime_t performanceTime) BSimpleMediaClient::HandleStart(bigtime_t performanceTime)
{ {
if (fNotifyHook != NULL) { if (fNotifyHook != NULL) {
(*fNotifyHook)(fNotifyCookie, (*fNotifyHook)(BSimpleMediaClient::fNotifyCookie,
BSimpleMediaClient::B_WILL_START, BSimpleMediaClient::B_WILL_START,
performanceTime); performanceTime);
} }
@ -72,7 +72,7 @@ void
BSimpleMediaClient::HandleStop(bigtime_t performanceTime) BSimpleMediaClient::HandleStop(bigtime_t performanceTime)
{ {
if (fNotifyHook != NULL) { if (fNotifyHook != NULL) {
(*fNotifyHook)(fNotifyCookie, (*fNotifyHook)(BSimpleMediaClient::fNotifyCookie,
BSimpleMediaClient::B_WILL_STOP, BSimpleMediaClient::B_WILL_STOP,
performanceTime); performanceTime);
} }
@ -83,7 +83,7 @@ void
BSimpleMediaClient::HandleSeek(bigtime_t mediaTime, bigtime_t performanceTime) BSimpleMediaClient::HandleSeek(bigtime_t mediaTime, bigtime_t performanceTime)
{ {
if (fNotifyHook != NULL) { if (fNotifyHook != NULL) {
(*fNotifyHook)(fNotifyCookie, (*fNotifyHook)(BSimpleMediaClient::fNotifyCookie,
BSimpleMediaClient::B_WILL_SEEK, BSimpleMediaClient::B_WILL_SEEK,
performanceTime, mediaTime); performanceTime, mediaTime);
} }
@ -94,7 +94,7 @@ void
BSimpleMediaClient::HandleTimeWarp(bigtime_t realTime, bigtime_t performanceTime) BSimpleMediaClient::HandleTimeWarp(bigtime_t realTime, bigtime_t performanceTime)
{ {
if (fNotifyHook != NULL) { if (fNotifyHook != NULL) {
(*fNotifyHook)(fNotifyCookie, (*fNotifyHook)(BSimpleMediaClient::fNotifyCookie,
BSimpleMediaClient::B_WILL_TIMEWARP, BSimpleMediaClient::B_WILL_TIMEWARP,
realTime, performanceTime); realTime, performanceTime);
} }
@ -107,7 +107,7 @@ BSimpleMediaClient::HandleFormatSuggestion(media_type type, int32 quality,
{ {
if (fNotifyHook != NULL) { if (fNotifyHook != NULL) {
status_t result = B_ERROR; status_t result = B_ERROR;
(*fNotifyHook)(fNotifyCookie, (*fNotifyHook)(BSimpleMediaClient::fNotifyCookie,
BSimpleMediaClient::B_FORMAT_SUGGESTION, BSimpleMediaClient::B_FORMAT_SUGGESTION,
type, quality, format, &result); type, quality, format, &result);
return result; return result;
@ -124,35 +124,18 @@ void BSimpleMediaClient::_ReservedSimpleMediaClient4() {}
void BSimpleMediaClient::_ReservedSimpleMediaClient5() {} void BSimpleMediaClient::_ReservedSimpleMediaClient5() {}
BSimpleMediaInput::BSimpleMediaInput() BSimpleMediaConnection::BSimpleMediaConnection(media_connection_kinds kinds)
: :
BMediaInput() BMediaConnection(kinds),
fProcessHook(NULL),
fNotifyHook(NULL),
fBufferCookie(NULL)
{ {
} }
void void
BSimpleMediaInput::Connected(const media_format& format) BSimpleMediaConnection::SetHooks(process_hook processHook,
{
if (fNotifyHook != NULL)
(*fNotifyHook)(B_CONNECTED, this);
BMediaInput::Connected(format);
}
void
BSimpleMediaInput::Disconnected()
{
if (fNotifyHook != NULL)
(*fNotifyHook)(B_DISCONNECTED, this);
BMediaConnection::Disconnected();
}
void
BSimpleMediaInput::SetHooks(process_hook processHook,
notify_hook notifyHook, void* cookie) notify_hook notifyHook, void* cookie)
{ {
CALLED(); CALLED();
@ -164,7 +147,7 @@ BSimpleMediaInput::SetHooks(process_hook processHook,
void* void*
BSimpleMediaInput::Cookie() const BSimpleMediaConnection::Cookie() const
{ {
CALLED(); CALLED();
@ -172,6 +155,35 @@ BSimpleMediaInput::Cookie() const
} }
BSimpleMediaInput::BSimpleMediaInput()
:
BMediaConnection(B_MEDIA_INPUT),
BSimpleMediaConnection(B_MEDIA_INPUT),
BMediaInput()
{
}
void
BSimpleMediaInput::Connected(const media_format& format)
{
if (fNotifyHook != NULL)
(*fNotifyHook)(BSimpleMediaConnection::B_INPUT_CONNECTED, this);
BMediaInput::Connected(format);
}
void
BSimpleMediaInput::Disconnected()
{
if (fNotifyHook != NULL)
(*fNotifyHook)(BSimpleMediaConnection::B_INPUT_DISCONNECTED, this);
BMediaConnection::Disconnected();
}
void void
BSimpleMediaInput::BufferReceived(BBuffer* buffer) BSimpleMediaInput::BufferReceived(BBuffer* buffer)
{ {
@ -184,6 +196,8 @@ BSimpleMediaInput::BufferReceived(BBuffer* buffer)
BSimpleMediaOutput::BSimpleMediaOutput() BSimpleMediaOutput::BSimpleMediaOutput()
: :
BMediaConnection(B_MEDIA_INPUT),
BSimpleMediaConnection(B_MEDIA_OUTPUT),
BMediaOutput() BMediaOutput()
{ {
} }
@ -193,7 +207,7 @@ status_t
BSimpleMediaOutput::FormatProposal(media_format* format) BSimpleMediaOutput::FormatProposal(media_format* format)
{ {
if (fNotifyHook != NULL) { if (fNotifyHook != NULL) {
return (*fNotifyHook)(BSimpleMediaOutput::B_FORMAT_PROPOSAL, return (*fNotifyHook)(BSimpleMediaConnection::B_FORMAT_PROPOSAL,
this, format); this, format);
} }
@ -205,9 +219,9 @@ void
BSimpleMediaOutput::Connected(const media_format& format) BSimpleMediaOutput::Connected(const media_format& format)
{ {
if (fNotifyHook != NULL) if (fNotifyHook != NULL)
(*fNotifyHook)(B_CONNECTED, this); (*fNotifyHook)(BSimpleMediaConnection::B_OUTPUT_CONNECTED, this);
BMediaConnection::Connected(format); BSimpleMediaConnection::Connected(format);
} }
@ -215,28 +229,7 @@ void
BSimpleMediaOutput::Disconnected() BSimpleMediaOutput::Disconnected()
{ {
if (fNotifyHook != NULL) if (fNotifyHook != NULL)
(*fNotifyHook)(B_DISCONNECTED, this); (*fNotifyHook)(BSimpleMediaConnection::B_OUTPUT_DISCONNECTED, this);
BMediaConnection::Disconnected(); BSimpleMediaConnection::Disconnected();
}
void
BSimpleMediaOutput::SetHooks(process_hook processHook,
notify_hook notifyHook, void* cookie)
{
CALLED();
fProcessHook = processHook;
fNotifyHook = notifyHook;
fBufferCookie = cookie;
}
void*
BSimpleMediaOutput::Cookie() const
{
CALLED();
return fBufferCookie;
} }