After having demonstrated how to implement a DLNA Media Renderer in the last blog, I show that it is equally simple to define a DLNA media server.

Here is the outline of a basic sketch:

#include "WiFi.h"
#include "DLNA.h"

// Replace with your WiFi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

const int port = 9000;
WiFiServer wifi(port);
HttpServer<WiFiClient, WiFiServer> server(wifi);
UDPService<WiFiUDP> udp;
DLNAMediaServer<WiFiClient> mediaServer(server, udp);

void setup() {
  Serial.begin(115200);
  DlnaLogger.begin(Serial, DlnaLogLevel::Warning);

  setupWifi();

  // define local base URL based on assigned IP address
  mediaServer.setFriendlyName("ArduinoMediaServer");
  mediaServer.setBaseURL(WiFi.localIP(), port);
  mediaServer.setReference(nullptr);  // not needed for this example
  mediaServer.setPrepareDataCallback(myPrepareData);
  mediaServer.setGetDataCallback(myGetData);

  if (mediaServer.begin()) {
    Serial.println("MediaServer started");
  } else {
    Serial.println("MediaServer failed to start");
  }
}

void loop() { mediaServer.loop(); }
Code language: PHP (php)

The logic for setting up and starting a server is basically the same like the one for the Media Renderer just using the DLNAMediaServer class: The key for this scenario however are two callbacks: one for preparing and the other for providing the data:

// prepare the data returinng the numberReturned and totalMatches
void myPrepareData(const char* objectID, ContentQueryType queryType,
                   const char* filter, int startingIndex, int requestedCount,
                   const char* sortCriteria, int& numberReturned,
                   int& totalMatches, int& updateID, void* reference) {
 // prepare the data using                 
}

// GetData callback: returns each item by index
bool myGetData(int index, MediaItem& item, void* reference) {
  // fill the MediaItem for the requested index
}
Code language: JavaScript (javascript)

In addition you might need to configure the webserver to render the file indicated by the resourceURI that you provide in the MediaItem.

Complete Examples

I am providing an example that just uses some predefined urls to internet radios and an example that loads the data from an SD to a tree using the SDFAT library with the approach described in my last blog.

I was testing this solution with an ESP32 using a ControlPoint in Linux, however I noticed that it is too slow to be usefull for handling many directories. So there is the need to extend the functionality to support handling the webserver in a separate task.

Categories: Arduino

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *