Skip to content

Valeria: iOS H.264 screen capture service#1674

Open
renegadelink wants to merge 1 commit into
doronz88:masterfrom
renegadelink:feature/valeria
Open

Valeria: iOS H.264 screen capture service#1674
renegadelink wants to merge 1 commit into
doronz88:masterfrom
renegadelink:feature/valeria

Conversation

@renegadelink

@renegadelink renegadelink commented May 2, 2026

Copy link
Copy Markdown

Summary

Adds ValeriaScreenCapture, a unified iOS H.264 screen-capture service, plus a pymobiledevice3 valeria CLI on top of it. Two backends behind one API:

  • macOSvaleria_cmio talks to CoreMediaIO directly via pure ctypes. Same kernel pathway QuickTime Player uses for iOS screen recording.

Both backends emit identical H264Frame objects carrying the iDevice's native H.264 video. Decode/render is the consumer's job.

Public API

from pymobiledevice3.services.valeria import ValeriaScreenCapture

cap = ValeriaScreenCapture.create(udid=None, backend="auto")
cap.start()
for frame in cap.frames():
    sys.stdout.buffer.write(frame.to_annex_b())

ValeriaScreenCapture.create() auto-selects cmio on macOS and libusb elsewhere. H264Frame.to_annex_b() produces bytes that decode straight into FFmpeg / a hardware H.264 decoder; frames() / aframes() give sync / async iterators.

CLI

pymobiledevice3 valeria -o capture.h264 --duration 10

Test plan

  • macOS: grant Screen Recording permission to the terminal (System Settings → Privacy & Security → Screen Recording).

AI Assistance

Used Claude extensively. Reviewed and verified this PR myself.

@nanoscopic

nanoscopic commented May 3, 2026

Copy link
Copy Markdown

Good job. Was wondering when someone else would do this.

Be aware that the CoreMediaIO pathway on MacOS is not stable and it crashes out relatively frequently and it can be difficult to reset it. I originally was using that as a video option years ago and removed it because it was too unstable.

How stable is this implementation? To what extent have you tested it? The main issue I've seen in implementing Valeria myself is that the timing code can lead to it crashing out.

I haven't fiddled with Valeria for maybe 2 months, but last I did I recall that it isn't always mode 5. Sometimes it needs mode 6... So I think that bit of the comment on the commit may be wrong.

@renegadelink

Copy link
Copy Markdown
Author

Good job. Was wondering when someone else would do this.

Be aware that the CoreMediaIO pathway on MacOS is not stable and it crashes out relatively frequently and it can be difficult to reset it. I originally was using that as a video option years ago and removed it because it was too unstable.

How stable is this implementation? To what extent have you tested it? The main issue I've seen in implementing Valeria myself is that the timing code can lead to it crashing out.

Thanks! I only have one device to test on but I did plenty of testing on it and didn't have an issue. I'm hoping others can help test with more devices though.

@renegadelink renegadelink changed the title valeria: iOS H.264 screen capture service Valeria: iOS H.264 screen capture service May 3, 2026
@nanoscopic

Copy link
Copy Markdown

It's doubtful that you can legally add this code into the project.

As it is an AI conversion of Quicktime Video Hack, which is MIT licensed, it is a derivative and needs to stay under the same license. It cannot be recast into GPL3 and therefore cannot be legally contributed into pymobiledevice3 without Daniel's permission.

Daniel may be willing to allow it as he owns the Copyright for that and could permit it. You should discuss it with him.

@renegadelink

renegadelink commented May 3, 2026

Copy link
Copy Markdown
Author

It's doubtful that you can legally add this code into the project.

As it is an AI conversion of Quicktime Video Hack, which is MIT licensed, it is a derivative and needs to stay under the same license. It cannot be recast into GPL3 and therefore cannot be legally contributed into pymobiledevice3 without Daniel's permission.

Daniel may be willing to allow it as he owns the Copyright for that and could permit it. You should discuss it with him.

Thanks! I'll do just that!

@nanoscopic

Copy link
Copy Markdown

@renegadelink I'll need to check my research to give you a better / clearer answer on the difference between mode 5 and 6. My recollection offhand is that it depends on the iOS version, and that you need mode 6 if you want to be able to simultaneously do xctest on some versions along with the video streaming. Essentially mode 6 is everything mode 5 is plus more things.

As there are now two implementations of Valeria out there now with you adding this, I'm likely to go ahead and release my implementation as open source as well as there is likely no point to hoarding it anymore. ( besides which that I haven't done anything with my implementation yet besides build and test it )

@nanoscopic

Copy link
Copy Markdown

@renegadelink Another option now that I think about it would be to make the derivative code portion a module and keep that MIT and then use that module from glue code in pymobiledevice3.

Probably Daniel is okay with the AI derivative using the new license though, so -shrug-. Options.

@renegadelink

renegadelink commented May 3, 2026

Copy link
Copy Markdown
Author

@renegadelink Another option now that I think about it would be to make the derivative code portion a module and keep that MIT and then use that module from glue code in pymobiledevice3.

Probably Daniel is okay with the AI derivative using the new license though, so -shrug-. Options.

Thanks @nanoscopic! I just pushed an update — Daniel's copyright + full MIT text are now at the top of valeria_libusb.py. I also opened a discussion on his repo to ask him about it.

Yea we could make it a pmd3 dependency that has MIT license if he wants or I don't hear back.

@renegadelink

Copy link
Copy Markdown
Author

@renegadelink I'll need to check my research to give you a better / clearer answer on the difference between mode 5 and 6. My recollection offhand is that it depends on the iOS version, and that you need mode 6 if you want to be able to simultaneously do xctest on some versions along with the video streaming. Essentially mode 6 is everything mode 5 is plus more things.

