| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 CHROMECAST_MEDIA_CMA_IPC_MEDIA_MESSAGE_H_ | |
| 6 #define CHROMECAST_MEDIA_CMA_IPC_MEDIA_MESSAGE_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include <memory> | |
| 12 | |
| 13 #include "base/callback.h" | |
| 14 #include "base/macros.h" | |
| 15 | |
| 16 namespace chromecast { | |
| 17 namespace media { | |
| 18 class MediaMemoryChunk; | |
| 19 | |
| 20 // MediaMessage - | |
| 21 // Represents a media message, including: | |
| 22 // - a message header that gives for example the message size or its type, | |
| 23 // - the content of the message, | |
| 24 // - and some possible padding if the content does not occupy the whole | |
| 25 // reserved space. | |
| 26 // | |
| 27 class MediaMessage { | |
| 28 public: | |
| 29 // Memory allocator: given a number of bytes to allocate, | |
| 30 // return the pointer to the allocated block if successful | |
| 31 // or NULL if allocation failed. | |
| 32 typedef base::Callback<std::unique_ptr<MediaMemoryChunk>(size_t)> | |
| 33 MemoryAllocatorCB; | |
| 34 | |
| 35 // Creates a message with no associated memory for its content, i.e. | |
| 36 // each write on this message is a dummy operation. | |
| 37 // This type of message can be useful to calculate first the size of the | |
| 38 // message, before allocating the real message. | |
| 39 static std::unique_ptr<MediaMessage> CreateDummyMessage(uint32_t type); | |
| 40 | |
| 41 // Creates a message with a capacity of at least |msg_content_capacity| | |
| 42 // bytes. The actual content size can be smaller than its capacity. | |
| 43 // The message can be populated with some Write functions. | |
| 44 static std::unique_ptr<MediaMessage> CreateMessage( | |
| 45 uint32_t type, | |
| 46 const MemoryAllocatorCB& memory_allocator, | |
| 47 size_t msg_content_capacity); | |
| 48 | |
| 49 // Creates a message of type |type| whose serialized structure is stored | |
| 50 // in |mem|. | |
| 51 static std::unique_ptr<MediaMessage> CreateMessage( | |
| 52 uint32_t type, | |
| 53 std::unique_ptr<MediaMemoryChunk> mem); | |
| 54 | |
| 55 // Creates a message from a memory area which already contains | |
| 56 // the serialized structure of the message. | |
| 57 // Only Read functions can be invoked on this type of message. | |
| 58 static std::unique_ptr<MediaMessage> MapMessage( | |
| 59 std::unique_ptr<MediaMemoryChunk> mem); | |
| 60 | |
| 61 // Return the minimum size of a message. | |
| 62 static size_t minimum_msg_size() { | |
| 63 return offsetof(SerializedMsg, content); | |
| 64 } | |
| 65 | |
| 66 ~MediaMessage(); | |
| 67 | |
| 68 // Indicate whether the underlying serialized structure of the message is | |
| 69 // available. | |
| 70 // Note: the serialized structure might be unavailable in case of a dummy | |
| 71 // message or if the underlying memory has been invalidated. | |
| 72 bool IsSerializedMsgAvailable() const; | |
| 73 | |
| 74 // Return the message and the total size of the message | |
| 75 // incuding the header, the content and the possible padding. | |
| 76 const void* msg() const { return msg_read_only_; } | |
| 77 size_t size() const { return cached_msg_.header.size; } | |
| 78 | |
| 79 // Return the size of the message without padding. | |
| 80 size_t actual_size() const { | |
| 81 return minimum_msg_size() + cached_msg_.header.content_size; | |
| 82 } | |
| 83 | |
| 84 // Return the size of the content of the message. | |
| 85 size_t content_size() const { return cached_msg_.header.content_size; } | |
| 86 | |
| 87 // Return the type of the message. | |
| 88 uint32_t type() const { return cached_msg_.header.type; } | |
| 89 | |
| 90 // Append a POD to the message. | |
| 91 // Return true if the POD has been succesfully written. | |
| 92 template<typename T> bool WritePod(T* const& pod); | |
| 93 template<typename T> bool WritePod(const T& pod) { | |
| 94 return WriteBuffer(&pod, sizeof(T)); | |
| 95 } | |
| 96 | |
| 97 // Append a raw buffer to the message. | |
| 98 bool WriteBuffer(const void* src, size_t size); | |
| 99 | |
| 100 // Read a POD from the message. | |
| 101 template<typename T> bool ReadPod(T* pod) { | |
| 102 return ReadBuffer(pod, sizeof(T)); | |
| 103 } | |
| 104 | |
| 105 // Read |size| bytes from the message from the last read position | |
| 106 // and write it to |dst|. | |
| 107 bool ReadBuffer(void* dst, size_t size); | |
| 108 | |
| 109 // Return a pointer to a buffer of size |size|. | |
| 110 // Return NULL if not successful. | |
| 111 const void* GetBuffer(size_t size); | |
| 112 void* GetWritableBuffer(size_t size); | |
| 113 | |
| 114 private: | |
| 115 MediaMessage(uint32_t type, size_t msg_size); | |
| 116 MediaMessage(uint32_t type, std::unique_ptr<MediaMemoryChunk> memory); | |
| 117 MediaMessage(std::unique_ptr<MediaMemoryChunk> memory); | |
| 118 | |
| 119 struct Header { | |
| 120 // Total size of the message (including both header & content). | |
| 121 uint32_t size; | |
| 122 // Indicate the message type. | |
| 123 uint32_t type; | |
| 124 // Actual size of the content in the message. | |
| 125 uint32_t content_size; | |
| 126 }; | |
| 127 | |
| 128 struct SerializedMsg { | |
| 129 // Message header. | |
| 130 Header header; | |
| 131 | |
| 132 // Start of the content of the message. | |
| 133 // Use uint8_t since no special alignment is needed. | |
| 134 uint8_t content; | |
| 135 }; | |
| 136 | |
| 137 // Indicate whether the message is a dummy message, i.e. a message without | |
| 138 // a complete underlying serialized structure: only the message header is | |
| 139 // available. | |
| 140 bool is_dummy_msg_; | |
| 141 | |
| 142 // |cached_msg_| is used for 2 purposes: | |
| 143 // - to create a dummy message | |
| 144 // - for security purpose: cache the msg header to avoid browser security | |
| 145 // issues. | |
| 146 SerializedMsg cached_msg_; | |
| 147 Header* const cached_header_; | |
| 148 | |
| 149 SerializedMsg* msg_; | |
| 150 SerializedMsg* msg_read_only_; | |
| 151 | |
| 152 // Memory allocated to store the underlying serialized structure into memory. | |
| 153 // Note: a dummy message has no underlying serialized structure: | |
| 154 // |mem_| is a null pointer in that case. | |
| 155 std::unique_ptr<MediaMemoryChunk> mem_; | |
| 156 | |
| 157 // Read iterator into the message. | |
| 158 size_t rd_offset_; | |
| 159 | |
| 160 DISALLOW_COPY_AND_ASSIGN(MediaMessage); | |
| 161 }; | |
| 162 | |
| 163 } // namespace media | |
| 164 } // namespace chromecast | |
| 165 | |
| 166 #endif | |
| OLD | NEW |