Android stagefright (awesome player) class diagram

Share with you what have learned about the stage fright these days... It covers three (sub)components: 1. the stagefright itself 2. how OMX was in cooperated into stagefright 3. the software encoder Looking forward your comments..

Android MediaRecorder and Camera class diagram

Record the the video shown in your camera preview. Simple: Open the camera and hit the record. But, how was the code structured?

Android storage

It can be quite confusing regarding where to storage your data and how the data is organized on Android device. Three APIs , that is getFilesDir , getExternalFilesDir and getExternalStoragePublicDirectory can be used to get the directory available for you usage. However, It is not easy to tell the difference only from the API name. So I come up with a table showing what is difference and hopefully, this table can clear things up a little bit. We will examine the difference in four aspects :
  1. privacy : other application can not access it.
  2. availability: should always be available.
  3. clean-up : will be deleted automatically when the apk is uninstalled.
  4. capacity : big size is needed.
1 2 3 4
getFilesDir Y Y Y N
getExternalFilesDir N N Y Y
getExternalStoragePublicDirectory N N N Y
Below are the visualization of the location got from each function and the file permission created there.













Hope it helps.

parse JSON in Ruby



JSON(JavaScript Object Notation) is a lightweight data-interchange format built basing on two fundamental data structures:
  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
See http://www.json.org/ for a thorough explanation.

It is really wonderful and lucky that all the data we want to exchange can be represented by  a combinations of arrays and objects. And note that , in the top level, an JSON file represents an object.

Check here for the test data we will use to parse today. Basically, this json data represents a object composed of a array of section objects , which in turn is composed of some section attributions and array of events.

To parse them in Ruby is simple. Too simple.

require 'rubygems' #otherwise ,require 'json' will fail
require 'json'

topObject = JSON.parse(File.read("event.json"));
sections = topObject ["sections"]
sections.each do |section|
           section_id = section["section_id"]     
           events = section["events"]
           events.each do |event|
                 event_id = event["event_id"]
                 event_name = event["event_name"]
           end
end




USB hotplug support - the home-brewed way

How will you mount/umount the USB mass storage that could be plug and unplug dynamically ? Obviously, Ietc/fstab is not the answer. That file is more for initialization purpose: you know certain disk/partition is available when machine is up or when you issue a "mount -a" command.

So, to support hotplug we need to be able to
1) detect the usb plug/unplug events 
2) know the which device/partition that trigger it
2) mount/umount that device/partition accordingly

Let's see how we can approach each of those need.

1. There are two ways to detect the usb plug/unplug events 

a.  Detect the usb insert/remove action by polling /proc/scsi/ .
  When dir /proc/scsi/usb-storage exist, you know a usb disk was plugged. You can mount now.
  When dir usb-storage disappeared, you know that it was unplugged. You can umount now.

b. utilize Hotplug support in kernel
  When a usb device plug in or plug out , Kernel will call /sbin/hotplug script with $1 = "usb" and the ACTION, be either "add" or "remove". You can do what ever you want in the /sbin/hotplug. Essentially, all you all to do is to perform steps 2, 3. We will see later.

2. know the which device/partition that trigger it
You can get all the partitions available now by checking "/etc/partitions". However, what you need to know is which are the newly added or which are removed. 

3. mount/umount that device/partition accordingly
Mount are easy as long as we know the file system type the partitions are. Otherwise, so we need to try each file system until we mount succeed. Umount is easy. 

Great achievement. We can support USB storage hutplug now. 


Last but not least, the applications that accessed the resources in the USB have to release the resources properly when either was told that the USB would be unplugged or the USB was unplugged abruptly by the user without notification. Failure to release the resource will make the umount fail and stale portions.

In this blog ,we analyzed the tasks should be accomplished to support hotplug and offered a home-brewed way. In next blog, we will try to see how Android system support this feature, much gracefully.

A macro used to measure how much a function cost

