chromecast: start the communication when the control class is created
[vlc.git] / modules / stream_out / chromecast / chromecast.h
1 /*****************************************************************************
2  * chromecast.cpp: Chromecast module for vlc
3  *****************************************************************************
4  * Copyright © 2014-2015 VideoLAN
5  *
6  * Authors: Adrien Maglo <magsoft@videolan.org>
7  *          Jean-Baptiste Kempf <jb@videolan.org>
8  *          Steve Lhomme <robux4@videolabs.io>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifndef VLC_CHROMECAST_H
30 #define VLC_CHROMECAST_H
31
32 #include <vlc_common.h>
33 #include <vlc_interface.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_tls.h>
37
38 #include <sstream>
39
40 #include "cast_channel.pb.h"
41
42 #define PACKET_HEADER_LEN 4
43
44 // Media player Chromecast app id
45 static const std::string DEFAULT_CHOMECAST_RECEIVER = "receiver-0";
46 /* see https://developers.google.com/cast/docs/reference/messages */
47 static const std::string NAMESPACE_MEDIA            = "urn:x-cast:com.google.cast.media";
48
49 #define HTTP_PORT               8010
50
51 // Status
52 enum connection_status
53 {
54     CHROMECAST_DISCONNECTED,
55     CHROMECAST_TLS_CONNECTED,
56     CHROMECAST_AUTHENTICATED,
57     CHROMECAST_APP_STARTED,
58     CHROMECAST_CONNECTION_DEAD,
59 };
60
61 enum command_status {
62     NO_CMD_PENDING,
63     CMD_LOAD_SENT,
64     CMD_PLAYBACK_SENT,
65     CMD_SEEK_SENT,
66 };
67
68 enum receiver_state {
69     RECEIVER_IDLE,
70     RECEIVER_PLAYING,
71     RECEIVER_BUFFERING,
72     RECEIVER_PAUSED,
73 };
74
75
76 /*****************************************************************************
77  * intf_sys_t: description and status of interface
78  *****************************************************************************/
79 struct intf_sys_t
80 {
81     intf_sys_t(vlc_object_t * const p_this, int local_port, std::string device_addr, int device_port = 0);
82     ~intf_sys_t();
83
84     vlc_object_t  * const p_module;
85     const int      i_port;
86     std::string    serverIP;
87     const int      i_target_port;
88     std::string    targetIP;
89     std::string    mime;
90
91     std::string appTransportId;
92     std::string mediaSessionId;
93     receiver_state receiverState;
94
95     int i_sock_fd;
96     vlc_tls_creds_t *p_creds;
97     vlc_tls_t *p_tls;
98
99     vlc_mutex_t  lock;
100     vlc_cond_t   loadCommandCond;
101     vlc_thread_t chromecastThread;
102
103     void msgAuth();
104     void msgReceiverClose(std::string destinationId);
105
106     void handleMessages();
107
108     connection_status getConnectionStatus() const
109     {
110         return conn_status;
111     }
112
113     void setConnectionStatus(connection_status status)
114     {
115         if (conn_status != status)
116         {
117 #ifndef NDEBUG
118             msg_Dbg(p_module, "change Chromecast connection status from %d to %d", conn_status, status);
119 #endif
120             conn_status = status;
121             vlc_cond_broadcast(&loadCommandCond);
122         }
123     }
124
125     int connectChromecast();
126     void disconnectChromecast();
127
128     void msgPing();
129     void msgPong();
130     void msgConnect(const std::string & destinationId = DEFAULT_CHOMECAST_RECEIVER);
131
132     void msgReceiverLaunchApp();
133     void msgReceiverGetStatus();
134
135     void msgPlayerLoad();
136     void msgPlayerPlay();
137     void msgPlayerStop();
138     void msgPlayerPause();
139     void msgPlayerGetStatus();
140     void msgPlayerSeek(const std::string & currentTime);
141     void msgPlayerSetVolume(float volume);
142     void msgPlayerSetMute(bool mute);
143
144     void processMessage(const castchannel::CastMessage &msg);
145
146     command_status getPlayerStatus() const
147     {
148         return cmd_status;
149     }
150
151 private:
152     int sendMessage(const castchannel::CastMessage &msg);
153
154     void buildMessage(const std::string & namespace_,
155                       const std::string & payload,
156                       const std::string & destinationId = DEFAULT_CHOMECAST_RECEIVER,
157                       castchannel::CastMessage_PayloadType payloadType = castchannel::CastMessage_PayloadType_STRING);
158
159     void pushMediaPlayerMessage(const std::stringstream & payload);
160
161     void setPlayerStatus(enum command_status status) {
162         if (cmd_status != status)
163         {
164             msg_Dbg(p_module, "change Chromecast command status from %d to %d", cmd_status, status);
165             cmd_status = status;
166         }
167     }
168
169     enum connection_status conn_status;
170     enum command_status    cmd_status;
171
172     unsigned i_receiver_requestId;
173     unsigned i_requestId;
174
175     static void* ChromecastThread(void* p_data);
176 };
177
178 #endif /* VLC_CHROMECAST_H */