Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(548)

Side by Side Diff: content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.h

Issue 2887403003: WebRtcMediaStreamTrackMap added (Closed)
Patch Set: WrapUnique Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // Private section needed here due to |AdapterRef|'s usage of |AdapterEntry|.
32 struct AdapterEntry {
33 AdapterEntry(const scoped_refptr<WebRtcMediaStreamTrackAdapter>& adapter);
34 AdapterEntry(AdapterEntry&& other);
35 ~AdapterEntry();
36
37 AdapterEntry(const AdapterEntry&) = delete;
38 AdapterEntry& operator=(const AdapterEntry&) = delete;
39
40 scoped_refptr<WebRtcMediaStreamTrackAdapter> adapter;
41 };
42
43 public:
44 // Acts as an accessor to adapter members without leaking a reference to the
45 // adapter. When the last |AdapterRef| is destroyed, the corresponding adapter
46 // is |Dispose|d and removed from the map.
47 class CONTENT_EXPORT AdapterRef {
48 public:
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 // Warning: Holding an external reference to the adapter will prevent
62 // |~AdapterRef| from disposing the adapter.
63 WebRtcMediaStreamTrackAdapter* GetAdapterForTesting() const {
64 return adapter_.get();
65 }
66
67 private:
68 friend class WebRtcMediaStreamTrackAdapterMap;
69
70 using MapEntryIterator = std::map<std::string, AdapterEntry>::iterator;
71 enum class Type { kLocal, kRemote };
72
73 // Increments the |AdapterEntry::ref_count|. Assumes map's |lock_| is held.
74 AdapterRef(const scoped_refptr<WebRtcMediaStreamTrackAdapterMap>& map,
75 Type type,
76 const MapEntryIterator& it);
77
78 AdapterEntry* entry() { return &it_->second; }
79
80 scoped_refptr<WebRtcMediaStreamTrackAdapterMap> map_;
81 Type type_;
82 MapEntryIterator it_;
83 // A reference to the entry's adapter, ensures that |HasOneRef| is false.
84 scoped_refptr<WebRtcMediaStreamTrackAdapter> adapter_;
85 };
86
87 WebRtcMediaStreamTrackAdapterMap(
88 PeerConnectionDependencyFactory* const factory,
89 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread);
90
91 // Gets the new reference to the local track adapter by ID, or null if no such
92 // adapter was found. When all references are destroyed the adapter is
93 // disposed and removed from the map. This method can be called from any
94 // thread, but references must be destroyed on the main thread.
95 std::unique_ptr<AdapterRef> GetLocalTrackAdapter(const std::string& id);
96 // Invoke on the main thread. Gets a new reference to the local track adapter
97 // for the web track. If no adapter exists for the track one is created and
98 // initialized. When all references are destroyed the adapter is disposed and
99 // removed from the map. References must be destroyed on the main thread.
100 std::unique_ptr<AdapterRef> GetOrCreateLocalTrackAdapter(
101 const blink::WebMediaStreamTrack& web_track);
102 size_t GetLocalTrackCount() const;
103
104 // Gets the new reference to the remote track adapter by ID, or null if no
105 // such adapter was found. When all references are destroyed the adapter is
106 // disposed and removed from the map. This method can be called from any
107 // thread, but references must be destroyed on the main thread.
108 std::unique_ptr<AdapterRef> GetRemoteTrackAdapter(const std::string& id);
109 // Invoke on the webrtc signaling thread. Gets a new reference to the remote
110 // track adapter for the webrtc track. If no adapter exists for the track one
111 // is created and initialization completes on the main thread in a post. When
112 // all references are destroyed the adapter is disposed and removed from the
113 // map. References must be destroyed on the main thread.
114 std::unique_ptr<AdapterRef> GetOrCreateRemoteTrackAdapter(
115 webrtc::MediaStreamTrackInterface* webrtc_track);
116 size_t GetRemoteTrackCount() const;
117
118 protected:
119 friend class base::RefCountedThreadSafe<WebRtcMediaStreamTrackAdapterMap>;
120
121 // Invoke on the main thread.
122 virtual ~WebRtcMediaStreamTrackAdapterMap();
123
124 private:
125 std::unique_ptr<AdapterRef> GetTrackAdapter(AdapterRef::Type type,
126 const std::string& id);
127 std::unique_ptr<AdapterRef> GetOrCreateTrackAdapter(
128 AdapterRef::Type type,
129 base::Callback<scoped_refptr<WebRtcMediaStreamTrackAdapter>()>
130 create_adapter_callback,
131 const std::string& id);
132
133 // Pointer to a |PeerConnectionDependencyFactory| owned by the |RenderThread|.
134 // It's valid for the lifetime of |RenderThread|.
135 PeerConnectionDependencyFactory* const factory_;
136 scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
137
138 mutable base::Lock lock_;
139 std::map<std::string, AdapterEntry> local_track_adapters_;
140 std::map<std::string, AdapterEntry> remote_track_adapters_;
141 };
142
143 } // namespace content
144
145 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_MEDIA_STREAM_TRACK_COLLECTION_H_
OLDNEW
« no previous file with comments | « content/renderer/BUILD.gn ('k') | content/renderer/media/webrtc/webrtc_media_stream_track_adapter_map.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698