1 // Copyright (c) 2011 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 // Note: ported from Chromium commit head: 39a7f93
5 
6 #ifndef MEDIA_BASE_BITSTREAM_BUFFER_H_
7 #define MEDIA_BASE_BITSTREAM_BUFFER_H_
8 
9 #include <stddef.h>
10 #include <stdint.h>
11 
12 #include "base/macros.h"
13 #include "base/memory/shared_memory.h"
14 #include "base/time/time.h"
15 
16 namespace media {
17 
18 // Indicates an invalid or missing timestamp.
19 constexpr base::TimeDelta kNoTimestamp =
20     base::TimeDelta::FromMicroseconds(std::numeric_limits<int64_t>::min());
21 
22 // Class for passing bitstream buffers around.  Does not take ownership of the
23 // data.  This is the media-namespace equivalent of PP_VideoBitstreamBuffer_Dev.
24 class BitstreamBuffer {
25  public:
26   BitstreamBuffer();
27 
28   // Constructs a new BitstreamBuffer. The content of the bitstream is located
29   // at |offset| bytes away from the start of the shared memory and the payload
30   // is |size| bytes. When not provided, the default value for |offset| is 0.
31   // |presentation_timestamp| is when the decoded frame should be displayed.
32   // When not provided, |presentation_timestamp| will be
33   // |media::kNoTimestamp|.
34   BitstreamBuffer(int32_t id,
35                   base::SharedMemoryHandle handle,
36                   size_t size,
37                   off_t offset = 0,
38                   base::TimeDelta presentation_timestamp = kNoTimestamp);
39 
40   BitstreamBuffer(const BitstreamBuffer& other);
41 
42   ~BitstreamBuffer();
43 
44   int32_t id() const { return id_; }
45   base::SharedMemoryHandle handle() const { return handle_; }
46 
47   // The number of bytes of the actual bitstream data. It is the size of the
48   // content instead of the whole shared memory.
49   size_t size() const { return size_; }
50 
51   // The offset to the start of actual bitstream data in the shared memory.
52   off_t offset() const { return offset_; }
53 
54   // The timestamp is only valid if it's not equal to |media::kNoTimestamp|.
55   base::TimeDelta presentation_timestamp() const {
56     return presentation_timestamp_;
57   }
58 
59   void set_handle(const base::SharedMemoryHandle& handle) { handle_ = handle; }
60 
61  private:
62   int32_t id_;
63   base::SharedMemoryHandle handle_;
64   size_t size_;
65   off_t offset_;
66 
67   // This is only set when necessary. For example, AndroidVideoDecodeAccelerator
68   // needs the timestamp because the underlying decoder may require it to
69   // determine the output order.
70   base::TimeDelta presentation_timestamp_;
71 
72   // Allow compiler-generated copy & assign constructors.
73 };
74 
75 }  // namespace media
76 
77 #endif  // MEDIA_BASE_BITSTREAM_BUFFER_H_
78