Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef MEDIA_BASE_DECODER_BUFFER_H_
      6 #define MEDIA_BASE_DECODER_BUFFER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/logging.h"
     11 #include "base/memory/aligned_memory.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/time/time.h"
     15 #include "build/build_config.h"
     16 #include "media/base/decrypt_config.h"
     17 #include "media/base/media_export.h"
     18 
     19 namespace media {
     20 
     21 // A specialized buffer for interfacing with audio / video decoders.
     22 //
     23 // Specifically ensures that data is aligned and padded as necessary by the
     24 // underlying decoding framework.  On desktop platforms this means memory is
     25 // allocated using FFmpeg with particular alignment and padding requirements.
     26 //
     27 // Also includes decoder specific functionality for decryption.
     28 //
     29 // NOTE: It is illegal to call any method when end_of_stream() is true.
     30 class MEDIA_EXPORT DecoderBuffer
     31     : public base::RefCountedThreadSafe<DecoderBuffer> {
     32  public:
     33   enum {
     34     kPaddingSize = 16,
     35 #if defined(ARCH_CPU_ARM_FAMILY)
     36     kAlignmentSize = 16
     37 #else
     38     kAlignmentSize = 32
     39 #endif
     40   };
     41 
     42   // Allocates buffer with |size| >= 0.  Buffer will be padded and aligned
     43   // as necessary.
     44   explicit DecoderBuffer(int size);
     45 
     46   // Create a DecoderBuffer whose |data_| is copied from |data|.  Buffer will be
     47   // padded and aligned as necessary.  |data| must not be NULL and |size| >= 0.
     48   static scoped_refptr<DecoderBuffer> CopyFrom(const uint8* data, int size);
     49 
     50   // Create a DecoderBuffer whose |data_| is copied from |data| and |side_data_|
     51   // is copied from |side_data|. Buffers will be padded and aligned as necessary
     52   // Data pointers must not be NULL and sizes must be >= 0.
     53   static scoped_refptr<DecoderBuffer> CopyFrom(const uint8* data, int size,
     54                                                const uint8* side_data,
     55                                                int side_data_size);
     56 
     57   // Create a DecoderBuffer indicating we've reached end of stream.
     58   //
     59   // Calling any method other than end_of_stream() on the resulting buffer
     60   // is disallowed.
     61   static scoped_refptr<DecoderBuffer> CreateEOSBuffer();
     62 
     63   base::TimeDelta timestamp() const {
     64     DCHECK(!end_of_stream());
     65     return timestamp_;
     66   }
     67 
     68   void set_timestamp(const base::TimeDelta& timestamp) {
     69     DCHECK(!end_of_stream());
     70     timestamp_ = timestamp;
     71   }
     72 
     73   base::TimeDelta duration() const {
     74     DCHECK(!end_of_stream());
     75     return duration_;
     76   }
     77 
     78   void set_duration(const base::TimeDelta& duration) {
     79     DCHECK(!end_of_stream());
     80     duration_ = duration;
     81   }
     82 
     83   const uint8* data() const {
     84     DCHECK(!end_of_stream());
     85     return data_.get();
     86   }
     87 
     88   uint8* writable_data() const {
     89     DCHECK(!end_of_stream());
     90     return data_.get();
     91   }
     92 
     93   int data_size() const {
     94     DCHECK(!end_of_stream());
     95     return size_;
     96   }
     97 
     98   const uint8* side_data() const {
     99     DCHECK(!end_of_stream());
    100     return side_data_.get();
    101   }
    102 
    103   int side_data_size() const {
    104     DCHECK(!end_of_stream());
    105     return side_data_size_;
    106   }
    107 
    108   const DecryptConfig* decrypt_config() const {
    109     DCHECK(!end_of_stream());
    110     return decrypt_config_.get();
    111   }
    112 
    113   void set_decrypt_config(scoped_ptr<DecryptConfig> decrypt_config) {
    114     DCHECK(!end_of_stream());
    115     decrypt_config_ = decrypt_config.Pass();
    116   }
    117 
    118   // If there's no data in this buffer, it represents end of stream.
    119   bool end_of_stream() const {
    120     return data_ == NULL;
    121   }
    122 
    123   // Returns a human-readable string describing |*this|.
    124   std::string AsHumanReadableString();
    125 
    126  protected:
    127   friend class base::RefCountedThreadSafe<DecoderBuffer>;
    128 
    129   // Allocates a buffer of size |size| >= 0 and copies |data| into it.  Buffer
    130   // will be padded and aligned as necessary.  If |data| is NULL then |data_| is
    131   // set to NULL and |buffer_size_| to 0.
    132   DecoderBuffer(const uint8* data, int size,
    133                 const uint8* side_data, int side_data_size);
    134   virtual ~DecoderBuffer();
    135 
    136  private:
    137   base::TimeDelta timestamp_;
    138   base::TimeDelta duration_;
    139 
    140   int size_;
    141   scoped_ptr<uint8, base::ScopedPtrAlignedFree> data_;
    142   int side_data_size_;
    143   scoped_ptr<uint8, base::ScopedPtrAlignedFree> side_data_;
    144   scoped_ptr<DecryptConfig> decrypt_config_;
    145 
    146   // Constructor helper method for memory allocations.
    147   void Initialize();
    148 
    149   DISALLOW_COPY_AND_ASSIGN(DecoderBuffer);
    150 };
    151 
    152 }  // namespace media
    153 
    154 #endif  // MEDIA_BASE_DECODER_BUFFER_H_
    155