[Guide] How to Enable Miracast for Pixel

Search This thread

sbjbs

Member
Aug 4, 2011
17
26
Introduction
Few weeks ago, I got a Pixel 1 as my backup phone, when I try to connect it to the Microsoft Wireless Display Adapter, it couldn't find the Adapter.
I haven't used Android phones for a long time. In my mind, Android natively supports Miracast. After some searching, I was frightened to find that Google has removed Miracast from Android.
I was really disappointed because I often cast my Windows devices easily by using Miracast. My Projector has only two HDMI ports, one for HDMI cable, and one for Wireless Display Adapter. I can't add a Chromecast unless a HDMI switch is used.
One possible solution is to add a line "persist.debug.wfd.enable=1" in "build.prop" to enable WiFi-Display, but many people say that it doesn't work since Android N, the phone can discover WiFi-Displays but unable to cast.

Since Android framework is open source, I did some searching and found that some WiFi-Display related code still exists. Android 9 still keeps the WifiDisplayAdapter in DisplayManagerService, but removed WifiDisplay from MediaPlayerService, that's why we can discover WiFi-Displays but can't cast.
I recovered some code from AOSP history commits, fixed the build errors, and debugged on my Pixel, finally the Miracast can work on my Pixel.

If you also want Miracast to work on your Google phones, you can follow my steps, but first make sure your ROM is open source so that you can change and rebuild it.
  • If you have a Pixel 1 running the same version of system, you can simply replace some files in your system with the files I built.
  • If you have other Google phones or running different versions, you can apply my patch to AOSP, and build it by yourself. I have built the latest version for all Google Pixel phones, but I haven't tested except Pixel 1.
  • If your ROM is not open source, I will no longer be able to help, since MediaPlayerService may have been changed by your ROM, you may can't replace it with the AOSP version.

Prerequisites
  • Pixel running Android 9.0.0 build number PQ1A.181205.002.A1 or PQ1A.190105.004.
  • Bootloader unlocked.
  • adb and fastboot tools and drivers are working for your device.
Note & Disclaimer
  • These steps will change files in your system, your Pixel should run the exact same version of system. Pushing files to different versions of system may cause unknown problems.
  • Please check carefully before running each command, a mistaken operation may result in unable to boot up, or destroy your data, or even damage your phone.
  • If you are not sure about what these steps are doing, but still want to go ahead, please backup your phone firstly, you may not be able to recover your phone to its original state.
  • I have only tested on Pixel 1 with build number PQ1A.181205.002.A1 and PQ1A.190105.004, for other phones or versions, you should build and test on your own.
Basic Steps
  1. Flash the same version of factory image for your phone.
  2. Download the prebuilt files from https://github.com/sbjbs/android-wfd-patch
  3. Add this line into "build.prop", without it, we will not be able to enable WiFi-Display:
    Code:
    persist.debug.wfd.enable=1
  4. Replace some files in system:
    Code:
    system/lib/libaudioflinger.so
    system/lib64/libaudioflinger.so
    system/lib/libserviceutility.so
    system/lib64/libserviceutility.so
    system/lib/libmedia.so
    system/lib64/libmedia.so
    system/lib/libwilhelm.so
    system/lib64/libwilhelm.so
    system/lib/libandroid_runtime.so
    system/lib64/libandroid_runtime.so
    system/lib/libstagefright.so
    system/lib64/libstagefright.so
    system/lib/libstagefright_wfd.so
    system/lib64/libstagefright_wfd.so
    system/lib/libmediaplayerservice.so
  5. If you have magisk installed, you can refer to Phoenix Wright's post:
    using "setprop" to set that property once is enough, no need to touch build.prop; given it's a "persist" property it'll be permanent.
    using magisk to overlay them on /system

Some extra steps that you may need
  • Remove dm-verity metadata of system.img
    The dm-verity can prevent us from changing the system, and what's more it has a forward-error-correction(FEC) feature, which can detect and correct some corrupted data.
    I spend a lot of time on attempting to change the build.prop but fail again and again, it just always correct the file, and I can't enable wifi-display.
    I don't know how to disable it. Since the error-correcting info should be stored in the dm-verity metadata, I just removed the dm-verity metadata from the stock system.img, and flash the system.img.
    I will list the steps to remove dm-verity metadata, but if you can disable dm-verity entirely, that should work too.
    For phones that have a vbmeta partition (like Pixel 2), seems you can flash an special vbmeta.img to disable AVB. You can build the empty vbmeta image by this command, but I don't have a device to test if it works:
    Code:
    avbtool make_vbmeta_image --flag 2 --padding_size 4096 --output vbmeta.img

    For Pixel 1, we need to remove dm-verity metadata that attached to system.img, first convert the sparse system.img to an ext4 image:
    Code:
    simg2img system.img system.ext4.img
    read the block count to calculate the system size:
    Code:
    od -j 1028 -An -N4 -tu system.ext4.img
    for Pixel 1, it should be 516099. You also can read it by a Hex Editor, e.g., WinHEX in Windows, read 4 bytes starting from 1028 bytes, as a little-endian hex value.
    so the real system partition size is 516099 * 4096 = 2113941504 bytes.
    we need to set the data to zero after that position. It's important to do this, since we need to overwrite the metadata in that area, rather than just truncate the size of system.img.

    next get the ext4 image size, for Pixel PQ1A.181205.002.A1, it should be 2147483648.
    so we need to write 2147483648 - 2113941504 = 33542144 bytes:
    Code:
    dd if=/dev/zero of=system.ext4.img seek=2113941504 count=33542144 oflag=seek_bytes iflag=count_bytes
    we also can use some other tools to do this, e.g., WinHEX in Windows. Open the file in in-place mode, and jump to 2113941504, and select it as the begin, and jump to the end, and select as the end, and fill the selection with all zero.

    after that, flash the changed system.ext4.img to Pixel:
    Code:
    fastboot flash system system.ext4.img
    Test that you can boot up normally. If not, check what's wrong and fix it.
  • Temporary boot up by TWRP image
    We don't have to install TWRP, but just use it to temporary boot up, so that we can mount the system partition for writing. If you have TWRP installed, you can just boot to TWRP.
    Download TWRP image for pixel, just need the twrp.img, we won't install the zip file.
    boot your device to bootloader:
    Code:
    adb reboot bootloader
    and boot by the twrp.img
    Code:
    fastboot boot twrp.img
    after TWRP booted up, just leave it there, no need to unlock or input password, we will change the system by adb.
  • Mount system partition for writing.
    In the current version of TWRP for Pixel (twrp-3.2.3-1-sailfish.img), its fstab has an entry for system partition, we can directly mount it.
    Code:
    adb shell mount /system
    run mount to check if successfully mounted:
    Code:
    adb shell mount
    you should see an entry like this:
    Code:
    /dev/block/sda34 on /system type ext4 (rw,seclabel,relatime)
    If not, you should stop now, and check the above steps.

    after mount, we can use adb to push files into system.
 
Last edited:

jean_michmich

Member
Jan 8, 2019
25
11
Hey guy,

You helped me a lot to understand why it was not working with Pie. I tried several custom ROMs + the official Pie and it was not working.
There is Wireless display option in quick settings but it' doesn't find my Microsoft Wireless Display Adaptor also.

I so downgrade to Oreo. With Oreo it works without any issue. (there is an option "allow Wireless display" to tick and then your miracast dongle will appear).

You did a great job to make it working on Pie. Congratulations. I will anyway stay on Oreo. I think google will change that (many people already complained i guess).

Jean Michel
 

sbjbs

Member
Aug 4, 2011
17
26
Hey guy,

You helped me a lot to understand why it was not working with Pie. I tried several custom ROMs + the official Pie and it was not working.
There is Wireless display option in quick settings but it' doesn't find my Microsoft Wireless Display Adaptor also.

I so downgrade to Oreo. With Oreo it works without any issue. (there is an option "allow Wireless display" to tick and then your miracast dongle will appear).

You did a great job to make it working on Pie. Congratulations. I will anyway stay on Oreo. I think google will change that (many people already complained i guess).

Jean Michel

Thanks for your reply!

I have tested on Pie on my Pixel, I often play online games while cast is on, and didn't meet any problems last week.

I also hope that Google will support Miracast again in the future, but it seems unlikely in the near future.
Google has abandoned it since Android 6 (source: Wikipedia), and now they're deleting unused source code related to wifi-display.
I'm more worried about that there will be more build errors and potential issues when we try to enable it for future Android versions.
 

jean_michmich

Member
Jan 8, 2019
25
11
Thanks for your reply!

I have tested on Pie on my Pixel, I often play online games while cast is on, and didn't meet any problems last week.

I also hope that Google will support Miracast again in the future, but it seems unlikely in the near future.
Google has abandoned it since Android 6 (source: Wikipedia), and now they're deleting unused source code related to wifi-display.
I'm more worried about that there will be more build errors and potential issues when we try to enable it for future Android versions.

OK it's getting more clear. You are right. Wikipedia says : "but support was dropped with Android 6.0 Marshmallow in 2015 in favor of Google's own proprietary Google Cast protocol.[15] Some manufacturers of Android devices step in and support Miracast through their software"

I'm not an "official android ROM" user.

I always use Pixel Dust custom ROM. For this custom ROM, Miracast was supported for Oreo but not for Pie (not yet).
 

sbjbs

Member
Aug 4, 2011
17
26
OK it's getting more clear. You are right. Wikipedia says : "but support was dropped with Android 6.0 Marshmallow in 2015 in favor of Google's own proprietary Google Cast protocol.[15] Some manufacturers of Android devices step in and support Miracast through their software"

I'm not an "official android ROM" user.

I always use Pixel Dust custom ROM. For this custom ROM, Miracast was supported for Oreo but not for Pie (not yet).

I used to be a ‘latest official ROM’ user;)

For custom ROMs, since they may have changed the framework, we can't replace files with AOSP version, but as long as they're open source, we can try to contribute to it.
Adding Miracast support in Android is not difficult, and many manufactures have their own support.
I hope your custom ROM will support Miracast for Pie in the near future.
 
  • Like
Reactions: jean_michmich

Phoenix Wright

