Bug 198912

Summary: [iOS] MediaRecorder incorrect screen orientation handling
Product: WebKit Reporter: Igor Ievsiukov <igor>
Component: WebRTCAssignee: youenn fablet <youennf>
Status: RESOLVED FIXED    
Severity: Blocker CC: boris.stodic, eric.carlson, ews-watchlist, glenn, info, jer.noble, philipj, Senthil.Ramachandran, sergio, webkit-bug-importer, wildenrother, youennf, zach
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: iPhone / iPad   
OS: iOS 12   
URL: https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder
Bug Depends on:    
Bug Blocks: 85851, 216664    
Attachments:
Description Flags
Patch
none
Patch
ews-feeder: commit-queue-
Patch
none
Patch
none
Patch
none
Patch for landing
none
Patch
none
Patch for landing none

Description Igor Ievsiukov 2019-06-17 03:27:09 PDT
When using MediaRecorder to recording a file in portrait mode, for ex. recording a video message using the frontal camera, Safari automatically switches width/height (640x480 => 480x640) but does not applies necessary transformation which results the video track to be rotated 90deg thus stretching the video making it hardly usable, please see [1].

Similar kind of distortions, upside-down video, etc. could be observed while holding the phone in any other orientation except landscape with camera on the right side.

After looking at the source code I discover recording is performed using AVAssetWriterInput[2]. According to the dev docs [3] the issue could be resolved using the "transform" property to rotate the image according to the current device orientation [5]. You can find an example of transformation for each screen orientation via the following link [6].

I would be glad to provide a patch, unfortunately iOS Simulator does not supports webcams nor it is possible to deploy a debug version of WebKit to an iOS device.

Please let me know if I can help with any further debugging.

Thank you very much in advance,
Igor

[1] https://www.dropbox.com/s/3kdx77bmw2mghvi/Vid%C3%A9o%2017.06.19%2011%2052%2040.mp4?dl=0
[2] https://github.com/WebKit/webkit/blob/master/Source/WebCore/platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.h#L70
[3] https://developer.apple.com/library/archive/qa/qa1744/_index.html#avassetwriter
[4] https://developer.apple.com/documentation/avfoundation/avassetwriterinput/1390183-transform
[5] https://github.com/remirobert/CameraEngine/blob/master/CameraEngine/CameraEngineVideoEncoder.swift#L130
[6] https://github.com/remirobert/CameraEngine/blob/master/CameraEngine/CameraEngineVideoEncoder.swift#L83-L86
Comment 1 Radar WebKit Bug Importer 2019-06-17 05:09:10 PDT
<rdar://problem/51802521>
Comment 2 borisstodic 2019-08-27 01:15:08 PDT
I am facing same issue. Created new ticket with steps to reproduce bug:

https://bugs.webkit.org/show_bug.cgi?id=201137
Comment 3 youenn fablet 2019-08-27 01:41:39 PDT
*** Bug 201137 has been marked as a duplicate of this bug. ***
Comment 4 youenn fablet 2020-09-21 02:49:18 PDT
Created attachment 409265 [details]
Patch
Comment 5 youenn fablet 2020-09-21 04:54:21 PDT
Created attachment 409269 [details]
Patch
Comment 6 youenn fablet 2020-09-21 05:08:12 PDT
Created attachment 409270 [details]
Patch
Comment 7 youenn fablet 2020-09-21 05:25:40 PDT
Created attachment 409272 [details]
Patch
Comment 8 youenn fablet 2020-09-21 06:19:23 PDT
Created attachment 409276 [details]
Patch
Comment 9 Eric Carlson 2020-09-21 10:48:11 PDT
Comment on attachment 409276 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=409276&action=review

> Source/WebCore/platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.mm:430
> +        if (sample.videoMirrored())
> +            videoTransform = CGAffineTransformScale(videoTransform, -1, 1);

It would be nice to have internals API to test mirrored mode, like we have setCameraMediaStreamTrackOrientation to test rotation.
Comment 10 youenn fablet 2020-09-21 11:06:06 PDT
(In reply to Eric Carlson from comment #9)
> Comment on attachment 409276 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=409276&action=review
> 
> > Source/WebCore/platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.mm:430
> > +        if (sample.videoMirrored())
> > +            videoTransform = CGAffineTransformScale(videoTransform, -1, 1);
> 
> It would be nice to have internals API to test mirrored mode, like we have
> setCameraMediaStreamTrackOrientation to test rotation.

I'll file a bug for that.

As you noted, I'll remove setting the transform in case of no rotation or no mirroring.
Comment 11 youenn fablet 2020-09-21 11:06:28 PDT
no rotation and no mirroring.
Comment 12 youenn fablet 2020-09-21 11:11:39 PDT
Created attachment 409286 [details]
Patch for landing
Comment 13 EWS 2020-09-21 13:57:22 PDT
Committed r267366: <https://trac.webkit.org/changeset/267366>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 409286 [details].
Comment 14 youenn fablet 2020-09-30 01:07:53 PDT
Reopening since this is not properly working
Comment 15 youenn fablet 2020-09-30 01:14:00 PDT
Created attachment 410099 [details]
Patch
Comment 16 Eric Carlson 2020-09-30 08:10:31 PDT
Comment on attachment 410099 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=410099&action=review

> Source/WebCore/platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.mm:433
> +            m_videoTransform = CGAffineTransformScale(*m_videoTransform, -1, 1);

Do we always want to flip the transform?
Comment 17 youenn fablet 2020-10-01 00:30:50 PDT
(In reply to Eric Carlson from comment #16)
> Comment on attachment 410099 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=410099&action=review
> 
> > Source/WebCore/platform/mediarecorder/cocoa/MediaRecorderPrivateWriterCocoa.mm:433
> > +            m_videoTransform = CGAffineTransformScale(*m_videoTransform, -1, 1);
> 
> Do we always want to flip the transform?

Indeed, will fix that.
Comment 18 youenn fablet 2020-10-01 00:33:11 PDT
Created attachment 410201 [details]
Patch for landing
Comment 19 EWS 2020-10-01 04:25:42 PDT
Committed r267828: <https://trac.webkit.org/changeset/267828>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 410201 [details].