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