OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/remote_media_stream_impl.h" | 5 #include "content/renderer/media/remote_media_stream_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "content/common/media/media_stream_track_metrics_host_messages.h" |
11 #include "content/renderer/media/media_stream.h" | 12 #include "content/renderer/media/media_stream.h" |
12 #include "content/renderer/media/media_stream_dependency_factory.h" | 13 #include "content/renderer/media/media_stream_dependency_factory.h" |
| 14 #include "content/renderer/render_thread_impl.h" |
13 #include "third_party/WebKit/public/platform/WebString.h" | 15 #include "third_party/WebKit/public/platform/WebString.h" |
14 | 16 |
15 namespace content { | 17 namespace content { |
16 | 18 |
17 // RemoteMediaStreamTrackObserver is responsible for listening on change | 19 // RemoteMediaStreamTrackObserver is responsible for listening on change |
18 // notification on a remote webrtc MediaStreamTrack and notify WebKit. | 20 // notification on a remote webrtc MediaStreamTrack and notify WebKit. |
19 class RemoteMediaStreamTrackObserver | 21 class RemoteMediaStreamTrackObserver |
20 : NON_EXPORTED_BASE(public webrtc::ObserverInterface), | 22 : NON_EXPORTED_BASE(public webrtc::ObserverInterface), |
21 NON_EXPORTED_BASE(public base::NonThreadSafe) { | 23 NON_EXPORTED_BASE(public base::NonThreadSafe) { |
22 public: | 24 public: |
23 RemoteMediaStreamTrackObserver( | 25 RemoteMediaStreamTrackObserver( |
24 webrtc::MediaStreamTrackInterface* webrtc_track, | 26 webrtc::MediaStreamTrackInterface* webrtc_track, |
25 const blink::WebMediaStreamTrack& webkit_track); | 27 const blink::WebMediaStreamTrack& webkit_track); |
26 virtual ~RemoteMediaStreamTrackObserver(); | 28 virtual ~RemoteMediaStreamTrackObserver(); |
27 | 29 |
28 webrtc::MediaStreamTrackInterface* observered_track() { | 30 webrtc::MediaStreamTrackInterface* observered_track() { |
29 return webrtc_track_.get(); | 31 return webrtc_track_.get(); |
30 } | 32 } |
31 const blink::WebMediaStreamTrack& webkit_track() { return webkit_track_; } | 33 const blink::WebMediaStreamTrack& webkit_track() { return webkit_track_; } |
32 | 34 |
33 private: | 35 private: |
34 // webrtc::ObserverInterface implementation. | 36 // webrtc::ObserverInterface implementation. |
35 virtual void OnChanged() OVERRIDE; | 37 virtual void OnChanged() OVERRIDE; |
36 | 38 |
| 39 // May be overridden by unit tests to avoid sending IPC messages. |
| 40 virtual void SendLifetimeMessage(bool creation); |
| 41 |
37 webrtc::MediaStreamTrackInterface::TrackState state_; | 42 webrtc::MediaStreamTrackInterface::TrackState state_; |
38 scoped_refptr<webrtc::MediaStreamTrackInterface> webrtc_track_; | 43 scoped_refptr<webrtc::MediaStreamTrackInterface> webrtc_track_; |
39 blink::WebMediaStreamTrack webkit_track_; | 44 blink::WebMediaStreamTrack webkit_track_; |
| 45 bool sent_ended_message_; |
40 | 46 |
41 DISALLOW_COPY_AND_ASSIGN(RemoteMediaStreamTrackObserver); | 47 DISALLOW_COPY_AND_ASSIGN(RemoteMediaStreamTrackObserver); |
42 }; | 48 }; |
43 | 49 |
| 50 // We need an ID that is unique for this observer, within the current |
| 51 // renderer process, for the lifetime of the observer object. The |
| 52 // simplest approach is to just use the object's pointer value. We |
| 53 // store it in a uint64 which will be large enough regardless of |
| 54 // platform. |
| 55 uint64 MakeUniqueId(RemoteMediaStreamTrackObserver* observer) { |
| 56 return reinterpret_cast<uint64>(reinterpret_cast<void*>(observer)); |
| 57 } |
| 58 |
44 } // namespace content | 59 } // namespace content |
45 | 60 |
46 namespace { | 61 namespace { |
47 | 62 |
48 void InitializeWebkitTrack(webrtc::MediaStreamTrackInterface* track, | 63 void InitializeWebkitTrack(webrtc::MediaStreamTrackInterface* track, |
49 blink::WebMediaStreamTrack* webkit_track, | 64 blink::WebMediaStreamTrack* webkit_track, |
50 blink::WebMediaStreamSource::Type type) { | 65 blink::WebMediaStreamSource::Type type) { |
51 blink::WebMediaStreamSource webkit_source; | 66 blink::WebMediaStreamSource webkit_source; |
52 blink::WebString webkit_track_id(base::UTF8ToUTF16(track->id())); | 67 blink::WebString webkit_track_id(base::UTF8ToUTF16(track->id())); |
53 | 68 |
(...skipping 17 matching lines...) Expand all Loading... |
71 | 86 |
72 } // namespace anonymous | 87 } // namespace anonymous |
73 | 88 |
74 namespace content { | 89 namespace content { |
75 | 90 |
76 RemoteMediaStreamTrackObserver::RemoteMediaStreamTrackObserver( | 91 RemoteMediaStreamTrackObserver::RemoteMediaStreamTrackObserver( |
77 webrtc::MediaStreamTrackInterface* webrtc_track, | 92 webrtc::MediaStreamTrackInterface* webrtc_track, |
78 const blink::WebMediaStreamTrack& webkit_track) | 93 const blink::WebMediaStreamTrack& webkit_track) |
79 : state_(webrtc_track->state()), | 94 : state_(webrtc_track->state()), |
80 webrtc_track_(webrtc_track), | 95 webrtc_track_(webrtc_track), |
81 webkit_track_(webkit_track) { | 96 webkit_track_(webkit_track), |
| 97 sent_ended_message_(false) { |
82 webrtc_track->RegisterObserver(this); | 98 webrtc_track->RegisterObserver(this); |
| 99 |
| 100 SendLifetimeMessage(true); |
83 } | 101 } |
84 | 102 |
85 RemoteMediaStreamTrackObserver::~RemoteMediaStreamTrackObserver() { | 103 RemoteMediaStreamTrackObserver::~RemoteMediaStreamTrackObserver() { |
| 104 // We send the lifetime-ended message here (it will only get sent if |
| 105 // not previously sent) in case we never received a kEnded state |
| 106 // change. |
| 107 SendLifetimeMessage(false); |
| 108 |
86 webrtc_track_->UnregisterObserver(this); | 109 webrtc_track_->UnregisterObserver(this); |
87 } | 110 } |
88 | 111 |
89 void RemoteMediaStreamTrackObserver::OnChanged() { | 112 void RemoteMediaStreamTrackObserver::OnChanged() { |
90 DCHECK(CalledOnValidThread()); | 113 DCHECK(CalledOnValidThread()); |
91 | 114 |
92 webrtc::MediaStreamTrackInterface::TrackState state = webrtc_track_->state(); | 115 webrtc::MediaStreamTrackInterface::TrackState state = webrtc_track_->state(); |
93 if (state == state_) | 116 if (state == state_) |
94 return; | 117 return; |
95 | 118 |
96 state_ = state; | 119 state_ = state; |
97 switch (state) { | 120 switch (state) { |
98 case webrtc::MediaStreamTrackInterface::kInitializing: | 121 case webrtc::MediaStreamTrackInterface::kInitializing: |
99 // Ignore the kInitializing state since there is no match in | 122 // Ignore the kInitializing state since there is no match in |
100 // WebMediaStreamSource::ReadyState. | 123 // WebMediaStreamSource::ReadyState. |
101 break; | 124 break; |
102 case webrtc::MediaStreamTrackInterface::kLive: | 125 case webrtc::MediaStreamTrackInterface::kLive: |
103 webkit_track_.source().setReadyState( | 126 webkit_track_.source().setReadyState( |
104 blink::WebMediaStreamSource::ReadyStateLive); | 127 blink::WebMediaStreamSource::ReadyStateLive); |
105 break; | 128 break; |
106 case webrtc::MediaStreamTrackInterface::kEnded: | 129 case webrtc::MediaStreamTrackInterface::kEnded: |
| 130 // This is a more reliable signal to use for duration, as |
| 131 // destruction of this object might not happen until |
| 132 // considerably later. |
| 133 SendLifetimeMessage(false); |
107 webkit_track_.source().setReadyState( | 134 webkit_track_.source().setReadyState( |
108 blink::WebMediaStreamSource::ReadyStateEnded); | 135 blink::WebMediaStreamSource::ReadyStateEnded); |
109 break; | 136 break; |
110 default: | 137 default: |
111 NOTREACHED(); | 138 NOTREACHED(); |
112 break; | 139 break; |
113 } | 140 } |
114 } | 141 } |
115 | 142 |
| 143 void RemoteMediaStreamTrackObserver::SendLifetimeMessage(bool creation) { |
| 144 // We need to mirror the lifetime state for tracks to the browser |
| 145 // process so that the duration of tracks can be accurately |
| 146 // reported, because of RenderProcessHost::FastShutdownIfPossible, |
| 147 // which in many cases will simply kill the renderer process. |
| 148 // |
| 149 // RenderThreadImpl::current() may be NULL in unit tests. |
| 150 RenderThreadImpl* render_thread = RenderThreadImpl::current(); |
| 151 if (render_thread) { |
| 152 if (creation) { |
| 153 RenderThreadImpl::current()->Send( |
| 154 new MediaStreamTrackMetricsHost_AddTrack( |
| 155 MakeUniqueId(this), |
| 156 webkit_track_.source().type() == |
| 157 blink::WebMediaStreamSource::TypeAudio, |
| 158 true)); |
| 159 } else { |
| 160 if (!sent_ended_message_) { |
| 161 sent_ended_message_ = true; |
| 162 RenderThreadImpl::current()->Send( |
| 163 new MediaStreamTrackMetricsHost_RemoveTrack(MakeUniqueId(this))); |
| 164 } |
| 165 } |
| 166 } |
| 167 } |
| 168 |
116 RemoteMediaStreamImpl::RemoteMediaStreamImpl( | 169 RemoteMediaStreamImpl::RemoteMediaStreamImpl( |
117 webrtc::MediaStreamInterface* webrtc_stream) | 170 webrtc::MediaStreamInterface* webrtc_stream) |
118 : webrtc_stream_(webrtc_stream) { | 171 : webrtc_stream_(webrtc_stream) { |
119 webrtc_stream_->RegisterObserver(this); | 172 webrtc_stream_->RegisterObserver(this); |
120 | 173 |
121 webrtc::AudioTrackVector webrtc_audio_tracks = | 174 webrtc::AudioTrackVector webrtc_audio_tracks = |
122 webrtc_stream_->GetAudioTracks(); | 175 webrtc_stream_->GetAudioTracks(); |
123 blink::WebVector<blink::WebMediaStreamTrack> webkit_audio_tracks( | 176 blink::WebVector<blink::WebMediaStreamTrack> webkit_audio_tracks( |
124 webrtc_audio_tracks.size()); | 177 webrtc_audio_tracks.size()); |
125 | 178 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 InitializeWebkitTrack(*it, &new_track, | 264 InitializeWebkitTrack(*it, &new_track, |
212 blink::WebMediaStreamSource::TypeVideo); | 265 blink::WebMediaStreamSource::TypeVideo); |
213 video_track_observers_.push_back( | 266 video_track_observers_.push_back( |
214 new RemoteMediaStreamTrackObserver(*it, new_track)); | 267 new RemoteMediaStreamTrackObserver(*it, new_track)); |
215 webkit_stream_.addTrack(new_track); | 268 webkit_stream_.addTrack(new_track); |
216 } | 269 } |
217 } | 270 } |
218 } | 271 } |
219 | 272 |
220 } // namespace content | 273 } // namespace content |
OLD | NEW |