#define ME(f) do{\
struct timeval startTime;\
struct timeval endTime;\
double startF,endF, diffF;\
gettimeofday(&startTime, NULL);\
(f);\
gettimeofday(&endTime, NULL);\
startF = (double)startTime.tv_sec * 1000 + (double) startTime.tv_usec/1000;\
endF = (double)endTime.tv_sec * 1000 + (double)endTime.tv_usec/1000;\
diffF = endF - startF;\
printf("measure:%s : %s : line:%d cost time %f ms\n ",__FILE__,__FUNCTION__, __LINE__,diffF);\
}while(0);

usage:
ME(the_function_i_want_to_measure(1,2));

//this also works
int n = 0 ;
ME (n = the_function_i_want_to_measure(1,2));

understand Android.mk

Android invented a new config file format called Android.mk to do the task that was once performed by Makefile. You could get a program compiled using the conventional Makefile way and it is really an excellent way to learn what is really behind the scene of Android.mk. Check out this great article introducing how to compile and run the classic helloWorld program on Android.

Today, however, we will focus on the less pain and more preferred way by using the Android.mk.

Our goal is quite simple: build a share library libhello.so and an executable sayHello that will call the hello() function inside of libhello.so.

Let's create an test dirctory under the ${android_source} (*note) with following structure and files
#for libhello.so 
${android_source}/test/lib/HelloLib.cpp 

${android_source}/test/lib/HelloLib.hpp 


#for sayHello
${android_source}/test/bin/sayHello.cpp

1. Build The share library
include $(CLEAR_VARS)

LOCAL_SRC_FILES:=               \
    HelloLib.cpp     


ifeq ($(TARGET_OS)-$(TARGET_SIMULATOR),linux-true)
LOCAL_LDLIBS += -ldl -lpthread
endif

LOCAL_SHARED_LIBRARIES :=       \
 libcutils                \
 libutils                 \
 libbinder                \
 libandroid_runtime       
 
LOCAL_C_INCLUDES :=                                                 \ 
 $(base)/test/lib            


LOCAL_MODULE:= libhello

LOCAL_PRELINK_MODULE := false   
include $(BUILD_SHARED_LIBRARY)


To build :
source ${android_source}/build/envsetup.sh 
cd ${android_source}/test/lib  
mm -B  
2. Build a executable
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
 sayHello.cpp 

LOCAL_SHARED_LIBRARIES := \
 libutils \
 libbinder \
        libhello

base := $(LOCAL_PATH)/../..

LOCAL_C_INCLUDES := \
    $(base)/test/lib

LOCAL_MODULE:= sayHello

include $(BUILD_EXECUTABLE)

To build:
source ${android_source}/build/envsetup.sh
cd ${android_source}/test/bin
mm -B
*note: I have not tried to build them using NDK and I will update it sometimes later.

Thread , Looper and Handler

A typical scenario is :
When user click a update button, a new thread - worker thread - will be started to fetch the update from the network. When update finish, the worker thread will notify the UI thread to update the UI.

Why should we start a new worker thread?
Because otherwise the time consuming task will block the UI thread and thus impact the UI responsiveness.

Then , Why not update the UI in the worker thread when task finish?
Because the UI component is not thread safe, you have to always use them in the UI thread.

So, how should we approach this common design task in Android? Although AsyncTask is more simply and preferable, the fundamental building block to handle ansync task in Android is Thread, Looper and Handler.

It is not difficult to find how to use Handle/Thread to perform task described above by Googling. In this article, we will look in depth how the Looper, Thread, Handler cowork with each other.

Taking the code from Looper's document for example :
LoopThread extents Thread {

void run() {
//1. creat a Looper which contains a message queue for this thread ,using ThreadLocal
Looper.Prepare()
//2. how this handle is associated with the looper 
mHandler = new Handler() { 
   void handleMessage(Message )
  {
      //handle the message and update the UI
  }

}
//3.get the looper with this thread, start the looper , which is an endless while processing the incoming message 
Looper.Loop(); 
}
} 
 
}

This piece of code is not easy to grasp , at lease for me. I just can not connect the dots from step1 to step3 until look at the code knowing what happend under the hood.

What is prepared? How the stuff prepared will be passed to and used in Looper.looper? Logically, the Looper.prepare() should prepare something that will be needed/used by a new Handler later constructed.
In other words, I was expecting something like following :

