1

EDIT:

I'm really running out of ideas and solutions. Here are the things I have attempted:

  1. Setting a variable to the stream. Failed because window.whatever references the window of the extension!

  2. Running a function from a background script and passing the stream to set the variable. Failed because when I tried, I need to put the stream in JSON and that does not work.

  3. Dispatching a Custom Event which holds the stream in its details. Failed because the details suddenly become null when I try adding them to the details. It works with normal strings or other stuff of that sort.

I can't figure out how to do anything more than this and this is more difficult than I thought.


How can I construct a MediaStream from another one without passing it directly? For example, if I could do this by using an di, an my id was abcdefghijklmnopqrstuvwxyz0123456789, then I would be able to get a MediaStream with:

var mystream=MediaStream("abcdefghijklmnopqrstuvwxyz0123456789");

Of course I'm doing something wrong, but how can I construct a MediaStream from an a preexisting MediaStream, without passing that previous one directly?

Explanation:

I have a browser extension. It picks up a MediaStream and now has it stored in a variable. Another page will need that variable so that it can display the live stream on itself. To do this, I will need that MediaSream in that variable. I have already learned that MediaStreams can't be stored in JSON, so I can't just put it in localStorage. How can I pass this MediaStream to the webpage without directly passing it (as that's not possible)?

MDN says:

Constructor

MediaStream()

Creates and returns a new MediaStream object. You can create an empty stream, a stream which is based upon an existing stream, or a stream that contains a specified list of tracks (specified as an array of MediaStreamTrack objects).

Bold mine, links from MDN

As seen in the bold, we can create one from a preexisting MediaStream. This does work:

// stream preexists as a MediaStream

var newstream=MediaStream(stream); // makes a new MediaStream with a new id

But I want to stream media from a preexisting MediaStream. How can I pass the MediaStream from the extension to the page?

Lakshya Raj
  • 1,669
  • 3
  • 10
  • 34
  • If its [not in the spec](https://www.w3.org/TR/mediacapture-streams/#dom-mediastream-id) then its not possible. There's no data encoded in a MediaStream `id`, its just a DOMString which identifies that resource. According to MDN its also only been implemented in Firefox and Safari. – lawrence-witt Feb 24 '21 at 22:23
  • @lawrence-witt: OK... so I won't be able to do what I wanted to do. Thanks for your help. I wasn't really expecting I could, just wanted to make sure so that I could maybe save myself a bit of trouble. Thanks again! – Lakshya Raj Feb 24 '21 at 22:25
  • You can use the `id` to identify MediaStreams in an array, for example, and then pass that to a new stream constructor. I'm not sure under what circumstances you could have a MediaStream's id and yet be unable to get the resource it refers to, assuming it actually still exists. Perhaps if you share more details about what you're trying to accomplish we could suggest a better solution. – lawrence-witt Feb 24 '21 at 22:30
  • @lawrence-witt: Well, you're right - that situation isn't likely. But I have the id *inside a browser extension* and I want a webpage (that I made) to be able to get the id and display the media in a video, and from [my previous question](https://stackoverflow.com/q/66358107/14469685), I learned you can't really just pass the MediaStream as is. So I thought, *"If I have the id, then why not try to start the media with an id?"*, therefore leading to this question. – Lakshya Raj Feb 24 '21 at 22:35
  • Not sure to get everything... You want documentB to access a MediaStream generated in documentA? MediaStreams are tied to the document that generated them, so you won't be able to do it. Maybe a local RTC connection is what you want. I think [user jib](https://stackoverflow.com/users/918910/jib) has something working out of the box somewhere. – Kaiido Feb 25 '21 at 00:46

0 Answers0