1 /*
2  * Copyright (C) 2010 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 #ifndef VIDEO_SOURCE_H_
18 
19 #define VIDEO_SOURCE_H_
20 
21 #include <media/MediaSource.h>
22 #include <media/stagefright/MediaBufferGroup.h>
23 #include <media/stagefright/MediaDefs.h>
24 #include <media/stagefright/MetaData.h>
25 
26 namespace android {
27 
28 class VideoSource : public MediaSource {
29     static const int32_t kFramerate = 24;  // fps
30 
31 public:
VideoSource(int width,int height)32     VideoSource(int width, int height)
33         : mWidth(width),
34           mHeight(height),
35           mSize((width * height * 3) / 2) {
36         mGroup.add_buffer(new MediaBuffer(mSize));
37     }
38 
getFormat()39     virtual sp<MetaData> getFormat() {
40         sp<MetaData> meta = new MetaData;
41         meta->setInt32(kKeyWidth, mWidth);
42         meta->setInt32(kKeyHeight, mHeight);
43         meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
44 
45         return meta;
46     }
47 
start(MetaData * params)48     virtual status_t start(MetaData *params) {
49         mNumFramesOutput = 0;
50         return OK;
51     }
52 
stop()53     virtual status_t stop() {
54         return OK;
55     }
56 
read(MediaBuffer ** buffer,const MediaSource::ReadOptions * options)57     virtual status_t read(
58             MediaBuffer **buffer, const MediaSource::ReadOptions *options) {
59         if (mNumFramesOutput == kFramerate * 100) {
60             // Stop returning data after 10 secs.
61             return ERROR_END_OF_STREAM;
62         }
63 
64         // printf("VideoSource::read\n");
65         status_t err = mGroup.acquire_buffer(buffer);
66         if (err != OK) {
67             return err;
68         }
69 
70         char x = (char)((double)rand() / RAND_MAX * 255);
71         memset((*buffer)->data(), x, mSize);
72         (*buffer)->set_range(0, mSize);
73         (*buffer)->meta_data()->clear();
74         (*buffer)->meta_data()->setInt64(
75                 kKeyTime, (mNumFramesOutput * 1000000) / kFramerate);
76         ++mNumFramesOutput;
77 
78         // printf("VideoSource::read - returning buffer\n");
79         // LOG(INFO)("VideoSource::read - returning buffer");
80         return OK;
81     }
82 
83 protected:
~VideoSource()84     virtual ~VideoSource() {}
85 
86 private:
87     MediaBufferGroup mGroup;
88     int mWidth, mHeight;
89     size_t mSize;
90     int64_t mNumFramesOutput;;
91 
92     VideoSource(const VideoSource &);
93     VideoSource &operator=(const VideoSource &);
94 };
95 
96 }  // namespace android
97 
98 #endif  // VIDEO_SOURCE_H_
99