As there are now two implementations of Valeria out there now with you adding this, I'm likely to go ahead and release my implementation as open source as well as there is likely no point to hoarding it anymore. ( besides which that I haven't done anything with my implementation yet besides build and test it )

Your reading of mode 5 and 6 makes sense to me from my testing. It's also something I would really need more devices to test on to be sure its a good implementation as I don't even know if all devices have mode 6 (which I kinda doubt).

@nanoscopic

Copy link
Copy Markdown

It definitely doesn't have mode 6 on all devices that is what confused me. Before recently I hadn't messed with Valeria in some years, and was only familiar with mode 5. And... it didn't work. And I was confused as to why and then I figured it out. It's fairly obvious when it doesn't work. Between myself and partners on my system we have 50+ phones and nearly as many iOS versions intentionally so we can test this sort of stuff...

I'll let you know when I put up my implementation. My license is different but I have no issue with you looking through it yourself or with AI to learn more options and ways to make it better. I only ask that you not have AI adapt my implementation into something else under a different license.

As an aside it's so irritating that Apple locked their system driver to iPhones so you can't readily attach to the interface for video and are forced to use their poor implementation of it on MacOS. I was attempting to work around that by modifying a cheap Linux device to serve as a MITM to alter the VID and PID, but unfortunately most gadget mode support in chipsets for cheap devices can't handle the number of endpoints on modern iPhones. With too many USB endpoints I couldn't get it to work cleanly.

With more expensive hardware I can make it work, but that defeats the point as I wanted a cheap way to do it.

Also spent a while altering firmware on USB hubs to see if I could manage to alter VID and PID with a standard USB hub with modified firmware. I was able to fully RE many common USB 3 hub firmwares and alter them but still couldn't manage to get them to work as a way to prevent MacOS latching on.

All this is somewhat irrelevant and makes little difference. My only point is I was looking for ways to get a clean Valeria implementation working on MacOS and what I've tried so far didn't work.

@doronz88

doronz88 commented May 3, 2026

Copy link
Copy Markdown
Owner

I will review this once all the licensing stuff are settled.

Also, I did not encounter anything about performing it on macOS on anything public at least. Care to share a link where is it?

The macOS implementation is just using the builtin module from the DSC (wrapped using ctypes) to retrieve the frames. There is no protocol implementation actually taken from a different repository, so as far as I understand, there shouldn't be a licensing issue at least for macOS.

@renegadelink renegadelink force-pushed the feature/valeria branch 2 times, most recently from 52009df to 3f64da5 Compare May 7, 2026 19:45
@renegadelink

Copy link
Copy Markdown
Author

I will review this once all the licensing stuff are settled.

Also, I did not encounter anything about performing it on macOS on anything public at least. Care to share a link where is it?

The macOS implementation is just using the builtin module from the DSC (wrapped using ctypes) to retrieve the frames. There is no protocol implementation actually taken from a different repository, so as far as I understand, there shouldn't be a licensing issue at least for macOS.

Okay I dropped libusb Valeria from this PR — macOS CoreMediaIO only for now. I'll follow up either as a separate PR or its own package pmd3 imports, depending on Daniel's response.

Adds the public Python API and `pymobiledevice3 valeria` CLI for iOS
H.264 screen capture over USB. The macOS backend (`valeria_cmio`)
talks to CoreMediaIO directly via pure ctypes — same kernel pathway
QuickTime Player uses for iOS screen recording.

API:
  ValeriaScreenCapture.create(udid)  -> backend instance
  cap.start(); cap.run(consume); cap.stop()
  for frame in cap.frames(): sink.write(frame.to_annex_b())

CLI:
  pymobiledevice3 valeria -o stream.h264 --duration 10
  pymobiledevice3 valeria -o - | ffplay -f h264 -

Backend dispatch is a factory (auto/cmio) so additional backends can
slot in by extending the literal and adding a factory branch.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@doronz88

doronz88 commented May 8, 2026

Copy link
Copy Markdown
Owner

@renegadelink do you consider this pr as ready for cr and possibly merging?

@renegadelink

Copy link
Copy Markdown
Author

@renegadelink do you consider this pr as ready for cr and possibly merging?

yup. I did what I could with my one test device and I have no issues.

@doronz88

Copy link
Copy Markdown
Owner

After testing, this PR needs much more work as the API supplied is too much unreliable

@nanoscopic

nanoscopic commented May 17, 2026

Copy link
Copy Markdown

@doronz88 I literally said that is why Valeria ( or at least the code by Daniel ) was unusable by me. I had to do a significant amount of work to add stuff to reset the interfaces ( on my Github... ) and quickly reset the services when they inevitably crash.

As this is based off AI rewrite of Daniel's code, it will be just as flaky. ( implementation wise )
As Valeria is Valeria, it is also terribly flaky from Apple side generally and that cannot be fixed.

The majority of the flakiness is from bad Apple implementation especially around being able to consistently start the stream and run it for more than a few minutes.

All this is why I removed Valeria use from my product, ControlFloor, because it so bad. Ultimately Upload Broadcast Extension is the only reliable official method.

I wouldn't call Valeria officially supported. More like half-assed by Apple and then abandoned.

Because the problems don't primarily stem from this code but rather Apple, I don't see why holding up contribution of it helps anything. First the code needs to go in, then it has to have all the crap I did to deal with the flakiness re-added.

If you try to demand it be reasonably stable from the start, it will never be added by anyone.

@renegadelink

renegadelink commented May 17, 2026

Copy link
Copy Markdown
Author

After testing, this PR needs much more work as the API supplied is too much unreliable

Understood. Well if anyone wants to help test by running -vv and giving me their logs I can try and help fix any issues. I don't have any issues on my test device so unfortunately there's not much more I can do. Should I close the PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants