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: |
| 29 // The map's entries are reference counted in order to |Dispose| the adapter |
| 30 // when all |AdapterRef|s referencing an entry are destroyed. |
| 31 struct AdapterEntry { |
| 32 AdapterEntry(const scoped_refptr<WebRtcMediaStreamTrackAdapter>& adapter); |
| 33 AdapterEntry(AdapterEntry&& other); |
| 34 AdapterEntry(const AdapterEntry&) = delete; |
| 35 ~AdapterEntry(); |
| 36 |
| 37 scoped_refptr<WebRtcMediaStreamTrackAdapter> adapter; |
| 38 size_t ref_count; |
| 39 }; |
| 40 |
| 41 public: |
| 42 // Acts as an accessor to adapter members without leaking a reference to the |
| 43 // adapter. When the last |AdapterRef| is destroyed, the corresponding adapter |
| 44 // is |Dispose|d and removed from the map. |
| 45 class AdapterRef { |
| 46 public: |
| 47 enum class Type { kLocal, kRemote }; |
| 48 |
| 49 // Must be invoked on the main thread. If this was the last reference to the |
| 50 // adapter it will be disposed and removed from the map. |
| 51 ~AdapterRef(); |
| 52 |
| 53 bool is_initialized() const { return adapter()->is_initialized(); } |
| 54 const blink::WebMediaStreamTrack& web_track() const { |
| 55 return adapter()->web_track(); |
| 56 } |
| 57 webrtc::MediaStreamTrackInterface* webrtc_track() const { |
| 58 return adapter()->webrtc_track(); |
| 59 } |
| 60 |
| 61 const scoped_refptr<WebRtcMediaStreamTrackAdapter>& GetAdapterForTesting() |
| 62 const { |
| 63 return adapter(); |
| 64 } |
| 65 |
| 66 private: |
| 67 friend class WebRtcMediaStreamTrackAdapterMap; |
| 68 |
| 69 // Increments the |AdapterEntry::ref_count|. Assumes map's |lock_| is held. |
| 70 AdapterRef(const scoped_refptr<WebRtcMediaStreamTrackAdapterMap>& map, |
| 71 Type type, |
| 72 const std::map<std::string, AdapterEntry>::iterator& it); |
| 73 |
| 74 AdapterEntry* entry() { return &it_->second; } |
| 75 const scoped_refptr<WebRtcMediaStreamTrackAdapter>& adapter() const { |
| 76 return it_->second.adapter; |
| 77 } |
| 78 |
| 79 scoped_refptr<WebRtcMediaStreamTrackAdapterMap> map_; |
| 80 Type type_; |
| 81 std::map<std::string, AdapterEntry>::iterator it_; |
| 82 }; |
| 83 |
| 84 WebRtcMediaStreamTrackAdapterMap( |
| 85 PeerConnectionDependencyFactory* const factory, |
| 86 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread); |
| 87 |
| 88 // Gets a new reference to the local track adapter by ID, or null if no such |
| 89 // adapter was found. When all references are destroyed the adapter is |
| 90 // disposed and removed from the map. This method can be called from any |
| 91 // thread, but references must be destroyed on the main thread. |
| 92 std::unique_ptr<AdapterRef> GetLocalTrackAdapter(const std::string& id); |
| 93 // Invoke on the main thread. Gets a new reference to the local track adapter |
| 94 // by web track. If no adapter exists for the track one is created and |
| 95 // initialized. When all references are destroyed the adapter is disposed and |
| 96 // removed from the map. References must be destroyed on the main thread. |
| 97 std::unique_ptr<AdapterRef> GetOrCreateLocalTrackAdapter( |
| 98 const blink::WebMediaStreamTrack& web_track); |
| 99 size_t GetLocalTrackCount() const; |
| 100 |
| 101 // Gets a new reference to the remote track adapter by ID, or null if no such |
| 102 // adapter was found. When all references are destroyed the adapter is |
| 103 // disposed and removed from the map. This method can be called from any |
| 104 // thread, but references must be destroyed on the main thread. |
| 105 std::unique_ptr<AdapterRef> GetRemoteTrackAdapter(const std::string& id); |
| 106 // Invoke on the webrtc signaling thread. Gets a new reference to the remote |
| 107 // track adapter by webrtc track. If no adapter exists for the track one is |
| 108 // created and initialization completes on the main thread in a post. When all |
| 109 // references are destroyed the adapter is disposed and removed from the map. |
| 110 // References must be destroyed on the main thread. |
| 111 std::unique_ptr<AdapterRef> GetOrCreateRemoteTrackAdapter( |
| 112 webrtc::MediaStreamTrackInterface* webrtc_track); |
| 113 size_t GetRemoteTrackCount() const; |
| 114 |
| 115 protected: |
| 116 friend class base::RefCountedThreadSafe<WebRtcMediaStreamTrackAdapterMap>; |
| 117 |
| 118 // Invoke on the main thread. |
| 119 virtual ~WebRtcMediaStreamTrackAdapterMap(); |
| 120 |
| 121 private: |
| 122 // Pointer to a |PeerConnectionDependencyFactory| owned by the |RenderThread|. |
| 123 // It's valid for the lifetime of |RenderThread|. |
| 124 PeerConnectionDependencyFactory* const factory_; |
| 125 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; |
| 126 |
| 127 mutable base::Lock lock_; |
| 128 std::map<std::string, AdapterEntry> local_track_adapters_; |
| 129 std::map<std::string, AdapterEntry> remote_track_adapters_; |
| 130 }; |
| 131 |
| 132 } // namespace content |
| 133 |
| 134 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_COLLECTION_H_ |
OLD | NEW |