Home | History | Annotate | Download | only in source
      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 SENDER_H_
     18 
     19 #define SENDER_H_
     20 
     21 #include <media/stagefright/foundation/AHandler.h>
     22 
     23 namespace android {
     24 
     25 #define LOG_TRANSPORT_STREAM            0
     26 #define ENABLE_RETRANSMISSION           0
     27 #define TRACK_BANDWIDTH                 0
     28 
     29 struct ABuffer;
     30 struct ANetworkSession;
     31 
     32 struct Sender : public AHandler {
     33     Sender(const sp<ANetworkSession> &netSession, const sp<AMessage> &notify);
     34 
     35     enum {
     36         kWhatInitDone,
     37         kWhatSessionDead,
     38         kWhatBinaryData,
     39     };
     40 
     41     enum TransportMode {
     42         TRANSPORT_UDP,
     43         TRANSPORT_TCP_INTERLEAVED,
     44         TRANSPORT_TCP,
     45     };
     46     status_t init(
     47             const char *clientIP, int32_t clientRtp, int32_t clientRtcp,
     48             TransportMode transportMode);
     49 
     50     status_t finishInit();
     51 
     52     int32_t getRTPPort() const;
     53 
     54     void queuePackets(int64_t timeUs, const sp<ABuffer> &packets);
     55     void scheduleSendSR();
     56 
     57 protected:
     58     virtual ~Sender();
     59     virtual void onMessageReceived(const sp<AMessage> &msg);
     60 
     61 private:
     62     enum {
     63         kWhatQueuePackets,
     64         kWhatSendSR,
     65         kWhatRTPNotify,
     66         kWhatRTCPNotify,
     67 #if ENABLE_RETRANSMISSION
     68         kWhatRTPRetransmissionNotify,
     69         kWhatRTCPRetransmissionNotify
     70 #endif
     71     };
     72 
     73     static const int64_t kSendSRIntervalUs = 10000000ll;
     74 
     75     static const uint32_t kSourceID = 0xdeadbeef;
     76     static const size_t kMaxHistoryLength = 128;
     77 
     78 #if ENABLE_RETRANSMISSION
     79     static const size_t kRetransmissionPortOffset = 120;
     80 #endif
     81 
     82     sp<ANetworkSession> mNetSession;
     83     sp<AMessage> mNotify;
     84 
     85     sp<ABuffer> mTSQueue;
     86 
     87     TransportMode mTransportMode;
     88     AString mClientIP;
     89 
     90     // in TCP mode
     91     int32_t mRTPChannel;
     92     int32_t mRTCPChannel;
     93 
     94     // in UDP mode
     95     int32_t mRTPPort;
     96     int32_t mRTPSessionID;
     97     int32_t mRTCPSessionID;
     98 
     99 #if ENABLE_RETRANSMISSION
    100     int32_t mRTPRetransmissionSessionID;
    101     int32_t mRTCPRetransmissionSessionID;
    102 #endif
    103 
    104     int32_t mClientRTPPort;
    105     int32_t mClientRTCPPort;
    106     bool mRTPConnected;
    107     bool mRTCPConnected;
    108 
    109 
    110     int64_t mFirstOutputBufferReadyTimeUs;
    111     int64_t mFirstOutputBufferSentTimeUs;
    112 
    113     uint32_t mRTPSeqNo;
    114 #if ENABLE_RETRANSMISSION
    115     uint32_t mRTPRetransmissionSeqNo;
    116 #endif
    117 
    118     uint64_t mLastNTPTime;
    119     uint32_t mLastRTPTime;
    120     uint32_t mNumRTPSent;
    121     uint32_t mNumRTPOctetsSent;
    122     uint32_t mNumSRsSent;
    123 
    124     bool mSendSRPending;
    125 
    126 #if ENABLE_RETRANSMISSION
    127     List<sp<ABuffer> > mHistory;
    128     size_t mHistoryLength;
    129 #endif
    130 
    131 #if TRACK_BANDWIDTH
    132     int64_t mFirstPacketTimeUs;
    133     uint64_t mTotalBytesSent;
    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 #if LOG_TRANSPORT_STREAM
    142     FILE *mLogFile;
    143 #endif
    144 
    145     ssize_t appendTSData(
    146             const void *data, size_t size, bool timeDiscontinuity, bool flush);
    147 
    148     void onQueuePackets(const sp<ABuffer> &packets);
    149 
    150 #if ENABLE_RETRANSMISSION
    151     status_t parseTSFB(const uint8_t *data, size_t size);
    152 #endif
    153 
    154     status_t parseRTCP(const sp<ABuffer> &buffer);
    155 
    156     status_t sendPacket(int32_t sessionID, const void *data, size_t size);
    157 
    158     void notifyInitDone();
    159     void notifySessionDead();
    160 
    161     DISALLOW_EVIL_CONSTRUCTORS(Sender);
    162 };
    163 
    164 }  // namespace android
    165 
    166 #endif  // SENDER_H_
    167