Looper looper = new Looper();
mHandler = new Handler(looper);
looper.loop();

Indeed, a looper will be created in the Looper.prepare and will be retrieved in the Handler 's constructor and Looper.loop() by calling Looper.myLooper(). The trick here is that the looper will be set as a thread's local variable using ThreadLocal and each thread should have at most one looper. If the thread has no looper , new Handler() will throw an exception ;if the thread has more than one looper , Looper.loop() will throw an exception.

Thins to remember :
1 ) Looper always binds with and run in a thread !
2 ) Looper has a loop that keeping processing the message by calling msg.target.dispatchMessage() , where the target is a Handler. dispatchMessage will in turns call handle.handleMessage which should override by the user class.
3)The handler can send message to the looper's message queue by calling postMessage().
4)Handler.postMesaage run in caller's thread - worker thread
5)Handler.handleMessage run in his looper's thread.

Why C++ need a copy constructor but Ruby does not

In C++, copy constructor is used to initialize a new declared object with another object by "copying" it.
Copy constructor be needed in 3 cases:

1. declaration ; see //1 and //2 in following code.
2. function parameter passing ;see //3 in following code
3. function return value ; see //4 . However , the copy constructor might not be called due to RVO technology. Compile and run following code; you will only see "copy constructor is called" being output 3 times. //4 will not incur the copy constructor.
#include 

class A {
public:
	A(){}
	A(const A& b){std::cout << "copy constructor is called" << std::endl;}
private:
};

A getA()
{
	A a;
	return a; 
}

void useA(A a ){ }

int main()
{
A a;
A b(a);    //1, call the copy constructor explicitly. 
A c = a;  //2, implicitly 
useA(a); //3
A d = getA(); //4. probably. But might be optimized out because of RVO(Return Value Optimization)

return 0;
}


