• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 NUPLAYER_RENDERER_H_
18 
19 #define NUPLAYER_RENDERER_H_
20 
21 #include "NuPlayer.h"
22 
23 namespace android {
24 
25 struct ABuffer;
26 
27 struct NuPlayer::Renderer : public AHandler {
28     Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
29              const sp<AMessage> &notify);
30 
31     void queueBuffer(
32             bool audio,
33             const sp<ABuffer> &buffer,
34             const sp<AMessage> &notifyConsumed);
35 
36     void queueEOS(bool audio, status_t finalResult);
37 
38     void flush(bool audio);
39 
40     void signalTimeDiscontinuity();
41 
42     void signalAudioSinkChanged();
43 
44     void pause();
45     void resume();
46 
47     enum {
48         kWhatEOS                = 'eos ',
49         kWhatFlushComplete      = 'fluC',
50         kWhatPosition           = 'posi',
51     };
52 
53 protected:
54     virtual ~Renderer();
55 
56     virtual void onMessageReceived(const sp<AMessage> &msg);
57 
58 private:
59     enum {
60         kWhatDrainAudioQueue    = 'draA',
61         kWhatDrainVideoQueue    = 'draV',
62         kWhatQueueBuffer        = 'queB',
63         kWhatQueueEOS           = 'qEOS',
64         kWhatFlush              = 'flus',
65         kWhatAudioSinkChanged   = 'auSC',
66         kWhatPause              = 'paus',
67         kWhatResume             = 'resm',
68     };
69 
70     struct QueueEntry {
71         sp<ABuffer> mBuffer;
72         sp<AMessage> mNotifyConsumed;
73         size_t mOffset;
74         status_t mFinalResult;
75     };
76 
77     static const int64_t kMinPositionUpdateDelayUs;
78 
79     sp<MediaPlayerBase::AudioSink> mAudioSink;
80     sp<AMessage> mNotify;
81     List<QueueEntry> mAudioQueue;
82     List<QueueEntry> mVideoQueue;
83     uint32_t mNumFramesWritten;
84 
85     bool mDrainAudioQueuePending;
86     bool mDrainVideoQueuePending;
87     int32_t mAudioQueueGeneration;
88     int32_t mVideoQueueGeneration;
89 
90     int64_t mAnchorTimeMediaUs;
91     int64_t mAnchorTimeRealUs;
92 
93     Mutex mFlushLock;  // protects the following 2 member vars.
94     bool mFlushingAudio;
95     bool mFlushingVideo;
96 
97     bool mHasAudio;
98     bool mHasVideo;
99     bool mSyncQueues;
100 
101     bool mPaused;
102 
103     int64_t mLastPositionUpdateUs;
104     int64_t mVideoLateByUs;
105 
106     bool onDrainAudioQueue();
107     void postDrainAudioQueue(int64_t delayUs = 0);
108 
109     void onDrainVideoQueue();
110     void postDrainVideoQueue();
111 
112     void onQueueBuffer(const sp<AMessage> &msg);
113     void onQueueEOS(const sp<AMessage> &msg);
114     void onFlush(const sp<AMessage> &msg);
115     void onAudioSinkChanged();
116     void onPause();
117     void onResume();
118 
119     void notifyEOS(bool audio, status_t finalResult);
120     void notifyFlushComplete(bool audio);
121     void notifyPosition();
122     void notifyVideoLateBy(int64_t lateByUs);
123 
124     void flushQueue(List<QueueEntry> *queue);
125     bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
126     void syncQueuesDone();
127 
128     DISALLOW_EVIL_CONSTRUCTORS(Renderer);
129 };
130 
131 }  // namespace android
132 
133 #endif  // NUPLAYER_RENDERER_H_
134