Index: content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.h |
diff --git a/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.h b/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1a3c4b1cf644522b998ae353b73ad5f57475f2bb |
--- /dev/null |
+++ b/content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.h |
@@ -0,0 +1,134 @@ |
+// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_COLLECTION_H_ |
+#define CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_COLLECTION_H_ |
+ |
+#include <map> |
+#include <string> |
+ |
+#include "base/memory/ref_counted.h" |
+#include "base/single_thread_task_runner.h" |
+#include "content/common/content_export.h" |
+#include "content/renderer/media/webrtc/webrtc_media_stream_track_adapter.h" |
+#include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
+#include "third_party/webrtc/api/mediastreaminterface.h" |
+ |
+namespace content { |
+ |
+class PeerConnectionDependencyFactory; |
+ |
+// A map and owner of |WebRtcMediaStreamTrackAdapter|s. It takes care of |
+// creating, initializing and disposing track adapters independently of media |
+// streams. Adapters are accessed via |AdapterRef|s, when all references to an |
+// adapter are destroyed it is disposed and removed from the map. |
+class CONTENT_EXPORT WebRtcMediaStreamTrackAdapterMap |
+ : public base::RefCountedThreadSafe<WebRtcMediaStreamTrackAdapterMap> { |
+ private: |
+ // The map's entries are reference counted in order to |Dispose| the adapter |
+ // when all |AdapterRef|s referencing an entry are destroyed. |
+ struct AdapterEntry { |
Guido Urdaneta
2017/06/02 13:19:01
Can you move the definition of this type to the .c
hbos_chromium
2017/06/05 11:55:40
It has to be *defined* in the header for AdapterEn
hbos_chromium
2017/06/05 12:25:00
Above I meant forward-declare and declare, not dec
Guido Urdaneta
2017/06/05 13:05:56
go for the old way then.
hbos_chromium
2017/06/08 09:47:45
Done.
|
+ AdapterEntry(const scoped_refptr<WebRtcMediaStreamTrackAdapter>& adapter); |
+ AdapterEntry(AdapterEntry&& other); |
+ AdapterEntry(const AdapterEntry&) = delete; |
+ ~AdapterEntry(); |
+ |
+ scoped_refptr<WebRtcMediaStreamTrackAdapter> adapter; |
+ size_t ref_count; |
Guido Urdaneta
2017/06/02 13:19:02
Can you reuse the reference count in adapter inste
hbos_chromium
2017/06/05 11:55:40
Done, using HasOneRef() to determine if disposing
|
+ }; |
+ |
+ public: |
+ // Acts as an accessor to adapter members without leaking a reference to the |
+ // adapter. When the last |AdapterRef| is destroyed, the corresponding adapter |
+ // is |Dispose|d and removed from the map. |
+ class CONTENT_EXPORT AdapterRef { |
+ public: |
+ enum class Type { kLocal, kRemote }; |
Guido Urdaneta
2017/06/02 13:19:02
Move to private section. It's used only by a priva
hbos_chromium
2017/06/05 11:55:40
Done.
|
+ |
+ // Must be invoked on the main thread. If this was the last reference to the |
+ // adapter it will be disposed and removed from the map. |
+ ~AdapterRef(); |
+ |
+ bool is_initialized() const { return adapter()->is_initialized(); } |
+ const blink::WebMediaStreamTrack& web_track() const { |
+ return adapter()->web_track(); |
+ } |
+ webrtc::MediaStreamTrackInterface* webrtc_track() const { |
+ return adapter()->webrtc_track(); |
+ } |
+ |
+ const scoped_refptr<WebRtcMediaStreamTrackAdapter>& GetAdapterForTesting() |
+ const { |
+ return adapter(); |
+ } |
+ |
+ private: |
+ friend class WebRtcMediaStreamTrackAdapterMap; |
+ |
+ // Increments the |AdapterEntry::ref_count|. Assumes map's |lock_| is held. |
+ AdapterRef(const scoped_refptr<WebRtcMediaStreamTrackAdapterMap>& map, |
+ Type type, |
+ const std::map<std::string, AdapterEntry>::iterator& it); |
+ |
+ AdapterEntry* entry() { return &it_->second; } |
+ const scoped_refptr<WebRtcMediaStreamTrackAdapter>& adapter() const { |
+ return it_->second.adapter; |
+ } |
+ |
+ scoped_refptr<WebRtcMediaStreamTrackAdapterMap> map_; |
+ Type type_; |
+ std::map<std::string, AdapterEntry>::iterator it_; |
Guido Urdaneta
2017/06/02 13:19:02
A private type alias (e.g., MapEntry = std::map<st
hbos_chromium
2017/06/05 11:55:40
Done.
|
+ }; |
+ |
+ WebRtcMediaStreamTrackAdapterMap( |
+ PeerConnectionDependencyFactory* const factory, |
+ const scoped_refptr<base::SingleThreadTaskRunner>& main_thread); |
+ |
+ // Gets a new reference to the local track adapter by ID, or null if no such |
Guido Urdaneta
2017/06/02 13:19:01
nit: the -> a
hbos_chromium
2017/06/05 11:55:40
Done.
|
+ // adapter was found. When all references are destroyed the adapter is |
+ // disposed and removed from the map. This method can be called from any |
+ // thread, but references must be destroyed on the main thread. |
+ std::unique_ptr<AdapterRef> GetLocalTrackAdapter(const std::string& id); |
+ // Invoke on the main thread. Gets a new reference to the local track adapter |
+ // by web track. If no adapter exists for the track one is created and |
Guido Urdaneta
2017/06/02 13:19:01
nit: by -> for
hbos_chromium
2017/06/05 11:55:40
Done.
|
+ // initialized. When all references are destroyed the adapter is disposed and |
+ // removed from the map. References must be destroyed on the main thread. |
+ std::unique_ptr<AdapterRef> GetOrCreateLocalTrackAdapter( |
+ const blink::WebMediaStreamTrack& web_track); |
+ size_t GetLocalTrackCount() const; |
+ |
+ // Gets a new reference to the remote track adapter by ID, or null if no such |
+ // adapter was found. When all references are destroyed the adapter is |
+ // disposed and removed from the map. This method can be called from any |
+ // thread, but references must be destroyed on the main thread. |
+ std::unique_ptr<AdapterRef> GetRemoteTrackAdapter(const std::string& id); |
+ // Invoke on the webrtc signaling thread. Gets a new reference to the remote |
+ // track adapter by webrtc track. If no adapter exists for the track one is |
+ // created and initialization completes on the main thread in a post. When all |
+ // references are destroyed the adapter is disposed and removed from the map. |
+ // References must be destroyed on the main thread. |
+ std::unique_ptr<AdapterRef> GetOrCreateRemoteTrackAdapter( |
+ webrtc::MediaStreamTrackInterface* webrtc_track); |
+ size_t GetRemoteTrackCount() const; |
+ |
+ protected: |
+ friend class base::RefCountedThreadSafe<WebRtcMediaStreamTrackAdapterMap>; |
+ |
+ // Invoke on the main thread. |
+ virtual ~WebRtcMediaStreamTrackAdapterMap(); |
+ |
+ private: |
+ // Pointer to a |PeerConnectionDependencyFactory| owned by the |RenderThread|. |
+ // It's valid for the lifetime of |RenderThread|. |
+ PeerConnectionDependencyFactory* const factory_; |
+ scoped_refptr<base::SingleThreadTaskRunner> main_thread_; |
+ |
+ mutable base::Lock lock_; |
+ std::map<std::string, AdapterEntry> local_track_adapters_; |
+ std::map<std::string, AdapterEntry> remote_track_adapters_; |
+}; |
+ |
+} // namespace content |
+ |
+#endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_COLLECTION_H_ |