The compiler will generate a copy constructor for you if you have not defined one yourself. Once your class contains pointer or non-share-able references, the compile generated copy constructor usually is not what you want as it only do the shallow copy, resulting in two objects point to the same resources. This will have two side effects : first, changing one object will effect another; second, we are running into the risk of operating on a NULL pointer. . Here.is a example of the second Ruby has only one constructor (in C++'s terminology) , that is the initialize method. What will happen in following code which are the counterpart of the C++ code above:
a = A.new 
b = A.new(a)   //1  - ignore this at this moment
c = a                //2
useA(a)           //3
d = getA()       //4

The important difference compared with corresponding C++ code are: 1 . c and a are actually refer to same object!
irb(main):001:0> a = Array.new
=> []
irb(main):002:0> c = a
=> []
irb(main):003:0> a.object_id
=> -607438628
irb(main):004:0> c.object_id
=> -607438628

2. modify parameter a in the useA() will modify a , c as well ,because it is the reference to a that is passed - Just as Java, for function parameters, ruby use pass reference by value approach.
irb(main):001:0> a = Array.new
=> []
irb(main):002:0> c = a
=> []
irb(main):003:0> a.object_id
=> -607438628
irb(main):004:0> c.object_id
=> -607438628
irb(main):005:0> def useA a
irb(main):006:1> a[0] = 1
irb(main):007:1> end
=> nil
irb(main):008:0> useA a
=> 1
irb(main):009:0> a
=> [1]
irb(main):010:0> c
=> [1]
irb(main):011:0>
3. for function return value , ruby will return the reference as well.
irb(main):011:0> $d=[1,2]
=> [1, 2]
irb(main):014:0> def getA
irb(main):015:1> $d
irb(main):016:1> end
=> nil
irb(main):017:0> e = getA
=> [1, 2]
irb(main):018:0> e[1] = 3
=> 3
irb(main):019:0> e
=> [1, 3]
irb(main):020:0> $d
=> [1, 3]


Ruby always pass around the object reference by value! By comparison, C++ could pass object by value! When you want to achieve the effect of pass by value using clone method. However, the default clone method in the Class Object will perform the shallow copy of the obj - the instance variables of obj are copied, but not the objects they reference - just as the compiler-generated copy constructor in C++.
class Klass
    attr_accessor :str
	def initialize s
		@str = s
	end
   end
   s1 = Klass.new("Hello")      #=> #
   s2 = s1.clone       #=> #
   s2.str[1,4] = "i"   #=> "i"
   s1.inspect          #=> "#"
   s2.inspect          #=> "#"

s1.str and s2.str actually point to the same object (You could check this by print out s1.str.object_id s2.str.object_id) , so change to s1.str will be reflect on s2.str. We could override the clone method for the KClass to do the deep copy. By this change, s2.str will be pointing to difference object as s1.str do.
class Klass
      attr_accessor :str

	def initialize s
		@str = s
	end
	def clone
		Klass.new(@str.clone)
	end
   end
   s1 = Klass.new("Hello")      #=> #
   s2 = s1.clone       #=> #
   s2.str[1,4] = "i"   #=> "i"
   s1.inspect          #=> "#"
   s2.inspect          #=> "#"


Summary,

1. Ruby always pass object by reference (or pass object reference by value) ; C++ give you the choice of passing by either value or reference

 2. Both Ruby and C++ have to handle object's deep copy. C++ approach it using Copy constructor; Ruby approach it by using clone.

 3. People (newbie) usually don't know or neglect the fact that Ruby is passing by reference (until be bite ) and don't even know the existence of the clone method (until it is necessary ); While every C++ user will learn the concept of copy constructor at their beginning of the learning adventure. So, Ruby is very easy to get started and C++ is much difficult. However, as programming languages, they all have to solve the fundamental issues such as object copy, though through different ways.

understand SQL SELECT using select/map/reduce


SQL is acronym for Structured _Query_ Language. It might be safe to say once we understand how to query a database we master the fundamental part of SQL.

There are three basic operations we want to apply on a set of data:
1. Filter, selecting only the elements that match a criteria/predicate.
       {Xi} -> { Xi | predicate(Xi) == true}

2. Map, making a transformation on each element,resulting a new set.
       {Xi} -> {f(Xi)}

3. Reduce, reduce to a single value basing on a collection of the elements. e.g: calculate the sum of all values.
       {Xi} -> R

Those operations , filter, map and reduces, are build-in operations in high level languages such as Ruby and Haskell. (Check references 2,3 at the end of the article )

SQL combines all the three operations using a single syntax :
SELECT projections FROM tables WHERE predicate
Quite neat!

In this article we will make a comparison between Ruby's syntax and SQL SELECT statement. The sample data we will be exercising upon is the Beatles Albums and we will be focusing in the album name only.

Prerequisite:

For the database, we have created a  TABLE call albums , with columns id and name.
For the Ruby code, we have a array variable albums, initialized with the all the albums' name.

1. Filter : 
select all the albums whose name's length is greater than 8
#SQL
SELECT id , name  FROM albums WHERE LENGTH(name) > 8;

#RUBY
albums.select (|x| x.length > 8)

"id, name" are the columns of the table. You can also apply functions on the column.  There are two types of functions scalar function and aggregate function.

Scalar functions :  Operate against a single value, and return a single value based on the input value. It is called map in functional programming terminology.

Aggregate functions : Operate against a collection of values, but return a single, summarizing value. It is called reduce in functional programming terminology.

2.map (scalar function)
Convert all the albums' name to upper case
#SQL
SELECT UPPER(name) FROM albums ;

#Ruby
albums.map {|x| x.upcase}


3.reduce(aggregate function)
Calculate the total numbers of all the albums
#SQL
SELECT COUNT(*) FROM albums;

#Ruby
albums.reduce(0) { |s,_| s+1}


4. combination of all the operation above
Calculate the summary of albums name's length on the albums whose name's length is greater than eight
#SQL
SELECT SUM(LENGTH(name)) FROM albums WHERE LENGTH(name) > 8;

#Ruby
albums.select{|x| x.length > 8}.map{|d| d.length}.reduce(0) {|s,i| s+i }


References:

1. List Comprehension
2. Filter,Map, reduce in Ruby
3. Map ,reduce in Haskhell
4. SQL function