1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
// Copyright (c) 2013 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.
#include "content/renderer/media/remote_media_stream_impl.h"
#include <string>
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "content/renderer/media/media_stream_dependency_factory.h"
#include "content/renderer/media/media_stream_extra_data.h"
#include "third_party/WebKit/public/platform/WebString.h"
namespace content {
// RemoteMediaStreamTrackObserver is responsible for listening on change
// notification on a remote webrtc MediaStreamTrack and notify WebKit.
class RemoteMediaStreamTrackObserver
: NON_EXPORTED_BASE(public webrtc::ObserverInterface),
NON_EXPORTED_BASE(public base::NonThreadSafe) {
public:
RemoteMediaStreamTrackObserver(
webrtc::MediaStreamTrackInterface* webrtc_track,
const blink::WebMediaStreamTrack& webkit_track);
virtual ~RemoteMediaStreamTrackObserver();
webrtc::MediaStreamTrackInterface* observered_track() {
return webrtc_track_.get();
}
const blink::WebMediaStreamTrack& webkit_track() { return webkit_track_; }
private:
// webrtc::ObserverInterface implementation.
virtual void OnChanged() OVERRIDE;
webrtc::MediaStreamTrackInterface::TrackState state_;
scoped_refptr<webrtc::MediaStreamTrackInterface> webrtc_track_;
blink::WebMediaStreamTrack webkit_track_;
DISALLOW_COPY_AND_ASSIGN(RemoteMediaStreamTrackObserver);
};
} // namespace content
namespace {
void InitializeWebkitTrack(webrtc::MediaStreamTrackInterface* track,
blink::WebMediaStreamTrack* webkit_track,
blink::WebMediaStreamSource::Type type) {
blink::WebMediaStreamSource webkit_source;
blink::WebString webkit_track_id(UTF8ToUTF16(track->id()));
webkit_source.initialize(webkit_track_id, type, webkit_track_id);
webkit_track->initialize(webkit_track_id, webkit_source);
content::MediaStreamDependencyFactory::AddNativeTrackToBlinkTrack(track,
*webkit_track);
}
content::RemoteMediaStreamTrackObserver* FindTrackObserver(
webrtc::MediaStreamTrackInterface* track,
const ScopedVector<content::RemoteMediaStreamTrackObserver>& observers) {
ScopedVector<content::RemoteMediaStreamTrackObserver>::const_iterator it =
observers.begin();
for (; it != observers.end(); ++it) {
if ((*it)->observered_track() == track)
return *it;
}
return NULL;
}
} // namespace anonymous
namespace content {
RemoteMediaStreamTrackObserver::RemoteMediaStreamTrackObserver(
webrtc::MediaStreamTrackInterface* webrtc_track,
const blink::WebMediaStreamTrack& webkit_track)
: state_(webrtc_track->state()),
webrtc_track_(webrtc_track),
webkit_track_(webkit_track) {
webrtc_track->RegisterObserver(this);
}
RemoteMediaStreamTrackObserver::~RemoteMediaStreamTrackObserver() {
webrtc_track_->UnregisterObserver(this);
}
void RemoteMediaStreamTrackObserver::OnChanged() {
DCHECK(CalledOnValidThread());
webrtc::MediaStreamTrackInterface::TrackState state = webrtc_track_->state();
if (state == state_)
return;
state_ = state;
switch (state) {
case webrtc::MediaStreamTrackInterface::kInitializing:
// Ignore the kInitializing state since there is no match in
// WebMediaStreamSource::ReadyState.
break;
case webrtc::MediaStreamTrackInterface::kLive:
webkit_track_.source().setReadyState(
blink::WebMediaStreamSource::ReadyStateLive);
break;
case webrtc::MediaStreamTrackInterface::kEnded:
webkit_track_.source().setReadyState(
blink::WebMediaStreamSource::ReadyStateEnded);
break;
default:
NOTREACHED();
break;
}
}
RemoteMediaStreamImpl::RemoteMediaStreamImpl(
webrtc::MediaStreamInterface* webrtc_stream)
: webrtc_stream_(webrtc_stream) {
webrtc_stream_->RegisterObserver(this);
webrtc::AudioTrackVector webrtc_audio_tracks =
webrtc_stream_->GetAudioTracks();
blink::WebVector<blink::WebMediaStreamTrack> webkit_audio_tracks(
webrtc_audio_tracks.size());
// Initialize WebKit audio tracks.
size_t i = 0;
for (; i < webrtc_audio_tracks.size(); ++i) {
webrtc::AudioTrackInterface* audio_track = webrtc_audio_tracks[i];
DCHECK(audio_track);
InitializeWebkitTrack(audio_track, &webkit_audio_tracks[i],
blink::WebMediaStreamSource::TypeAudio);
audio_track_observers_.push_back(
new RemoteMediaStreamTrackObserver(audio_track,
webkit_audio_tracks[i]));
}
// Initialize WebKit video tracks.
webrtc::VideoTrackVector webrtc_video_tracks =
webrtc_stream_->GetVideoTracks();
blink::WebVector<blink::WebMediaStreamTrack> webkit_video_tracks(
webrtc_video_tracks.size());
for (i = 0; i < webrtc_video_tracks.size(); ++i) {
webrtc::VideoTrackInterface* video_track = webrtc_video_tracks[i];
DCHECK(video_track);
InitializeWebkitTrack(video_track, &webkit_video_tracks[i],
blink::WebMediaStreamSource::TypeVideo);
video_track_observers_.push_back(
new RemoteMediaStreamTrackObserver(video_track,
webkit_video_tracks[i]));
}
webkit_stream_.initialize(UTF8ToUTF16(webrtc_stream->label()),
webkit_audio_tracks, webkit_video_tracks);
webkit_stream_.setExtraData(new MediaStreamExtraData(webrtc_stream, false));
}
RemoteMediaStreamImpl::~RemoteMediaStreamImpl() {
webrtc_stream_->UnregisterObserver(this);
}
void RemoteMediaStreamImpl::OnChanged() {
// Find removed audio tracks.
ScopedVector<RemoteMediaStreamTrackObserver>::iterator audio_it =
audio_track_observers_.begin();
while (audio_it != audio_track_observers_.end()) {
std::string track_id = (*audio_it)->observered_track()->id();
if (webrtc_stream_->FindAudioTrack(track_id) == NULL) {
webkit_stream_.removeTrack((*audio_it)->webkit_track());
audio_it = audio_track_observers_.erase(audio_it);
} else {
++audio_it;
}
}
// Find removed video tracks.
ScopedVector<RemoteMediaStreamTrackObserver>::iterator video_it =
video_track_observers_.begin();
while (video_it != video_track_observers_.end()) {
std::string track_id = (*video_it)->observered_track()->id();
if (webrtc_stream_->FindVideoTrack(track_id) == NULL) {
webkit_stream_.removeTrack((*video_it)->webkit_track());
video_it = video_track_observers_.erase(video_it);
} else {
++video_it;
}
}
// Find added audio tracks.
webrtc::AudioTrackVector webrtc_audio_tracks =
webrtc_stream_->GetAudioTracks();
for (webrtc::AudioTrackVector::iterator it = webrtc_audio_tracks.begin();
it != webrtc_audio_tracks.end(); ++it) {
if (!FindTrackObserver(*it, audio_track_observers_)) {
blink::WebMediaStreamTrack new_track;
InitializeWebkitTrack(*it, &new_track,
blink::WebMediaStreamSource::TypeAudio);
audio_track_observers_.push_back(
new RemoteMediaStreamTrackObserver(*it, new_track));
webkit_stream_.addTrack(new_track);
}
}
// Find added video tracks.
webrtc::VideoTrackVector webrtc_video_tracks =
webrtc_stream_->GetVideoTracks();
for (webrtc::VideoTrackVector::iterator it = webrtc_video_tracks.begin();
it != webrtc_video_tracks.end(); ++it) {
if (!FindTrackObserver(*it, video_track_observers_)) {
blink::WebMediaStreamTrack new_track;
InitializeWebkitTrack(*it, &new_track,
blink::WebMediaStreamSource::TypeVideo);
video_track_observers_.push_back(
new RemoteMediaStreamTrackObserver(*it, new_track));
webkit_stream_.addTrack(new_track);
}
}
}
} // namespace content
|