Skip to content

Commit c807138

Browse files
committed
Initial commit
0 parents  commit c807138

26 files changed

+4049
-0
lines changed

.gitignore

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.DS_Store
2+
__MACOSX
3+
*.pbxuser
4+
!default.pbxuser
5+
*.mode1v3
6+
!default.mode1v3
7+
*.mode2v3
8+
!default.mode2v3
9+
*.perspectivev3
10+
!default.perspectivev3
11+
*.xcworkspace
12+
!default.xcworkspace
13+
xcuserdata
14+
profile
15+
*.moved-aside
16+
DerivedData
17+
.idea/
18+
Crashlytics.sh
19+
generatechangelog.sh
20+
Pods/

.gitmodules

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "Vendor/Protobuf"]
2+
path = Vendor/Protobuf
3+
url = https://github.com/alexeyxo/protobuf-swift.git
4+
[submodule "Vendor/SwiftyJSON"]
5+
path = Vendor/SwiftyJSON
6+
url = https://github.com/SwiftyJSON/SwiftyJSON.git

ChromeCastCore.xcodeproj/project.pbxproj

+426
Large diffs are not rendered by default.

ChromeCastCore.xcodeproj/project.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0800"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "DD674A371DB7E83400E1FC24"
18+
BuildableName = "ChromeCastCore.framework"
19+
BlueprintName = "ChromeCastCore"
20+
ReferencedContainer = "container:ChromeCastCore.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<Testables>
31+
</Testables>
32+
<AdditionalOptions>
33+
</AdditionalOptions>
34+
</TestAction>
35+
<LaunchAction
36+
buildConfiguration = "Debug"
37+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
38+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
39+
launchStyle = "0"
40+
useCustomWorkingDirectory = "NO"
41+
ignoresPersistentStateOnLaunch = "NO"
42+
debugDocumentVersioning = "YES"
43+
debugServiceExtension = "internal"
44+
allowLocationSimulation = "YES">
45+
<MacroExpansion>
46+
<BuildableReference
47+
BuildableIdentifier = "primary"
48+
BlueprintIdentifier = "DD674A371DB7E83400E1FC24"
49+
BuildableName = "ChromeCastCore.framework"
50+
BlueprintName = "ChromeCastCore"
51+
ReferencedContainer = "container:ChromeCastCore.xcodeproj">
52+
</BuildableReference>
53+
</MacroExpansion>
54+
<AdditionalOptions>
55+
</AdditionalOptions>
56+
</LaunchAction>
57+
<ProfileAction
58+
buildConfiguration = "Release"
59+
shouldUseLaunchSchemeArgsEnv = "YES"
60+
savedToolIdentifier = ""
61+
useCustomWorkingDirectory = "NO"
62+
debugDocumentVersioning = "YES">
63+
<MacroExpansion>
64+
<BuildableReference
65+
BuildableIdentifier = "primary"
66+
BlueprintIdentifier = "DD674A371DB7E83400E1FC24"
67+
BuildableName = "ChromeCastCore.framework"
68+
BlueprintName = "ChromeCastCore"
69+
ReferencedContainer = "container:ChromeCastCore.xcodeproj">
70+
</BuildableReference>
71+
</MacroExpansion>
72+
</ProfileAction>
73+
<AnalyzeAction
74+
buildConfiguration = "Debug">
75+
</AnalyzeAction>
76+
<ArchiveAction
77+
buildConfiguration = "Release"
78+
revealArchiveInOrganizer = "YES">
79+
</ArchiveAction>
80+
</Scheme>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>ChromeCastCore.xcscheme</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>1</integer>
11+
</dict>
12+
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>DD674A371DB7E83400E1FC24</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
</dict>
21+
</dict>
22+
</plist>

ChromeCastCore/CASTV2PlatformReader.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// CASTV2PlatformReader.h
3+
// ChromeCastCore
4+
//
5+
// Created by Guilherme Rambo on 20/10/16.
6+
// Copyright © 2016 Guilherme Rambo. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface CASTV2PlatformReader : NSObject
14+
15+
- (instancetype)initWithStream:(NSInputStream *)stream;
16+
17+
- (void)readStream;
18+
- (NSData *__nullable)nextMessage;
19+
20+
@end
21+
22+
NS_ASSUME_NONNULL_END

