Repository navigation
Description
Is your feature request related to a problem? Please describe.
Maintainers of SIP.js have made clear in several instances (#588, #602, #696) that using a custom MediaStream as a source instead of getUserMedia() should be done through the implementation of a custom SessionDescriptionHandler. I'm fine with that.
In 0.14 however, the library has been rewritten in typescript, and most of the methods of Web.SessionDescriptionHandler are now private. This means that, to implement the rather small change from #588, one is required to reimplement the whole getDescription() method, while previously overwriting acquire() was enough.
Duplicating everything adds to the maintenance burden, as it forces to track every change to the upstream class. Bugs can easily slip through. It also means duplicating the whole class including modifier handling, ICE, etc., which are already solved problems.
Describe the solution you'd like
Because of that, would it be possible to devise a way to only override the acquisition part of Web.SessionDescriptionHandler (ideally, only the actual stream creation part)?
I can imagine two ways this could be implemented, depending on what fits the library style best, while not adding much maintenance burden to SIP.js itself:
- through inheritance, using a protected
getMediaStreams(constraints: any): Promise<MediaStream[]>method, - through inheritance, by splitting
getDescription()into individually overridable parts, - though some sort of callback in the
SessionDescriptionHandlerOptionsinterface or in events.
Also, due to the way the library uses different options in different contexts (in this case, UA sessionDescriptionHandlerFactoryOptions at creation time but constructor options at INVITE time) it might be useful to pass the options along to whatever solution is chosen.
Describe alternatives you've considered
I tried implementing a modifier that would add mediastream tracks and call createOffer() again, but this fails due to Firefox complaining that you cannot call createOffer() twice.
I could not find any other hook or overridable methods that could make this feature available easily.
Additional context
I need to create a session without requesting user device access.
Activity
[-]Overriding media acquisition is too hard mediastream[/-][+]Overriding media acquisition is too hard[/+]nud commentedon Jul 5, 2019
For reference, my first proposal would look something like this:
I'm fine with providing the pull request as soon as a solution is chosen.
grischa commentedon Jul 8, 2019
@nud I was fighting with this, too. I agree there should be a hook to override only media acquisition.
However, in the meantime, you could try this hack, seems to work for me:
After all TS is just JS when you need to get work done ;)
nud commentedon Jul 8, 2019
@grischa that looks indeed like a solution, but I am afraid that the typescript compiler could eventually benefit from the "privateness" of the method to optimize it out / change its name.
IMO an officially supported way is desirable, especially as the need seems to be pretty common.
Also, some missing public types (
Logger,SessionDescriptionHandlerObserver) make it hard to inherit fromWeb.SessionDescriptionHandler, so that part could be improved as well.nud commentedon Jul 8, 2019
For reference, here is my current not-pretty-at-all implementation:
It is worth noting that:
getMediahandler.IMO any improvement in these areas is worth taking, especially since I don't think this is such an uncommon case.
Just off the top of my head, what about having some MediaStreamFactory class, whose default implementation would be to get the browser's user media? IT would not feel foreign to the current design and would be very flexible without requiring users to reimplement the whole SDP handling part.
egreenmachine commentedon Jul 8, 2019
We just finished discussing this internally. We think that making the
acquirefunction (and potentially others) protected is a great idea. The big issue is, once we do that, we are essentially supporting that interface moving forward. We want to take a bit of time and review these functions to make sure we like the headers and return types and then we will push an update to make them protected.nud commentedon Jul 8, 2019
@egreenmachine glad to hear it! 👍
Alternatively, providing some "mediastream" promise (that would default to getting the browser's devices) as a parameter might be a simpler and easier solution to maintain on your side, or splitting parts out as i suggested in my previous comments.
I get that supporting inheritance is a big maintenance burden (composition is usually easier) but at the same time the Session Description Handler does way more than just "media management" at this point...
Also, maybe some code could be shared between the react and the Web SDH? I didn't dig into the react one but I suspect the whole SDP management thing is very similar in both implementations.
[Web/SDH]
egreenmachine commentedon Jul 9, 2019
I pushed 7caeb88 as a first pass at this. Let me know if it covers what you were looking for. We plan to do a bit more work here after the next release is settled.
nud commentedon Jul 9, 2019
@egreenmachine at first sight it looks nice. It certainly does the job for my own current use case. The improved typing is good. The small improvements I could suggest are:
getMediaStream())Thank you very much!
egreenmachine commentedon Jul 10, 2019
We already export the types needed to make your own factory... https://github.com/onsip/SIP.js/blob/master/src/session-description-handler-factory.ts. I am not sure what else you are asking for here.
nud commentedon Jul 11, 2019
@egreenmachine what I mean is this:
if you want to inherit from
Web.SessionDescriptionHandler, you will need at some point to call the constructor. It means you need access to the following types (from the code sample above):Otherwise you cannot write this:
If you want to create a new
SessionDescriptionHandlersubclass from scratch, you need to be able to overridegetDescription()andsetDescription()so you need the following type:These can be used already, but it involves looking into the library and importing the right private files.
Most probably any type that is part of the prototype of a function that one is supposed to override should be part of the exported API.
egreenmachine commentedon Jul 11, 2019
I see. You are absolutely correct that they are not exported. I do not want to export the
Observeras we are moving towards a standard approach for the library withdelegates. I will try and find some time to make the necessary changes and get the required interfaces public.nud commentedon Jul 11, 2019
@egreenmachine if this is part of the legacy API that is going to die in 0.16 I would not spend too much time on this. Since many changes will be required anyway, importing private stuff, while bad practice, is not too bad :-)
Thank you very much for the time you spent on this issue.
john-e-riordan commentedon May 27, 2020
Hi,
#815 is a new implementation which I believe/hope addresses this issue.
Media Acquisition
The function which returns a new default SessionDescriptionHandlerFactory....
now takes a
MediaStreamFactoryas a parameter...So you may provide whatever implementation you wish to acquire a local
MediaStream.If a
MediaStreamFactoryis not specified, a default which usesgetUserMedia()is provided.This can be set via the existing
UserAgentOption.sessionDescriptionHandlerFactory.Extending SessionDescription Handler
I've attempt to make the new implementation extensible enough to be useful but without going so far as creating a maintenance headache. But I'm interested in feedback - particularly if it ends up resulting a "less is more" outcome.
nud commentedon May 28, 2020
@john-e-riordan here is what my current (0.16) implementation looks like:
I suppose the 0.16 API already removed a bunch of obstacles. But the MediaStreamFactory sure looks nice as long as it can be set in the Invite options or receive parameters from there.
nud commentedon May 28, 2020
With your MR I suppose the code above will become something like this:
Then in the UA options:
Is that correct? I couldn't find the place where you set the stream factory in the MR.
john-e-riordan commentedon May 28, 2020
Hey,
john-e-riordan commentedon May 28, 2020
So you mentioned "as long as it can be set in the Invite options or receive parameters from there."
From my prior comment above, you can see that the current
constraints(from the invite options) and thesessionDescriptionHandleritself get passed to the custommediaStreamFactory. I'd like to understand what else, if anything, you might need to pass and why?nud commentedon May 28, 2020
@john-e-riordan no it's fine, currently I pass everything using the constraints object, the typing of which got relaxed in the 0.16 API.
I just provided the code samples so that you get the use case, but I think your API proposal is fine.
james-criscuolo commentedon Jul 6, 2020
Closing this as the proposal was released in 0.17.0.