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

Update googlecast binding and sample #24

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions googlecast/macios/GoogleCast.Binding/ApiDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ interface GoogleCastManager
// -(void)loadMediaWithUrl:(NSString * _Nonnull)url contentType:(NSString * _Nonnull)contentType title:(NSString * _Nonnull)title subtitle:(NSString * _Nonnull)subtitle imageUrl:(NSString * _Nonnull)imageUrl imageHeight:(NSInteger)imageHeight imageWidth:(NSInteger)imageWidth;
[Export ("loadMediaWithUrl:contentType:title:subtitle:imageUrl:imageHeight:imageWidth:")]
void LoadMedia (string url, string contentType, string title, string subtitle, string imageUrl, nint imageHeight, nint imageWidth);

// +(BOOL)getIsCastSessionActive __attribute__((warn_unused_result("")));
[Export ("getIsCastSessionActive")]
bool IsCastSessionActive { get; }
}


Expand Down
10 changes: 8 additions & 2 deletions googlecast/macios/GoogleCast.Binding/GoogleCast.Binding.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@

<Target Name="NativeDependencies">
<PropertyGroup>
<GoogleCastiOSSdkVersion>4.8.0</GoogleCastiOSSdkVersion>
<GoogleCastiOSSdkUrl>https://dl.google.com/dl/chromecast/sdk/ios/GoogleCastSDK-ios-$(GoogleCastiOSSdkVersion)_dynamic_xcframework.zip</GoogleCastiOSSdkUrl>
<!-- TODO: Update this URL once a new official cast SDK is released.
The 4.8.x versions of the Google Cast SDK contains a bug that breaks the cast button on physical iOS devices:
https://issuetracker.google.com/issues/298066142

<GoogleCastiOSSdkVersion>4.8.1</GoogleCastiOSSdkVersion>
<GoogleCastiOSSdkUrl>https://dl.google.com/dl/chromecast/sdk/ios/GoogleCastSDK-ios-$(GoogleCastiOSSdkVersion)_dynamic.zip</GoogleCastiOSSdkUrl>
-->
<GoogleCastiOSSdkUrl>https://github.com/react-native-google-cast/google-cast-sdk-dynamic-xcframework-no-bluetooth/archive/refs/tags/4.7.1.zip</GoogleCastiOSSdkUrl>
</PropertyGroup>

<DownloadFile SourceUrl="$(GoogleCastiOSSdkUrl)" DestinationFolder="$([System.IO.Path]::GetFullPath($(MSBuildProjectDirectory)/../native/build/deps))">
Expand Down
3 changes: 3 additions & 0 deletions googlecast/macios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ This folder contains a slim binding for the Google Cast SDK.
```

### Configure
To deploy this sample to a physical iOS device you will need to open `native/MauiGoogleCast.xcodeproj`
in Xcode and configure the signing team/settings in Targets -> MauiGoogleCast -> Signing & Capabilities.

For more details, reference the [Get Started][0] page.

[0]: https://developers.google.com/cast
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
/* Begin PBXBuildFile section */
64B9934B2B97A71D00BAFB55 /* MauiGoogleCast.h in Headers */ = {isa = PBXBuildFile; fileRef = 64B9934A2B97A71D00BAFB55 /* MauiGoogleCast.h */; settings = {ATTRIBUTES = (Public, ); }; };
64B993572B97A7B300BAFB55 /* MauiGoogleCast.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64B993562B97A7B300BAFB55 /* MauiGoogleCast.swift */; };
D0D666252C06BE5F005451D1 /* GoogleCast.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0D666232C06BD7B005451D1 /* GoogleCast.xcframework */; };
D0D666262C06BE5F005451D1 /* GoogleCast.xcframework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D0D666232C06BD7B005451D1 /* GoogleCast.xcframework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
D0A597A62C211F9D00C52635 /* GoogleCast.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0A597A42C211EE600C52635 /* GoogleCast.xcframework */; };
D0A597A72C211F9D00C52635 /* GoogleCast.xcframework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D0A597A42C211EE600C52635 /* GoogleCast.xcframework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
D0D666272C06BE5F005451D1 /* Embed Frameworks */ = {
D0023C6E2C210F8100EFDC57 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
D0D666262C06BE5F005451D1 /* GoogleCast.xcframework in Embed Frameworks */,
D0A597A72C211F9D00C52635 /* GoogleCast.xcframework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -31,15 +31,15 @@
64B993472B97A71D00BAFB55 /* MauiGoogleCast.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MauiGoogleCast.framework; sourceTree = BUILT_PRODUCTS_DIR; };
64B9934A2B97A71D00BAFB55 /* MauiGoogleCast.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MauiGoogleCast.h; sourceTree = "<group>"; };
64B993562B97A7B300BAFB55 /* MauiGoogleCast.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MauiGoogleCast.swift; sourceTree = "<group>"; };
D0D666232C06BD7B005451D1 /* GoogleCast.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = GoogleCast.xcframework; path = "build/deps/GoogleCastSDK-ios-4.8.0_dynamic_xcframework/GoogleCast.xcframework"; sourceTree = "<group>"; };
D0A597A42C211EE600C52635 /* GoogleCast.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = GoogleCast.xcframework; path = "build/deps/google-cast-sdk-dynamic-xcframework-no-bluetooth-4.7.1/GoogleCast.xcframework"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
64B993442B97A71D00BAFB55 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D0D666252C06BE5F005451D1 /* GoogleCast.xcframework in Frameworks */,
D0A597A62C211F9D00C52635 /* GoogleCast.xcframework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -75,7 +75,7 @@
64B993512B97A7A200BAFB55 /* Frameworks */ = {
isa = PBXGroup;
children = (
D0D666232C06BD7B005451D1 /* GoogleCast.xcframework */,
D0A597A42C211EE600C52635 /* GoogleCast.xcframework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand All @@ -102,7 +102,7 @@
64B993432B97A71D00BAFB55 /* Sources */,
64B993442B97A71D00BAFB55 /* Frameworks */,
64B993452B97A71D00BAFB55 /* Resources */,
D0D666272C06BE5F005451D1 /* Embed Frameworks */,
D0023C6E2C210F8100EFDC57 /* Embed Frameworks */,
);
buildRules = (
);
Expand Down
45 changes: 26 additions & 19 deletions googlecast/macios/native/MauiGoogleCast/MauiGoogleCast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,28 @@ public class GoogleCastManager : NSObject, GCKLoggerDelegate, GCKRequestDelegate

let kReceiverAppID = kGCKDefaultMediaReceiverApplicationID
let kDebugLoggingEnabled = true

@objc
public func configure() {
let criteria = GCKDiscoveryCriteria(applicationID: kReceiverAppID)
let options = GCKCastOptions(discoveryCriteria: criteria)
GCKCastContext.setSharedInstanceWith(options)

// Enable logger.
GCKLogger.sharedInstance().delegate = self

let options = GCKCastOptions(discoveryCriteria: criteria)
GCKCastContext.setSharedInstanceWith(options)

// Enable logger.
GCKLogger.sharedInstance().delegate = self
}

@objc
public func loadMedia(url: String, contentType: String, title: String, subtitle: String, imageUrl: String, imageHeight: Int, imageWidth: Int) {
if !getIsCastSessionActive() {
logMessage("cast session not found, please start one", at: GCKLoggerLevel.error, fromFunction: "loadMedia", location: "line 31")
return
}

let url = URL.init(string: url)
guard let mediaURL = url else {
print("invalid mediaURL")
return
logMessage("invalid mediaURL", at: GCKLoggerLevel.error, fromFunction: "loadMedia", location: "line 36")
return
}

let metadata = GCKMediaMetadata()
Expand All @@ -39,28 +43,31 @@ public class GoogleCastManager : NSObject, GCKLoggerDelegate, GCKRequestDelegate
metadata.addImage(GCKImage(url: URL(string: imageUrl)!,
width: imageHeight,
height: imageWidth))

let mediaInfoBuilder = GCKMediaInformationBuilder.init(contentURL: mediaURL)
mediaInfoBuilder.streamType = GCKMediaStreamType.none;
mediaInfoBuilder.contentType = contentType //eg: video/mp4
mediaInfoBuilder.metadata = metadata;
let mediaInformation = mediaInfoBuilder.build()

let sessionManager = GCKCastContext.sharedInstance().sessionManager
if let request = sessionManager.currentSession?.remoteMediaClient?.loadMedia(mediaInformation) {
request.delegate = self

if let request = GCKCastContext.sharedInstance().sessionManager.currentSession?.remoteMediaClient?.loadMedia(mediaInformation) {
request.delegate = self
}
}

@objc
public func getIsCastSessionActive() -> Bool {
return GCKCastContext.sharedInstance().sessionManager.currentSession != nil
}

public func logMessage(_ message: String,
at level: GCKLoggerLevel,
fromFunction function: String,
location: String) {
at level: GCKLoggerLevel,
fromFunction function: String,
location: String) {
if (kDebugLoggingEnabled) {
print(function + " - " + message)
print(function + " - " + message)
}
}
}
}


Expand Down
2 changes: 1 addition & 1 deletion googlecast/macios/sample/GoogleCastButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected override NativeGoogleCastButton CreatePlatformView()
#if IOS
var b = new GoogleCastButton();
b.Frame = new CoreGraphics.CGRect(0, 0, 24, 24);
b.TintColor = UIKit.UIColor.Gray;
b.TintColor = UIKit.UIColor.Orange;
return b;
#else
return null;
Expand Down
13 changes: 7 additions & 6 deletions googlecast/macios/sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Image
Source="dotnet_bot.png"
HeightRequest="185"
Aspect="AspectFit"
SemanticProperties.Description="dot net bot in a race car number eight" />

<Label
Text="Google Cast Demo"
Style="{StaticResource Headline}"
Expand All @@ -24,6 +18,13 @@
Style="{StaticResource SubHeadline}"/>

<local:GoogleCastButtonView x:Name="GoogleCastButton" />

<Button
x:Name="LoadMediaBtn"
Text="Cast Media"
Clicked="OnLoadMediaBtnClicked"
HorizontalOptions="Fill" />

</VerticalStackLayout>
</ScrollView>

Expand Down
82 changes: 44 additions & 38 deletions googlecast/macios/sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
namespace GoogleCast.Sample;

public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}

protected override async void OnAppearing()
{
base.OnAppearing();

if (googleCastManager is null)
{
googleCastManager = new GoogleCast.GoogleCastManager();
googleCastManager.Configure();

#if IOS
await Permissions.RequestAsync<Permissions.NetworkState>();
#endif
}
}

GoogleCast.GoogleCastManager? googleCastManager;

private void OnCounterClicked(object sender, EventArgs e)
{
googleCastManager?.LoadMedia("https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4",
"video/mp4",
"Big Buck Bunny",
"Big Buck Bunny (open-source movie)",
"https://peach.blender.org/wp-content/uploads/title_anouncement.jpg",
100,
100);
}
}

namespace GoogleCast.Sample;

public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}

GoogleCast.GoogleCastManager? googleCastManager;

protected override async void OnAppearing()
{
base.OnAppearing();

if (googleCastManager is null)
{
googleCastManager = new GoogleCast.GoogleCastManager();
googleCastManager.Configure();

#if IOS
await Permissions.RequestAsync<Permissions.NetworkState>();
#endif
}
}

void OnLoadMediaBtnClicked(object sender, EventArgs e)
{
if (!googleCastManager?.IsCastSessionActive ?? true)
{
DisplayAlert("Error", "Please tap the cast button to begin casting.", "OK");
return;
}

googleCastManager?.LoadMedia(
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
"video/mp4",
"Big Buck Bunny (2008)",
"Big Buck Bunny tells the story of a giant rabbit with a heart bigger than himself.",
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg",
480,
360);
}
}
Loading