ChromeCastCore/CASTV2PlatformReader.m

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//
2+
// CASTV2PlatformReader.m
3+
// ChromeCastCore
4+
//
5+
// Created by Guilherme Rambo on 20/10/16.
6+
// Copyright © 2016 Guilherme Rambo. All rights reserved.
7+
//
8+
9+
#import "CASTV2PlatformReader.h"
10+
11+
#define MAX_BUFFER_LENGTH 8192
12+
13+
@interface CASTV2PlatformReader ()
14+
15+
@property (strong) NSInputStream *stream;
16+
17+
@property (assign) NSUInteger readPosition;
18+
@property (strong) NSMutableData *buffer;
19+
20+
@end
21+
22+
@implementation CASTV2PlatformReader
23+
24+
- (instancetype)initWithStream:(NSInputStream *)stream
25+
{
26+
self = [super init];
27+
28+
self.stream = stream;
29+
self.buffer = [[NSMutableData alloc] initWithCapacity:MAX_BUFFER_LENGTH];
30+
31+
return self;
32+
}
33+
34+
- (void)readStream
35+
{
36+
@synchronized (self) {
37+
NSInteger totalBytesRead = 0;
38+
size_t bufferSize = 32;
39+
40+
while (self.stream.hasBytesAvailable) {
41+
uint8_t *bytes = malloc(sizeof(uint8_t) * bufferSize);
42+
43+
NSInteger bytesRead = [self.stream read:bytes maxLength:bufferSize];
44+
if (bytesRead < 0) continue;
45+
46+
[self.buffer appendBytes:(const void *)bytes length:bytesRead];
47+
48+
free(bytes);
49+
50+
totalBytesRead += bytesRead;
51+
}
52+
}
53+
}
54+
55+
- (NSData *)nextMessage
56+
{
57+
@synchronized (self) {
58+
size_t headerSize = sizeof(SInt32);
59+
60+
// no data yet
61+
if (self.buffer.length - self.readPosition < headerSize) return nil;
62+
63+
// read message header (contains the size of the payload)
64+
const void *bufferPtr = self.buffer.bytes + self.readPosition;
65+
SInt32 header = 0;
66+
memcpy(&header, bufferPtr, headerSize);
67+
SInt32 payloadSize = CFSwapInt32BigToHost(header);
68+
69+
// increment buffer reading position
70+
self.readPosition += headerSize;
71+
NSUInteger payloadEnd = self.readPosition + payloadSize;
72+
// see if there's a full message in the buffer for us to read
73+
if (payloadEnd > self.buffer.length || payloadSize > self.buffer.length - self.readPosition || payloadSize < 0) {
74+
// message has not arrived yet
75+
self.readPosition -= headerSize;
76+
return nil;
77+
}
78+
79+
// memory to hold the actual message payload
80+
void *message = malloc(payloadSize);
81+
memcpy(message, (self.buffer.bytes + self.readPosition), payloadSize);
82+
83+
// increment buffer reading position for the next call
84+
self.readPosition += payloadSize;
85+
86+
[self resetBufferIfNeeded];
87+
88+
return [[NSData alloc] initWithBytesNoCopy:message length:payloadSize freeWhenDone:YES];
89+
}
90+
}
91+
92+
- (void)resetBufferIfNeeded {
93+
if (self.buffer.length < MAX_BUFFER_LENGTH) return;
94+
95+
@synchronized (self) {
96+
// store the bytes remaining on the buffer to be used later
97+
size_t remainingBytes = self.buffer.length - self.readPosition;
98+
void *rest = malloc(remainingBytes);
99+
memcpy(rest, (self.buffer.bytes + self.readPosition), remainingBytes);
100+
101+
// allocate the new buffer
102+
self.buffer = [[NSMutableData alloc] initWithBytes:(const void *)rest length:remainingBytes];
103+
104+
// reset read position
105+
self.readPosition = 0;
106+
107+
NSLog(@"BUFFER RESET WITH %zu bytes remaining", remainingBytes);
108+
}
109+
}
110+
111+
@end

ChromeCastCore/CASTV2Protocol.swift

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// CASTV2Protocol.swift
3+
// ChromeCastCore
4+
//
5+
// Created by Guilherme Rambo on 21/10/16.
6+
// Copyright © 2016 Guilherme Rambo. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
enum CastNamespace: String {
12+
case connection = "urn:x-cast:com.google.cast.tp.connection"
13+
case heartbeat = "urn:x-cast:com.google.cast.tp.heartbeat"
14+
case receiver = "urn:x-cast:com.google.cast.receiver"
15+
case media = "urn:x-cast:com.google.cast.media"
16+
}
17+
18+
enum CastMessageType: String {
19+
case ping = "PING"
20+
case pong = "PONG"
21+
case connect = "CONNECT"
22+
case close = "CLOSE"
23+
case status = "RECEIVER_STATUS"
24+
case launch = "LAUNCH"
25+
case load = "LOAD"
26+
case statusRequest = "GET_STATUS"
27+
case availableApps = "GET_APP_AVAILABILITY"
28+
case mediaStatus = "MEDIA_STATUS"
29+
}
30+
31+
extension CastMessageType {
32+
33+
var needsRequestId: Bool {
34+
switch self {
35+
case .launch, .load, .statusRequest: return true
36+
default: return false
37+
}
38+
}
39+
40+
}
41+
42+
struct CastJSONPayloadKeys {
43+
static let type = "type"
44+
static let requestId = "requestId"
45+
static let status = "status"
46+
static let applications = "applications"
47+
static let appId = "appId"
48+
static let displayName = "displayName"
49+
static let sessionId = "sessionId"
50+
static let transportId = "transportId"
51+
static let statusText = "statusText"
52+
static let isIdleScreen = "isIdleScreen"
53+
static let namespaces = "namespaces"
54+
static let volume = "volume"
55+
static let controlType = "controlType"
56+
static let level = "level"
57+
static let muted = "muted"
58+
static let mediaSessionId = "mediaSessionId"
59+
}
60+
61+
struct CastConstants {
62+
static let senderName = "sender-0"
63+
static let receiverName = "receiver-0"
64+
}

0 commit comments

Comments
 (0)