OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_COLLECTION_H_ | |
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_COLLECTION_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/single_thread_task_runner.h" | |
13 #include "content/common/content_export.h" | |
14 #include "content/renderer/media/webrtc/webrtc_media_stream_track_adapter.h" | |
15 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | |
16 #include "third_party/webrtc/api/mediastreaminterface.h" | |
17 | |
18 namespace content { | |
19 | |
20 class PeerConnectionDependencyFactory; | |
21 | |
22 // A map and owner of |WebRtcMediaStreamTrackAdapter|s. It takes care of | |
23 // creating, initializing and disposing track adapters independently of media | |
24 // streams. Adapters are accessed via |AdapterRef|s, when all references to an | |
25 // adapter are destroyed it is disposed and removed from the map. | |
26 class CONTENT_EXPORT WebRtcMediaStreamTrackAdapterMap | |
27 : public base::RefCountedThreadSafe<WebRtcMediaStreamTrackAdapterMap> { | |
28 private: | |
Guido Urdaneta
2017/06/05 13:05:56
Add a comment explaining why you need this private
hbos_chromium
2017/06/08 09:47:45
Done.
| |
29 struct AdapterEntry; | |
30 | |
31 public: | |
32 // Acts as an accessor to adapter members without leaking a reference to the | |
33 // adapter. When the last |AdapterRef| is destroyed, the corresponding adapter | |
34 // is |Dispose|d and removed from the map. | |
35 class CONTENT_EXPORT AdapterRef { | |
36 public: | |
37 // Must be invoked on the main thread. If this was the last reference to the | |
38 // adapter it will be disposed and removed from the map. | |
39 ~AdapterRef(); | |
40 | |
41 bool is_initialized() const { return adapter_->is_initialized(); } | |
42 const blink::WebMediaStreamTrack& web_track() const { | |
43 return adapter_->web_track(); | |
44 } | |
45 webrtc::MediaStreamTrackInterface* webrtc_track() const { | |
46 return adapter_->webrtc_track(); | |
47 } | |
48 | |
49 // Warning: Holding an external reference to the adapter will prevent | |
50 // |~AdapterRef| from disposing the adapter. | |
51 WebRtcMediaStreamTrackAdapter* GetAdapterForTesting() const { | |
52 return adapter_.get(); | |
53 } | |
54 | |
55 private: | |
56 friend class WebRtcMediaStreamTrackAdapterMap; | |
57 | |
58 typedef std::map<std::string, AdapterEntry>::iterator MapEntryIterator; | |
Guido Urdaneta
2017/06/05 13:05:56
prefer using to typedef.
hbos_chromium
2017/06/08 09:47:45
Done.
| |
59 enum class Type { kLocal, kRemote }; | |
60 | |
61 // Increments the |AdapterEntry::ref_count|. Assumes map's |lock_| is held. | |
62 AdapterRef(const scoped_refptr<WebRtcMediaStreamTrackAdapterMap>& map, | |
63 Type type, | |
64 const MapEntryIterator& it); | |
65 | |
66 AdapterEntry* entry() { return &it_->second; } | |
67 | |
68 scoped_refptr<WebRtcMediaStreamTrackAdapterMap> map_; | |
69 Type type_; | |
70 MapEntryIterator it_; | |
71 // A reference to the entry's adapter, ensures that |HasOneRef| is false. | |
72 scoped_refptr<WebRtcMediaStreamTrackAdapter> adapter_; | |
73 }; | |
74 | |
75 WebRtcMediaStreamTrackAdapterMap( | |
76 PeerConnectionDependencyFactory* const factory, | |
77 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread); | |
78 | |
79 // Gets the new reference to the local track adapter by ID, or null if no such | |
80 // adapter was found. When all references are destroyed the adapter is | |
81 // disposed and removed from the map. This method can be called from any | |
82 // thread, but references must be destroyed on the main thread. | |
83 std::unique_ptr<AdapterRef> GetLocalTrackAdapter(const std::string& id); | |
84 // Invoke on the main thread. Gets a new reference to the local track adapter | |
85 // for the web track. If no adapter exists for the track one is created and | |
86 // initialized. When all references are destroyed the adapter is disposed and | |
87 // removed from the map. References must be destroyed on the main thread. | |
88 std::unique_ptr<AdapterRef> GetOrCreateLocalTrackAdapter( | |
89 const blink::WebMediaStreamTrack& web_track); | |
90 size_t GetLocalTrackCount() const; | |
91 | |
92 // Gets the new reference to the remote track adapter by ID, or null if no | |
93 // such adapter was found. When all references are destroyed the adapter is | |
94 // disposed and removed from the map. This method can be called from any | |
95 // thread, but references must be destroyed on the main thread. | |
96 std::unique_ptr<AdapterRef> GetRemoteTrackAdapter(const std::string& id); | |
97 // Invoke on the webrtc signaling thread. Gets a new reference to the remote | |
98 // track adapter for the webrtc track. If no adapter exists for the track one | |
99 // is created and initialization completes on the main thread in a post. When | |
100 // all references are destroyed the adapter is disposed and removed from the | |
101 // map. References must be destroyed on the main thread. | |
102 std::unique_ptr<AdapterRef> GetOrCreateRemoteTrackAdapter( | |
103 webrtc::MediaStreamTrackInterface* webrtc_track); | |
104 size_t GetRemoteTrackCount() const; | |
105 | |
106 protected: | |
107 friend class base::RefCountedThreadSafe<WebRtcMediaStreamTrackAdapterMap>; | |
108 | |
109 // Invoke on the main thread. | |
110 virtual ~WebRtcMediaStreamTrackAdapterMap(); | |
111 | |
112 private: | |
113 // The map's entries are reference counted in order to |Dispose| the adapter | |
114 // when all |AdapterRef|s referencing an entry are destroyed. | |
115 struct AdapterEntry { | |
116 AdapterEntry(const scoped_refptr<WebRtcMediaStreamTrackAdapter>& adapter); | |
117 AdapterEntry(AdapterEntry&& other); | |
118 AdapterEntry(const AdapterEntry&) = delete; | |
Guido Urdaneta
2017/06/05 13:05:57
Shouldn't you also delete the copy-assignment oper
hbos_chromium
2017/06/08 09:47:45
Done.
| |
119 ~AdapterEntry(); | |
120 | |
121 scoped_refptr<WebRtcMediaStreamTrackAdapter> adapter; | |
122 }; | |
123 | |
124 std::unique_ptr<AdapterRef> GetTrackAdapter(AdapterRef::Type type, | |
125 const std::string& id); | |
126 std::unique_ptr<AdapterRef> GetOrCreateTrackAdapter( | |
127 AdapterRef::Type type, | |
128 base::Callback<scoped_refptr<WebRtcMediaStreamTrackAdapter>()> | |
129 create_adapter_callback, | |
130 const std::string& id); | |
131 | |
132 // Pointer to a |PeerConnectionDependencyFactory| owned by the |RenderThread|. | |
133 // It's valid for the lifetime of |RenderThread|. | |
134 PeerConnectionDependencyFactory* const factory_; | |
135 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; | |
136 | |
137 mutable base::Lock lock_; | |
138 std::map<std::string, AdapterEntry> local_track_adapters_; | |
139 std::map<std::string, AdapterEntry> remote_track_adapters_; | |
140 }; | |
141 | |
142 } // namespace content | |
143 | |
144 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_COLLECTION_H_ | |
OLD | NEW |