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

MultiChannels for tvOS player #217

Closed
wants to merge 5 commits into from
Closed
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
Binary file modified .DS_Store
Binary file not shown.
Binary file modified rts-viewer-tvos/.DS_Store
Binary file not shown.
120 changes: 112 additions & 8 deletions rts-viewer-tvos/RTSViewer.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// ChannelDetailInputBox.swift
//

import DolbyIOUIKit
import RTSCore
import SwiftUI

struct ChannelDetailInputBox: View {
@ObservedObject var viewModel: ChannelDetailInputViewModel

init(viewModel: ChannelDetailInputViewModel) {
self.viewModel = viewModel
}

var body: some View {
GeometryReader { proxy in
VStack(spacing: Layout.spacing2x) {
Text(
text: "stream-detail-input.header.label",
fontAsset: .avenirNextDemiBold(
size: FontSize.body,
style: .body
)
)

VStack(spacing: Layout.spacing1x) {
Text(
text: "channel-detail-input.start-a-channel.label",
mode: .secondary,
fontAsset: .avenirNextDemiBold(
size: FontSize.title3,
style: .title3
)
)

Text(
text: "channel-detail-input.subtitle.label",
fontAsset: .avenirNextRegular(
size: FontSize.caption2,
style: .caption2
)
)
}
ScrollView {
channelInput(placeholderStream: "channel-detail-input.streamName.placeholder1.label",
placeholderAccount: "channel-detail-input.accountId.placeholder1.label",
channelName: "channel-detail-input.channel-1.label",
streamName: $viewModel.streamName1,
accountID: $viewModel.accountID1)

channelInput(placeholderStream: "channel-detail-input.streamName.placeholder2.label",
placeholderAccount: "channel-detail-input.accountId.placeholder2.label",
channelName: "channel-detail-input.channel-2.label",
streamName: $viewModel.streamName2,
accountID: $viewModel.accountID2)

channelInput(placeholderStream: "channel-detail-input.streamName.placeholder3.label",
placeholderAccount: "channel-detail-input.accountId.placeholder3.label",
channelName: "channel-detail-input.channel-3.label",
streamName: $viewModel.streamName3,
accountID: $viewModel.accountID3)

channelInput(placeholderStream: "channel-detail-input.streamName.placeholder4.label",
placeholderAccount: "channel-detail-input.accountId.placeholder4.label",
channelName: "channel-detail-input.channel-4.label",
streamName: $viewModel.streamName4,
accountID: $viewModel.accountID4)
}

Button(
action: {
viewModel.playButtonPressed()
},
text: "stream-detail-input.play.button"
)

Spacer()
.frame(height: Layout.spacing8x)
}
.padding(.all, Layout.spacing5x)
.background(Color(uiColor: UIColor.Background.black))
.cornerRadius(Layout.cornerRadius6x)
.frame(width: proxy.size.width / 3)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}

@ViewBuilder
private func channelInput(
placeholderStream: LocalizedStringKey,
placeholderAccount: LocalizedStringKey,
channelName: LocalizedStringKey,
streamName: Binding<String>,
accountID: Binding<String>
) -> some View {
VStack(alignment: .leading, spacing: Layout.spacing2x) {
Text(text: channelName,
font: .custom("AvenirNext-Bold", size: FontSize.caption1, relativeTo: .caption)
)
.multilineTextAlignment(.leading)
.padding(.top, Layout.spacing3x)

TextField(placeholderStream, text: streamName)
.font(.avenirNextRegular(withStyle: .caption, size: FontSize.caption1))

TextField(placeholderAccount, text: accountID)
.font(.avenirNextRegular(withStyle: .caption, size: FontSize.caption1))

}
}
}

// #Preview {
// ChannelDetailInputBox(viewModel: ChannelDetailInputViewModel(isShowingChannelView: .constant(true), onPlayTapped: {}))
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// ChannelDetailInputViewModel.swift
//

import Foundation
import RTSCore
import SwiftUI

struct StreamPair {
let streamName: String
let accountID: String
}

@MainActor
class ChannelDetailInputViewModel: ObservableObject {
@Binding private var isShowingChannelView: Bool
@Binding private var channels: [Channel]?

@Published var streamName1: String = "game"
@Published var accountID1: String = "7csQUs"
@Published var streamName2: String = "multiview"
@Published var accountID2: String = "k9Mwad"
@Published var streamName3: String = "game"
@Published var accountID3: String = "7csQUs"
@Published var streamName4: String = "multiview"
@Published var accountID4: String = "k9Mwad"

init(
channels: Binding<[Channel]?>, isShowingChannelView: Binding<Bool>) {
self._channels = channels
self._isShowingChannelView = isShowingChannelView
}

func playButtonPressed() {
var confirmedChannels = [Channel]()
let streamDetails = createStreamDetailArray()
for detail in streamDetails {
guard let channel = setupChannel(for: detail) else { return }
confirmedChannels.append(channel)
}
guard !confirmedChannels.isEmpty else { return }
channels = confirmedChannels
isShowingChannelView = true
}
}

private extension ChannelDetailInputViewModel {
func setupChannel(for detail: StreamPair) -> Channel? {
guard !detail.streamName.isEmpty, !detail.accountID.isEmpty else { return nil }
let subscriptionManager = SubscriptionManager()
let videoTracksManager = VideoTracksManager(subscriptionManager: subscriptionManager)
return Channel(streamDetail: detail,
subscriptionManager: subscriptionManager,
videoTracksManager: videoTracksManager)
}

func createStreamDetailArray() -> [StreamPair] {
var streamPairs = [StreamPair]()
if !streamName1.isEmpty, !accountID1.isEmpty {
let streamPair = StreamPair(streamName: streamName1, accountID: accountID1)
streamPairs.append(streamPair)
}
if !streamName2.isEmpty, !accountID2.isEmpty {
let streamPair = StreamPair(streamName: streamName2, accountID: accountID2)
streamPairs.append(streamPair)
}
if !streamName3.isEmpty, !accountID3.isEmpty {
let streamPair = StreamPair(streamName: streamName3, accountID: accountID3)
streamPairs.append(streamPair)
}
if !streamName4.isEmpty, !accountID4.isEmpty {
let streamPair = StreamPair(streamName: streamName4, accountID: accountID4)
streamPairs.append(streamPair)
}
return streamPairs
}

func checkIfCredentialsAreValid(streamName: String, accountID: String) -> Bool {
return streamName.count > 0 && accountID.count > 0
}
}
66 changes: 66 additions & 0 deletions rts-viewer-tvos/RTSViewer/ChannelGridView/ChannelGridView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// ChannelGridView.swift
//

import DolbyIOUIKit
import MillicastSDK
import RTSCore
import SwiftUI

struct ChannelGridView: View {
private let viewModel: ChannelGridViewModel

static let numberOfColumns = 2

init(viewModel: ChannelGridViewModel) {
self.viewModel = viewModel
}

var body: some View {
GeometryReader { proxy in
let screenSize = proxy.size
let tileWidth = screenSize.width / CGFloat(Self.numberOfColumns)
let columns = [GridItem](repeating: GridItem(.flexible(), spacing: Layout.spacing1x), count: Self.numberOfColumns)

LazyVGrid(columns: columns, alignment: .leading) {
ForEach(viewModel.channels) { channel in
let source = channel.source
let preferredVideoQuality: VideoQuality = .auto
let displayLabel = source.sourceId.displayLabel
let viewId = "\(ChannelGridView.self).\(displayLabel)"
VideoRendererView(source: source,
isSelectedVideoSource: true,
isSelectedAudioSource: true,
showSourceLabel: false,
showAudioIndicator: false,
maxWidth: tileWidth,
maxHeight: .infinity,
accessibilityIdentifier: "ChannelGridViewVideoTile.\(source.sourceId.displayLabel)",
preferredVideoQuality: preferredVideoQuality,
subscriptionManager: channel.subscriptionManager,
videoTracksManager: channel.videoTracksManager)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
.onAppear {
ChannelGridViewModel.logger.debug("♼ Channel Grid view: Video view appear for \(source.sourceId)")
Task {
await channel.videoTracksManager.enableTrack(for: source, with: preferredVideoQuality, on: viewId)
}
}
.onDisappear {
ChannelGridViewModel.logger.debug("♼ Channel Grid view: Video view disappear for \(source.sourceId)")
Task {
await channel.videoTracksManager.disableTrack(for: source, on: viewId)
}
}
.id(source.id)
.id(channel.source.id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second id view modifier is redundant?

}
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
}
}
}

#Preview {
ChannelGridView(viewModel: ChannelGridViewModel(channels: []))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// ChannelGridViewModel.swift
//

import Foundation
import MillicastSDK
import os
import SwiftUI

@MainActor
final class ChannelGridViewModel: ObservableObject {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if no mutations it does not need to be ObservableObject and can be a struct?

static let logger = Logger(
subsystem: Bundle.main.bundleIdentifier!,
category: String(describing: ChannelGridViewModel.self)
)

let channels: [SourcedChannel]

init(channels: [SourcedChannel]) {
self.channels = channels
}
}
58 changes: 58 additions & 0 deletions rts-viewer-tvos/RTSViewer/ChannelView/ChannelView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// ChannelView.swift
//

import DolbyIOUIKit
import RTSCore
import SwiftUI

struct ChannelView: View {
@ObservedObject private var viewModel: ChannelViewModel
@ObservedObject private var themeManager = ThemeManager.shared

private var theme: Theme { themeManager.theme }

init(viewModel: ChannelViewModel) {
self.viewModel = viewModel
}

var body: some View {
NavigationView {
ZStack {
switch viewModel.state {
case let .success(channels: channels):
let viewModel = ChannelGridViewModel(channels: channels)
ChannelGridView(viewModel: viewModel)
case .loading:
progressView
case let .error(title: title, subtitle: subtitle, showLiveIndicator: showLiveIndicator):
errorView(title: title, subtitle: subtitle, showLiveIndicator: showLiveIndicator)
}
}
}
.onAppear {
UIApplication.shared.isIdleTimerDisabled = true
viewModel.viewStreams()
}
.onDisappear {
UIApplication.shared.isIdleTimerDisabled = false
viewModel.endStream()
}
}

@ViewBuilder
private func errorView(title: String, subtitle: String?, showLiveIndicator: Bool) -> some View {
ErrorView(title: title, subtitle: subtitle)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}

@ViewBuilder
private var progressView: some View {
ProgressView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}

#Preview {
ChannelView(viewModel: ChannelViewModel(channels: .constant([]), onClose: {}))
}
Loading
Loading