Chromium Code Reviews| 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_FIFO_H_ | |
| 6 #define CHROMECAST_MEDIA_CMA_IPC_MEDIA_MESSAGE_FIFO_H_ | |
| 7 | |
| 8 #include <list> | |
| 9 | |
| 10 #include "base/atomicops.h" | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/memory/weak_ptr.h" | |
| 18 #include "base/threading/thread_checker.h" | |
| 19 | |
| 20 namespace chromecast { | |
| 21 namespace media { | |
| 22 class MediaMemoryChunk; | |
| 23 class MediaMessage; | |
| 24 class MediaMessageFlag; | |
| 25 | |
| 26 class MediaMessageFifo { | |
| 27 public: | |
| 28 // Creates a media message fifo using |mem| as the underlying serialized | |
| 29 // structure. | |
| 30 // If |init| is true, the underlying fifo structure is initialized. | |
| 31 MediaMessageFifo(scoped_ptr<MediaMemoryChunk> mem, bool init); | |
| 32 ~MediaMessageFifo(); | |
| 33 | |
| 34 // When the consumer and the feeder are living in two different processes, | |
| 35 // we might want to convey some messages between these two processes to notify | |
| 36 // about some fifo activity. | |
| 37 void ObserveReadActivity(const base::Closure& read_event_cb); | |
| 38 void ObserveWriteActivity(const base::Closure& write_event_cb); | |
| 39 | |
| 40 // Reserves a writeable block of memory at the back of the fifo, | |
| 41 // corresponding to the serialized structure of the message. | |
| 42 scoped_ptr<MediaMemoryChunk> ReserveMemory(size_t size); | |
|
xhwang
2014/09/03 17:40:33
What if the memory of |size| cannot be reserved? W
damienv1
2014/09/03 23:59:05
Done.
| |
| 43 | |
| 44 // Pop a message from the queue. | |
| 45 // Returns a null pointer if there is no message left. | |
| 46 scoped_ptr<MediaMessage> Pop(); | |
| 47 | |
| 48 // Flush the fifo. | |
| 49 void Flush(); | |
| 50 | |
| 51 private: | |
| 52 struct Descriptor { | |
| 53 size_t size; | |
| 54 size_t rd_offset; | |
| 55 size_t wr_offset; | |
| 56 | |
| 57 // Ensure the first item has the same alignment as an int64. | |
| 58 int64 first_item; | |
| 59 }; | |
| 60 | |
| 61 // Add some accessors to ensure security on the browser process side. | |
| 62 size_t current_rd_offset() const; | |
| 63 size_t current_wr_offset() const; | |
| 64 size_t internal_rd_offset() const { | |
| 65 DCHECK_LT(internal_rd_offset_, size_); | |
| 66 return internal_rd_offset_; | |
| 67 } | |
| 68 size_t internal_wr_offset() const { | |
| 69 DCHECK_LT(internal_wr_offset_, size_); | |
| 70 return internal_wr_offset_; | |
| 71 } | |
| 72 | |
| 73 // Reserve a block of free memory without doing any check on the available | |
| 74 // space. Invoke this function only when all the checks have been done. | |
| 75 scoped_ptr<MediaMemoryChunk> ReserveMemoryNoCheck(size_t size); | |
| 76 | |
| 77 // Invoked each time there is a memory region in the free space of the fifo | |
| 78 // that has possibly been written. | |
| 79 void OnWrMemoryReleased(); | |
| 80 | |
| 81 // Invoked each time there is a memory region in the allocated space | |
| 82 // of the fifo that has possibly been released. | |
| 83 void OnRdMemoryReleased(); | |
| 84 | |
| 85 void CommitRead(size_t new_rd_offset); | |
| 86 void CommitWrite(size_t new_wr_offset); | |
| 87 void CommitInternalRead(size_t new_rd_offset); | |
| 88 void CommitInternalWrite(size_t new_wr_offset); | |
| 89 | |
| 90 // Having a thread checker does not mean that the feeder and the consumer | |
| 91 // cannot live on different threads. If the consumer and the feeder are | |
| 92 // living on two different threads, then there should be one MediaMessageFifo | |
| 93 // instance for the feeder side and another instance for the consumer side, | |
| 94 // both pointing to the same fifo memory structure. | |
| 95 base::ThreadChecker thread_checker_; | |
|
xhwang
2014/09/03 17:40:33
Given the comments and w/o looking at the impl, I
damienv1
2014/09/03 23:59:06
I updated the comment. Hope it's less confusing.
| |
| 96 | |
| 97 // Callbacks invoked to notify either of some read or write activity on the | |
| 98 // fifo. This is especially useful when the feeder and consumer are living in | |
| 99 // two different processes. | |
| 100 base::Closure read_event_cb_; | |
| 101 base::Closure write_event_cb_; | |
| 102 | |
| 103 // The serialized structure of the fifo. | |
| 104 scoped_ptr<MediaMemoryChunk> mem_; | |
| 105 | |
| 106 // The size in bytes of the fifo is cached locally for security purpose. | |
| 107 // (the renderer process cannot modify the size and make the browser process | |
| 108 // access out of range addresses). | |
| 109 size_t size_; | |
| 110 | |
| 111 // TODO(damienv): This is a work-around since atomicops.h does not define | |
| 112 // an atomic size_t type. | |
| 113 #if SIZE_MAX == UINT32_MAX | |
| 114 typedef base::subtle::Atomic32 AtomicSize; | |
| 115 #elif SIZE_MAX == UINT64_MAX | |
| 116 typedef base::subtle::Atomic64 AtomicSize; | |
| 117 #elif | |
| 118 #error "Unsupported size_t" | |
| 119 #endif | |
| 120 AtomicSize* rd_offset_; | |
| 121 AtomicSize* wr_offset_; | |
| 122 | |
| 123 // Internal read offset: this is where data is actually read from. | |
| 124 // The external offset |rd_offset_| is only used to protect data from being | |
| 125 // overwritten by the feeder. | |
| 126 // At any time, the internal read pointer must be between the external read | |
| 127 // offset and the write offset (circular fifo definition of "between"). | |
| 128 size_t internal_rd_offset_; | |
| 129 size_t internal_wr_offset_; | |
| 130 | |
| 131 // Note: all the memory read/write are followed by a memory fence before | |
| 132 // updating the rd/wr pointer. | |
| 133 void* base_; | |
| 134 | |
| 135 std::list<scoped_refptr<MediaMessageFlag> > rd_flags_; | |
| 136 std::list<scoped_refptr<MediaMessageFlag> > wr_flags_; | |
| 137 | |
| 138 base::WeakPtrFactory<MediaMessageFifo> weak_factory_; | |
| 139 base::WeakPtr<MediaMessageFifo> weak_this_; | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(MediaMessageFifo); | |
| 142 }; | |
| 143 | |
| 144 } // namespace media | |
| 145 } // namespace chromecast | |
| 146 | |
| 147 #endif // CHROMECAST_MEDIA_CMA_IPC_MEDIA_MESSAGE_FIFO_H_ | |
| OLD | NEW |