shared_memory.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_DESKTOP_CAPTURE_SHARED_MEMORY_H_
  11. #define MODULES_DESKTOP_CAPTURE_SHARED_MEMORY_H_
  12. #include <stddef.h>
  13. #if defined(WEBRTC_WIN)
  14. #include <windows.h>
  15. #endif
  16. #include <memory>
  17. #include "rtc_base/constructor_magic.h"
  18. #include "rtc_base/system/rtc_export.h"
  19. namespace webrtc {
  20. // SharedMemory is a base class for shared memory. It stores all required
  21. // parameters of the buffer, but doesn't have any logic to allocate or destroy
  22. // the actual buffer. DesktopCapturer consumers that need to use shared memory
  23. // for video frames must extend this class with creation and destruction logic
  24. // specific for the target platform and then call
  25. // DesktopCapturer::SetSharedMemoryFactory().
  26. class RTC_EXPORT SharedMemory {
  27. public:
  28. #if defined(WEBRTC_WIN)
  29. typedef HANDLE Handle;
  30. static const Handle kInvalidHandle;
  31. #else
  32. typedef int Handle;
  33. static const Handle kInvalidHandle;
  34. #endif
  35. void* data() const { return data_; }
  36. size_t size() const { return size_; }
  37. // Platform-specific handle of the buffer.
  38. Handle handle() const { return handle_; }
  39. // Integer identifier that can be used used by consumers of DesktopCapturer
  40. // interface to identify shared memory buffers it created.
  41. int id() const { return id_; }
  42. virtual ~SharedMemory() {}
  43. protected:
  44. SharedMemory(void* data, size_t size, Handle handle, int id);
  45. void* const data_;
  46. const size_t size_;
  47. const Handle handle_;
  48. const int id_;
  49. private:
  50. RTC_DISALLOW_COPY_AND_ASSIGN(SharedMemory);
  51. };
  52. // Interface used to create SharedMemory instances.
  53. class SharedMemoryFactory {
  54. public:
  55. SharedMemoryFactory() {}
  56. virtual ~SharedMemoryFactory() {}
  57. virtual std::unique_ptr<SharedMemory> CreateSharedMemory(size_t size) = 0;
  58. private:
  59. RTC_DISALLOW_COPY_AND_ASSIGN(SharedMemoryFactory);
  60. };
  61. } // namespace webrtc
  62. #endif // MODULES_DESKTOP_CAPTURE_SHARED_MEMORY_H_