1

I've been looking for info for a while now but haven't been able to find anything useful for us. We're building kind of a SmartCar complex system, which controls more or less everything in a car.. Its a dev project, nothing "reinventing the wheel" here, just the point of doing it completely from the scratch.

Since it's going to have quite a heavy load, we built a PC based on i5 proc with 8GB of ram, running Windows Server 2012 Datacenter on Intel's S335 SSD hard drive.

So far so good, the main controllers are running as services and all respond well for operations such as lock/unlock, side mirrors movement, windows movement, complete climatronic control, ignition, lights etc... However, we kinda can't get to play a sound from it...

The reason we use all as services is because it has no monitor at all, but rather communicate via WiFi and 3G with Android tablet and phone, and Windows tablet as well. We've tried doing it via remote desktop connection, but it's waste of resources on both sides, so we rather use Tcp/ip client/server version. Also, since it needs to hibernate/shutdown in order not to waste energy resources, instant power on is vital, and without a need to log in it all works within a few seconds, which is perfect.

So, my question here is, how could I make a music player to run as a service and play audio files, which we select via our remore app? The remote part is not an issue here, but rather the player part. I've tried several delphi demo music player example codes transformed into services, but nome of them played the sound. I guess it has something to do with services not being normally allowed to interact directly with user interface, but I know it's possible, just dont have a clue on how.

Also, I guess its not possible, but still worth asking: if we'd want to extend it to use as a video player as well, could this be done without any user logged in? Such as having vga/dvi video monitor connected, but to only show a video when it's selected to be played? (the monitor would be as extended monitor in this case, so that no login sceen would ever be shown).

I make all of the services in Delphi. Any info would be appreciated. Thanks.

That Marc
  • 1,134
  • 3
  • 19
  • 42
  • I don't think that audio or video will work from session 0 – David Heffernan Feb 17 '14 at 12:09
  • @Marc, have you tried the [`Windows Core Audio API`](http://msdn.microsoft.com/en-us/library/windows/desktop/dd370802(v=vs.85).aspx) ? – TLama Feb 17 '14 at 12:49
  • @TLama What about session 0? – David Heffernan Feb 17 '14 at 13:23
  • @David, I don't know. I haven't tried. Some [`people say`](http://stackoverflow.com/a/2198729/960757). – TLama Feb 17 '14 at 13:24
  • @TLama Maybe. Can't see video working. Or perhaps I'm wrong there too. – David Heffernan Feb 17 '14 at 13:39
  • Is this question about `[delphi]`? – Free Consulting Feb 17 '14 at 13:42
  • This question is not a Delphi question – David Heffernan Feb 17 '14 at 13:58
  • @David, I think you are correct, Session 0 will not allow audio. And I don't know if 'allow the service to interact with the desktop' will help. – Hugh Jones Feb 17 '14 at 14:52
  • Yet another downvote... – That Marc Feb 17 '14 at 16:50
  • Yet another downvote... Anyways, @David it is possible with some NAudio from C#, as far as I was able to dig further, so this should work one way or another. Also, if I can communicate with my service app, that controls usb input/output board ysing HID driver, I really can't see such perfect limitations to use audii drivers as well. :/ – That Marc Feb 17 '14 at 16:53
  • @TLama looks like you're right, Windows Audio Core API seems to be abke to do the trick. http://stackoverflow.com/questions/2143439/play-wave-file-from-a-windows-service-c I just have to figure out how to use it. Do you think video have some possible similar abillity too? – That Marc Feb 17 '14 at 16:58
  • Out of curiosity, in which country would such a home grown car be road legal? – David Heffernan Feb 17 '14 at 18:27
  • 1
    It's in Slovenia, if you wonder where we idiots live... ;) – That Marc Feb 17 '14 at 18:33
  • AFAIK there shouldn't be any problem since its not affecting any drive experiences, and its not replacing any of stock controls, but rather just duplicate it. So its basically not seen directly. Also, its for a project, and not for customizing and being swagers all the way... ;) – That Marc Feb 17 '14 at 18:35
  • 3
    I would approach this sort of problem with external devices. Have an external sound device controlled through USB/RS232/Lan or something like that and the same with the video. I would definitely not want to be hacking around on something that will be controlling parts of the car. One mistake and you crash windows and everything stops working. – Graymatter Feb 17 '14 at 19:43
  • @JustMarc If I were you, I would stick with Graymatter's solution. Although it uses a more hardware oriented approach, modularizing the onboard system makes it more errorproof. I mean you wouldn't like the ignition controller to brake because of a broken .mp3 download, right? – mg30rg Feb 18 '14 at 08:38
  • I would expect audio API work well in session 0. Media players which involve visual presentation might be in trouble though. @DavidHeffernan: the car would be okay for Ukraine, illegal formally but noone cares. – Roman R. Feb 18 '14 at 11:44
  • @Graymatter that is the way I first thought about, but still wanted to know if it is possible in one system anyway. And I am more interested in audio, which will be in use all the time, video was just a question nearby. – That Marc Feb 18 '14 at 11:53
  • @Graymatter as for the ignition and so... I wasn't about to trust windows this kind of executions anyway, so no troubles. The IO board is 8dig+4pwm channel board, which uses sequences and pwm signals to trigger actions via AtMegas micros... So in case of a crash, everything stays the same, nothing happens, since theres almost 0 chance that the board would make an exact sequence that triggers any action. +, again, all controls are cloned, not replacements to original, so I still can ignite and turn off the car with a key, etc... :) Only true dedicated sys would be trusted to do all explicitly. – That Marc Feb 18 '14 at 12:00
  • @mg30rg I might go for that solution for audio too, if I'll be able to connect to any outer audio interface through my own service app. Im not an expert, so I don't know quite how to do that... As for using only interfaces to control outer player, that would be worthless. The main reason we're even moving to win player is having the whole list of songs and making selections, playlists etc very easily. From the same app where the car controls are... :/ – That Marc Feb 18 '14 at 12:05
  • @David +, if windows is able to play its own audio output via soundcard from service (ie login screen have an audio played before you login...), then it is posdible indeed, even though the session 0 should prevent it. The API must have a workaround obviously, don't you agree? – That Marc Feb 18 '14 at 12:08
  • 1
    @JustMarc It's wasn't the ignition that worried me. Locks and Windows can be just as devastating. Car is in an accident and you can't open the door to get out because Windows has crashed. I understand your solution to the problem though. Still not sure I trust Windows with my car :) – Graymatter Feb 18 '14 at 19:53
  • @Graymatter I only give an ignition as example, all of the commands are controlled that way. So, basically, as already stated, its almost impossible to randomly trigger any action. And, again, you could simply unlock (for example of an accident, as u mentioned) from the driver's door lock switch (although, most of the cars are designed so that the inner doorlock opens the doors eveb though they were electronically locked, afaik. Unless the kids protection is on, in which case they also wouldn't open any way ;) ). And this sequence coding was made exactly because I dont trust windows neithet ;) – That Marc Feb 18 '14 at 22:57

3 Answers3

3

Audio APIs work well from a service (just to make sure because there is some confusing around: by service I mean user mode service process, which has nothing to do with kernel mode driver), just the same way they do it on interactive session. Specifically, you can open default endpoint device for audio rendering (IMMDeviceEnumerator::GetDefaultAudioEndpoint, IAudioClient::Initialize) and send data for playback (IAudioRenderClient::GetBuffer). I don't expect other APIs like DirectShow to be limited in part of audio output from service.

There is a different story with video devices: standard video rendering eventually takes place on desktop, where you service does not have access to. To have desktop video on system running without explicit user logon, you might prefer to design a regular app and set Windows to auto-logon and auto-start your application. If you however prefer to use a sort of external video output (HDMI video output on Intensity Pro board - just an example), this should be working from service - no desktop needed for this video output.

To control a service application you are to implement something to accept external commands. This can be a web server or otherwise something listening on a socket and responding to incoming data. If you want to control the service from another process, such as interactive, you can simply put a COM interface on Running Object Table from the service and then grab it from the application to call its methods.

Update: Here is some sample code proving audio operation from service. It works under assumption that default mixing format is 32-bit float audio and emits five seconds of high frequency signal to default audio output device.

  • PlayService.exe /regserver - registers to run as app
  • cscript PlayService_Play.js - starts code within .exe and plays sound
  • PlayService.exe /service - re-registers to run as service
  • cscript PlayService_Play.js - plays the same sound from service
  • PlayService.exe /unregserver - unregisters and cleans things up

This code block shows API used (C++ code).

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Thanks for the hints. I still need to check that audio api and implementation of it... As for the video: So you're saying there is a way of playing back the video to the external monitor without user being logged in, if I don't need any desktop interaction (which I don't, just the video output...) through the video card? But, does it need to be external card, such as the one (Intensity pro) that you mentioned, or can it be done via the second output on internal graphics card (internal but not integrated one, I mean...) as well? – That Marc Feb 18 '14 at 23:11
  • Oh, and as for the controls - that is already solved and is working, via tcp/ip server/client connection between the control app and the service itself... :) And I kinda hope I can run all from the main process (service) and avoid service-to-service interactive communication if possible... – That Marc Feb 18 '14 at 23:13
  • Yes, video card output is a good chance for service operation eliminating need in additional hardware. – Roman R. Feb 19 '14 at 06:25
  • The link to the sample code seems like dead... :/ I've been trying for hours now. Do you happen to have it somewhere else too...? :) – That Marc Feb 20 '14 at 20:27
  • Nevermind, it seems to work now. Got it all downloaded, but, to hell, can't figure out what's going on in the code... My C++ knowledge is pure zero... -.- – That Marc Feb 20 '14 at 21:29
  • It's not about how to make a service. It's almost standard service template + [25 lines of audio output code](http://www.alax.info/trac/public/browser/trunk/Utilities/Miscellaneous/WasapiAudioOutput/PlayService/Play.h#L37) which you can easily port to Delphi. The point is that it proves that audio output is OK for service process. – Roman R. Feb 20 '14 at 21:41
  • I'll give it a try to port that to delphi... I hope I'll make it through.... Thanks so far! – That Marc Feb 20 '14 at 22:28
3

Windows Embedded Automotive 7 provides Audio and Video support and can be deployed on x86 or ARM

haz
  • 116
  • 3
1

I know a solution to both your problems, but it is somewhat nasty. (At this point I would like to point out, that for starters, I would not do what I'm suggesting here, only no more viable solutions were provided. No "This is a terrible suggestion" comments required - I already know that.)


About my solution: If you wish to use audio- or video devices to work outside the user interface, it can be realised by custom-built kernel mode drivers. Kernel mode drivers can communicate directly with windows services and have access to all hardware components in your device. However if you wish to utilise such solution you will have to deal with some caveeats:

  • You can't use Pascal for creating a Windows driver (at least not a reliable one, there might be opensource DDK's based on Pascal, but I don't know about any). If you wish to create a windows driver, you will have to use WDK (practically together with a powerful IDE like MSVS).
  • You will have limited access to the software environment of your computer. (Harder to read registry, more complex memory reservation etc.)
  • Writing a windows driver is a hell of a job. I did that a few times, and I would not wish it for my sworn enemies. (There are pointless conventions, outdated datatypes, underdocumented functionality... and so on.)
  • There are no huge component and API libraries to help you create your driver. On ring 0 you are almost completely on your own.
  • If a windows kernel driver is buggy, it won't throw an exception or fail gracefully. It will create a BSOD error and you have to reset your system to gain control over it again.

Update:

  • One of my friends in the open-source industry told me, that there is a way to target kernel mode drivers with project Lazarus.
  • Some further research showed maybe a filter-driver might also help the OP with his troubles. (And filter-drivers don't exist in ring-0.)
mg30rg
  • 1,311
  • 13
  • 24
  • Its possible without any other drivers, as it seems. http://stackoverflow.com/questions/2143439/play-wave-file-from-a-windows-service-c – That Marc Feb 17 '14 at 16:57
  • @JustMarc You asked about both audio and video – David Heffernan Feb 17 '14 at 17:01
  • @JustMarc Mkey. I did not "say" my solution was the only one, or the best one. I only said that this is a working solution, and I do not know any other. – mg30rg Feb 18 '14 at 08:15
  • @mg30rg I know. I just pointed that out, that your solution is kinda last-option - the hard way... :) Thanks for answer, of course. But I really don't feel like writing a driver at all.. not even close to that knowledge ;) – That Marc Feb 18 '14 at 23:05