stream.cpp revision 2df788fb0c402938f827bf9c9ce2ca3ab1dcd464
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#include <binder/ProcessState.h>
18
19#include <media/IStreamSource.h>
20#include <media/mediaplayer.h>
21#include <media/stagefright/foundation/ADebug.h>
22#include <media/stagefright/foundation/AMessage.h>
23
24#include <binder/IServiceManager.h>
25#include <media/IMediaPlayerService.h>
26#include <surfaceflinger/ISurfaceComposer.h>
27#include <surfaceflinger/SurfaceComposerClient.h>
28
29#include <fcntl.h>
30
31using namespace android;
32
33struct MyStreamSource : public BnStreamSource {
34    // Caller retains ownership of fd.
35    MyStreamSource(int fd);
36
37    virtual void setListener(const sp<IStreamListener> &listener);
38    virtual void setBuffers(const Vector<sp<IMemory> > &buffers);
39
40    virtual void onBufferAvailable(size_t index);
41
42protected:
43    virtual ~MyStreamSource();
44
45private:
46    int mFd;
47    off64_t mFileSize;
48    int64_t mNextSeekTimeUs;
49
50    sp<IStreamListener> mListener;
51    Vector<sp<IMemory> > mBuffers;
52
53    DISALLOW_EVIL_CONSTRUCTORS(MyStreamSource);
54};
55
56MyStreamSource::MyStreamSource(int fd)
57    : mFd(fd),
58      mFileSize(0),
59      mNextSeekTimeUs(-1) {  // ALooper::GetNowUs() + 5000000ll) {
60    CHECK_GE(fd, 0);
61
62    mFileSize = lseek64(fd, 0, SEEK_END);
63    lseek64(fd, 0, SEEK_SET);
64}
65
66MyStreamSource::~MyStreamSource() {
67}
68
69void MyStreamSource::setListener(const sp<IStreamListener> &listener) {
70    mListener = listener;
71}
72
73void MyStreamSource::setBuffers(const Vector<sp<IMemory> > &buffers) {
74    mBuffers = buffers;
75}
76
77void MyStreamSource::onBufferAvailable(size_t index) {
78    CHECK_LT(index, mBuffers.size());
79
80    if (mNextSeekTimeUs >= 0 && mNextSeekTimeUs <= ALooper::GetNowUs()) {
81        off64_t offset = (off64_t)(((float)rand() / RAND_MAX) * mFileSize * 0.8);
82        offset = (offset / 188) * 188;
83
84        lseek(mFd, offset, SEEK_SET);
85
86        mListener->issueCommand(
87                IStreamListener::DISCONTINUITY, false /* synchronous */);
88
89        mNextSeekTimeUs = -1;
90        mNextSeekTimeUs = ALooper::GetNowUs() + 5000000ll;
91    }
92
93    sp<IMemory> mem = mBuffers.itemAt(index);
94
95    ssize_t n = read(mFd, mem->pointer(), mem->size());
96    if (n <= 0) {
97        mListener->issueCommand(IStreamListener::EOS, false /* synchronous */);
98    } else {
99        mListener->queueBuffer(index, n);
100    }
101}
102
103////////////////////////////////////////////////////////////////////////////////
104
105struct MyClient : public BnMediaPlayerClient {
106    MyClient()
107        : mEOS(false) {
108    }
109
110    virtual void notify(int msg, int ext1, int ext2, const Parcel *obj) {
111        Mutex::Autolock autoLock(mLock);
112
113        if (msg == MEDIA_ERROR || msg == MEDIA_PLAYBACK_COMPLETE) {
114            mEOS = true;
115            mCondition.signal();
116        }
117    }
118
119    void waitForEOS() {
120        Mutex::Autolock autoLock(mLock);
121        while (!mEOS) {
122            mCondition.wait(mLock);
123        }
124    }
125
126protected:
127    virtual ~MyClient() {
128    }
129
130private:
131    Mutex mLock;
132    Condition mCondition;
133
134    bool mEOS;
135
136    DISALLOW_EVIL_CONSTRUCTORS(MyClient);
137};
138
139int main(int argc, char **argv) {
140    android::ProcessState::self()->startThreadPool();
141
142    if (argc != 2) {
143        fprintf(stderr, "Usage: %s filename\n", argv[0]);
144        return 1;
145    }
146
147    sp<SurfaceComposerClient> composerClient = new SurfaceComposerClient;
148    CHECK_EQ(composerClient->initCheck(), (status_t)OK);
149
150    sp<SurfaceControl> control =
151        composerClient->createSurface(
152                String8("A Surface"),
153                0,
154                1280,
155                800,
156                PIXEL_FORMAT_RGB_565,
157                0);
158
159    CHECK(control != NULL);
160    CHECK(control->isValid());
161
162    SurfaceComposerClient::openGlobalTransaction();
163    CHECK_EQ(control->setLayer(30000), (status_t)OK);
164    CHECK_EQ(control->show(), (status_t)OK);
165    SurfaceComposerClient::closeGlobalTransaction();
166
167    sp<Surface> surface = control->getSurface();
168    CHECK(surface != NULL);
169
170    sp<IServiceManager> sm = defaultServiceManager();
171    sp<IBinder> binder = sm->getService(String16("media.player"));
172    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
173
174    CHECK(service.get() != NULL);
175
176    int fd = open(argv[1], O_RDONLY);
177
178    if (fd < 0) {
179        fprintf(stderr, "Failed to open file '%s'.", argv[1]);
180        return 1;
181    }
182
183    sp<MyClient> client = new MyClient;
184
185    sp<IMediaPlayer> player =
186        service->create(getpid(), client, new MyStreamSource(fd), 0);
187
188    if (player != NULL) {
189        player->setVideoSurface(surface);
190        player->start();
191
192        client->waitForEOS();
193
194        player->stop();
195    } else {
196        fprintf(stderr, "failed to instantiate player.\n");
197    }
198
199    close(fd);
200    fd = -1;
201
202    composerClient->dispose();
203
204    return 0;
205}
206