OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 // This file contains Macros for creating asynchronous proxies for webrtc | |
12 // interfaces. Many methods in the WebRtc MediaEngine callback interfaces are | |
13 // called on an arbitrary thread. Using these macros- it is guaranteed that | |
14 // all methods will be called on the same thread as where the proxy is created. | |
15 | |
16 // TODO(perkj): Now before landing -- decide if this make sence and if it make | |
nisse-webrtc
2016/02/17 08:19:46
I din't think there's any point in doing some macr
| |
17 // sence to created Macros instead similar to api/proxy.h. | |
18 | |
19 #ifndef WEBRTC_MEDIA_BASE_ASYNCPROXY_H_ | |
20 #define WEBRTC_MEDIA_BASE_ASYNCPROXY_H_ | |
21 | |
22 #include "webrtc/base/asyncinvoker.h" | |
23 #include "webrtc/base/checks.h" | |
24 #include "webrtc/base/event.h" | |
25 #include "webrtc/base/thread.h" | |
26 | |
27 namespace rtc { | |
28 | |
29 class LoadObserverProxy { | |
30 public: | |
31 explicit LoadObserverProxy(webrtc::LoadObserver* observer) { | |
32 helper_ = new rtc::RefCountedObject<Helper>(observer); | |
33 } | |
34 ~LoadObserverProxy() { helper_->Detach(); } | |
35 | |
36 webrtc::LoadObserver* proxy() { return helper_; } | |
37 | |
38 private: | |
39 class Helper : public webrtc::LoadObserver, public rtc::RefCountInterface { | |
40 public: | |
41 explicit Helper(webrtc::LoadObserver* observer) | |
42 : thread_(rtc::Thread::Current()), observer_(observer) {} | |
43 void Detach() { | |
44 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
45 observer_ = nullptr; | |
46 } | |
47 void OnLoadUpdate(webrtc::LoadObserver::Load load) override { | |
48 if (rtc::Thread::Current() == thread_) { | |
49 observer_->OnLoadUpdate(load); | |
50 return; | |
51 } | |
52 invoker_.AsyncInvoke<void>( | |
53 thread_, rtc::Bind(&Helper::OnLoadUpdateOnCorrectThread, this, load)); | |
54 } | |
55 void OnLoadUpdateOnCorrectThread(webrtc::LoadObserver::Load load) { | |
56 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
57 if (observer_) { | |
58 observer_->OnLoadUpdate(load); | |
59 } | |
60 } | |
61 | |
62 private: | |
63 rtc::ThreadChecker thread_checker_; | |
64 rtc::AsyncInvoker invoker_; | |
65 rtc::Thread* thread_; | |
66 webrtc::LoadObserver* observer_; | |
67 }; | |
68 | |
69 rtc::scoped_refptr<Helper> helper_; | |
70 }; | |
71 | |
72 } // namespace rtc | |
73 | |
74 #endif // WEBRTC_MEDIA_BASE_ASYNCPROXY_H_ | |
OLD | NEW |