Skip to content

Commit 0021e51

Browse files
authored
feat(apple): media renditions, Now Playing, video upload (#116051)
* feat(apple): complete media playback and uploads * fix(apple): preserve movie picker metadata * fix(apple): harden rendition and picker routing * style(apple): fix macOS media formatting
1 parent e1cc617 commit 0021e51

25 files changed

Lines changed: 1174 additions & 168 deletions

‎apps/.i18n/native-source.json‎

Lines changed: 130 additions & 98 deletions
Large diffs are not rendered by default.

‎apps/ios/Sources/Chat/IOSGatewayChatTransport.swift‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,8 @@ struct IOSGatewayChatTransport: OpenClawChatTransport {
568568
func loadMediaArtifact(
569569
sessionKey: String,
570570
artifactId: String,
571-
kind: OpenClawChatMediaKind) async throws -> OpenClawChatLoadedMedia?
571+
kind: OpenClawChatMediaKind,
572+
playback: OpenClawChatPlaybackMode?) async throws -> OpenClawChatLoadedMedia?
572573
{
573574
guard kind.acceptsManagedArtifactID(artifactId),
574575
let mediaArtifactLoader,
@@ -586,6 +587,7 @@ struct IOSGatewayChatTransport: OpenClawChatTransport {
586587
let loaded = try await mediaArtifactLoader.load(
587588
response: response,
588589
kind: kind,
590+
playback: playback,
589591
expectedGatewayID: gatewayID)
590592
guard await self.gateway.currentRoute() == route else { throw CancellationError() }
591593
return loaded

‎apps/ios/Sources/Chat/IOSMediaArtifactLoader.swift‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ struct IOSMediaArtifactLoader: Sendable {
5050
func load(
5151
response: ArtifactsDownloadResult,
5252
kind: OpenClawChatMediaKind,
53+
playback: OpenClawChatPlaybackMode? = nil,
5354
expectedGatewayID: String) async throws -> OpenClawChatLoadedMedia
5455
{
5556
let maximumBytes = Self.maximumBytes(for: kind)
5657
let declaredMIME = response.artifact.mimetype?.lowercased()
57-
if let encoded = response.data?.trimmingCharacters(in: .whitespacesAndNewlines),
58+
if playback != .transcode,
59+
let encoded = response.data?.trimmingCharacters(in: .whitespacesAndNewlines),
5860
!encoded.isEmpty
5961
{
6062
guard response.encoding == "base64",
@@ -69,7 +71,8 @@ struct IOSMediaArtifactLoader: Sendable {
6971
let path = response.url?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
7072
guard let connection = await self.connectionProvider(),
7173
connection.gatewayID == expectedGatewayID,
72-
let url = Self.managedMediaURL(config: connection.config, path: path)
74+
let sourceURL = Self.managedMediaURL(config: connection.config, path: path),
75+
let url = Self.playbackURL(sourceURL, mode: playback)
7376
else { throw LoadError.invalidSource }
7477

7578
let headers = url.scheme?.lowercased() == "https"
@@ -82,7 +85,7 @@ struct IOSMediaArtifactLoader: Sendable {
8285
connection.config.tls == nil &&
8386
headers.isEmpty &&
8487
declaredMIME?.hasPrefix(kind.mimeTypePrefix) == true
85-
if canStreamDirectly, let declaredMIME {
88+
if canStreamDirectly, playback != .transcode, let declaredMIME {
8689
return .stream(OpenClawChatMediaStream(
8790
url: url,
8891
mimeType: declaredMIME,
@@ -92,6 +95,9 @@ struct IOSMediaArtifactLoader: Sendable {
9295
var request = URLRequest(url: url)
9396
request.timeoutInterval = kind == .video ? 60 : 20
9497
request.setValue("\(kind.rawValue)/*", forHTTPHeaderField: "Accept")
98+
if canStreamDirectly {
99+
request.setValue("bytes=0-0", forHTTPHeaderField: "Range")
100+
}
95101
for (name, value) in headers {
96102
request.setValue(value, forHTTPHeaderField: name)
97103
}
@@ -108,12 +114,21 @@ struct IOSMediaArtifactLoader: Sendable {
108114
throw LoadError.payloadTooLarge
109115
}
110116
guard let http = urlResponse as? HTTPURLResponse else { throw LoadError.invalidResponse }
117+
if http.statusCode == 202 {
118+
return .preparing
119+
}
111120
guard (200..<300).contains(http.statusCode) else {
112121
throw LoadError.requestFailed(statusCode: http.statusCode)
113122
}
114123
guard let mimeType = http.mimeType?.lowercased(),
115124
mimeType.hasPrefix(kind.mimeTypePrefix)
116125
else { throw LoadError.unsupportedMediaType }
126+
if canStreamDirectly {
127+
return .stream(OpenClawChatMediaStream(
128+
url: url,
129+
mimeType: mimeType,
130+
sizeBytes: response.artifact.sizebytes))
131+
}
117132
guard data.count <= maximumBytes else { throw LoadError.payloadTooLarge }
118133
return .data(OpenClawChatMediaData(data: data, mimeType: mimeType))
119134
}
@@ -149,4 +164,14 @@ struct IOSMediaArtifactLoader: Sendable {
149164
base.fragment = nil
150165
return base.url
151166
}
167+
168+
private static func playbackURL(_ url: URL, mode: OpenClawChatPlaybackMode?) -> URL? {
169+
guard mode == .transcode else { return url }
170+
guard var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return nil }
171+
var queryItems = components.queryItems ?? []
172+
queryItems.removeAll { $0.name == "playback" }
173+
queryItems.append(URLQueryItem(name: "playback", value: "1"))
174+
components.queryItems = queryItems
175+
return components.url
176+
}
152177
}

‎apps/ios/Tests/IOSMediaArtifactLoaderTests.swift‎

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,138 @@ struct IOSMediaArtifactLoaderTests {
135135
#expect(media.mimeType == "video/mp4")
136136
}
137137

138+
@Test @MainActor func `requests playback rendition for buffered audio`() async throws {
139+
let config = try Self.config(url: #require(URL(string: "wss://gateway.example")))
140+
let loader = IOSMediaArtifactLoader(
141+
connectionProvider: {
142+
IOSMediaArtifactLoader.Connection(
143+
config: config,
144+
gatewayID: config.effectiveStableID,
145+
customHeaders: [:])
146+
},
147+
requestFactory: { _, _ in
148+
{ request in
149+
#expect(request.url?.absoluteString == Self.ticketedPlaybackAbsoluteURL)
150+
#expect(request.value(forHTTPHeaderField: "Range") == nil)
151+
return try Self.response(
152+
for: request,
153+
mimeType: "audio/mp4",
154+
data: Data([10, 11, 12]))
155+
}
156+
})
157+
158+
let loaded = try await loader.load(
159+
response: Self.downloadResult(mimeType: "audio/x-caf"),
160+
kind: .audio,
161+
playback: .transcode,
162+
expectedGatewayID: config.effectiveStableID)
163+
164+
guard case let .data(media) = loaded else {
165+
Issue.record("audio rendition should be buffered")
166+
return
167+
}
168+
#expect(media.data == Data([10, 11, 12]))
169+
#expect(media.mimeType == "audio/mp4")
170+
}
171+
172+
@Test @MainActor func `transcode playback bypasses inline bytes for ticketed rendition`() async throws {
173+
let config = try Self.config(url: #require(URL(string: "wss://gateway.example")))
174+
let loader = IOSMediaArtifactLoader(
175+
connectionProvider: {
176+
IOSMediaArtifactLoader.Connection(
177+
config: config,
178+
gatewayID: config.effectiveStableID,
179+
customHeaders: [:])
180+
},
181+
requestFactory: { _, _ in
182+
{ request in
183+
#expect(request.url?.absoluteString == Self.ticketedPlaybackAbsoluteURL)
184+
return try Self.response(
185+
for: request,
186+
mimeType: "audio/mp4",
187+
data: Data([20, 21, 22]))
188+
}
189+
})
190+
191+
let loaded = try await loader.load(
192+
response: Self.downloadResult(
193+
mimeType: "audio/x-caf",
194+
inlineData: Data([99, 98, 97])),
195+
kind: .audio,
196+
playback: .transcode,
197+
expectedGatewayID: config.effectiveStableID)
198+
199+
guard case let .data(media) = loaded else {
200+
Issue.record("transcode playback should fetch the ticketed rendition")
201+
return
202+
}
203+
#expect(media.data == Data([20, 21, 22]))
204+
#expect(media.mimeType == "audio/mp4")
205+
}
206+
207+
@Test @MainActor func `native playback retains inline byte fast path`() async throws {
208+
let config = Self.config()
209+
let loader = IOSMediaArtifactLoader(
210+
connectionProvider: {
211+
IOSMediaArtifactLoader.Connection(
212+
config: config,
213+
gatewayID: config.effectiveStableID,
214+
customHeaders: [:])
215+
},
216+
requestFactory: { _, _ in
217+
Issue.record("native inline bytes must not reach the URL path")
218+
return { _ in throw CancellationError() }
219+
})
220+
221+
let loaded = try await loader.load(
222+
response: Self.downloadResult(
223+
mimeType: "audio/mpeg",
224+
inlineData: Data([30, 31, 32])),
225+
kind: .audio,
226+
playback: .native,
227+
expectedGatewayID: config.effectiveStableID)
228+
229+
guard case let .data(media) = loaded else {
230+
Issue.record("native playback should retain inline bytes")
231+
return
232+
}
233+
#expect(media.data == Data([30, 31, 32]))
234+
#expect(media.mimeType == "audio/mpeg")
235+
}
236+
237+
@Test @MainActor func `returns preparing for a pending streamed video rendition`() async throws {
238+
let config = try Self.config(url: #require(URL(string: "wss://gateway.example")))
239+
let loader = IOSMediaArtifactLoader(
240+
connectionProvider: {
241+
IOSMediaArtifactLoader.Connection(
242+
config: config,
243+
gatewayID: config.effectiveStableID,
244+
customHeaders: [:])
245+
},
246+
requestFactory: { _, _ in
247+
{ request in
248+
#expect(request.url?.absoluteString == Self.ticketedPlaybackAbsoluteURL)
249+
#expect(request.value(forHTTPHeaderField: "Range") == "bytes=0-0")
250+
return try Self.response(
251+
for: request,
252+
statusCode: 202,
253+
mimeType: "application/json",
254+
data: Data(#"{"status":"preparing"}"#.utf8))
255+
}
256+
})
257+
258+
let loaded = try await loader.load(
259+
response: Self.downloadResult(mimeType: "video/x-matroska"),
260+
kind: .video,
261+
playback: .transcode,
262+
expectedGatewayID: config.effectiveStableID)
263+
264+
guard case .preparing = loaded else {
265+
Issue.record("pending rendition should remain in preparing state")
266+
return
267+
}
268+
}
269+
138270
@Test @MainActor func `rejects absolute and unticketed paths before fetching`() async {
139271
let config = Self.config()
140272
let loader = IOSMediaArtifactLoader(
@@ -170,10 +302,12 @@ struct IOSMediaArtifactLoaderTests {
170302
private static let ticketedPath =
171303
"/api/chat/media/outgoing/main/11111111-1111-4111-8111-111111111111/full?mediaTicket=ticket"
172304
private static let ticketedAbsoluteURL = "https://gateway.example\(ticketedPath)"
305+
private static let ticketedPlaybackAbsoluteURL = "\(ticketedAbsoluteURL)&playback=1"
173306

174307
private static func downloadResult(
175308
mimeType: String?,
176309
sizeBytes: Int? = nil,
310+
inlineData: Data? = nil,
177311
url: String = ticketedPath) -> ArtifactsDownloadResult
178312
{
179313
ArtifactsDownloadResult(
@@ -184,18 +318,21 @@ struct IOSMediaArtifactLoaderTests {
184318
mimetype: mimeType,
185319
sizebytes: sizeBytes,
186320
download: ["mode": AnyCodable("url")]),
321+
encoding: inlineData == nil ? nil : "base64",
322+
data: inlineData?.base64EncodedString(),
187323
url: url)
188324
}
189325

190326
private static func response(
191327
for request: URLRequest,
328+
statusCode: Int = 200,
192329
mimeType: String,
193330
data: Data) throws -> (Data, URLResponse)
194331
{
195332
let responseURL = try #require(request.url)
196333
let response = try #require(HTTPURLResponse(
197334
url: responseURL,
198-
statusCode: 200,
335+
statusCode: statusCode,
199336
httpVersion: nil,
200337
headerFields: ["Content-Type": mimeType]))
201338
return (data, response)

‎apps/ios/Tests/SwiftUIRenderSmokeTests.swift‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ struct SwiftUIRenderSmokeTests {
272272
inlineWidgetResourceResolver: { _, _ in nil },
273273
mediaArtifactResolverReady: false,
274274
mediaPlaybackAllowed: { true },
275-
loadMediaArtifact: { _, _ in nil })
275+
loadMediaArtifact: { _, _, _ in nil })
276276
.environment(\.dynamicTypeSize, typeSize)
277277

278278
_ = Self.host(root, size: CGSize(width: 320, height: 420))
@@ -312,7 +312,7 @@ struct SwiftUIRenderSmokeTests {
312312
inlineWidgetResourceResolver: { _, _ in nil },
313313
mediaArtifactResolverReady: true,
314314
mediaPlaybackAllowed: { true },
315-
loadMediaArtifact: { requested, kind in
315+
loadMediaArtifact: { requested, kind, _ in
316316
requestedArtifactId = requested
317317
#expect(kind == .image)
318318
return OpenClawChatLoadedMedia.data(OpenClawChatMediaData(
@@ -394,7 +394,7 @@ struct SwiftUIRenderSmokeTests {
394394
inlineWidgetResourceResolver: { _, _ in nil },
395395
mediaArtifactResolverReady: false,
396396
mediaPlaybackAllowed: { true },
397-
loadMediaArtifact: { _, _ in nil })
397+
loadMediaArtifact: { _, _, _ in nil })
398398
ChatStreamingAssistantBubble(
399399
text: text,
400400
markdownVariant: .standard,
@@ -451,7 +451,7 @@ struct SwiftUIRenderSmokeTests {
451451
inlineWidgetResourceResolver: { _, _ in nil },
452452
mediaArtifactResolverReady: false,
453453
mediaPlaybackAllowed: { true },
454-
loadMediaArtifact: { _, _ in nil })
454+
loadMediaArtifact: { _, _, _ in nil })
455455
.environment(\.dynamicTypeSize, typeSize)
456456

457457
_ = Self.host(root, size: CGSize(width: 320, height: 280))

0 commit comments

Comments
 (0)