×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Regular Contributor
    Join Date
    Oct 2006
    Posts
    157

    CCamera::Reserve() and MCameraObserver::ReserveComplete()

    If you read the documentation you find that it's stated that CCamera::Reserve() is an asynchronous function that initiate and reserves the camera and when completed calls MCameraObserver::ReserveComplete().

    So my minds image of it was something like; a new thread is created (since it's asynchronous), function returns and, in the background, the camera initiates, reserves and ReserveComplete() is immediately called.

    But when running some simple code, like this;

    void TestCamera::ConstructL()
    {
    iCamera = CCamera::NewL(*this, 0);
    iCamera->Reserve();

    while(!iDone);
    }

    void TestCamera::ReserveComplete(TInt aError)
    {
    iDone = ETrue;
    }

    I found that ConstructL() never returns. So ReserveComplete() isn't actually called directly after the camera is reserved. My guess is that a flag or something is set and some time, after the function that have called TestCamera::ConstructL() (in my case HelloAppUI::ConstructL()) the call to ReserveComplete() is done.

    In regular use of CCamera wont be a problem, since there is no need to wait for the camera to be reserved in the same function that do the the call to Reserve(). But in my special case I need to do this.

    So what does acctually happends, when is the acctual call to ReserveComplete() made, and why does it work like this?

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,734

    Re: CCamera::Reserve() and MCameraObserver::ReserveComplete()

    Quote Originally Posted by erst
    So my minds image of it was something like; a new thread is created (since it's asynchronous), function returns and, in the background, the camera initiates, reserves and ReserveComplete() is immediately called.
    However it happens otherwise.
    Symbian does not use threads unless it is explicitly stated (like in CImageDecoder::TOptions::EOptionAlwaysThread), and even it uses threads, it never calls back to your code from another thread.
    CCamera has a hidden active object somewhere, and RunL of that active object invokes your ReserveComplete, etc. methods. This happens in case of any other M-class based callback too.
    So in practice the camera gets reserved, but your ReserveComplete cannot be invoked.
    But when running some simple code, like this;

    void TestCamera::ConstructL()
    {
    iCamera = CCamera::NewL(*this, 0);
    iCamera->Reserve();

    while(!iDone);
    }

    void TestCamera::ReserveComplete(TInt aError)
    {
    iDone = ETrue;
    }
    Note that "while(!iDone)" is very CPU intensive, and considered bad in any other programming environment too.

    However starting a nested scheduler loop should work:
    Code:
    void TestCamera::ConstructL()
    {
      iCamera = CCamera::NewL(*this, 0);
      iCamera->Reserve();
    
      CActiveScheduler::Start(); // this call returns after CActiveScheduler::Stop gets invoked.
    }
    
    void TestCamera::ReserveComplete(TInt aError)
    {
      CActiveScheduler::Stop();
    }

  3. #3
    Regular Contributor
    Join Date
    Oct 2006
    Posts
    157

    Re: CCamera::Reserve() and MCameraObserver::ReserveComplete()

    Ok, didn't know that it never calls back to my code through another thread.

    For my propose it would be easiest with a straight forward synchronous approach, something like this:

    Reserve()
    {
    TInt success;
    success = InitiateCamera();
    success = ReserveCamera();

    if (success)
    MCameraObserver::ReserveComplete();
    }

    Of course this is not suitable when you want to do do other stuff while you'r waiting for the camera to be initiated and reserved.

    I'll try with CActiveScheduler and come back...

  4. #4
    Regular Contributor
    Join Date
    Oct 2006
    Posts
    157

    Re: CCamera::Reserve() and MCameraObserver::ReserveComplete()

    Ok, seems to work! I was just a little confused when CActiveScheduler::Start() doesn't hang the application, but that is of course only good. Thanks!

    So what should happen after CActiveScheduler::Start() is called is:
    1. all pending active objects executes their RunL() function
    2. this somehow make CCamera call MCameraObserver::ReserveComplete()
    3. ReserveComplete() do what it needs and finaly calls CActiveScheduler::Stop() and returns
    4. CActiveScheduler::Start() will now also return and everyone is happy!

    Have I got it correctly?

    What is actually happening when CActiveScheduler::Start() is not called explicitly then?

  5. #5
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,734

    Re: CCamera::Reserve() and MCameraObserver::ReserveComplete()

    Quote Originally Posted by erst
    Have I got it correctly?
    Yes, I guess so.
    What is actually happening when CActiveScheduler::Start() is not called explicitly then?
    Your ConstructL will continue (and return) immediately. After a while ReserveComplete is going to be invoked of course, and after that you can PowerOn, etc.
    The nested scheduler loop is not necessary at all; it is there because your original code looks like if you want wait for the completion of reservation - making an asynchronous/non-blocking action synchronous/blocking.

  6. #6
    Nokia Developer Expert
    Join Date
    Dec 2003
    Posts
    148

    Re: CCamera::Reserve() and MCameraObserver::ReserveComplete()

    You should use CActiveSchedulerWait instead of CActiveScheduler.
    janza

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×