PlaybackSession.h revision dca352880e210e0ca0ff39de074540d3640ecfab
1/*
2 * Copyright 2012, 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 PLAYBACK_SESSION_H_
18
19#define PLAYBACK_SESSION_H_
20
21#include "WifiDisplaySource.h"
22
23namespace android {
24
25struct ABuffer;
26struct BufferQueue;
27struct ISurfaceTexture;
28struct MediaPuller;
29struct MediaSource;
30struct Serializer;
31struct TSPacketizer;
32
33#define LOG_TRANSPORT_STREAM    0
34
35// Encapsulates the state of an RTP/RTCP session in the context of wifi
36// display.
37struct WifiDisplaySource::PlaybackSession : public AHandler {
38    PlaybackSession(
39            const sp<ANetworkSession> &netSession,
40            const sp<AMessage> &notify,
41            bool legacyMode);
42
43    status_t init(
44            const char *clientIP, int32_t clientRtp, int32_t clientRtcp,
45            bool useInterleavedTCP);
46
47    status_t destroy();
48
49    int32_t getRTPPort() const;
50
51    int64_t getLastLifesignUs() const;
52    void updateLiveness();
53
54    status_t play();
55    status_t pause();
56
57    sp<ISurfaceTexture> getSurfaceTexture();
58    int32_t width() const;
59    int32_t height() const;
60
61    enum {
62        kWhatSessionDead,
63        kWhatBinaryData,
64    };
65
66protected:
67    virtual void onMessageReceived(const sp<AMessage> &msg);
68    virtual ~PlaybackSession();
69
70private:
71    struct Track;
72
73    enum {
74        kWhatSendSR,
75        kWhatRTPNotify,
76        kWhatRTCPNotify,
77        kWhatSerializerNotify,
78        kWhatConverterNotify,
79        kWhatUpdateSurface,
80    };
81
82    static const int64_t kSendSRIntervalUs = 10000000ll;
83    static const uint32_t kSourceID = 0xdeadbeef;
84    static const size_t kMaxHistoryLength = 128;
85
86    sp<ANetworkSession> mNetSession;
87    sp<AMessage> mNotify;
88    bool mLegacyMode;
89
90    int64_t mLastLifesignUs;
91
92    sp<ALooper> mSerializerLooper;
93    sp<Serializer> mSerializer;
94    sp<TSPacketizer> mPacketizer;
95    sp<BufferQueue> mBufferQueue;
96
97    KeyedVector<size_t, sp<Track> > mTracks;
98    ssize_t mVideoTrackIndex;
99
100    sp<ABuffer> mTSQueue;
101    int64_t mPrevTimeUs;
102
103    bool mUseInterleavedTCP;
104
105    // in TCP mode
106    int32_t mRTPChannel;
107    int32_t mRTCPChannel;
108
109    // in UDP mode
110    int32_t mRTPPort;
111    int32_t mRTPSessionID;
112    int32_t mRTCPSessionID;
113
114
115    uint32_t mRTPSeqNo;
116
117    uint64_t mLastNTPTime;
118    uint32_t mLastRTPTime;
119    uint32_t mNumRTPSent;
120    uint32_t mNumRTPOctetsSent;
121    uint32_t mNumSRsSent;
122
123    bool mSendSRPending;
124
125    int64_t mFirstPacketTimeUs;
126
127    List<sp<ABuffer> > mHistory;
128    size_t mHistoryLength;
129
130    uint64_t mTotalBytesSent;
131
132#if LOG_TRANSPORT_STREAM
133    FILE *mLogFile;
134#endif
135
136    void onSendSR();
137    void addSR(const sp<ABuffer> &buffer);
138    void addSDES(const sp<ABuffer> &buffer);
139    static uint64_t GetNowNTP();
140
141    status_t setupPacketizer();
142
143    status_t addFakeSources();
144
145    status_t addSource(
146            bool isVideo,
147            const sp<MediaSource> &source,
148            size_t *numInputBuffers);
149
150    status_t addVideoSource();
151    status_t addAudioSource();
152
153    ssize_t appendTSData(
154            const void *data, size_t size, bool timeDiscontinuity, bool flush);
155
156    void scheduleSendSR();
157
158    status_t parseRTCP(const sp<ABuffer> &buffer);
159    status_t parseTSFB(const uint8_t *data, size_t size);
160
161    DISALLOW_EVIL_CONSTRUCTORS(PlaybackSession);
162};
163
164}  // namespace android
165
166#endif  // PLAYBACK_SESSION_H_
167
168