Category Archives: G_Tips

Android M – Systemless Unroot, OTA and re-Root

Since Google is now pushing monthly security updates, it becomes a regular procedure for me to temporarily un-root my phone to get OTA updates.
This post introduces the steps of how to unroot Android M (6.0 or above), get OTA update and then re-root it.

[Update] Let’s take Nexus6p as example to OTA from angler-MHC19I to angler-MHC19Q.

Pre-condition

Preparation

  • Factory image of MHC19I
  • TWRP (e.g. 3.0.0.1)
  • SuperSU (2.60 or above, e.g. 2.68)

Steps

Unroot

  1. Extract MHC19I’s factory image to get boot.img and recovery.img
    tar -xf angler-mhc19i-factory-8c31db3f.tgz angler-mhc19i/image-angler-mhc19i.zip
    cd angler-mhc19i
    unzip image-angler-mhc19i.zip boot.img recovery.img
    
  2. Flash stock boot and recovery
    adb reboot bootloader
    fastboot flash boot boot.img
    fastboot flash recovery recovery.img
    

Now the phone is un-rooted and we get the stock Android.

OTA or Sideload

If you got OTA update from Google, just run OTA, Android does the OTA automatically.

If you did not get OTA update, you have the option to use adb sideload to manually flash the OTA package.

  1. Get the OTA package;
  2. Go to stock recovery, in recovery screen, press and hold the Power key and the Volume up key for a few seconds, and then let go of the Volume up key, but keep pressing Power.
  3. In stock recovery, run below command to do the OTA upgrade:
    adb sideload f7303e0c33450419deeb292b0887c1595fb5588d.signed-angler-MHC19Q-from-MHC19I.zip
    

After OTA, we’re now on MHC19Q (April 2016 release).

Root again

  1. Flash TWRP
    adb reboot bootloader
    fastboot flash recovery twrp-3.0.0-1-angler.img
    
  2. Flash SuperSU
    • Press the Volumn and Power key to enter Recovery
    • In TWRP, flash BETA-SuperSU-v2.68-20160228150503.zip
  3. Wipe cache/dalvik
  4. Reboot to system.

Now the phone is rooted again systemlessly.
Enjoy the new version 😉

Share

Handle Discontinuity and PAT/PMT change in Android Multicast Playback

In previous post it briefly introduces how to implement multicast playback in Android.
In real world, multicast may get discontinuities or PAT/PMT changes, which will cause the playback intermittent or failure if it’s not correctly handled.

In this post, it will introduces how to handle discontinuity and PAT/PMT changes.

Background

In NuPlayer::Decoder::fetchInputData(), it handles three types of DISCONTINUITY:

  • DISCONTINUITY_TIME
  • DISCONTINUITY_AUDIO_FORMAT
  • DISCONTINUITY_VIDEO_FORMAT

Originally, they are used to handle adaptive cases in HLS, e.g. switch from low-resolution stream to high-resolution stream. They’re perfect for multicast stream’s discontinuity and PAT/PMT change as well.

Handle Discontinuity

In ATSParser::Stream::parse(), it already checks the ContinuityCounter in TS streams. We can just queue a time discontinuity unit, then NuPlayer::Decoder will handle the case, reset the playback and re-sync A/V.

if (mExpectedContinuityCounter >= 0
    && (unsigned)mExpectedContinuityCounter != continuity_counter) {
  ALOGI("discontinuity on stream pid 0x%04x", mElementaryPID);
  ...
  signalDiscontinuity(DISCONTINUITY_TIME, NULL);
  ...
}

In ATSParser::Program::convertPTSToTimestamp(), there is a special flag for calculating time stamp from PTS.

if (!(mParser->mFlags & TS_TIMESTAMPS_ARE_ABSOLUTE)) {
  if (!mFirstPTSValid) {
    mFirstPTSValid = true;
    mFirstPTS = PTS;
    PTS = 0;
  } else if (PTS < mFirstPTS) {
    PTS = 0;
  } else {
    PTS -= mFirstPTS;
  }
}
...

If TS_TIMESTAMPS_ARE_ABSOLUTE is not set, it uses the first valid PTS as the base time stamp, and assumes the PTS is increasing all the time. This is not the case for multicast live streaming. So we need to set this flag to let it calculate time stamp by absolute value of PTS.

  // In MulticastSession::onConnect()
  mTSParser = new ATSParser(ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE);

With the changes, multicast discontinuity is correctly handled and it works fine when multicast stream is looped.

Handle PAT/PMT change

In ATSParser implementation, it just parses PAT/PMT and store the program table and the A/V streams. If PAT/PMT is changed, it just update the program table and streams, and requires the “user” of ATSParser to handle the change, which is quite complicated.
In the other hand, when PAT/PMT is changed in multicast, we know the stream is changed and thus can just throw away the previous program table. So why not delete the previous ATSParser and create a new one to handle the new stream? It makes things much easier.
The changes:

  • ATSParser
    1. Add an observer that should receive the event of PAT/PMT change
    2. Save PAT/PMT crc32 and detect the change; If it’s changed, call onPatPmtChange() and notify the observer
  • MulticastSession
    1. Receive parser’s event and call onPatPmtChanged() for PAT/PMT changed event
    2. Reset ATSParser by delete and create new one
    3. Queue FORMAT change DISCONTINUITY for both audio and video
    4. In dequeueAccessUnit(), check the discontinuity queue and return DISCONTINUITY for audio/video stream.

