简体   繁体   English

使用 socket.io 流式传输网络摄像头

[英]Stream WebCam using socket.io

I have been trying to implement a web application that will be able to handle following scenario:我一直在尝试实现一个能够处理以下场景的 Web 应用程序:

  • Streaming video/audio from a client to other clients (actually a particular set of them, no broadcasting) and server at the same time.同时将视频/音频从客户端流式传输到其他客户端(实际上是一组特定的客户端,没有广播)和服务器。 The data source would be a webcam of the client.数据源将是客户端的网络摄像头。
  • This streamed data has to be displayed in the real time on the other clients' browser and be saved on the server side for the 'archiving' purposes.该流数据必须实时显示在其他客户端的浏览器上,保存在服务器端以用于“存档”目的。
  • It has to be implemented in node.js + socket.io environment.它必须在 node.js + socket.io 环境中实现。

To put it in some more specific context... The scenario is that there is a guy that makes a kind of a room for the users that he chooses.把它放在一些更具体的上下文中...... 场景是有一个人为他选择的用户创造了一个房间。 After the chosen users join the room, the creator starts streaming video/audio from his/her built in devices (webcam).选定的用户加入房间后,创建者开始从他/她的内置设备(网络摄像头)流式传输视频/音频。 All of the guests receive the data in real time, moreover the data is being sent to the server where it is stored so it can be recovered after the stream and room get closed.所有客人都实时接收数据,而且数据正在发送到存储它的服务器,以便在流和房间关闭后可以恢复。

I was thinking about mixing Socket.IO with WebRTC.我正在考虑将 Socket.IO 与 WebRTC 混合使用。 In theory the combination of these two seem just perfect for the job.从理论上讲,这两者的结合似乎非常适合这项工作。 The Socket.IO is great for gathering specific set of users by assigning some sockets to a room and for signaling process demanded by the WebRTC. Socket.IO 非常适合通过将一些套接字分配给房间来收集特定的用户集以及 WebRTC 要求的信号处理。 At the same time WebRTC is awesome for P2P connection between users gathered in the same room, it is also really easy to get access to the webcam and other built in devices that I might want to use.同时,WebRTC 非常适合聚集在同一个房间的用户之间的 P2P 连接,访问网络摄像头和我可能想要使用的其他内置设备也非常容易。

So yeah, everything is looking pretty decent in theory but I would really need to see some code in action so I could actually try to implement it on my own.所以是的,理论上一切看起来都不错,但我真的需要看到一些代码在运行,这样我才能真正尝试自己实现它。 Moreover, I see some issues:此外,我看到了一些问题:

  • How do I save the stream that is sent by the P2P connection?如何保存P2P连接发送的流? Obviously server does not have access to that.显然服务器无权访问它。 I was thinking that I might treat the server as another 'guest', so it would be just another endpoint of the P2P connection with the creator of the room.我在想我可能会将服务器视为另一个“客人”,因此它只是与房间创建者的 P2P 连接的另一个端点。 Somehow it feels edgy, though.不过,不知怎的,它感觉很前卫。
  • Wouldn't it be better to treat server as the middleman between the creator and the clients?将服务器视为创建者和客户端之间的中间人不是更好吗? At one point there might be some, probably insignificant, delay comparing to P2P but presumably it would be the same for all the clients.在某一时刻,与 P2P 相比可能会有一些可能微不足道的延迟,但大概对所有客户端来说都是一样的。 (I tried that but I can't get the streaming from webcam to the server done, that's however is the topic for a different question as I am having problems with processing the MediaStream) (我试过了,但我无法完成从网络摄像头到服务器的流式传输,但是这是另一个问题的主题,因为我在处理 MediaStream 时遇到了问题)

I was looking for some nice solutions but without any success.我一直在寻找一些不错的解决方案,但没有任何成功。 I have seen that there is this nice P2P solution made for socket.io: http://socket.io/blog/socket-io-p2p/ .我已经看到有一个为 socket.io 制作的不错的 P2P 解决方案: http ://socket.io/blog/socket-io-p2p/。 The thing is - I don't think it will handle the data stream well.问题是 - 我认为它不能很好地处理数据流。 The examples mention only simple chat app and I need something a little bit heavier than that.这些例子只提到了简单的聊天应用程序,我需要比这更重的东西。

I would be really thankful for some specific examples, docs, whatever may lead me a little closer to the implementation of it as I really don't know how to approach it.我真的很感谢一些具体的例子,文档,任何可能让我更接近它的实现的东西,因为我真的不知道如何处理它。

Thanks in advance :)提前致谢:)

You task can be solved by using one of the open source WebRTC-servers.您的任务可以通过使用开源 WebRTC 服务器之一来解决。 For example, kurento .例如, kurento

You can realize schemas of stream:您可以实现流的模式:

  • One to one一对一
  • One to many一对多
  • Many to many多对多

WebRtc-server schema WebRtc-服务器架构

Clients will connect to each other through the WebRTC server.客户端将通过 WebRTC 服务器相互连接。 So, on server side you can record the stream, or send it for transcoding.因此,在服务器端,您可以记录流,或将其发送以进行转码。

webSocket is used for communicating with server. webSocket 用于与服务器通信。

You can find some examples according to your task您可以根据您的任务找到一些示例

Video streaming to multiple users is a really hard problem that unfortunately requires extensive infrastructure to achieve.视频流向多个用户是一个非常困难的问题,不幸的是需要大量的基础设施才能实现。 You will not be able to stream video data through a websocket.您将无法通过 websocket 流式传输视频数据。 WebRTC is also not a viable solution for what you are describing because, as you mentioned, the WebRTC protocol is P2P, as in the streaming user will need to make a direct connection to all the 'viewers'. WebRTC 也不是您所描述的可行解决方案,因为正如您所提到的,WebRTC 协议是 P2P,因为在流媒体中,用户需要与所有“观众”建立直接连接。 This will obviously not scale beyond a few 'viewers'.这显然不会超过少数“观众”。 WebRTC is more for direct video calls like in Skype for example. WebRTC 更适用于直接视频通话,例如在 Skype 中。

Here is an article describing the architecture used by a somewhat popular live streaming service. 这里有一篇文章描述了一个有点流行的实时流媒体服务所使用的架构。 As you can see achieving live video at any sort of scale will require considerable resources.如您所见,实现任何规模的实时视频都需要大量资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM