DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
io_cache.h
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef PACKAGER_FILE_IO_CACHE_H_
8 #define PACKAGER_FILE_IO_CACHE_H_
9 
10 #include <stdint.h>
11 #include <vector>
12 #include "packager/base/macros.h"
13 #include "packager/base/synchronization/lock.h"
14 #include "packager/base/synchronization/waitable_event.h"
15 
16 namespace edash_packager {
17 namespace media {
18 
20 class IoCache {
21  public:
22  explicit IoCache(uint64_t cache_size);
23  ~IoCache();
24 
31  uint64_t Read(void* buffer, uint64_t size);
32 
40  uint64_t Write(const void* buffer, uint64_t size);
41 
43  void Clear();
44 
47  void Close();
48 
51  uint64_t BytesCached();
52 
55  uint64_t BytesFree();
56 
59 
60  private:
61  uint64_t BytesCachedInternal();
62  uint64_t BytesFreeInternal();
63 
64  const uint64_t cache_size_;
65  base::Lock lock_;
66  base::WaitableEvent read_event_;
67  base::WaitableEvent write_event_;
68  std::vector<uint8_t> circular_buffer_;
69  const uint8_t* end_ptr_;
70  uint8_t* r_ptr_;
71  uint8_t* w_ptr_;
72  bool closed_;
73 
74  DISALLOW_COPY_AND_ASSIGN(IoCache);
75 };
76 
77 } // namespace media
78 } // namespace edash_packager
79 
80 #endif // PACKAGER_FILE_IO_CACHE_H
Declaration of class which implements a thread-safe circular buffer.
Definition: io_cache.h:20
void WaitUntilEmptyOrClosed()
Waits until the cache is empty or has been closed.
Definition: io_cache.cc:136
uint64_t Write(const void *buffer, uint64_t size)
Definition: io_cache.cc:66
void Clear()
Empties the cache.
Definition: io_cache.cc:102
uint64_t Read(void *buffer, uint64_t size)
Definition: io_cache.cc:38