With the changes, ATSParser is reset when PAT/PMT changes, as if it plays a new stream, which is exactly the expected behavior.

Share

Multicast playback in Android

This article is to briefly introduce how to add multicast playback support in Android platform based on AOSP 5.0.0_r2 tag.

Background

  • NuPlayer is the default media player in Android L. It’s part of libmediaplayerservice located in frameworks/av/media/libmediaplayerservice
  • libstagefright is a library used by NuPlayer to provide a lot of media related functions, e.g. media download, parse and extract. Located in frameworks/av/media/libstagefright
  • chromium_org is a library used by Browser to provide all webview related functions. <video> tag in Webview also uses this library to render its content. Located in external/chromium_org
  • Gallery2 is a default system app that handles media playback intents. Located in packages/apps/Gallery

The above components will be changed to add multicast support.

Implementations

Precondition

Multicast as a kernel option, shall be enabled in kernel build.
Note: this option is enabled by default on Nexus Tablets, but disabled on Nexus Phones.
So you probably need to compile your custom kernel if you plan to test multicast on your phone.

  CONFIG_IP_MULTICAST=y
NuPlayer
  • MediaPlayerFactory::IFactory::scoreFactory() checks the URI scheme to decide which player shall be used to play a certain media.
    In NuPlayerFactory::scoreFactory(), multicast’s score shall be added to be chosen for playing multicast stream.
    if (!strncasecmp("http://", url, 7)
        || !strncasecmp("https://", url, 8)
        || !strncasecmp("file://", url, 7)) {
      … // return kOurScore
    }
    if (!strncasecmp("rtsp://", url, 7)) {
      return kOurScore;
    }
    if (!strncasecmp("udp://", url, 6)
        || !strncasecmp("igmp://", url, 7)) {
      return kOurScore;
    }
  • NuPlayer checks the URI scheme and creates different DataSource to receive media data. MulticastSource is added to handle multicast stream.
    In NuPlayer::setDataSourceAsync(), multicast shall be supported as below
	if (IsHTTPLiveURL(url)) {
	  … // new HTTPLiveSource
	} else if (!strncasecmp(url, "rtsp://", 7)) {
	  … // new RTSPSource
	}
	…
	else if (!strncasecmp(url, "udp://", 6)
	         || !strncasecmp("igmp://", url, 7)) {
	  source = new MulticastSource(notify, httpService, url, headers);
	}
	else {
	  … // Generic source
	}
  • MulticastSource inherits NuPlayer::Source and provide related functions. Basically it just handles events and pass all functions to MulticastSession, which is implemented in libstagefright.
libstagefright
  • Multicast socket related code shall be implemented to provide the socket interface.
  • MulticastSession uses the socket to receive media data, uses existing ATSParser to parse the TS stream, and extract so-called accessUnit to feed the decoder. It could be put at frameworks/av/media/libstagefright/multicast
    This class handles the main part of multicast playback.
  • MulticastDataSource is created by DataSource and used by MediaExtractor to extract the meta data of the media, e.g. whether it has video or audio, what the video resolution is, etc.
    This class is used in <video> tag playback, that webview will retrieve the media element’s metadata.
    Note: DataSource expects the media as static content, so it uses readAt(offset) to read the data from the source. But multicast is a stream, so MulticastDataSource shall be implemented carefully to provide the correct data with offset to DataSource.
  • DataSource shall be updated to create MulticastDataSource for multicast scheme.
	if (!strncasecmp("file://", uri, 7)) {
	  … // new FileSource
	} else if (!strncasecmp("http://", uri, 7)
	           || !strncasecmp("https://", uri, 8)
	           || isWidevine) {
	  … // new Http or Cache source
	} else if (!strncasecmp("data:", uri, 5)) {
	  … // new DataURISource
	} else if (!strncasecmp("igmp://", uri, 7)
	           || !strncasecmp("udp://", uri, 6)) {
	  source = new MulticastDataSource(uri);
	} else {
	  … // new FileSource
	}
chromium_org

By default, WebMediaPlayerAndroid does not support multicast scheme, and thus it treats the URI as a file and fails to load the media. WebMediaPlayerAndroid::load() shall be updated to support multicast scheme.

  if (url_.scheme() == "udp" || url_.scheme() == "igmp") {
    DVLOG(1) << "Do not load multicast";
    UpdateReadyState(WebMediaPlayer::ReadyStateHaveNothing);
    DidLoadMediaInfo(MediaInfoLoader::kOk, GURL(url), GURL(""), false);
    return;
  }
Gallery2

By default, Gallery2 does not handle multicast, and thus if an application sends a VIEW intent with multicast scheme, no application handles the intent and exception throws. AndroidManifest.xml shall be updated to support multicast scheme, so that it handles such VIEW intent.

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="udp" />
                <data android:scheme="igmp" />
                <data android:mimeType="video/*" />
                <data android:mimeType="audio/*" />
             </intent-filter>

