Implement listener interfaces.

This commit is contained in:
Peter Johnson
2016-11-05 13:13:09 -07:00
parent 8d2efb2838
commit 29d8d1d74c
13 changed files with 616 additions and 13 deletions

View File

@@ -238,6 +238,8 @@ void CS_SetSinkEnabled(CS_Sink sink, CS_Bool enabled, CS_Status* status);
//
// Listener Functions
//
void CS_SetListenerOnStart(void (*onStart)(void* data), void* data);
void CS_SetListenerOnExit(void (*onExit)(void* data), void* data);
CS_Listener CS_AddListener(void* data,
void (*callback)(void* data, const CS_Event* event),
int eventMask, int immediateNotify,
@@ -245,6 +247,8 @@ CS_Listener CS_AddListener(void* data,
void CS_RemoveListener(CS_Listener handle, CS_Status* status);
int CS_NotifierDestroyed(void);
//
// Utility Functions
//

View File

@@ -83,11 +83,36 @@ struct RawEvent {
kSourcePropertyChoicesUpdated = CS_SOURCE_PROPERTY_CHOICES_UPDATED
};
RawEvent() = default;
RawEvent(llvm::StringRef name_, CS_Handle handle_, RawEvent::Type type_)
: type{type_}, name{name_} {
if (type_ == kSinkCreated || type_ == kSinkDestroyed ||
type_ == kSinkEnabled || type_ == kSinkDisabled)
sinkHandle = handle_;
else
sourceHandle = handle_;
}
RawEvent(llvm::StringRef name_, CS_Source source_, const VideoMode& mode_)
: type{kSourceVideoModeChanged},
sourceHandle{source_},
name{name_},
mode{mode_} {}
RawEvent(llvm::StringRef name_, CS_Source source_, RawEvent::Type type_,
CS_Property property_, CS_PropertyType propertyType_, int value_,
llvm::StringRef valueStr_)
: type{type_},
sourceHandle{source_},
name{name_},
propertyHandle{property_},
propertyType{propertyType_},
value{value_},
valueStr{valueStr_} {}
Type type;
// Valid for kSource* and kSink* respectively
CS_Source sourceHandle;
CS_Sink sinkHandle;
CS_Source sourceHandle = CS_INVALID_HANDLE;
CS_Sink sinkHandle = CS_INVALID_HANDLE;
// Source/sink name
std::string name;
@@ -95,7 +120,7 @@ struct RawEvent {
// Fields for kSourceVideoModeChanged event
VideoMode mode;
// Fields for CS_SOURCE_PROPERTY_* events
// Fields for kSourceProperty* events
CS_Property propertyHandle;
CS_PropertyType propertyType;
int value;
@@ -225,11 +250,16 @@ void SetSinkEnabled(CS_Sink sink, bool enabled, CS_Status* status);
//
// Listener Functions
//
void SetListenerOnStart(std::function<void()> onStart);
void SetListenerOnExit(std::function<void()> onExit);
CS_Listener AddListener(std::function<void(const RawEvent& event)> callback,
int eventMask, bool immediateNotify, CS_Status* status);
void RemoveListener(CS_Listener handle, CS_Status* status);
bool NotifierDestroyed();
//
// Utility Functions
//
@@ -242,4 +272,4 @@ llvm::ArrayRef<CS_Sink> EnumerateSinkHandles(
} // namespace cs
#endif /* CAMERASERVER_CPP_H_ */
#endif // CAMERASERVER_CPP_H_

View File

@@ -358,16 +358,30 @@ class CvSink : public VideoSink {
void SetEnabled(bool enabled);
};
/// An event generated by the library and provided to event listeners.
class VideoEvent : public RawEvent {
public:
/// Get the source associated with the event (if any).
VideoSource GetSource() const;
/// Get the sink associated with the event (if any).
VideoSink GetSink() const;
/// Get the property associated with the event (if any).
VideoProperty GetProperty() const;
};
/// An event listener. This calls back to a desigated callback function when
/// an event matching the specified mask is generated by the library.
class VideoListener {
public:
VideoListener() : m_handle(0) {}
/// Create an event listener.
/// @param callback Callback function
/// @param eventMask Bitmask of VideoEvent::Type values
/// @param immediateNotify Whether callback should be immediately called with
/// a representative set of events for the current library state.
VideoListener(std::function<void(const VideoEvent& event)> callback,
int eventMask, bool immediateNotify);
@@ -389,4 +403,4 @@ class VideoListener {
#include "cameraserver_oo.inl"
#endif /* CAMERASERVER_OO_H_ */
#endif // CAMERASERVER_OO_H_