Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "DummyRecorder"
     18 // #define LOG_NDEBUG 0
     19 
     20 #include <media/stagefright/MediaBuffer.h>
     21 #include <media/stagefright/MediaSource.h>
     22 #include "DummyRecorder.h"
     23 
     24 #include <utils/Log.h>
     25 
     26 namespace android {
     27 
     28 // static
     29 void *DummyRecorder::threadWrapper(void *pthis) {
     30     ALOGV("ThreadWrapper: %p", pthis);
     31     DummyRecorder *writer = static_cast<DummyRecorder *>(pthis);
     32     writer->readFromSource();
     33     return NULL;
     34 }
     35 
     36 
     37 status_t DummyRecorder::start() {
     38     ALOGV("Start");
     39     mStarted = true;
     40 
     41     mSource->start();
     42 
     43     pthread_attr_t attr;
     44     pthread_attr_init(&attr);
     45     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
     46     int err = pthread_create(&mThread, &attr, threadWrapper, this);
     47     pthread_attr_destroy(&attr);
     48 
     49     if (err) {
     50         ALOGE("Error creating thread!");
     51         return -ENODEV;
     52     }
     53     return OK;
     54 }
     55 
     56 
     57 status_t DummyRecorder::stop() {
     58     ALOGV("Stop");
     59     mStarted = false;
     60 
     61     mSource->stop();
     62     void *dummy;
     63     pthread_join(mThread, &dummy);
     64     status_t err = (status_t) dummy;
     65 
     66     ALOGV("Ending the reading thread");
     67     return err;
     68 }
     69 
     70 // pretend to read the source buffers
     71 void DummyRecorder::readFromSource() {
     72     ALOGV("ReadFromSource");
     73     if (!mStarted) {
     74         return;
     75     }
     76 
     77     status_t err = OK;
     78     MediaBuffer *buffer;
     79     ALOGV("A fake writer accessing the frames");
     80     while (mStarted && (err = mSource->read(&buffer)) == OK){
     81         // if not getting a valid buffer from source, then exit
     82         if (buffer == NULL) {
     83             return;
     84         }
     85         buffer->release();
     86         buffer = NULL;
     87     }
     88 }
     89 
     90 
     91 } // end of namespace android
     92