will and way

ただの自分用メモを人に伝える形式で書くことでわかりやすくまとめてるはずのブログ

CMSampleBufferの方向を取得する

ReplayKitのRPBroadcastSampleHandlerでは1フレームの情報をCMSampleBufferとして受け取り、それを自由に扱うことができます。
今回はCMSampleBufferから方向を取得してみたいと思います。

結論からいうと下記でできます。

internal extension CMSampleBuffer {
    internal var orientation: CGImagePropertyOrientation {
        if let orientationAttachment =  CMGetAttachment(self, RPVideoSampleOrientationKey as CFString, nil) as? NSNumber {
            if let orientation = CGImagePropertyOrientation(rawValue: orientationAttachment.uint32Value) {
                return orientation
            }
        }
        return .up
    }
}

しかし、iOSの特定バージョン以前では RPVideoSampleOrientationKeyがリンクエラーで起動時に失敗します。

iOS 11.0 ~ NG
iOS 11.1 ~ NG
iOS 11.2.1 NG
iOS 11.2.5 OK

という状況です。

また、 RPVideoSampleOrientationKeyの中身はRPSampleBufferVideoOrientationなので、iOS11.0系の端末を使って下記のようにハードコードで試してみました。




しかしながら、方向の情報はnilになってしまい、取得することができませんでした。

CMGetAttachment(self, kCGImagePropertyOrientation, nil)

ダメ元で、CGImageのOrientationも試してみましたがダメ...

Apple Developer Documentation

公式の情報を見る限りはiOS11.0~使えるはずなのですが。。。使えないじゃん(泣)という情報でした。