Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Websocket to Canvas WebGL stream from PeterXu #620 #623

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
367 changes: 366 additions & 1 deletion js/MediaStreamRenderer.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions js/iosrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ function registerGlobals(doNotRestoreCallbacksSupport) {
);
}

// Prevent WebRTC-adapter to overide navigator.mediaDevices after shim is applied since ios 14.3
Object.freeze(navigator.mediaDevices);

window.RTCPeerConnection = RTCPeerConnection;
window.webkitRTCPeerConnection = RTCPeerConnection;
window.RTCSessionDescription = RTCSessionDescription;
Expand Down
161 changes: 161 additions & 0 deletions lib/psocket/headers/PSWebSocket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2014 Zwopple Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>
#import "PSWebSocketTypes.h"

typedef NS_ENUM(NSInteger, PSWebSocketReadyState) {
PSWebSocketReadyStateConnecting = 0,
PSWebSocketReadyStateOpen,
PSWebSocketReadyStateClosing,
PSWebSocketReadyStateClosed
};

@class PSWebSocket;

/**
* PSWebSocketDelegate
*/
@protocol PSWebSocketDelegate <NSObject>

@required
- (void)webSocketDidOpen:(PSWebSocket *)webSocket;
- (void)webSocket:(PSWebSocket *)webSocket didFailWithError:(NSError *)error;
- (void)webSocket:(PSWebSocket *)webSocket didReceiveMessage:(id)message;
- (void)webSocket:(PSWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
@optional
- (void)webSocketIsHungry:(PSWebSocket *)webSocket;
- (BOOL)webSocket:(PSWebSocket *)webSocket validateServerTrust: (SecTrustRef)trust;
@end

/**
* PSWebSocket
*/
@interface PSWebSocket : NSObject

#pragma mark - Class Methods

/**
* Given a NSURLRequest determine if it is a websocket request based on it's headers
*
* @param request request to check
*
* @return whether or not the given request is a websocket request
*/
+ (BOOL)isWebSocketRequest:(NSURLRequest *)request;

+ (NSData*) peerAddressOfStream: (NSInputStream*)inputStream;

#pragma mark - Properties

@property (nonatomic, assign, readonly) PSWebSocketReadyState readyState;
@property (nonatomic, weak) id <PSWebSocketDelegate> delegate;
@property (nonatomic, strong) dispatch_queue_t delegateQueue;

@property (nonatomic, strong, readonly) NSURLRequest* URLRequest;
@property (nonatomic, strong, readonly) NSData* remoteAddress;
@property (nonatomic, strong, readonly) NSString* remoteHost;
@property (nonatomic, strong) NSArray* SSLClientCertificates;

@property (nonatomic, strong) NSString* protocol;

#pragma mark - Initialization

/**
* Initialize a PSWebSocket instance in client mode.
*
* @param request that is to be used to initiate the handshake
*
* @return an initialized instance of PSWebSocket in client mode
*/
+ (instancetype)clientSocketWithRequest:(NSURLRequest *)request;

/**
* Initialize a PSWebSocket instance in server mode
*
* @param request request that is to be used to initiate the handshake response
* @param inputStream opened input stream to be taken over by the websocket
* @param outputStream opened output stream to be taken over by the websocket
*
* @return an initialized instance of PSWebSocket in server mode
*/
+ (instancetype)serverSocketWithRequest:(NSURLRequest *)request inputStream:(NSInputStream *)inputStream outputStream:(NSOutputStream *)outputStream;

#pragma mark - Actions

/**
* Opens the websocket connection and initiates the handshake. Once
* opened an instance of PSWebSocket can never be opened again. The
* connection obeys any timeout interval set on the NSURLRequest used
* to initialize the websocket.
*/
- (void)open;

/**
* Setting this property to YES stops the WebSocket from reading data from the TCP stream.
* This can be useful for flow control, if messages are arriving faster than the application
* can process them, so the messages don't pile up in memory.
*/
@property BOOL readPaused;

/**
* Send a message over the websocket
*
* @param message an instance of NSData or NSString to send
*/
- (void)send:(id)message;

/**
* Send a ping over the websocket
*
* @param pingData data to include with the ping
* @param handler optional callback handler when the corrosponding pong is received
*/
- (void)ping:(NSData *)pingData handler:(void (^)(NSData *pongData))handler;


/**
* Close the websocket will default to code 1000 and nil reason
*/
- (void)close;

/**
* Close the websocket with a specific code and/or reason
*
* @param code close code reason
* @param reason short textual reason why the connection was closed
*/
- (void)closeWithCode:(NSInteger)code reason:(NSString *)reason;

#pragma mark - Stream Properties

/**
* Copy a property from the streams this websocket is backed by
*
* @param key property key - see kCFStreamProperty constants
*
* @return property value
*/
- (CFTypeRef)copyStreamPropertyForKey:(NSString *)key;

/**
* Set a property on the streams this websocket is backed by. Calling this
* method once the websocket has been opened will raise an exception.
*
* @param property property value
* @param key property key - see kCFStreamProperty constants
*/
- (void)setStreamProperty:(CFTypeRef)property forKey:(NSString *)key;

@end
65 changes: 65 additions & 0 deletions lib/psocket/headers/PSWebSocketServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2014 Zwopple Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>
#import "PSWebSocket.h"

@class PSWebSocketServer;

@protocol PSWebSocketServerDelegate <NSObject>

@required

- (void)serverDidStart:(PSWebSocketServer *)server;
- (void)server:(PSWebSocketServer *)server didFailWithError:(NSError *)error;
- (void)serverDidStop:(PSWebSocketServer *)server;

- (void)server:(PSWebSocketServer *)server webSocketDidOpen:(PSWebSocket *)webSocket;
- (void)server:(PSWebSocketServer *)server webSocket:(PSWebSocket *)webSocket didReceiveMessage:(id)message;
- (void)server:(PSWebSocketServer *)server webSocket:(PSWebSocket *)webSocket didFailWithError:(NSError *)error;
- (void)server:(PSWebSocketServer *)server webSocket:(PSWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;

@optional
// Delegate may implement either one of these; variant with response is preferred:
- (BOOL)server:(PSWebSocketServer *)server
acceptWebSocketWithRequest:(NSURLRequest *)request;
- (BOOL)server:(PSWebSocketServer *)server
acceptWebSocketFrom:(NSData*)address
withRequest:(NSURLRequest *)request
trust:(SecTrustRef)trust
response:(NSHTTPURLResponse **)response;

- (void)server:(PSWebSocketServer *)server webSocketIsHungry:(PSWebSocket *)webSocket;
@end

@interface PSWebSocketServer : NSObject

#pragma mark - Properties

@property (nonatomic, weak) id <PSWebSocketServerDelegate> delegate;
@property (nonatomic, strong) dispatch_queue_t delegateQueue;
@property (readonly) uint16_t realPort;

#pragma mark - Initialization

+ (instancetype)serverWithHost:(NSString *)host port:(NSUInteger)port;
+ (instancetype)serverWithHost:(NSString *)host port:(NSUInteger)port SSLCertificates:(NSArray *)SSLCertificates;

#pragma mark - Actions

- (void)start;
- (void)stop;
- (void)setTcpNoDelay:(BOOL)on;

@end
47 changes: 47 additions & 0 deletions lib/psocket/headers/PSWebSocketTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2014 Zwopple Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, PSWebSocketMode) {
PSWebSocketModeClient = 0,
PSWebSocketModeServer
};

typedef NS_ENUM(NSInteger, PSWebSocketErrorCodes) {
PSWebSocketErrorCodeUnknown = 0,
PSWebSocketErrorCodeTimedOut,
PSWebSocketErrorCodeHandshakeFailed,
PSWebSocketErrorCodeConnectionFailed
};

typedef NS_ENUM(NSInteger, PSWebSocketStatusCode) {
PSWebSocketStatusCodeNormal = 1000,
PSWebSocketStatusCodeGoingAway = 1001,
PSWebSocketStatusCodeProtocolError = 1002,
PSWebSocketStatusCodeUnhandledType = 1003,
// 1004 reserved
PSWebSocketStatusCodeNoStatusReceived = 1005,
// 1006 reserved
PSWebSocketStatusCodeInvalidUTF8 = 1007,
PSWebSocketStatusCodePolicyViolated = 1008,
PSWebSocketStatusCodeMessageTooBig = 1009
};

#define PSWebSocketGUID @"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
#define PSWebSocketErrorDomain @"PSWebSocketErrorDomain"

// NSError userInfo keys, used with PSWebSocketErrorCodeHandshakeFailed:
#define PSHTTPStatusErrorKey @"HTTPStatus" // The HTTP status (404, etc.)
#define PSHTTPResponseErrorKey @"HTTPResponse" // The entire HTTP response as an CFHTTPMessageRef
Binary file added lib/psocket/libPocketSocket.a
Binary file not shown.
14 changes: 14 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@
<source-file src="src/PluginRTCRtpCodecParameters.swift" />
<source-file src="src/PluginRTCRtpEncodingParameters.swift" />

<!-- Import from cordova-plugin-websocket-server -->
<source-file src="src/PluginInterface.swift" />
<source-file src="src/PluginWebSocketServer.swift"/>
<header-file src="lib/psocket/headers/PSWebSocket.h"/>
<header-file src="lib/psocket/headers/PSWebSocketServer.h"/>
<header-file src="lib/psocket/headers/PSWebSocketTypes.h"/>

<!-- iOS shared dependencies -->
<framework src="AVFoundation.framework" />
<framework src="CoreGraphics.framework" />
Expand All @@ -101,6 +108,13 @@
<framework src="libsqlite3.0.dylib" />
<framework src="VideoToolbox.framework" />

<!-- Import from cordova-plugin-websocket-server -->
<framework src="Foundation.framework"/>
<framework src="CFNetwork.framework"/>
<framework src="Security.framework"/>
<framework src="libz.tbd"/>
<source-file framework="true" src="lib/psocket/libPocketSocket.a" />

<!-- WebRTC library -->
<framework src="lib/WebRTC.xcframework" custom="true" embed="true" />
</platform>
Expand Down
Loading