kodi
DVDMessageQueue.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "DVDMessage.h"
12 #include "threads/CriticalSection.h"
13 #include "threads/Event.h"
14 
15 #include <algorithm>
16 #include <atomic>
17 #include <list>
18 #include <string>
19 
21 {
22  DVDMessageListItem(std::shared_ptr<CDVDMsg> msg, int prio) : message(std::move(msg))
23  {
24  priority = prio;
25  }
26  DVDMessageListItem() { priority = 0; }
27  DVDMessageListItem(const DVDMessageListItem&) = delete;
28  ~DVDMessageListItem() = default;
29 
30  DVDMessageListItem& operator=(const DVDMessageListItem&) = delete;
31 
32  std::shared_ptr<CDVDMsg> message;
33  int priority;
34 };
35 
36 enum MsgQueueReturnCode
37 {
38  MSGQ_OK = 1,
39  MSGQ_TIMEOUT = 0,
40  MSGQ_ABORT = -1, // negative for legacy, not an error actually
41  MSGQ_NOT_INITIALIZED = -2,
42  MSGQ_INVALID_MSG = -3,
43  MSGQ_OUT_OF_MEMORY = -4
44 };
45 
46 #define MSGQ_IS_ERROR(c) (c < 0)
47 
49 {
50 public:
51  explicit CDVDMessageQueue(const std::string &owner);
52  virtual ~CDVDMessageQueue();
53 
54  void Init();
55  void Flush(CDVDMsg::Message message = CDVDMsg::DEMUXER_PACKET);
56  void Abort();
57  void End();
58 
59  MsgQueueReturnCode Put(const std::shared_ptr<CDVDMsg>& pMsg, int priority = 0);
60  MsgQueueReturnCode PutBack(const std::shared_ptr<CDVDMsg>& pMsg, int priority = 0);
61 
67  MsgQueueReturnCode Get(std::shared_ptr<CDVDMsg>& pMsg,
68  std::chrono::milliseconds timeout,
69  int& priority);
70  MsgQueueReturnCode Get(std::shared_ptr<CDVDMsg>& pMsg, std::chrono::milliseconds timeout)
71  {
72  int priority = 0;
73  return Get(pMsg, timeout, priority);
74  }
75 
76  int GetDataSize() const { return m_iDataSize; }
77  int GetTimeSize() const;
78  unsigned GetPacketCount(CDVDMsg::Message type);
79  bool ReceivedAbortRequest() { return m_bAbortRequest; }
80  void WaitUntilEmpty();
81 
82  // non messagequeue related functions
83  bool IsFull() const { return GetLevel() == 100; }
84  int GetLevel() const;
85 
86  void SetMaxDataSize(int iMaxDataSize) { m_iMaxDataSize = iMaxDataSize; }
87  void SetMaxTimeSize(double sec) { m_TimeSize = 1.0 / std::max(1.0, sec); }
88  int GetMaxDataSize() const { return m_iMaxDataSize; }
89  double GetMaxTimeSize() const { return m_TimeSize; }
90  bool IsInited() const { return m_bInitialized; }
91  bool IsDataBased() const;
92 
93 private:
94  MsgQueueReturnCode Put(const std::shared_ptr<CDVDMsg>& pMsg, int priority, bool front);
95  void UpdateTimeFront();
96  void UpdateTimeBack();
97 
98  CEvent m_hEvent;
99  mutable CCriticalSection m_section;
100 
101  std::atomic<bool> m_bAbortRequest = false;
102  bool m_bInitialized;
103  bool m_drain = false;
104 
105  int m_iDataSize;
106  double m_TimeFront;
107  double m_TimeBack;
108  double m_TimeSize;
109 
110  int m_iMaxDataSize;
111  std::string m_owner;
112 
113  std::list<DVDMessageListItem> m_messages;
114  std::list<DVDMessageListItem> m_prioMessages;
115 };
116 
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
Definition: DVDMessageQueue.h:48
Definition: DVDMessageQueue.h:20