]> git.sesse.net Git - casparcg/blob - server/consumers/Audio/AudioConsumer.cpp
1.8.1:
[casparcg] / server / consumers / Audio / AudioConsumer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20  \r
21 #include "..\..\stdafx.h"\r
22 \r
23 #include "AudioConsumer.h"\r
24 #include "..\..\frame\FramePlaybackControl.h"\r
25 #include "..\..\frame\SystemFrameManager.h"\r
26 #include "..\..\frame\Frame.h"\r
27 #include "..\..\frame\FramePlaybackStrategy.h"\r
28 #include "..\..\utils\image\Image.hpp"\r
29 \r
30 namespace caspar {\r
31 namespace audio {\r
32 \r
33 \r
34 struct AudioConsumer::Implementation\r
35 {\r
36         struct AudioPlaybackStrategy : public IFramePlaybackStrategy\r
37         {\r
38                 explicit AudioPlaybackStrategy(Implementation* pConsumerImpl) : pConsumerImpl_(pConsumerImpl)\r
39                 {\r
40                         lastTime_ = timeGetTime();\r
41                 }\r
42 \r
43                 FrameManagerPtr GetFrameManager()\r
44                 {\r
45                         return pConsumerImpl_->pFrameManager_;\r
46                 }\r
47                 FramePtr GetReservedFrame()\r
48                 {\r
49                         return pConsumerImpl_->pFrameManager_->CreateFrame();\r
50                 }\r
51 \r
52                 void DisplayFrame(Frame* pFrame)\r
53                 {\r
54                         DWORD timediff = timeGetTime() - lastTime_;\r
55                         if(timediff < 30) {\r
56                                 Sleep(40 - timediff);\r
57                                 lastTime_ += 40;\r
58                         }\r
59                         else\r
60                                 lastTime_ = timeGetTime();\r
61 \r
62                         if(pFrame == NULL || pFrame->ID() == lastFrameID_)\r
63                                 return;\r
64 \r
65                         lastFrameID_ = pFrame->ID();\r
66                 }\r
67 \r
68                 Implementation* pConsumerImpl_;\r
69                 DWORD lastTime_;        \r
70                 utils::ID lastFrameID_;\r
71         };\r
72 \r
73         explicit Implementation(const FrameFormatDescription& fmtDesc) : fmtDesc_(fmtDesc)\r
74         {       \r
75                 SetupDevice();\r
76         }\r
77 \r
78         ~Implementation()\r
79         {\r
80                 ReleaseDevice();\r
81         }\r
82 \r
83         bool SetupDevice()\r
84         {\r
85                 pFrameManager_.reset(new SystemFrameManager(fmtDesc_));\r
86                 pPlaybackControl_.reset(new FramePlaybackControl(FramePlaybackStrategyPtr(new AudioPlaybackStrategy(this))));\r
87 \r
88                 pPlaybackControl_->Start();\r
89                 return true;\r
90         }\r
91 \r
92         bool ReleaseDevice()\r
93         {\r
94                 pPlaybackControl_->Stop();\r
95                 return true;\r
96         }\r
97 \r
98         FramePlaybackControlPtr pPlaybackControl_;\r
99         SystemFrameManagerPtr pFrameManager_;\r
100 \r
101         FrameFormatDescription fmtDesc_;\r
102 };\r
103 \r
104 AudioConsumer::AudioConsumer(const FrameFormatDescription& fmtDesc) : pImpl_(new Implementation(fmtDesc))\r
105 {}\r
106 \r
107 AudioConsumer::~AudioConsumer()\r
108 {}\r
109 \r
110 IPlaybackControl* AudioConsumer::GetPlaybackControl() const\r
111 {\r
112         return pImpl_->pPlaybackControl_.get();\r
113 }\r
114 \r
115 bool AudioConsumer::SetupDevice(unsigned int deviceIndex)\r
116 {\r
117         return pImpl_->SetupDevice();\r
118 }\r
119 \r
120 bool AudioConsumer::ReleaseDevice()\r
121 {\r
122         return pImpl_->ReleaseDevice();\r
123 }\r
124 \r
125 void AudioConsumer::EnableVideoOutput(){}\r
126 void AudioConsumer::DisableVideoOutput(){}\r
127 \r
128 const FrameFormatDescription& AudioConsumer::GetFrameFormatDescription() const {\r
129         return pImpl_->fmtDesc_;\r
130 }\r
131 const TCHAR* AudioConsumer::GetFormatDescription() const {\r
132         return pImpl_->fmtDesc_.name;\r
133 }\r
134 \r
135 \r
136 }       //namespace audio\r
137 }       //namespace caspar