| 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_ADAPTER_MAP_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_ADAPTER_MAP_H_ |
| 7 |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 10 #include "content/common/content_export.h" |
| 11 #include "content/renderer/media/webrtc/webrtc_media_stream_adapter.h" |
| 12 #include "third_party/WebKit/public/platform/WebMediaStream.h" |
| 13 #include "third_party/webrtc/api/mediastreaminterface.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 // A map and owner of |WebRtcMediaStreamAdapter|s. Adapters are the glue between |
| 18 // blink and webrtc layer versions of streams. As long as a stream is in use by |
| 19 // a peer connection there has to exist an adapter for it. The map takes care of |
| 20 // creating and disposing stream adapters. Adapters are accessed via |
| 21 // |AdapterRef|s, when all references to an adapter are destroyed it is |
| 22 // destroyed and removed from the map. |
| 23 class CONTENT_EXPORT WebRtcMediaStreamAdapterMap |
| 24 : public base::RefCountedThreadSafe<WebRtcMediaStreamAdapterMap> { |
| 25 private: |
| 26 // The map's entries are reference counted in order to remove the adapter when |
| 27 // all |AdapterRef|s referencing an entry are destroyed. |
| 28 // Private section needed here due to |AdapterRef|'s usage of |AdapterEntry|. |
| 29 struct AdapterEntry { |
| 30 AdapterEntry(std::unique_ptr<WebRtcMediaStreamAdapter> adapter); |
| 31 AdapterEntry(AdapterEntry&& other); |
| 32 ~AdapterEntry(); |
| 33 |
| 34 AdapterEntry(const AdapterEntry&) = delete; |
| 35 AdapterEntry& operator=(const AdapterEntry&) = delete; |
| 36 |
| 37 std::unique_ptr<WebRtcMediaStreamAdapter> adapter; |
| 38 size_t ref_count; |
| 39 }; |
| 40 |
| 41 public: |
| 42 // Accessor to an adapter to take care of reference counting. When the last |
| 43 // |AdapterRef| is destroyed, the corresponding adapter is destroyed and |
| 44 // removed from the map. |
| 45 class CONTENT_EXPORT AdapterRef { |
| 46 public: |
| 47 // Must be invoked on the main thread. If this was the last reference to the |
| 48 // adapter it will be disposed and removed from the map. |
| 49 ~AdapterRef(); |
| 50 |
| 51 const WebRtcMediaStreamAdapter& adapter() const { |
| 52 return *it_->second.adapter; |
| 53 } |
| 54 |
| 55 private: |
| 56 friend class WebRtcMediaStreamAdapterMap; |
| 57 using MapEntryIterator = std::map<std::string, AdapterEntry>::iterator; |
| 58 |
| 59 // Increments the |AdapterEntry::ref_count|. |
| 60 AdapterRef(scoped_refptr<WebRtcMediaStreamAdapterMap> map, |
| 61 const MapEntryIterator& it); |
| 62 |
| 63 AdapterEntry* entry() { return &it_->second; } |
| 64 |
| 65 scoped_refptr<WebRtcMediaStreamAdapterMap> map_; |
| 66 MapEntryIterator it_; |
| 67 }; |
| 68 |
| 69 // Must be invoked on the main thread. |
| 70 WebRtcMediaStreamAdapterMap( |
| 71 PeerConnectionDependencyFactory* const factory, |
| 72 scoped_refptr<WebRtcMediaStreamTrackAdapterMap> track_adapter_map); |
| 73 |
| 74 // Invoke on the main thread. Gets a new reference to the local stream adapter |
| 75 // by ID, or null if no such adapter was found. When all references are |
| 76 // destroyed the adapter is destroyed and removed from the map. References |
| 77 // must be destroyed on the main thread. |
| 78 std::unique_ptr<AdapterRef> GetLocalStreamAdapter(const std::string& id); |
| 79 // Invoke on the main thread. Gets a new reference to the local stream adapter |
| 80 // for the web stream. If no adapter exists for the stream one is created. |
| 81 // When all references are destroyed the adapter is destroyed and removed from |
| 82 // the map. References must be destroyed on the main thread. |
| 83 std::unique_ptr<AdapterRef> GetOrCreateLocalStreamAdapter( |
| 84 const blink::WebMediaStream& web_stream); |
| 85 // Invoke on the main thread. |
| 86 size_t GetLocalStreamCount() const; |
| 87 |
| 88 protected: |
| 89 friend class base::RefCountedThreadSafe<WebRtcMediaStreamAdapterMap>; |
| 90 |
| 91 // Invoke on the main thread. |
| 92 virtual ~WebRtcMediaStreamAdapterMap(); |
| 93 |
| 94 private: |
| 95 // Pointer to a |PeerConnectionDependencyFactory| owned by the |RenderThread|. |
| 96 // It's valid for the lifetime of |RenderThread|. |
| 97 PeerConnectionDependencyFactory* const factory_; |
| 98 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; |
| 99 // Takes care of creating and owning track adapters, used by stream adapters. |
| 100 scoped_refptr<WebRtcMediaStreamTrackAdapterMap> track_adapter_map_; |
| 101 |
| 102 std::map<std::string, AdapterEntry> local_stream_adapters_; |
| 103 // TODO(hbos): Take care of remote stream adapters as well. This will require |
| 104 // usage of the signaling thread. crbug.com/705901 |
| 105 }; |
| 106 |
| 107 } // namespace content |
| 108 |
| 109 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_ADAPTER_MAP_H_ |
| OLD | NEW |