|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 #if !defined(MediaResourceServer_h_) |
|
7 #define MediaResourceServer_h_ |
|
8 |
|
9 #include <map> |
|
10 #include "nsIServerSocket.h" |
|
11 #include "MediaResource.h" |
|
12 |
|
13 namespace mozilla { |
|
14 |
|
15 class MediaResource; |
|
16 |
|
17 /* |
|
18 MediaResourceServer instantiates a socket server that understands |
|
19 HTTP requests for MediaResource instances. The server runs on an |
|
20 automatically selected port and MediaResource instances are registered. |
|
21 The registration returns a string URL than can be used to fetch the |
|
22 resource. That URL contains a randomly generated path to make it |
|
23 difficult for other local applications on the device to guess it. |
|
24 |
|
25 The HTTP protocol is limited in that it supports only what the |
|
26 Android DataSource implementation uses to fetch media. It |
|
27 understands HTTP GET and byte range requests. |
|
28 |
|
29 The intent of this class is to be used in Media backends that |
|
30 have a system component that does its own network requests. These |
|
31 requests are made against this server which then uses standard |
|
32 Gecko network requests and media cache usage. |
|
33 |
|
34 The MediaResourceServer can be instantiated on any thread and |
|
35 its methods are threadsafe - they can be called on any thread. |
|
36 The server socket itself is always run on the main thread and |
|
37 this is done by the Start() static method by synchronously |
|
38 dispatching to the main thread. |
|
39 */ |
|
40 class MediaResourceServer : public nsRunnable |
|
41 { |
|
42 private: |
|
43 // Mutex protecting private members of MediaResourceServer. |
|
44 // All member variables below this point in the class definition |
|
45 // must acquire the mutex before access. |
|
46 mozilla::Mutex mMutex; |
|
47 |
|
48 // Server socket used to listen for incoming connections |
|
49 nsCOMPtr<nsIServerSocket> mSocket; |
|
50 |
|
51 // Mapping between MediaResource URL's to the MediaResource |
|
52 // object served at that URL. |
|
53 typedef std::map<nsCString, |
|
54 nsRefPtr<mozilla::MediaResource> > ResourceMap; |
|
55 ResourceMap mResources; |
|
56 |
|
57 // Create a MediaResourceServer that will listen on an automatically |
|
58 // selected port when started. This is private as it should only be |
|
59 // called internally from the public 'Start' method. |
|
60 MediaResourceServer(); |
|
61 NS_IMETHOD Run(); |
|
62 |
|
63 // Append a random URL path to a string. This is used for creating a |
|
64 // unique URl for a resource which helps prevent malicious software |
|
65 // running on the same machine as the server from guessing the URL |
|
66 // and accessing video data. |
|
67 nsresult AppendRandomPath(nsCString& aURL); |
|
68 |
|
69 public: |
|
70 // Create a MediaResourceServer and start it listening. This call will |
|
71 // perform a synchronous request on the main thread. |
|
72 static already_AddRefed<MediaResourceServer> Start(); |
|
73 |
|
74 // Stops the server from listening and accepting further connections. |
|
75 void Stop(); |
|
76 |
|
77 // Add a MediaResource to be served by this server. Stores the |
|
78 // absolute URL that can be used to access the resource in 'aUrl'. |
|
79 nsresult AddResource(mozilla::MediaResource* aResource, nsCString& aUrl); |
|
80 |
|
81 // Remove a MediaResource so it is no longer served by this server. |
|
82 // The URL provided must match exactly that provided by a previous |
|
83 // call to "AddResource". |
|
84 void RemoveResource(nsCString const& aUrl); |
|
85 |
|
86 // Returns the prefix for HTTP requests to the server. This plus |
|
87 // the result of AddResource results in an Absolute URL. |
|
88 nsCString GetURLPrefix(); |
|
89 |
|
90 // Returns the resource asociated with a given URL |
|
91 already_AddRefed<mozilla::MediaResource> GetResource(nsCString const& aUrl); |
|
92 }; |
|
93 |
|
94 } // namespace mozilla |
|
95 |
|
96 #endif |