Skip to content

Instantly share code, notes, and snippets.

Created January 6, 2014 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6d9661ffb769d21b5d6c to your computer and use it in GitHub Desktop.
Save anonymous/6d9661ffb769d21b5d6c to your computer and use it in GitHub Desktop.
#ifndef BAREBONESHANDLER_H_
#define BAREBONESHANDLER_H_
#include "include/cef_client.h"
class BareBonesHandler : public CefClient {
public:
BareBonesHandler() { }
~BareBonesHandler() { }
CefRefPtr<CefBrowser> m_Browser;
CefRefPtr<CefBrowser> GetBrowser() { return m_Browser; }
// Note that any of the IMPLEMENT_WHATEVER
// macros that come with CEF can (and do) set
// access modifiers, so you'll want them after
// everything else in your class or you may be
// in for a surprise when the access of a member
// isn't what you expect it to be!
IMPLEMENT_REFCOUNTING(BareBonesHandler);
};
#endif
#include <winsock2.h>//part of Nice
#include <Windows.h>
#undef GetNextSibling
#undef GetFirstChild
///parts of nice
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <agent.h>
///parts of nice
#include "include/cef_app.h"
#include "bareboneshandler.h"
// Define this, define that, define everything!
//#define TEST_SINGLE_THREADED_MESSAGE_LOOP
#define JS_BUTTON 1001
CefRefPtr<BareBonesHandler> cf_handler;
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void jprint(gchar *data);
static GMainLoop *gloop;
static guint stream_id;
static const gchar *candidate_type_name[] = {"host", "srflx", "prflx", "relay"};
static const gchar *state_name[] = {"disconnected", "gathering", "connecting",
"connected", "ready", "failed"};
int NiceCheck = 0;
static int print_local_data(NiceAgent *agent, guint stream_id,
guint component_id);
static int parse_remote_data(NiceAgent *agent, guint stream_id,
guint component_id, char *line);
static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id,
gpointer data);
static void cb_new_selected_pair(NiceAgent *agent, guint stream_id,
guint component_id, gchar *lfoundation,
gchar *rfoundation, gpointer data);
static void cb_component_state_changed(NiceAgent *agent, guint stream_id,
guint component_id, guint state,
gpointer data);
static void cb_nice_recv(NiceAgent *agent, guint stream_id, guint component_id,
guint len, gchar *buf, gpointer data);
static gboolean stdin_remote_info_cb (GIOChannel *source, GIOCondition cond,
gpointer data);
static gboolean stdin_send_data_cb (GIOChannel *source, GIOCondition cond,
gpointer data);
/////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASSEX wClass;
MSG Msg;
CefSettings settings;
cf_handler = new BareBonesHandler();
NiceAgent *agent;
gchar *stun_addr = NULL;
guint stun_port;
gboolean controlling;
CefMainArgs main_args(hInst);
ZeroMemory(&wClass,sizeof(WNDCLASSEX));
wClass.cbClsExtra=NULL;
wClass.cbSize=sizeof(WNDCLASSEX);
wClass.cbWndExtra=NULL;
wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wClass.hIcon=NULL;
wClass.hIconSm=NULL;
wClass.hInstance=hInst;
wClass.lpfnWndProc=(WNDPROC)WinProc;
wClass.lpszClassName=L"Window Class";
wClass.lpszMenuName=NULL;
wClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClassEx(&wClass))
{
int nResult=GetLastError();
MessageBox(NULL,
L"Window class creation failed",
L"Window Class Failed",
MB_ICONERROR);
}
int exitCode = CefExecuteProcess(main_args, NULL);
if (exitCode >= 0) {
return exitCode;
}
CefInitialize(main_args, settings, NULL);
//Create gloop
g_type_init();
gloop = g_main_loop_new(NULL, FALSE);
// Create the nice agent
agent = nice_agent_new(g_main_loop_get_context (gloop),
NICE_COMPATIBILITY_RFC5245);
if (agent == NULL)
g_error("Failed to create agent");
//
HWND hWnd=CreateWindowEx(NULL,
L"Window Class",
L"Testing Viewium",
WS_OVERLAPPEDWINDOW,
200,
200,
1280,
768,
NULL,
NULL,
hInst,
NULL);
if(!hWnd)
{
int nResult=GetLastError();
MessageBox(NULL,
L"Window creation failed",
L"Window Creation Failed",
MB_ICONERROR);
}
ShowWindow(hWnd,nShowCmd);
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
// Set the STUN settings and controlling mode
stun_addr = "77.72.174.163";
stun_port = 3478;
controlling = 1;
g_debug("Using stun server '[%s]:%u'\n", stun_addr, stun_port);
g_object_set(G_OBJECT(agent), "stun-server", stun_addr, NULL);
g_object_set(G_OBJECT(agent), "stun-server-port", stun_port, NULL);
g_object_set(G_OBJECT(agent), "controlling-mode", controlling, NULL);
// Connect to the signals
g_signal_connect(G_OBJECT(agent), "candidate-gathering-done",
G_CALLBACK(cb_candidate_gathering_done), NULL);
/*g_signal_connect(G_OBJECT(agent), "new-selected-pair",
G_CALLBACK(cb_new_selected_pair), NULL);
g_signal_connect(G_OBJECT(agent), "component-state-changed",
G_CALLBACK(cb_component_state_changed), NULL);
*/
// Create a new stream with one component
stream_id = nice_agent_add_stream(agent, 1);
if (stream_id == 0)
g_error("Failed to add stream");
// Attach to the component to receive the data
// Without this call, candidates cannot be gathered
nice_agent_attach_recv(agent, stream_id, 1,
g_main_loop_get_context (gloop), cb_nice_recv, NULL);
// Start gathering local candidates
if (!nice_agent_gather_candidates(agent, stream_id))
g_error("Failed to start candidate gathering");
g_debug("waiting for candidate-gathering-done signal...");
// Run the mainloop. Everything else will happen asynchronously
//CefRunMessageLoop();
//Loop integration Test
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
CefDoMessageLoopWork();
g_main_context_iteration(g_main_loop_get_context (gloop), FALSE);
}
return Msg.wParam;
///
CefShutdown();
return 0;
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static HWND hJSButton;
LONG btny;
switch(msg)
{
case WM_DESTROY:
{
cf_handler->m_Browser = NULL;
CefQuitMessageLoop();
PostQuitMessage(0);
return 0;
}
break;
case WM_CREATE:
{
RECT rect;
GetClientRect(hWnd, &rect);
CefWindowInfo info;
rect.bottom = rect.bottom - 100;
info.SetAsChild(hWnd, rect);
CefBrowserSettings settings;
cf_handler->m_Browser = CefBrowserHost::CreateBrowserSync(info, cf_handler.get(),
"http://localhost:4242/chattest", settings);
/*cf_handler->m_Browser = CefBrowserHost::CreateBrowserSync(info, cf_handler.get(),
"http://www.youtube.com/watch?v=MyUC_28HIvA", settings);*/
btny = rect.bottom + 25;
hJSButton = CreateWindow( L"button", L"JSButton",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
50, btny,
100, 50,
hWnd, (HMENU) JS_BUTTON,
NULL, NULL );
}
break;
case WM_COMMAND:
{
switch(wParam)
{
case JS_BUTTON:
jprint("JS CEF Keep on Rockin!");
return 0;
}
break;
}
break;
}
CefRefPtr<CefBrowser> browser;
CefRefPtr<CefFrame> frame;
CefRefPtr<CefV8Context> context;
if (cf_handler.get()) {browser = cf_handler->GetBrowser();}
if (browser.get()) {frame = browser->GetMainFrame();}
if (frame.get()) {context = frame->GetV8Context();}
if (context.get()) {CefRefPtr<CefV8Value> windowobj = context->GetGlobal();
CefRefPtr<CefV8Value> GetPloaded = windowobj->GetValue("Ploaded");
int Ploaded = GetPloaded->GetIntValue();
if (Ploaded == 1 && NiceCheck == 1) {
context->GetGlobal()->SetValue("Ploaded", CefV8Value::CreateInt(0), V8_PROPERTY_ATTRIBUTE_NONE);
NiceCheck = 0;
jprint("Page is loaded - CEF");
}
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
void jprint(gchar *data) {
CefRefPtr<CefBrowser> browser;
if (cf_handler.get())
browser = cf_handler->GetBrowser();
if (browser.get()) {
CefRefPtr<CefFrame> frame = browser->GetMainFrame();
CefRefPtr<CefV8Context> context = frame->GetV8Context();
gchar *jprint = g_strdup_printf("%s%s%s", "print('", data, "');");
frame->ExecuteJavaScript(jprint,
frame->GetURL(), 0);
}
}
static void cb_nice_recv(NiceAgent *agent, guint stream_id, guint component_id,
guint len, gchar *buf, gpointer data)
{
if (len == 1 && buf[0] == '\0')
g_main_loop_quit (gloop);
printf("%.*s", len, buf);
fflush(stdout);
}
static void
cb_candidate_gathering_done(NiceAgent *agent, guint stream_id,
gpointer data)
{
CefRefPtr<CefBrowser> browser;
if (cf_handler.get())
browser = cf_handler->GetBrowser();
jprint("SIGNAL candidate gathering done");
NiceCheck = 1;
// Candidate gathering is done. Send our local candidates on stdout
jprint("Copy this line to remote client:");
print_local_data(agent, stream_id, 1);
// Listen on stdin for the remote candidate list
/*printf("Enter remote data (single line, no wrapping):\n");
g_io_add_watch(io_stdin, G_IO_IN, stdin_remote_info_cb, agent);
printf("> ");
fflush (stdout);*/
}
static int
print_local_data (NiceAgent *agent, guint stream_id, guint component_id)
{
int result = EXIT_FAILURE;
gchar *data = NULL;
gchar *local_ufrag = NULL;
gchar *local_password = NULL;
gchar ipaddr[INET6_ADDRSTRLEN];
GSList *cands = NULL, *item;
if (!nice_agent_get_local_credentials(agent, stream_id,
&local_ufrag, &local_password))
goto end;
cands = nice_agent_get_local_candidates(agent, stream_id, component_id);
if (cands == NULL)
goto end;
//printf("%s %s", local_ufrag, local_password);
for (item = cands; item; item = item->next) {
NiceCandidate *c = (NiceCandidate *)item->data;
nice_address_to_string(&c->addr, ipaddr);
// (foundation),(prio),(addr),(port),(type)
data = g_strdup_printf("%s %s %s,%u,%s,%u,%s",
local_ufrag,
local_password,
c->foundation,
c->priority,
ipaddr,
nice_address_get_port(&c->addr),
candidate_type_name[c->type]);
}
jprint(data);
result = EXIT_SUCCESS;
end:
if (data)
g_free(data);
if (local_ufrag)
g_free(local_ufrag);
if (local_password)
g_free(local_password);
if (cands)
g_slist_free_full(cands, (GDestroyNotify)&nice_candidate_free);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment