[MediaStream] Firing negotiationneeded event upon track add/remove on MediaStream

https://bugs.webkit.org/show_bug.cgi?id=125243

Patch by Thiago de Barros Lacerda <thiago.lacerda@openbossa.org> on 2013-12-05
Reviewed by Eric Carlson.

Spec states that: In particular, if an RTCPeerConnection object is consuming a MediaStream on which a track is
added, by, e.g., the addTrack() method being invoked, the RTCPeerConnection object must fire the
"negotiationneeded" event. Removal of media components must also trigger "negotiationneeded".

Source/WebCore:

Existing tests updated.

* Modules/mediastream/MediaStream.cpp:
(WebCore::MediaStream::addTrack):
(WebCore::MediaStream::removeTrack):
(WebCore::MediaStream::addObserver):
(WebCore::MediaStream::removeObserver):
* Modules/mediastream/MediaStream.h:
* Modules/mediastream/RTCPeerConnection.cpp:
(WebCore::RTCPeerConnection::~RTCPeerConnection):
(WebCore::RTCPeerConnection::addStream):
(WebCore::RTCPeerConnection::removeStream):
(WebCore::RTCPeerConnection::didAddOrRemoveTrack):
* Modules/mediastream/RTCPeerConnection.h:
* platform/mock/RTCPeerConnectionHandlerMock.cpp:
(WebCore::RTCPeerConnectionHandlerMock::addStream):

LayoutTests:

* fast/mediastream/RTCPeerConnection-AddRemoveStream-expected.txt:
* fast/mediastream/RTCPeerConnection-AddRemoveStream.html:
* fast/mediastream/RTCPeerConnection-onnegotiationneeded-expected.txt:
* fast/mediastream/RTCPeerConnection-onnegotiationneeded.html:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@160181 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 7fa8a1b1
2013-12-05 Thiago de Barros Lacerda <thiago.lacerda@openbossa.org>
[MediaStream] Firing negotiationneeded event upon track add/remove on MediaStream
https://bugs.webkit.org/show_bug.cgi?id=125243
Reviewed by Eric Carlson.
Spec states that: In particular, if an RTCPeerConnection object is consuming a MediaStream on which a track is
added, by, e.g., the addTrack() method being invoked, the RTCPeerConnection object must fire the
"negotiationneeded" event. Removal of media components must also trigger "negotiationneeded".
* fast/mediastream/RTCPeerConnection-AddRemoveStream-expected.txt:
* fast/mediastream/RTCPeerConnection-AddRemoveStream.html:
* fast/mediastream/RTCPeerConnection-onnegotiationneeded-expected.txt:
* fast/mediastream/RTCPeerConnection-onnegotiationneeded.html:
2013-12-05 Zoltan Horvath <zoltan@webkit.org>
[CSS Shapes] Fix inset when only a subset of the arguments are defined
......@@ -4,6 +4,8 @@ On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE
PASS Got a stream.
PASS pc.addStream(stream, {mandatory:{invalid:1}}) threw exception Error: SyntaxError: DOM Exception 12.
PASS pc.getLocalStreams().length is 0
PASS Got another stream.
PASS stream.id === stream2.id is false
PASS onAddStream was called.
......
......@@ -11,12 +11,14 @@
var stream2 = null;
var pc = null;
function error() {
function error()
{
testFailed('Stream generation failed.');
finishJSTest();
}
function getUserMedia(dictionary, callback) {
function getUserMedia(dictionary, callback)
{
try {
navigator.webkitGetUserMedia(dictionary, callback, error);
} catch (e) {
......@@ -25,12 +27,14 @@
}
}
function onErroneousNegotiationNeeded() {
function onErroneousNegotiationNeeded()
{
testFailed('onErroneousNegotiationNeeded was called.');
finishJSTest();
}
function onRemoveStream(event) {
function onRemoveStream(event)
{
testPassed('onRemoveStream was called.');
shouldBe('pc.getLocalStreams().length', '0');
......@@ -38,7 +42,8 @@
finishJSTest();
}
function onAddStream(event) {
function onAddStream(event)
{
testPassed('onAddStream was called.');
shouldBe('pc.getStreamById(stream.id)', 'stream');
......@@ -54,21 +59,28 @@
pc.removeStream(stream);
}
function gotStream2(s) {
function gotStream2(s)
{
testPassed('Got another stream.');
stream2 = s;
shouldBeFalse("stream.id === stream2.id");
pc = new webkitRTCPeerConnection({iceServers:[]});
pc.onnegotiationneeded = onAddStream;
pc.addStream(stream);
}
function gotStream1(s) {
function gotStream1(s)
{
testPassed('Got a stream.');
stream = s;
pc = new webkitRTCPeerConnection({iceServers:[]});
pc.onnegotiationneeded = onErroneousNegotiationNeeded;
shouldThrow("pc.addStream(stream, {mandatory:{invalid:1}})");
shouldBe('pc.getLocalStreams().length', '0');
getUserMedia({audio:true, video:true}, gotStream2);
}
......
......@@ -5,6 +5,12 @@ On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE
PASS Got a stream.
PASS onNegotiationNeeded was called.
PASS stream.getAudioTracks().length is 1
PASS stream.getAudioTracks().length is 0
PASS onNegotiationNeeded was called when track was removed.
PASS stream.getAudioTracks().length is 0
PASS stream.getAudioTracks().length is 1
PASS onNegotiationNeeded was called when track was added.
PASS successfullyParsed is true
TEST COMPLETE
......
......@@ -9,13 +9,16 @@
var stream = null;
var pc = null;
var track = null;
function error() {
function error()
{
testFailed('Stream generation failed.');
finishJSTest();
}
function getUserMedia(dictionary, callback) {
function getUserMedia(dictionary, callback)
{
try {
navigator.webkitGetUserMedia(dictionary, callback, error);
} catch (e) {
......@@ -24,13 +27,26 @@
}
}
function onNegotiationNeeded(event) {
function onNegotiationNeeded(event)
{
testPassed('onNegotiationNeeded was called.');
removeTrack();
}
function onNegotiationNeeded2(event)
{
testPassed('onNegotiationNeeded was called when track was removed.');
addTrack();
}
function onNegotiationNeeded3(event)
{
testPassed('onNegotiationNeeded was called when track was added.');
finishJSTest();
}
function gotStream(s) {
function gotStream(s)
{
testPassed('Got a stream.');
stream = s;
......@@ -40,6 +56,23 @@
pc.addStream(stream);
}
function removeTrack()
{
pc.onnegotiationneeded = onNegotiationNeeded2;
shouldBe('stream.getAudioTracks().length', '1');
track = stream.getAudioTracks()[0];
stream.removeTrack(track);
shouldBe('stream.getAudioTracks().length', '0');
}
function addTrack()
{
pc.onnegotiationneeded = onNegotiationNeeded3;
shouldBe('stream.getAudioTracks().length', '0');
stream.addTrack(track);
shouldBe('stream.getAudioTracks().length', '1');
}
getUserMedia({audio:true, video:true}, gotStream);
window.jsTestIsAsync = true;
......
2013-12-05 Thiago de Barros Lacerda <thiago.lacerda@openbossa.org>
[MediaStream] Firing negotiationneeded event upon track add/remove on MediaStream
https://bugs.webkit.org/show_bug.cgi?id=125243
Reviewed by Eric Carlson.
Spec states that: In particular, if an RTCPeerConnection object is consuming a MediaStream on which a track is
added, by, e.g., the addTrack() method being invoked, the RTCPeerConnection object must fire the
"negotiationneeded" event. Removal of media components must also trigger "negotiationneeded".
Existing tests updated.
* Modules/mediastream/MediaStream.cpp:
(WebCore::MediaStream::addTrack):
(WebCore::MediaStream::removeTrack):
(WebCore::MediaStream::addObserver):
(WebCore::MediaStream::removeObserver):
* Modules/mediastream/MediaStream.h:
* Modules/mediastream/RTCPeerConnection.cpp:
(WebCore::RTCPeerConnection::~RTCPeerConnection):
(WebCore::RTCPeerConnection::addStream):
(WebCore::RTCPeerConnection::removeStream):
(WebCore::RTCPeerConnection::didAddOrRemoveTrack):
* Modules/mediastream/RTCPeerConnection.h:
* platform/mock/RTCPeerConnectionHandlerMock.cpp:
(WebCore::RTCPeerConnectionHandlerMock::addStream):
2013-12-05 Beth Dakin <bdakin@apple.com>
Bad repaints on twitter when the tile cache has a margin
......@@ -153,7 +153,10 @@ void MediaStream::addTrack(PassRefPtr<MediaStreamTrack> prpTrack, ExceptionCode&
return;
}
addTrack(prpTrack);
if (addTrack(prpTrack)) {
for (auto observer = m_observers.begin(), end = m_observers.end(); observer != end; ++observer)
(*observer)->didAddOrRemoveTrack();
}
}
bool MediaStream::addTrack(PassRefPtr<MediaStreamTrack> prpTrack)
......@@ -184,7 +187,10 @@ void MediaStream::removeTrack(PassRefPtr<MediaStreamTrack> prpTrack, ExceptionCo
return;
}
removeTrack(prpTrack);
if (removeTrack(prpTrack)) {
for (auto observer = m_observers.begin(), end = m_observers.end(); observer != end; ++observer)
(*observer)->didAddOrRemoveTrack();
}
}
bool MediaStream::removeTrack(PassRefPtr<MediaStreamTrack> prpTrack)
......@@ -376,6 +382,19 @@ Vector<RefPtr<MediaStreamTrack>>* MediaStream::trackVectorForType(MediaStreamSou
return nullptr;
}
void MediaStream::addObserver(MediaStream::Observer* observer)
{
if (m_observers.find(observer) == notFound)
m_observers.append(observer);
}
void MediaStream::removeObserver(MediaStream::Observer* observer)
{
size_t pos = m_observers.find(observer);
if (pos != notFound)
m_observers.remove(pos);
}
} // namespace WebCore
#endif // ENABLE(MEDIA_STREAM)
......@@ -47,6 +47,11 @@ class MediaStreamCenter;
class MediaStream FINAL : public RefCounted<MediaStream>, public URLRegistrable, public ScriptWrappable, public MediaStreamPrivateClient, public EventTargetWithInlineData, public ContextDestructionObserver {
public:
class Observer {
public:
virtual void didAddOrRemoveTrack() = 0;
};
static PassRefPtr<MediaStream> create(ScriptExecutionContext&);
static PassRefPtr<MediaStream> create(ScriptExecutionContext&, PassRefPtr<MediaStream>);
static PassRefPtr<MediaStream> create(ScriptExecutionContext&, const Vector<RefPtr<MediaStreamTrack>>&);
......@@ -82,6 +87,9 @@ public:
// URLRegistrable
virtual URLRegistry& registry() const OVERRIDE;
void addObserver(Observer*);
void removeObserver(Observer*);
protected:
MediaStream(ScriptExecutionContext&, PassRefPtr<MediaStreamPrivate>);
......@@ -119,6 +127,8 @@ private:
Timer<MediaStream> m_scheduledEventTimer;
Vector<RefPtr<Event>> m_scheduledEvents;
Vector<Observer*> m_observers;
};
typedef Vector<RefPtr<MediaStream>> MediaStreamVector;
......
......@@ -165,6 +165,9 @@ RTCPeerConnection::RTCPeerConnection(ScriptExecutionContext& context, PassRefPtr
RTCPeerConnection::~RTCPeerConnection()
{
stop();
for (auto stream = m_localStreams.begin(), end = m_localStreams.end(); stream != end; ++stream)
(*stream)->removeObserver(this);
}
void RTCPeerConnection::createOffer(PassRefPtr<RTCSessionDescriptionCallback> successCallback, PassRefPtr<RTCErrorCallback> errorCallback, const Dictionary& mediaConstraints, ExceptionCode& ec)
......@@ -383,11 +386,13 @@ void RTCPeerConnection::addStream(PassRefPtr<MediaStream> prpStream, const Dicti
if (ec)
return;
m_localStreams.append(stream);
bool valid = m_peerHandler->addStream(stream->privateStream(), constraints);
if (!valid)
ec = SYNTAX_ERR;
else {
m_localStreams.append(stream);
stream->addObserver(this);
}
}
void RTCPeerConnection::removeStream(PassRefPtr<MediaStream> prpStream, ExceptionCode& ec)
......@@ -409,7 +414,7 @@ void RTCPeerConnection::removeStream(PassRefPtr<MediaStream> prpStream, Exceptio
return;
m_localStreams.remove(pos);
stream->removeObserver(this);
m_peerHandler->removeStream(stream->privateStream());
}
......@@ -601,6 +606,11 @@ void RTCPeerConnection::stop()
(*i)->stop();
}
void RTCPeerConnection::didAddOrRemoveTrack()
{
negotiationNeeded();
}
void RTCPeerConnection::changeSignalingState(SignalingState signalingState)
{
if (m_signalingState != SignalingStateClosed && m_signalingState != signalingState) {
......
......@@ -59,7 +59,7 @@ class RTCSessionDescriptionCallback;
class RTCStatsCallback;
class VoidCallback;
class RTCPeerConnection FINAL : public RefCounted<RTCPeerConnection>, public ScriptWrappable, public RTCPeerConnectionHandlerClient, public EventTargetWithInlineData, public ActiveDOMObject {
class RTCPeerConnection FINAL : public RefCounted<RTCPeerConnection>, public ScriptWrappable, public RTCPeerConnectionHandlerClient, public EventTargetWithInlineData, public ActiveDOMObject, public MediaStream::Observer {
public:
static PassRefPtr<RTCPeerConnection> create(ScriptExecutionContext&, const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionCode&);
~RTCPeerConnection();
......@@ -127,6 +127,9 @@ public:
// ActiveDOMObject
virtual void stop() OVERRIDE;
// MediaStream::Observer
virtual void didAddOrRemoveTrack() OVERRIDE;
using RefCounted<RTCPeerConnection>::ref;
using RefCounted<RTCPeerConnection>::deref;
......
......@@ -135,8 +135,12 @@ bool RTCPeerConnectionHandlerMock::addIceCandidate(PassRefPtr<RTCVoidRequest> re
return true;
}
bool RTCPeerConnectionHandlerMock::addStream(PassRefPtr<MediaStreamPrivate>, PassRefPtr<MediaConstraints>)
bool RTCPeerConnectionHandlerMock::addStream(PassRefPtr<MediaStreamPrivate>, PassRefPtr<MediaConstraints> constraints)
{
String invalidQuery = MediaConstraintsMock::verifyConstraints(constraints);
if (!invalidQuery.isEmpty())
return false;
// Spec states that every time a stream is added, a negotiationneeded event must be fired.
m_client->negotiationNeeded();
return true;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment