-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds support for capturing application audio during screen share when using a broadcast extension. Note: PR #598 should be merged first. --------- Co-authored-by: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com>
- Loading branch information
1 parent
8317d31
commit 4f2f5c2
Showing
8 changed files
with
375 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
patch type="added" "Add support for screen share audio on iOS when using a broadcast extension" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
Sources/LiveKit/Broadcast/IPC/BroadcastAudioCodec.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2025 LiveKit | ||
* | ||
* 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. | ||
*/ | ||
|
||
#if os(iOS) | ||
|
||
import AVFoundation | ||
|
||
/// Encode and decode audio samples for transport. | ||
struct BroadcastAudioCodec { | ||
struct Metadata: Codable { | ||
let sampleCount: Int32 | ||
let description: AudioStreamBasicDescription | ||
} | ||
|
||
enum Error: Swift.Error { | ||
case encodingFailed | ||
case decodingFailed | ||
} | ||
|
||
func encode(_ audioBuffer: CMSampleBuffer) throws -> (Metadata, Data) { | ||
guard let formatDescription = audioBuffer.formatDescription, | ||
let basicDescription = formatDescription.audioStreamBasicDescription, | ||
let blockBuffer = audioBuffer.dataBuffer | ||
else { | ||
throw Error.encodingFailed | ||
} | ||
|
||
var count = 0 | ||
var dataPointer: UnsafeMutablePointer<Int8>? | ||
|
||
guard CMBlockBufferGetDataPointer( | ||
blockBuffer, | ||
atOffset: 0, | ||
lengthAtOffsetOut: nil, | ||
totalLengthOut: &count, | ||
dataPointerOut: &dataPointer | ||
) == kCMBlockBufferNoErr, let dataPointer else { | ||
throw Error.encodingFailed | ||
} | ||
|
||
let data = Data(bytes: dataPointer, count: count) | ||
let metadata = Metadata( | ||
sampleCount: Int32(audioBuffer.numSamples), | ||
description: basicDescription | ||
) | ||
return (metadata, data) | ||
} | ||
|
||
func decode(_ encodedData: Data, with metadata: Metadata) throws -> AVAudioPCMBuffer { | ||
guard !encodedData.isEmpty else { | ||
throw Error.decodingFailed | ||
} | ||
|
||
var description = metadata.description | ||
guard let format = AVAudioFormat(streamDescription: &description) else { | ||
throw Error.decodingFailed | ||
} | ||
|
||
let sampleCount = AVAudioFrameCount(metadata.sampleCount) | ||
guard let pcmBuffer = AVAudioPCMBuffer( | ||
pcmFormat: format, | ||
frameCapacity: sampleCount | ||
) else { | ||
throw Error.decodingFailed | ||
} | ||
pcmBuffer.frameLength = sampleCount | ||
|
||
guard format.isInterleaved else { | ||
throw Error.decodingFailed | ||
} | ||
|
||
guard let mData = pcmBuffer.audioBufferList.pointee.mBuffers.mData else { | ||
throw Error.decodingFailed | ||
} | ||
encodedData.copyBytes( | ||
to: mData.assumingMemoryBound(to: UInt8.self), | ||
count: encodedData.count | ||
) | ||
return pcmBuffer | ||
} | ||
} | ||
|
||
extension AudioStreamBasicDescription: Codable { | ||
public func encode(to encoder: any Encoder) throws { | ||
var container = encoder.unkeyedContainer() | ||
try container.encode(mSampleRate) | ||
try container.encode(mFormatID) | ||
try container.encode(mFormatFlags) | ||
try container.encode(mBytesPerPacket) | ||
try container.encode(mFramesPerPacket) | ||
try container.encode(mBytesPerFrame) | ||
try container.encode(mChannelsPerFrame) | ||
try container.encode(mBitsPerChannel) | ||
} | ||
|
||
public init(from decoder: any Decoder) throws { | ||
var container = try decoder.unkeyedContainer() | ||
try self.init( | ||
mSampleRate: container.decode(Float64.self), | ||
mFormatID: container.decode(AudioFormatID.self), | ||
mFormatFlags: container.decode(AudioFormatFlags.self), | ||
mBytesPerPacket: container.decode(UInt32.self), | ||
mFramesPerPacket: container.decode(UInt32.self), | ||
mBytesPerFrame: container.decode(UInt32.self), | ||
mChannelsPerFrame: container.decode(UInt32.self), | ||
mBitsPerChannel: container.decode(UInt32.self), | ||
mReserved: 0 // as per documentation | ||
) | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.