Home | History | Annotate | Download | only in include
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      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
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 // -*- c++ -*-
     19 #ifndef PACKET_H
     20 #define PACKET_H
     21 
     22 #include "oscl_media_data.h"
     23 #include "oscl_media_status.h"
     24 
     25 #include "media_fragment.h"
     26 
     27 const int DEF_PACKET_MAX_FRAGMENTS = 128;
     28 const int DEF_PACKET_IMMEDIATE_DATA = 128;
     29 const int DEF_MAX_PACKET_SIZE = 1024;
     30 
     31 /* A packet represents a message.  It contains a list of MediaFragments.
     32    Packets can be chained. */
     33 class Packet : public MediaData<Packet, DEF_PACKET_MAX_FRAGMENTS, DEF_PACKET_IMMEDIATE_DATA>
     34 {
     35     public:
     36         Packet(): flat_ptr(NULL)
     37         {
     38         }
     39 
     40         virtual ~Packet()
     41         {
     42             if (flat_ptr) OSCL_DEFAULT_FREE(flat_ptr);
     43             flat_ptr = NULL;
     44             Clear();
     45         }
     46 
     47         BufFragStatusClass::status_t AddBufferFragment(BufferFragment& frag, BufferState* state)
     48         {
     49             return AddFragment(frag, state);
     50         }
     51 
     52         virtual uint8* AddHeader(MediaFragment* mfrag)
     53         {
     54             AddFragment(*mfrag->GetFragment(), mfrag->GetState(), 0);
     55             return mfrag->GetPtr();
     56         }
     57 
     58         virtual uint8* AddTrailer(MediaFragment* mfrag)
     59         {
     60             AddFragment(*mfrag->GetFragment(), mfrag->GetState(), APPEND_MEDIA_AT_END);
     61             return mfrag->GetPtr();
     62         }
     63 
     64         /*  virtual BufFragStatus AddFragment(MediaFragment* fragment, int32 location_offset = APPEND_MEDIA_AT_END) = 0;
     65           virtual void Clear() = 0;
     66           virtual BufferFragment* GetFragments() = 0;
     67           virtual MediaFragment* GetFragment(int32 idx, MediaFragment& fragment) = 0;
     68           virtual int GetAvailableBufferSize() = 0;
     69           virtual int32 GetNumFrags() const = 0;
     70           virtual int32 GetMaxFrags() const = 0;
     71           virtual uint32 GetLength() const = 0;
     72           virtual int GetMediaSize() const = 0;
     73           virtual void AppendNext(Packet *next_ptr) = 0;
     74           virtual Packet*  GetNext() const = 0;*/
     75         /* constructs a flat buffer containing the media data.  It gets deleted when the Packet is destroyed
     76            Shall be removed in later versions */
     77 
     78         virtual uint8* GetDataPtr()
     79         {
     80             BufferFragment* frag = NULL;
     81             if (GetMediaSize() <= 0) return NULL;
     82             frag = GetFragment(0);
     83             return ((uint8 *) frag->ptr);
     84         }
     85 
     86         virtual uint8* GetMediaPtr()
     87         {
     88             uint8* buf = NULL;
     89             BufferFragment* frag = NULL;
     90 
     91             if (GetMediaSize() <= 0) return NULL;
     92             if (flat_ptr) return flat_ptr;
     93             buf = flat_ptr = (uint8*)OSCL_DEFAULT_MALLOC(GetMediaSize());
     94             for (int frag_num = 0; frag_num < GetNumFrags(); frag_num++)
     95             {
     96                 frag = GetFragment(frag_num);
     97                 oscl_memcpy(buf, frag->ptr, frag->len);
     98                 buf += frag->len;
     99             }
    100             return flat_ptr;
    101         }
    102 
    103         virtual void ClearMediaPtr()
    104         {
    105             if (flat_ptr) OSCL_DEFAULT_FREE(flat_ptr);
    106             flat_ptr = NULL;
    107         }
    108     private:
    109         uint8* flat_ptr;
    110 };
    111 
    112 
    113 typedef enum PacketTypes
    114 {
    115     EPacketDefault
    116 } EPacketType;
    117 
    118 #endif
    119