Senior Member
Feb 19, 2009
270
326
Tested on Pixel 2 XL and working (for what it's worth, using "setprop" to set that property once is enough, no need to touch build.prop; given it's a "persist" property it'll be permanent). I built the .so files on my local aosp tree and I'm using magisk to overlay them on /system.
 
  • Like
Reactions: saham424 and sbjbs

spezi77

Recognized Developer / Contributor
Jan 27, 2013
3,519
7,852
/home/less
Hi @sbjbs

If I understand you right then you have patched the AOSP codebase. Would you mind sharing the repositories/commits on your GitHub? That would be highly appreciated. :good:

Regards
spezi

Sent from my Pixel 3 using XDA Labs
 

sbjbs

Member
Aug 4, 2011
17
26
Tested on Pixel 2 XL and working (for what it's worth, using "setprop" to set that property once is enough, no need to touch build.prop; given it's a "persist" property it'll be permanent). I built the .so files on my local aosp tree and I'm using magisk to overlay them on /system.

Great to hear that!
You method is simpler and more elegant, that's awesome!
 

nitin.chobhe

Recognized Contributor
Jan 3, 2013
8,298
19,955
India
Google Pixel 2 XL
Google Pixel 7 Pro
Tested on Pixel 2 XL and working (for what it's worth, using "setprop" to set that property once is enough, no need to touch build.prop; given it's a "persist" property it'll be permanent). I built the .so files on my local aosp tree and I'm using magisk to overlay them on /system.

Can you elaborate how you did use "magisk to overlay them on /system"?

I pushed them using adb as mentioned on github but it is stuck at Google splash screen. I've taimen BTW and am on Pixeldust ROM. It booted while writing this post but the screen was not responding to touch.

Nitin
 

Phoenix Wright

Senior Member
Feb 19, 2009
270
326
Can you elaborate how you did use "magisk to overlay them on /system"?

I pushed them using adb as mentioned on github but it is stuck at Google splash screen. I've taimen BTW and am on Pixeldust ROM. It booted while writing this post but the screen was not responding to touch.

Nitin

I made a module with the .so files I compiled: https://workupload.com/file/pd4EUBdw
Note that I can't vouch for this to work on your ROM, it works on taimen on stock 9.0 with January security patches though.
 

sbjbs

Member
Aug 4, 2011
17
26
Can you elaborate how you did use "magisk to overlay them on /system"?

I pushed them using adb as mentioned on github but it is stuck at Google splash screen. I've taimen BTW and am on Pixeldust ROM. It booted while writing this post but the screen was not responding to touch.

Nitin

Did you build the so files for your ROM from source? the patch has changed the interface of IMediaPlayerService, if anything else in your ROM is using IMediaPlayerService.h, you'd better rebuild and replace them.
that's why I uploaded the libwilhelm.so and libandroid_runtime.so, even though I didn't change the source code of them.
 

nitin.chobhe

Recognized Contributor
Jan 3, 2013
8,298
19,955
India
Google Pixel 2 XL
Google Pixel 7 Pro
I made a module with the .so files I compiled: https://workupload.com/file/pd4EUBdw
Note that I can't vouch for this to work on your ROM, it works on taimen on stock 9.0 with January security patches though.
Thanks!
Did you build the so files for your ROM from source? the patch has changed the interface of IMediaPlayerService, if anything else in your ROM is using IMediaPlayerService.h, you'd better rebuild and replace them.
that's why I uploaded the libwilhelm.so and libandroid_runtime.so, even though I didn't change the source code of them.
I tried building with my ROM but I got build errors related to OMX.h, I need to find a solution for them. Thanks anyways!

Nitin
 

Mati24

Senior Member
Jun 5, 2013
73
1
Rennes
It works perfectly ! I used the prebuilt files on the march build.
Do you know if we can force the 16/9 ratio ?
Thanks

edit : I will try with second screens !
 
Last edited:

marshyrob

Member
Nov 13, 2010
19
3
this works a treat on my Pixel 3 XL (Stock Pie) just overlayed the files from Phoenix Wright using magisk and it works now....sweet thanks, now just need to figure out how to fool sky sports app into letting me cast.....
 

marshyrob

Member
Nov 13, 2010
19
3
Can you explain more how did you do?
Did you just flash the zip from Phoenix Wright in Magisk?

Yes just copied the .zip file from Phoenix Wright's post to my device then used the module install option in Magisk like if you are installing a module found in Magisk (use the plus sign to add your own .zip file instead and browse to the file you just saved) then reboot. Then i went to the cast option in settings - connected devices - connection preferences, there i could tick in the top right corner under the 3 dots "enable wireless display" and it showed my Panasonic TV, chose connected and boom it connected. Needed to enable the mirroring option on my Panasonic but this is a TV setting nothing to do with the phone, you might need to do a similar step based on your TV as they need to be able to see each other.
 
  • Like
Reactions: zard and EMJI79

EMJI79

Senior Member
Yes just copied the .zip file from Phoenix Wright's post to my device then used the module install option in Magisk like if you are installing a module found in Magisk (use the plus sign to add your own .zip file instead and browse to the file you just saved) then reboot. Then i went to the cast option in settings - connected devices - connection preferences, there i could tick in the top right corner under the 3 dots "enable wireless display" and it showed my Panasonic TV, chose connected and boom it connected. Needed to enable the mirroring option on my Panasonic but this is a TV setting nothing to do with the phone, you might need to do a similar step based on your TV as they need to be able to see each other.


It did work today with my Sony television.
Thanks!
 
Jun 2, 2017
20
3
OMG!!! You made my week. Few hiccups tho....
Can anyone else confirm that Hulu and HBO go and Amazon prime . YouTube works.
Hulu and amazon I can log on but when it plays all you get is audio. Haven't tried Netflix I don't sub them.
HBO go as soon as I try to play a video it says this video cannot be played. If I disable the mirror it plays perfectly. Same thing with Hulu and prime. Disable the mirror an it works perfectly on my phone.
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 16
    Introduction
    Few weeks ago, I got a Pixel 1 as my backup phone, when I try to connect it to the Microsoft Wireless Display Adapter, it couldn't find the Adapter.
    I haven't used Android phones for a long time. In my mind, Android natively supports Miracast. After some searching, I was frightened to find that Google has removed Miracast from Android.
    I was really disappointed because I often cast my Windows devices easily by using Miracast. My Projector has only two HDMI ports, one for HDMI cable, and one for Wireless Display Adapter. I can't add a Chromecast unless a HDMI switch is used.
    One possible solution is to add a line "persist.debug.wfd.enable=1" in "build.prop" to enable WiFi-Display, but many people say that it doesn't work since Android N, the phone can discover WiFi-Displays but unable to cast.

    Since Android framework is open source, I did some searching and found that some WiFi-Display related code still exists. Android 9 still keeps the WifiDisplayAdapter in DisplayManagerService, but removed WifiDisplay from MediaPlayerService, that's why we can discover WiFi-Displays but can't cast.
    I recovered some code from AOSP history commits, fixed the build errors, and debugged on my Pixel, finally the Miracast can work on my Pixel.

    If you also want Miracast to work on your Google phones, you can follow my steps, but first make sure your ROM is open source so that you can change and rebuild it.
    • If you have a Pixel 1 running the same version of system, you can simply replace some files in your system with the files I built.
    • If you have other Google phones or running different versions, you can apply my patch to AOSP, and build it by yourself. I have built the latest version for all Google Pixel phones, but I haven't tested except Pixel 1.
    • If your ROM is not open source, I will no longer be able to help, since MediaPlayerService may have been changed by your ROM, you may can't replace it with the AOSP version.

    Prerequisites
    • Pixel running Android 9.0.0 build number PQ1A.181205.002.A1 or PQ1A.190105.004.
    • Bootloader unlocked.
    • adb and fastboot tools and drivers are working for your device.
    Note & Disclaimer
    • These steps will change files in your system, your Pixel should run the exact same version of system. Pushing files to different versions of system may cause unknown problems.
    • Please check carefully before running each command, a mistaken operation may result in unable to boot up, or destroy your data, or even damage your phone.
    • If you are not sure about what these steps are doing, but still want to go ahead, please backup your phone firstly, you may not be able to recover your phone to its original state.
    • I have only tested on Pixel 1 with build number PQ1A.181205.002.A1 and PQ1A.190105.004, for other phones or versions, you should build and test on your own.
    Basic Steps
    1. Flash the same version of factory image for your phone.
    2. Download the prebuilt files from https://github.com/sbjbs/android-wfd-patch
    3. Add this line into "build.prop", without it, we will not be able to enable WiFi-Display:
      Code:
      persist.debug.wfd.enable=1
    4. Replace some files in system:
      Code:
      system/lib/libaudioflinger.so
      system/lib64/libaudioflinger.so
      system/lib/libserviceutility.so
      system/lib64/libserviceutility.so
      system/lib/libmedia.so
      system/lib64/libmedia.so
      system/lib/libwilhelm.so
      system/lib64/libwilhelm.so
      system/lib/libandroid_runtime.so
      system/lib64/libandroid_runtime.so
      system/lib/libstagefright.so
      system/lib64/libstagefright.so
      system/lib/libstagefright_wfd.so
      system/lib64/libstagefright_wfd.so
      system/lib/libmediaplayerservice.so
    5. If you have magisk installed, you can refer to Phoenix Wright's post:
      using "setprop" to set that property once is enough, no need to touch build.prop; given it's a "persist" property it'll be permanent.
      using magisk to overlay them on /system

    Some extra steps that you may need
    • Remove dm-verity metadata of system.img
      The dm-verity can prevent us from changing the system, and what's more it has a forward-error-correction(FEC) feature, which can detect and correct some corrupted data.
      I spend a lot of time on attempting to change the build.prop but fail again and again, it just always correct the file, and I can't enable wifi-display.
      I don't know how to disable it. Since the error-correcting info should be stored in the dm-verity metadata, I just removed the dm-verity metadata from the stock system.img, and flash the system.img.
      I will list the steps to remove dm-verity metadata, but if you can disable dm-verity entirely, that should work too.
      For phones that have a vbmeta partition (like Pixel 2), seems you can flash an special vbmeta.img to disable AVB. You can build the empty vbmeta image by this command, but I don't have a device to test if it works:
      Code:
      avbtool make_vbmeta_image --flag 2 --padding_size 4096 --output vbmeta.img

      For Pixel 1, we need to remove dm-verity metadata that attached to system.img, first convert the sparse system.img to an ext4 image:
      Code:
      simg2img system.img system.ext4.img
      read the block count to calculate the system size:
      Code:
      od -j 1028 -An -N4 -tu system.ext4.img
      for Pixel 1, it should be 516099. You also can read it by a Hex Editor, e.g., WinHEX in Windows, read 4 bytes starting from 1028 bytes, as a little-endian hex value.
      so the real system partition size is 516099 * 4096 = 2113941504 bytes.
      we need to set the data to zero after that position. It's important to do this, since we need to overwrite the metadata in that area, rather than just truncate the size of system.img.

      next get the ext4 image size, for Pixel PQ1A.181205.002.A1, it should be 2147483648.
      so we need to write 2147483648 - 2113941504 = 33542144 bytes:
      Code:
      dd if=/dev/zero of=system.ext4.img seek=2113941504 count=33542144 oflag=seek_bytes iflag=count_bytes
      we also can use some other tools to do this, e.g., WinHEX in Windows. Open the file in in-place mode, and jump to 2113941504, and select it as the begin, and jump to the end, and select as the end, and fill the selection with all zero.

      after that, flash the changed system.ext4.img to Pixel:
      Code:
      fastboot flash system system.ext4.img
      Test that you can boot up normally. If not, check what's wrong and fix it.
    • Temporary boot up by TWRP image
      We don't have to install TWRP, but just use it to temporary boot up, so that we can mount the system partition for writing. If you have TWRP installed, you can just boot to TWRP.
      Download TWRP image for pixel, just need the twrp.img, we won't install the zip file.
      boot your device to bootloader:
      Code:
      adb reboot bootloader
      and boot by the twrp.img
      Code:
      fastboot boot twrp.img
      after TWRP booted up, just leave it there, no need to unlock or input password, we will change the system by adb.
    • Mount system partition for writing.
      In the current version of TWRP for Pixel (twrp-3.2.3-1-sailfish.img), its fstab has an entry for system partition, we can directly mount it.
      Code:
      adb shell mount /system
      run mount to check if successfully mounted:
      Code:
      adb shell mount
      you should see an entry like this:
      Code:
      /dev/block/sda34 on /system type ext4 (rw,seclabel,relatime)
      If not, you should stop now, and check the above steps.

      after mount, we can use adb to push files into system.
    6
    Can you elaborate how you did use "magisk to overlay them on /system"?

    I pushed them using adb as mentioned on github but it is stuck at Google splash screen. I've taimen BTW and am on Pixeldust ROM. It booted while writing this post but the screen was not responding to touch.

    Nitin

    I made a module with the .so files I compiled: https://workupload.com/file/pd4EUBdw
    Note that I can't vouch for this to work on your ROM, it works on taimen on stock 9.0 with January security patches though.
    3
    Do you want to brick people's phones with this sh*t

    People do not read sh*t, brick their device and then someone who put an effort and his/her own time to make something for free for everyone is at fault?

    You brick it, you are noob! Should have read the instructions, works like charm for me!
    2
    Hey guy,

    You helped me a lot to understand why it was not working with Pie. I tried several custom ROMs + the official Pie and it was not working.
    There is Wireless display option in quick settings but it' doesn't find my Microsoft Wireless Display Adaptor also.

    I so downgrade to Oreo. With Oreo it works without any issue. (there is an option "allow Wireless display" to tick and then your miracast dongle will appear).

    You did a great job to make it working on Pie. Congratulations. I will anyway stay on Oreo. I think google will change that (many people already complained i guess).

    Jean Michel
    2
    Tested on Pixel 2 XL and working (for what it's worth, using "setprop" to set that property once is enough, no need to touch build.prop; given it's a "persist" property it'll be permanent). I built the .so files on my local aosp tree and I'm using magisk to overlay them on /system.