1 : /*
2 : * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : /*
8 : * Simple implementation of IMultimedia interface
9 : */
10 :
11 : #include <assert.h>
12 : #include <string.h>
13 :
14 : #include <functional>
15 : #include <queue>
16 :
17 : #include "native_client/src/shared/platform/nacl_log.h"
18 : #include "native_client/src/shared/platform/nacl_semaphore.h"
19 : #include "native_client/src/shared/platform/nacl_sync.h"
20 : #include "native_client/src/shared/platform/nacl_sync_checked.h"
21 : #include "native_client/src/trusted/sel_universal/primitives.h"
22 :
23 : class NaClCommandLoop;
24 :
25 : // Standard helper class to tie mutex lock/unlock to a scope.
26 : class ScopedMutexLock {
27 : public:
28 0 : explicit ScopedMutexLock(NaClMutex* m)
29 0 : : mutex_(m) {
30 : // work around compiler warning, cast to void does not work!
31 : // More instances of this hack below
32 0 : NaClXMutexLock(mutex_);
33 0 : }
34 :
35 0 : ~ScopedMutexLock() {
36 0 : NaClXMutexUnlock(mutex_);
37 0 : }
38 :
39 : private:
40 : NaClMutex* mutex_;
41 : };
42 :
43 :
44 : class EmuPrimitivesSimple : public IMultimedia {
45 : public:
46 0 : EmuPrimitivesSimple(int width, int heigth, const char* title)
47 0 : : video_width_(width), video_height_(heigth) {
48 : UNREFERENCED_PARAMETER(title);
49 0 : NaClLog(2, "PrimitivesSimpleL::Constructor\n");
50 0 : NaClXMutexCtor(&mutex_);
51 0 : NaClSemCtor(&sem_, 0);
52 0 : }
53 :
54 0 : virtual ~EmuPrimitivesSimple() {
55 0 : }
56 :
57 0 : virtual int VideoWidth() {
58 0 : return video_width_;
59 : }
60 :
61 0 : virtual int VideoHeight() {
62 0 : return video_height_;
63 : }
64 :
65 0 : virtual int VideoBufferSize() {
66 0 : NaClLog(LOG_FATAL, "VideoBufferSize() not supported\n");
67 0 : return -1;
68 : }
69 :
70 0 : virtual void VideoUpdate(const void* data) {
71 : UNREFERENCED_PARAMETER(data);
72 0 : NaClLog(LOG_FATAL, "VideoUpdate() not supported\n");
73 0 : }
74 :
75 0 : virtual void PushUserEvent(UserEvent* event) {
76 0 : ScopedMutexLock lock(&mutex_);
77 0 : queue_.push(event);
78 0 : NaClSemPost(&sem_);
79 0 : }
80 :
81 0 : virtual void PushDelayedUserEvent(int delay, UserEvent* event) {
82 : // for now ignore the delay
83 : UNREFERENCED_PARAMETER(delay);
84 0 : PushUserEvent(event);
85 0 : }
86 :
87 0 : virtual UserEvent* EventPoll() {
88 : UserEvent* event;
89 0 : ScopedMutexLock lock(&mutex_);
90 0 : if (queue_.size() > 0) {
91 : // This should always go through without delay
92 0 : NaClSemWait(&sem_);
93 0 : event = queue_.front();
94 0 : queue_.pop();
95 0 : return event;
96 : } else {
97 0 : return 0;
98 0 : }
99 : }
100 :
101 0 : virtual UserEvent* EventGet() {
102 0 : NaClSemWait(&sem_);
103 0 : ScopedMutexLock lock(&mutex_);
104 0 : assert(queue_.size() > 0);
105 : // copy
106 0 : UserEvent* event = queue_.front();
107 0 : queue_.pop();
108 0 : return event;
109 : }
110 :
111 : virtual void AudioInit16Bit(int frequency,
112 : int channels,
113 : int frame_size,
114 : AUDIO_CALLBACK cb,
115 0 : void* cb_data) {
116 : UNREFERENCED_PARAMETER(frequency);
117 : UNREFERENCED_PARAMETER(channels);
118 : UNREFERENCED_PARAMETER(frame_size);
119 : UNREFERENCED_PARAMETER(cb);
120 : UNREFERENCED_PARAMETER(cb_data);
121 0 : NaClLog(LOG_FATAL, "AudioInit16Bit() not supported\n");
122 0 : }
123 :
124 0 : virtual void AudioStart() {
125 0 : NaClLog(LOG_FATAL, "AudioStart() not supported\n");
126 0 : }
127 :
128 0 : virtual void AudioStop() {
129 0 : NaClLog(LOG_FATAL, "AudioStop() not supported\n");
130 0 : }
131 :
132 : private:
133 : int video_width_;
134 : int video_height_;
135 : NaClMutex mutex_;
136 : NaClSemaphore sem_;
137 : std::queue<UserEvent*> queue_;
138 : };
139 :
140 : // Factor, so we can hide class MultimediaSDL from the outside world
141 0 : IMultimedia* MakeEmuPrimitives(int width, int heigth, const char* title) {
142 0 : return new EmuPrimitivesSimple(width, heigth, title);
143 : }
144 :
145 :
146 : void InvokeInputEventCallback(NaClCommandLoop* ncl,
147 : UserEvent* event,
148 : int instances,
149 0 : int event_resource) {
150 : UNREFERENCED_PARAMETER(ncl);
151 : UNREFERENCED_PARAMETER(event);
152 : UNREFERENCED_PARAMETER(instances);
153 : UNREFERENCED_PARAMETER(event_resource);
154 0 : NaClLog(LOG_FATAL, "InputEvents not supported\n");
155 0 : }
|