Conclusion

The above is a simplified introduction of the changes needed to support multicast playback in AOSP.
Google will probably not officially add such support, but some fork may be interested in multicast. This could be a start of the work.

Share

发挥余热的Raspberry Pi 1

迁移到Raspberry Pi2之后,原来的Pi1继续发挥余热——接着个PS2 Eyetoy摄像头,每小时自动拍照、获取空气质量指数、发twitter。当然,Eyetoy拍出来的渣画质不能看,只是做着玩的。

这里记录一下实现的细节。

  • 拍照
    树莓派的官方镜像的kernel已经支持很多摄像头了,USB接口的Eyetoy插上去,直接就有/dev/video0设备。
    各种软件都可以拍照,比如说fswebcam, streamer,等等。我用的是fswebcam

      fswebcam -r 640x480 -S 20 --title "xxx"
    

    其中`-r`是分辨率,不多说了;`-S 20`是跳过20帧,因为如果只取第一帧,画面会不完整,曝光也不对,20帧(大概3秒之后)差不多就够了; `–title “xxx”`是嵌在图像里的标题。

  • 获取AQI
    在aqicn.org上查了一下,离家最近的是张江的监测点,网页是http://aqicn.org/city/shanghai/pudongzhangjiang/
    网页内容里已经带了AQI指数,所以要获取AQI的数值,直接在页面里用正则表达式匹配一下就行了。

        pattern = (' Air Pollution measured on (\w+) (.+): ' +
                   city.capitalize() +
                   ' overall air quality index is (\d+)')
        results = re.findall(pattern, content)
    

    得到的`results[0]`就是匹配到的内容,是个tuple,分别是weekday,time,AQI。

  • 发Twitter
    网上有各种twitter的库,google一下,选了个简单的twython。
    使用 Twitter API 也超简单,到https://apps.twitter.com/申请一个app拿到consumer key/secret;
    再generate一个access token/secret;
    然后用这4个参数初始化twython,发带图片的tweet调用`update_status_with_media`,搞定。
    不过`update_status_with_media`已经过时了,以后应该需要用`upload_media`+`update_status`来搞定,不过现在还能用,先这么用着。

      CONSUMER_KEY = 'YOUR-CONSUMER-KEY'
      CONSUMER_SECRET = 'YOUR-CONSUMER-SECRET'
      ACCESS_KEY = 'YOUR-ACCESS-KEY'
      ACCESS_SECRET = 'YOUR-ACCESS-SECRET'
      ...
      api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
      image = open(imagefile, 'rb')
      api.update_status_with_media(media=image, status=tweet)
    
  • 其它
    要定时跑脚本,crontab里加一个每小时跑一次的脚本就行;
    要翻墙,用的是shadowsocks + cow,然后在脚本前加上http/https proxy的环境变量:

    0 * * * * http_proxy=http://127.0.0.1:7777/ https_proxy=http://127.0.0.1:7777/ /home/pi/pi-pudongair/main.py
    

完整的code放在了gist上:https://gist.github.com/mine260309/771512fc912d283496d9

效果见 @PiPudongAir,欢迎关注。

Share

RaspberryPi2克隆Pi1的系统

RaspberryPi 2发布了有一段时间了,RS版本一直没货,忍不住在淘宝上买了个国产的版本。

到手之后,发现果然可以直接用Pi1的系统:

  • 如果原来就是用TF转SD卡的,直接插到Pi2上就可以启动了
  • 如果原来用的是SD卡,最简单的方式是把SD卡的内容dd出来,然后dd到TF卡上
  • 如果像我一样发现同样都是16G的卡,SD卡的block数多于TF卡,dd的时候size不对,很可能会有问题,只能按下面的方式来clone系统了。

以下步骤假设Pi1的系统在SD卡上,要把这个系统clone到一张略小于SD卡的TF卡上:

  1. 给TF卡分好区,最简单(但是有点花时间)的方式就是直接dd官方的镜像,比如说
      dd bs=4M if=2015-02-16-raspbian-wheezy.img of=/dev/<your-device>
    

    然后启动系统,在raspi-config的菜单里Expand Filesystem,这样TF卡的分区就弄好了。

  2. 备份SD卡的内容,假设我备份到~/raspberry_pi/
      sudo tar czvf ~/raspberry_pi/pi1_boot.tar.gz --directory <path-to-boot> . #备份boot分区
      sudo tar czvf ~/raspberry_pi/pi1_rfs.tar.gz --directory <path-to-rootdisk> . #备份rootfs
    
  3. 删除TF卡各分区的内容
      sudo rm -rf <path-to-boot>/*
      sudo rm -rf <path-to-rootdisk>/*
    
  4. 把tar包解压到TF卡上
      sudo tar xzvf ~/raspberry_pi/pi1_boot.tar.gz -C <path-to-boot>
      sudo tar xzvf ~/raspberry_pi/pi1_rfs.tar.gz -C <path-to-rootdisk>
    
  5. 把TF卡插到Pi2上,启动,应该一切正常!
Share