43 #ifndef INCLUDE_CRTC_H_ 44 #define INCLUDE_CRTC_H_ 52 #define CRTC_EXPORT __declspec(dllexport) 53 #define CRTC_NO_EXPORT __declspec(dllimport) 55 #define CRTC_EXPORT __attribute__((visibility("default"))) 56 #define CRTC_NO_EXPORT __attribute__((visibility("hidden"))) 60 #define CRTC_STATIC(className) \ 61 explicit className() = delete; \ 62 className(const className&) = delete; \ 63 className& operator=(const className&) = delete; 67 #define CRTC_PRIVATE(className) \ 68 className(const className&) = delete; \ 69 className& operator=(const className&) = delete; 78 static int Increment(
volatile int *arg);
79 static int Decrement(
volatile int *arg);
80 static int AcquireLoad(
volatile int *arg);
87 static int64_t Diff(int64_t begin, int64_t end = Now());
88 static double Since(int64_t begin, int64_t end = Now());
93 template <
class T>
class Let {
95 template <
typename... Args>
inline static Let<T> New(Args&&... args) {
96 return Let<T>(
new Constructor<Args...>(std::forward<Args>(args)...));
99 inline static Let<T> Empty() {
103 inline explicit Let() : _ptr(
nullptr) { }
109 inline Let(T* ptr) : _ptr(ptr) {
113 inline Let(
const Let<T> &src) : _ptr(*src) {
117 template <
class S>
inline Let(
Let<S> src) : _ptr(reinterpret_cast<T*>(*src)) {
121 inline Let<T> &operator=(T* src) {
122 if (src) { src->AddRef(); }
130 template <
class S>
inline static Let<T> Cast(S* src) {
131 return Let<T>(
static_cast<T*
>(src));
134 template <
class S>
inline static Let<T> Cast(
const Let<S> &src) {
135 return Let<T>(
reinterpret_cast<T*
>(*src));
138 inline Let<T> &operator=(
const Let<T> &src) {
return *
this = src._ptr; }
139 inline bool IsEmpty()
const {
return (_ptr ==
nullptr); }
140 inline operator T* ()
const {
return _ptr; }
141 inline T* operator*()
const {
return _ptr; }
142 inline T* operator->()
const {
return _ptr; }
144 inline void Dispose()
const {
153 template <
class R>
inline R& operator[](
const size_t index) {
157 template <
class R>
inline const R& operator[](
const size_t index)
const {
162 template <
typename... Args>
class Constructor :
public T {
164 explicit Constructor(Args&&... args) : T(std::forward<Args>(args)...), reference_count(0) { }
167 inline virtual int AddRef()
const {
168 return Atomic::Increment(&reference_count);
171 inline virtual int RemoveRef()
const {
172 int res = Atomic::Decrement(&reference_count);
181 inline virtual int RefCount()
const {
182 return Atomic::AcquireLoad(&reference_count);
185 virtual ~Constructor() { }
188 mutable volatile int reference_count = 0;
191 inline void AddRef()
const {
192 if (_ptr) { _ptr->AddRef(); }
195 inline void RemoveRef()
const {
196 if (_ptr) { _ptr->RemoveRef(); }
204 template <
class T>
friend class Let;
209 virtual int AddRef()
const = 0;
210 virtual int RemoveRef()
const = 0;
211 virtual int RefCount()
const = 0;
247 template <
typename T>
class CRTC_EXPORT Functor;
248 template <
typename R,
typename... Args>
class CRTC_EXPORT Functor<R(Args...)> {
255 explicit Functor() : _flags(kNone) { }
256 virtual ~Functor() { }
258 Functor(
const Functor<R(Args...)> &functor,
const Functor<
void()> ¬ifier) : _flags(kOnce), _callback(
Let<VerifyWrap<void>>::New(functor, notifier)) { }
259 Functor(
const Functor<R(Args...)> &functor) : _flags(functor._flags), _callback(functor._callback) { }
260 Functor(Functor&& functor) : _flags(functor._flags), _callback(std::move(functor._callback)) { }
262 template <
class T>
inline Functor(
const T& functor, Flags flags = kNone) : _flags(flags), _callback(
Let<Wrap<T> >::New(functor)) { }
263 template <
class Object,
class Method>
inline Functor(Object *
object,
const Method& method, Flags flags = kNone) : _flags(flags), _callback(
Let<ObjectWrap<Object, Method> >::New(
object, method)) { }
264 template <
class Object,
class Method>
inline Functor(
const Let<Object> &
object,
const Method& method, Flags flags = kNone) : _flags(flags), _callback(
Let<LetWrap<Object, Method> >::New(
object, method)) { }
266 inline R operator()(Args... args)
const {
267 if (!_callback.IsEmpty()) {
270 if (_flags & kOnce) {
274 return callback->Call(std::move(args)...);
280 inline Functor<R(Args...)>& operator=(
const Functor<R(Args...)> &functor) {
281 _flags = functor._flags;
282 _callback = std::move(functor._callback);
286 inline bool IsEmpty()
const {
287 return _callback.IsEmpty();
290 operator bool()
const {
291 return !_callback.IsEmpty();
294 inline void Dispose()
const {
299 class Callback :
virtual public Reference {
300 CRTC_PRIVATE(Callback);
301 friend class Let<Callback>;
304 virtual R Call(Args&&... args)
const = 0;
307 explicit Callback() { }
308 ~Callback()
override { }
311 template <
class T>
class Wrap :
public Callback {
313 friend class Let<Wrap>;
316 inline R Call(Args&&... args)
const override {
317 return _functor(std::forward<Args>(args)...);
321 explicit Wrap(
const T& functor) : _functor(functor) { }
327 template <
class Object,
class Method>
class ObjectWrap :
public Callback {
328 CRTC_PRIVATE(ObjectWrap);
329 friend class Let<ObjectWrap>;
332 inline R Call(Args&&... args)
const override {
334 return (_object->*_method)(std::forward<Args>(args)...);
341 explicit ObjectWrap(Object *
object,
const Method &method) : _object(
object), _method(method) { }
342 ~ObjectWrap()
override { }
348 template <
class Object,
class Method>
class LetWrap :
public Callback {
349 CRTC_PRIVATE(LetWrap);
350 friend class Let<LetWrap>;
353 inline R Call(Args&&... args)
const override {
354 if (!_object.IsEmpty()) {
355 return (_object->*_method)(std::forward<Args>(args)...);
362 explicit LetWrap(
const Let<Object> &
object,
const Method &method) : _object(
object), _method(method) { }
363 ~LetWrap()
override { }
369 template <
class Type =
void>
class VerifyWrap :
public Callback {
370 CRTC_PRIVATE(VerifyWrap);
371 friend class Let<VerifyWrap>;
374 inline R Call(Args&&... args)
const override {
376 return _functor(std::forward<Args>(args)...);
380 explicit VerifyWrap(
const Functor<R(Args...)> &functor,
const Functor<Type()> ¬ifier) : _functor(functor), _notifier(notifier) { }
382 ~VerifyWrap()
override {
386 Functor<R(Args...)> _functor;
387 Functor<Type()> _notifier;
394 typedef Functor<void()> Callback;
399 static Let<Worker> New(
const Callback &runnable = Callback());
411 virtual void Start(uint32_t interval_ms = 0) = 0;
412 virtual void Stop() = 0;
422 static void Call(Callback callback,
int delay = 0,
Let<Worker> worker = Worker::This());
427 template <
typename F,
typename... Args>
static inline void SetImmediate(F&& func, Args... args) {
428 Functor<void(Args...)> callback(func);
430 Async::Call(Callback([=]() {
439 template <
typename F,
typename... Args>
static inline void SetTimeout(F&& func,
int delay, Args... args) {
440 Functor<void(Args... args)> callback(func);
442 Async::Call(Callback([=]() {
446 }), (delay > 0) ? delay : 0);
453 static Let<Error> New(std::string message, std::string fileName = __FILE__,
int lineNumber = __LINE__);
455 virtual std::string Message()
const = 0;
456 virtual std::string FileName()
const = 0;
457 virtual int LineNumber()
const = 0;
459 virtual std::string ToString()
const = 0;
463 ~
Error()
override { }
466 typedef Functor<void(const Let<Error> &error)> ErrorCallback;
472 friend class Let<Promise<Args...>>;
475 typedef Functor<void(Args...)> FullFilledCallback;
476 typedef Callback FinallyCallback;
477 typedef ErrorCallback RejectedCallback;
478 typedef Functor<void(const FullFilledCallback &resolve, const RejectedCallback &reject)> ExecutorCallback;
480 inline static Let<Promise<Args...>> New(
const ExecutorCallback &executor,
const Let<Worker> &worker = Worker::This()) {
481 Let<Promise<Args...>>
self =
Let<Promise<Args...>>::New();
483 RejectedCallback reject([=](
const Let<Error> &error) {
484 if (!
self.IsEmpty()) {
485 for (
const auto &callback: self->_onreject) {
489 for (
const auto &callback: self->_onfinally) {
493 self->_onfinally.clear();
494 self->_onreject.clear();
495 self->_onresolve.clear();
501 RejectedCallback asyncReject([=](
const Let<Error> &error) {
502 Async::Call(Callback([=]() {
509 FullFilledCallback resolve([=](Args... args) {
510 Async::Call(Callback([=]() {
511 if (!self.IsEmpty()) {
512 for (const auto &callback: self->_onresolve) {
513 callback(std::move(args)...);
516 for (const auto &callback: self->_onfinally) {
520 self->_onfinally.clear();
521 self->_onreject.clear();
522 self->_onresolve.clear();
527 asyncReject(Error::New(
"Reference Lost.", __FILE__, __LINE__));
530 asyncReject(Error::New(
"Reference Lost.", __FILE__, __LINE__));
533 if (!executor.IsEmpty()) {
534 executor(resolve, asyncReject);
536 asyncReject(Error::New(
"Invalid Executor Callback.", __FILE__, __LINE__));
542 inline Let<
Promise<Args...>> Then(
const FullFilledCallback &callback) {
543 _onresolve.push_back(callback);
547 inline Let<
Promise<Args...>> Catch(
const RejectedCallback &callback) {
548 _onreject.push_back(callback);
552 inline Let<
Promise<Args...>> Finally(
const FinallyCallback &callback) {
553 _onfinally.push_back(callback);
558 std::vector<FullFilledCallback> _onresolve;
559 std::vector<RejectedCallback> _onreject;
560 std::vector<FinallyCallback> _onfinally;
579 virtual size_t ByteLength()
const = 0;
583 virtual uint8_t *Data() = 0;
584 virtual const uint8_t *Data()
const = 0;
586 virtual std::string ToString()
const = 0;
598 TypedArray(ArrayBuffer::New(length *
sizeof(T)))
601 TypedArray(
const T *data,
size_t length = 0) :
602 TypedArray(ArrayBuffer::New(data, length *
sizeof(T)))
607 _data(typedArray._data),
608 _length(typedArray._length),
609 _byteOffset(typedArray._byteOffset),
610 _byteLength(typedArray._byteLength),
611 _buffer(typedArray._buffer)
626 if (_buffer.IsEmpty()) {
627 _buffer = ArrayBuffer::New(byteLength - byteOffset);
630 byteLength = (!byteLength) ? _buffer->ByteLength() : byteLength;
631 byteLength -= byteOffset;
633 if (byteLength && (byteLength %
sizeof(T)) == 0) {
634 _data =
reinterpret_cast<T*
>(buffer->Data() + byteOffset);
635 _byteOffset = byteOffset;
636 _byteLength = byteLength;
637 _length = byteLength /
sizeof(T);
641 inline size_t Length()
const {
645 inline size_t ByteOffset()
const {
649 inline size_t ByteLength()
const {
659 return _buffer->Slice(begin *
sizeof(T), end *
sizeof(T));
662 return ArrayBuffer::New();
666 return (_length) ? _data :
nullptr;
669 inline const T *Data()
const {
670 return (_length) ? _data :
nullptr;
673 inline T& Get(
const size_t index) {
674 if (index < _length) {
681 inline const T& Get(
const size_t index)
const {
682 if (index < _length) {
689 inline void Set(
const size_t index,
const T &value) {
690 if (index < _length) {
691 _data[index] = index;
695 inline T& operator[](
const size_t index) {
696 if (index < _length) {
703 inline const T& operator[](
const size_t index)
const {
704 if (index < _length) {
712 static const T empty = 0;
751 static bool DispatchEvents(
bool kForever =
false);
752 static void Dispose();
757 friend class Let<Event>;
783 virtual bool Enabled()
const = 0;
784 virtual bool Muted()
const = 0;
785 virtual bool Remote()
const = 0;
786 virtual std::string Id()
const = 0;
788 virtual Type Kind()
const = 0;
789 virtual State ReadyState()
const = 0;
805 typedef std::vector<Let<MediaStreamTrack>> MediaStreamTracks;
813 virtual std::string Id()
const = 0;
824 virtual MediaStreamTracks GetAudioTracks()
const = 0;
825 virtual MediaStreamTracks GetVideoTracks()
const = 0;
831 Functor<void(const Let<MediaStreamTrack> &track)> onaddtrack;
832 Functor<void(const Let<MediaStreamTrack> &track)> onremovetrack;
839 typedef std::vector<Let<MediaStream>> MediaStreams;
845 static Let<AudioBuffer> New(
int channels = 2,
int sampleRate = 44100,
int bitsPerSample = 8,
int frames = 1);
848 virtual int Channels()
const = 0;
849 virtual int SampleRate()
const = 0;
850 virtual int BitsPerSample()
const = 0;
851 virtual int Frames()
const = 0;
864 virtual bool IsRunning()
const = 0;
865 virtual void Stop() = 0;
867 virtual void Write(
const Let<AudioBuffer> &buffer, ErrorCallback callback = ErrorCallback()) = 0;
882 virtual bool IsRunning()
const = 0;
883 virtual void Stop() = 0;
885 Functor<void(const Let<AudioBuffer> &buffer)> ondata;
899 static size_t ByteLength(
int height,
int stride_y,
int stride_u,
int stride_v);
900 static size_t ByteLength(
int width,
int height);
902 virtual int Width()
const = 0;
903 virtual int Height()
const = 0;
905 virtual const uint8_t* DataY()
const = 0;
906 virtual const uint8_t* DataU()
const = 0;
907 virtual const uint8_t* DataV()
const = 0;
909 virtual int StrideY()
const = 0;
910 virtual int StrideU()
const = 0;
911 virtual int StrideV()
const = 0;
922 static Let<VideoSource> New(
int width = 1280,
int height = 720,
float fps = 30);
924 virtual bool IsRunning()
const = 0;
925 virtual void Stop() = 0;
927 virtual int Width()
const = 0;
928 virtual int Height()
const = 0;
929 virtual float Fps()
const = 0;
931 virtual void Write(
const Let<ImageBuffer> &frame, ErrorCallback callback = ErrorCallback()) = 0;
946 virtual bool IsRunning()
const = 0;
947 virtual void Stop() = 0;
949 Functor<void(const Let<ImageBuffer> &frame)> ondata;
962 typedef Functor<void(const Let<ArrayBuffer> &buffer,
bool binary)> MessageCallback;
973 virtual int Id() = 0;
977 virtual std::string Label() = 0;
981 virtual uint64_t BufferedAmount() = 0;
985 virtual uint64_t BufferedAmountLowThreshold() = 0;
986 virtual void SetBufferedAmountLowThreshold(uint64_t threshold = 0) = 0;
990 virtual uint16_t MaxPacketLifeTime() = 0;
994 virtual uint16_t MaxRetransmits() = 0;
998 virtual bool Negotiated() = 0;
1002 virtual bool Ordered() = 0;
1006 virtual std::string Protocol() = 0;
1010 virtual State ReadyState() = 0;
1014 virtual void Close() = 0;
1056 maxPacketLifeTime(-1),
1063 int maxPacketLifeTime;
1067 std::string protocol;
1091 kHaveRemotePrAnswer,
1141 std::string candidate;
1143 uint32_t sdpMLineIndex;
1149 std::string credential;
1150 std::string credentialType;
1151 std::string username;
1152 std::vector<std::string> urls;
1155 typedef std::vector<RTCIceServer> RTCIceServers;
1157 static RTCIceServers defaultIceServers;
1163 ~RTCConfiguration();
1165 uint16_t iceCandidatePoolSize;
1167 RTCIceServers iceServers;
1176 bool voiceActivityDetection;
1190 typedef Functor<void(const Let<MediaStream> &stream)> StreamCallback;
1191 typedef Functor<void(const Let<RTCDataChannel> &dataChannel)> DataChannelCallback;
1192 typedef Functor<void(const RTCIceCandidate &candidate)> IceCandidateCallback;
1216 virtual MediaStreams GetLocalStreams() = 0;
1220 virtual MediaStreams GetRemoteStreams() = 0;
1240 virtual void Close() = 0;
1253 Callback onnegotiationneeded;
1254 Callback onsignalingstatechange;
1255 Callback onicegatheringstatechange;
1256 Callback oniceconnectionstatechange;
1257 Callback onicecandidatesremoved;
1258 StreamCallback onaddstream;
1259 StreamCallback onremovestream;
1260 DataChannelCallback ondatachannel;
1261 IceCandidateCallback onicecandidate;
1269 #endif // INCLUDE_CRTC_H_
RTCIceGatheringState
Definition: crtc.h:1097
RTCSignalingState
Definition: crtc.h:1086
Callback onbufferedamountlow
Definition: crtc.h:1022
Callback onclose
Definition: crtc.h:1026
ErrorCallback onerror
Definition: crtc.h:1030
RTCBundlePolicy
Definition: crtc.h:1117
RTCIceTransportPolicy
Definition: crtc.h:1125
Callback onopen
Definition: crtc.h:1038
RTCIceConnectionState
Definition: crtc.h:1105
RTCRtcpMuxPolicy
Definition: crtc.h:1133
MessageCallback onmessage
Definition: crtc.h:1034