AxisCommunications / media-stream-library-js

JavaScript library to handle media streams on the command line (Node.js) and in the browser.
MIT License
295 stars 101 forks source link

Wrong /axis-cgi/rtspwssession.cgi url when using BasicPlayer #856

Open SampsaKaskela opened 9 months ago

SampsaKaskela commented 9 months ago

Describe the bug I'm using BasicPlayer component from media-stream-player. I give it hostname prop which contains my camera hostname (192.168.220.70) and format (RTP_H264). Then when camera starts it begins connecting to following urls:

ws://192.168.220.70/rtsp-over-websocket http://localhost:5173/axis-cgi/rtspwssession.cgi?1701346271345

This is clearly wrong. It should not make request to localhost (that is where my UI is run with Vite) but to hostname that was given so correct would probably be

http://192.168.220.70/axis-cgi/rtspwssession.cgi?1701346271345

To Reproduce Import BasicPlayer component and give it hostname and format RTP_H264. Check console for urls where it tries to connect.

Environment (please complete the following information):

Additional context I suspect the problem might be in WsRtspVideo component that BasicPlayer uses internally. WsRtspVideo component opens Html5VideoPipeline and gives it ws config { uri: "ws url here" }. But when it opens websocket it merges with default config which is calculated like this in current code:

const defaultConfig = (
  host: string = window.location.host,
  scheme: string = window.location.protocol
): WSConfig => {
  const wsScheme = scheme === 'https:' ? 'wss:' : 'ws:'

  return {
    uri: `${wsScheme}//${host}/rtsp-over-websocket`,
    tokenUri: `${scheme}//${host}/axis-cgi/rtspwssession.cgi`,
    protocol: 'binary',
    timeout: WEBSOCKET_TIMEOUT,
  }
}

So since it merges default config and given ws config then that tokenUri part ends up being wrong. Fix maybe could be that instead of giving { uri: ws } you would give it { host: "myHost" } since defaultConfig function is called with defaultConfig(config.host, config.scheme) so in this case it should then calculate uri and tokenUri correctly. Alternatively you could pass { uri: ws, tokenUri: wsTokenUri } and that would probably work too.

SampsaKaskela commented 9 months ago

I tried adding the tokenUri to config by hardcoding the correct uri in and it seemed to work. The issues then ended up being that since my camera requires Authorization header to authenticate then I got 401 response because it was missing from request and there seemed to be no way to add Authorization header to request. After hard coding that into openWebsocket function then the request went through and stream opened. This might be separate issue tho since currently there doesn't seem to be way to include Authorization header to that request and so that request is kind of useless without that functionality.