Shaka Packager SDK
threaded_io_file.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_THREADED_IO_FILE_H_
8 #define PACKAGER_FILE_THREADED_IO_FILE_H_
9 
10 #include <atomic>
11 #include <memory>
12 #include "packager/base/synchronization/waitable_event.h"
13 #include "packager/file/file.h"
14 #include "packager/file/file_closer.h"
15 #include "packager/file/io_cache.h"
16 
17 namespace shaka {
18 
20 class ThreadedIoFile : public File {
21  public:
22  enum Mode { kInputMode, kOutputMode };
23 
24  ThreadedIoFile(std::unique_ptr<File, FileCloser> internal_file,
25  Mode mode,
26  uint64_t io_cache_size,
27  uint64_t io_block_size);
28 
31  bool Close() override;
32  int64_t Read(void* buffer, uint64_t length) override;
33  int64_t Write(const void* buffer, uint64_t length) override;
34  int64_t Size() override;
35  bool Flush() override;
36  bool Seek(uint64_t position) override;
37  bool Tell(uint64_t* position) override;
39 
40  protected:
41  ~ThreadedIoFile() override;
42 
43  bool Open() override;
44 
45  private:
46  // Internal task handler implementation. Will dispatch to either
47  // |RunInInputMode| or |RunInOutputMode| depending on |mode_|.
48  void TaskHandler();
49  void RunInInputMode();
50  void RunInOutputMode();
51 
52  std::unique_ptr<File, FileCloser> internal_file_;
53  const Mode mode_;
54  IoCache cache_;
55  std::vector<uint8_t> io_buffer_;
56  uint64_t position_;
57  uint64_t size_;
58  std::atomic<bool> eof_;
59  bool flushing_;
60  base::WaitableEvent flush_complete_event_;
61  std::atomic<int32_t> internal_file_error_;
62  // Signalled when thread task exits.
63  base::WaitableEvent task_exit_event_;
64 
65  DISALLOW_COPY_AND_ASSIGN(ThreadedIoFile);
66 };
67 
68 } // namespace shaka
69 
70 #endif // PACKAGER_FILE_THREADED_IO_FILE_H
Define an abstract file interface.
Definition: file.h:27
Declaration of class which implements a thread-safe circular buffer.
Definition: io_cache.h:19
Declaration of class which implements a thread-safe circular buffer.
bool Open() override
Internal open. Should not be used directly.
bool Tell(uint64_t *position) override
bool Seek(uint64_t position) override
int64_t Write(const void *buffer, uint64_t length) override
int64_t Size() override
int64_t Read(void *buffer, uint64_t length) override
All the methods that are virtual are virtual for mocking.